blob: 4f4df5dd0a4526dcf1282df8bfa05967c6bf4675 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040023import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070024import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040025import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070026import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040028import android.annotation.SdkConstant;
29import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040030import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010031import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010034import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040035import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020036import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050037import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020038import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070039import android.content.res.Configuration;
40import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040041import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040042import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070044import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010045import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010046import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040047import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040048import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070049import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080050import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070051import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040053import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020054import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050055import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Parcel;
58import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040059import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080060import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070061import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070062import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.SpannableStringBuilder;
64import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080067import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070068import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080069import android.text.style.RelativeSizeSpan;
70import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070071import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040072import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010073import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050074import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070075import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070076import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080077import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080078import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070079import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070080import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.widget.RemoteViews;
82
Griff Hazen959591e2014-05-15 22:26:18 -070083import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070084import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070085import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070086import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080087import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070088
Tor Norbyed9273d62013-05-30 15:59:53 -070089import java.lang.annotation.Retention;
90import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020091import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070093import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070094import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070095import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050096import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050097import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060098import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100/**
101 * A class that represents how a persistent notification is to be presented to
102 * the user using the {@link android.app.NotificationManager}.
103 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800104 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
105 * easier to construct Notifications.</p>
106 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For a guide to creating notifications, read the
110 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
111 * developer guide.</p>
112 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
114public class Notification implements Parcelable
115{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400116 private static final String TAG = "Notification";
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400119 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400121 */
122 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
123 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
124 = "android.intent.category.NOTIFICATION_PREFERENCES";
125
126 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500130 */
131 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
132
133 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400135 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
136 * what settings should be shown in the target app.
137 */
138 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
146
147 /**
148 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
149 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
150 * that can be used to narrow down what settings should be shown in the target app.
151 */
152 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * Use all default values (where applicable).
156 */
157 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Use the default notification sound. This will ignore any given
161 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that is noisy is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_SOUND = 1;
171
172 /**
173 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700175 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
Chris Wren47c20a12014-06-18 17:27:29 -0400177 * <p>
178 * A notification that vibrates is more likely to be presented as a heads-up notification.
179 * </p>
180 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 */
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 /**
187 * Use the default notification lights. This will ignore the
188 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
189 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192 */
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200197 * Maximum length of CharSequences accepted by Builder and friends.
198 *
199 * <p>
200 * Avoids spamming the system with overly large strings such as full e-mails.
201 */
202 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
203
204 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800205 * Maximum entries of reply text that are accepted by Builder and friends.
206 */
207 private static final int MAX_REPLY_HISTORY = 5;
208
Tony Mak09db2ea2018-06-27 18:12:48 +0100209 /**
210 * Maximum numbers of action buttons in a notification.
211 * @hide
212 */
213 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800214
215 /**
216 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
217 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
218 *
219 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
220 * sends messages.</p>
221 */
222 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
223
Adrian Roose458aa82015-12-08 16:17:19 -0800224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * Default value: {@link System#currentTimeMillis() Now}.
228 *
229 * Choose a timestamp that will be most relevant to the user. For most finite events, this
230 * corresponds to the time the event happened (or will happen, in the case of events that have
231 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * timestamped according to when the activity began.
233 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500234 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800235 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500236 * <ul>
237 * <li>Notification of a new chat message should be stamped when the message was received.</li>
238 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
239 * <li>Notification of a completed file download should be stamped when the download finished.</li>
240 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
241 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
242 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800243 * </ul>
244 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700245 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
246 * anymore by default and must be opted into by using
247 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 */
249 public long when;
250
251 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700252 * The creation time of the notification
253 */
254 private long creationTime;
255
256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400258 *
259 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Dan Sandler86647982015-05-13 23:41:13 -0400261 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700262 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public int icon;
264
265 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
267 * leave it at its default value of 0.
268 *
269 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700270 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 */
272 public int iconLevel;
273
274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500275 * The number of events that this notification represents. For example, in a new mail
276 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800277 *
Julia Reynolds30331982017-04-27 10:12:50 -0400278 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500279 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
280 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
Julia Reynolds30331982017-04-27 10:12:50 -0400282 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 /**
285 * The intent to execute when the expanded status entry is clicked. If
286 * this is an activity, it must include the
287 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800288 * that you take care of task management as described in the
289 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800290 * Stack</a> document. In particular, make sure to read the notification section
291 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
292 * Notifications</a> for the correct ways to launch an application from a
293 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent contentIntent;
296
297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500298 * The intent to execute when the notification is explicitly dismissed by the user, either with
299 * the "Clear All" button or by swiping it away individually.
300 *
301 * This probably shouldn't be launching an activity since several of those will be sent
302 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 public PendingIntent deleteIntent;
305
306 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700307 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800308 *
Chris Wren47c20a12014-06-18 17:27:29 -0400309 * <p>
310 * The system UI may choose to display a heads-up notification, instead of
311 * launching this intent, while the user is using the device.
312 * </p>
313 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800314 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400315 */
316 public PendingIntent fullScreenIntent;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Text that summarizes this notification for accessibility services.
320 *
321 * As of the L release, this text is no longer shown on screen, but it is still useful to
322 * accessibility services (where it serves as an audible announcement of the notification's
323 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
327 public CharSequence tickerText;
328
329 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400330 * Formerly, a view showing the {@link #tickerText}.
331 *
332 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400333 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800335 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400336
337 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400338 * The view that will represent this notification in the notification list (which is pulled
339 * down from the status bar).
340 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500341 * As of N, this field may be null. The notification view is determined by the inputs
342 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 public RemoteViews contentView;
347
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400348 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400349 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400350 * opportunity to show more detail. The system UI may choose to show this
351 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400352 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500353 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400354 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
355 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400356 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400358 public RemoteViews bigContentView;
359
Chris Wren8fd39ec2014-02-27 17:43:26 -0500360
361 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400362 * A medium-format version of {@link #contentView}, providing the Notification an
363 * opportunity to add action buttons to contentView. At its discretion, the system UI may
364 * choose to show this as a heads-up notification, which will pop up so the user can see
365 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400366 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500367 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400368 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
369 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500370 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400371 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500372 public RemoteViews headsUpContentView;
373
Julia Reynoldsfc640012018-02-21 12:25:27 -0500374 private boolean mUsesStandardHeader;
375
376 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
377 static {
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
389 }
390
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400391 /**
Dan Sandler86647982015-05-13 23:41:13 -0400392 * A large bitmap to be shown in the notification content area.
393 *
394 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
Dan Sandler86647982015-05-13 23:41:13 -0400396 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800397 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
399 /**
400 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400403 * A notification that is noisy is more likely to be presented as a heads-up notification.
404 * </p>
405 *
406 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500407 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public Uri sound;
413
414 /**
415 * Use this constant as the value for audioStreamType to request that
416 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700417 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public static final int STREAM_DEFAULT = -1;
423
424 /**
425 * The audio stream type to use when playing the sound.
426 * Should be one of the STREAM_ constants from
427 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400428 *
429 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public int audioStreamType = STREAM_DEFAULT;
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400435 * The default value of {@link #audioAttributes}.
436 */
437 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
438 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
439 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
440 .build();
441
442 /**
443 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 *
445 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400446 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400448 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
449
450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500451 * The pattern with which to vibrate.
452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * <p>
454 * To vibrate the default pattern, see {@link #defaults}.
455 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 public long[] vibrate;
462
463 /**
464 * The color of the led. The hardware will do its best approximation.
465 *
466 * @see #FLAG_SHOW_LIGHTS
467 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500468 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Tor Norbye80756e32015-03-02 09:39:27 -0800470 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int ledARGB;
473
474 /**
475 * The number of milliseconds for the LED to be on while it's flashing.
476 * The hardware will do its best approximation.
477 *
478 * @see #FLAG_SHOW_LIGHTS
479 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500480 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 public int ledOnMS;
484
485 /**
486 * The number of milliseconds for the LED to be off while it's flashing.
487 * The hardware will do its best approximation.
488 *
489 * @see #FLAG_SHOW_LIGHTS
490 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 *
492 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 public int ledOffMS;
496
497 /**
498 * Specifies which values should be taken from the defaults.
499 * <p>
500 * To set, OR the desired from {@link #DEFAULT_SOUND},
501 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
502 * values, use {@link #DEFAULT_ALL}.
503 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500504 *
505 * @deprecated use {@link NotificationChannel#getSound()} and
506 * {@link NotificationChannel#shouldShowLights()} and
507 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500509 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public int defaults;
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
514 * set if you want the LED on for this notification.
515 * <ul>
516 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
517 * or 0 for both ledOnMS and ledOffMS.</li>
518 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
519 * <li>To flash the LED, pass the number of milliseconds that it should
520 * be on and off to ledOnMS and ledOffMS.</li>
521 * </ul>
522 * <p>
523 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800524 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * because they will be set to values that work on any given hardware.
526 * <p>
527 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500528 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500529 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that should be
536 * set if this notification is in reference to something that is ongoing,
537 * like a phone call. It should not be set if this notification is in
538 * reference to something that happened at a particular point in time,
539 * like a missed phone call.
540 */
541 public static final int FLAG_ONGOING_EVENT = 0x00000002;
542
543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700545 * the audio will be repeated until the notification is
546 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 */
548 public static final int FLAG_INSISTENT = 0x00000004;
549
550 /**
551 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700552 * set if you would only like the sound, vibrate and ticker to be played
553 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 */
555 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
556
557 /**
558 * Bit to be bitwise-ored into the {@link #flags} field that should be
559 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500560 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 */
562 public static final int FLAG_AUTO_CANCEL = 0x00000010;
563
564 /**
565 * Bit to be bitwise-ored into the {@link #flags} field that should be
566 * set if the notification should not be canceled when the user clicks
567 * the Clear all button.
568 */
569 public static final int FLAG_NO_CLEAR = 0x00000020;
570
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700571 /**
572 * Bit to be bitwise-ored into the {@link #flags} field that should be
573 * set if this notification represents a currently running service. This
574 * will normally be set for you by {@link Service#startForeground}.
575 */
576 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
577
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400578 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800580 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400582 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700583 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400585
Griff Hazendfcb0802014-02-11 12:00:00 -0800586 /**
587 * Bit to be bitswise-ored into the {@link #flags} field that should be
588 * set if this notification is relevant to the current device only
589 * and it is not recommended that it bridge to other devices.
590 */
591 public static final int FLAG_LOCAL_ONLY = 0x00000100;
592
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700593 /**
594 * Bit to be bitswise-ored into the {@link #flags} field that should be
595 * set if this notification is the group summary for a group of notifications.
596 * Grouped notifications may display in a cluster or stack on devices which
597 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
598 */
599 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
600
Julia Reynoldse46bb372016-03-17 11:05:58 -0400601 /**
602 * Bit to be bitswise-ored into the {@link #flags} field that should be
603 * set if this notification is the group summary for an auto-group of notifications.
604 *
605 * @hide
606 */
607 @SystemApi
608 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
609
Julia Reynolds4db59552017-06-30 13:34:01 -0400610 /**
611 * @hide
612 */
613 public static final int FLAG_CAN_COLORIZE = 0x00000800;
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public int flags;
616
Tor Norbyed9273d62013-05-30 15:59:53 -0700617 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700618 @IntDef(prefix = { "PRIORITY_" }, value = {
619 PRIORITY_DEFAULT,
620 PRIORITY_LOW,
621 PRIORITY_MIN,
622 PRIORITY_HIGH,
623 PRIORITY_MAX
624 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700625 @Retention(RetentionPolicy.SOURCE)
626 public @interface Priority {}
627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 * Default notification {@link #priority}. If your application does not prioritize its own
630 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_DEFAULT = 0;
636
637 /**
638 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
639 * items smaller, or at a different position in the list, compared with your app's
640 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_LOW = -1;
646
647 /**
648 * Lowest {@link #priority}; these items might not be shown to the user except under special
649 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 *
651 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500653 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 public static final int PRIORITY_MIN = -2;
655
656 /**
657 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
658 * show these items larger, or at a different position in notification lists, compared with
659 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 *
661 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500663 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 public static final int PRIORITY_HIGH = 1;
665
666 /**
667 * Highest {@link #priority}, for your application's most important items that require the
668 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500669 *
670 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500672 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500673 public static final int PRIORITY_MAX = 2;
674
675 /**
676 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500678 * Priority is an indication of how much of the user's valuable attention should be consumed by
679 * this notification. Low-priority notifications may be hidden from the user in certain
680 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500681 * system will make a determination about how to interpret this priority when presenting
682 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400683 *
684 * <p>
685 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
686 * as a heads-up notification.
687 * </p>
688 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500689 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700691 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500692 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800694
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 /**
696 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
697 * to be applied by the standard Style templates when presenting this notification.
698 *
699 * The current template design constructs a colorful header image by overlaying the
700 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
701 * ignored.
702 */
Tor Norbye80756e32015-03-02 09:39:27 -0800703 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400704 public int color = COLOR_DEFAULT;
705
706 /**
707 * Special value of {@link #color} telling the system not to decorate this notification with
708 * any special color but instead use default colors when presenting this notification.
709 */
Tor Norbye80756e32015-03-02 09:39:27 -0800710 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400711 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600712
713 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800714 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800715 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800716 */
717 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800718 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800719
720 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700721 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
722 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600723 * lockscreen).
724 *
725 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
726 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
727 * shown in all situations, but the contents are only available if the device is unlocked for
728 * the appropriate user.
729 *
730 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
731 * can be read even in an "insecure" context (that is, above a secure lockscreen).
732 * To modify the public version of this notification—for example, to redact some portions—see
733 * {@link Builder#setPublicVersion(Notification)}.
734 *
735 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
736 * and ticker until the user has bypassed the lockscreen.
737 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600738 public @Visibility int visibility;
739
740 /** @hide */
741 @IntDef(prefix = { "VISIBILITY_" }, value = {
742 VISIBILITY_PUBLIC,
743 VISIBILITY_PRIVATE,
744 VISIBILITY_SECRET,
745 })
746 @Retention(RetentionPolicy.SOURCE)
747 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600748
Griff Hazenfc3922d2014-08-20 11:56:44 -0700749 /**
750 * Notification visibility: Show this notification in its entirety on all lockscreens.
751 *
752 * {@see #visibility}
753 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600754 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700755
756 /**
757 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
758 * private information on secure lockscreens.
759 *
760 * {@see #visibility}
761 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600762 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700763
764 /**
765 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
766 *
767 * {@see #visibility}
768 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600769 public static final int VISIBILITY_SECRET = -1;
770
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775
776 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700777 * Notification category: map turn-by-turn navigation.
778 */
779 public static final String CATEGORY_NAVIGATION = "navigation";
780
781 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800
801 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500803 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 public static final String CATEGORY_ALARM = "alarm";
805
806 /**
807 * Notification category: progress of a long-running background operation.
808 */
809 public static final String CATEGORY_PROGRESS = "progress";
810
811 /**
812 * Notification category: social network or sharing update.
813 */
814 public static final String CATEGORY_SOCIAL = "social";
815
816 /**
817 * Notification category: error in background operation or authentication status.
818 */
819 public static final String CATEGORY_ERROR = "err";
820
821 /**
822 * Notification category: media transport control for playback.
823 */
824 public static final String CATEGORY_TRANSPORT = "transport";
825
826 /**
827 * Notification category: system or device status update. Reserved for system use.
828 */
829 public static final String CATEGORY_SYSTEM = "sys";
830
831 /**
832 * Notification category: indication of running background service.
833 */
834 public static final String CATEGORY_SERVICE = "service";
835
836 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400837 * Notification category: a specific, timely recommendation for a single thing.
838 * For example, a news app might want to recommend a news story it believes the user will
839 * want to read next.
840 */
841 public static final String CATEGORY_RECOMMENDATION = "recommendation";
842
843 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400844 * Notification category: ongoing information about device or contextual status.
845 */
846 public static final String CATEGORY_STATUS = "status";
847
848 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400849 * Notification category: user-scheduled reminder.
850 */
851 public static final String CATEGORY_REMINDER = "reminder";
852
853 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700854 * Notification category: extreme car emergencies.
855 * @hide
856 */
857 @SystemApi
858 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
859
860 /**
861 * Notification category: car warnings.
862 * @hide
863 */
864 @SystemApi
865 public static final String CATEGORY_CAR_WARNING = "car_warning";
866
867 /**
868 * Notification category: general car system information.
869 * @hide
870 */
871 @SystemApi
872 public static final String CATEGORY_CAR_INFORMATION = "car_information";
873
874 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400875 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
876 * that best describes this Notification. May be used by the system for ranking and filtering.
877 */
878 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500879
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100880 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700881 private String mGroupKey;
882
883 /**
884 * Get the key used to group this notification into a cluster or stack
885 * with other notifications on devices which support such rendering.
886 */
887 public String getGroup() {
888 return mGroupKey;
889 }
890
891 private String mSortKey;
892
893 /**
894 * Get a sort key that orders this notification among other notifications from the
895 * same package. This can be useful if an external sort was already applied and an app
896 * would like to preserve this. Notifications will be sorted lexicographically using this
897 * value, although providing different priorities in addition to providing sort key may
898 * cause this value to be ignored.
899 *
900 * <p>This sort key can also be used to order members of a notification group. See
901 * {@link Builder#setGroup}.
902 *
903 * @see String#compareTo(String)
904 */
905 public String getSortKey() {
906 return mSortKey;
907 }
908
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500909 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 * <p>
912 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
913 * APIs, and are intended to be used by
914 * {@link android.service.notification.NotificationListenerService} implementations to extract
915 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public Bundle extras = new Bundle();
918
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700920 * All pending intents in the notification as the system needs to be able to access them but
921 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700922 * custom parcelable objects.
923 *
924 * @hide
925 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100926 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700927 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700928
929 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700930 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
931 * pending intents inside of it, so only those will get the behavior.
932 *
933 * @hide
934 */
Adrian Roosfb921842017-10-26 14:49:56 +0200935 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700936
937 /**
938 * Must be set by a process to start associating tokens with Notification objects
939 * coming in to it. This is set by NotificationManagerService.
940 *
941 * @hide
942 */
943 static public IBinder processWhitelistToken;
944
945 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946 * {@link #extras} key: this is the title of the notification,
947 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
948 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500949 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is the title of the notification when shown in expanded form,
953 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
958 * {@link #extras} key: this is the main text payload, as supplied to
959 * {@link Builder#setContentText(CharSequence)}.
960 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500961 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962
963 /**
964 * {@link #extras} key: this is a third line of text, as supplied to
965 * {@link Builder#setSubText(CharSequence)}.
966 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400967 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400968
969 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800970 * {@link #extras} key: this is the remote input history, as supplied to
971 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700972 *
973 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
974 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
975 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
976 * notifications once the other party has responded).
977 *
978 * The extra with this key is of type CharSequence[] and contains the most recent entry at
979 * the 0 index, the second most recent at the 1 index, etc.
980 *
981 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800982 */
983 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
984
985 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100986 * {@link #extras} key: boolean as supplied to
987 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
988 *
989 * If set to true, then the view displaying the remote input history from
990 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
991 *
992 * @see Builder#setShowRemoteInputSpinner(boolean)
993 * @hide
994 */
995 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
996
997 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100998 * {@link #extras} key: boolean as supplied to
999 * {@link Builder#setHideSmartReplies(boolean)}.
1000 *
1001 * If set to true, then any smart reply buttons will be hidden.
1002 *
1003 * @see Builder#setHideSmartReplies(boolean)
1004 * @hide
1005 */
1006 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: this is a small piece of additional text as supplied to
1010 * {@link Builder#setContentInfo(CharSequence)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a line of summary information intended to be shown
1016 * alongside expanded notifications, as supplied to (e.g.)
1017 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020
1021 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001022 * {@link #extras} key: this is the longer text shown in the big form of a
1023 * {@link BigTextStyle} notification, as supplied to
1024 * {@link BigTextStyle#bigText(CharSequence)}.
1025 */
1026 public static final String EXTRA_BIG_TEXT = "android.bigText";
1027
1028 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001029 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1030 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001031 *
1032 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001033 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001034 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001035 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001036
1037 /**
1038 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1039 * notification payload, as
1040 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001041 *
1042 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001044 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001045 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046
1047 /**
1048 * {@link #extras} key: this is a bitmap to be used instead of the one from
1049 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1050 * shown in its expanded form, as supplied to
1051 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1052 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001053 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054
1055 /**
1056 * {@link #extras} key: this is the progress value supplied to
1057 * {@link Builder#setProgress(int, int, boolean)}.
1058 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: this is the maximum value supplied to
1063 * {@link Builder#setProgress(int, int, boolean)}.
1064 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001065 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066
1067 /**
1068 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1069 * {@link Builder#setProgress(int, int, boolean)}.
1070 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001071 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001072
1073 /**
1074 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1075 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1076 * {@link Builder#setUsesChronometer(boolean)}.
1077 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001078 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001079
1080 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001081 * {@link #extras} key: whether the chronometer set on the notification should count down
1082 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001083 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001084 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001085 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001086
1087 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001088 * {@link #extras} key: whether {@link #when} should be shown,
1089 * as supplied to {@link Builder#setShowWhen(boolean)}.
1090 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001091 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001092
1093 /**
1094 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1095 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1096 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001097 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001098
1099 /**
1100 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1101 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1102 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001103 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001104
1105 /**
1106 * {@link #extras} key: A string representing the name of the specific
1107 * {@link android.app.Notification.Style} used to create this notification.
1108 */
Chris Wren91ad5632013-06-05 15:05:57 -04001109 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001110
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001112 * {@link #extras} key: A String array containing the people that this notification relates to,
1113 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001114 *
1115 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001116 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001117 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001118
1119 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001120 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1121 * this notification relates to.
1122 */
1123 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1124
1125 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001126 * Allow certain system-generated notifications to appear before the device is provisioned.
1127 * Only available to notifications coming from the android package.
1128 * @hide
1129 */
Maurice Lam96c10032017-03-29 15:42:38 -07001130 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001131 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001132 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1133
1134 /**
Robin Leed107af62018-04-27 13:55:56 +02001135 * {@link #extras} key:
1136 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1137 * pointing to an image that can be displayed in the background when the notification is
1138 * selected. Used on television platforms. The URI must point to an image stream suitable for
1139 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001140 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001141 */
1142 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1143
1144 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001145 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001146 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001147 * {@link android.app.Notification.MediaStyle} notification.
1148 */
1149 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1150
1151 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001152 * {@link #extras} key: the indices of actions to be shown in the compact view,
1153 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1154 */
1155 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1156
Christoph Studer943aa672014-08-03 20:31:16 +02001157 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001158 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1159 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001160 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1161 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001162 *
1163 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001164 */
1165 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1166
1167 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001168 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1169 * direct replies
1170 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1171 * {@link Person}
1172 */
1173 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1174
1175 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001177 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001179 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001180
1181 /**
1182 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1183 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001184 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1185 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001186 */
1187 public static final String EXTRA_MESSAGES = "android.messages";
1188
1189 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001190 * {@link #extras} key: an array of
1191 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1192 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1193 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1194 * array of bundles.
1195 */
1196 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1197
1198 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001199 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1200 * represents a group conversation.
1201 */
1202 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1203
1204 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001205 * {@link #extras} key: whether the notification should be colorized as
1206 * supplied to {@link Builder#setColorized(boolean)}}.
1207 */
1208 public static final String EXTRA_COLORIZED = "android.colorized";
1209
1210 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001211 * @hide
1212 */
1213 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1214
Selim Cinek247fa012016-02-18 09:50:48 -08001215 /**
1216 * @hide
1217 */
1218 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1219
Shane Brennan472a3b32016-12-12 15:28:10 -08001220 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001221 * @hide
1222 */
1223 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1224
1225 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 * {@link #extras} key: the audio contents of this notification.
1227 *
1228 * This is for use when rendering the notification on an audio-focused interface;
1229 * the audio contents are a complete sound sample that contains the contents/body of the
1230 * notification. This may be used in substitute of a Text-to-Speech reading of the
1231 * notification. For example if the notification represents a voice message this should point
1232 * to the audio of that message.
1233 *
1234 * The data stored under this key should be a String representation of a Uri that contains the
1235 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1236 *
1237 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1238 * has a field for holding data URI. That field can be used for audio.
1239 * See {@code Message#setData}.
1240 *
1241 * Example usage:
1242 * <pre>
1243 * {@code
1244 * Notification.Builder myBuilder = (build your Notification as normal);
1245 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1246 * }
1247 * </pre>
1248 */
1249 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1250
Dan Sandler80eaa592016-04-14 23:34:54 -04001251 /** @hide */
1252 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001253 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001254 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1255
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001256 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001257 * This is set on the notifications shown by system_server about apps running foreground
1258 * services. It indicates that the notification should be shown
1259 * only if any of the given apps do not already have a properly tagged
1260 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1261 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001262 * @hide
1263 */
1264 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1265
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001266 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001267 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001268 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001269 private Icon mLargeIcon;
1270
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001271 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001272 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001273 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001274
Julia Reynolds13d898c2017-02-02 12:22:05 -05001275 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001276 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001277
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001278 private PendingIntent mAppOverlayIntent;
1279
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001280 /** @hide */
1281 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1282 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1283 })
1284 @Retention(RetentionPolicy.SOURCE)
1285 public @interface GroupAlertBehavior {}
1286
1287 /**
1288 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1289 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1290 * notification will not be muted when it is in a group.
1291 */
1292 public static final int GROUP_ALERT_ALL = 0;
1293
1294 /**
1295 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1296 * notification in a group should be silenced (no sound or vibration) even if they are posted
1297 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001298 * mute this notification if this notification is a group child. This must be applied to all
1299 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001300 *
1301 * <p> For example, you might want to use this constant if you post a number of children
1302 * notifications at once (say, after a periodic sync), and only need to notify the user
1303 * audibly once.
1304 */
1305 public static final int GROUP_ALERT_SUMMARY = 1;
1306
1307 /**
1308 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1309 * notification in a group should be silenced (no sound or vibration) even if they are
1310 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1311 * to mute this notification if this notification is a group summary.
1312 *
1313 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001314 * in your group have content and the summary is only used to visually group notifications
1315 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001316 */
1317 public static final int GROUP_ALERT_CHILDREN = 2;
1318
Julia Reynolds2f431e22017-06-07 14:12:09 +00001319 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001320
Julia Reynolds13d898c2017-02-02 12:22:05 -05001321 /**
1322 * If this notification is being shown as a badge, always show as a number.
1323 */
1324 public static final int BADGE_ICON_NONE = 0;
1325
1326 /**
1327 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1328 * represent this notification.
1329 */
1330 public static final int BADGE_ICON_SMALL = 1;
1331
1332 /**
1333 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1334 * represent this notification.
1335 */
1336 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001337 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001338
Chris Wren51c75102013-07-16 20:49:17 -04001339 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001340 * Structure to encapsulate a named action that can be shown as part of this notification.
1341 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1342 * selected by the user.
1343 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001344 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1345 * or {@link Notification.Builder#addAction(Notification.Action)}
1346 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001347 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001348 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001349 /**
1350 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1351 * {@link RemoteInput}s.
1352 *
1353 * This is intended for {@link RemoteInput}s that only accept data, meaning
1354 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1355 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1356 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1357 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1358 *
1359 * You can test if a RemoteInput matches these constraints using
1360 * {@link RemoteInput#isDataOnly}.
1361 */
1362 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1363
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001364 /**
1365 * {@link }: No semantic action defined.
1366 */
1367 public static final int SEMANTIC_ACTION_NONE = 0;
1368
1369 /**
1370 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1371 * may be appropriate.
1372 */
1373 public static final int SEMANTIC_ACTION_REPLY = 1;
1374
1375 /**
1376 * {@code SemanticAction}: Mark content as read.
1377 */
1378 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1379
1380 /**
1381 * {@code SemanticAction}: Mark content as unread.
1382 */
1383 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1384
1385 /**
1386 * {@code SemanticAction}: Delete the content associated with the notification. This
1387 * could mean deleting an email, message, etc.
1388 */
1389 public static final int SEMANTIC_ACTION_DELETE = 4;
1390
1391 /**
1392 * {@code SemanticAction}: Archive the content associated with the notification. This
1393 * could mean archiving an email, message, etc.
1394 */
1395 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1396
1397 /**
1398 * {@code SemanticAction}: Mute the content associated with the notification. This could
1399 * mean silencing a conversation or currently playing media.
1400 */
1401 public static final int SEMANTIC_ACTION_MUTE = 6;
1402
1403 /**
1404 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1405 * mean un-silencing a conversation or currently playing media.
1406 */
1407 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1408
1409 /**
1410 * {@code SemanticAction}: Mark content with a thumbs up.
1411 */
1412 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1413
1414 /**
1415 * {@code SemanticAction}: Mark content with a thumbs down.
1416 */
1417 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1418
Kodlee Yincda5b092018-02-15 15:34:53 -08001419 /**
1420 * {@code SemanticAction}: Call a contact, group, etc.
1421 */
1422 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001423
Griff Hazen959591e2014-05-15 22:26:18 -07001424 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001425 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001426 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001427 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001428 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001429 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001430
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001431 /**
1432 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001433 *
1434 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001435 */
Dan Sandler86647982015-05-13 23:41:13 -04001436 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001437 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001438
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001439 /**
1440 * Title of the action.
1441 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001442 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001443
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001444 /**
1445 * Intent to send when the user invokes this action. May be null, in which case the action
1446 * may be rendered in a disabled presentation by the system UI.
1447 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001449
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001450 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001451 if (in.readInt() != 0) {
1452 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001453 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1454 icon = mIcon.getResId();
1455 }
Dan Sandler86647982015-05-13 23:41:13 -04001456 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001457 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1458 if (in.readInt() == 1) {
1459 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1460 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001461 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001462 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001463 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001464 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001465 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 /**
Dan Sandler86647982015-05-13 23:41:13 -04001468 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001469 */
Dan Sandler86647982015-05-13 23:41:13 -04001470 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001471 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1473 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001474 }
1475
Adrian Roos7af53622016-10-12 13:44:05 -07001476 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001477 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001478 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1479 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001480 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001481 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1482 this.icon = icon.getResId();
1483 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001484 this.title = title;
1485 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001486 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001487 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001488 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001489 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001490 }
1491
1492 /**
Dan Sandler86647982015-05-13 23:41:13 -04001493 * Return an icon representing the action.
1494 */
1495 public Icon getIcon() {
1496 if (mIcon == null && icon != 0) {
1497 // you snuck an icon in here without using the builder; let's try to keep it
1498 mIcon = Icon.createWithResource("", icon);
1499 }
1500 return mIcon;
1501 }
1502
1503 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001504 * Get additional metadata carried around with this Action.
1505 */
1506 public Bundle getExtras() {
1507 return mExtras;
1508 }
1509
1510 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001511 * Return whether the platform should automatically generate possible replies for this
1512 * {@link Action}
1513 */
1514 public boolean getAllowGeneratedReplies() {
1515 return mAllowGeneratedReplies;
1516 }
1517
1518 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001520 * May return null if no remote inputs were added. Only returns inputs which accept
1521 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001522 */
1523 public RemoteInput[] getRemoteInputs() {
1524 return mRemoteInputs;
1525 }
1526
1527 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001528 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1529 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1530 * (eg. reply, mark as read, delete, etc).
1531 */
1532 public @SemanticAction int getSemanticAction() {
1533 return mSemanticAction;
1534 }
1535
1536 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001537 * Get the list of inputs to be collected from the user that ONLY accept data when this
1538 * action is sent. These remote inputs are guaranteed to return true on a call to
1539 * {@link RemoteInput#isDataOnly}.
1540 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001541 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001542 *
1543 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1544 * of non-textual RemoteInputs do not access these remote inputs.
1545 */
1546 public RemoteInput[] getDataOnlyRemoteInputs() {
1547 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1548 }
1549
1550 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001551 * Builder class for {@link Action} objects.
1552 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001553 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001554 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001555 private final CharSequence mTitle;
1556 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001557 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001558 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001559 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001560 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001561
1562 /**
1563 * Construct a new builder for {@link Action} object.
1564 * @param icon icon to show for this action
1565 * @param title the title of the action
1566 * @param intent the {@link PendingIntent} to fire when users trigger this action
1567 */
Dan Sandler86647982015-05-13 23:41:13 -04001568 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001569 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001570 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001571 }
1572
1573 /**
1574 * Construct a new builder for {@link Action} object.
1575 * @param icon icon to show for this action
1576 * @param title the title of the action
1577 * @param intent the {@link PendingIntent} to fire when users trigger this action
1578 */
1579 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001580 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001581 }
1582
1583 /**
1584 * Construct a new builder for {@link Action} object using the fields from an
1585 * {@link Action}.
1586 * @param action the action to read fields from.
1587 */
1588 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001589 this(action.getIcon(), action.title, action.actionIntent,
1590 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001591 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001592 }
1593
Dan Sandler86647982015-05-13 23:41:13 -04001594 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001595 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1596 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001597 mIcon = icon;
1598 mTitle = title;
1599 mIntent = intent;
1600 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001601 if (remoteInputs != null) {
1602 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1603 Collections.addAll(mRemoteInputs, remoteInputs);
1604 }
Adrian Roos7af53622016-10-12 13:44:05 -07001605 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001606 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001607 }
1608
1609 /**
1610 * Merge additional metadata into this builder.
1611 *
1612 * <p>Values within the Bundle will replace existing extras values in this Builder.
1613 *
1614 * @see Notification.Action#extras
1615 */
1616 public Builder addExtras(Bundle extras) {
1617 if (extras != null) {
1618 mExtras.putAll(extras);
1619 }
1620 return this;
1621 }
1622
1623 /**
1624 * Get the metadata Bundle used by this Builder.
1625 *
1626 * <p>The returned Bundle is shared with this Builder.
1627 */
1628 public Bundle getExtras() {
1629 return mExtras;
1630 }
1631
1632 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001633 * Add an input to be collected from the user when this action is sent.
1634 * Response values can be retrieved from the fired intent by using the
1635 * {@link RemoteInput#getResultsFromIntent} function.
1636 * @param remoteInput a {@link RemoteInput} to add to the action
1637 * @return this object for method chaining
1638 */
1639 public Builder addRemoteInput(RemoteInput remoteInput) {
1640 if (mRemoteInputs == null) {
1641 mRemoteInputs = new ArrayList<RemoteInput>();
1642 }
1643 mRemoteInputs.add(remoteInput);
1644 return this;
1645 }
1646
1647 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001648 * Set whether the platform should automatically generate possible replies to add to
1649 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1650 * {@link RemoteInput}, this has no effect.
1651 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1652 * otherwise
1653 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001654 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001655 */
1656 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1657 mAllowGeneratedReplies = allowGeneratedReplies;
1658 return this;
1659 }
1660
1661 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001662 * Sets the {@code SemanticAction} for this {@link Action}. A
1663 * {@code SemanticAction} denotes what an {@link Action}'s
1664 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1665 * @param semanticAction a SemanticAction defined within {@link Action} with
1666 * {@code SEMANTIC_ACTION_} prefixes
1667 * @return this object for method chaining
1668 */
1669 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1670 mSemanticAction = semanticAction;
1671 return this;
1672 }
1673
1674 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001675 * Apply an extender to this action builder. Extenders may be used to add
1676 * metadata or change options on this builder.
1677 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001678 public Builder extend(Extender extender) {
1679 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001680 return this;
1681 }
1682
1683 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001684 * Combine all of the options that have been set and return a new {@link Action}
1685 * object.
1686 * @return the built action
1687 */
1688 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001689 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1690 RemoteInput[] previousDataInputs =
1691 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001692 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001693 for (RemoteInput input : previousDataInputs) {
1694 dataOnlyInputs.add(input);
1695 }
1696 }
1697 List<RemoteInput> textInputs = new ArrayList<>();
1698 if (mRemoteInputs != null) {
1699 for (RemoteInput input : mRemoteInputs) {
1700 if (input.isDataOnly()) {
1701 dataOnlyInputs.add(input);
1702 } else {
1703 textInputs.add(input);
1704 }
1705 }
1706 }
1707 if (!dataOnlyInputs.isEmpty()) {
1708 RemoteInput[] dataInputsArr =
1709 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1710 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1711 }
1712 RemoteInput[] textInputsArr = textInputs.isEmpty()
1713 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1714 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001715 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001716 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001717 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001718
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 @Override
1720 public Action clone() {
1721 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001722 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001723 title,
1724 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001725 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001726 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001727 getAllowGeneratedReplies(),
1728 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001729 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001730
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001731 @Override
1732 public int describeContents() {
1733 return 0;
1734 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001735
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001736 @Override
1737 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001738 final Icon ic = getIcon();
1739 if (ic != null) {
1740 out.writeInt(1);
1741 ic.writeToParcel(out, 0);
1742 } else {
1743 out.writeInt(0);
1744 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001745 TextUtils.writeToParcel(title, out, flags);
1746 if (actionIntent != null) {
1747 out.writeInt(1);
1748 actionIntent.writeToParcel(out, flags);
1749 } else {
1750 out.writeInt(0);
1751 }
Griff Hazen959591e2014-05-15 22:26:18 -07001752 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001753 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001754 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001755 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001756 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001757
Griff Hazen959591e2014-05-15 22:26:18 -07001758 public static final Parcelable.Creator<Action> CREATOR =
1759 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001760 public Action createFromParcel(Parcel in) {
1761 return new Action(in);
1762 }
1763 public Action[] newArray(int size) {
1764 return new Action[size];
1765 }
1766 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001767
1768 /**
1769 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1770 * metadata or change options on an action builder.
1771 */
1772 public interface Extender {
1773 /**
1774 * Apply this extender to a notification action builder.
1775 * @param builder the builder to be modified.
1776 * @return the build object for chaining.
1777 */
1778 public Builder extend(Builder builder);
1779 }
1780
1781 /**
1782 * Wearable extender for notification actions. To add extensions to an action,
1783 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1784 * the {@code WearableExtender()} constructor and apply it to a
1785 * {@link android.app.Notification.Action.Builder} using
1786 * {@link android.app.Notification.Action.Builder#extend}.
1787 *
1788 * <pre class="prettyprint">
1789 * Notification.Action action = new Notification.Action.Builder(
1790 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001791 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001792 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001793 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001794 */
1795 public static final class WearableExtender implements Extender {
1796 /** Notification action extra which contains wearable extensions */
1797 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1798
Pete Gastaf6781d2014-10-07 15:17:05 -04001799 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001800 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001801 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1802 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1803 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001804
1805 // Flags bitwise-ored to mFlags
1806 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001807 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001808 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001809
1810 // Default value for flags integer
1811 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1812
1813 private int mFlags = DEFAULT_FLAGS;
1814
Pete Gastaf6781d2014-10-07 15:17:05 -04001815 private CharSequence mInProgressLabel;
1816 private CharSequence mConfirmLabel;
1817 private CharSequence mCancelLabel;
1818
Griff Hazen61a9e862014-05-22 16:05:19 -07001819 /**
1820 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1821 * options.
1822 */
1823 public WearableExtender() {
1824 }
1825
1826 /**
1827 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1828 * wearable options present in an existing notification action.
1829 * @param action the notification action to inspect.
1830 */
1831 public WearableExtender(Action action) {
1832 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1833 if (wearableBundle != null) {
1834 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001835 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1836 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1837 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001838 }
1839 }
1840
1841 /**
1842 * Apply wearable extensions to a notification action that is being built. This is
1843 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1844 * method of {@link android.app.Notification.Action.Builder}.
1845 */
1846 @Override
1847 public Action.Builder extend(Action.Builder builder) {
1848 Bundle wearableBundle = new Bundle();
1849
1850 if (mFlags != DEFAULT_FLAGS) {
1851 wearableBundle.putInt(KEY_FLAGS, mFlags);
1852 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001853 if (mInProgressLabel != null) {
1854 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1855 }
1856 if (mConfirmLabel != null) {
1857 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1858 }
1859 if (mCancelLabel != null) {
1860 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1861 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001862
1863 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1864 return builder;
1865 }
1866
1867 @Override
1868 public WearableExtender clone() {
1869 WearableExtender that = new WearableExtender();
1870 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001871 that.mInProgressLabel = this.mInProgressLabel;
1872 that.mConfirmLabel = this.mConfirmLabel;
1873 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001874 return that;
1875 }
1876
1877 /**
1878 * Set whether this action is available when the wearable device is not connected to
1879 * a companion device. The user can still trigger this action when the wearable device is
1880 * offline, but a visual hint will indicate that the action may not be available.
1881 * Defaults to true.
1882 */
1883 public WearableExtender setAvailableOffline(boolean availableOffline) {
1884 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1885 return this;
1886 }
1887
1888 /**
1889 * Get whether this action is available when the wearable device is not connected to
1890 * a companion device. The user can still trigger this action when the wearable device is
1891 * offline, but a visual hint will indicate that the action may not be available.
1892 * Defaults to true.
1893 */
1894 public boolean isAvailableOffline() {
1895 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1896 }
1897
1898 private void setFlag(int mask, boolean value) {
1899 if (value) {
1900 mFlags |= mask;
1901 } else {
1902 mFlags &= ~mask;
1903 }
1904 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001905
1906 /**
1907 * Set a label to display while the wearable is preparing to automatically execute the
1908 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1909 *
1910 * @param label the label to display while the action is being prepared to execute
1911 * @return this object for method chaining
1912 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001913 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001914 public WearableExtender setInProgressLabel(CharSequence label) {
1915 mInProgressLabel = label;
1916 return this;
1917 }
1918
1919 /**
1920 * Get the label to display while the wearable is preparing to automatically execute
1921 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1922 *
1923 * @return the label to display while the action is being prepared to execute
1924 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001925 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001926 public CharSequence getInProgressLabel() {
1927 return mInProgressLabel;
1928 }
1929
1930 /**
1931 * Set a label to display to confirm that the action should be executed.
1932 * This is usually an imperative verb like "Send".
1933 *
1934 * @param label the label to confirm the action should be executed
1935 * @return this object for method chaining
1936 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001937 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001938 public WearableExtender setConfirmLabel(CharSequence label) {
1939 mConfirmLabel = label;
1940 return this;
1941 }
1942
1943 /**
1944 * Get the label to display to confirm that the action should be executed.
1945 * This is usually an imperative verb like "Send".
1946 *
1947 * @return the label to confirm the action should be executed
1948 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001949 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001950 public CharSequence getConfirmLabel() {
1951 return mConfirmLabel;
1952 }
1953
1954 /**
1955 * Set a label to display to cancel the action.
1956 * This is usually an imperative verb, like "Cancel".
1957 *
1958 * @param label the label to display to cancel the action
1959 * @return this object for method chaining
1960 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001961 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001962 public WearableExtender setCancelLabel(CharSequence label) {
1963 mCancelLabel = label;
1964 return this;
1965 }
1966
1967 /**
1968 * Get the label to display to cancel the action.
1969 * This is usually an imperative verb like "Cancel".
1970 *
1971 * @return the label to display to cancel the action
1972 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001973 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001974 public CharSequence getCancelLabel() {
1975 return mCancelLabel;
1976 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001977
1978 /**
1979 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1980 * platform that it can generate the appropriate transitions.
1981 * @param hintLaunchesActivity {@code true} if the content intent will launch
1982 * an activity and transitions should be generated, false otherwise.
1983 * @return this object for method chaining
1984 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001985 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001986 boolean hintLaunchesActivity) {
1987 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1988 return this;
1989 }
1990
1991 /**
1992 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1993 * platform that it can generate the appropriate transitions
1994 * @return {@code true} if the content intent will launch an activity and transitions
1995 * should be generated, false otherwise. The default value is {@code false} if this was
1996 * never set.
1997 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001998 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001999 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2000 }
Alex Hills9f087612016-06-07 09:08:59 -04002001
2002 /**
2003 * Set a hint that this Action should be displayed inline.
2004 *
2005 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2006 * otherwise
2007 * @return this object for method chaining
2008 */
2009 public WearableExtender setHintDisplayActionInline(
2010 boolean hintDisplayInline) {
2011 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2012 return this;
2013 }
2014
2015 /**
2016 * Get a hint that this Action should be displayed inline.
2017 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002018 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002019 * otherwise. The default value is {@code false} if this was never set.
2020 */
2021 public boolean getHintDisplayActionInline() {
2022 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2023 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002024 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002025
2026 /**
2027 * Provides meaning to an {@link Action} that hints at what the associated
2028 * {@link PendingIntent} will do. For example, an {@link Action} with a
2029 * {@link PendingIntent} that replies to a text message notification may have the
2030 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2031 *
2032 * @hide
2033 */
2034 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2035 SEMANTIC_ACTION_NONE,
2036 SEMANTIC_ACTION_REPLY,
2037 SEMANTIC_ACTION_MARK_AS_READ,
2038 SEMANTIC_ACTION_MARK_AS_UNREAD,
2039 SEMANTIC_ACTION_DELETE,
2040 SEMANTIC_ACTION_ARCHIVE,
2041 SEMANTIC_ACTION_MUTE,
2042 SEMANTIC_ACTION_UNMUTE,
2043 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002044 SEMANTIC_ACTION_THUMBS_DOWN,
2045 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002046 })
2047 @Retention(RetentionPolicy.SOURCE)
2048 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002049 }
2050
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002051 /**
2052 * Array of all {@link Action} structures attached to this notification by
2053 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2054 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2055 * interface for invoking actions.
2056 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002057 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002058
2059 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002060 * Replacement version of this notification whose content will be shown
2061 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2062 * and {@link #VISIBILITY_PUBLIC}.
2063 */
2064 public Notification publicVersion;
2065
2066 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002068 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 */
2070 public Notification()
2071 {
2072 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002073 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002074 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076
2077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 * @hide
2079 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002080 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 public Notification(Context context, int icon, CharSequence tickerText, long when,
2082 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2083 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002084 new Builder(context)
2085 .setWhen(when)
2086 .setSmallIcon(icon)
2087 .setTicker(tickerText)
2088 .setContentTitle(contentTitle)
2089 .setContentText(contentText)
2090 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2091 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
2093
2094 /**
2095 * Constructs a Notification object with the information needed to
2096 * have a status bar icon without the standard expanded view.
2097 *
2098 * @param icon The resource id of the icon to put in the status bar.
2099 * @param tickerText The text that flows by in the status bar when the notification first
2100 * activates.
2101 * @param when The time to show in the time field. In the System.currentTimeMillis
2102 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002103 *
2104 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002106 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 public Notification(int icon, CharSequence tickerText, long when)
2108 {
2109 this.icon = icon;
2110 this.tickerText = tickerText;
2111 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002112 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
2114
2115 /**
2116 * Unflatten the notification from a parcel.
2117 */
Svet Ganovddb94882016-06-23 19:55:24 -07002118 @SuppressWarnings("unchecked")
2119 public Notification(Parcel parcel) {
2120 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2121 // intents in extras are always written as the last entry.
2122 readFromParcelImpl(parcel);
2123 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002124 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002125 }
2126
2127 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 {
2129 int version = parcel.readInt();
2130
Adrian Roosfb921842017-10-26 14:49:56 +02002131 mWhitelistToken = parcel.readStrongBinder();
2132 if (mWhitelistToken == null) {
2133 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002134 }
2135 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002136 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002139 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002140 if (parcel.readInt() != 0) {
2141 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002142 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2143 icon = mSmallIcon.getResId();
2144 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 number = parcel.readInt();
2147 if (parcel.readInt() != 0) {
2148 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2149 }
2150 if (parcel.readInt() != 0) {
2151 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2152 }
2153 if (parcel.readInt() != 0) {
2154 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2155 }
2156 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002157 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002158 }
2159 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2161 }
Joe Onorato561d3852010-11-20 18:09:34 -08002162 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002163 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 defaults = parcel.readInt();
2166 flags = parcel.readInt();
2167 if (parcel.readInt() != 0) {
2168 sound = Uri.CREATOR.createFromParcel(parcel);
2169 }
2170
2171 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002172 if (parcel.readInt() != 0) {
2173 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 vibrate = parcel.createLongArray();
2176 ledARGB = parcel.readInt();
2177 ledOnMS = parcel.readInt();
2178 ledOffMS = parcel.readInt();
2179 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002180
2181 if (parcel.readInt() != 0) {
2182 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2183 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002184
2185 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002186
John Spurlockfd7f1e02014-03-18 16:41:57 -04002187 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002188
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002189 mGroupKey = parcel.readString();
2190
2191 mSortKey = parcel.readString();
2192
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002193 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002194 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002195
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002196 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2197
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002198 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002199 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2200 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002201
Chris Wren8fd39ec2014-02-27 17:43:26 -05002202 if (parcel.readInt() != 0) {
2203 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2204 }
2205
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002206 visibility = parcel.readInt();
2207
2208 if (parcel.readInt() != 0) {
2209 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2210 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002211
2212 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002213
2214 if (parcel.readInt() != 0) {
2215 mChannelId = parcel.readString();
2216 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002217 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002218
2219 if (parcel.readInt() != 0) {
2220 mShortcutId = parcel.readString();
2221 }
2222
2223 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002224
2225 if (parcel.readInt() != 0) {
2226 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2227 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002228
2229 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002230 if (parcel.readInt() != 0) {
2231 mAppOverlayIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
2234
Andy Stadler110988c2010-12-03 14:29:16 -08002235 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002236 public Notification clone() {
2237 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002238 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002239 return that;
2240 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002241
Daniel Sandler1a497d32013-04-18 14:52:45 -04002242 /**
2243 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2244 * of this into that.
2245 * @hide
2246 */
2247 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002248 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002249 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002250 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002251 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002252 that.number = this.number;
2253
2254 // PendingIntents are global, so there's no reason (or way) to clone them.
2255 that.contentIntent = this.contentIntent;
2256 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002257 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002258
2259 if (this.tickerText != null) {
2260 that.tickerText = this.tickerText.toString();
2261 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002262 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002263 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002264 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002265 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002266 that.contentView = this.contentView.clone();
2267 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002268 if (heavy && this.mLargeIcon != null) {
2269 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002270 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002271 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002272 that.sound = this.sound; // android.net.Uri is immutable
2273 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002274 if (this.audioAttributes != null) {
2275 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2276 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002277
2278 final long[] vibrate = this.vibrate;
2279 if (vibrate != null) {
2280 final int N = vibrate.length;
2281 final long[] vib = that.vibrate = new long[N];
2282 System.arraycopy(vibrate, 0, vib, 0, N);
2283 }
2284
2285 that.ledARGB = this.ledARGB;
2286 that.ledOnMS = this.ledOnMS;
2287 that.ledOffMS = this.ledOffMS;
2288 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002289
Joe Onorato18e69df2010-05-17 22:26:12 -07002290 that.flags = this.flags;
2291
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002292 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002293
John Spurlockfd7f1e02014-03-18 16:41:57 -04002294 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002295
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002296 that.mGroupKey = this.mGroupKey;
2297
2298 that.mSortKey = this.mSortKey;
2299
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002300 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002301 try {
2302 that.extras = new Bundle(this.extras);
2303 // will unparcel
2304 that.extras.size();
2305 } catch (BadParcelableException e) {
2306 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2307 that.extras = null;
2308 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309 }
2310
Felipe Lemedd85da62016-06-28 11:29:54 -07002311 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2312 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002313 }
2314
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002315 if (this.actions != null) {
2316 that.actions = new Action[this.actions.length];
2317 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002318 if ( this.actions[i] != null) {
2319 that.actions[i] = this.actions[i].clone();
2320 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002321 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002322 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002323
Daniel Sandler1a497d32013-04-18 14:52:45 -04002324 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002325 that.bigContentView = this.bigContentView.clone();
2326 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002327
Chris Wren8fd39ec2014-02-27 17:43:26 -05002328 if (heavy && this.headsUpContentView != null) {
2329 that.headsUpContentView = this.headsUpContentView.clone();
2330 }
2331
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002332 that.visibility = this.visibility;
2333
2334 if (this.publicVersion != null) {
2335 that.publicVersion = new Notification();
2336 this.publicVersion.cloneInto(that.publicVersion, heavy);
2337 }
2338
Dan Sandler26e81cf2014-05-06 10:01:27 -04002339 that.color = this.color;
2340
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002341 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002342 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002343 that.mShortcutId = this.mShortcutId;
2344 that.mBadgeIcon = this.mBadgeIcon;
2345 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002346 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002347 that.mAppOverlayIntent = this.mAppOverlayIntent;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002348
Daniel Sandler1a497d32013-04-18 14:52:45 -04002349 if (!heavy) {
2350 that.lightenPayload(); // will clean out extras
2351 }
2352 }
2353
2354 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002355 * Note all {@link Uri} that are referenced internally, with the expectation
2356 * that Uri permission grants will need to be issued to ensure the recipient
2357 * of this object is able to render its contents.
2358 *
2359 * @hide
2360 */
2361 public void visitUris(@NonNull Consumer<Uri> visitor) {
2362 visitor.accept(sound);
2363
2364 if (tickerView != null) tickerView.visitUris(visitor);
2365 if (contentView != null) contentView.visitUris(visitor);
2366 if (bigContentView != null) bigContentView.visitUris(visitor);
2367 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2368
2369 if (extras != null) {
2370 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002371 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2372 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2373 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002374 }
2375
2376 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2377 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2378 if (!ArrayUtils.isEmpty(messages)) {
2379 for (MessagingStyle.Message message : MessagingStyle.Message
2380 .getMessagesFromBundleArray(messages)) {
2381 visitor.accept(message.getDataUri());
2382 }
2383 }
2384
2385 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2386 if (!ArrayUtils.isEmpty(historic)) {
2387 for (MessagingStyle.Message message : MessagingStyle.Message
2388 .getMessagesFromBundleArray(historic)) {
2389 visitor.accept(message.getDataUri());
2390 }
2391 }
2392 }
2393 }
2394
2395 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002396 * Removes heavyweight parts of the Notification object for archival or for sending to
2397 * listeners when the full contents are not necessary.
2398 * @hide
2399 */
2400 public final void lightenPayload() {
2401 tickerView = null;
2402 contentView = null;
2403 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002404 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002405 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002406 if (extras != null && !extras.isEmpty()) {
2407 final Set<String> keyset = extras.keySet();
2408 final int N = keyset.size();
2409 final String[] keys = keyset.toArray(new String[N]);
2410 for (int i=0; i<N; i++) {
2411 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002412 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2413 continue;
2414 }
Dan Sandler50128532015-12-08 15:42:41 -05002415 final Object obj = extras.get(key);
2416 if (obj != null &&
2417 ( obj instanceof Parcelable
2418 || obj instanceof Parcelable[]
2419 || obj instanceof SparseArray
2420 || obj instanceof ArrayList)) {
2421 extras.remove(key);
2422 }
2423 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002424 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002425 }
2426
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002427 /**
2428 * Make sure this CharSequence is safe to put into a bundle, which basically
2429 * means it had better not be some custom Parcelable implementation.
2430 * @hide
2431 */
2432 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002433 if (cs == null) return cs;
2434 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2435 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2436 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002437 if (cs instanceof Parcelable) {
2438 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2439 + " instance is a custom Parcelable and not allowed in Notification");
2440 return cs.toString();
2441 }
Selim Cinek60a54252016-02-26 17:03:25 -08002442 return removeTextSizeSpans(cs);
2443 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002444
Selim Cinek60a54252016-02-26 17:03:25 -08002445 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2446 if (charSequence instanceof Spanned) {
2447 Spanned ss = (Spanned) charSequence;
2448 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2449 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2450 for (Object span : spans) {
2451 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002452 if (resultSpan instanceof CharacterStyle) {
2453 resultSpan = ((CharacterStyle) span).getUnderlying();
2454 }
2455 if (resultSpan instanceof TextAppearanceSpan) {
2456 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002457 resultSpan = new TextAppearanceSpan(
2458 originalSpan.getFamily(),
2459 originalSpan.getTextStyle(),
2460 -1,
2461 originalSpan.getTextColor(),
2462 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002463 } else if (resultSpan instanceof RelativeSizeSpan
2464 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002465 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002466 } else {
2467 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002468 }
2469 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2470 ss.getSpanFlags(span));
2471 }
2472 return builder;
2473 }
2474 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002475 }
2476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 public int describeContents() {
2478 return 0;
2479 }
2480
2481 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002482 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 */
Svet Ganovddb94882016-06-23 19:55:24 -07002484 public void writeToParcel(Parcel parcel, int flags) {
2485 // We need to mark all pending intents getting into the notification
2486 // system as being put there to later allow the notification ranker
2487 // to launch them and by doing so add the app to the battery saver white
2488 // list for a short period of time. The problem is that the system
2489 // cannot look into the extras as there may be parcelables there that
2490 // the platform does not know how to handle. To go around that we have
2491 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002492 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002493 if (collectPendingIntents) {
2494 PendingIntent.setOnMarshaledListener(
2495 (PendingIntent intent, Parcel out, int outFlags) -> {
2496 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002497 if (allPendingIntents == null) {
2498 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002499 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002500 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002501 }
2502 });
2503 }
2504 try {
2505 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002506 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002507 writeToParcelImpl(parcel, flags);
2508 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002509 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002510 } finally {
2511 if (collectPendingIntents) {
2512 PendingIntent.setOnMarshaledListener(null);
2513 }
2514 }
2515 }
2516
2517 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 parcel.writeInt(1);
2519
Adrian Roosfb921842017-10-26 14:49:56 +02002520 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002522 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002523 if (mSmallIcon == null && icon != 0) {
2524 // you snuck an icon in here without using the builder; let's try to keep it
2525 mSmallIcon = Icon.createWithResource("", icon);
2526 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002527 if (mSmallIcon != null) {
2528 parcel.writeInt(1);
2529 mSmallIcon.writeToParcel(parcel, 0);
2530 } else {
2531 parcel.writeInt(0);
2532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 parcel.writeInt(number);
2534 if (contentIntent != null) {
2535 parcel.writeInt(1);
2536 contentIntent.writeToParcel(parcel, 0);
2537 } else {
2538 parcel.writeInt(0);
2539 }
2540 if (deleteIntent != null) {
2541 parcel.writeInt(1);
2542 deleteIntent.writeToParcel(parcel, 0);
2543 } else {
2544 parcel.writeInt(0);
2545 }
2546 if (tickerText != null) {
2547 parcel.writeInt(1);
2548 TextUtils.writeToParcel(tickerText, parcel, flags);
2549 } else {
2550 parcel.writeInt(0);
2551 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002552 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002553 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002554 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002555 } else {
2556 parcel.writeInt(0);
2557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 if (contentView != null) {
2559 parcel.writeInt(1);
2560 contentView.writeToParcel(parcel, 0);
2561 } else {
2562 parcel.writeInt(0);
2563 }
Selim Cinek279fa862016-06-14 10:57:25 -07002564 if (mLargeIcon == null && largeIcon != null) {
2565 // you snuck an icon in here without using the builder; let's try to keep it
2566 mLargeIcon = Icon.createWithBitmap(largeIcon);
2567 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002568 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002569 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002570 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002571 } else {
2572 parcel.writeInt(0);
2573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574
2575 parcel.writeInt(defaults);
2576 parcel.writeInt(this.flags);
2577
2578 if (sound != null) {
2579 parcel.writeInt(1);
2580 sound.writeToParcel(parcel, 0);
2581 } else {
2582 parcel.writeInt(0);
2583 }
2584 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002585
2586 if (audioAttributes != null) {
2587 parcel.writeInt(1);
2588 audioAttributes.writeToParcel(parcel, 0);
2589 } else {
2590 parcel.writeInt(0);
2591 }
2592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 parcel.writeLongArray(vibrate);
2594 parcel.writeInt(ledARGB);
2595 parcel.writeInt(ledOnMS);
2596 parcel.writeInt(ledOffMS);
2597 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002598
2599 if (fullScreenIntent != null) {
2600 parcel.writeInt(1);
2601 fullScreenIntent.writeToParcel(parcel, 0);
2602 } else {
2603 parcel.writeInt(0);
2604 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002605
2606 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002607
John Spurlockfd7f1e02014-03-18 16:41:57 -04002608 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002609
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002610 parcel.writeString(mGroupKey);
2611
2612 parcel.writeString(mSortKey);
2613
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002614 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002615
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002616 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002617
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002618 if (bigContentView != null) {
2619 parcel.writeInt(1);
2620 bigContentView.writeToParcel(parcel, 0);
2621 } else {
2622 parcel.writeInt(0);
2623 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002624
Chris Wren8fd39ec2014-02-27 17:43:26 -05002625 if (headsUpContentView != null) {
2626 parcel.writeInt(1);
2627 headsUpContentView.writeToParcel(parcel, 0);
2628 } else {
2629 parcel.writeInt(0);
2630 }
2631
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002632 parcel.writeInt(visibility);
2633
2634 if (publicVersion != null) {
2635 parcel.writeInt(1);
2636 publicVersion.writeToParcel(parcel, 0);
2637 } else {
2638 parcel.writeInt(0);
2639 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002640
2641 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002642
2643 if (mChannelId != null) {
2644 parcel.writeInt(1);
2645 parcel.writeString(mChannelId);
2646 } else {
2647 parcel.writeInt(0);
2648 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002649 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002650
2651 if (mShortcutId != null) {
2652 parcel.writeInt(1);
2653 parcel.writeString(mShortcutId);
2654 } else {
2655 parcel.writeInt(0);
2656 }
2657
2658 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002659
2660 if (mSettingsText != null) {
2661 parcel.writeInt(1);
2662 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2663 } else {
2664 parcel.writeInt(0);
2665 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002666
2667 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002668
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002669 if (mAppOverlayIntent != null) {
2670 parcel.writeInt(1);
2671 mAppOverlayIntent.writeToParcel(parcel, 0);
2672 } else {
2673 parcel.writeInt(0);
2674 }
2675
Julia Reynoldsfc640012018-02-21 12:25:27 -05002676 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 }
2678
2679 /**
2680 * Parcelable.Creator that instantiates Notification objects
2681 */
2682 public static final Parcelable.Creator<Notification> CREATOR
2683 = new Parcelable.Creator<Notification>()
2684 {
2685 public Notification createFromParcel(Parcel parcel)
2686 {
2687 return new Notification(parcel);
2688 }
2689
2690 public Notification[] newArray(int size)
2691 {
2692 return new Notification[size];
2693 }
2694 };
2695
2696 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002697 * @hide
2698 */
2699 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2700 Notification.Action[] firstAs = first.actions;
2701 Notification.Action[] secondAs = second.actions;
2702 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2703 return true;
2704 }
2705 if (firstAs != null && secondAs != null) {
2706 if (firstAs.length != secondAs.length) {
2707 return true;
2708 }
2709 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002710 if (!Objects.equals(String.valueOf(firstAs[i].title),
2711 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002712 return true;
2713 }
2714 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2715 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2716 if (firstRs == null) {
2717 firstRs = new RemoteInput[0];
2718 }
2719 if (secondRs == null) {
2720 secondRs = new RemoteInput[0];
2721 }
2722 if (firstRs.length != secondRs.length) {
2723 return true;
2724 }
2725 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002726 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2727 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002728 return true;
2729 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002730 }
2731 }
2732 }
2733 return false;
2734 }
2735
2736 /**
2737 * @hide
2738 */
2739 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2740 if (first.getStyle() == null) {
2741 return second.getStyle() != null;
2742 }
2743 if (second.getStyle() == null) {
2744 return true;
2745 }
2746 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2747 }
2748
2749 /**
2750 * @hide
2751 */
2752 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002753 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2754 return true;
2755 }
2756
2757 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2758 return true;
2759 }
2760 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2761 return true;
2762 }
2763 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2764 return true;
2765 }
2766
2767 return false;
2768 }
2769
2770 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2771 if (first == null && second == null) {
2772 return false;
2773 }
2774 if (first == null && second != null || first != null && second == null) {
2775 return true;
2776 }
2777
2778 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2779 return true;
2780 }
2781
2782 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2783 return true;
2784 }
2785
2786 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002787 }
2788
2789 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002790 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2791 * <p>
2792 * For backwards compatibility {@code extras} holds some references to "real" member data such
2793 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2794 * fine as long as the object stays in one process.
2795 * <p>
2796 * However, once the notification goes into a parcel each reference gets marshalled separately,
2797 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2798 */
2799 private void fixDuplicateExtras() {
2800 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002801 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2802 }
2803 }
2804
2805 /**
2806 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2807 * separate object, replace it with the field's version to avoid holding duplicate copies.
2808 */
2809 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2810 if (original != null && extras.getParcelable(extraName) != null) {
2811 extras.putParcelable(extraName, original);
2812 }
2813 }
2814
2815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2817 * layout.
2818 *
2819 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2820 * in the view.</p>
2821 * @param context The context for your application / activity.
2822 * @param contentTitle The title that goes in the expanded entry.
2823 * @param contentText The text that goes in the expanded entry.
2824 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2825 * If this is an activity, it must include the
2826 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002827 * that you take care of task management as described in the
2828 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2829 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002831 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002832 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002834 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 public void setLatestEventInfo(Context context,
2836 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002837 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2838 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2839 new Throwable());
2840 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002841
Selim Cinek4ac6f602016-06-13 15:47:03 -07002842 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2843 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2844 }
2845
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002846 // ensure that any information already set directly is preserved
2847 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002848
2849 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002851 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 }
2853 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002854 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002856 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002857
2858 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
Julia Reynoldsda303542015-11-23 14:00:20 -05002861 /**
2862 * @hide
2863 */
2864 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002865 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002866 }
2867
2868 /**
2869 * @hide
2870 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002871 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002872 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002873 }
2874
Yi Jin6b514142017-10-30 14:54:12 -07002875 /**
2876 * @hide
2877 */
2878 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2879 long token = proto.start(fieldId);
2880 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2881 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2882 proto.write(NotificationProto.FLAGS, this.flags);
2883 proto.write(NotificationProto.COLOR, this.color);
2884 proto.write(NotificationProto.CATEGORY, this.category);
2885 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2886 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2887 if (this.actions != null) {
2888 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2889 }
2890 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2891 proto.write(NotificationProto.VISIBILITY, this.visibility);
2892 }
2893 if (publicVersion != null) {
2894 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2895 }
2896 proto.end(token);
2897 }
2898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 @Override
2900 public String toString() {
2901 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002902 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002903 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002904 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002905 sb.append(priority);
2906 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002907 if (contentView != null) {
2908 sb.append(contentView.getPackage());
2909 sb.append("/0x");
2910 sb.append(Integer.toHexString(contentView.getLayoutId()));
2911 } else {
2912 sb.append("null");
2913 }
2914 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002915 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2916 sb.append("default");
2917 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 int N = this.vibrate.length-1;
2919 sb.append("[");
2920 for (int i=0; i<N; i++) {
2921 sb.append(this.vibrate[i]);
2922 sb.append(',');
2923 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002924 if (N != -1) {
2925 sb.append(this.vibrate[N]);
2926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 } else {
2929 sb.append("null");
2930 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002931 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002932 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002934 } else if (this.sound != null) {
2935 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 } else {
2937 sb.append("null");
2938 }
Chris Wren365b6d32015-07-16 10:39:26 -04002939 if (this.tickerText != null) {
2940 sb.append(" tick");
2941 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002942 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002945 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002946 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002947 if (this.category != null) {
2948 sb.append(" category=");
2949 sb.append(this.category);
2950 }
2951 if (this.mGroupKey != null) {
2952 sb.append(" groupKey=");
2953 sb.append(this.mGroupKey);
2954 }
2955 if (this.mSortKey != null) {
2956 sb.append(" sortKey=");
2957 sb.append(this.mSortKey);
2958 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002959 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002960 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002961 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002962 }
2963 sb.append(" vis=");
2964 sb.append(visibilityToString(this.visibility));
2965 if (this.publicVersion != null) {
2966 sb.append(" publicVersion=");
2967 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002968 }
2969 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 return sb.toString();
2971 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002972
Dan Sandler1b718782014-07-18 12:43:45 -04002973 /**
2974 * {@hide}
2975 */
2976 public static String visibilityToString(int vis) {
2977 switch (vis) {
2978 case VISIBILITY_PRIVATE:
2979 return "PRIVATE";
2980 case VISIBILITY_PUBLIC:
2981 return "PUBLIC";
2982 case VISIBILITY_SECRET:
2983 return "SECRET";
2984 default:
2985 return "UNKNOWN(" + String.valueOf(vis) + ")";
2986 }
2987 }
2988
Joe Onoratocb109a02011-01-18 17:57:41 -08002989 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002990 * {@hide}
2991 */
2992 public static String priorityToString(@Priority int pri) {
2993 switch (pri) {
2994 case PRIORITY_MIN:
2995 return "MIN";
2996 case PRIORITY_LOW:
2997 return "LOW";
2998 case PRIORITY_DEFAULT:
2999 return "DEFAULT";
3000 case PRIORITY_HIGH:
3001 return "HIGH";
3002 case PRIORITY_MAX:
3003 return "MAX";
3004 default:
3005 return "UNKNOWN(" + String.valueOf(pri) + ")";
3006 }
3007 }
3008
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003009 /**
3010 * @hide
3011 */
3012 public boolean hasCompletedProgress() {
3013 // not a progress notification; can't be complete
3014 if (!extras.containsKey(EXTRA_PROGRESS)
3015 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3016 return false;
3017 }
3018 // many apps use max 0 for 'indeterminate'; not complete
3019 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3020 return false;
3021 }
3022 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3023 }
3024
Jeff Sharkey000ce802017-04-29 13:13:27 -06003025 /** @removed */
3026 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003027 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003028 return mChannelId;
3029 }
3030
3031 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003032 * Returns the id of the channel this notification posts to.
3033 */
3034 public String getChannelId() {
3035 return mChannelId;
3036 }
3037
Jeff Sharkey000ce802017-04-29 13:13:27 -06003038 /** @removed */
3039 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003040 public long getTimeout() {
3041 return mTimeout;
3042 }
3043
3044 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003045 * Returns the duration from posting after which this notification should be canceled by the
3046 * system, if it's not canceled already.
3047 */
3048 public long getTimeoutAfter() {
3049 return mTimeout;
3050 }
3051
3052 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003053 * Returns what icon should be shown for this notification if it is being displayed in a
3054 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3055 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3056 */
3057 public int getBadgeIconType() {
3058 return mBadgeIcon;
3059 }
3060
3061 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003062 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003063 *
3064 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3065 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003066 */
3067 public String getShortcutId() {
3068 return mShortcutId;
3069 }
3070
Julia Reynolds3aedded2017-03-31 14:42:09 -04003071
3072 /**
3073 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3074 */
3075 public CharSequence getSettingsText() {
3076 return mSettingsText;
3077 }
3078
Julia Reynolds13d898c2017-02-02 12:22:05 -05003079 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003080 * Returns which type of notifications in a group are responsible for audibly alerting the
3081 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3082 * {@link #GROUP_ALERT_SUMMARY}.
3083 */
3084 public @GroupAlertBehavior int getGroupAlertBehavior() {
3085 return mGroupAlertBehavior;
3086 }
3087
3088 /**
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003089 * Returns the intent that will be used to display app content in a floating window over the
3090 * existing foreground activity.
3091 */
3092 public PendingIntent getAppOverlayIntent() {
3093 return mAppOverlayIntent;
3094 }
3095
3096 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003097 * The small icon representing this notification in the status bar and content view.
3098 *
3099 * @return the small icon representing this notification.
3100 *
3101 * @see Builder#getSmallIcon()
3102 * @see Builder#setSmallIcon(Icon)
3103 */
3104 public Icon getSmallIcon() {
3105 return mSmallIcon;
3106 }
3107
3108 /**
3109 * Used when notifying to clean up legacy small icons.
3110 * @hide
3111 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003112 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003113 public void setSmallIcon(Icon icon) {
3114 mSmallIcon = icon;
3115 }
3116
3117 /**
3118 * The large icon shown in this notification's content view.
3119 * @see Builder#getLargeIcon()
3120 * @see Builder#setLargeIcon(Icon)
3121 */
3122 public Icon getLargeIcon() {
3123 return mLargeIcon;
3124 }
3125
3126 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003127 * @hide
3128 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003129 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003130 public boolean isGroupSummary() {
3131 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3132 }
3133
3134 /**
3135 * @hide
3136 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003137 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003138 public boolean isGroupChild() {
3139 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3140 }
3141
3142 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003143 * @hide
3144 */
3145 public boolean suppressAlertingDueToGrouping() {
3146 if (isGroupSummary()
3147 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3148 return true;
3149 } else if (isGroupChild()
3150 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3151 return true;
3152 }
3153 return false;
3154 }
3155
Tony Mak638430e2018-10-08 19:19:10 +01003156
3157 /**
3158 * Finds and returns a remote input and its corresponding action.
3159 *
3160 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3161 * @return the result pair, {@code null} if no result is found.
3162 *
3163 * @hide
3164 */
3165 @Nullable
3166 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3167 if (actions == null) {
3168 return null;
3169 }
3170 for (Notification.Action action : actions) {
3171 if (action.getRemoteInputs() == null) {
3172 continue;
3173 }
3174 RemoteInput resultRemoteInput = null;
3175 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3176 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3177 resultRemoteInput = remoteInput;
3178 }
3179 }
3180 if (resultRemoteInput != null) {
3181 return Pair.create(resultRemoteInput, action);
3182 }
3183 }
3184 return null;
3185 }
3186
Julia Reynolds30203152017-05-26 13:36:31 -04003187 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003188 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003189 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003190 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003191 * content views using the platform's notification layout template. If your app supports
3192 * versions of Android as old as API level 4, you can instead use
3193 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3194 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3195 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003196 *
Scott Main183bf112012-08-13 19:12:13 -07003197 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003199 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003200 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003201 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3202 * .setContentText(subject)
3203 * .setSmallIcon(R.drawable.new_mail)
3204 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003205 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003206 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003207 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003209 /**
3210 * @hide
3211 */
3212 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3213 "android.rebuild.contentViewActionCount";
3214 /**
3215 * @hide
3216 */
3217 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3218 = "android.rebuild.bigViewActionCount";
3219 /**
3220 * @hide
3221 */
3222 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3223 = "android.rebuild.hudViewActionCount";
3224
Selim Cinek6743c0b2017-01-18 18:24:01 -08003225 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3226 SystemProperties.getBoolean("notifications.only_title", true);
3227
Selim Cinek389edcd2017-05-11 19:16:44 -07003228 /**
3229 * The lightness difference that has to be added to the primary text color to obtain the
3230 * secondary text color when the background is light.
3231 */
3232 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3233
3234 /**
3235 * The lightness difference that has to be added to the primary text color to obtain the
3236 * secondary text color when the background is dark.
3237 * A bit less then the above value, since it looks better on dark backgrounds.
3238 */
3239 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3240
Joe Onorato46439ce2010-11-19 13:56:21 -08003241 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003242 private Notification mN;
3243 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003244 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003245 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003246 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003247 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003248 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003249 private boolean mIsLegacy;
3250 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003251
3252 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003253 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3254 */
3255 private int mCachedContrastColor = COLOR_INVALID;
3256 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003257 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003258 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003259 */
3260 private int mCachedAmbientColor = COLOR_INVALID;
3261 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003262 /**
3263 * A neutral color color that can be used for icons.
3264 */
3265 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003266
3267 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003268 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3269 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3270 */
3271 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003272 private int mTextColorsAreForBackground = COLOR_INVALID;
3273 private int mPrimaryTextColor = COLOR_INVALID;
3274 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003275 private int mBackgroundColor = COLOR_INVALID;
3276 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003277 /**
3278 * A temporary location where actions are stored. If != null the view originally has action
3279 * but doesn't have any for this inflation.
3280 */
3281 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003282 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003283
Anthony Chenad4d1582017-04-10 16:07:58 -07003284 private boolean mTintActionButtons;
3285 private boolean mInNightMode;
3286
Adrian Roos70d7aa32017-01-11 15:39:06 -08003287 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003288 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003289 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003290 * @param context
3291 * A {@link Context} that will be used by the Builder to construct the
3292 * RemoteViews. The Context will not be held past the lifetime of this Builder
3293 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003294 * @param channelId
3295 * The constructed Notification will be posted on this
3296 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3297 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003299 public Builder(Context context, String channelId) {
3300 this(context, (Notification) null);
3301 mN.mChannelId = channelId;
3302 }
3303
3304 /**
3305 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3306 * instead. All posted Notifications must specify a NotificationChannel Id.
3307 */
3308 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003309 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003310 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003311 }
3312
Joe Onoratocb109a02011-01-18 17:57:41 -08003313 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003314 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003315 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003316 public Builder(Context context, Notification toAdopt) {
3317 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003318 Resources res = mContext.getResources();
3319 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3320
3321 if (res.getBoolean(R.bool.config_enableNightMode)) {
3322 Configuration currentConfig = res.getConfiguration();
3323 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3324 == Configuration.UI_MODE_NIGHT_YES;
3325 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003326
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003327 if (toAdopt == null) {
3328 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003329 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3330 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3331 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003332 mN.priority = PRIORITY_DEFAULT;
3333 mN.visibility = VISIBILITY_PRIVATE;
3334 } else {
3335 mN = toAdopt;
3336 if (mN.actions != null) {
3337 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003338 }
3339
Selim Cineke7238dd2017-12-14 17:48:32 -08003340 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3341 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3342 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003343 }
3344
Selim Cinek4ac6f602016-06-13 15:47:03 -07003345 if (mN.getSmallIcon() == null && mN.icon != 0) {
3346 setSmallIcon(mN.icon);
3347 }
3348
3349 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3350 setLargeIcon(mN.largeIcon);
3351 }
3352
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003353 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3354 if (!TextUtils.isEmpty(templateClass)) {
3355 final Class<? extends Style> styleClass
3356 = getNotificationStyleClass(templateClass);
3357 if (styleClass == null) {
3358 Log.d(TAG, "Unknown style class: " + templateClass);
3359 } else {
3360 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003361 final Constructor<? extends Style> ctor =
3362 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003363 ctor.setAccessible(true);
3364 final Style style = ctor.newInstance();
3365 style.restoreFromExtras(mN.extras);
3366
3367 if (style != null) {
3368 setStyle(style);
3369 }
3370 } catch (Throwable t) {
3371 Log.e(TAG, "Could not create Style", t);
3372 }
3373 }
3374 }
3375
3376 }
3377 }
3378
Lucas Dupina291d192018-06-07 13:59:42 -07003379 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003380 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003381 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003382 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003383 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003384 }
3385
3386 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003387 * If this notification is duplicative of a Launcher shortcut, sets the
3388 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3389 * the shortcut.
3390 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003391 * This field will be ignored by Launchers that don't support badging, don't show
3392 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003393 *
3394 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3395 * supersedes
3396 */
3397 public Builder setShortcutId(String shortcutId) {
3398 mN.mShortcutId = shortcutId;
3399 return this;
3400 }
3401
3402 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003403 * Sets which icon to display as a badge for this notification.
3404 *
3405 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3406 * {@link #BADGE_ICON_LARGE}.
3407 *
3408 * Note: This value might be ignored, for launchers that don't support badge icons.
3409 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003410 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003411 mN.mBadgeIcon = icon;
3412 return this;
3413 }
3414
3415 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003416 * Sets the group alert behavior for this notification. Use this method to mute this
3417 * notification if alerts for this notification's group should be handled by a different
3418 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003419 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3420 * mute. For example, if you want only the summary of your group to make noise, all
3421 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003422 *
3423 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3424 */
3425 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3426 mN.mGroupAlertBehavior = groupAlertBehavior;
3427 return this;
3428 }
3429
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003430 /**
3431 * Sets the intent that will be used to display app content in a floating window
3432 * over the existing foreground activity.
3433 *
3434 * <p>This intent will be ignored unless this notification is posted to a channel that
3435 * allows {@link NotificationChannel#canOverlayApps() app overlays}.</p>
3436 *
3437 * <p>Notifications with a valid and allowed app overlay intent will be displayed as
3438 * floating windows outside of the notification shade on unlocked devices. When a user
3439 * interacts with one of these windows, this app overlay intent will be invoked and
3440 * displayed.</p>
3441 */
3442 public Builder setAppOverlayIntent(PendingIntent intent) {
3443 mN.mAppOverlayIntent = intent;
3444 return this;
3445 }
3446
Jeff Sharkey000ce802017-04-29 13:13:27 -06003447 /** @removed */
3448 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003449 public Builder setChannel(String channelId) {
3450 mN.mChannelId = channelId;
3451 return this;
3452 }
3453
3454 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003455 * Specifies the channel the notification should be delivered on.
3456 */
3457 public Builder setChannelId(String channelId) {
3458 mN.mChannelId = channelId;
3459 return this;
3460 }
3461
Jeff Sharkey000ce802017-04-29 13:13:27 -06003462 /** @removed */
3463 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003464 public Builder setTimeout(long durationMs) {
3465 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003466 return this;
3467 }
3468
3469 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003470 * Specifies a duration in milliseconds after which this notification should be canceled,
3471 * if it is not already canceled.
3472 */
3473 public Builder setTimeoutAfter(long durationMs) {
3474 mN.mTimeout = durationMs;
3475 return this;
3476 }
3477
3478 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003479 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003480 *
3481 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3482 * shown anymore by default and must be opted into by using
3483 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003484 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003485 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003486 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003487 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003488 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003489 return this;
3490 }
3491
Joe Onoratocb109a02011-01-18 17:57:41 -08003492 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003493 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003494 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003495 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3496 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003497 */
3498 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003500 return this;
3501 }
3502
3503 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003504 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003505 *
3506 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003507 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003508 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003509 * Useful when showing an elapsed time (like an ongoing phone call).
3510 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003511 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003512 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003513 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003514 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003515 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003516 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003517 */
3518 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003520 return this;
3521 }
3522
3523 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003524 * Sets the Chronometer to count down instead of counting up.
3525 *
3526 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3527 * If it isn't set the chronometer will count up.
3528 *
3529 * @see #setUsesChronometer(boolean)
3530 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003531 public Builder setChronometerCountDown(boolean countDown) {
3532 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003533 return this;
3534 }
3535
3536 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003537 * Set the small icon resource, which will be used to represent the notification in the
3538 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003539 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003540
3541 * The platform template for the expanded view will draw this icon in the left, unless a
3542 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3543 * icon will be moved to the right-hand side.
3544 *
3545
3546 * @param icon
3547 * A resource ID in the application's package of the drawable to use.
3548 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003549 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003550 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003551 return setSmallIcon(icon != 0
3552 ? Icon.createWithResource(mContext, icon)
3553 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003554 }
3555
Joe Onoratocb109a02011-01-18 17:57:41 -08003556 /**
3557 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3558 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3559 * LevelListDrawable}.
3560 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003561 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 * @param level The level to use for the icon.
3563 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 * @see Notification#icon
3565 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003567 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003568 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003569 return setSmallIcon(icon);
3570 }
3571
3572 /**
3573 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003574 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003575 * {@link #setLargeIcon(Bitmap) large icon}).
3576 *
3577 * @param icon An Icon object to use.
3578 * @see Notification#icon
3579 */
3580 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003581 mN.setSmallIcon(icon);
3582 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3583 mN.icon = icon.getResId();
3584 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003585 return this;
3586 }
3587
Joe Onoratocb109a02011-01-18 17:57:41 -08003588 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003589 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003590 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003591 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003592 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003593 return this;
3594 }
3595
Joe Onoratocb109a02011-01-18 17:57:41 -08003596 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003597 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003598 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003599 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003600 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003601 return this;
3602 }
3603
Joe Onoratocb109a02011-01-18 17:57:41 -08003604 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003605 * This provides some additional information that is displayed in the notification. No
3606 * guarantees are given where exactly it is displayed.
3607 *
3608 * <p>This information should only be provided if it provides an essential
3609 * benefit to the understanding of the notification. The more text you provide the
3610 * less readable it becomes. For example, an email client should only provide the account
3611 * name here if more than one email account has been added.</p>
3612 *
3613 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3614 * notification header area.
3615 *
3616 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3617 * this will be shown in the third line of text in the platform notification template.
3618 * You should not be using {@link #setProgress(int, int, boolean)} at the
3619 * same time on those versions; they occupy the same place.
3620 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003621 */
3622 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003624 return this;
3625 }
3626
3627 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003628 * Provides text that will appear as a link to your application's settings.
3629 *
3630 * <p>This text does not appear within notification {@link Style templates} but may
3631 * appear when the user uses an affordance to learn more about the notification.
3632 * Additionally, this text will not appear unless you provide a valid link target by
3633 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3634 *
3635 * <p>This text is meant to be concise description about what the user can customize
3636 * when they click on this link. The recommended maximum length is 40 characters.
3637 * @param text
3638 * @return
3639 */
3640 public Builder setSettingsText(CharSequence text) {
3641 mN.mSettingsText = safeCharSequence(text);
3642 return this;
3643 }
3644
3645 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003646 * Set the remote input history.
3647 *
3648 * This should be set to the most recent inputs that have been sent
3649 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3650 * longer relevant (e.g. for chat notifications once the other party has responded).
3651 *
3652 * The most recent input must be stored at the 0 index, the second most recent at the
3653 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3654 * and how much of each individual input is shown.
3655 *
3656 * <p>Note: The reply text will only be shown on notifications that have least one action
3657 * with a {@code RemoteInput}.</p>
3658 */
3659 public Builder setRemoteInputHistory(CharSequence[] text) {
3660 if (text == null) {
3661 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3662 } else {
3663 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3664 CharSequence[] safe = new CharSequence[N];
3665 for (int i = 0; i < N; i++) {
3666 safe[i] = safeCharSequence(text[i]);
3667 }
3668 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3669 }
3670 return this;
3671 }
3672
3673 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003674 * Sets whether remote history entries view should have a spinner.
3675 * @hide
3676 */
3677 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3678 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3679 return this;
3680 }
3681
3682 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003683 * Sets whether smart reply buttons should be hidden.
3684 * @hide
3685 */
3686 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3687 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3688 return this;
3689 }
3690
3691 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003692 * Sets the number of items this notification represents. May be displayed as a badge count
3693 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003694 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003695 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003697 return this;
3698 }
3699
Joe Onoratocb109a02011-01-18 17:57:41 -08003700 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003701 * A small piece of additional information pertaining to this notification.
3702 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003703 * The platform template will draw this on the last line of the notification, at the far
3704 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003705 *
3706 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3707 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3708 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003710 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003711 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003712 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003713 return this;
3714 }
3715
Joe Onoratocb109a02011-01-18 17:57:41 -08003716 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003717 * Set the progress this notification represents.
3718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003719 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003720 */
3721 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.extras.putInt(EXTRA_PROGRESS, progress);
3723 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3724 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003725 return this;
3726 }
3727
3728 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003729 * Supply a custom RemoteViews to use instead of the platform template.
3730 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003731 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003732 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003733 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003734 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003735 return setCustomContentView(views);
3736 }
3737
3738 /**
3739 * Supply custom RemoteViews to use instead of the platform template.
3740 *
3741 * This will override the layout that would otherwise be constructed by this Builder
3742 * object.
3743 */
3744 public Builder setCustomContentView(RemoteViews contentView) {
3745 mN.contentView = contentView;
3746 return this;
3747 }
3748
3749 /**
3750 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3751 *
3752 * This will override the expanded layout that would otherwise be constructed by this
3753 * Builder object.
3754 */
3755 public Builder setCustomBigContentView(RemoteViews contentView) {
3756 mN.bigContentView = contentView;
3757 return this;
3758 }
3759
3760 /**
3761 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3762 *
3763 * This will override the heads-up layout that would otherwise be constructed by this
3764 * Builder object.
3765 */
3766 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3767 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003768 return this;
3769 }
3770
Joe Onoratocb109a02011-01-18 17:57:41 -08003771 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003772 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3773 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003774 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3775 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3776 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003777 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003778 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003779 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003780 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003781 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003782 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003783 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003784 return this;
3785 }
3786
Joe Onoratocb109a02011-01-18 17:57:41 -08003787 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003788 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3789 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003790 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003791 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003792 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003793 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003794 return this;
3795 }
3796
Joe Onoratocb109a02011-01-18 17:57:41 -08003797 /**
3798 * An intent to launch instead of posting the notification to the status bar.
3799 * Only for use with extremely high-priority notifications demanding the user's
3800 * <strong>immediate</strong> attention, such as an incoming phone call or
3801 * alarm clock that the user has explicitly set to a particular time.
3802 * If this facility is used for something else, please give the user an option
3803 * to turn it off and use a normal notification, as this can be extremely
3804 * disruptive.
3805 *
Chris Wren47c20a12014-06-18 17:27:29 -04003806 * <p>
3807 * The system UI may choose to display a heads-up notification, instead of
3808 * launching this intent, while the user is using the device.
3809 * </p>
3810 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003811 * @param intent The pending intent to launch.
3812 * @param highPriority Passing true will cause this notification to be sent
3813 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003814 *
3815 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003816 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003817 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003818 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003819 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3820 return this;
3821 }
3822
Joe Onoratocb109a02011-01-18 17:57:41 -08003823 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003824 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003825 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003826 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003827 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003828 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003829 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003830 return this;
3831 }
3832
Joe Onoratocb109a02011-01-18 17:57:41 -08003833 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003834 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003835 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003836 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003837 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003838 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003839 setTicker(tickerText);
3840 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003841 return this;
3842 }
3843
Joe Onoratocb109a02011-01-18 17:57:41 -08003844 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003845 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003846 *
3847 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003848 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3849 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003850 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003851 public Builder setLargeIcon(Bitmap b) {
3852 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3853 }
3854
3855 /**
3856 * Add a large icon to the notification content view.
3857 *
3858 * In the platform template, this image will be shown on the left of the notification view
3859 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3860 * badge atop the large icon).
3861 */
3862 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003863 mN.mLargeIcon = icon;
3864 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003865 return this;
3866 }
3867
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003869 * Set the sound to play.
3870 *
John Spurlockc0650f022014-07-19 13:22:39 -04003871 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3872 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003873 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003874 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003875 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003876 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003877 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003878 mN.sound = sound;
3879 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003880 return this;
3881 }
3882
Joe Onoratocb109a02011-01-18 17:57:41 -08003883 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003884 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003885 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003886 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3887 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003888 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003889 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003890 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003892 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003893 mN.sound = sound;
3894 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003895 return this;
3896 }
3897
Joe Onoratocb109a02011-01-18 17:57:41 -08003898 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003899 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3900 * use during playback.
3901 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003902 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003903 * @see Notification#sound
3904 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003905 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003906 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 mN.sound = sound;
3908 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003909 return this;
3910 }
3911
3912 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003913 * Set the vibration pattern to use.
3914 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003915 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3916 * <code>pattern</code> parameter.
3917 *
Chris Wren47c20a12014-06-18 17:27:29 -04003918 * <p>
3919 * A notification that vibrates is more likely to be presented as a heads-up notification.
3920 * </p>
3921 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003922 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003923 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003924 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003925 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003926 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003927 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003928 return this;
3929 }
3930
Joe Onoratocb109a02011-01-18 17:57:41 -08003931 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003932 * Set the desired color for the indicator LED on the device, as well as the
3933 * blink duty cycle (specified in milliseconds).
3934 *
3935
3936 * Not all devices will honor all (or even any) of these values.
3937 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003938 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003939 * @see Notification#ledARGB
3940 * @see Notification#ledOnMS
3941 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003943 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003944 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003945 mN.ledARGB = argb;
3946 mN.ledOnMS = onMs;
3947 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003948 if (onMs != 0 || offMs != 0) {
3949 mN.flags |= FLAG_SHOW_LIGHTS;
3950 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003951 return this;
3952 }
3953
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003955 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003956 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003957
3958 * Ongoing notifications cannot be dismissed by the user, so your application or service
3959 * must take care of canceling them.
3960 *
3961
3962 * They are typically used to indicate a background task that the user is actively engaged
3963 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3964 * (e.g., a file download, sync operation, active network connection).
3965 *
3966
3967 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08003968 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003969 public Builder setOngoing(boolean ongoing) {
3970 setFlag(FLAG_ONGOING_EVENT, ongoing);
3971 return this;
3972 }
3973
Joe Onoratocb109a02011-01-18 17:57:41 -08003974 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003975 * Set whether this notification should be colorized. When set, the color set with
3976 * {@link #setColor(int)} will be used as the background color of this notification.
3977 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003978 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3979 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003980 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003981 * For most styles, the coloring will only be applied if the notification is for a
3982 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003983 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003984 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003985 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07003986 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003987 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003988 */
3989 public Builder setColorized(boolean colorize) {
3990 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3991 return this;
3992 }
3993
3994 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003995 * Set this flag if you would only like the sound, vibrate
3996 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003997 *
3998 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004000 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4001 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4002 return this;
4003 }
4004
Joe Onoratocb109a02011-01-18 17:57:41 -08004005 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004006 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004007 *
4008 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004011 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004012 return this;
4013 }
4014
Joe Onoratocb109a02011-01-18 17:57:41 -08004015 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004016 * Set whether or not this notification should not bridge to other devices.
4017 *
4018 * <p>Some notifications can be bridged to other devices for remote display.
4019 * This hint can be set to recommend this notification not be bridged.
4020 */
4021 public Builder setLocalOnly(boolean localOnly) {
4022 setFlag(FLAG_LOCAL_ONLY, localOnly);
4023 return this;
4024 }
4025
4026 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004027 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004028 * <p>
4029 * The value should be one or more of the following fields combined with
4030 * bitwise-or:
4031 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4032 * <p>
4033 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004034 *
4035 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004036 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004037 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004038 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004039 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004040 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004042 return this;
4043 }
4044
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004045 /**
4046 * Set the priority of this notification.
4047 *
4048 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004049 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004050 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004051 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004052 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004053 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004054 return this;
4055 }
Joe Malin8d40d042012-11-05 11:36:40 -08004056
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004057 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004058 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004059 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004060 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004061 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04004062 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004063 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004064 return this;
4065 }
4066
4067 /**
Chris Wrendde75302014-03-26 17:24:15 -04004068 * Add a person that is relevant to this notification.
4069 *
Chris Wrene6c48932014-09-29 17:19:27 -04004070 * <P>
4071 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004072 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4073 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4074 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004075 * </P>
4076 *
4077 * <P>
4078 * The person should be specified by the {@code String} representation of a
4079 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4080 * </P>
4081 *
4082 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4083 * URIs. The path part of these URIs must exist in the contacts database, in the
4084 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4085 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004086 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4087 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004088 * </P>
4089 *
4090 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004091 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004092 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004093 */
Chris Wrene6c48932014-09-29 17:19:27 -04004094 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004095 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004096 return this;
4097 }
4098
4099 /**
4100 * Add a person that is relevant to this notification.
4101 *
4102 * <P>
4103 * Depending on user preferences, this annotation may allow the notification to pass
4104 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4105 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4106 * appear more prominently in the user interface.
4107 * </P>
4108 *
4109 * <P>
4110 * A person should usually contain a uri in order to benefit from the ranking boost.
4111 * However, even if no uri is provided, it's beneficial to provide other people in the
4112 * notification, such that listeners and voice only devices can announce and handle them
4113 * properly.
4114 * </P>
4115 *
4116 * @param person the person to add.
4117 * @see Notification#EXTRA_PEOPLE_LIST
4118 */
4119 public Builder addPerson(Person person) {
4120 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004121 return this;
4122 }
4123
4124 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004125 * Set this notification to be part of a group of notifications sharing the same key.
4126 * Grouped notifications may display in a cluster or stack on devices which
4127 * support such rendering.
4128 *
4129 * <p>To make this notification the summary for its group, also call
4130 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4131 * {@link #setSortKey}.
4132 * @param groupKey The group key of the group.
4133 * @return this object for method chaining
4134 */
4135 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004136 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004137 return this;
4138 }
4139
4140 /**
4141 * Set this notification to be the group summary for a group of notifications.
4142 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004143 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4144 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004145 * @param isGroupSummary Whether this notification should be a group summary.
4146 * @return this object for method chaining
4147 */
4148 public Builder setGroupSummary(boolean isGroupSummary) {
4149 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4150 return this;
4151 }
4152
4153 /**
4154 * Set a sort key that orders this notification among other notifications from the
4155 * same package. This can be useful if an external sort was already applied and an app
4156 * would like to preserve this. Notifications will be sorted lexicographically using this
4157 * value, although providing different priorities in addition to providing sort key may
4158 * cause this value to be ignored.
4159 *
4160 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004161 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004162 *
4163 * @see String#compareTo(String)
4164 */
4165 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004166 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004167 return this;
4168 }
4169
4170 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004171 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004172 *
Griff Hazen720042b2014-02-24 15:46:56 -08004173 * <p>Values within the Bundle will replace existing extras values in this Builder.
4174 *
4175 * @see Notification#extras
4176 */
Griff Hazen959591e2014-05-15 22:26:18 -07004177 public Builder addExtras(Bundle extras) {
4178 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004179 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004180 }
4181 return this;
4182 }
4183
4184 /**
4185 * Set metadata for this notification.
4186 *
4187 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004188 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004189 * called.
4190 *
Griff Hazen720042b2014-02-24 15:46:56 -08004191 * <p>Replaces any existing extras values with those from the provided Bundle.
4192 * Use {@link #addExtras} to merge in metadata instead.
4193 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004194 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004195 */
Griff Hazen959591e2014-05-15 22:26:18 -07004196 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004197 if (extras != null) {
4198 mUserExtras = extras;
4199 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004200 return this;
4201 }
4202
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004203 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004204 * Get the current metadata Bundle used by this notification Builder.
4205 *
4206 * <p>The returned Bundle is shared with this Builder.
4207 *
4208 * <p>The current contents of this Bundle are copied into the Notification each time
4209 * {@link #build()} is called.
4210 *
4211 * @see Notification#extras
4212 */
4213 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004214 return mUserExtras;
4215 }
4216
4217 private Bundle getAllExtras() {
4218 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4219 saveExtras.putAll(mN.extras);
4220 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004221 }
4222
4223 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004224 * Add an action to this notification. Actions are typically displayed by
4225 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004226 * <p>
4227 * Every action must have an icon (32dp square and matching the
4228 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4229 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4230 * <p>
4231 * A notification in its expanded form can display up to 3 actions, from left to right in
4232 * the order they were added. Actions will not be displayed when the notification is
4233 * collapsed, however, so be sure that any essential functions may be accessed by the user
4234 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004235 *
4236 * @param icon Resource ID of a drawable that represents the action.
4237 * @param title Text describing the action.
4238 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004239 *
4240 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004241 */
Dan Sandler86647982015-05-13 23:41:13 -04004242 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004243 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004244 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004245 return this;
4246 }
4247
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004248 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004249 * Add an action to this notification. Actions are typically displayed by
4250 * the system as a button adjacent to the notification content.
4251 * <p>
4252 * Every action must have an icon (32dp square and matching the
4253 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4254 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4255 * <p>
4256 * A notification in its expanded form can display up to 3 actions, from left to right in
4257 * the order they were added. Actions will not be displayed when the notification is
4258 * collapsed, however, so be sure that any essential functions may be accessed by the user
4259 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4260 *
4261 * @param action The action to add.
4262 */
4263 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004264 if (action != null) {
4265 mActions.add(action);
4266 }
Griff Hazen959591e2014-05-15 22:26:18 -07004267 return this;
4268 }
4269
4270 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004271 * Alter the complete list of actions attached to this notification.
4272 * @see #addAction(Action).
4273 *
4274 * @param actions
4275 * @return
4276 */
4277 public Builder setActions(Action... actions) {
4278 mActions.clear();
4279 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004280 if (actions[i] != null) {
4281 mActions.add(actions[i]);
4282 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004283 }
4284 return this;
4285 }
4286
4287 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004288 * Add a rich notification style to be applied at build time.
4289 *
4290 * @param style Object responsible for modifying the notification style.
4291 */
4292 public Builder setStyle(Style style) {
4293 if (mStyle != style) {
4294 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004295 if (mStyle != null) {
4296 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004297 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4298 } else {
4299 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004300 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004301 }
4302 return this;
4303 }
4304
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004305 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004306 * Returns the style set by {@link #setStyle(Style)}.
4307 */
4308 public Style getStyle() {
4309 return mStyle;
4310 }
4311
4312 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004313 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004314 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004315 * @return The same Builder.
4316 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004317 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004318 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004319 return this;
4320 }
4321
4322 /**
4323 * Supply a replacement Notification whose contents should be shown in insecure contexts
4324 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4325 * @param n A replacement notification, presumably with some or all info redacted.
4326 * @return The same Builder.
4327 */
4328 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004329 if (n != null) {
4330 mN.publicVersion = new Notification();
4331 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4332 } else {
4333 mN.publicVersion = null;
4334 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004335 return this;
4336 }
4337
Griff Hazenb720abe2014-05-20 13:15:30 -07004338 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004339 * Apply an extender to this notification builder. Extenders may be used to add
4340 * metadata or change options on this builder.
4341 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004342 public Builder extend(Extender extender) {
4343 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004344 return this;
4345 }
4346
Dan Sandler4e787062015-06-17 15:09:48 -04004347 /**
4348 * @hide
4349 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004350 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004351 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004352 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004353 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004354 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004355 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004356 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004357 }
4358
Dan Sandler26e81cf2014-05-06 10:01:27 -04004359 /**
4360 * Sets {@link Notification#color}.
4361 *
4362 * @param argb The accent color to use
4363 *
4364 * @return The same Builder.
4365 */
Tor Norbye80756e32015-03-02 09:39:27 -08004366 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004367 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004368 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004369 return this;
4370 }
4371
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004372 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004373 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4374 // This user can never be a badged profile,
4375 // and also includes USER_ALL system notifications.
4376 return null;
4377 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004378 // Note: This assumes that the current user can read the profile badge of the
4379 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004380 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004381 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004382 }
4383
4384 private Bitmap getProfileBadge() {
4385 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004386 if (badge == null) {
4387 return null;
4388 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004389 final int size = mContext.getResources().getDimensionPixelSize(
4390 R.dimen.notification_badge_size);
4391 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004392 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004393 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004394 badge.draw(canvas);
4395 return bitmap;
4396 }
4397
Selim Cinekc848c3a2016-01-13 15:27:30 -08004398 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004399 Bitmap profileBadge = getProfileBadge();
4400
Kenny Guy98193ea2014-07-24 19:54:37 +01004401 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004402 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4403 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004404 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004405 contentView.setDrawableTint(R.id.profile_badge, false,
4406 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004407 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004408 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004409 }
4410
Julia Reynoldsfc640012018-02-21 12:25:27 -05004411 /**
4412 * @hide
4413 */
4414 public boolean usesStandardHeader() {
4415 if (mN.mUsesStandardHeader) {
4416 return true;
4417 }
4418 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4419 if (mN.contentView == null && mN.bigContentView == null) {
4420 return true;
4421 }
4422 }
4423 boolean contentViewUsesHeader = mN.contentView == null
4424 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4425 boolean bigContentViewUsesHeader = mN.bigContentView == null
4426 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4427 return contentViewUsesHeader && bigContentViewUsesHeader;
4428 }
4429
Christoph Studerfe718432014-09-01 18:21:18 +02004430 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004431 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004432 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004433 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004434 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004435 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004436 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004437 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004438 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004439 }
4440
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004441 /**
4442 * Resets the notification header to its original state
4443 */
4444 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004445 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4446 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004447 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004448 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004449 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004450 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004451 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004452 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4453 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004454 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004455 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004456 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004457 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004458 contentView.setImageViewIcon(R.id.profile_badge, null);
4459 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004460 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004461 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004462 }
4463
Selim Cinek384804b2018-04-18 14:31:07 +08004464 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4465 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4466 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004467 }
4468
4469 /**
4470 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004471 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004472 */
Selim Cinek384804b2018-04-18 14:31:07 +08004473 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4474 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004475 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004476 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004477 }
4478
Selim Cinek384804b2018-04-18 14:31:07 +08004479 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4480 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004481 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004482
Christoph Studerfe718432014-09-01 18:21:18 +02004483 resetStandardTemplate(contentView);
4484
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004485 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004486 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004487 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004488 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004489 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4490 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004491 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004492 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004493 if (!p.ambient) {
4494 setTextViewColorPrimary(contentView, R.id.title);
4495 }
Selim Cinek954cc232016-05-20 13:29:23 -07004496 contentView.setViewLayoutWidth(R.id.title, showProgress
4497 ? ViewGroup.LayoutParams.WRAP_CONTENT
4498 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004499 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004500 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004501 int textId = showProgress ? com.android.internal.R.id.text_line_1
4502 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004503 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004504 if (!p.ambient) {
4505 setTextViewColorSecondary(contentView, textId);
4506 }
Selim Cinek41598732016-01-11 16:58:37 -08004507 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004508 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004509
Selim Cinek279fa862016-06-14 10:57:25 -07004510 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004511
Selim Cinek29603462015-11-17 19:04:39 -08004512 return contentView;
4513 }
4514
Selim Cinek48f66b72017-08-18 16:17:51 -07004515 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004516 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004517 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004518 }
4519 return text;
4520 }
4521
Selim Cinek7b9605b2017-01-19 17:36:00 -08004522 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4523 ensureColors();
4524 contentView.setTextColor(id, mPrimaryTextColor);
4525 }
4526
Selim Cinek48f66b72017-08-18 16:17:51 -07004527 private boolean hasForegroundColor() {
4528 return mForegroundColor != COLOR_INVALID;
4529 }
4530
Selim Cinek389edcd2017-05-11 19:16:44 -07004531 /**
4532 * @return the primary text color
4533 * @hide
4534 */
4535 @VisibleForTesting
4536 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004537 ensureColors();
4538 return mPrimaryTextColor;
4539 }
4540
Selim Cinek389edcd2017-05-11 19:16:44 -07004541 /**
4542 * @return the secondary text color
4543 * @hide
4544 */
4545 @VisibleForTesting
4546 public int getSecondaryTextColor() {
4547 ensureColors();
4548 return mSecondaryTextColor;
4549 }
4550
Selim Cinek7b9605b2017-01-19 17:36:00 -08004551 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4552 ensureColors();
4553 contentView.setTextColor(id, mSecondaryTextColor);
4554 }
4555
4556 private void ensureColors() {
4557 int backgroundColor = getBackgroundColor();
4558 if (mPrimaryTextColor == COLOR_INVALID
4559 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004560 || mTextColorsAreForBackground != backgroundColor) {
4561 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004562 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004563 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004564 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004565 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004566 backgroundColor, mInNightMode);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004567 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004568 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004569 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004570 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004571 mSecondaryTextColor, backgroundColor, 4.5);
4572 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004573 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004574 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4575 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4576 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004577 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004578 // We only respect the given colors if worst case Black or White still has
4579 // contrast
4580 boolean backgroundLight = backLum > textLum
4581 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4582 || backLum <= textLum
4583 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004584 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004585 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004586 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004587 mForegroundColor,
4588 backgroundColor,
4589 true /* findFG */,
4590 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004591 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004592 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004593 } else {
4594 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004595 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004596 mForegroundColor,
4597 backgroundColor,
4598 true /* findFG */,
4599 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004600 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004601 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004602 }
4603 } else {
4604 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004605 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004606 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4607 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004608 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004609 backgroundColor) < 4.5f) {
4610 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004611 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004612 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004613 mSecondaryTextColor,
4614 backgroundColor,
4615 true /* findFG */,
4616 4.5f);
4617 } else {
4618 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004619 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004620 mSecondaryTextColor,
4621 backgroundColor,
4622 true /* findFG */,
4623 4.5f);
4624 }
Lucas Dupina291d192018-06-07 13:59:42 -07004625 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004626 mSecondaryTextColor, backgroundLight
4627 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4628 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004629 }
4630 }
4631 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004632 }
4633 }
4634
4635 private void updateBackgroundColor(RemoteViews contentView) {
4636 if (isColorized()) {
4637 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4638 getBackgroundColor());
4639 } else {
4640 // Clear it!
4641 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4642 0);
4643 }
4644 }
4645
Selim Cinek860b6da2015-12-16 19:02:19 -08004646 /**
4647 * @param remoteView the remote view to update the minheight in
4648 * @param hasMinHeight does it have a mimHeight
4649 * @hide
4650 */
4651 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4652 int minHeight = 0;
4653 if (hasMinHeight) {
4654 // we need to set the minHeight of the notification
4655 minHeight = mContext.getResources().getDimensionPixelSize(
4656 com.android.internal.R.dimen.notification_min_content_height);
4657 }
4658 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4659 }
4660
Selim Cinek29603462015-11-17 19:04:39 -08004661 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004662 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4663 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4664 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4665 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004666 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004667 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004668 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004669 contentView.setProgressBackgroundTintList(
4670 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4671 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004672 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004673 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004674 contentView.setProgressTintList(R.id.progress, colorStateList);
4675 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004676 }
Selim Cinek29603462015-11-17 19:04:39 -08004677 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004678 } else {
4679 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004680 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004681 }
Joe Onorato561d3852010-11-20 18:09:34 -08004682 }
4683
Selim Cinek384804b2018-04-18 14:31:07 +08004684 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4685 TemplateBindResult result) {
4686 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004687 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004688 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004689 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4690 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4691 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4692 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4693 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004694 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004695 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004696 }
4697 }
4698
Selim Cinek1c72fa02018-04-23 18:00:54 +08004699 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4700 int marginEnd = 0;
4701 int contentMargin = mContext.getResources().getDimensionPixelSize(
4702 R.dimen.notification_content_margin_end);
4703 int iconSize = mContext.getResources().getDimensionPixelSize(
4704 R.dimen.notification_right_icon_size);
4705 if (replyIconShown) {
4706 // The size of the reply icon
4707 marginEnd += iconSize;
4708
4709 int replyInset = mContext.getResources().getDimensionPixelSize(
4710 R.dimen.notification_reply_inset);
4711 // We're subtracting the inset of the reply icon to make sure it's
4712 // aligned nicely on the right, and remove it from the following padding
4713 marginEnd -= replyInset * 2;
4714 }
4715 if (largeIconShown) {
4716 // adding size of the right icon
4717 marginEnd += iconSize;
4718
4719 if (replyIconShown) {
4720 // We also add some padding to the reply icon if it's around
4721 marginEnd += contentMargin;
4722 }
4723 }
4724 if (replyIconShown || largeIconShown) {
4725 // The padding to the content
4726 marginEnd += contentMargin;
4727 }
4728 return marginEnd;
4729 }
4730
Selim Cinek384804b2018-04-18 14:31:07 +08004731 /**
4732 * Bind the large icon.
4733 * @return if the largeIcon is visible
4734 */
4735 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004736 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4737 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4738 }
Selim Cinek88188f22017-09-19 16:46:56 -07004739 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4740 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004741 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4742 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4743 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004744 }
Selim Cinek384804b2018-04-18 14:31:07 +08004745 return showLargeIcon;
4746 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004747
Selim Cinek384804b2018-04-18 14:31:07 +08004748 /**
4749 * Bind the reply icon.
4750 * @return if the reply icon is visible
4751 */
4752 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4753 boolean actionVisible = !hideReplyIcon;
4754 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004755 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004756 action = findReplyAction();
4757 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004758 }
Selim Cinek384804b2018-04-18 14:31:07 +08004759 if (actionVisible) {
4760 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4761 contentView.setDrawableTint(R.id.reply_icon_action,
4762 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004763 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004764 PorterDuff.Mode.SRC_ATOP);
4765 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4766 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4767 } else {
4768 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4769 }
4770 contentView.setViewVisibility(R.id.reply_icon_action,
4771 actionVisible ? View.VISIBLE : View.GONE);
4772 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004773 }
4774
4775 private Action findReplyAction() {
4776 ArrayList<Action> actions = mActions;
4777 if (mOriginalActions != null) {
4778 actions = mOriginalActions;
4779 }
4780 int numActions = actions.size();
4781 for (int i = 0; i < numActions; i++) {
4782 Action action = actions.get(i);
4783 if (hasValidRemoteInput(action)) {
4784 return action;
4785 }
4786 }
4787 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004788 }
4789
Selim Cinekafeed292017-12-12 17:32:44 -08004790 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4791 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004792 bindSmallIcon(contentView, ambient);
4793 bindHeaderAppName(contentView, ambient);
4794 if (!ambient) {
4795 // Ambient view does not have these
4796 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004797 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004798 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004799 bindProfileBadge(contentView);
4800 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004801 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004802 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004803 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004804 }
4805
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004806 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004807 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004808 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4809 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4810 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4811 }
4812
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004813 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004814 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004815 contentView.setDrawableTint(R.id.expand_button, false, color,
4816 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004817 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004818 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004819 }
4820
4821 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4822 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004823 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004824 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004825 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4826 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4827 contentView.setLong(R.id.chronometer, "setBase",
4828 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4829 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004830 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004831 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004832 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004833 } else {
4834 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4835 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004836 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004837 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004838 } else {
4839 // We still want a time to be set but gone, such that we can show and hide it
4840 // on demand in case it's a child notification without anything in the header
4841 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004842 }
4843 }
4844
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004845 private void bindHeaderText(RemoteViews contentView) {
4846 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4847 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004848 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004849 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004850 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004851 if (headerText == null
4852 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4853 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4854 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4855 }
4856 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004857 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004858 contentView.setTextViewText(R.id.header_text, processTextSpans(
4859 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004860 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004861 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4862 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004863 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004864 }
4865 }
4866
Selim Cinekafeed292017-12-12 17:32:44 -08004867 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4868 if (!TextUtils.isEmpty(secondaryText)) {
4869 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4870 processLegacyText(secondaryText)));
4871 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4872 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4873 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4874 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4875 }
4876 }
4877
Adrian Rooseba05822016-04-22 17:09:27 -07004878 /**
4879 * @hide
4880 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01004881 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004882 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004883 CharSequence name = null;
4884 final PackageManager pm = mContext.getPackageManager();
4885 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4886 // only system packages which lump together a bunch of unrelated stuff
4887 // may substitute a different name to make the purpose of the
4888 // notification more clear. the correct package label should always
4889 // be accessible via SystemUI.
4890 final String pkg = mContext.getPackageName();
4891 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4892 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4893 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4894 name = subName;
4895 } else {
4896 Log.w(TAG, "warning: pkg "
4897 + pkg + " attempting to substitute app name '" + subName
4898 + "' without holding perm "
4899 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4900 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004901 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004902 if (TextUtils.isEmpty(name)) {
4903 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4904 }
4905 if (TextUtils.isEmpty(name)) {
4906 // still nothing?
4907 return null;
4908 }
4909
4910 return String.valueOf(name);
4911 }
Adrian Roos487374f2017-01-11 15:48:14 -08004912 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004913 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004914 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004915 setTextViewColorPrimary(contentView, R.id.app_name_text);
4916 } else {
4917 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004918 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004919 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004920 }
4921
Adrian Roos487374f2017-01-11 15:48:14 -08004922 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004923 if (mN.mSmallIcon == null && mN.icon != 0) {
4924 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4925 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004926 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004927 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004928 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004929 }
4930
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004931 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004932 * @return true if the built notification will show the time or the chronometer; false
4933 * otherwise
4934 */
4935 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004936 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004937 }
4938
Christoph Studerfe718432014-09-01 18:21:18 +02004939 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004940 // actions_container is only reset when there are no actions to avoid focus issues with
4941 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004942 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004943 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004944
4945 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4946 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004947 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4948 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004949
4950 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4951 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4952 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4953 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004954
Selim Cineked64a142018-02-06 18:06:01 -08004955 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4956 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004957 }
4958
Selim Cinek384804b2018-04-18 14:31:07 +08004959 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4960 TemplateBindResult result) {
4961 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4962 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004963 }
4964
Adrian Roos70d7aa32017-01-11 15:39:06 -08004965 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004966 StandardTemplateParams p, TemplateBindResult result) {
4967 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004968
Christoph Studerfe718432014-09-01 18:21:18 +02004969 resetStandardTemplateWithActions(big);
4970
Adrian Roose458aa82015-12-08 16:17:19 -08004971 boolean validRemoteInput = false;
4972
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004973 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004974 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004975 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004976 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004977 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004978 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004979 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004980 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004981 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004982 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004983 boolean actionHasValidInput = hasValidRemoteInput(action);
4984 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004985
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004986 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004987 p.ambient);
4988 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004989 // Clear the drawable
4990 button.setInt(R.id.action0, "setBackgroundResource", 0);
4991 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004992 big.addView(R.id.actions, button);
4993 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004994 } else {
4995 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004996 }
Adrian Roose458aa82015-12-08 16:17:19 -08004997
4998 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004999 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005000 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5001 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005002 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005003 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005004 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5005 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005006 big.setTextViewText(R.id.notification_material_reply_text_1,
5007 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005008 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01005009 big.setViewVisibility(R.id.notification_material_reply_progress,
5010 showSpinner ? View.VISIBLE : View.GONE);
5011 big.setProgressIndeterminateTintList(
5012 R.id.notification_material_reply_progress,
5013 ColorStateList.valueOf(
5014 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08005015
Selim Cinekbee4e072018-05-21 22:06:43 -07005016 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5017 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005018 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005019 big.setTextViewText(R.id.notification_material_reply_text_2,
5020 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005021 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08005022
Selim Cinekbee4e072018-05-21 22:06:43 -07005023 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5024 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005025 big.setViewVisibility(
5026 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005027 big.setTextViewText(R.id.notification_material_reply_text_3,
5028 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005029 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08005030 }
5031 }
5032 }
5033
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005034 return big;
5035 }
5036
Adrian Roose458aa82015-12-08 16:17:19 -08005037 private boolean hasValidRemoteInput(Action action) {
5038 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5039 // Weird actions
5040 return false;
5041 }
5042
5043 RemoteInput[] remoteInputs = action.getRemoteInputs();
5044 if (remoteInputs == null) {
5045 return false;
5046 }
5047
5048 for (RemoteInput r : remoteInputs) {
5049 CharSequence[] choices = r.getChoices();
5050 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5051 return true;
5052 }
5053 }
5054 return false;
5055 }
5056
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005057 /**
5058 * Construct a RemoteViews for the final 1U notification layout. In order:
5059 * 1. Custom contentView from the caller
5060 * 2. Style's proposed content view
5061 * 3. Standard template view
5062 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005063 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005064 return createContentView(false /* increasedheight */ );
5065 }
5066
5067 /**
5068 * Construct a RemoteViews for the smaller content view.
5069 *
5070 * @param increasedHeight true if this layout be created with an increased height. Some
5071 * styles may support showing more then just that basic 1U size
5072 * and the system may decide to render important notifications
5073 * slightly bigger even when collapsed.
5074 *
5075 * @hide
5076 */
5077 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005078 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005079 return mN.contentView;
5080 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005081 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005082 if (styleView != null) {
5083 return styleView;
5084 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005085 }
Selim Cinek384804b2018-04-18 14:31:07 +08005086 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005087 }
5088
Selim Cineka7679b62017-05-10 16:33:25 -07005089 private boolean useExistingRemoteView() {
5090 return mStyle == null || (!mStyle.displayCustomViewInline()
5091 && !mRebuildStyledRemoteViews);
5092 }
5093
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005094 /**
5095 * Construct a RemoteViews for the final big notification layout.
5096 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005097 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005098 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005099 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005100 return mN.bigContentView;
5101 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005102 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005103 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005104 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005105 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5106 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005107 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005108 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005109 return result;
5110 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005111
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005112 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005113 * Construct a RemoteViews for the final notification header only. This will not be
5114 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005115 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005116 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005117 * @hide
5118 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005119 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005120 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5121 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005122 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005123 ambient ? R.layout.notification_template_ambient_header
5124 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005125 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005126 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005127 if (colorized != null) {
5128 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5129 } else {
5130 mN.extras.remove(EXTRA_COLORIZED);
5131 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005132 return header;
5133 }
5134
Adrian Roos487374f2017-01-11 15:48:14 -08005135 /**
5136 * Construct a RemoteViews for the ambient version of the notification.
5137 *
5138 * @hide
5139 */
5140 public RemoteViews makeAmbientNotification() {
5141 RemoteViews ambient = applyStandardTemplateWithActions(
5142 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005143 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5144 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005145 return ambient;
5146 }
5147
Selim Cinek29603462015-11-17 19:04:39 -08005148 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005149 if (result != null) {
5150 result.setViewVisibility(R.id.text_line_1, View.GONE);
5151 }
Selim Cinek29603462015-11-17 19:04:39 -08005152 }
5153
Selim Cinek6743c0b2017-01-18 18:24:01 -08005154 /**
5155 * Adapt the Notification header if this view is used as an expanded view.
5156 *
5157 * @hide
5158 */
5159 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005160 if (result != null) {
5161 result.setBoolean(R.id.notification_header, "setExpanded", true);
5162 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005163 }
5164
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005165 /**
5166 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005167 *
5168 * @param increasedHeight true if this layout be created with an increased height. Some
5169 * styles may support showing more then just that basic 1U size
5170 * and the system may decide to render important notifications
5171 * slightly bigger even when collapsed.
5172 *
5173 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005174 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005175 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005176 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005177 return mN.headsUpContentView;
5178 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005179 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5180 if (styleView != null) {
5181 return styleView;
5182 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005183 } else if (mActions.size() == 0) {
5184 return null;
5185 }
5186
Selim Cinekbee4e072018-05-21 22:06:43 -07005187 // We only want at most a single remote input history to be shown here, otherwise
5188 // the content would become squished.
5189 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5190 .setMaxRemoteInputHistory(1);
5191 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5192 p,
5193 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005194 }
5195
Selim Cinek624c02db2015-12-14 21:00:02 -08005196 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005197 * Construct a RemoteViews for the final heads-up notification layout.
5198 */
5199 public RemoteViews createHeadsUpContentView() {
5200 return createHeadsUpContentView(false /* useIncreasedHeight */);
5201 }
5202
5203 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005204 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5205 *
5206 * @hide
5207 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005208 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005209 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005210 return makePublicView(false /* ambient */);
5211 }
5212
5213 /**
5214 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5215 *
5216 * @hide
5217 */
5218 public RemoteViews makePublicAmbientNotification() {
5219 return makePublicView(true /* ambient */);
5220 }
5221
5222 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005223 if (mN.publicVersion != null) {
5224 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005225 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005226 }
5227 Bundle savedBundle = mN.extras;
5228 Style style = mStyle;
5229 mStyle = null;
5230 Icon largeIcon = mN.mLargeIcon;
5231 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005232 Bitmap largeIconLegacy = mN.largeIcon;
5233 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005234 ArrayList<Action> actions = mActions;
5235 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005236 Bundle publicExtras = new Bundle();
5237 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5238 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5239 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5240 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005241 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5242 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005243 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5244 if (appName != null) {
5245 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5246 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005247 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005248 RemoteViews view;
5249 if (ambient) {
5250 publicExtras.putCharSequence(EXTRA_TITLE,
5251 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5252 view = makeAmbientNotification();
5253 } else{
5254 view = makeNotificationHeader(false /* ambient */);
5255 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5256 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005257 mN.extras = savedBundle;
5258 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005259 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005260 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005261 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005262 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005263 }
5264
Selim Cinek6743c0b2017-01-18 18:24:01 -08005265 /**
5266 * Construct a content view for the display when low - priority
5267 *
5268 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5269 * a new subtext is created consisting of the content of the
5270 * notification.
5271 * @hide
5272 */
5273 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5274 int color = mN.color;
5275 mN.color = COLOR_DEFAULT;
5276 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5277 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5278 CharSequence newSummary = createSummaryText();
5279 if (!TextUtils.isEmpty(newSummary)) {
5280 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5281 }
5282 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005283
Adrian Roos6f6e1592017-05-02 16:22:53 -07005284 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005285 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005286 if (summary != null) {
5287 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5288 } else {
5289 mN.extras.remove(EXTRA_SUB_TEXT);
5290 }
5291 mN.color = color;
5292 return header;
5293 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005294
Selim Cinek6743c0b2017-01-18 18:24:01 -08005295 private CharSequence createSummaryText() {
5296 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5297 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5298 return titleText;
5299 }
5300 SpannableStringBuilder summary = new SpannableStringBuilder();
5301 if (titleText == null) {
5302 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5303 }
5304 BidiFormatter bidi = BidiFormatter.getInstance();
5305 if (titleText != null) {
5306 summary.append(bidi.unicodeWrap(titleText));
5307 }
5308 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5309 if (titleText != null && contentText != null) {
5310 summary.append(bidi.unicodeWrap(mContext.getText(
5311 R.string.notification_header_divider_symbol_with_spaces)));
5312 }
5313 if (contentText != null) {
5314 summary.append(bidi.unicodeWrap(contentText));
5315 }
5316 return summary;
5317 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005318
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005319 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005320 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005321 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005322 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005323 emphazisedMode ? getEmphasizedActionLayoutResource()
5324 : tombstone ? getActionTombstoneLayoutResource()
5325 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005326 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005327 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005328 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005329 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005330 if (action.mRemoteInputs != null) {
5331 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5332 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005333 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005334 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005335 CharSequence title = action.title;
5336 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005337 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005338 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005339 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005340 } else {
5341 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005342 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005343 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005344 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005345 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005346 int rippleColor;
5347 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5348 if (hasColorOverride) {
5349 // There's a span spanning the full text, let's take it and use it as the
5350 // background color
5351 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005352 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005353 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005354 button.setTextColor(R.id.action0, textColor);
5355 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005356 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005357 rippleColor = resolveContrastColor();
5358 button.setTextColor(R.id.action0, rippleColor);
5359 } else {
5360 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005361 }
Selim Cinek396caca2018-04-10 17:46:46 -07005362 // We only want about 20% alpha for the ripple
5363 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5364 button.setColorStateList(R.id.action0, "setRippleColor",
5365 ColorStateList.valueOf(rippleColor));
5366 button.setColorStateList(R.id.action0, "setButtonBackground",
5367 ColorStateList.valueOf(background));
5368 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005369 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005370 button.setTextViewText(R.id.action0, processTextSpans(
5371 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005372 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005373 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005374 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005375 button.setTextColor(R.id.action0,
5376 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005377 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005378 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005379 return button;
5380 }
5381
Joe Onoratocb109a02011-01-18 17:57:41 -08005382 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005383 * Ensures contrast on color spans against a background color. also returns the color of the
5384 * text if a span was found that spans over the whole text.
5385 *
5386 * @param charSequence the charSequence on which the spans are
5387 * @param background the background color to ensure the contrast against
5388 * @param outResultColor an array in which a color will be returned as the first element if
5389 * there exists a full length color span.
5390 * @return the contrasted charSequence
5391 */
5392 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5393 ColorStateList[] outResultColor) {
5394 if (charSequence instanceof Spanned) {
5395 Spanned ss = (Spanned) charSequence;
5396 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5397 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5398 for (Object span : spans) {
5399 Object resultSpan = span;
5400 int spanStart = ss.getSpanStart(span);
5401 int spanEnd = ss.getSpanEnd(span);
5402 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5403 if (resultSpan instanceof CharacterStyle) {
5404 resultSpan = ((CharacterStyle) span).getUnderlying();
5405 }
5406 if (resultSpan instanceof TextAppearanceSpan) {
5407 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5408 ColorStateList textColor = originalSpan.getTextColor();
5409 if (textColor != null) {
5410 int[] colors = textColor.getColors();
5411 int[] newColors = new int[colors.length];
5412 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005413 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005414 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005415 }
5416 textColor = new ColorStateList(textColor.getStates().clone(),
5417 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005418 if (fullLength) {
5419 outResultColor[0] = textColor;
5420 // Let's drop the color from the span
5421 textColor = null;
5422 }
Selim Cinek981962e2016-07-20 20:41:58 -07005423 resultSpan = new TextAppearanceSpan(
5424 originalSpan.getFamily(),
5425 originalSpan.getTextStyle(),
5426 originalSpan.getTextSize(),
5427 textColor,
5428 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005429 }
5430 } else if (resultSpan instanceof ForegroundColorSpan) {
5431 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5432 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005433 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005434 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005435 if (fullLength) {
5436 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005437 resultSpan = null;
5438 } else {
5439 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005440 }
5441 } else {
5442 resultSpan = span;
5443 }
Selim Cinek396caca2018-04-10 17:46:46 -07005444 if (resultSpan != null) {
5445 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5446 }
Selim Cinek981962e2016-07-20 20:41:58 -07005447 }
5448 return builder;
5449 }
5450 return charSequence;
5451 }
5452
5453 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005454 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005455 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005456 */
5457 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005458 if (!mIsLegacyInitialized) {
5459 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5460 < Build.VERSION_CODES.LOLLIPOP;
5461 mIsLegacyInitialized = true;
5462 }
5463 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005464 }
5465
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005466 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005467 return processLegacyText(charSequence, false /* ambient */);
5468 }
5469
5470 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5471 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5472 boolean wantLightText = ambient;
5473 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005474 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005475 } else {
5476 return charSequence;
5477 }
5478 }
5479
Dan Sandler26e81cf2014-05-06 10:01:27 -04005480 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005481 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005482 */
Adrian Roos487374f2017-01-11 15:48:14 -08005483 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5484 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005485 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005486 int color;
5487 if (ambient) {
5488 color = resolveAmbientColor();
5489 } else if (isColorized()) {
5490 color = getPrimaryTextColor();
5491 } else {
5492 color = resolveContrastColor();
5493 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005494 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005495 contentView.setDrawableTint(R.id.icon, false, color,
5496 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005497
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005498 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005499 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005500 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005501 }
5502
Dan Sandler26e81cf2014-05-06 10:01:27 -04005503 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005504 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005505 * if it's grayscale).
5506 */
5507 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005508 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5509 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005510 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005511 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005512 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5513 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005514 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005515 }
5516
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005517 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005518 if (mN.color != COLOR_DEFAULT) {
5519 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005520 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005521 }
5522
Adrian Roos4ff3b122016-02-01 12:26:13 -08005523 int resolveContrastColor() {
5524 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5525 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005526 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005527
Selim Cinekac5f0272017-05-02 16:05:41 -07005528 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005529 int background = mContext.getColor(
5530 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005531 if (mN.color == COLOR_DEFAULT) {
5532 ensureColors();
Lucas Dupinf03e7522018-06-25 16:21:13 -07005533 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005534 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005535 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005536 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005537 }
5538 if (Color.alpha(color) < 255) {
5539 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005540 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005541 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005542 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005543 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005544 }
5545
Selim Cinek4717d862018-04-19 09:19:15 +08005546 int resolveNeutralColor() {
5547 if (mNeutralColor != COLOR_INVALID) {
5548 return mNeutralColor;
5549 }
5550 int background = mContext.getColor(
5551 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005552 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5553 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005554 if (Color.alpha(mNeutralColor) < 255) {
5555 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005556 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005557 }
5558 return mNeutralColor;
5559 }
5560
Adrian Roos487374f2017-01-11 15:48:14 -08005561 int resolveAmbientColor() {
5562 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5563 return mCachedAmbientColor;
5564 }
Lucas Dupina291d192018-06-07 13:59:42 -07005565 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005566
5567 mCachedAmbientColorIsFor = mN.color;
5568 return mCachedAmbientColor = contrasted;
5569 }
5570
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005571 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005572 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005573 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005574 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005575 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005576 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005577 mN.actions = new Action[mActions.size()];
5578 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005579 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005580 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005581 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005582 }
Selim Cinek247fa012016-02-18 09:50:48 -08005583 if (mN.bigContentView != null || mN.contentView != null
5584 || mN.headsUpContentView != null) {
5585 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5586 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005587 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005588 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005589
Julia Reynolds3b848122016-02-26 10:45:32 -05005590 /**
5591 * Creates a Builder from an existing notification so further changes can be made.
5592 * @param context The context for your application / activity.
5593 * @param n The notification to create a Builder from.
5594 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005595 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005596 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005597 ApplicationInfo applicationInfo = n.extras.getParcelable(
5598 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005599 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005600 if (applicationInfo != null) {
5601 try {
5602 builderContext = context.createApplicationContext(applicationInfo,
5603 Context.CONTEXT_RESTRICTED);
5604 } catch (NameNotFoundException e) {
5605 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5606 builderContext = context; // try with our context
5607 }
5608 } else {
5609 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005610 }
5611
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005612 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005613 }
5614
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005615 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005616 * @deprecated Use {@link #build()} instead.
5617 */
5618 @Deprecated
5619 public Notification getNotification() {
5620 return build();
5621 }
5622
5623 /**
5624 * Combine all of the options that have been set and return a new {@link Notification}
5625 * object.
5626 */
5627 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005628 // first, add any extras from the calling code
5629 if (mUserExtras != null) {
5630 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005631 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005632
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005633 mN.creationTime = System.currentTimeMillis();
5634
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005635 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005636 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005637
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005638 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005639
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005640 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005641 mStyle.reduceImageSizes(mContext);
5642 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005643 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005644 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005645 }
Selim Cinekd0426622017-07-11 13:19:59 +02005646 mN.reduceImageSizes(mContext);
5647
Adrian Roos5081c0d2016-02-26 16:04:19 -08005648 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005649 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005650 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005651 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005652 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5653 mN.contentView.getSequenceNumber());
5654 }
5655 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005656 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005657 if (mN.bigContentView != null) {
5658 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5659 mN.bigContentView.getSequenceNumber());
5660 }
5661 }
5662 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005663 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005664 if (mN.headsUpContentView != null) {
5665 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5666 mN.headsUpContentView.getSequenceNumber());
5667 }
5668 }
5669 }
5670
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005671 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5672 mN.flags |= FLAG_SHOW_LIGHTS;
5673 }
5674
Adrian Roosfb921842017-10-26 14:49:56 +02005675 mN.allPendingIntents = null;
5676
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005677 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005678 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005679
5680 /**
5681 * Apply this Builder to an existing {@link Notification} object.
5682 *
5683 * @hide
5684 */
5685 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005686 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005687 return n;
5688 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005689
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005690 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005691 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005692 * change. Also removes extenders on low ram devices, as
5693 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005694 *
5695 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5696 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005697 * @hide
5698 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005699 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5700 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005701 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005702
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005703 // Only strip views for known Styles because we won't know how to
5704 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005705 if (!isLowRam
5706 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005707 && getNotificationStyleClass(templateClass) == null) {
5708 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005709 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005710
5711 // Only strip unmodified BuilderRemoteViews.
5712 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005713 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005714 n.contentView.getSequenceNumber();
5715 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005716 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005717 n.bigContentView.getSequenceNumber();
5718 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005719 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005720 n.headsUpContentView.getSequenceNumber();
5721
5722 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005723 if (!isLowRam
5724 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005725 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005726 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005727
5728 Notification clone = n.clone();
5729 if (stripContentView) {
5730 clone.contentView = null;
5731 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5732 }
5733 if (stripBigContentView) {
5734 clone.bigContentView = null;
5735 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5736 }
5737 if (stripHeadsUpContentView) {
5738 clone.headsUpContentView = null;
5739 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5740 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005741 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005742 String[] allowedServices = context.getResources().getStringArray(
5743 R.array.config_allowedManagedServicesOnLowRamDevices);
5744 if (allowedServices.length == 0) {
5745 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5746 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5747 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5748 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005749 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005750 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005751 }
5752
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005753 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005754 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005755 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005756 }
5757
5758 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005759 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005760 }
5761
5762 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005763 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005764 }
5765
5766 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005767 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005768 }
5769
5770 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005771 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005772 }
5773
Adrian Roosc1a80b02016-04-05 14:54:55 -07005774 private int getMessagingLayoutResource() {
5775 return R.layout.notification_template_material_messaging;
5776 }
5777
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005778 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005779 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005780 }
5781
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005782 private int getEmphasizedActionLayoutResource() {
5783 return R.layout.notification_material_action_emphasized;
5784 }
5785
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005786 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005787 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005788 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005789
5790 private int getBackgroundColor() {
5791 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005792 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005793 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005794 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005795 }
5796 }
5797
Selim Cinek396caca2018-04-10 17:46:46 -07005798 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005799 * Gets a neutral color that can be used for icons or similar that should not stand out.
5800 */
5801 private int getNeutralColor() {
5802 if (isColorized()) {
5803 return getSecondaryTextColor();
5804 } else {
5805 return resolveNeutralColor();
5806 }
5807 }
5808
5809 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005810 * Same as getBackgroundColor but also resolved the default color to the background.
5811 */
5812 private int resolveBackgroundColor() {
5813 int backgroundColor = getBackgroundColor();
5814 if (backgroundColor == COLOR_DEFAULT) {
5815 backgroundColor = mContext.getColor(
5816 com.android.internal.R.color.notification_material_background_color);
5817 }
5818 return backgroundColor;
5819 }
5820
Selim Cinek7b9605b2017-01-19 17:36:00 -08005821 private boolean isColorized() {
5822 return mN.isColorized();
5823 }
Selim Cinek99104832017-01-25 14:47:33 -08005824
Anthony Chenad4d1582017-04-10 16:07:58 -07005825 private boolean shouldTintActionButtons() {
5826 return mTintActionButtons;
5827 }
5828
Selim Cinek99104832017-01-25 14:47:33 -08005829 private boolean textColorsNeedInversion() {
5830 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5831 return false;
5832 }
5833 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5834 return targetSdkVersion > Build.VERSION_CODES.M
5835 && targetSdkVersion < Build.VERSION_CODES.O;
5836 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005837
5838 /**
5839 * Set a color palette to be used as the background and textColors
5840 *
5841 * @param backgroundColor the color to be used as the background
5842 * @param foregroundColor the color to be used as the foreground
5843 *
5844 * @hide
5845 */
5846 public void setColorPalette(int backgroundColor, int foregroundColor) {
5847 mBackgroundColor = backgroundColor;
5848 mForegroundColor = foregroundColor;
5849 mTextColorsAreForBackground = COLOR_INVALID;
5850 ensureColors();
5851 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005852
5853 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005854 * Forces all styled remoteViews to be built from scratch and not use any cached
5855 * RemoteViews.
5856 * This is needed for legacy apps that are baking in their remoteviews into the
5857 * notification.
5858 *
5859 * @hide
5860 */
5861 public void setRebuildStyledRemoteViews(boolean rebuild) {
5862 mRebuildStyledRemoteViews = rebuild;
5863 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005864
5865 /**
5866 * Get the text that should be displayed in the statusBar when heads upped. This is
5867 * usually just the app name, but may be different depending on the style.
5868 *
5869 * @param publicMode If true, return a text that is safe to display in public.
5870 *
5871 * @hide
5872 */
5873 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5874 if (mStyle != null && !publicMode) {
5875 CharSequence text = mStyle.getHeadsUpStatusBarText();
5876 if (!TextUtils.isEmpty(text)) {
5877 return text;
5878 }
5879 }
5880 return loadHeaderAppName();
5881 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005882 }
5883
5884 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005885 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5886 * remote views.
5887 *
5888 * @hide
5889 */
5890 void reduceImageSizes(Context context) {
5891 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5892 return;
5893 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005894 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005895 if (mLargeIcon != null || largeIcon != null) {
5896 Resources resources = context.getResources();
5897 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005898 int maxWidth = resources.getDimensionPixelSize(isLowRam
5899 ? R.dimen.notification_right_icon_size_low_ram
5900 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005901 int maxHeight = maxWidth;
5902 if (MediaStyle.class.equals(style)
5903 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005904 maxHeight = resources.getDimensionPixelSize(isLowRam
5905 ? R.dimen.notification_media_image_max_height_low_ram
5906 : R.dimen.notification_media_image_max_height);
5907 maxWidth = resources.getDimensionPixelSize(isLowRam
5908 ? R.dimen.notification_media_image_max_width_low_ram
5909 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005910 }
5911 if (mLargeIcon != null) {
5912 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5913 }
5914 if (largeIcon != null) {
5915 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5916 }
5917 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005918 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5919 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5920 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005921 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5922 }
5923
Selim Cineka8cb1262017-08-15 16:53:44 -07005924 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5925 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005926 if (remoteView != null) {
5927 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005928 int maxWidth = resources.getDimensionPixelSize(isLowRam
5929 ? R.dimen.notification_custom_view_max_image_width_low_ram
5930 : R.dimen.notification_custom_view_max_image_width);
5931 int maxHeight = resources.getDimensionPixelSize(isLowRam
5932 ? R.dimen.notification_custom_view_max_image_height_low_ram
5933 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005934 remoteView.reduceImageSizes(maxWidth, maxHeight);
5935 }
5936 }
5937
5938 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005939 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005940 */
Selim Cinek22714f12017-04-13 16:23:53 -07005941 private boolean isForegroundService() {
5942 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005943 }
5944
5945 /**
Selim Cinek99104832017-01-25 14:47:33 -08005946 * @return whether this notification has a media session attached
5947 * @hide
5948 */
5949 public boolean hasMediaSession() {
5950 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5951 }
5952
5953 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005954 * @return the style class of this notification
5955 * @hide
5956 */
5957 public Class<? extends Notification.Style> getNotificationStyle() {
5958 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5959
5960 if (!TextUtils.isEmpty(templateClass)) {
5961 return Notification.getNotificationStyleClass(templateClass);
5962 }
5963 return null;
5964 }
5965
5966 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005967 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005968 *
5969 * @hide
5970 */
5971 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005972 if (isColorizedMedia()) {
5973 return true;
5974 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005975 return extras.getBoolean(EXTRA_COLORIZED)
5976 && (hasColorizedPermission() || isForegroundService());
5977 }
5978
5979 /**
5980 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5981 * permission. The permission is checked when a notification is enqueued.
5982 */
5983 private boolean hasColorizedPermission() {
5984 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005985 }
5986
5987 /**
5988 * @return true if this notification is colorized and it is a media notification
5989 *
5990 * @hide
5991 */
5992 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005993 Class<? extends Style> style = getNotificationStyle();
5994 if (MediaStyle.class.equals(style)) {
5995 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5996 if ((colorized == null || colorized) && hasMediaSession()) {
5997 return true;
5998 }
5999 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6000 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6001 return true;
6002 }
6003 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006004 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006005 }
6006
Selim Cinek0847acd2017-04-24 19:48:29 -07006007
6008 /**
6009 * @return true if this is a media notification
6010 *
6011 * @hide
6012 */
6013 public boolean isMediaNotification() {
6014 Class<? extends Style> style = getNotificationStyle();
6015 if (MediaStyle.class.equals(style)) {
6016 return true;
6017 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6018 return true;
6019 }
6020 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006021 }
6022
Selim Cinek279fa862016-06-14 10:57:25 -07006023 private boolean hasLargeIcon() {
6024 return mLargeIcon != null || largeIcon != null;
6025 }
6026
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006027 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006028 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006029 * @hide
6030 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006031 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006032 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6033 }
6034
6035 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006036 * @return true if the notification will show a chronometer; false otherwise
6037 * @hide
6038 */
6039 public boolean showsChronometer() {
6040 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6041 }
6042
6043 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006044 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006045 */
6046 @SystemApi
6047 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6048 Class<? extends Style>[] classes = new Class[] {
6049 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6050 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6051 MessagingStyle.class };
6052 for (Class<? extends Style> innerClass : classes) {
6053 if (templateClass.equals(innerClass.getName())) {
6054 return innerClass;
6055 }
6056 }
6057 return null;
6058 }
6059
6060 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006061 * An object that can apply a rich notification style to a {@link Notification.Builder}
6062 * object.
6063 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006064 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006065
6066 /**
6067 * The number of items allowed simulatanously in the remote input history.
6068 * @hide
6069 */
6070 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006071 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006072
6073 /**
6074 * @hide
6075 */
6076 protected CharSequence mSummaryText = null;
6077
6078 /**
6079 * @hide
6080 */
6081 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006082
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006083 protected Builder mBuilder;
6084
Chris Wrend6297db2012-05-03 16:20:13 -04006085 /**
6086 * Overrides ContentTitle in the big form of the template.
6087 * This defaults to the value passed to setContentTitle().
6088 */
6089 protected void internalSetBigContentTitle(CharSequence title) {
6090 mBigContentTitle = title;
6091 }
6092
6093 /**
6094 * Set the first line of text after the detail section in the big form of the template.
6095 */
6096 protected void internalSetSummaryText(CharSequence cs) {
6097 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006098 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006099 }
6100
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006101 public void setBuilder(Builder builder) {
6102 if (mBuilder != builder) {
6103 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006104 if (mBuilder != null) {
6105 mBuilder.setStyle(this);
6106 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006107 }
6108 }
6109
Chris Wrend6297db2012-05-03 16:20:13 -04006110 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006111 if (mBuilder == null) {
6112 throw new IllegalArgumentException("Style requires a valid Builder object");
6113 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006114 }
Chris Wrend6297db2012-05-03 16:20:13 -04006115
6116 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006117 return getStandardView(layoutId, null);
6118 }
6119
6120 /**
6121 * Get the standard view for this style.
6122 *
6123 * @param layoutId The layout id to use
6124 * @param result The result where template bind information is saved.
6125 * @return A remoteView for this style.
6126 * @hide
6127 */
6128 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006129 checkBuilder();
6130
Christoph Studer4600f9b2014-07-22 22:44:43 +02006131 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006132 CharSequence oldBuilderContentTitle =
6133 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006134 if (mBigContentTitle != null) {
6135 mBuilder.setContentTitle(mBigContentTitle);
6136 }
6137
Selim Cinek384804b2018-04-18 14:31:07 +08006138 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006139
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006140 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006141
Chris Wrend6297db2012-05-03 16:20:13 -04006142 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6143 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006144 } else {
6145 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006146 }
6147
Chris Wrend6297db2012-05-03 16:20:13 -04006148 return contentView;
6149 }
6150
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006151 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006152 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006153 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006154 *
6155 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006156 * @hide
6157 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006158 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006159 return null;
6160 }
6161
6162 /**
6163 * Construct a Style-specific RemoteViews for the final big notification layout.
6164 * @hide
6165 */
6166 public RemoteViews makeBigContentView() {
6167 return null;
6168 }
6169
6170 /**
6171 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006172 *
6173 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006174 * @hide
6175 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006176 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006177 return null;
6178 }
6179
6180 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006181 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006182 * @hide
6183 */
6184 public void addExtras(Bundle extras) {
6185 if (mSummaryTextSet) {
6186 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6187 }
6188 if (mBigContentTitle != null) {
6189 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6190 }
Chris Wren91ad5632013-06-05 15:05:57 -04006191 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006192 }
6193
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006194 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006195 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006196 * @hide
6197 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006198 protected void restoreFromExtras(Bundle extras) {
6199 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6200 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6201 mSummaryTextSet = true;
6202 }
6203 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6204 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6205 }
6206 }
6207
6208
6209 /**
6210 * @hide
6211 */
6212 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006213 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006214 return wip;
6215 }
6216
Daniel Sandler0ec46202015-06-24 01:27:05 -04006217 /**
6218 * @hide
6219 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006220 public void purgeResources() {}
6221
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006222 /**
6223 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6224 * attached to.
6225 *
6226 * @return the fully constructed Notification.
6227 */
6228 public Notification build() {
6229 checkBuilder();
6230 return mBuilder.build();
6231 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006232
6233 /**
6234 * @hide
6235 * @return true if the style positions the progress bar on the second line; false if the
6236 * style hides the progress bar
6237 */
6238 protected boolean hasProgress() {
6239 return true;
6240 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006241
6242 /**
6243 * @hide
6244 * @return Whether we should put the summary be put into the notification header
6245 */
6246 public boolean hasSummaryInHeader() {
6247 return true;
6248 }
Selim Cinek593610c2016-02-16 18:42:57 -08006249
6250 /**
6251 * @hide
6252 * @return Whether custom content views are displayed inline in the style
6253 */
6254 public boolean displayCustomViewInline() {
6255 return false;
6256 }
Selim Cinekd0426622017-07-11 13:19:59 +02006257
6258 /**
6259 * Reduces the image sizes contained in this style.
6260 *
6261 * @hide
6262 */
6263 public void reduceImageSizes(Context context) {
6264 }
Selim Cinek90343862018-02-01 11:07:11 -08006265
6266 /**
6267 * Validate that this style was properly composed. This is called at build time.
6268 * @hide
6269 */
6270 public void validate(Context context) {
6271 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006272
6273 /**
6274 * @hide
6275 */
6276 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006277
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006278 /**
koprivaa1a78482018-10-09 10:09:23 -07006279 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006280 * If {@code null} is returned, the default implementation will be used.
6281 *
6282 * @hide
6283 */
6284 public CharSequence getHeadsUpStatusBarText() {
6285 return null;
6286 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006287 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006288
6289 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006290 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006291 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006292 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006293 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006294 * Notification notif = new Notification.Builder(mContext)
6295 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6296 * .setContentText(subject)
6297 * .setSmallIcon(R.drawable.new_post)
6298 * .setLargeIcon(aBitmap)
6299 * .setStyle(new Notification.BigPictureStyle()
6300 * .bigPicture(aBigBitmap))
6301 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006302 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006303 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006304 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006305 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006306 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006307 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006308 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006309 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006310
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006311 public BigPictureStyle() {
6312 }
6313
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006314 /**
6315 * @deprecated use {@code BigPictureStyle()}.
6316 */
6317 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006318 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006319 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006320 }
6321
Chris Wrend6297db2012-05-03 16:20:13 -04006322 /**
6323 * Overrides ContentTitle in the big form of the template.
6324 * This defaults to the value passed to setContentTitle().
6325 */
6326 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006327 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006328 return this;
6329 }
6330
6331 /**
6332 * Set the first line of text after the detail section in the big form of the template.
6333 */
6334 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006335 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006336 return this;
6337 }
6338
Chris Wren0bd664d2012-08-01 13:56:56 -04006339 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006340 * @hide
6341 */
6342 public Bitmap getBigPicture() {
6343 return mPicture;
6344 }
6345
6346 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006347 * Provide the bitmap to be used as the payload for the BigPicture notification.
6348 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006349 public BigPictureStyle bigPicture(Bitmap b) {
6350 mPicture = b;
6351 return this;
6352 }
6353
Chris Wren3745a3d2012-05-22 15:11:52 -04006354 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006355 * Override the large icon when the big notification is shown.
6356 */
6357 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006358 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6359 }
6360
6361 /**
6362 * Override the large icon when the big notification is shown.
6363 */
6364 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006365 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006366 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006367 return this;
6368 }
6369
Riley Andrews0394a0c2015-11-03 23:36:52 -08006370 /** @hide */
6371 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6372
Daniel Sandler0ec46202015-06-24 01:27:05 -04006373 /**
6374 * @hide
6375 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006376 @Override
6377 public void purgeResources() {
6378 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006379 if (mPicture != null &&
6380 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006381 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006382 mPicture = mPicture.createAshmemBitmap();
6383 }
6384 if (mBigLargeIcon != null) {
6385 mBigLargeIcon.convertToAshmem();
6386 }
6387 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006388
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006389 /**
6390 * @hide
6391 */
Selim Cinekd0426622017-07-11 13:19:59 +02006392 @Override
6393 public void reduceImageSizes(Context context) {
6394 super.reduceImageSizes(context);
6395 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006396 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006397 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006398 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6399 ? R.dimen.notification_big_picture_max_height_low_ram
6400 : R.dimen.notification_big_picture_max_height);
6401 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6402 ? R.dimen.notification_big_picture_max_width_low_ram
6403 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006404 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6405 }
6406 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006407 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6408 ? R.dimen.notification_right_icon_size_low_ram
6409 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006410 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6411 }
6412 }
6413
6414 /**
6415 * @hide
6416 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006417 public RemoteViews makeBigContentView() {
6418 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006419 // This covers the following cases:
6420 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006421 // mN.mLargeIcon
6422 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006423 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006424 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006425 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006426 oldLargeIcon = mBuilder.mN.mLargeIcon;
6427 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006428 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6429 // replacement if the other one is null. Because we're restoring these legacy icons
6430 // for old listeners, this is in general non-null.
6431 largeIconLegacy = mBuilder.mN.largeIcon;
6432 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006433 }
6434
Selim Cinek384804b2018-04-18 14:31:07 +08006435 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6436 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006437 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006438 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6439 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006440 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006441 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006442 }
Selim Cinek279fa862016-06-14 10:57:25 -07006443 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006444
Christoph Studer5c510ee2014-12-15 16:32:27 +01006445 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006446 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006447 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006448 }
6449
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006450 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006451 return contentView;
6452 }
6453
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006454 /**
6455 * @hide
6456 */
6457 public void addExtras(Bundle extras) {
6458 super.addExtras(extras);
6459
6460 if (mBigLargeIconSet) {
6461 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6462 }
6463 extras.putParcelable(EXTRA_PICTURE, mPicture);
6464 }
6465
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006466 /**
6467 * @hide
6468 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006469 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006470 protected void restoreFromExtras(Bundle extras) {
6471 super.restoreFromExtras(extras);
6472
6473 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006474 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006475 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006476 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006477 mPicture = extras.getParcelable(EXTRA_PICTURE);
6478 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006479
6480 /**
6481 * @hide
6482 */
6483 @Override
6484 public boolean hasSummaryInHeader() {
6485 return false;
6486 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006487
6488 /**
6489 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006490 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6491 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006492 */
6493 @Override
6494 public boolean areNotificationsVisiblyDifferent(Style other) {
6495 if (other == null || getClass() != other.getClass()) {
6496 return true;
6497 }
6498 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006499 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6500 }
6501
6502 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6503 if (a == b) {
6504 return false;
6505 }
6506 if (a == null || b == null) {
6507 return true;
6508 }
6509 return a.getWidth() != b.getWidth()
6510 || a.getHeight() != b.getHeight()
6511 || a.getConfig() != b.getConfig()
6512 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006513 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006514 }
6515
6516 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006517 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006518 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006519 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006520 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006521 * Notification notif = new Notification.Builder(mContext)
6522 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6523 * .setContentText(subject)
6524 * .setSmallIcon(R.drawable.new_mail)
6525 * .setLargeIcon(aBitmap)
6526 * .setStyle(new Notification.BigTextStyle()
6527 * .bigText(aVeryLongString))
6528 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006529 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006530 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006531 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006532 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006533 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006534
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006535 private CharSequence mBigText;
6536
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006537 public BigTextStyle() {
6538 }
6539
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006540 /**
6541 * @deprecated use {@code BigTextStyle()}.
6542 */
6543 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006544 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006545 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006546 }
6547
Chris Wrend6297db2012-05-03 16:20:13 -04006548 /**
6549 * Overrides ContentTitle in the big form of the template.
6550 * This defaults to the value passed to setContentTitle().
6551 */
6552 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006553 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006554 return this;
6555 }
6556
6557 /**
6558 * Set the first line of text after the detail section in the big form of the template.
6559 */
6560 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006561 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006562 return this;
6563 }
6564
Chris Wren0bd664d2012-08-01 13:56:56 -04006565 /**
6566 * Provide the longer text to be displayed in the big form of the
6567 * template in place of the content text.
6568 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006569 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006570 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006571 return this;
6572 }
6573
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006574 /**
6575 * @hide
6576 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006577 public CharSequence getBigText() {
6578 return mBigText;
6579 }
6580
6581 /**
6582 * @hide
6583 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006584 public void addExtras(Bundle extras) {
6585 super.addExtras(extras);
6586
Christoph Studer4600f9b2014-07-22 22:44:43 +02006587 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6588 }
6589
6590 /**
6591 * @hide
6592 */
6593 @Override
6594 protected void restoreFromExtras(Bundle extras) {
6595 super.restoreFromExtras(extras);
6596
6597 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006598 }
6599
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006600 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006601 * @param increasedHeight true if this layout be created with an increased height.
6602 *
6603 * @hide
6604 */
6605 @Override
6606 public RemoteViews makeContentView(boolean increasedHeight) {
6607 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006608 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006609 mBuilder.mActions = new ArrayList<>();
6610 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006611 mBuilder.mActions = mBuilder.mOriginalActions;
6612 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006613 return remoteViews;
6614 }
6615 return super.makeContentView(increasedHeight);
6616 }
6617
6618 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006619 * @hide
6620 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006621 @Override
6622 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6623 if (increasedHeight && mBuilder.mActions.size() > 0) {
6624 return makeBigContentView();
6625 }
6626 return super.makeHeadsUpContentView(increasedHeight);
6627 }
6628
6629 /**
6630 * @hide
6631 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006632 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006633
6634 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006635 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006636 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006637
Selim Cinek384804b2018-04-18 14:31:07 +08006638 TemplateBindResult result = new TemplateBindResult();
6639 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006640 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006641
Selim Cinek75998782016-04-26 10:39:17 -07006642 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006643
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006644 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006645 if (TextUtils.isEmpty(bigTextText)) {
6646 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6647 // experience
6648 bigTextText = mBuilder.processLegacyText(text);
6649 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006650 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006651
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006652 return contentView;
6653 }
6654
Julia Reynolds7217dc92018-03-07 12:12:09 -05006655 /**
6656 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006657 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006658 */
6659 @Override
6660 public boolean areNotificationsVisiblyDifferent(Style other) {
6661 if (other == null || getClass() != other.getClass()) {
6662 return true;
6663 }
6664 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006665 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006666 }
6667
Adrian Roosb1f427c2016-05-26 12:27:15 -07006668 static void applyBigTextContentView(Builder builder,
6669 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006670 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006671 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006672 contentView.setViewVisibility(R.id.big_text,
6673 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006674 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006675 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006676 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006677
6678 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006679 * Helper class for generating large-format notifications that include multiple back-and-forth
6680 * messages of varying types between any number of people.
6681 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006682 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006683 * If the platform does not provide large-format notifications, this method has no effect. The
6684 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006685 *
6686 * <p>
6687 * If the app is targeting Android P and above, it is required to use the {@link Person}
6688 * class in order to get an optimal rendering of the notification and its avatars. For
6689 * conversations involving multiple people, the app should also make sure that it marks the
6690 * conversation as a group with {@link #setGroupConversation(boolean)}.
6691 *
6692 * <p>
6693 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6694 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006695 * <pre class="prettyprint">
6696 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006697 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6698 * MessagingStyle style = new MessagingStyle(user)
6699 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6700 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6701 * .setGroupConversation(hasMultiplePeople());
6702 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006703 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006704 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006705 * .setContentText(subject)
6706 * .setSmallIcon(R.drawable.new_message)
6707 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006708 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006709 * .build();
6710 * </pre>
6711 */
6712 public static class MessagingStyle extends Style {
6713
6714 /**
6715 * The maximum number of messages that will be retained in the Notification itself (the
6716 * number displayed is up to the platform).
6717 */
6718 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6719
Selim Cinekcb8b9852017-12-15 18:01:52 -08006720 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006721 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006722 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006723 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006724 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006725
6726 MessagingStyle() {
6727 }
6728
6729 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006730 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6731 * user before the posting app reposts the notification with those messages after they've
6732 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006733 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006734 *
6735 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006736 */
Alex Hillsfd590442016-10-07 09:52:44 -04006737 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006738 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006739 }
6740
6741 /**
6742 * @param user Required - The person displayed for any messages that are sent by the
6743 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6744 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006745 * by this user. The user also needs to have a valid name associated with it, which will
6746 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006747 */
6748 public MessagingStyle(@NonNull Person user) {
6749 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006750 }
6751
6752 /**
6753 * Validate that this style was properly composed. This is called at build time.
6754 * @hide
6755 */
6756 @Override
6757 public void validate(Context context) {
6758 super.validate(context);
6759 if (context.getApplicationInfo().targetSdkVersion
6760 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6761 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006762 }
6763 }
6764
6765 /**
koprivaa1a78482018-10-09 10:09:23 -07006766 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006767 * If {@code null} is returned, the default implementation will be used.
6768 *
6769 * @hide
6770 */
6771 @Override
6772 public CharSequence getHeadsUpStatusBarText() {
6773 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6774 ? super.mBigContentTitle
6775 : mConversationTitle;
6776 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6777 return conversationTitle;
6778 }
6779 return null;
6780 }
6781
6782 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006783 * @return the user to be displayed for any replies sent by the user
6784 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006785 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006786 public Person getUser() {
6787 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006788 }
6789
6790 /**
6791 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006792 *
6793 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006794 */
6795 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006796 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006797 }
6798
6799 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006800 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6801 *
Kodlee Yin14656422017-12-22 17:00:46 -08006802 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6803 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6804 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6805 * {@code true} and passing {@code null} will make it return {@code false}. In
6806 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6807 * to set group conversation status.
6808 *
6809 * @param conversationTitle Title displayed for this conversation
6810 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006811 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006812 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006813 mConversationTitle = conversationTitle;
6814 return this;
6815 }
6816
6817 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006818 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006819 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006820 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006821 public CharSequence getConversationTitle() {
6822 return mConversationTitle;
6823 }
6824
6825 /**
6826 * Adds a message for display by this notification. Convenience call for a simple
6827 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6828 * @param text A {@link CharSequence} to be displayed as the message content
6829 * @param timestamp Time at which the message arrived
6830 * @param sender A {@link CharSequence} to be used for displaying the name of the
6831 * sender. Should be <code>null</code> for messages by the current user, in which case
6832 * the platform will insert {@link #getUserDisplayName()}.
6833 * Should be unique amongst all individuals in the conversation, and should be
6834 * consistent during re-posts of the notification.
6835 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006836 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006837 *
6838 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006839 *
6840 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006841 */
6842 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006843 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006844 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006845 }
6846
6847 /**
6848 * Adds a message for display by this notification. Convenience call for a simple
6849 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6850 * @param text A {@link CharSequence} to be displayed as the message content
6851 * @param timestamp Time at which the message arrived
6852 * @param sender The {@link Person} who sent the message.
6853 * Should be <code>null</code> for messages by the current user, in which case
6854 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6855 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006856 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006857 *
6858 * @return this object for method chaining
6859 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006860 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6861 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006862 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006863 }
6864
6865 /**
6866 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006867 *
6868 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6869 * the newest last.
6870 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006871 * @param message The {@link Message} to be displayed
6872 * @return this object for method chaining
6873 */
6874 public MessagingStyle addMessage(Message message) {
6875 mMessages.add(message);
6876 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6877 mMessages.remove(0);
6878 }
6879 return this;
6880 }
6881
6882 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006883 * Adds a {@link Message} for historic context in this notification.
6884 *
6885 * <p>Messages should be added as historic if they are not the main subject of the
6886 * notification but may give context to a conversation. The system may choose to present
6887 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6888 *
6889 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6890 * the newest last.
6891 *
6892 * @param message The historic {@link Message} to be added
6893 * @return this object for method chaining
6894 */
6895 public MessagingStyle addHistoricMessage(Message message) {
6896 mHistoricMessages.add(message);
6897 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6898 mHistoricMessages.remove(0);
6899 }
6900 return this;
6901 }
6902
6903 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006904 * Gets the list of {@code Message} objects that represent the notification
6905 */
6906 public List<Message> getMessages() {
6907 return mMessages;
6908 }
6909
6910 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006911 * Gets the list of historic {@code Message}s in the notification.
6912 */
6913 public List<Message> getHistoricMessages() {
6914 return mHistoricMessages;
6915 }
6916
6917 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006918 * Sets whether this conversation notification represents a group. If the app is targeting
6919 * Android P, this is required if the app wants to display the largeIcon set with
6920 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006921 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006922 * @param isGroupConversation {@code true} if the conversation represents a group,
6923 * {@code false} otherwise.
6924 * @return this object for method chaining
6925 */
6926 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6927 mIsGroupConversation = isGroupConversation;
6928 return this;
6929 }
6930
6931 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006932 * Returns {@code true} if this notification represents a group conversation, otherwise
6933 * {@code false}.
6934 *
6935 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6936 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6937 * not the conversation title is set; returning {@code true} if the conversation title is
6938 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6939 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6940 * named, non-group conversations.
6941 *
6942 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006943 */
6944 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006945 // When target SDK version is < P, a non-null conversation title dictates if this is
6946 // as group conversation.
6947 if (mBuilder != null
6948 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6949 < Build.VERSION_CODES.P) {
6950 return mConversationTitle != null;
6951 }
6952
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006953 return mIsGroupConversation;
6954 }
6955
6956 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006957 * @hide
6958 */
6959 @Override
6960 public void addExtras(Bundle extras) {
6961 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006962 if (mUser != null) {
6963 // For legacy usages
6964 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6965 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006966 }
6967 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006968 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006969 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006970 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6971 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006972 }
Adrian Roos437cd562017-01-18 15:47:03 -08006973 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6974 Message.getBundleArrayForMessages(mHistoricMessages));
6975 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006976
6977 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006978 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006979 }
6980
6981 private void fixTitleAndTextExtras(Bundle extras) {
6982 Message m = findLatestIncomingMessage();
6983 CharSequence text = (m == null) ? null : m.mText;
6984 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006985 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6986 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006987 CharSequence title;
6988 if (!TextUtils.isEmpty(mConversationTitle)) {
6989 if (!TextUtils.isEmpty(sender)) {
6990 BidiFormatter bidi = BidiFormatter.getInstance();
6991 title = mBuilder.mContext.getString(
6992 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006993 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006994 } else {
6995 title = mConversationTitle;
6996 }
6997 } else {
6998 title = sender;
6999 }
7000
7001 if (title != null) {
7002 extras.putCharSequence(EXTRA_TITLE, title);
7003 }
7004 if (text != null) {
7005 extras.putCharSequence(EXTRA_TEXT, text);
7006 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007007 }
7008
7009 /**
7010 * @hide
7011 */
7012 @Override
7013 protected void restoreFromExtras(Bundle extras) {
7014 super.restoreFromExtras(extras);
7015
Selim Cinekcb8b9852017-12-15 18:01:52 -08007016 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7017 if (mUser == null) {
7018 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007019 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007020 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007021 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007022 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007023 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007024 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007025 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007026 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007027 }
7028
7029 /**
7030 * @hide
7031 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007032 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007033 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007034 mBuilder.mOriginalActions = mBuilder.mActions;
7035 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007036 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007037 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007038 mBuilder.mActions = mBuilder.mOriginalActions;
7039 mBuilder.mOriginalActions = null;
7040 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007041 }
7042
Julia Reynolds7217dc92018-03-07 12:12:09 -05007043 /**
7044 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007045 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007046 */
7047 @Override
7048 public boolean areNotificationsVisiblyDifferent(Style other) {
7049 if (other == null || getClass() != other.getClass()) {
7050 return true;
7051 }
7052 MessagingStyle newS = (MessagingStyle) other;
7053 List<MessagingStyle.Message> oldMs = getMessages();
7054 List<MessagingStyle.Message> newMs = newS.getMessages();
7055
Dan Sandler7d67bd42018-05-15 14:06:38 -04007056 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007057 newMs = new ArrayList<>();
7058 }
7059
7060 int n = oldMs.size();
7061 if (n != newMs.size()) {
7062 return true;
7063 }
7064 for (int i = 0; i < n; i++) {
7065 MessagingStyle.Message oldM = oldMs.get(i);
7066 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007067 if (!Objects.equals(
7068 String.valueOf(oldM.getText()),
7069 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007070 return true;
7071 }
7072 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7073 return true;
7074 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007075 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7076 ? oldM.getSender()
7077 : oldM.getSenderPerson().getName());
7078 String newSender = String.valueOf(newM.getSenderPerson() == null
7079 ? newM.getSender()
7080 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007081 if (!Objects.equals(oldSender, newSender)) {
7082 return true;
7083 }
7084
7085 String oldKey = oldM.getSenderPerson() == null
7086 ? null : oldM.getSenderPerson().getKey();
7087 String newKey = newM.getSenderPerson() == null
7088 ? null : newM.getSenderPerson().getKey();
7089 if (!Objects.equals(oldKey, newKey)) {
7090 return true;
7091 }
7092 // Other fields (like timestamp) intentionally excluded
7093 }
7094 return false;
7095 }
7096
Adrian Roosc1a80b02016-04-05 14:54:55 -07007097 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007098 return findLatestIncomingMessage(mMessages);
7099 }
7100
7101 /**
7102 * @hide
7103 */
7104 @Nullable
7105 public static Message findLatestIncomingMessage(
7106 List<Message> messages) {
7107 for (int i = messages.size() - 1; i >= 0; i--) {
7108 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007109 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007110 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007111 return m;
7112 }
7113 }
Selim Cinek88188f22017-09-19 16:46:56 -07007114 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007115 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007116 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007117 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007118 return null;
7119 }
7120
7121 /**
7122 * @hide
7123 */
7124 @Override
7125 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007126 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007127 }
7128
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007129 /**
7130 * Create a messaging layout.
7131 *
7132 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7133 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007134 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007135 * @return the created remoteView.
7136 */
Selim Cinekafeed292017-12-12 17:32:44 -08007137 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007138 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007139 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007140 ? super.mBigContentTitle
7141 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007142 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7143 >= Build.VERSION_CODES.P;
7144 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007145 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007146 Icon avatarReplacement = null;
7147 if (!atLeastP) {
7148 isOneToOne = TextUtils.isEmpty(conversationTitle);
7149 avatarReplacement = mBuilder.mN.mLargeIcon;
7150 if (hasOnlyWhiteSpaceSenders()) {
7151 isOneToOne = true;
7152 nameReplacement = conversationTitle;
7153 conversationTitle = null;
7154 }
7155 } else {
7156 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007157 }
Selim Cinek384804b2018-04-18 14:31:07 +08007158 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007159 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007160 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007161 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007162 .hideLargeIcon(hideRightIcons || isOneToOne)
7163 .hideReplyIcon(hideRightIcons)
7164 .headerTextSecondary(conversationTitle),
7165 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007166 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007167 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007168 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7169 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007170 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007171 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7172 : mBuilder.resolveContrastColor());
7173 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7174 mBuilder.getPrimaryTextColor());
7175 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7176 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007177 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7178 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007179 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7180 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007181 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7182 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007183 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7184 isOneToOne);
7185 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7186 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007187 return contentView;
7188 }
7189
Selim Cinekf7409db2017-10-24 16:17:14 -07007190 private boolean hasOnlyWhiteSpaceSenders() {
7191 for (int i = 0; i < mMessages.size(); i++) {
7192 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007193 Person sender = m.getSenderPerson();
7194 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007195 return false;
7196 }
7197 }
7198 return true;
7199 }
7200
7201 private boolean isWhiteSpace(CharSequence sender) {
7202 if (TextUtils.isEmpty(sender)) {
7203 return true;
7204 }
7205 if (sender.toString().matches("^\\s*$")) {
7206 return true;
7207 }
7208 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7209 // For the presentation that we had.
7210 for (int i = 0; i < sender.length(); i++) {
7211 char c = sender.charAt(i);
7212 if (c != '\u200B') {
7213 return false;
7214 }
7215 }
7216 return true;
7217 }
7218
Selim Cinek88188f22017-09-19 16:46:56 -07007219 private CharSequence createConversationTitleFromMessages() {
7220 ArraySet<CharSequence> names = new ArraySet<>();
7221 for (int i = 0; i < mMessages.size(); i++) {
7222 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007223 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007224 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007225 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007226 }
7227 }
7228 SpannableStringBuilder title = new SpannableStringBuilder();
7229 int size = names.size();
7230 for (int i = 0; i < size; i++) {
7231 CharSequence name = names.valueAt(i);
7232 if (!TextUtils.isEmpty(title)) {
7233 title.append(", ");
7234 }
7235 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7236 }
7237 return title;
7238 }
7239
Adrian Roosdedd1df2016-04-26 16:38:47 -07007240 /**
7241 * @hide
7242 */
7243 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007244 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007245 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007246 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007247 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7248 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007249 }
7250
Adrian Roosc1a80b02016-04-05 14:54:55 -07007251 private static TextAppearanceSpan makeFontColorSpan(int color) {
7252 return new TextAppearanceSpan(null, 0, 0,
7253 ColorStateList.valueOf(color), null);
7254 }
7255
Alex Hillsd9b04d92016-04-11 16:38:16 -04007256 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007257 /** @hide */
7258 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007259 static final String KEY_TIMESTAMP = "time";
7260 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007261 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007262 static final String KEY_DATA_MIME_TYPE = "type";
7263 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007264 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007265 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007266
7267 private final CharSequence mText;
7268 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007269 @Nullable
7270 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007271 /** True if this message was generated from the extra
7272 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7273 */
7274 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007275
Shane Brennan5a871862017-03-11 13:14:17 -08007276 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007277 private String mDataMimeType;
7278 private Uri mDataUri;
7279
7280 /**
7281 * Constructor
7282 * @param text A {@link CharSequence} to be displayed as the message content
7283 * @param timestamp Time at which the message arrived
7284 * @param sender A {@link CharSequence} to be used for displaying the name of the
7285 * sender. Should be <code>null</code> for messages by the current user, in which case
7286 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7287 * Should be unique amongst all individuals in the conversation, and should be
7288 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007289 *
7290 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007291 */
7292 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007293 this(text, timestamp, sender == null ? null
7294 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007295 }
7296
7297 /**
7298 * Constructor
7299 * @param text A {@link CharSequence} to be displayed as the message content
7300 * @param timestamp Time at which the message arrived
7301 * @param sender The {@link Person} who sent the message.
7302 * Should be <code>null</code> for messages by the current user, in which case
7303 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7304 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007305 * The person provided should contain an Icon, set with
7306 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7307 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7308 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7309 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007310 * </p>
7311 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007312 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007313 this(text, timestamp, sender, false /* remoteHistory */);
7314 }
7315
7316 /**
7317 * Constructor
7318 * @param text A {@link CharSequence} to be displayed as the message content
7319 * @param timestamp Time at which the message arrived
7320 * @param sender The {@link Person} who sent the message.
7321 * Should be <code>null</code> for messages by the current user, in which case
7322 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7323 * @param remoteInputHistory True if the messages was generated from the extra
7324 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7325 * <p>
7326 * The person provided should contain an Icon, set with
7327 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7328 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7329 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7330 * to differentiate between the different users.
7331 * </p>
7332 * @hide
7333 */
7334 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7335 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007336 mText = text;
7337 mTimestamp = timestamp;
7338 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007339 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007340 }
7341
7342 /**
7343 * Sets a binary blob of data and an associated MIME type for a message. In the case
7344 * where the platform doesn't support the MIME type, the original text provided in the
7345 * constructor will be used.
7346 * @param dataMimeType The MIME type of the content. See
7347 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7348 * types on Android and Android Wear.
7349 * @param dataUri The uri containing the content whose type is given by the MIME type.
7350 * <p class="note">
7351 * <ol>
7352 * <li>Notification Listeners including the System UI need permission to access the
7353 * data the Uri points to. The recommended ways to do this are:</li>
7354 * <li>Store the data in your own ContentProvider, making sure that other apps have
7355 * the correct permission to access your provider. The preferred mechanism for
7356 * providing access is to use per-URI permissions which are temporary and only
7357 * grant access to the receiving application. An easy way to create a
7358 * ContentProvider like this is to use the FileProvider helper class.</li>
7359 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7360 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7361 * also store non-media types (see MediaStore.Files for more info). Files can be
7362 * inserted into the MediaStore using scanFile() after which a content:// style
7363 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7364 * Note that once added to the system MediaStore the content is accessible to any
7365 * app on the device.</li>
7366 * </ol>
7367 * @return this object for method chaining
7368 */
7369 public Message setData(String dataMimeType, Uri dataUri) {
7370 mDataMimeType = dataMimeType;
7371 mDataUri = dataUri;
7372 return this;
7373 }
7374
Alex Hillsfc737de2016-03-23 17:33:02 -04007375 /**
7376 * Get the text to be used for this message, or the fallback text if a type and content
7377 * Uri have been set
7378 */
7379 public CharSequence getText() {
7380 return mText;
7381 }
7382
7383 /**
7384 * Get the time at which this message arrived
7385 */
7386 public long getTimestamp() {
7387 return mTimestamp;
7388 }
7389
7390 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007391 * Get the extras Bundle for this message.
7392 */
7393 public Bundle getExtras() {
7394 return mExtras;
7395 }
7396
7397 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007398 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007399 *
7400 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007401 */
7402 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007403 return mSender == null ? null : mSender.getName();
7404 }
7405
7406 /**
7407 * Get the sender associated with this message.
7408 */
7409 @Nullable
7410 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007411 return mSender;
7412 }
7413
7414 /**
7415 * Get the MIME type of the data pointed to by the Uri
7416 */
7417 public String getDataMimeType() {
7418 return mDataMimeType;
7419 }
7420
7421 /**
koprivaa1a78482018-10-09 10:09:23 -07007422 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007423 * {@see #getText()} is used.
7424 */
7425 public Uri getDataUri() {
7426 return mDataUri;
7427 }
7428
Kenny Guya0f6de82018-04-06 16:20:16 +01007429 /**
7430 * @return True if the message was generated from
7431 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7432 * @hide
7433 */
7434 public boolean isRemoteInputHistory() {
7435 return mRemoteInputHistory;
7436 }
7437
Alex Hillsd9b04d92016-04-11 16:38:16 -04007438 private Bundle toBundle() {
7439 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007440 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007441 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007442 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007443 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007444 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007445 // Legacy listeners need this
7446 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7447 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007448 }
7449 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007450 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007451 }
7452 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007453 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007454 }
Shane Brennan5a871862017-03-11 13:14:17 -08007455 if (mExtras != null) {
7456 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7457 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007458 if (mRemoteInputHistory) {
7459 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7460 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007461 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007462 }
7463
Alex Hillsd9b04d92016-04-11 16:38:16 -04007464 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7465 Bundle[] bundles = new Bundle[messages.size()];
7466 final int N = messages.size();
7467 for (int i = 0; i < N; i++) {
7468 bundles[i] = messages.get(i).toBundle();
7469 }
7470 return bundles;
7471 }
7472
Selim Cinek88188f22017-09-19 16:46:56 -07007473 /**
7474 * @return A list of messages read from the bundles.
7475 *
7476 * @hide
7477 */
7478 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7479 if (bundles == null) {
7480 return new ArrayList<>();
7481 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007482 List<Message> messages = new ArrayList<>(bundles.length);
7483 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007484 if (bundles[i] instanceof Bundle) {
7485 Message message = getMessageFromBundle((Bundle)bundles[i]);
7486 if (message != null) {
7487 messages.add(message);
7488 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007489 }
7490 }
7491 return messages;
7492 }
7493
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007494 /**
7495 * @return The message that is stored in the bundle or null if the message couldn't be
7496 * resolved.
7497 *
7498 * @hide
7499 */
7500 @Nullable
7501 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007502 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007503 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007504 return null;
7505 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007506
7507 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7508 if (senderPerson == null) {
7509 // Legacy apps that use compat don't actually provide the sender objects
7510 // We need to fix the compat version to provide people / use
7511 // the native api instead
7512 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7513 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007514 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007515 }
7516 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007517 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007518 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007519 senderPerson,
7520 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007521 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7522 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007523 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7524 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007525 }
Shane Brennan5a871862017-03-11 13:14:17 -08007526 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7527 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7528 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007529 return message;
7530 }
7531 } catch (ClassCastException e) {
7532 return null;
7533 }
7534 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007535 }
7536 }
7537
7538 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007539 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007540 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007541 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007542 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007543 * Notification notif = new Notification.Builder(mContext)
7544 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7545 * .setContentText(subject)
7546 * .setSmallIcon(R.drawable.new_mail)
7547 * .setLargeIcon(aBitmap)
7548 * .setStyle(new Notification.InboxStyle()
7549 * .addLine(str1)
7550 * .addLine(str2)
7551 * .setContentTitle(&quot;&quot;)
7552 * .setSummaryText(&quot;+3 more&quot;))
7553 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007554 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007555 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007556 * @see Notification#bigContentView
7557 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007558 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007559
7560 /**
7561 * The number of lines of remote input history allowed until we start reducing lines.
7562 */
7563 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007564 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7565
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007566 public InboxStyle() {
7567 }
7568
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007569 /**
7570 * @deprecated use {@code InboxStyle()}.
7571 */
7572 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007573 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007574 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007575 }
7576
Chris Wrend6297db2012-05-03 16:20:13 -04007577 /**
7578 * Overrides ContentTitle in the big form of the template.
7579 * This defaults to the value passed to setContentTitle().
7580 */
7581 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007582 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007583 return this;
7584 }
7585
7586 /**
7587 * Set the first line of text after the detail section in the big form of the template.
7588 */
7589 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007590 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007591 return this;
7592 }
7593
Chris Wren0bd664d2012-08-01 13:56:56 -04007594 /**
7595 * Append a line to the digest section of the Inbox notification.
7596 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007597 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007598 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007599 return this;
7600 }
7601
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007602 /**
7603 * @hide
7604 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007605 public ArrayList<CharSequence> getLines() {
7606 return mTexts;
7607 }
7608
7609 /**
7610 * @hide
7611 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007612 public void addExtras(Bundle extras) {
7613 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007614
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007615 CharSequence[] a = new CharSequence[mTexts.size()];
7616 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7617 }
7618
Christoph Studer4600f9b2014-07-22 22:44:43 +02007619 /**
7620 * @hide
7621 */
7622 @Override
7623 protected void restoreFromExtras(Bundle extras) {
7624 super.restoreFromExtras(extras);
7625
7626 mTexts.clear();
7627 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7628 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7629 }
7630 }
7631
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007632 /**
7633 * @hide
7634 */
7635 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007636 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007637 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007638 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7639 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007640
Selim Cinek384804b2018-04-18 14:31:07 +08007641 TemplateBindResult result = new TemplateBindResult();
7642 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007643
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007644 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007645
Chris Wrend6297db2012-05-03 16:20:13 -04007646 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 -04007647 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007648
Chris Wren4ed80d52012-05-17 09:30:03 -04007649 // Make sure all rows are gone in case we reuse a view.
7650 for (int rowId : rowIds) {
7651 contentView.setViewVisibility(rowId, View.GONE);
7652 }
7653
Daniel Sandler879c5e02012-04-17 16:46:51 -04007654 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007655 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7656 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007657 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007658 int onlyViewId = 0;
7659 int maxRows = rowIds.length;
7660 if (mBuilder.mActions.size() > 0) {
7661 maxRows--;
7662 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007663 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7664 EXTRA_REMOTE_INPUT_HISTORY);
7665 if (remoteInputHistory != null
7666 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7667 // Let's remove some messages to make room for the remote input history.
7668 // 1 is always able to fit, but let's remove them if they are 2 or 3
7669 int numRemoteInputs = Math.min(remoteInputHistory.length,
7670 MAX_REMOTE_INPUT_HISTORY_LINES);
7671 int totalNumRows = mTexts.size() + numRemoteInputs
7672 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7673 if (totalNumRows > maxRows) {
7674 int overflow = totalNumRows - maxRows;
7675 if (mTexts.size() > maxRows) {
7676 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7677 // few messages, even with the remote input
7678 maxRows -= overflow;
7679 } else {
7680 // otherwise we drop the first messages
7681 i = overflow;
7682 }
7683 }
7684 }
Selim Cinek07c80172016-04-21 16:40:47 -07007685 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007686 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007687 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007688 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007689 contentView.setTextViewText(rowIds[i],
7690 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007691 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007692 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007693 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007694 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007695 if (first) {
7696 onlyViewId = rowIds[i];
7697 } else {
7698 onlyViewId = 0;
7699 }
Selim Cinek247fa012016-02-18 09:50:48 -08007700 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007701 }
7702 i++;
7703 }
Selim Cinek07c80172016-04-21 16:40:47 -07007704 if (onlyViewId != 0) {
7705 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7706 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7707 R.dimen.notification_text_margin_top);
7708 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7709 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007710
Daniel Sandler879c5e02012-04-17 16:46:51 -04007711 return contentView;
7712 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007713
Julia Reynolds7217dc92018-03-07 12:12:09 -05007714 /**
7715 * @hide
7716 */
7717 @Override
7718 public boolean areNotificationsVisiblyDifferent(Style other) {
7719 if (other == null || getClass() != other.getClass()) {
7720 return true;
7721 }
7722 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007723
7724 final ArrayList<CharSequence> myLines = getLines();
7725 final ArrayList<CharSequence> newLines = newS.getLines();
7726 final int n = myLines.size();
7727 if (n != newLines.size()) {
7728 return true;
7729 }
7730
7731 for (int i = 0; i < n; i++) {
7732 if (!Objects.equals(
7733 String.valueOf(myLines.get(i)),
7734 String.valueOf(newLines.get(i)))) {
7735 return true;
7736 }
7737 }
7738 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007739 }
7740
Selim Cinek384804b2018-04-18 14:31:07 +08007741 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007742 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007743 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007744 if (first) {
7745 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7746 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7747 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007748 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007749 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007750 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007751 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007752 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007753 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007754 }
Dan Sandler842dd772014-05-15 09:36:47 -04007755
7756 /**
7757 * Notification style for media playback notifications.
7758 *
7759 * In the expanded form, {@link Notification#bigContentView}, up to 5
7760 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007761 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007762 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7763 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7764 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007765 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007766 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7767 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007768 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007769 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007770 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007771 * Notifications created with MediaStyle will have their category set to
7772 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7773 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007774 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007775 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7776 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007777 * the System UI can identify this as a notification representing an active media session
7778 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7779 *
Selim Cinek99104832017-01-25 14:47:33 -08007780 * <p>
7781 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7782 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7783 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7784 * <p>
7785 *
Dan Sandler842dd772014-05-15 09:36:47 -04007786 * To use this style with your Notification, feed it to
7787 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7788 * <pre class="prettyprint">
7789 * Notification noti = new Notification.Builder()
7790 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007791 * .setContentTitle(&quot;Track title&quot;)
7792 * .setContentText(&quot;Artist - Album&quot;)
7793 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007794 * .setStyle(<b>new Notification.MediaStyle()</b>
7795 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007796 * .build();
7797 * </pre>
7798 *
7799 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007800 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007801 */
7802 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04007803 // Changing max media buttons requires also changing templates
7804 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007805 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007806 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04007807 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
7808 R.id.action0,
7809 R.id.action1,
7810 R.id.action2,
7811 R.id.action3,
7812 R.id.action4,
7813 };
Dan Sandler842dd772014-05-15 09:36:47 -04007814
7815 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007816 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007817
7818 public MediaStyle() {
7819 }
7820
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007821 /**
7822 * @deprecated use {@code MediaStyle()}.
7823 */
7824 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007825 public MediaStyle(Builder builder) {
7826 setBuilder(builder);
7827 }
7828
7829 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007830 * 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 -04007831 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007832 *
7833 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007834 */
7835 public MediaStyle setShowActionsInCompactView(int...actions) {
7836 mActionsToShowInCompact = actions;
7837 return this;
7838 }
7839
7840 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007841 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7842 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007843 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007844 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007845 mToken = token;
7846 return this;
7847 }
7848
Christoph Studer4600f9b2014-07-22 22:44:43 +02007849 /**
7850 * @hide
7851 */
Dan Sandler842dd772014-05-15 09:36:47 -04007852 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007853 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007854 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007855 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007856 if (wip.category == null) {
7857 wip.category = Notification.CATEGORY_TRANSPORT;
7858 }
Dan Sandler842dd772014-05-15 09:36:47 -04007859 return wip;
7860 }
7861
Christoph Studer4600f9b2014-07-22 22:44:43 +02007862 /**
7863 * @hide
7864 */
7865 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007866 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007867 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007868 }
7869
7870 /**
7871 * @hide
7872 */
7873 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007874 public RemoteViews makeBigContentView() {
7875 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007876 }
7877
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007878 /**
7879 * @hide
7880 */
7881 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007882 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007883 RemoteViews expanded = makeMediaBigContentView();
7884 return expanded != null ? expanded : makeMediaContentView();
7885 }
7886
Dan Sandler842dd772014-05-15 09:36:47 -04007887 /** @hide */
7888 @Override
7889 public void addExtras(Bundle extras) {
7890 super.addExtras(extras);
7891
7892 if (mToken != null) {
7893 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7894 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007895 if (mActionsToShowInCompact != null) {
7896 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7897 }
Dan Sandler842dd772014-05-15 09:36:47 -04007898 }
7899
Christoph Studer4600f9b2014-07-22 22:44:43 +02007900 /**
7901 * @hide
7902 */
7903 @Override
7904 protected void restoreFromExtras(Bundle extras) {
7905 super.restoreFromExtras(extras);
7906
7907 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7908 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7909 }
7910 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7911 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7912 }
7913 }
7914
Julia Reynolds7217dc92018-03-07 12:12:09 -05007915 /**
7916 * @hide
7917 */
7918 @Override
7919 public boolean areNotificationsVisiblyDifferent(Style other) {
7920 if (other == null || getClass() != other.getClass()) {
7921 return true;
7922 }
7923 // All fields to compare are on the Notification object
7924 return false;
7925 }
7926
Gus Prevas9cc96602018-10-11 11:24:23 -04007927 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
7928 Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007929 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04007930 container.setViewVisibility(buttonId, View.VISIBLE);
7931 container.setImageViewIcon(buttonId, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007932
7933 // If the action buttons should not be tinted, then just use the default
7934 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04007935 Resources resources = mBuilder.mContext.getResources();
7936 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07007937 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
7938 == Configuration.UI_MODE_NIGHT_YES;
Anthony Chenad4d1582017-04-10 16:07:58 -07007939 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7940 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007941 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07007942 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07007943
Gus Prevas9cc96602018-10-11 11:24:23 -04007944 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07007945 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04007946
7947 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
7948 new int[]{ android.R.attr.colorControlHighlight });
7949 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
7950 typedArray.recycle();
7951 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
7952 Color.blue(tintColor));
7953 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
7954
Dan Sandler842dd772014-05-15 09:36:47 -04007955 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04007956 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04007957 }
Gus Prevas9cc96602018-10-11 11:24:23 -04007958 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04007959 }
7960
7961 private RemoteViews makeMediaContentView() {
7962 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007963 R.layout.notification_template_material_media, false, /* hasProgress */
7964 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007965
7966 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04007967 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04007968 ? 0
7969 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04007970 if (numActionsToShow > numActions) {
7971 throw new IllegalArgumentException(String.format(
7972 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7973 numActions, numActions - 1));
7974 }
7975 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
7976 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04007977 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Gus Prevas9cc96602018-10-11 11:24:23 -04007978 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, getActionColor());
7979 } else {
7980 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04007981 }
7982 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007983 handleImage(view);
7984 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007985 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007986 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007987 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007988 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007989 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007990 return view;
7991 }
7992
Selim Cinekc7f5a822018-03-20 19:32:06 -07007993 private int getActionColor() {
7994 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7995 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007996 }
7997
Dan Sandler842dd772014-05-15 09:36:47 -04007998 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007999 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008000 // Dont add an expanded view if there is no more content to be revealed
8001 int actionsInCompact = mActionsToShowInCompact == null
8002 ? 0
8003 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008004 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008005 return null;
8006 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008007 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008008 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008009
Gus Prevas9cc96602018-10-11 11:24:23 -04008010 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8011 if (i < actionCount) {
8012 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07008013 getActionColor());
Gus Prevas9cc96602018-10-11 11:24:23 -04008014 } else {
8015 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008016 }
8017 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05008018 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008019 return big;
8020 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008021
Selim Cinek5bf069a2015-11-10 19:14:27 -05008022 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008023 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008024 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8025 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008026 }
8027 }
8028
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008029 /**
8030 * @hide
8031 */
8032 @Override
8033 protected boolean hasProgress() {
8034 return false;
8035 }
Dan Sandler842dd772014-05-15 09:36:47 -04008036 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008037
Selim Cinek593610c2016-02-16 18:42:57 -08008038 /**
8039 * Notification style for custom views that are decorated by the system
8040 *
8041 * <p>Instead of providing a notification that is completely custom, a developer can set this
8042 * style and still obtain system decorations like the notification header with the expand
8043 * affordance and actions.
8044 *
8045 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8046 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8047 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8048 * corresponding custom views to display.
8049 *
8050 * To use this style with your Notification, feed it to
8051 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8052 * <pre class="prettyprint">
8053 * Notification noti = new Notification.Builder()
8054 * .setSmallIcon(R.drawable.ic_stat_player)
8055 * .setLargeIcon(albumArtBitmap))
8056 * .setCustomContentView(contentView);
8057 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8058 * .build();
8059 * </pre>
8060 */
8061 public static class DecoratedCustomViewStyle extends Style {
8062
8063 public DecoratedCustomViewStyle() {
8064 }
8065
Selim Cinek593610c2016-02-16 18:42:57 -08008066 /**
8067 * @hide
8068 */
8069 public boolean displayCustomViewInline() {
8070 return true;
8071 }
8072
8073 /**
8074 * @hide
8075 */
8076 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008077 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008078 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8079 }
8080
8081 /**
8082 * @hide
8083 */
8084 @Override
8085 public RemoteViews makeBigContentView() {
8086 return makeDecoratedBigContentView();
8087 }
8088
8089 /**
8090 * @hide
8091 */
8092 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008093 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008094 return makeDecoratedHeadsUpContentView();
8095 }
8096
Selim Cinek593610c2016-02-16 18:42:57 -08008097 private RemoteViews makeDecoratedHeadsUpContentView() {
8098 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8099 ? mBuilder.mN.contentView
8100 : mBuilder.mN.headsUpContentView;
8101 if (mBuilder.mActions.size() == 0) {
8102 return makeStandardTemplateWithCustomContent(headsUpContentView);
8103 }
Selim Cinek384804b2018-04-18 14:31:07 +08008104 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008105 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008106 mBuilder.getBigBaseLayoutResource(), result);
8107 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008108 return remoteViews;
8109 }
8110
Selim Cinek593610c2016-02-16 18:42:57 -08008111 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008112 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008113 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008114 mBuilder.getBaseLayoutResource(), result);
8115 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008116 return remoteViews;
8117 }
8118
Selim Cinek593610c2016-02-16 18:42:57 -08008119 private RemoteViews makeDecoratedBigContentView() {
8120 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8121 ? mBuilder.mN.contentView
8122 : mBuilder.mN.bigContentView;
8123 if (mBuilder.mActions.size() == 0) {
8124 return makeStandardTemplateWithCustomContent(bigContentView);
8125 }
Selim Cinek384804b2018-04-18 14:31:07 +08008126 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008127 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008128 mBuilder.getBigBaseLayoutResource(), result);
8129 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008130 return remoteViews;
8131 }
Selim Cinek247fa012016-02-18 09:50:48 -08008132
8133 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008134 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008135 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008136 // Need to clone customContent before adding, because otherwise it can no longer be
8137 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008138 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008139 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8140 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008141 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008142 }
Selim Cinek247fa012016-02-18 09:50:48 -08008143 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008144 Resources resources = mBuilder.mContext.getResources();
8145 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008146 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008147 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008148 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008149
8150 /**
8151 * @hide
8152 */
8153 @Override
8154 public boolean areNotificationsVisiblyDifferent(Style other) {
8155 if (other == null || getClass() != other.getClass()) {
8156 return true;
8157 }
8158 // Comparison done for all custom RemoteViews, independent of style
8159 return false;
8160 }
Selim Cinek593610c2016-02-16 18:42:57 -08008161 }
8162
Selim Cinek03eb3b72016-02-18 10:39:45 -08008163 /**
8164 * Notification style for media custom views that are decorated by the system
8165 *
8166 * <p>Instead of providing a media notification that is completely custom, a developer can set
8167 * this style and still obtain system decorations like the notification header with the expand
8168 * affordance and actions.
8169 *
8170 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8171 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8172 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8173 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008174 * <p>
8175 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8176 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8177 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008178 * To use this style with your Notification, feed it to
8179 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8180 * <pre class="prettyprint">
8181 * Notification noti = new Notification.Builder()
8182 * .setSmallIcon(R.drawable.ic_stat_player)
8183 * .setLargeIcon(albumArtBitmap))
8184 * .setCustomContentView(contentView);
8185 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8186 * .setMediaSession(mySession))
8187 * .build();
8188 * </pre>
8189 *
8190 * @see android.app.Notification.DecoratedCustomViewStyle
8191 * @see android.app.Notification.MediaStyle
8192 */
8193 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8194
8195 public DecoratedMediaCustomViewStyle() {
8196 }
8197
Selim Cinek03eb3b72016-02-18 10:39:45 -08008198 /**
8199 * @hide
8200 */
8201 public boolean displayCustomViewInline() {
8202 return true;
8203 }
8204
8205 /**
8206 * @hide
8207 */
8208 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008209 public RemoteViews makeContentView(boolean increasedHeight) {
8210 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008211 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8212 mBuilder.mN.contentView);
8213 }
8214
8215 /**
8216 * @hide
8217 */
8218 @Override
8219 public RemoteViews makeBigContentView() {
8220 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8221 ? mBuilder.mN.bigContentView
8222 : mBuilder.mN.contentView;
8223 return makeBigContentViewWithCustomContent(customRemoteView);
8224 }
8225
8226 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8227 RemoteViews remoteViews = super.makeBigContentView();
8228 if (remoteViews != null) {
8229 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8230 customRemoteView);
8231 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008232 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008233 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8234 customRemoteView);
8235 } else {
8236 return null;
8237 }
8238 }
8239
8240 /**
8241 * @hide
8242 */
8243 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008244 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008245 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8246 ? mBuilder.mN.headsUpContentView
8247 : mBuilder.mN.contentView;
8248 return makeBigContentViewWithCustomContent(customRemoteView);
8249 }
8250
Julia Reynolds7217dc92018-03-07 12:12:09 -05008251 /**
8252 * @hide
8253 */
8254 @Override
8255 public boolean areNotificationsVisiblyDifferent(Style other) {
8256 if (other == null || getClass() != other.getClass()) {
8257 return true;
8258 }
8259 // Comparison done for all custom RemoteViews, independent of style
8260 return false;
8261 }
8262
Selim Cinek03eb3b72016-02-18 10:39:45 -08008263 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8264 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008265 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008266 // Need to clone customContent before adding, because otherwise it can no longer be
8267 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008268 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008269 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008270 remoteViews.removeAllViews(id);
8271 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008272 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008273 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008274 return remoteViews;
8275 }
8276 }
8277
Christoph Studer4600f9b2014-07-22 22:44:43 +02008278 // When adding a new Style subclass here, don't forget to update
8279 // Builder.getNotificationStyleClass.
8280
Griff Hazen61a9e862014-05-22 16:05:19 -07008281 /**
8282 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8283 * metadata or change options on a notification builder.
8284 */
8285 public interface Extender {
8286 /**
8287 * Apply this extender to a notification builder.
8288 * @param builder the builder to be modified.
8289 * @return the build object for chaining.
8290 */
8291 public Builder extend(Builder builder);
8292 }
8293
8294 /**
8295 * Helper class to add wearable extensions to notifications.
8296 * <p class="note"> See
8297 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8298 * for Android Wear</a> for more information on how to use this class.
8299 * <p>
8300 * To create a notification with wearable extensions:
8301 * <ol>
8302 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8303 * properties.
8304 * <li>Create a {@link android.app.Notification.WearableExtender}.
8305 * <li>Set wearable-specific properties using the
8306 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8307 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8308 * notification.
8309 * <li>Post the notification to the notification system with the
8310 * {@code NotificationManager.notify(...)} methods.
8311 * </ol>
8312 *
8313 * <pre class="prettyprint">
8314 * Notification notif = new Notification.Builder(mContext)
8315 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8316 * .setContentText(subject)
8317 * .setSmallIcon(R.drawable.new_mail)
8318 * .extend(new Notification.WearableExtender()
8319 * .setContentIcon(R.drawable.new_mail))
8320 * .build();
8321 * NotificationManager notificationManger =
8322 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8323 * notificationManger.notify(0, notif);</pre>
8324 *
8325 * <p>Wearable extensions can be accessed on an existing notification by using the
8326 * {@code WearableExtender(Notification)} constructor,
8327 * and then using the {@code get} methods to access values.
8328 *
8329 * <pre class="prettyprint">
8330 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8331 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008332 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008333 */
8334 public static final class WearableExtender implements Extender {
8335 /**
8336 * Sentinel value for an action index that is unset.
8337 */
8338 public static final int UNSET_ACTION_INDEX = -1;
8339
8340 /**
8341 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8342 * default sizing.
8343 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008344 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008345 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008346 *
8347 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008348 */
Gus Prevasd7363752018-09-18 14:35:15 -04008349 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008350 public static final int SIZE_DEFAULT = 0;
8351
8352 /**
8353 * Size value for use with {@link #setCustomSizePreset} to show this notification
8354 * with an extra small size.
8355 * <p>This value is only applicable for custom display notifications created using
8356 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008357 *
8358 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008359 */
Gus Prevasd7363752018-09-18 14:35:15 -04008360 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008361 public static final int SIZE_XSMALL = 1;
8362
8363 /**
8364 * Size value for use with {@link #setCustomSizePreset} to show this notification
8365 * with a small size.
8366 * <p>This value is only applicable for custom display notifications created using
8367 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008368 *
8369 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008370 */
Gus Prevasd7363752018-09-18 14:35:15 -04008371 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008372 public static final int SIZE_SMALL = 2;
8373
8374 /**
8375 * Size value for use with {@link #setCustomSizePreset} to show this notification
8376 * with a medium size.
8377 * <p>This value is only applicable for custom display notifications created using
8378 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008379 *
8380 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008381 */
Gus Prevasd7363752018-09-18 14:35:15 -04008382 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008383 public static final int SIZE_MEDIUM = 3;
8384
8385 /**
8386 * Size value for use with {@link #setCustomSizePreset} to show this notification
8387 * with a large size.
8388 * <p>This value is only applicable for custom display notifications created using
8389 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008390 *
8391 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008392 */
Gus Prevasd7363752018-09-18 14:35:15 -04008393 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008394 public static final int SIZE_LARGE = 4;
8395
Griff Hazend5f11f92014-05-27 15:40:09 -07008396 /**
8397 * Size value for use with {@link #setCustomSizePreset} to show this notification
8398 * full screen.
8399 * <p>This value is only applicable for custom display notifications created using
8400 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008401 *
8402 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07008403 */
Gus Prevasd7363752018-09-18 14:35:15 -04008404 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07008405 public static final int SIZE_FULL_SCREEN = 5;
8406
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008407 /**
8408 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8409 * short amount of time when this notification is displayed on the screen. This
8410 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04008411 *
8412 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008413 */
Gus Prevasd7363752018-09-18 14:35:15 -04008414 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008415 public static final int SCREEN_TIMEOUT_SHORT = 0;
8416
8417 /**
8418 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8419 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04008420 *
8421 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008422 */
Gus Prevasd7363752018-09-18 14:35:15 -04008423 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008424 public static final int SCREEN_TIMEOUT_LONG = -1;
8425
Griff Hazen61a9e862014-05-22 16:05:19 -07008426 /** Notification extra which contains wearable extensions */
8427 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8428
Pete Gastaf6781d2014-10-07 15:17:05 -04008429 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008430 private static final String KEY_ACTIONS = "actions";
8431 private static final String KEY_FLAGS = "flags";
8432 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8433 private static final String KEY_PAGES = "pages";
8434 private static final String KEY_BACKGROUND = "background";
8435 private static final String KEY_CONTENT_ICON = "contentIcon";
8436 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8437 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8438 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8439 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8440 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008441 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008442 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008443 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008444
8445 // Flags bitwise-ored to mFlags
8446 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8447 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8448 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8449 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008450 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008451 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008452 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008453
8454 // Default value for flags integer
8455 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8456
8457 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8458 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8459
8460 private ArrayList<Action> mActions = new ArrayList<Action>();
8461 private int mFlags = DEFAULT_FLAGS;
8462 private PendingIntent mDisplayIntent;
8463 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8464 private Bitmap mBackground;
8465 private int mContentIcon;
8466 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8467 private int mContentActionIndex = UNSET_ACTION_INDEX;
8468 private int mCustomSizePreset = SIZE_DEFAULT;
8469 private int mCustomContentHeight;
8470 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008471 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008472 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008473 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008474
8475 /**
8476 * Create a {@link android.app.Notification.WearableExtender} with default
8477 * options.
8478 */
8479 public WearableExtender() {
8480 }
8481
8482 public WearableExtender(Notification notif) {
8483 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8484 if (wearableBundle != null) {
8485 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8486 if (actions != null) {
8487 mActions.addAll(actions);
8488 }
8489
8490 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8491 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8492
8493 Notification[] pages = getNotificationArrayFromBundle(
8494 wearableBundle, KEY_PAGES);
8495 if (pages != null) {
8496 Collections.addAll(mPages, pages);
8497 }
8498
8499 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8500 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8501 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8502 DEFAULT_CONTENT_ICON_GRAVITY);
8503 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8504 UNSET_ACTION_INDEX);
8505 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8506 SIZE_DEFAULT);
8507 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8508 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008509 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008510 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008511 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008512 }
8513 }
8514
8515 /**
8516 * Apply wearable extensions to a notification that is being built. This is typically
8517 * called by the {@link android.app.Notification.Builder#extend} method of
8518 * {@link android.app.Notification.Builder}.
8519 */
8520 @Override
8521 public Notification.Builder extend(Notification.Builder builder) {
8522 Bundle wearableBundle = new Bundle();
8523
8524 if (!mActions.isEmpty()) {
8525 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8526 }
8527 if (mFlags != DEFAULT_FLAGS) {
8528 wearableBundle.putInt(KEY_FLAGS, mFlags);
8529 }
8530 if (mDisplayIntent != null) {
8531 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8532 }
8533 if (!mPages.isEmpty()) {
8534 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8535 new Notification[mPages.size()]));
8536 }
8537 if (mBackground != null) {
8538 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8539 }
8540 if (mContentIcon != 0) {
8541 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8542 }
8543 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8544 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8545 }
8546 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8547 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8548 mContentActionIndex);
8549 }
8550 if (mCustomSizePreset != SIZE_DEFAULT) {
8551 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8552 }
8553 if (mCustomContentHeight != 0) {
8554 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8555 }
8556 if (mGravity != DEFAULT_GRAVITY) {
8557 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8558 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008559 if (mHintScreenTimeout != 0) {
8560 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8561 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008562 if (mDismissalId != null) {
8563 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8564 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008565 if (mBridgeTag != null) {
8566 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8567 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008568
8569 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8570 return builder;
8571 }
8572
8573 @Override
8574 public WearableExtender clone() {
8575 WearableExtender that = new WearableExtender();
8576 that.mActions = new ArrayList<Action>(this.mActions);
8577 that.mFlags = this.mFlags;
8578 that.mDisplayIntent = this.mDisplayIntent;
8579 that.mPages = new ArrayList<Notification>(this.mPages);
8580 that.mBackground = this.mBackground;
8581 that.mContentIcon = this.mContentIcon;
8582 that.mContentIconGravity = this.mContentIconGravity;
8583 that.mContentActionIndex = this.mContentActionIndex;
8584 that.mCustomSizePreset = this.mCustomSizePreset;
8585 that.mCustomContentHeight = this.mCustomContentHeight;
8586 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008587 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008588 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008589 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008590 return that;
8591 }
8592
8593 /**
8594 * Add a wearable action to this notification.
8595 *
8596 * <p>When wearable actions are added using this method, the set of actions that
8597 * show on a wearable device splits from devices that only show actions added
8598 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8599 * of which actions display on different devices.
8600 *
8601 * @param action the action to add to this notification
8602 * @return this object for method chaining
8603 * @see android.app.Notification.Action
8604 */
8605 public WearableExtender addAction(Action action) {
8606 mActions.add(action);
8607 return this;
8608 }
8609
8610 /**
8611 * Adds wearable actions to this notification.
8612 *
8613 * <p>When wearable actions are added using this method, the set of actions that
8614 * show on a wearable device splits from devices that only show actions added
8615 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8616 * of which actions display on different devices.
8617 *
8618 * @param actions the actions to add to this notification
8619 * @return this object for method chaining
8620 * @see android.app.Notification.Action
8621 */
8622 public WearableExtender addActions(List<Action> actions) {
8623 mActions.addAll(actions);
8624 return this;
8625 }
8626
8627 /**
8628 * Clear all wearable actions present on this builder.
8629 * @return this object for method chaining.
8630 * @see #addAction
8631 */
8632 public WearableExtender clearActions() {
8633 mActions.clear();
8634 return this;
8635 }
8636
8637 /**
8638 * Get the wearable actions present on this notification.
8639 */
8640 public List<Action> getActions() {
8641 return mActions;
8642 }
8643
8644 /**
8645 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008646 * this notification. The {@link PendingIntent} provided should be for an activity.
8647 *
8648 * <pre class="prettyprint">
8649 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8650 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8651 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8652 * Notification notif = new Notification.Builder(context)
8653 * .extend(new Notification.WearableExtender()
8654 * .setDisplayIntent(displayPendingIntent)
8655 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8656 * .build();</pre>
8657 *
8658 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008659 * should have an empty task affinity. It is also recommended to use the device
8660 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008661 *
8662 * <p>Example AndroidManifest.xml entry:
8663 * <pre class="prettyprint">
8664 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8665 * android:exported=&quot;true&quot;
8666 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008667 * android:taskAffinity=&quot;&quot;
8668 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008669 *
8670 * @param intent the {@link PendingIntent} for an activity
8671 * @return this object for method chaining
8672 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04008673 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008674 */
Gus Prevasd7363752018-09-18 14:35:15 -04008675 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008676 public WearableExtender setDisplayIntent(PendingIntent intent) {
8677 mDisplayIntent = intent;
8678 return this;
8679 }
8680
8681 /**
8682 * Get the intent to launch inside of an activity view when displaying this
8683 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04008684 *
8685 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008686 */
Gus Prevasd7363752018-09-18 14:35:15 -04008687 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008688 public PendingIntent getDisplayIntent() {
8689 return mDisplayIntent;
8690 }
8691
8692 /**
8693 * Add an additional page of content to display with this notification. The current
8694 * notification forms the first page, and pages added using this function form
8695 * subsequent pages. This field can be used to separate a notification into multiple
8696 * sections.
8697 *
8698 * @param page the notification to add as another page
8699 * @return this object for method chaining
8700 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008701 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008702 */
Gus Prevasd7363752018-09-18 14:35:15 -04008703 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008704 public WearableExtender addPage(Notification page) {
8705 mPages.add(page);
8706 return this;
8707 }
8708
8709 /**
8710 * Add additional pages of content to display with this notification. The current
8711 * notification forms the first page, and pages added using this function form
8712 * subsequent pages. This field can be used to separate a notification into multiple
8713 * sections.
8714 *
8715 * @param pages a list of notifications
8716 * @return this object for method chaining
8717 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04008718 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008719 */
Gus Prevasd7363752018-09-18 14:35:15 -04008720 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008721 public WearableExtender addPages(List<Notification> pages) {
8722 mPages.addAll(pages);
8723 return this;
8724 }
8725
8726 /**
8727 * Clear all additional pages present on this builder.
8728 * @return this object for method chaining.
8729 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04008730 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008731 */
Gus Prevasd7363752018-09-18 14:35:15 -04008732 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008733 public WearableExtender clearPages() {
8734 mPages.clear();
8735 return this;
8736 }
8737
8738 /**
8739 * Get the array of additional pages of content for displaying this notification. The
8740 * current notification forms the first page, and elements within this array form
8741 * subsequent pages. This field can be used to separate a notification into multiple
8742 * sections.
8743 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04008744 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008745 */
Gus Prevasd7363752018-09-18 14:35:15 -04008746 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008747 public List<Notification> getPages() {
8748 return mPages;
8749 }
8750
8751 /**
8752 * Set a background image to be displayed behind the notification content.
8753 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8754 * will work with any notification style.
8755 *
8756 * @param background the background bitmap
8757 * @return this object for method chaining
8758 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008759 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008760 */
Gus Prevasd7363752018-09-18 14:35:15 -04008761 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008762 public WearableExtender setBackground(Bitmap background) {
8763 mBackground = background;
8764 return this;
8765 }
8766
8767 /**
8768 * Get a background image to be displayed behind the notification content.
8769 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8770 * will work with any notification style.
8771 *
8772 * @return the background image
8773 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04008774 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008775 */
Gus Prevasd7363752018-09-18 14:35:15 -04008776 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008777 public Bitmap getBackground() {
8778 return mBackground;
8779 }
8780
8781 /**
8782 * Set an icon that goes with the content of this notification.
8783 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008784 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008785 public WearableExtender setContentIcon(int icon) {
8786 mContentIcon = icon;
8787 return this;
8788 }
8789
8790 /**
8791 * Get an icon that goes with the content of this notification.
8792 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008793 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008794 public int getContentIcon() {
8795 return mContentIcon;
8796 }
8797
8798 /**
8799 * Set the gravity that the content icon should have within the notification display.
8800 * Supported values include {@link android.view.Gravity#START} and
8801 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8802 * @see #setContentIcon
8803 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008804 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008805 public WearableExtender setContentIconGravity(int contentIconGravity) {
8806 mContentIconGravity = contentIconGravity;
8807 return this;
8808 }
8809
8810 /**
8811 * Get the gravity that the content icon should have within the notification display.
8812 * Supported values include {@link android.view.Gravity#START} and
8813 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8814 * @see #getContentIcon
8815 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008816 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008817 public int getContentIconGravity() {
8818 return mContentIconGravity;
8819 }
8820
8821 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008822 * Set an action from this notification's actions as the primary action. If the action has a
8823 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
8824 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07008825 *
Gus Prevasd7363752018-09-18 14:35:15 -04008826 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07008827 * If wearable actions were added to the main notification, this index
8828 * will apply to that list, otherwise it will apply to the regular
8829 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008830 */
8831 public WearableExtender setContentAction(int actionIndex) {
8832 mContentActionIndex = actionIndex;
8833 return this;
8834 }
8835
8836 /**
Gus Prevasd7363752018-09-18 14:35:15 -04008837 * Get the index of the notification action, if any, that was specified as the primary
8838 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07008839 *
8840 * <p>If wearable specific actions were added to the main notification, this index will
8841 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008842 *
8843 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008844 */
8845 public int getContentAction() {
8846 return mContentActionIndex;
8847 }
8848
8849 /**
8850 * Set the gravity that this notification should have within the available viewport space.
8851 * Supported values include {@link android.view.Gravity#TOP},
8852 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8853 * The default value is {@link android.view.Gravity#BOTTOM}.
8854 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008855 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008856 public WearableExtender setGravity(int gravity) {
8857 mGravity = gravity;
8858 return this;
8859 }
8860
8861 /**
8862 * Get the gravity that this notification should have within the available viewport space.
8863 * Supported values include {@link android.view.Gravity#TOP},
8864 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8865 * The default value is {@link android.view.Gravity#BOTTOM}.
8866 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008867 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008868 public int getGravity() {
8869 return mGravity;
8870 }
8871
8872 /**
8873 * Set the custom size preset for the display of this notification out of the available
8874 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8875 * {@link #SIZE_LARGE}.
8876 * <p>Some custom size presets are only applicable for custom display notifications created
8877 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8878 * documentation for the preset in question. See also
8879 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8880 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008881 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008882 public WearableExtender setCustomSizePreset(int sizePreset) {
8883 mCustomSizePreset = sizePreset;
8884 return this;
8885 }
8886
8887 /**
8888 * Get the custom size preset for the display of this notification out of the available
8889 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8890 * {@link #SIZE_LARGE}.
8891 * <p>Some custom size presets are only applicable for custom display notifications created
8892 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8893 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8894 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008895 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008896 public int getCustomSizePreset() {
8897 return mCustomSizePreset;
8898 }
8899
8900 /**
8901 * Set the custom height in pixels for the display of this notification's content.
8902 * <p>This option is only available for custom display notifications created
8903 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8904 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8905 * {@link #getCustomContentHeight}.
8906 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008907 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008908 public WearableExtender setCustomContentHeight(int height) {
8909 mCustomContentHeight = height;
8910 return this;
8911 }
8912
8913 /**
8914 * Get the custom height in pixels for the display of this notification's content.
8915 * <p>This option is only available for custom display notifications created
8916 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8917 * {@link #setCustomContentHeight}.
8918 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008919 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008920 public int getCustomContentHeight() {
8921 return mCustomContentHeight;
8922 }
8923
8924 /**
8925 * Set whether the scrolling position for the contents of this notification should start
8926 * at the bottom of the contents instead of the top when the contents are too long to
8927 * display within the screen. Default is false (start scroll at the top).
8928 */
8929 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8930 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8931 return this;
8932 }
8933
8934 /**
8935 * Get whether the scrolling position for the contents of this notification should start
8936 * at the bottom of the contents instead of the top when the contents are too long to
8937 * display within the screen. Default is false (start scroll at the top).
8938 */
8939 public boolean getStartScrollBottom() {
8940 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8941 }
8942
8943 /**
8944 * Set whether the content intent is available when the wearable device is not connected
8945 * to a companion device. The user can still trigger this intent when the wearable device
8946 * is offline, but a visual hint will indicate that the content intent may not be available.
8947 * Defaults to true.
8948 */
8949 public WearableExtender setContentIntentAvailableOffline(
8950 boolean contentIntentAvailableOffline) {
8951 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8952 return this;
8953 }
8954
8955 /**
8956 * Get whether the content intent is available when the wearable device is not connected
8957 * to a companion device. The user can still trigger this intent when the wearable device
8958 * is offline, but a visual hint will indicate that the content intent may not be available.
8959 * Defaults to true.
8960 */
8961 public boolean getContentIntentAvailableOffline() {
8962 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8963 }
8964
8965 /**
8966 * Set a hint that this notification's icon should not be displayed.
8967 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8968 * @return this object for method chaining
8969 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008970 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008971 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8972 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8973 return this;
8974 }
8975
8976 /**
8977 * Get a hint that this notification's icon should not be displayed.
8978 * @return {@code true} if this icon should not be displayed, false otherwise.
8979 * The default value is {@code false} if this was never set.
8980 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008981 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008982 public boolean getHintHideIcon() {
8983 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8984 }
8985
8986 /**
8987 * Set a visual hint that only the background image of this notification should be
8988 * displayed, and other semantic content should be hidden. This hint is only applicable
8989 * to sub-pages added using {@link #addPage}.
8990 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008991 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008992 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8993 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8994 return this;
8995 }
8996
8997 /**
8998 * Get a visual hint that only the background image of this notification should be
8999 * displayed, and other semantic content should be hidden. This hint is only applicable
9000 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9001 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009002 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009003 public boolean getHintShowBackgroundOnly() {
9004 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9005 }
9006
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009007 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009008 * Set a hint that this notification's background should not be clipped if possible,
9009 * and should instead be resized to fully display on the screen, retaining the aspect
9010 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009011 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9012 * @return this object for method chaining
9013 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009014 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009015 public WearableExtender setHintAvoidBackgroundClipping(
9016 boolean hintAvoidBackgroundClipping) {
9017 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9018 return this;
9019 }
9020
9021 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009022 * Get a hint that this notification's background should not be clipped if possible,
9023 * and should instead be resized to fully display on the screen, retaining the aspect
9024 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009025 * @return {@code true} if it's ok if the background is clipped on the screen, false
9026 * otherwise. The default value is {@code false} if this was never set.
9027 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009028 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009029 public boolean getHintAvoidBackgroundClipping() {
9030 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9031 }
9032
9033 /**
9034 * Set a hint that the screen should remain on for at least this duration when
9035 * this notification is displayed on the screen.
9036 * @param timeout The requested screen timeout in milliseconds. Can also be either
9037 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9038 * @return this object for method chaining
9039 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009040 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009041 public WearableExtender setHintScreenTimeout(int timeout) {
9042 mHintScreenTimeout = timeout;
9043 return this;
9044 }
9045
9046 /**
9047 * Get the duration, in milliseconds, that the screen should remain on for
9048 * when this notification is displayed.
9049 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9050 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9051 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009052 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009053 public int getHintScreenTimeout() {
9054 return mHintScreenTimeout;
9055 }
9056
Alex Hills9ab3a232016-04-05 14:54:56 -04009057 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009058 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9059 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9060 * qr codes, as well as other simple black-and-white tickets.
9061 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9062 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009063 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009064 */
Gus Prevasd7363752018-09-18 14:35:15 -04009065 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009066 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9067 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9068 return this;
9069 }
9070
9071 /**
9072 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9073 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9074 * qr codes, as well as other simple black-and-white tickets.
9075 * @return {@code true} if it should be displayed in ambient, false otherwise
9076 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009077 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009078 */
Gus Prevasd7363752018-09-18 14:35:15 -04009079 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009080 public boolean getHintAmbientBigPicture() {
9081 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9082 }
9083
9084 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009085 * Set a hint that this notification's content intent will launch an {@link Activity}
9086 * directly, telling the platform that it can generate the appropriate transitions.
9087 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9088 * an activity and transitions should be generated, false otherwise.
9089 * @return this object for method chaining
9090 */
9091 public WearableExtender setHintContentIntentLaunchesActivity(
9092 boolean hintContentIntentLaunchesActivity) {
9093 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9094 return this;
9095 }
9096
9097 /**
9098 * Get a hint that this notification's content intent will launch an {@link Activity}
9099 * directly, telling the platform that it can generate the appropriate transitions
9100 * @return {@code true} if the content intent will launch an activity and transitions should
9101 * be generated, false otherwise. The default value is {@code false} if this was never set.
9102 */
9103 public boolean getHintContentIntentLaunchesActivity() {
9104 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9105 }
9106
Nadia Benbernou948627e2016-04-14 14:41:08 -04009107 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009108 * Sets the dismissal id for this notification. If a notification is posted with a
9109 * dismissal id, then when that notification is canceled, notifications on other wearables
9110 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009111 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009112 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009113 * @param dismissalId the dismissal id of the notification.
9114 * @return this object for method chaining
9115 */
9116 public WearableExtender setDismissalId(String dismissalId) {
9117 mDismissalId = dismissalId;
9118 return this;
9119 }
9120
9121 /**
9122 * Returns the dismissal id of the notification.
9123 * @return the dismissal id of the notification or null if it has not been set.
9124 */
9125 public String getDismissalId() {
9126 return mDismissalId;
9127 }
9128
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009129 /**
9130 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9131 * posted from a phone to provide finer-grained control on what notifications are bridged
9132 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9133 * Features to Notifications</a> for more information.
9134 * @param bridgeTag the bridge tag of the notification.
9135 * @return this object for method chaining
9136 */
9137 public WearableExtender setBridgeTag(String bridgeTag) {
9138 mBridgeTag = bridgeTag;
9139 return this;
9140 }
9141
9142 /**
9143 * Returns the bridge tag of the notification.
9144 * @return the bridge tag or null if not present.
9145 */
9146 public String getBridgeTag() {
9147 return mBridgeTag;
9148 }
9149
Griff Hazen61a9e862014-05-22 16:05:19 -07009150 private void setFlag(int mask, boolean value) {
9151 if (value) {
9152 mFlags |= mask;
9153 } else {
9154 mFlags &= ~mask;
9155 }
9156 }
9157 }
9158
9159 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009160 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9161 * with car extensions:
9162 *
9163 * <ol>
9164 * <li>Create an {@link Notification.Builder}, setting any desired
9165 * properties.
9166 * <li>Create a {@link CarExtender}.
9167 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9168 * {@link CarExtender}.
9169 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9170 * to apply the extensions to a notification.
9171 * </ol>
9172 *
9173 * <pre class="prettyprint">
9174 * Notification notification = new Notification.Builder(context)
9175 * ...
9176 * .extend(new CarExtender()
9177 * .set*(...))
9178 * .build();
9179 * </pre>
9180 *
9181 * <p>Car extensions can be accessed on an existing notification by using the
9182 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9183 * to access values.
9184 */
9185 public static final class CarExtender implements Extender {
9186 private static final String TAG = "CarExtender";
9187
9188 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9189 private static final String EXTRA_LARGE_ICON = "large_icon";
9190 private static final String EXTRA_CONVERSATION = "car_conversation";
9191 private static final String EXTRA_COLOR = "app_color";
9192
9193 private Bitmap mLargeIcon;
9194 private UnreadConversation mUnreadConversation;
9195 private int mColor = Notification.COLOR_DEFAULT;
9196
9197 /**
9198 * Create a {@link CarExtender} with default options.
9199 */
9200 public CarExtender() {
9201 }
9202
9203 /**
9204 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9205 *
9206 * @param notif The notification from which to copy options.
9207 */
9208 public CarExtender(Notification notif) {
9209 Bundle carBundle = notif.extras == null ?
9210 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9211 if (carBundle != null) {
9212 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9213 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9214
9215 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9216 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9217 }
9218 }
9219
9220 /**
9221 * Apply car extensions to a notification that is being built. This is typically called by
9222 * the {@link Notification.Builder#extend(Notification.Extender)}
9223 * method of {@link Notification.Builder}.
9224 */
9225 @Override
9226 public Notification.Builder extend(Notification.Builder builder) {
9227 Bundle carExtensions = new Bundle();
9228
9229 if (mLargeIcon != null) {
9230 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9231 }
9232 if (mColor != Notification.COLOR_DEFAULT) {
9233 carExtensions.putInt(EXTRA_COLOR, mColor);
9234 }
9235
9236 if (mUnreadConversation != null) {
9237 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9238 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9239 }
9240
9241 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9242 return builder;
9243 }
9244
9245 /**
9246 * Sets the accent color to use when Android Auto presents the notification.
9247 *
9248 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9249 * to accent the displayed notification. However, not all colors are acceptable in an
9250 * automotive setting. This method can be used to override the color provided in the
9251 * notification in such a situation.
9252 */
Tor Norbye80756e32015-03-02 09:39:27 -08009253 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009254 mColor = color;
9255 return this;
9256 }
9257
9258 /**
9259 * Gets the accent color.
9260 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009261 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009262 */
Tor Norbye80756e32015-03-02 09:39:27 -08009263 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009264 public int getColor() {
9265 return mColor;
9266 }
9267
9268 /**
9269 * Sets the large icon of the car notification.
9270 *
9271 * If no large icon is set in the extender, Android Auto will display the icon
9272 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9273 *
9274 * @param largeIcon The large icon to use in the car notification.
9275 * @return This object for method chaining.
9276 */
9277 public CarExtender setLargeIcon(Bitmap largeIcon) {
9278 mLargeIcon = largeIcon;
9279 return this;
9280 }
9281
9282 /**
9283 * Gets the large icon used in this car notification, or null if no icon has been set.
9284 *
9285 * @return The large icon for the car notification.
9286 * @see CarExtender#setLargeIcon
9287 */
9288 public Bitmap getLargeIcon() {
9289 return mLargeIcon;
9290 }
9291
9292 /**
9293 * Sets the unread conversation in a message notification.
9294 *
9295 * @param unreadConversation The unread part of the conversation this notification conveys.
9296 * @return This object for method chaining.
9297 */
9298 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9299 mUnreadConversation = unreadConversation;
9300 return this;
9301 }
9302
9303 /**
9304 * Returns the unread conversation conveyed by this notification.
9305 * @see #setUnreadConversation(UnreadConversation)
9306 */
9307 public UnreadConversation getUnreadConversation() {
9308 return mUnreadConversation;
9309 }
9310
9311 /**
9312 * A class which holds the unread messages from a conversation.
9313 */
9314 public static class UnreadConversation {
9315 private static final String KEY_AUTHOR = "author";
9316 private static final String KEY_TEXT = "text";
9317 private static final String KEY_MESSAGES = "messages";
9318 private static final String KEY_REMOTE_INPUT = "remote_input";
9319 private static final String KEY_ON_REPLY = "on_reply";
9320 private static final String KEY_ON_READ = "on_read";
9321 private static final String KEY_PARTICIPANTS = "participants";
9322 private static final String KEY_TIMESTAMP = "timestamp";
9323
9324 private final String[] mMessages;
9325 private final RemoteInput mRemoteInput;
9326 private final PendingIntent mReplyPendingIntent;
9327 private final PendingIntent mReadPendingIntent;
9328 private final String[] mParticipants;
9329 private final long mLatestTimestamp;
9330
9331 UnreadConversation(String[] messages, RemoteInput remoteInput,
9332 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9333 String[] participants, long latestTimestamp) {
9334 mMessages = messages;
9335 mRemoteInput = remoteInput;
9336 mReadPendingIntent = readPendingIntent;
9337 mReplyPendingIntent = replyPendingIntent;
9338 mParticipants = participants;
9339 mLatestTimestamp = latestTimestamp;
9340 }
9341
9342 /**
9343 * Gets the list of messages conveyed by this notification.
9344 */
9345 public String[] getMessages() {
9346 return mMessages;
9347 }
9348
9349 /**
9350 * Gets the remote input that will be used to convey the response to a message list, or
9351 * null if no such remote input exists.
9352 */
9353 public RemoteInput getRemoteInput() {
9354 return mRemoteInput;
9355 }
9356
9357 /**
9358 * Gets the pending intent that will be triggered when the user replies to this
9359 * notification.
9360 */
9361 public PendingIntent getReplyPendingIntent() {
9362 return mReplyPendingIntent;
9363 }
9364
9365 /**
9366 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9367 * in this object's message list.
9368 */
9369 public PendingIntent getReadPendingIntent() {
9370 return mReadPendingIntent;
9371 }
9372
9373 /**
9374 * Gets the participants in the conversation.
9375 */
9376 public String[] getParticipants() {
9377 return mParticipants;
9378 }
9379
9380 /**
9381 * Gets the firs participant in the conversation.
9382 */
9383 public String getParticipant() {
9384 return mParticipants.length > 0 ? mParticipants[0] : null;
9385 }
9386
9387 /**
9388 * Gets the timestamp of the conversation.
9389 */
9390 public long getLatestTimestamp() {
9391 return mLatestTimestamp;
9392 }
9393
9394 Bundle getBundleForUnreadConversation() {
9395 Bundle b = new Bundle();
9396 String author = null;
9397 if (mParticipants != null && mParticipants.length > 1) {
9398 author = mParticipants[0];
9399 }
9400 Parcelable[] messages = new Parcelable[mMessages.length];
9401 for (int i = 0; i < messages.length; i++) {
9402 Bundle m = new Bundle();
9403 m.putString(KEY_TEXT, mMessages[i]);
9404 m.putString(KEY_AUTHOR, author);
9405 messages[i] = m;
9406 }
9407 b.putParcelableArray(KEY_MESSAGES, messages);
9408 if (mRemoteInput != null) {
9409 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9410 }
9411 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9412 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9413 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9414 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9415 return b;
9416 }
9417
9418 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9419 if (b == null) {
9420 return null;
9421 }
9422 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9423 String[] messages = null;
9424 if (parcelableMessages != null) {
9425 String[] tmp = new String[parcelableMessages.length];
9426 boolean success = true;
9427 for (int i = 0; i < tmp.length; i++) {
9428 if (!(parcelableMessages[i] instanceof Bundle)) {
9429 success = false;
9430 break;
9431 }
9432 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9433 if (tmp[i] == null) {
9434 success = false;
9435 break;
9436 }
9437 }
9438 if (success) {
9439 messages = tmp;
9440 } else {
9441 return null;
9442 }
9443 }
9444
9445 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9446 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9447
9448 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9449
9450 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9451 if (participants == null || participants.length != 1) {
9452 return null;
9453 }
9454
9455 return new UnreadConversation(messages,
9456 remoteInput,
9457 onReply,
9458 onRead,
9459 participants, b.getLong(KEY_TIMESTAMP));
9460 }
9461 };
9462
9463 /**
9464 * Builder class for {@link CarExtender.UnreadConversation} objects.
9465 */
9466 public static class Builder {
9467 private final List<String> mMessages = new ArrayList<String>();
9468 private final String mParticipant;
9469 private RemoteInput mRemoteInput;
9470 private PendingIntent mReadPendingIntent;
9471 private PendingIntent mReplyPendingIntent;
9472 private long mLatestTimestamp;
9473
9474 /**
9475 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9476 *
9477 * @param name The name of the other participant in the conversation.
9478 */
9479 public Builder(String name) {
9480 mParticipant = name;
9481 }
9482
9483 /**
9484 * Appends a new unread message to the list of messages for this conversation.
9485 *
9486 * The messages should be added from oldest to newest.
9487 *
9488 * @param message The text of the new unread message.
9489 * @return This object for method chaining.
9490 */
9491 public Builder addMessage(String message) {
9492 mMessages.add(message);
9493 return this;
9494 }
9495
9496 /**
9497 * Sets the pending intent and remote input which will convey the reply to this
9498 * notification.
9499 *
9500 * @param pendingIntent The pending intent which will be triggered on a reply.
9501 * @param remoteInput The remote input parcelable which will carry the reply.
9502 * @return This object for method chaining.
9503 *
9504 * @see CarExtender.UnreadConversation#getRemoteInput
9505 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9506 */
9507 public Builder setReplyAction(
9508 PendingIntent pendingIntent, RemoteInput remoteInput) {
9509 mRemoteInput = remoteInput;
9510 mReplyPendingIntent = pendingIntent;
9511
9512 return this;
9513 }
9514
9515 /**
9516 * Sets the pending intent that will be sent once the messages in this notification
9517 * are read.
9518 *
9519 * @param pendingIntent The pending intent to use.
9520 * @return This object for method chaining.
9521 */
9522 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9523 mReadPendingIntent = pendingIntent;
9524 return this;
9525 }
9526
9527 /**
9528 * Sets the timestamp of the most recent message in an unread conversation.
9529 *
9530 * If a messaging notification has been posted by your application and has not
9531 * yet been cancelled, posting a later notification with the same id and tag
9532 * but without a newer timestamp may result in Android Auto not displaying a
9533 * heads up notification for the later notification.
9534 *
9535 * @param timestamp The timestamp of the most recent message in the conversation.
9536 * @return This object for method chaining.
9537 */
9538 public Builder setLatestTimestamp(long timestamp) {
9539 mLatestTimestamp = timestamp;
9540 return this;
9541 }
9542
9543 /**
9544 * Builds a new unread conversation object.
9545 *
9546 * @return The new unread conversation object.
9547 */
9548 public UnreadConversation build() {
9549 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9550 String[] participants = { mParticipant };
9551 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9552 mReadPendingIntent, participants, mLatestTimestamp);
9553 }
9554 }
9555 }
9556
9557 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009558 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9559 * with a TV extension:
9560 *
9561 * <ol>
9562 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9563 * <li>Create a {@link TvExtender}.
9564 * <li>Set TV-specific properties using the {@code set} methods of
9565 * {@link TvExtender}.
9566 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9567 * to apply the extension to a notification.
9568 * </ol>
9569 *
9570 * <pre class="prettyprint">
9571 * Notification notification = new Notification.Builder(context)
9572 * ...
9573 * .extend(new TvExtender()
9574 * .set*(...))
9575 * .build();
9576 * </pre>
9577 *
9578 * <p>TV extensions can be accessed on an existing notification by using the
9579 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9580 * to access values.
9581 *
9582 * @hide
9583 */
9584 @SystemApi
9585 public static final class TvExtender implements Extender {
9586 private static final String TAG = "TvExtender";
9587
9588 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9589 private static final String EXTRA_FLAGS = "flags";
9590 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9591 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009592 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009593 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009594
9595 // Flags bitwise-ored to mFlags
9596 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9597
9598 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009599 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009600 private PendingIntent mContentIntent;
9601 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009602 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009603
9604 /**
9605 * Create a {@link TvExtender} with default options.
9606 */
9607 public TvExtender() {
9608 mFlags = FLAG_AVAILABLE_ON_TV;
9609 }
9610
9611 /**
9612 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9613 *
9614 * @param notif The notification from which to copy options.
9615 */
9616 public TvExtender(Notification notif) {
9617 Bundle bundle = notif.extras == null ?
9618 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9619 if (bundle != null) {
9620 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009621 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009622 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009623 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9624 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9625 }
9626 }
9627
9628 /**
9629 * Apply a TV extension to a notification that is being built. This is typically called by
9630 * the {@link Notification.Builder#extend(Notification.Extender)}
9631 * method of {@link Notification.Builder}.
9632 */
9633 @Override
9634 public Notification.Builder extend(Notification.Builder builder) {
9635 Bundle bundle = new Bundle();
9636
9637 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009638 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009639 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009640 if (mContentIntent != null) {
9641 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9642 }
9643
9644 if (mDeleteIntent != null) {
9645 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9646 }
9647
9648 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9649 return builder;
9650 }
9651
9652 /**
9653 * Returns true if this notification should be shown on TV. This method return true
9654 * if the notification was extended with a TvExtender.
9655 */
9656 public boolean isAvailableOnTv() {
9657 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9658 }
9659
9660 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009661 * Specifies the channel the notification should be delivered on when shown on TV.
9662 * It can be different from the channel that the notification is delivered to when
9663 * posting on a non-TV device.
9664 */
9665 public TvExtender setChannel(String channelId) {
9666 mChannelId = channelId;
9667 return this;
9668 }
9669
9670 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009671 * Specifies the channel the notification should be delivered on when shown on TV.
9672 * It can be different from the channel that the notification is delivered to when
9673 * posting on a non-TV device.
9674 */
9675 public TvExtender setChannelId(String channelId) {
9676 mChannelId = channelId;
9677 return this;
9678 }
9679
Jeff Sharkey000ce802017-04-29 13:13:27 -06009680 /** @removed */
9681 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009682 public String getChannel() {
9683 return mChannelId;
9684 }
9685
9686 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009687 * Returns the id of the channel this notification posts to on TV.
9688 */
9689 public String getChannelId() {
9690 return mChannelId;
9691 }
9692
9693 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009694 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9695 * If provided, it is used instead of the content intent specified
9696 * at the level of Notification.
9697 */
9698 public TvExtender setContentIntent(PendingIntent intent) {
9699 mContentIntent = intent;
9700 return this;
9701 }
9702
9703 /**
9704 * Returns the TV-specific content intent. If this method returns null, the
9705 * main content intent on the notification should be used.
9706 *
9707 * @see {@link Notification#contentIntent}
9708 */
9709 public PendingIntent getContentIntent() {
9710 return mContentIntent;
9711 }
9712
9713 /**
9714 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9715 * by the user on TV. If provided, it is used instead of the delete intent specified
9716 * at the level of Notification.
9717 */
9718 public TvExtender setDeleteIntent(PendingIntent intent) {
9719 mDeleteIntent = intent;
9720 return this;
9721 }
9722
9723 /**
9724 * Returns the TV-specific delete intent. If this method returns null, the
9725 * main delete intent on the notification should be used.
9726 *
9727 * @see {@link Notification#deleteIntent}
9728 */
9729 public PendingIntent getDeleteIntent() {
9730 return mDeleteIntent;
9731 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009732
9733 /**
9734 * Specifies whether this notification should suppress showing a message over top of apps
9735 * outside of the launcher.
9736 */
9737 public TvExtender setSuppressShowOverApps(boolean suppress) {
9738 mSuppressShowOverApps = suppress;
9739 return this;
9740 }
9741
9742 /**
9743 * Returns true if this notification should not show messages over top of apps
9744 * outside of the launcher.
9745 */
9746 public boolean getSuppressShowOverApps() {
9747 return mSuppressShowOverApps;
9748 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009749 }
9750
9751 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009752 * Get an array of Notification objects from a parcelable array bundle field.
9753 * Update the bundle to have a typed array so fetches in the future don't need
9754 * to do an array copy.
9755 */
9756 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9757 Parcelable[] array = bundle.getParcelableArray(key);
9758 if (array instanceof Notification[] || array == null) {
9759 return (Notification[]) array;
9760 }
9761 Notification[] typedArray = Arrays.copyOf(array, array.length,
9762 Notification[].class);
9763 bundle.putParcelableArray(key, typedArray);
9764 return typedArray;
9765 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009766
9767 private static class BuilderRemoteViews extends RemoteViews {
9768 public BuilderRemoteViews(Parcel parcel) {
9769 super(parcel);
9770 }
9771
Kenny Guy77320062014-08-27 21:37:15 +01009772 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9773 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009774 }
9775
9776 @Override
9777 public BuilderRemoteViews clone() {
9778 Parcel p = Parcel.obtain();
9779 writeToParcel(p, 0);
9780 p.setDataPosition(0);
9781 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9782 p.recycle();
9783 return brv;
9784 }
9785 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009786
Selim Cinek384804b2018-04-18 14:31:07 +08009787 /**
9788 * A result object where information about the template that was created is saved.
9789 */
9790 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009791 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009792
9793 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009794 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009795 * with the right actions.
9796 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009797 public int getIconMarginEnd() {
9798 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009799 }
9800
Selim Cinek1c72fa02018-04-23 18:00:54 +08009801 public void setIconMarginEnd(int iconMarginEnd) {
9802 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009803 }
9804 }
9805
Adrian Roos70d7aa32017-01-11 15:39:06 -08009806 private static class StandardTemplateParams {
9807 boolean hasProgress = true;
9808 boolean ambient = false;
9809 CharSequence title;
9810 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009811 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009812 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009813 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009814 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009815
9816 final StandardTemplateParams reset() {
9817 hasProgress = true;
9818 ambient = false;
9819 title = null;
9820 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009821 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009822 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009823 return this;
9824 }
9825
9826 final StandardTemplateParams hasProgress(boolean hasProgress) {
9827 this.hasProgress = hasProgress;
9828 return this;
9829 }
9830
9831 final StandardTemplateParams title(CharSequence title) {
9832 this.title = title;
9833 return this;
9834 }
9835
9836 final StandardTemplateParams text(CharSequence text) {
9837 this.text = text;
9838 return this;
9839 }
9840
Selim Cinekafeed292017-12-12 17:32:44 -08009841 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9842 this.headerTextSecondary = text;
9843 return this;
9844 }
9845
Selim Cinek384804b2018-04-18 14:31:07 +08009846 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9847 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009848 return this;
9849 }
9850
Selim Cinek384804b2018-04-18 14:31:07 +08009851 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9852 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009853 return this;
9854 }
9855
Adrian Roos70d7aa32017-01-11 15:39:06 -08009856 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009857 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009858 this.ambient = ambient;
9859 return this;
9860 }
9861
9862 final StandardTemplateParams fillTextsFrom(Builder b) {
9863 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009864 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9865
9866 // Big text notifications should contain their content when viewed in ambient mode.
9867 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9868 if (!ambient || TextUtils.isEmpty(text)) {
9869 text = extras.getCharSequence(EXTRA_TEXT);
9870 }
9871 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009872 return this;
9873 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009874
9875 /**
9876 * Set the maximum lines of remote input history lines allowed.
9877 * @param maxRemoteInputHistory The number of lines.
9878 * @return The builder for method chaining.
9879 */
9880 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9881 this.maxRemoteInputHistory = maxRemoteInputHistory;
9882 return this;
9883 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885}