blob: 6262bac14aa0ea62b757d7c040eb4b7a993304c3 [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
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050092import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050093import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060094import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096/**
97 * A class that represents how a persistent notification is to be presented to
98 * the user using the {@link android.app.NotificationManager}.
99 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800100 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
101 * easier to construct Notifications.</p>
102 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700103 * <div class="special reference">
104 * <h3>Developer Guides</h3>
105 * <p>For a guide to creating notifications, read the
106 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
107 * developer guide.</p>
108 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
110public class Notification implements Parcelable
111{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400112 private static final String TAG = "Notification";
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400116 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400117 */
118 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
119 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
120 = "android.intent.category.NOTIFICATION_PREFERENCES";
121
122 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500123 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
124 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500126 */
127 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
128
129 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400130 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400131 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
132 * what settings should be shown in the target app.
133 */
134 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
135
136 /**
137 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400138 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
139 * that can be used to narrow down what settings should be shown in the target app.
140 */
141 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
142
143 /**
144 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
145 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
146 * that can be used to narrow down what settings should be shown in the target app.
147 */
148 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
149
150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * Use all default values (where applicable).
152 */
153 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /**
156 * Use the default notification sound. This will ignore any given
157 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 *
Chris Wren47c20a12014-06-18 17:27:29 -0400159 * <p>
160 * A notification that is noisy is more likely to be presented as a heads-up notification.
161 * </p>
162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 */
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int DEFAULT_SOUND = 1;
167
168 /**
169 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700171 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500172 *
Chris Wren47c20a12014-06-18 17:27:29 -0400173 * <p>
174 * A notification that vibrates is more likely to be presented as a heads-up notification.
175 * </p>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 */
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
183 * Use the default notification lights. This will ignore the
184 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
185 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188 */
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200193 * Maximum length of CharSequences accepted by Builder and friends.
194 *
195 * <p>
196 * Avoids spamming the system with overly large strings such as full e-mails.
197 */
198 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
199
200 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800201 * Maximum entries of reply text that are accepted by Builder and friends.
202 */
203 private static final int MAX_REPLY_HISTORY = 5;
204
Selim Cinekde4de0e2018-01-24 16:21:07 -0800205
206 /**
207 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
208 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
209 *
210 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
211 * sends messages.</p>
212 */
213 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
214
Adrian Roose458aa82015-12-08 16:17:19 -0800215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500218 * Default value: {@link System#currentTimeMillis() Now}.
219 *
220 * Choose a timestamp that will be most relevant to the user. For most finite events, this
221 * corresponds to the time the event happened (or will happen, in the case of events that have
222 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800223 * timestamped according to when the activity began.
224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * <ul>
228 * <li>Notification of a new chat message should be stamped when the message was received.</li>
229 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
230 * <li>Notification of a completed file download should be stamped when the download finished.</li>
231 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
232 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
233 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800234 * </ul>
235 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700236 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
237 * anymore by default and must be opted into by using
238 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public long when;
241
242 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700243 * The creation time of the notification
244 */
245 private long creationTime;
246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400249 *
250 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
Dan Sandler86647982015-05-13 23:41:13 -0400252 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700253 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public int icon;
255
256 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800257 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
258 * leave it at its default value of 0.
259 *
260 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700261 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800262 */
263 public int iconLevel;
264
265 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500266 * The number of events that this notification represents. For example, in a new mail
267 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800268 *
Julia Reynolds30331982017-04-27 10:12:50 -0400269 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500270 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
271 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
Julia Reynolds30331982017-04-27 10:12:50 -0400273 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
275 /**
276 * The intent to execute when the expanded status entry is clicked. If
277 * this is an activity, it must include the
278 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800279 * that you take care of task management as described in the
280 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800281 * Stack</a> document. In particular, make sure to read the notification section
282 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
283 * Notifications</a> for the correct ways to launch an application from a
284 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 */
286 public PendingIntent contentIntent;
287
288 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500289 * The intent to execute when the notification is explicitly dismissed by the user, either with
290 * the "Clear All" button or by swiping it away individually.
291 *
292 * This probably shouldn't be launching an activity since several of those will be sent
293 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent deleteIntent;
296
297 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700298 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800299 *
Chris Wren47c20a12014-06-18 17:27:29 -0400300 * <p>
301 * The system UI may choose to display a heads-up notification, instead of
302 * launching this intent, while the user is using the device.
303 * </p>
304 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800305 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400306 */
307 public PendingIntent fullScreenIntent;
308
309 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400310 * Text that summarizes this notification for accessibility services.
311 *
312 * As of the L release, this text is no longer shown on screen, but it is still useful to
313 * accessibility services (where it serves as an audible announcement of the notification's
314 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400315 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800316 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 */
318 public CharSequence tickerText;
319
320 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400321 * Formerly, a view showing the {@link #tickerText}.
322 *
323 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400325 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400327
328 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400329 * The view that will represent this notification in the notification list (which is pulled
330 * down from the status bar).
331 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500332 * As of N, this field may be null. The notification view is determined by the inputs
333 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public RemoteViews contentView;
338
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400339 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400340 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400341 * opportunity to show more detail. The system UI may choose to show this
342 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500344 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
346 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400347 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400348 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400349 public RemoteViews bigContentView;
350
Chris Wren8fd39ec2014-02-27 17:43:26 -0500351
352 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400353 * A medium-format version of {@link #contentView}, providing the Notification an
354 * opportunity to add action buttons to contentView. At its discretion, the system UI may
355 * choose to show this as a heads-up notification, which will pop up so the user can see
356 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500358 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400359 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
360 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400362 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500363 public RemoteViews headsUpContentView;
364
Julia Reynoldsfc640012018-02-21 12:25:27 -0500365 private boolean mUsesStandardHeader;
366
367 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
368 static {
369 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
370 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
371 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
372 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
373 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
378 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
380 }
381
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400382 /**
Dan Sandler86647982015-05-13 23:41:13 -0400383 * A large bitmap to be shown in the notification content area.
384 *
385 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 */
Dan Sandler86647982015-05-13 23:41:13 -0400387 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800388 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
390 /**
391 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400394 * A notification that is noisy is more likely to be presented as a heads-up notification.
395 * </p>
396 *
397 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500398 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500400 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500402 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 public Uri sound;
404
405 /**
406 * Use this constant as the value for audioStreamType to request that
407 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700408 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400409 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public static final int STREAM_DEFAULT = -1;
414
415 /**
416 * The audio stream type to use when playing the sound.
417 * Should be one of the STREAM_ constants from
418 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400419 *
420 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public int audioStreamType = STREAM_DEFAULT;
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400426 * The default value of {@link #audioAttributes}.
427 */
428 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
429 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
430 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
431 .build();
432
433 /**
434 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500435 *
436 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400437 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500438 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400439 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
440
441 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 * The pattern with which to vibrate.
443 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 * <p>
445 * To vibrate the default pattern, see {@link #defaults}.
446 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500451 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 public long[] vibrate;
453
454 /**
455 * The color of the led. The hardware will do its best approximation.
456 *
457 * @see #FLAG_SHOW_LIGHTS
458 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Tor Norbye80756e32015-03-02 09:39:27 -0800461 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500462 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public int ledARGB;
464
465 /**
466 * The number of milliseconds for the LED to be on while it's flashing.
467 * The hardware will do its best approximation.
468 *
469 * @see #FLAG_SHOW_LIGHTS
470 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500473 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 public int ledOnMS;
475
476 /**
477 * The number of milliseconds for the LED to be off while it's flashing.
478 * The hardware will do its best approximation.
479 *
480 * @see #FLAG_SHOW_LIGHTS
481 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 *
483 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500485 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public int ledOffMS;
487
488 /**
489 * Specifies which values should be taken from the defaults.
490 * <p>
491 * To set, OR the desired from {@link #DEFAULT_SOUND},
492 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
493 * values, use {@link #DEFAULT_ALL}.
494 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 *
496 * @deprecated use {@link NotificationChannel#getSound()} and
497 * {@link NotificationChannel#shouldShowLights()} and
498 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500500 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 public int defaults;
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
504 * Bit to be bitwise-ored into the {@link #flags} field that should be
505 * set if you want the LED on for this notification.
506 * <ul>
507 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
508 * or 0 for both ledOnMS and ledOffMS.</li>
509 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
510 * <li>To flash the LED, pass the number of milliseconds that it should
511 * be on and off to ledOnMS and ledOffMS.</li>
512 * </ul>
513 * <p>
514 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800515 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 * because they will be set to values that work on any given hardware.
517 * <p>
518 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500519 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500520 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500522 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
524
525 /**
526 * Bit to be bitwise-ored into the {@link #flags} field that should be
527 * set if this notification is in reference to something that is ongoing,
528 * like a phone call. It should not be set if this notification is in
529 * reference to something that happened at a particular point in time,
530 * like a missed phone call.
531 */
532 public static final int FLAG_ONGOING_EVENT = 0x00000002;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700536 * the audio will be repeated until the notification is
537 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 */
539 public static final int FLAG_INSISTENT = 0x00000004;
540
541 /**
542 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700543 * set if you would only like the sound, vibrate and ticker to be played
544 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 */
546 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
547
548 /**
549 * Bit to be bitwise-ored into the {@link #flags} field that should be
550 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500551 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public static final int FLAG_AUTO_CANCEL = 0x00000010;
554
555 /**
556 * Bit to be bitwise-ored into the {@link #flags} field that should be
557 * set if the notification should not be canceled when the user clicks
558 * the Clear all button.
559 */
560 public static final int FLAG_NO_CLEAR = 0x00000020;
561
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700562 /**
563 * Bit to be bitwise-ored into the {@link #flags} field that should be
564 * set if this notification represents a currently running service. This
565 * will normally be set for you by {@link Service#startForeground}.
566 */
567 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
568
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800571 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500572 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400573 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700574 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400576
Griff Hazendfcb0802014-02-11 12:00:00 -0800577 /**
578 * Bit to be bitswise-ored into the {@link #flags} field that should be
579 * set if this notification is relevant to the current device only
580 * and it is not recommended that it bridge to other devices.
581 */
582 public static final int FLAG_LOCAL_ONLY = 0x00000100;
583
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700584 /**
585 * Bit to be bitswise-ored into the {@link #flags} field that should be
586 * set if this notification is the group summary for a group of notifications.
587 * Grouped notifications may display in a cluster or stack on devices which
588 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
589 */
590 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
591
Julia Reynoldse46bb372016-03-17 11:05:58 -0400592 /**
593 * Bit to be bitswise-ored into the {@link #flags} field that should be
594 * set if this notification is the group summary for an auto-group of notifications.
595 *
596 * @hide
597 */
598 @SystemApi
599 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
600
Julia Reynolds4db59552017-06-30 13:34:01 -0400601 /**
602 * @hide
603 */
604 public static final int FLAG_CAN_COLORIZE = 0x00000800;
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public int flags;
607
Tor Norbyed9273d62013-05-30 15:59:53 -0700608 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700609 @IntDef(prefix = { "PRIORITY_" }, value = {
610 PRIORITY_DEFAULT,
611 PRIORITY_LOW,
612 PRIORITY_MIN,
613 PRIORITY_HIGH,
614 PRIORITY_MAX
615 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700616 @Retention(RetentionPolicy.SOURCE)
617 public @interface Priority {}
618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 * Default notification {@link #priority}. If your application does not prioritize its own
621 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 public static final int PRIORITY_DEFAULT = 0;
627
628 /**
629 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
630 * items smaller, or at a different position in the list, compared with your app's
631 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_LOW = -1;
637
638 /**
639 * Lowest {@link #priority}; these items might not be shown to the user except under special
640 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} 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_MIN = -2;
646
647 /**
648 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
649 * show these items larger, or at a different position in notification lists, compared with
650 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 *
652 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public static final int PRIORITY_HIGH = 1;
656
657 /**
658 * Highest {@link #priority}, for your application's most important items that require the
659 * user's prompt attention or input.
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_MAX = 2;
665
666 /**
667 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800668 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 * Priority is an indication of how much of the user's valuable attention should be consumed by
670 * this notification. Low-priority notifications may be hidden from the user in certain
671 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500672 * system will make a determination about how to interpret this priority when presenting
673 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400674 *
675 * <p>
676 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
677 * as a heads-up notification.
678 * </p>
679 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500680 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500681 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700682 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500683 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500684 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800685
Dan Sandler26e81cf2014-05-06 10:01:27 -0400686 /**
687 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
688 * to be applied by the standard Style templates when presenting this notification.
689 *
690 * The current template design constructs a colorful header image by overlaying the
691 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
692 * ignored.
693 */
Tor Norbye80756e32015-03-02 09:39:27 -0800694 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 public int color = COLOR_DEFAULT;
696
697 /**
698 * Special value of {@link #color} telling the system not to decorate this notification with
699 * any special color but instead use default colors when presenting this notification.
700 */
Tor Norbye80756e32015-03-02 09:39:27 -0800701 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400702 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600703
704 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800705 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800706 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800707 */
708 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800709 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800710
711 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700712 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
713 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600714 * lockscreen).
715 *
716 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
717 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
718 * shown in all situations, but the contents are only available if the device is unlocked for
719 * the appropriate user.
720 *
721 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
722 * can be read even in an "insecure" context (that is, above a secure lockscreen).
723 * To modify the public version of this notification—for example, to redact some portions—see
724 * {@link Builder#setPublicVersion(Notification)}.
725 *
726 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
727 * and ticker until the user has bypassed the lockscreen.
728 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600729 public @Visibility int visibility;
730
731 /** @hide */
732 @IntDef(prefix = { "VISIBILITY_" }, value = {
733 VISIBILITY_PUBLIC,
734 VISIBILITY_PRIVATE,
735 VISIBILITY_SECRET,
736 })
737 @Retention(RetentionPolicy.SOURCE)
738 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600739
Griff Hazenfc3922d2014-08-20 11:56:44 -0700740 /**
741 * Notification visibility: Show this notification in its entirety on all lockscreens.
742 *
743 * {@see #visibility}
744 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600745 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700746
747 /**
748 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
749 * private information on secure lockscreens.
750 *
751 * {@see #visibility}
752 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600753 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700754
755 /**
756 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
757 *
758 * {@see #visibility}
759 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600760 public static final int VISIBILITY_SECRET = -1;
761
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400763 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500764 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400765 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500766
767 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700768 * Notification category: map turn-by-turn navigation.
769 */
770 public static final String CATEGORY_NAVIGATION = "navigation";
771
772 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776
777 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400778 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500779 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_ALARM = "alarm";
796
797 /**
798 * Notification category: progress of a long-running background operation.
799 */
800 public static final String CATEGORY_PROGRESS = "progress";
801
802 /**
803 * Notification category: social network or sharing update.
804 */
805 public static final String CATEGORY_SOCIAL = "social";
806
807 /**
808 * Notification category: error in background operation or authentication status.
809 */
810 public static final String CATEGORY_ERROR = "err";
811
812 /**
813 * Notification category: media transport control for playback.
814 */
815 public static final String CATEGORY_TRANSPORT = "transport";
816
817 /**
818 * Notification category: system or device status update. Reserved for system use.
819 */
820 public static final String CATEGORY_SYSTEM = "sys";
821
822 /**
823 * Notification category: indication of running background service.
824 */
825 public static final String CATEGORY_SERVICE = "service";
826
827 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400828 * Notification category: a specific, timely recommendation for a single thing.
829 * For example, a news app might want to recommend a news story it believes the user will
830 * want to read next.
831 */
832 public static final String CATEGORY_RECOMMENDATION = "recommendation";
833
834 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400835 * Notification category: ongoing information about device or contextual status.
836 */
837 public static final String CATEGORY_STATUS = "status";
838
839 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400840 * Notification category: user-scheduled reminder.
841 */
842 public static final String CATEGORY_REMINDER = "reminder";
843
844 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700845 * Notification category: extreme car emergencies.
846 * @hide
847 */
848 @SystemApi
849 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
850
851 /**
852 * Notification category: car warnings.
853 * @hide
854 */
855 @SystemApi
856 public static final String CATEGORY_CAR_WARNING = "car_warning";
857
858 /**
859 * Notification category: general car system information.
860 * @hide
861 */
862 @SystemApi
863 public static final String CATEGORY_CAR_INFORMATION = "car_information";
864
865 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400866 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
867 * that best describes this Notification. May be used by the system for ranking and filtering.
868 */
869 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500870
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700871 private String mGroupKey;
872
873 /**
874 * Get the key used to group this notification into a cluster or stack
875 * with other notifications on devices which support such rendering.
876 */
877 public String getGroup() {
878 return mGroupKey;
879 }
880
881 private String mSortKey;
882
883 /**
884 * Get a sort key that orders this notification among other notifications from the
885 * same package. This can be useful if an external sort was already applied and an app
886 * would like to preserve this. Notifications will be sorted lexicographically using this
887 * value, although providing different priorities in addition to providing sort key may
888 * cause this value to be ignored.
889 *
890 * <p>This sort key can also be used to order members of a notification group. See
891 * {@link Builder#setGroup}.
892 *
893 * @see String#compareTo(String)
894 */
895 public String getSortKey() {
896 return mSortKey;
897 }
898
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500899 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400900 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901 * <p>
902 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
903 * APIs, and are intended to be used by
904 * {@link android.service.notification.NotificationListenerService} implementations to extract
905 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500906 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400907 public Bundle extras = new Bundle();
908
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400909 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700910 * All pending intents in the notification as the system needs to be able to access them but
911 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700912 * custom parcelable objects.
913 *
914 * @hide
915 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700916 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700917
918 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700919 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
920 * pending intents inside of it, so only those will get the behavior.
921 *
922 * @hide
923 */
Adrian Roosfb921842017-10-26 14:49:56 +0200924 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700925
926 /**
927 * Must be set by a process to start associating tokens with Notification objects
928 * coming in to it. This is set by NotificationManagerService.
929 *
930 * @hide
931 */
932 static public IBinder processWhitelistToken;
933
934 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935 * {@link #extras} key: this is the title of the notification,
936 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
937 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500938 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400939
940 /**
941 * {@link #extras} key: this is the title of the notification when shown in expanded form,
942 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
943 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400944 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400945
946 /**
947 * {@link #extras} key: this is the main text payload, as supplied to
948 * {@link Builder#setContentText(CharSequence)}.
949 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500950 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is a third line of text, as supplied to
954 * {@link Builder#setSubText(CharSequence)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800959 * {@link #extras} key: this is the remote input history, as supplied to
960 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700961 *
962 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
963 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
964 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
965 * notifications once the other party has responded).
966 *
967 * The extra with this key is of type CharSequence[] and contains the most recent entry at
968 * the 0 index, the second most recent at the 1 index, etc.
969 *
970 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800971 */
972 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
973
974 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400975 * {@link #extras} key: this is a small piece of additional text as supplied to
976 * {@link Builder#setContentInfo(CharSequence)}.
977 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400978 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400979
980 /**
981 * {@link #extras} key: this is a line of summary information intended to be shown
982 * alongside expanded notifications, as supplied to (e.g.)
983 * {@link BigTextStyle#setSummaryText(CharSequence)}.
984 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400985 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400986
987 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200988 * {@link #extras} key: this is the longer text shown in the big form of a
989 * {@link BigTextStyle} notification, as supplied to
990 * {@link BigTextStyle#bigText(CharSequence)}.
991 */
992 public static final String EXTRA_BIG_TEXT = "android.bigText";
993
994 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
996 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400997 *
998 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001000 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001001 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001002
1003 /**
1004 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1005 * notification payload, as
1006 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001007 *
1008 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001010 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001011 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012
1013 /**
1014 * {@link #extras} key: this is a bitmap to be used instead of the one from
1015 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1016 * shown in its expanded form, as supplied to
1017 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001020
1021 /**
1022 * {@link #extras} key: this is the progress value supplied to
1023 * {@link Builder#setProgress(int, int, boolean)}.
1024 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001025 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001026
1027 /**
1028 * {@link #extras} key: this is the maximum value supplied to
1029 * {@link Builder#setProgress(int, int, boolean)}.
1030 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001031 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032
1033 /**
1034 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1035 * {@link Builder#setProgress(int, int, boolean)}.
1036 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001037 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001038
1039 /**
1040 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1041 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1042 * {@link Builder#setUsesChronometer(boolean)}.
1043 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001044 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001045
1046 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001047 * {@link #extras} key: whether the chronometer set on the notification should count down
1048 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001049 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001050 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001051 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001052
1053 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054 * {@link #extras} key: whether {@link #when} should be shown,
1055 * as supplied to {@link Builder#setShowWhen(boolean)}.
1056 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001057 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001058
1059 /**
1060 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1061 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1062 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001063 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001064
1065 /**
1066 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1067 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1068 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001069 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001070
1071 /**
1072 * {@link #extras} key: A string representing the name of the specific
1073 * {@link android.app.Notification.Style} used to create this notification.
1074 */
Chris Wren91ad5632013-06-05 15:05:57 -04001075 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001076
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001077 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001078 * {@link #extras} key: A String array containing the people that this notification relates to,
1079 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001080 *
1081 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001082 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001083 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001084
1085 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001086 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1087 * this notification relates to.
1088 */
1089 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1090
1091 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001092 * Allow certain system-generated notifications to appear before the device is provisioned.
1093 * Only available to notifications coming from the android package.
1094 * @hide
1095 */
Maurice Lam96c10032017-03-29 15:42:38 -07001096 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001097 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001098 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1099
1100 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001101 * {@link #extras} key: A
1102 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001103 * in the background when the notification is selected. Used on television platforms.
1104 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001105 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001106 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001107 */
1108 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1109
1110 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001111 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001112 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001113 * {@link android.app.Notification.MediaStyle} notification.
1114 */
1115 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1116
1117 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001118 * {@link #extras} key: the indices of actions to be shown in the compact view,
1119 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1120 */
1121 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1122
Christoph Studer943aa672014-08-03 20:31:16 +02001123 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001124 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1125 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001126 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1127 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001128 *
1129 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001130 */
1131 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1132
1133 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001134 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1135 * direct replies
1136 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1137 * {@link Person}
1138 */
1139 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1140
1141 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001142 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001143 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001144 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001145 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001146
1147 /**
1148 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1149 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001150 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1151 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001152 */
1153 public static final String EXTRA_MESSAGES = "android.messages";
1154
1155 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001156 * {@link #extras} key: an array of
1157 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1158 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1159 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1160 * array of bundles.
1161 */
1162 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1163
1164 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001165 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1166 * represents a group conversation.
1167 */
1168 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1169
1170 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001171 * {@link #extras} key: whether the notification should be colorized as
1172 * supplied to {@link Builder#setColorized(boolean)}}.
1173 */
1174 public static final String EXTRA_COLORIZED = "android.colorized";
1175
1176 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001177 * @hide
1178 */
1179 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1180
Selim Cinek247fa012016-02-18 09:50:48 -08001181 /**
1182 * @hide
1183 */
1184 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1185
Shane Brennan472a3b32016-12-12 15:28:10 -08001186 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001187 * @hide
1188 */
1189 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1190
1191 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001192 * {@link #extras} key: the audio contents of this notification.
1193 *
1194 * This is for use when rendering the notification on an audio-focused interface;
1195 * the audio contents are a complete sound sample that contains the contents/body of the
1196 * notification. This may be used in substitute of a Text-to-Speech reading of the
1197 * notification. For example if the notification represents a voice message this should point
1198 * to the audio of that message.
1199 *
1200 * The data stored under this key should be a String representation of a Uri that contains the
1201 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1202 *
1203 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1204 * has a field for holding data URI. That field can be used for audio.
1205 * See {@code Message#setData}.
1206 *
1207 * Example usage:
1208 * <pre>
1209 * {@code
1210 * Notification.Builder myBuilder = (build your Notification as normal);
1211 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1212 * }
1213 * </pre>
1214 */
1215 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1216
Dan Sandler80eaa592016-04-14 23:34:54 -04001217 /** @hide */
1218 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001219 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001220 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1221
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001222 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001223 * This is set on the notifications shown by system_server about apps running foreground
1224 * services. It indicates that the notification should be shown
1225 * only if any of the given apps do not already have a properly tagged
1226 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1227 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001228 * @hide
1229 */
1230 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1231
Dan Sandlerd63f9322015-05-06 15:18:49 -04001232 private Icon mSmallIcon;
1233 private Icon mLargeIcon;
1234
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001235 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001236 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001237
Julia Reynolds13d898c2017-02-02 12:22:05 -05001238 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001239 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001240
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001241 /** @hide */
1242 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1243 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1244 })
1245 @Retention(RetentionPolicy.SOURCE)
1246 public @interface GroupAlertBehavior {}
1247
1248 /**
1249 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1250 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1251 * notification will not be muted when it is in a group.
1252 */
1253 public static final int GROUP_ALERT_ALL = 0;
1254
1255 /**
1256 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1257 * notification in a group should be silenced (no sound or vibration) even if they are posted
1258 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001259 * mute this notification if this notification is a group child. This must be applied to all
1260 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001261 *
1262 * <p> For example, you might want to use this constant if you post a number of children
1263 * notifications at once (say, after a periodic sync), and only need to notify the user
1264 * audibly once.
1265 */
1266 public static final int GROUP_ALERT_SUMMARY = 1;
1267
1268 /**
1269 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1270 * notification in a group should be silenced (no sound or vibration) even if they are
1271 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1272 * to mute this notification if this notification is a group summary.
1273 *
1274 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001275 * in your group have content and the summary is only used to visually group notifications
1276 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001277 */
1278 public static final int GROUP_ALERT_CHILDREN = 2;
1279
Julia Reynolds2f431e22017-06-07 14:12:09 +00001280 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001281
Julia Reynolds13d898c2017-02-02 12:22:05 -05001282 /**
1283 * If this notification is being shown as a badge, always show as a number.
1284 */
1285 public static final int BADGE_ICON_NONE = 0;
1286
1287 /**
1288 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1289 * represent this notification.
1290 */
1291 public static final int BADGE_ICON_SMALL = 1;
1292
1293 /**
1294 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1295 * represent this notification.
1296 */
1297 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001298 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001299
Chris Wren51c75102013-07-16 20:49:17 -04001300 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001301 * Structure to encapsulate a named action that can be shown as part of this notification.
1302 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1303 * selected by the user.
1304 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001305 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1306 * or {@link Notification.Builder#addAction(Notification.Action)}
1307 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001308 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001309 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001310 /**
1311 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1312 * {@link RemoteInput}s.
1313 *
1314 * This is intended for {@link RemoteInput}s that only accept data, meaning
1315 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1316 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1317 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1318 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1319 *
1320 * You can test if a RemoteInput matches these constraints using
1321 * {@link RemoteInput#isDataOnly}.
1322 */
1323 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1324
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001325 /**
1326 * {@link }: No semantic action defined.
1327 */
1328 public static final int SEMANTIC_ACTION_NONE = 0;
1329
1330 /**
1331 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1332 * may be appropriate.
1333 */
1334 public static final int SEMANTIC_ACTION_REPLY = 1;
1335
1336 /**
1337 * {@code SemanticAction}: Mark content as read.
1338 */
1339 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1340
1341 /**
1342 * {@code SemanticAction}: Mark content as unread.
1343 */
1344 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1345
1346 /**
1347 * {@code SemanticAction}: Delete the content associated with the notification. This
1348 * could mean deleting an email, message, etc.
1349 */
1350 public static final int SEMANTIC_ACTION_DELETE = 4;
1351
1352 /**
1353 * {@code SemanticAction}: Archive the content associated with the notification. This
1354 * could mean archiving an email, message, etc.
1355 */
1356 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1357
1358 /**
1359 * {@code SemanticAction}: Mute the content associated with the notification. This could
1360 * mean silencing a conversation or currently playing media.
1361 */
1362 public static final int SEMANTIC_ACTION_MUTE = 6;
1363
1364 /**
1365 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1366 * mean un-silencing a conversation or currently playing media.
1367 */
1368 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1369
1370 /**
1371 * {@code SemanticAction}: Mark content with a thumbs up.
1372 */
1373 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1374
1375 /**
1376 * {@code SemanticAction}: Mark content with a thumbs down.
1377 */
1378 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1379
Kodlee Yincda5b092018-02-15 15:34:53 -08001380 /**
1381 * {@code SemanticAction}: Call a contact, group, etc.
1382 */
1383 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001384
Griff Hazen959591e2014-05-15 22:26:18 -07001385 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001386 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001387 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001388 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001389 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001390
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001391 /**
1392 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001393 *
1394 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001395 */
Dan Sandler86647982015-05-13 23:41:13 -04001396 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001397 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001398
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001399 /**
1400 * Title of the action.
1401 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001402 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001403
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001404 /**
1405 * Intent to send when the user invokes this action. May be null, in which case the action
1406 * may be rendered in a disabled presentation by the system UI.
1407 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001408 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001409
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001410 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001411 if (in.readInt() != 0) {
1412 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001413 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1414 icon = mIcon.getResId();
1415 }
Dan Sandler86647982015-05-13 23:41:13 -04001416 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001417 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1418 if (in.readInt() == 1) {
1419 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1420 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001421 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001422 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001423 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001424 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001425 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001426
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001427 /**
Dan Sandler86647982015-05-13 23:41:13 -04001428 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001429 */
Dan Sandler86647982015-05-13 23:41:13 -04001430 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001431 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001432 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1433 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001434 }
1435
Adrian Roos7af53622016-10-12 13:44:05 -07001436 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001437 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001438 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1439 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001440 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001441 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1442 this.icon = icon.getResId();
1443 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001444 this.title = title;
1445 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001446 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001447 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001448 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001449 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001450 }
1451
1452 /**
Dan Sandler86647982015-05-13 23:41:13 -04001453 * Return an icon representing the action.
1454 */
1455 public Icon getIcon() {
1456 if (mIcon == null && icon != 0) {
1457 // you snuck an icon in here without using the builder; let's try to keep it
1458 mIcon = Icon.createWithResource("", icon);
1459 }
1460 return mIcon;
1461 }
1462
1463 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001464 * Get additional metadata carried around with this Action.
1465 */
1466 public Bundle getExtras() {
1467 return mExtras;
1468 }
1469
1470 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001471 * Return whether the platform should automatically generate possible replies for this
1472 * {@link Action}
1473 */
1474 public boolean getAllowGeneratedReplies() {
1475 return mAllowGeneratedReplies;
1476 }
1477
1478 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001479 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001480 * May return null if no remote inputs were added. Only returns inputs which accept
1481 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001482 */
1483 public RemoteInput[] getRemoteInputs() {
1484 return mRemoteInputs;
1485 }
1486
1487 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001488 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1489 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1490 * (eg. reply, mark as read, delete, etc).
1491 */
1492 public @SemanticAction int getSemanticAction() {
1493 return mSemanticAction;
1494 }
1495
1496 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001497 * Get the list of inputs to be collected from the user that ONLY accept data when this
1498 * action is sent. These remote inputs are guaranteed to return true on a call to
1499 * {@link RemoteInput#isDataOnly}.
1500 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001501 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001502 *
1503 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1504 * of non-textual RemoteInputs do not access these remote inputs.
1505 */
1506 public RemoteInput[] getDataOnlyRemoteInputs() {
1507 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1508 }
1509
1510 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001511 * Builder class for {@link Action} objects.
1512 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001513 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001514 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001515 private final CharSequence mTitle;
1516 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001517 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001518 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001520 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001521
1522 /**
1523 * Construct a new builder for {@link Action} object.
1524 * @param icon icon to show for this action
1525 * @param title the title of the action
1526 * @param intent the {@link PendingIntent} to fire when users trigger this action
1527 */
Dan Sandler86647982015-05-13 23:41:13 -04001528 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001529 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001530 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001531 }
1532
1533 /**
1534 * Construct a new builder for {@link Action} object.
1535 * @param icon icon to show for this action
1536 * @param title the title of the action
1537 * @param intent the {@link PendingIntent} to fire when users trigger this action
1538 */
1539 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001540 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001541 }
1542
1543 /**
1544 * Construct a new builder for {@link Action} object using the fields from an
1545 * {@link Action}.
1546 * @param action the action to read fields from.
1547 */
1548 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001549 this(action.getIcon(), action.title, action.actionIntent,
1550 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001551 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001552 }
1553
Dan Sandler86647982015-05-13 23:41:13 -04001554 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001555 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1556 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001557 mIcon = icon;
1558 mTitle = title;
1559 mIntent = intent;
1560 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001561 if (remoteInputs != null) {
1562 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1563 Collections.addAll(mRemoteInputs, remoteInputs);
1564 }
Adrian Roos7af53622016-10-12 13:44:05 -07001565 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001566 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001567 }
1568
1569 /**
1570 * Merge additional metadata into this builder.
1571 *
1572 * <p>Values within the Bundle will replace existing extras values in this Builder.
1573 *
1574 * @see Notification.Action#extras
1575 */
1576 public Builder addExtras(Bundle extras) {
1577 if (extras != null) {
1578 mExtras.putAll(extras);
1579 }
1580 return this;
1581 }
1582
1583 /**
1584 * Get the metadata Bundle used by this Builder.
1585 *
1586 * <p>The returned Bundle is shared with this Builder.
1587 */
1588 public Bundle getExtras() {
1589 return mExtras;
1590 }
1591
1592 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001593 * Add an input to be collected from the user when this action is sent.
1594 * Response values can be retrieved from the fired intent by using the
1595 * {@link RemoteInput#getResultsFromIntent} function.
1596 * @param remoteInput a {@link RemoteInput} to add to the action
1597 * @return this object for method chaining
1598 */
1599 public Builder addRemoteInput(RemoteInput remoteInput) {
1600 if (mRemoteInputs == null) {
1601 mRemoteInputs = new ArrayList<RemoteInput>();
1602 }
1603 mRemoteInputs.add(remoteInput);
1604 return this;
1605 }
1606
1607 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001608 * Set whether the platform should automatically generate possible replies to add to
1609 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1610 * {@link RemoteInput}, this has no effect.
1611 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1612 * otherwise
1613 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001614 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001615 */
1616 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1617 mAllowGeneratedReplies = allowGeneratedReplies;
1618 return this;
1619 }
1620
1621 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001622 * Sets the {@code SemanticAction} for this {@link Action}. A
1623 * {@code SemanticAction} denotes what an {@link Action}'s
1624 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1625 * @param semanticAction a SemanticAction defined within {@link Action} with
1626 * {@code SEMANTIC_ACTION_} prefixes
1627 * @return this object for method chaining
1628 */
1629 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1630 mSemanticAction = semanticAction;
1631 return this;
1632 }
1633
1634 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001635 * Apply an extender to this action builder. Extenders may be used to add
1636 * metadata or change options on this builder.
1637 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001638 public Builder extend(Extender extender) {
1639 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001640 return this;
1641 }
1642
1643 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001644 * Combine all of the options that have been set and return a new {@link Action}
1645 * object.
1646 * @return the built action
1647 */
1648 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001649 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1650 RemoteInput[] previousDataInputs =
1651 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001652 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001653 for (RemoteInput input : previousDataInputs) {
1654 dataOnlyInputs.add(input);
1655 }
1656 }
1657 List<RemoteInput> textInputs = new ArrayList<>();
1658 if (mRemoteInputs != null) {
1659 for (RemoteInput input : mRemoteInputs) {
1660 if (input.isDataOnly()) {
1661 dataOnlyInputs.add(input);
1662 } else {
1663 textInputs.add(input);
1664 }
1665 }
1666 }
1667 if (!dataOnlyInputs.isEmpty()) {
1668 RemoteInput[] dataInputsArr =
1669 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1670 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1671 }
1672 RemoteInput[] textInputsArr = textInputs.isEmpty()
1673 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1674 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001675 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001676 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001677 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001678
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001679 @Override
1680 public Action clone() {
1681 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001682 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001683 title,
1684 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001685 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001686 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001687 getAllowGeneratedReplies(),
1688 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001689 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001690
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001691 @Override
1692 public int describeContents() {
1693 return 0;
1694 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001695
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001696 @Override
1697 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001698 final Icon ic = getIcon();
1699 if (ic != null) {
1700 out.writeInt(1);
1701 ic.writeToParcel(out, 0);
1702 } else {
1703 out.writeInt(0);
1704 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001705 TextUtils.writeToParcel(title, out, flags);
1706 if (actionIntent != null) {
1707 out.writeInt(1);
1708 actionIntent.writeToParcel(out, flags);
1709 } else {
1710 out.writeInt(0);
1711 }
Griff Hazen959591e2014-05-15 22:26:18 -07001712 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001713 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001714 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001715 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001716 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001717
Griff Hazen959591e2014-05-15 22:26:18 -07001718 public static final Parcelable.Creator<Action> CREATOR =
1719 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001720 public Action createFromParcel(Parcel in) {
1721 return new Action(in);
1722 }
1723 public Action[] newArray(int size) {
1724 return new Action[size];
1725 }
1726 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001727
1728 /**
1729 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1730 * metadata or change options on an action builder.
1731 */
1732 public interface Extender {
1733 /**
1734 * Apply this extender to a notification action builder.
1735 * @param builder the builder to be modified.
1736 * @return the build object for chaining.
1737 */
1738 public Builder extend(Builder builder);
1739 }
1740
1741 /**
1742 * Wearable extender for notification actions. To add extensions to an action,
1743 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1744 * the {@code WearableExtender()} constructor and apply it to a
1745 * {@link android.app.Notification.Action.Builder} using
1746 * {@link android.app.Notification.Action.Builder#extend}.
1747 *
1748 * <pre class="prettyprint">
1749 * Notification.Action action = new Notification.Action.Builder(
1750 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001751 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001752 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001753 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001754 */
1755 public static final class WearableExtender implements Extender {
1756 /** Notification action extra which contains wearable extensions */
1757 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1758
Pete Gastaf6781d2014-10-07 15:17:05 -04001759 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001760 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001761 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1762 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1763 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001764
1765 // Flags bitwise-ored to mFlags
1766 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001767 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001768 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001769
1770 // Default value for flags integer
1771 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1772
1773 private int mFlags = DEFAULT_FLAGS;
1774
Pete Gastaf6781d2014-10-07 15:17:05 -04001775 private CharSequence mInProgressLabel;
1776 private CharSequence mConfirmLabel;
1777 private CharSequence mCancelLabel;
1778
Griff Hazen61a9e862014-05-22 16:05:19 -07001779 /**
1780 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1781 * options.
1782 */
1783 public WearableExtender() {
1784 }
1785
1786 /**
1787 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1788 * wearable options present in an existing notification action.
1789 * @param action the notification action to inspect.
1790 */
1791 public WearableExtender(Action action) {
1792 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1793 if (wearableBundle != null) {
1794 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001795 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1796 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1797 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001798 }
1799 }
1800
1801 /**
1802 * Apply wearable extensions to a notification action that is being built. This is
1803 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1804 * method of {@link android.app.Notification.Action.Builder}.
1805 */
1806 @Override
1807 public Action.Builder extend(Action.Builder builder) {
1808 Bundle wearableBundle = new Bundle();
1809
1810 if (mFlags != DEFAULT_FLAGS) {
1811 wearableBundle.putInt(KEY_FLAGS, mFlags);
1812 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001813 if (mInProgressLabel != null) {
1814 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1815 }
1816 if (mConfirmLabel != null) {
1817 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1818 }
1819 if (mCancelLabel != null) {
1820 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1821 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001822
1823 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1824 return builder;
1825 }
1826
1827 @Override
1828 public WearableExtender clone() {
1829 WearableExtender that = new WearableExtender();
1830 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001831 that.mInProgressLabel = this.mInProgressLabel;
1832 that.mConfirmLabel = this.mConfirmLabel;
1833 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001834 return that;
1835 }
1836
1837 /**
1838 * Set whether this action is available when the wearable device is not connected to
1839 * a companion device. The user can still trigger this action when the wearable device is
1840 * offline, but a visual hint will indicate that the action may not be available.
1841 * Defaults to true.
1842 */
1843 public WearableExtender setAvailableOffline(boolean availableOffline) {
1844 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1845 return this;
1846 }
1847
1848 /**
1849 * Get whether this action is available when the wearable device is not connected to
1850 * a companion device. The user can still trigger this action when the wearable device is
1851 * offline, but a visual hint will indicate that the action may not be available.
1852 * Defaults to true.
1853 */
1854 public boolean isAvailableOffline() {
1855 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1856 }
1857
1858 private void setFlag(int mask, boolean value) {
1859 if (value) {
1860 mFlags |= mask;
1861 } else {
1862 mFlags &= ~mask;
1863 }
1864 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001865
1866 /**
1867 * Set a label to display while the wearable is preparing to automatically execute the
1868 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1869 *
1870 * @param label the label to display while the action is being prepared to execute
1871 * @return this object for method chaining
1872 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001873 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001874 public WearableExtender setInProgressLabel(CharSequence label) {
1875 mInProgressLabel = label;
1876 return this;
1877 }
1878
1879 /**
1880 * Get the label to display while the wearable is preparing to automatically execute
1881 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1882 *
1883 * @return the label to display while the action is being prepared to execute
1884 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001885 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001886 public CharSequence getInProgressLabel() {
1887 return mInProgressLabel;
1888 }
1889
1890 /**
1891 * Set a label to display to confirm that the action should be executed.
1892 * This is usually an imperative verb like "Send".
1893 *
1894 * @param label the label to confirm the action should be executed
1895 * @return this object for method chaining
1896 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001897 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001898 public WearableExtender setConfirmLabel(CharSequence label) {
1899 mConfirmLabel = label;
1900 return this;
1901 }
1902
1903 /**
1904 * Get the label to display to confirm that the action should be executed.
1905 * This is usually an imperative verb like "Send".
1906 *
1907 * @return the label to confirm the action should be executed
1908 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001909 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001910 public CharSequence getConfirmLabel() {
1911 return mConfirmLabel;
1912 }
1913
1914 /**
1915 * Set a label to display to cancel the action.
1916 * This is usually an imperative verb, like "Cancel".
1917 *
1918 * @param label the label to display to cancel the action
1919 * @return this object for method chaining
1920 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001921 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001922 public WearableExtender setCancelLabel(CharSequence label) {
1923 mCancelLabel = label;
1924 return this;
1925 }
1926
1927 /**
1928 * Get the label to display to cancel the action.
1929 * This is usually an imperative verb like "Cancel".
1930 *
1931 * @return the label to display to cancel the action
1932 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001933 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001934 public CharSequence getCancelLabel() {
1935 return mCancelLabel;
1936 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001937
1938 /**
1939 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1940 * platform that it can generate the appropriate transitions.
1941 * @param hintLaunchesActivity {@code true} if the content intent will launch
1942 * an activity and transitions should be generated, false otherwise.
1943 * @return this object for method chaining
1944 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001945 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001946 boolean hintLaunchesActivity) {
1947 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1948 return this;
1949 }
1950
1951 /**
1952 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1953 * platform that it can generate the appropriate transitions
1954 * @return {@code true} if the content intent will launch an activity and transitions
1955 * should be generated, false otherwise. The default value is {@code false} if this was
1956 * never set.
1957 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001958 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001959 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1960 }
Alex Hills9f087612016-06-07 09:08:59 -04001961
1962 /**
1963 * Set a hint that this Action should be displayed inline.
1964 *
1965 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1966 * otherwise
1967 * @return this object for method chaining
1968 */
1969 public WearableExtender setHintDisplayActionInline(
1970 boolean hintDisplayInline) {
1971 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1972 return this;
1973 }
1974
1975 /**
1976 * Get a hint that this Action should be displayed inline.
1977 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001978 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001979 * otherwise. The default value is {@code false} if this was never set.
1980 */
1981 public boolean getHintDisplayActionInline() {
1982 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1983 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001984 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001985
1986 /**
1987 * Provides meaning to an {@link Action} that hints at what the associated
1988 * {@link PendingIntent} will do. For example, an {@link Action} with a
1989 * {@link PendingIntent} that replies to a text message notification may have the
1990 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1991 *
1992 * @hide
1993 */
1994 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1995 SEMANTIC_ACTION_NONE,
1996 SEMANTIC_ACTION_REPLY,
1997 SEMANTIC_ACTION_MARK_AS_READ,
1998 SEMANTIC_ACTION_MARK_AS_UNREAD,
1999 SEMANTIC_ACTION_DELETE,
2000 SEMANTIC_ACTION_ARCHIVE,
2001 SEMANTIC_ACTION_MUTE,
2002 SEMANTIC_ACTION_UNMUTE,
2003 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002004 SEMANTIC_ACTION_THUMBS_DOWN,
2005 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002006 })
2007 @Retention(RetentionPolicy.SOURCE)
2008 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002009 }
2010
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002011 /**
2012 * Array of all {@link Action} structures attached to this notification by
2013 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2014 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2015 * interface for invoking actions.
2016 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002017 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002018
2019 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002020 * Replacement version of this notification whose content will be shown
2021 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2022 * and {@link #VISIBILITY_PUBLIC}.
2023 */
2024 public Notification publicVersion;
2025
2026 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002027 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002028 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 */
2030 public Notification()
2031 {
2032 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002033 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002034 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036
2037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 * @hide
2039 */
2040 public Notification(Context context, int icon, CharSequence tickerText, long when,
2041 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2042 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002043 new Builder(context)
2044 .setWhen(when)
2045 .setSmallIcon(icon)
2046 .setTicker(tickerText)
2047 .setContentTitle(contentTitle)
2048 .setContentText(contentText)
2049 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2050 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052
2053 /**
2054 * Constructs a Notification object with the information needed to
2055 * have a status bar icon without the standard expanded view.
2056 *
2057 * @param icon The resource id of the icon to put in the status bar.
2058 * @param tickerText The text that flows by in the status bar when the notification first
2059 * activates.
2060 * @param when The time to show in the time field. In the System.currentTimeMillis
2061 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002062 *
2063 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002065 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 public Notification(int icon, CharSequence tickerText, long when)
2067 {
2068 this.icon = icon;
2069 this.tickerText = tickerText;
2070 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002071 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073
2074 /**
2075 * Unflatten the notification from a parcel.
2076 */
Svet Ganovddb94882016-06-23 19:55:24 -07002077 @SuppressWarnings("unchecked")
2078 public Notification(Parcel parcel) {
2079 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2080 // intents in extras are always written as the last entry.
2081 readFromParcelImpl(parcel);
2082 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002083 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002084 }
2085
2086 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 {
2088 int version = parcel.readInt();
2089
Adrian Roosfb921842017-10-26 14:49:56 +02002090 mWhitelistToken = parcel.readStrongBinder();
2091 if (mWhitelistToken == null) {
2092 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002093 }
2094 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002095 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002098 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002099 if (parcel.readInt() != 0) {
2100 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002101 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2102 icon = mSmallIcon.getResId();
2103 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 number = parcel.readInt();
2106 if (parcel.readInt() != 0) {
2107 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2108 }
2109 if (parcel.readInt() != 0) {
2110 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2111 }
2112 if (parcel.readInt() != 0) {
2113 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2114 }
2115 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002116 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002117 }
2118 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2120 }
Joe Onorato561d3852010-11-20 18:09:34 -08002121 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002122 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 defaults = parcel.readInt();
2125 flags = parcel.readInt();
2126 if (parcel.readInt() != 0) {
2127 sound = Uri.CREATOR.createFromParcel(parcel);
2128 }
2129
2130 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002131 if (parcel.readInt() != 0) {
2132 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 vibrate = parcel.createLongArray();
2135 ledARGB = parcel.readInt();
2136 ledOnMS = parcel.readInt();
2137 ledOffMS = parcel.readInt();
2138 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002139
2140 if (parcel.readInt() != 0) {
2141 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2142 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143
2144 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002145
John Spurlockfd7f1e02014-03-18 16:41:57 -04002146 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002147
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002148 mGroupKey = parcel.readString();
2149
2150 mSortKey = parcel.readString();
2151
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002152 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002153 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002154
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002155 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2156
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002157 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002158 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2159 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002160
Chris Wren8fd39ec2014-02-27 17:43:26 -05002161 if (parcel.readInt() != 0) {
2162 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2163 }
2164
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002165 visibility = parcel.readInt();
2166
2167 if (parcel.readInt() != 0) {
2168 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2169 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002170
2171 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002172
2173 if (parcel.readInt() != 0) {
2174 mChannelId = parcel.readString();
2175 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002176 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002177
2178 if (parcel.readInt() != 0) {
2179 mShortcutId = parcel.readString();
2180 }
2181
2182 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002183
2184 if (parcel.readInt() != 0) {
2185 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2186 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002187
2188 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
2190
Andy Stadler110988c2010-12-03 14:29:16 -08002191 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002192 public Notification clone() {
2193 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002194 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002195 return that;
2196 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002197
Daniel Sandler1a497d32013-04-18 14:52:45 -04002198 /**
2199 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2200 * of this into that.
2201 * @hide
2202 */
2203 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002204 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002205 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002206 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002207 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002208 that.number = this.number;
2209
2210 // PendingIntents are global, so there's no reason (or way) to clone them.
2211 that.contentIntent = this.contentIntent;
2212 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002213 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002214
2215 if (this.tickerText != null) {
2216 that.tickerText = this.tickerText.toString();
2217 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002218 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002219 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002220 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002221 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002222 that.contentView = this.contentView.clone();
2223 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002224 if (heavy && this.mLargeIcon != null) {
2225 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002226 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002227 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002228 that.sound = this.sound; // android.net.Uri is immutable
2229 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002230 if (this.audioAttributes != null) {
2231 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2232 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002233
2234 final long[] vibrate = this.vibrate;
2235 if (vibrate != null) {
2236 final int N = vibrate.length;
2237 final long[] vib = that.vibrate = new long[N];
2238 System.arraycopy(vibrate, 0, vib, 0, N);
2239 }
2240
2241 that.ledARGB = this.ledARGB;
2242 that.ledOnMS = this.ledOnMS;
2243 that.ledOffMS = this.ledOffMS;
2244 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245
Joe Onorato18e69df2010-05-17 22:26:12 -07002246 that.flags = this.flags;
2247
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002248 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002249
John Spurlockfd7f1e02014-03-18 16:41:57 -04002250 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002251
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002252 that.mGroupKey = this.mGroupKey;
2253
2254 that.mSortKey = this.mSortKey;
2255
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002256 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002257 try {
2258 that.extras = new Bundle(this.extras);
2259 // will unparcel
2260 that.extras.size();
2261 } catch (BadParcelableException e) {
2262 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2263 that.extras = null;
2264 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002265 }
2266
Felipe Lemedd85da62016-06-28 11:29:54 -07002267 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2268 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002269 }
2270
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002271 if (this.actions != null) {
2272 that.actions = new Action[this.actions.length];
2273 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002274 if ( this.actions[i] != null) {
2275 that.actions[i] = this.actions[i].clone();
2276 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002277 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002278 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002279
Daniel Sandler1a497d32013-04-18 14:52:45 -04002280 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002281 that.bigContentView = this.bigContentView.clone();
2282 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002283
Chris Wren8fd39ec2014-02-27 17:43:26 -05002284 if (heavy && this.headsUpContentView != null) {
2285 that.headsUpContentView = this.headsUpContentView.clone();
2286 }
2287
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002288 that.visibility = this.visibility;
2289
2290 if (this.publicVersion != null) {
2291 that.publicVersion = new Notification();
2292 this.publicVersion.cloneInto(that.publicVersion, heavy);
2293 }
2294
Dan Sandler26e81cf2014-05-06 10:01:27 -04002295 that.color = this.color;
2296
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002297 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002298 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002299 that.mShortcutId = this.mShortcutId;
2300 that.mBadgeIcon = this.mBadgeIcon;
2301 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002302 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002303
Daniel Sandler1a497d32013-04-18 14:52:45 -04002304 if (!heavy) {
2305 that.lightenPayload(); // will clean out extras
2306 }
2307 }
2308
2309 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002310 * Note all {@link Uri} that are referenced internally, with the expectation
2311 * that Uri permission grants will need to be issued to ensure the recipient
2312 * of this object is able to render its contents.
2313 *
2314 * @hide
2315 */
2316 public void visitUris(@NonNull Consumer<Uri> visitor) {
2317 visitor.accept(sound);
2318
2319 if (tickerView != null) tickerView.visitUris(visitor);
2320 if (contentView != null) contentView.visitUris(visitor);
2321 if (bigContentView != null) bigContentView.visitUris(visitor);
2322 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2323
2324 if (extras != null) {
2325 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
2326 visitor.accept(extras.getParcelable(EXTRA_BACKGROUND_IMAGE_URI));
2327 }
2328
2329 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2330 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2331 if (!ArrayUtils.isEmpty(messages)) {
2332 for (MessagingStyle.Message message : MessagingStyle.Message
2333 .getMessagesFromBundleArray(messages)) {
2334 visitor.accept(message.getDataUri());
2335 }
2336 }
2337
2338 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2339 if (!ArrayUtils.isEmpty(historic)) {
2340 for (MessagingStyle.Message message : MessagingStyle.Message
2341 .getMessagesFromBundleArray(historic)) {
2342 visitor.accept(message.getDataUri());
2343 }
2344 }
2345 }
2346 }
2347
2348 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002349 * Removes heavyweight parts of the Notification object for archival or for sending to
2350 * listeners when the full contents are not necessary.
2351 * @hide
2352 */
2353 public final void lightenPayload() {
2354 tickerView = null;
2355 contentView = null;
2356 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002357 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002358 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002359 if (extras != null && !extras.isEmpty()) {
2360 final Set<String> keyset = extras.keySet();
2361 final int N = keyset.size();
2362 final String[] keys = keyset.toArray(new String[N]);
2363 for (int i=0; i<N; i++) {
2364 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002365 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2366 continue;
2367 }
Dan Sandler50128532015-12-08 15:42:41 -05002368 final Object obj = extras.get(key);
2369 if (obj != null &&
2370 ( obj instanceof Parcelable
2371 || obj instanceof Parcelable[]
2372 || obj instanceof SparseArray
2373 || obj instanceof ArrayList)) {
2374 extras.remove(key);
2375 }
2376 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002377 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002378 }
2379
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002380 /**
2381 * Make sure this CharSequence is safe to put into a bundle, which basically
2382 * means it had better not be some custom Parcelable implementation.
2383 * @hide
2384 */
2385 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002386 if (cs == null) return cs;
2387 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2388 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2389 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002390 if (cs instanceof Parcelable) {
2391 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2392 + " instance is a custom Parcelable and not allowed in Notification");
2393 return cs.toString();
2394 }
Selim Cinek60a54252016-02-26 17:03:25 -08002395 return removeTextSizeSpans(cs);
2396 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002397
Selim Cinek60a54252016-02-26 17:03:25 -08002398 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2399 if (charSequence instanceof Spanned) {
2400 Spanned ss = (Spanned) charSequence;
2401 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2402 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2403 for (Object span : spans) {
2404 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002405 if (resultSpan instanceof CharacterStyle) {
2406 resultSpan = ((CharacterStyle) span).getUnderlying();
2407 }
2408 if (resultSpan instanceof TextAppearanceSpan) {
2409 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002410 resultSpan = new TextAppearanceSpan(
2411 originalSpan.getFamily(),
2412 originalSpan.getTextStyle(),
2413 -1,
2414 originalSpan.getTextColor(),
2415 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002416 } else if (resultSpan instanceof RelativeSizeSpan
2417 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002418 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002419 } else {
2420 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002421 }
2422 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2423 ss.getSpanFlags(span));
2424 }
2425 return builder;
2426 }
2427 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002428 }
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 public int describeContents() {
2431 return 0;
2432 }
2433
2434 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002435 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 */
Svet Ganovddb94882016-06-23 19:55:24 -07002437 public void writeToParcel(Parcel parcel, int flags) {
2438 // We need to mark all pending intents getting into the notification
2439 // system as being put there to later allow the notification ranker
2440 // to launch them and by doing so add the app to the battery saver white
2441 // list for a short period of time. The problem is that the system
2442 // cannot look into the extras as there may be parcelables there that
2443 // the platform does not know how to handle. To go around that we have
2444 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002445 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002446 if (collectPendingIntents) {
2447 PendingIntent.setOnMarshaledListener(
2448 (PendingIntent intent, Parcel out, int outFlags) -> {
2449 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002450 if (allPendingIntents == null) {
2451 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002452 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002453 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002454 }
2455 });
2456 }
2457 try {
2458 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002459 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002460 writeToParcelImpl(parcel, flags);
2461 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002462 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002463 } finally {
2464 if (collectPendingIntents) {
2465 PendingIntent.setOnMarshaledListener(null);
2466 }
2467 }
2468 }
2469
2470 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 parcel.writeInt(1);
2472
Adrian Roosfb921842017-10-26 14:49:56 +02002473 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002475 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002476 if (mSmallIcon == null && icon != 0) {
2477 // you snuck an icon in here without using the builder; let's try to keep it
2478 mSmallIcon = Icon.createWithResource("", icon);
2479 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002480 if (mSmallIcon != null) {
2481 parcel.writeInt(1);
2482 mSmallIcon.writeToParcel(parcel, 0);
2483 } else {
2484 parcel.writeInt(0);
2485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 parcel.writeInt(number);
2487 if (contentIntent != null) {
2488 parcel.writeInt(1);
2489 contentIntent.writeToParcel(parcel, 0);
2490 } else {
2491 parcel.writeInt(0);
2492 }
2493 if (deleteIntent != null) {
2494 parcel.writeInt(1);
2495 deleteIntent.writeToParcel(parcel, 0);
2496 } else {
2497 parcel.writeInt(0);
2498 }
2499 if (tickerText != null) {
2500 parcel.writeInt(1);
2501 TextUtils.writeToParcel(tickerText, parcel, flags);
2502 } else {
2503 parcel.writeInt(0);
2504 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002505 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002506 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002507 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002508 } else {
2509 parcel.writeInt(0);
2510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 if (contentView != null) {
2512 parcel.writeInt(1);
2513 contentView.writeToParcel(parcel, 0);
2514 } else {
2515 parcel.writeInt(0);
2516 }
Selim Cinek279fa862016-06-14 10:57:25 -07002517 if (mLargeIcon == null && largeIcon != null) {
2518 // you snuck an icon in here without using the builder; let's try to keep it
2519 mLargeIcon = Icon.createWithBitmap(largeIcon);
2520 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002521 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002522 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002523 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002524 } else {
2525 parcel.writeInt(0);
2526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
2528 parcel.writeInt(defaults);
2529 parcel.writeInt(this.flags);
2530
2531 if (sound != null) {
2532 parcel.writeInt(1);
2533 sound.writeToParcel(parcel, 0);
2534 } else {
2535 parcel.writeInt(0);
2536 }
2537 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002538
2539 if (audioAttributes != null) {
2540 parcel.writeInt(1);
2541 audioAttributes.writeToParcel(parcel, 0);
2542 } else {
2543 parcel.writeInt(0);
2544 }
2545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 parcel.writeLongArray(vibrate);
2547 parcel.writeInt(ledARGB);
2548 parcel.writeInt(ledOnMS);
2549 parcel.writeInt(ledOffMS);
2550 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002551
2552 if (fullScreenIntent != null) {
2553 parcel.writeInt(1);
2554 fullScreenIntent.writeToParcel(parcel, 0);
2555 } else {
2556 parcel.writeInt(0);
2557 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002558
2559 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002560
John Spurlockfd7f1e02014-03-18 16:41:57 -04002561 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002562
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002563 parcel.writeString(mGroupKey);
2564
2565 parcel.writeString(mSortKey);
2566
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002567 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002568
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002569 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002570
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002571 if (bigContentView != null) {
2572 parcel.writeInt(1);
2573 bigContentView.writeToParcel(parcel, 0);
2574 } else {
2575 parcel.writeInt(0);
2576 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002577
Chris Wren8fd39ec2014-02-27 17:43:26 -05002578 if (headsUpContentView != null) {
2579 parcel.writeInt(1);
2580 headsUpContentView.writeToParcel(parcel, 0);
2581 } else {
2582 parcel.writeInt(0);
2583 }
2584
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002585 parcel.writeInt(visibility);
2586
2587 if (publicVersion != null) {
2588 parcel.writeInt(1);
2589 publicVersion.writeToParcel(parcel, 0);
2590 } else {
2591 parcel.writeInt(0);
2592 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002593
2594 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002595
2596 if (mChannelId != null) {
2597 parcel.writeInt(1);
2598 parcel.writeString(mChannelId);
2599 } else {
2600 parcel.writeInt(0);
2601 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002602 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002603
2604 if (mShortcutId != null) {
2605 parcel.writeInt(1);
2606 parcel.writeString(mShortcutId);
2607 } else {
2608 parcel.writeInt(0);
2609 }
2610
2611 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002612
2613 if (mSettingsText != null) {
2614 parcel.writeInt(1);
2615 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2616 } else {
2617 parcel.writeInt(0);
2618 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002619
2620 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002621
2622 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 }
2624
2625 /**
2626 * Parcelable.Creator that instantiates Notification objects
2627 */
2628 public static final Parcelable.Creator<Notification> CREATOR
2629 = new Parcelable.Creator<Notification>()
2630 {
2631 public Notification createFromParcel(Parcel parcel)
2632 {
2633 return new Notification(parcel);
2634 }
2635
2636 public Notification[] newArray(int size)
2637 {
2638 return new Notification[size];
2639 }
2640 };
2641
2642 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002643 * @hide
2644 */
2645 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2646 Notification.Action[] firstAs = first.actions;
2647 Notification.Action[] secondAs = second.actions;
2648 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2649 return true;
2650 }
2651 if (firstAs != null && secondAs != null) {
2652 if (firstAs.length != secondAs.length) {
2653 return true;
2654 }
2655 for (int i = 0; i < firstAs.length; i++) {
2656 if (!Objects.equals(firstAs[i].title, secondAs[i].title)) {
2657 return true;
2658 }
2659 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2660 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2661 if (firstRs == null) {
2662 firstRs = new RemoteInput[0];
2663 }
2664 if (secondRs == null) {
2665 secondRs = new RemoteInput[0];
2666 }
2667 if (firstRs.length != secondRs.length) {
2668 return true;
2669 }
2670 for (int j = 0; j < firstRs.length; j++) {
2671 if (!Objects.equals(firstRs[j].getLabel(), secondRs[j].getLabel())) {
2672 return true;
2673 }
Julia Reynolds5a366fb2018-03-21 15:40:53 -04002674 CharSequence[] firstCs = firstRs[j].getChoices();
2675 CharSequence[] secondCs = secondRs[j].getChoices();
Julia Reynolds7217dc92018-03-07 12:12:09 -05002676 if (firstCs == null) {
2677 firstCs = new CharSequence[0];
2678 }
2679 if (secondCs == null) {
2680 secondCs = new CharSequence[0];
2681 }
2682 if (firstCs.length != secondCs.length) {
2683 return true;
2684 }
2685 for (int k = 0; k < firstCs.length; k++) {
2686 if (!Objects.equals(firstCs[k], secondCs[k])) {
2687 return true;
2688 }
2689 }
2690 }
2691 }
2692 }
2693 return false;
2694 }
2695
2696 /**
2697 * @hide
2698 */
2699 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2700 if (first.getStyle() == null) {
2701 return second.getStyle() != null;
2702 }
2703 if (second.getStyle() == null) {
2704 return true;
2705 }
2706 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2707 }
2708
2709 /**
2710 * @hide
2711 */
2712 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
2713 return !first.usesStandardHeader() || !second.usesStandardHeader();
2714 }
2715
2716 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002717 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2718 * <p>
2719 * For backwards compatibility {@code extras} holds some references to "real" member data such
2720 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2721 * fine as long as the object stays in one process.
2722 * <p>
2723 * However, once the notification goes into a parcel each reference gets marshalled separately,
2724 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2725 */
2726 private void fixDuplicateExtras() {
2727 if (extras != null) {
2728 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2729 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2730 }
2731 }
2732
2733 /**
2734 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2735 * separate object, replace it with the field's version to avoid holding duplicate copies.
2736 */
2737 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2738 if (original != null && extras.getParcelable(extraName) != null) {
2739 extras.putParcelable(extraName, original);
2740 }
2741 }
2742
2743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2745 * layout.
2746 *
2747 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2748 * in the view.</p>
2749 * @param context The context for your application / activity.
2750 * @param contentTitle The title that goes in the expanded entry.
2751 * @param contentText The text that goes in the expanded entry.
2752 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2753 * If this is an activity, it must include the
2754 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002755 * that you take care of task management as described in the
2756 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2757 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002758 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002759 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002760 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002762 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 public void setLatestEventInfo(Context context,
2764 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002765 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2766 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2767 new Throwable());
2768 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002769
Selim Cinek4ac6f602016-06-13 15:47:03 -07002770 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2771 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2772 }
2773
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002774 // ensure that any information already set directly is preserved
2775 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002776
2777 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002779 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002782 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002784 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002785
2786 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 }
2788
Julia Reynoldsda303542015-11-23 14:00:20 -05002789 /**
2790 * @hide
2791 */
2792 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002793 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002794 }
2795
2796 /**
2797 * @hide
2798 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002799 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002800 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002801 }
2802
Yi Jin6b514142017-10-30 14:54:12 -07002803 /**
2804 * @hide
2805 */
2806 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2807 long token = proto.start(fieldId);
2808 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2809 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2810 proto.write(NotificationProto.FLAGS, this.flags);
2811 proto.write(NotificationProto.COLOR, this.color);
2812 proto.write(NotificationProto.CATEGORY, this.category);
2813 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2814 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2815 if (this.actions != null) {
2816 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2817 }
2818 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2819 proto.write(NotificationProto.VISIBILITY, this.visibility);
2820 }
2821 if (publicVersion != null) {
2822 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2823 }
2824 proto.end(token);
2825 }
2826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 @Override
2828 public String toString() {
2829 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002830 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002831 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002832 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002833 sb.append(priority);
2834 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002835 if (contentView != null) {
2836 sb.append(contentView.getPackage());
2837 sb.append("/0x");
2838 sb.append(Integer.toHexString(contentView.getLayoutId()));
2839 } else {
2840 sb.append("null");
2841 }
2842 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002843 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2844 sb.append("default");
2845 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 int N = this.vibrate.length-1;
2847 sb.append("[");
2848 for (int i=0; i<N; i++) {
2849 sb.append(this.vibrate[i]);
2850 sb.append(',');
2851 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002852 if (N != -1) {
2853 sb.append(this.vibrate[N]);
2854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 } else {
2857 sb.append("null");
2858 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002859 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002860 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002862 } else if (this.sound != null) {
2863 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 } else {
2865 sb.append("null");
2866 }
Chris Wren365b6d32015-07-16 10:39:26 -04002867 if (this.tickerText != null) {
2868 sb.append(" tick");
2869 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002870 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002872 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002873 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002874 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002875 if (this.category != null) {
2876 sb.append(" category=");
2877 sb.append(this.category);
2878 }
2879 if (this.mGroupKey != null) {
2880 sb.append(" groupKey=");
2881 sb.append(this.mGroupKey);
2882 }
2883 if (this.mSortKey != null) {
2884 sb.append(" sortKey=");
2885 sb.append(this.mSortKey);
2886 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002887 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002888 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002889 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002890 }
2891 sb.append(" vis=");
2892 sb.append(visibilityToString(this.visibility));
2893 if (this.publicVersion != null) {
2894 sb.append(" publicVersion=");
2895 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002896 }
2897 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 return sb.toString();
2899 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002900
Dan Sandler1b718782014-07-18 12:43:45 -04002901 /**
2902 * {@hide}
2903 */
2904 public static String visibilityToString(int vis) {
2905 switch (vis) {
2906 case VISIBILITY_PRIVATE:
2907 return "PRIVATE";
2908 case VISIBILITY_PUBLIC:
2909 return "PUBLIC";
2910 case VISIBILITY_SECRET:
2911 return "SECRET";
2912 default:
2913 return "UNKNOWN(" + String.valueOf(vis) + ")";
2914 }
2915 }
2916
Joe Onoratocb109a02011-01-18 17:57:41 -08002917 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002918 * {@hide}
2919 */
2920 public static String priorityToString(@Priority int pri) {
2921 switch (pri) {
2922 case PRIORITY_MIN:
2923 return "MIN";
2924 case PRIORITY_LOW:
2925 return "LOW";
2926 case PRIORITY_DEFAULT:
2927 return "DEFAULT";
2928 case PRIORITY_HIGH:
2929 return "HIGH";
2930 case PRIORITY_MAX:
2931 return "MAX";
2932 default:
2933 return "UNKNOWN(" + String.valueOf(pri) + ")";
2934 }
2935 }
2936
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002937 /**
2938 * @hide
2939 */
2940 public boolean hasCompletedProgress() {
2941 // not a progress notification; can't be complete
2942 if (!extras.containsKey(EXTRA_PROGRESS)
2943 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2944 return false;
2945 }
2946 // many apps use max 0 for 'indeterminate'; not complete
2947 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2948 return false;
2949 }
2950 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2951 }
2952
Jeff Sharkey000ce802017-04-29 13:13:27 -06002953 /** @removed */
2954 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002955 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002956 return mChannelId;
2957 }
2958
2959 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002960 * Returns the id of the channel this notification posts to.
2961 */
2962 public String getChannelId() {
2963 return mChannelId;
2964 }
2965
Jeff Sharkey000ce802017-04-29 13:13:27 -06002966 /** @removed */
2967 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002968 public long getTimeout() {
2969 return mTimeout;
2970 }
2971
2972 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002973 * Returns the duration from posting after which this notification should be canceled by the
2974 * system, if it's not canceled already.
2975 */
2976 public long getTimeoutAfter() {
2977 return mTimeout;
2978 }
2979
2980 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002981 * Returns what icon should be shown for this notification if it is being displayed in a
2982 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2983 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2984 */
2985 public int getBadgeIconType() {
2986 return mBadgeIcon;
2987 }
2988
2989 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002990 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002991 *
2992 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2993 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002994 */
2995 public String getShortcutId() {
2996 return mShortcutId;
2997 }
2998
Julia Reynolds3aedded2017-03-31 14:42:09 -04002999
3000 /**
3001 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3002 */
3003 public CharSequence getSettingsText() {
3004 return mSettingsText;
3005 }
3006
Julia Reynolds13d898c2017-02-02 12:22:05 -05003007 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003008 * Returns which type of notifications in a group are responsible for audibly alerting the
3009 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3010 * {@link #GROUP_ALERT_SUMMARY}.
3011 */
3012 public @GroupAlertBehavior int getGroupAlertBehavior() {
3013 return mGroupAlertBehavior;
3014 }
3015
3016 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003017 * The small icon representing this notification in the status bar and content view.
3018 *
3019 * @return the small icon representing this notification.
3020 *
3021 * @see Builder#getSmallIcon()
3022 * @see Builder#setSmallIcon(Icon)
3023 */
3024 public Icon getSmallIcon() {
3025 return mSmallIcon;
3026 }
3027
3028 /**
3029 * Used when notifying to clean up legacy small icons.
3030 * @hide
3031 */
3032 public void setSmallIcon(Icon icon) {
3033 mSmallIcon = icon;
3034 }
3035
3036 /**
3037 * The large icon shown in this notification's content view.
3038 * @see Builder#getLargeIcon()
3039 * @see Builder#setLargeIcon(Icon)
3040 */
3041 public Icon getLargeIcon() {
3042 return mLargeIcon;
3043 }
3044
3045 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003046 * @hide
3047 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02003048 public boolean isGroupSummary() {
3049 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3050 }
3051
3052 /**
3053 * @hide
3054 */
3055 public boolean isGroupChild() {
3056 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3057 }
3058
3059 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003060 * @hide
3061 */
3062 public boolean suppressAlertingDueToGrouping() {
3063 if (isGroupSummary()
3064 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3065 return true;
3066 } else if (isGroupChild()
3067 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3068 return true;
3069 }
3070 return false;
3071 }
3072
3073 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003074 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003075 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003076 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003077 * content views using the platform's notification layout template. If your app supports
3078 * versions of Android as old as API level 4, you can instead use
3079 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3080 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3081 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003082 *
Scott Main183bf112012-08-13 19:12:13 -07003083 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003084 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003085 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003086 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003087 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3088 * .setContentText(subject)
3089 * .setSmallIcon(R.drawable.new_mail)
3090 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003091 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003092 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003093 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003094 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003095 /**
3096 * @hide
3097 */
3098 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3099 "android.rebuild.contentViewActionCount";
3100 /**
3101 * @hide
3102 */
3103 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3104 = "android.rebuild.bigViewActionCount";
3105 /**
3106 * @hide
3107 */
3108 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3109 = "android.rebuild.hudViewActionCount";
3110
Daniel Sandler602ad1c2012-06-12 16:06:27 -04003111 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04003112
Selim Cinek6743c0b2017-01-18 18:24:01 -08003113 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3114 SystemProperties.getBoolean("notifications.only_title", true);
3115
Selim Cinek389edcd2017-05-11 19:16:44 -07003116 /**
3117 * The lightness difference that has to be added to the primary text color to obtain the
3118 * secondary text color when the background is light.
3119 */
3120 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3121
3122 /**
3123 * The lightness difference that has to be added to the primary text color to obtain the
3124 * secondary text color when the background is dark.
3125 * A bit less then the above value, since it looks better on dark backgrounds.
3126 */
3127 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3128
Joe Onorato46439ce2010-11-19 13:56:21 -08003129 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003130 private Notification mN;
3131 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003132 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003133 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003134 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003135 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003136 private boolean mIsLegacy;
3137 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003138
3139 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003140 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3141 */
3142 private int mCachedContrastColor = COLOR_INVALID;
3143 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003144 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003145 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003146 */
3147 private int mCachedAmbientColor = COLOR_INVALID;
3148 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003149 /**
3150 * A neutral color color that can be used for icons.
3151 */
3152 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003153
3154 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003155 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3156 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3157 */
3158 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003159 private int mTextColorsAreForBackground = COLOR_INVALID;
3160 private int mPrimaryTextColor = COLOR_INVALID;
3161 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003162 private int mBackgroundColor = COLOR_INVALID;
3163 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003164 /**
3165 * A temporary location where actions are stored. If != null the view originally has action
3166 * but doesn't have any for this inflation.
3167 */
3168 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003169 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003170
Anthony Chenad4d1582017-04-10 16:07:58 -07003171 private boolean mTintActionButtons;
3172 private boolean mInNightMode;
3173
Adrian Roos70d7aa32017-01-11 15:39:06 -08003174 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003175 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003176 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003177 * @param context
3178 * A {@link Context} that will be used by the Builder to construct the
3179 * RemoteViews. The Context will not be held past the lifetime of this Builder
3180 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003181 * @param channelId
3182 * The constructed Notification will be posted on this
3183 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3184 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003185 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003186 public Builder(Context context, String channelId) {
3187 this(context, (Notification) null);
3188 mN.mChannelId = channelId;
3189 }
3190
3191 /**
3192 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3193 * instead. All posted Notifications must specify a NotificationChannel Id.
3194 */
3195 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003196 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003197 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003198 }
3199
Joe Onoratocb109a02011-01-18 17:57:41 -08003200 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003201 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003202 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003203 public Builder(Context context, Notification toAdopt) {
3204 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003205 Resources res = mContext.getResources();
3206 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3207
3208 if (res.getBoolean(R.bool.config_enableNightMode)) {
3209 Configuration currentConfig = res.getConfiguration();
3210 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3211 == Configuration.UI_MODE_NIGHT_YES;
3212 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003213
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003214 if (toAdopt == null) {
3215 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003216 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3217 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3218 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003219 mN.priority = PRIORITY_DEFAULT;
3220 mN.visibility = VISIBILITY_PRIVATE;
3221 } else {
3222 mN = toAdopt;
3223 if (mN.actions != null) {
3224 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003225 }
3226
Selim Cineke7238dd2017-12-14 17:48:32 -08003227 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3228 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3229 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003230 }
3231
Selim Cinek4ac6f602016-06-13 15:47:03 -07003232 if (mN.getSmallIcon() == null && mN.icon != 0) {
3233 setSmallIcon(mN.icon);
3234 }
3235
3236 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3237 setLargeIcon(mN.largeIcon);
3238 }
3239
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003240 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3241 if (!TextUtils.isEmpty(templateClass)) {
3242 final Class<? extends Style> styleClass
3243 = getNotificationStyleClass(templateClass);
3244 if (styleClass == null) {
3245 Log.d(TAG, "Unknown style class: " + templateClass);
3246 } else {
3247 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003248 final Constructor<? extends Style> ctor =
3249 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003250 ctor.setAccessible(true);
3251 final Style style = ctor.newInstance();
3252 style.restoreFromExtras(mN.extras);
3253
3254 if (style != null) {
3255 setStyle(style);
3256 }
3257 } catch (Throwable t) {
3258 Log.e(TAG, "Could not create Style", t);
3259 }
3260 }
3261 }
3262
3263 }
3264 }
3265
3266 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003267 if (mColorUtil == null) {
3268 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003269 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003270 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003271 }
3272
3273 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003274 * If this notification is duplicative of a Launcher shortcut, sets the
3275 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3276 * the shortcut.
3277 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003278 * This field will be ignored by Launchers that don't support badging, don't show
3279 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003280 *
3281 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3282 * supersedes
3283 */
3284 public Builder setShortcutId(String shortcutId) {
3285 mN.mShortcutId = shortcutId;
3286 return this;
3287 }
3288
3289 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003290 * Sets which icon to display as a badge for this notification.
3291 *
3292 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3293 * {@link #BADGE_ICON_LARGE}.
3294 *
3295 * Note: This value might be ignored, for launchers that don't support badge icons.
3296 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003297 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003298 mN.mBadgeIcon = icon;
3299 return this;
3300 }
3301
3302 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003303 * Sets the group alert behavior for this notification. Use this method to mute this
3304 * notification if alerts for this notification's group should be handled by a different
3305 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003306 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3307 * mute. For example, if you want only the summary of your group to make noise, all
3308 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003309 *
3310 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3311 */
3312 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3313 mN.mGroupAlertBehavior = groupAlertBehavior;
3314 return this;
3315 }
3316
Jeff Sharkey000ce802017-04-29 13:13:27 -06003317 /** @removed */
3318 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003319 public Builder setChannel(String channelId) {
3320 mN.mChannelId = channelId;
3321 return this;
3322 }
3323
3324 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003325 * Specifies the channel the notification should be delivered on.
3326 */
3327 public Builder setChannelId(String channelId) {
3328 mN.mChannelId = channelId;
3329 return this;
3330 }
3331
Jeff Sharkey000ce802017-04-29 13:13:27 -06003332 /** @removed */
3333 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003334 public Builder setTimeout(long durationMs) {
3335 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003336 return this;
3337 }
3338
3339 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003340 * Specifies a duration in milliseconds after which this notification should be canceled,
3341 * if it is not already canceled.
3342 */
3343 public Builder setTimeoutAfter(long durationMs) {
3344 mN.mTimeout = durationMs;
3345 return this;
3346 }
3347
3348 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003349 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003350 *
3351 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3352 * shown anymore by default and must be opted into by using
3353 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003354 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003355 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003356 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003357 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003358 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003359 return this;
3360 }
3361
Joe Onoratocb109a02011-01-18 17:57:41 -08003362 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003363 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003364 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003365 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3366 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003367 */
3368 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003369 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003370 return this;
3371 }
3372
3373 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003374 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003375 *
3376 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003377 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003378 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003379 * Useful when showing an elapsed time (like an ongoing phone call).
3380 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003381 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003382 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003383 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003384 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003385 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003386 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003387 */
3388 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003389 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003390 return this;
3391 }
3392
3393 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003394 * Sets the Chronometer to count down instead of counting up.
3395 *
3396 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3397 * If it isn't set the chronometer will count up.
3398 *
3399 * @see #setUsesChronometer(boolean)
3400 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003401 public Builder setChronometerCountDown(boolean countDown) {
3402 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003403 return this;
3404 }
3405
3406 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003407 * Set the small icon resource, which will be used to represent the notification in the
3408 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003409 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003410
3411 * The platform template for the expanded view will draw this icon in the left, unless a
3412 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3413 * icon will be moved to the right-hand side.
3414 *
3415
3416 * @param icon
3417 * A resource ID in the application's package of the drawable to use.
3418 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003419 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003420 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003421 return setSmallIcon(icon != 0
3422 ? Icon.createWithResource(mContext, icon)
3423 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003424 }
3425
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 /**
3427 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3428 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3429 * LevelListDrawable}.
3430 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003431 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003432 * @param level The level to use for the icon.
3433 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003434 * @see Notification#icon
3435 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003436 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003437 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003438 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003439 return setSmallIcon(icon);
3440 }
3441
3442 /**
3443 * Set the small icon, which will be used to represent the notification in the
3444 * status bar and content view (unless overriden there by a
3445 * {@link #setLargeIcon(Bitmap) large icon}).
3446 *
3447 * @param icon An Icon object to use.
3448 * @see Notification#icon
3449 */
3450 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003451 mN.setSmallIcon(icon);
3452 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3453 mN.icon = icon.getResId();
3454 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003455 return this;
3456 }
3457
Joe Onoratocb109a02011-01-18 17:57:41 -08003458 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003459 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003460 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003461 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003462 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003463 return this;
3464 }
3465
Joe Onoratocb109a02011-01-18 17:57:41 -08003466 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003467 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003468 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003469 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003470 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003471 return this;
3472 }
3473
Joe Onoratocb109a02011-01-18 17:57:41 -08003474 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003475 * This provides some additional information that is displayed in the notification. No
3476 * guarantees are given where exactly it is displayed.
3477 *
3478 * <p>This information should only be provided if it provides an essential
3479 * benefit to the understanding of the notification. The more text you provide the
3480 * less readable it becomes. For example, an email client should only provide the account
3481 * name here if more than one email account has been added.</p>
3482 *
3483 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3484 * notification header area.
3485 *
3486 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3487 * this will be shown in the third line of text in the platform notification template.
3488 * You should not be using {@link #setProgress(int, int, boolean)} at the
3489 * same time on those versions; they occupy the same place.
3490 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003491 */
3492 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003493 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003494 return this;
3495 }
3496
3497 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003498 * Provides text that will appear as a link to your application's settings.
3499 *
3500 * <p>This text does not appear within notification {@link Style templates} but may
3501 * appear when the user uses an affordance to learn more about the notification.
3502 * Additionally, this text will not appear unless you provide a valid link target by
3503 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3504 *
3505 * <p>This text is meant to be concise description about what the user can customize
3506 * when they click on this link. The recommended maximum length is 40 characters.
3507 * @param text
3508 * @return
3509 */
3510 public Builder setSettingsText(CharSequence text) {
3511 mN.mSettingsText = safeCharSequence(text);
3512 return this;
3513 }
3514
3515 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003516 * Set the remote input history.
3517 *
3518 * This should be set to the most recent inputs that have been sent
3519 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3520 * longer relevant (e.g. for chat notifications once the other party has responded).
3521 *
3522 * The most recent input must be stored at the 0 index, the second most recent at the
3523 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3524 * and how much of each individual input is shown.
3525 *
3526 * <p>Note: The reply text will only be shown on notifications that have least one action
3527 * with a {@code RemoteInput}.</p>
3528 */
3529 public Builder setRemoteInputHistory(CharSequence[] text) {
3530 if (text == null) {
3531 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3532 } else {
3533 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3534 CharSequence[] safe = new CharSequence[N];
3535 for (int i = 0; i < N; i++) {
3536 safe[i] = safeCharSequence(text[i]);
3537 }
3538 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3539 }
3540 return this;
3541 }
3542
3543 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003544 * Sets the number of items this notification represents. May be displayed as a badge count
3545 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003546 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003547 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003548 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003549 return this;
3550 }
3551
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003553 * A small piece of additional information pertaining to this notification.
3554 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003555 * The platform template will draw this on the last line of the notification, at the far
3556 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003557 *
3558 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3559 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3560 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003561 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003562 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003563 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003564 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003565 return this;
3566 }
3567
Joe Onoratocb109a02011-01-18 17:57:41 -08003568 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003569 * Set the progress this notification represents.
3570 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003571 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003572 */
3573 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003574 mN.extras.putInt(EXTRA_PROGRESS, progress);
3575 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3576 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003577 return this;
3578 }
3579
3580 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003581 * Supply a custom RemoteViews to use instead of the platform template.
3582 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003583 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003584 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003585 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003586 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 return setCustomContentView(views);
3588 }
3589
3590 /**
3591 * Supply custom RemoteViews to use instead of the platform template.
3592 *
3593 * This will override the layout that would otherwise be constructed by this Builder
3594 * object.
3595 */
3596 public Builder setCustomContentView(RemoteViews contentView) {
3597 mN.contentView = contentView;
3598 return this;
3599 }
3600
3601 /**
3602 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3603 *
3604 * This will override the expanded layout that would otherwise be constructed by this
3605 * Builder object.
3606 */
3607 public Builder setCustomBigContentView(RemoteViews contentView) {
3608 mN.bigContentView = contentView;
3609 return this;
3610 }
3611
3612 /**
3613 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3614 *
3615 * This will override the heads-up layout that would otherwise be constructed by this
3616 * Builder object.
3617 */
3618 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3619 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003620 return this;
3621 }
3622
Joe Onoratocb109a02011-01-18 17:57:41 -08003623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003624 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3625 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003626 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3627 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3628 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003629 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003630 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003631 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003632 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003633 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 return this;
3637 }
3638
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003640 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3641 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003642 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003643 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003644 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003645 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003646 return this;
3647 }
3648
Joe Onoratocb109a02011-01-18 17:57:41 -08003649 /**
3650 * An intent to launch instead of posting the notification to the status bar.
3651 * Only for use with extremely high-priority notifications demanding the user's
3652 * <strong>immediate</strong> attention, such as an incoming phone call or
3653 * alarm clock that the user has explicitly set to a particular time.
3654 * If this facility is used for something else, please give the user an option
3655 * to turn it off and use a normal notification, as this can be extremely
3656 * disruptive.
3657 *
Chris Wren47c20a12014-06-18 17:27:29 -04003658 * <p>
3659 * The system UI may choose to display a heads-up notification, instead of
3660 * launching this intent, while the user is using the device.
3661 * </p>
3662 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003663 * @param intent The pending intent to launch.
3664 * @param highPriority Passing true will cause this notification to be sent
3665 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003666 *
3667 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003668 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003669 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003670 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003671 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3672 return this;
3673 }
3674
Joe Onoratocb109a02011-01-18 17:57:41 -08003675 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003676 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003678 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003679 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003680 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003681 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003682 return this;
3683 }
3684
Joe Onoratocb109a02011-01-18 17:57:41 -08003685 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003686 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003687 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003688 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003689 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003690 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003691 setTicker(tickerText);
3692 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003693 return this;
3694 }
3695
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003697 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698 *
3699 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003700 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3701 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003702 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003703 public Builder setLargeIcon(Bitmap b) {
3704 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3705 }
3706
3707 /**
3708 * Add a large icon to the notification content view.
3709 *
3710 * In the platform template, this image will be shown on the left of the notification view
3711 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3712 * badge atop the large icon).
3713 */
3714 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003715 mN.mLargeIcon = icon;
3716 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003717 return this;
3718 }
3719
Joe Onoratocb109a02011-01-18 17:57:41 -08003720 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003721 * Set the sound to play.
3722 *
John Spurlockc0650f022014-07-19 13:22:39 -04003723 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3724 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003725 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003726 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003727 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003728 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003729 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003730 mN.sound = sound;
3731 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003732 return this;
3733 }
3734
Joe Onoratocb109a02011-01-18 17:57:41 -08003735 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003736 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003737 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003738 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3739 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003740 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003742 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003743 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003744 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003745 mN.sound = sound;
3746 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003747 return this;
3748 }
3749
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003751 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3752 * use during playback.
3753 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003754 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003755 * @see Notification#sound
3756 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003757 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003758 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003759 mN.sound = sound;
3760 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003761 return this;
3762 }
3763
3764 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003765 * Set the vibration pattern to use.
3766 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003767 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3768 * <code>pattern</code> parameter.
3769 *
Chris Wren47c20a12014-06-18 17:27:29 -04003770 * <p>
3771 * A notification that vibrates is more likely to be presented as a heads-up notification.
3772 * </p>
3773 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003774 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003775 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003776 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003777 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003778 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003780 return this;
3781 }
3782
Joe Onoratocb109a02011-01-18 17:57:41 -08003783 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003784 * Set the desired color for the indicator LED on the device, as well as the
3785 * blink duty cycle (specified in milliseconds).
3786 *
3787
3788 * Not all devices will honor all (or even any) of these values.
3789 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003790 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003791 * @see Notification#ledARGB
3792 * @see Notification#ledOnMS
3793 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003794 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003795 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003796 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003797 mN.ledARGB = argb;
3798 mN.ledOnMS = onMs;
3799 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003800 if (onMs != 0 || offMs != 0) {
3801 mN.flags |= FLAG_SHOW_LIGHTS;
3802 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003803 return this;
3804 }
3805
Joe Onoratocb109a02011-01-18 17:57:41 -08003806 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003807 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003808 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003809
3810 * Ongoing notifications cannot be dismissed by the user, so your application or service
3811 * must take care of canceling them.
3812 *
3813
3814 * They are typically used to indicate a background task that the user is actively engaged
3815 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3816 * (e.g., a file download, sync operation, active network connection).
3817 *
3818
3819 * @see Notification#FLAG_ONGOING_EVENT
3820 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003821 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003822 public Builder setOngoing(boolean ongoing) {
3823 setFlag(FLAG_ONGOING_EVENT, ongoing);
3824 return this;
3825 }
3826
Joe Onoratocb109a02011-01-18 17:57:41 -08003827 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003828 * Set whether this notification should be colorized. When set, the color set with
3829 * {@link #setColor(int)} will be used as the background color of this notification.
3830 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003831 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3832 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003833 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003834 * For most styles, the coloring will only be applied if the notification is for a
3835 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003836 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003837 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003838 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003839 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003840 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003841 */
3842 public Builder setColorized(boolean colorize) {
3843 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3844 return this;
3845 }
3846
3847 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003848 * Set this flag if you would only like the sound, vibrate
3849 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003850 *
3851 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003852 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003853 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3854 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3855 return this;
3856 }
3857
Joe Onoratocb109a02011-01-18 17:57:41 -08003858 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003859 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003860 *
3861 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003862 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003863 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003864 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003865 return this;
3866 }
3867
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003869 * Set whether or not this notification should not bridge to other devices.
3870 *
3871 * <p>Some notifications can be bridged to other devices for remote display.
3872 * This hint can be set to recommend this notification not be bridged.
3873 */
3874 public Builder setLocalOnly(boolean localOnly) {
3875 setFlag(FLAG_LOCAL_ONLY, localOnly);
3876 return this;
3877 }
3878
3879 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003881 * <p>
3882 * The value should be one or more of the following fields combined with
3883 * bitwise-or:
3884 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3885 * <p>
3886 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003887 *
3888 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003889 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003890 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003891 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003892 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003893 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003894 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003895 return this;
3896 }
3897
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003898 /**
3899 * Set the priority of this notification.
3900 *
3901 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003902 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003903 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003904 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003905 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003906 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003907 return this;
3908 }
Joe Malin8d40d042012-11-05 11:36:40 -08003909
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003910 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003911 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003912 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003913 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003914 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003915 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003916 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003917 return this;
3918 }
3919
3920 /**
Chris Wrendde75302014-03-26 17:24:15 -04003921 * Add a person that is relevant to this notification.
3922 *
Chris Wrene6c48932014-09-29 17:19:27 -04003923 * <P>
3924 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003925 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3926 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3927 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003928 * </P>
3929 *
3930 * <P>
3931 * The person should be specified by the {@code String} representation of a
3932 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3933 * </P>
3934 *
3935 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3936 * URIs. The path part of these URIs must exist in the contacts database, in the
3937 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3938 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003939 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3940 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003941 * </P>
3942 *
3943 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003944 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003945 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003946 */
Chris Wrene6c48932014-09-29 17:19:27 -04003947 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07003948 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08003949 return this;
3950 }
3951
3952 /**
3953 * Add a person that is relevant to this notification.
3954 *
3955 * <P>
3956 * Depending on user preferences, this annotation may allow the notification to pass
3957 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3958 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3959 * appear more prominently in the user interface.
3960 * </P>
3961 *
3962 * <P>
3963 * A person should usually contain a uri in order to benefit from the ranking boost.
3964 * However, even if no uri is provided, it's beneficial to provide other people in the
3965 * notification, such that listeners and voice only devices can announce and handle them
3966 * properly.
3967 * </P>
3968 *
3969 * @param person the person to add.
3970 * @see Notification#EXTRA_PEOPLE_LIST
3971 */
3972 public Builder addPerson(Person person) {
3973 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003974 return this;
3975 }
3976
3977 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003978 * Set this notification to be part of a group of notifications sharing the same key.
3979 * Grouped notifications may display in a cluster or stack on devices which
3980 * support such rendering.
3981 *
3982 * <p>To make this notification the summary for its group, also call
3983 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3984 * {@link #setSortKey}.
3985 * @param groupKey The group key of the group.
3986 * @return this object for method chaining
3987 */
3988 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003989 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003990 return this;
3991 }
3992
3993 /**
3994 * Set this notification to be the group summary for a group of notifications.
3995 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003996 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3997 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003998 * @param isGroupSummary Whether this notification should be a group summary.
3999 * @return this object for method chaining
4000 */
4001 public Builder setGroupSummary(boolean isGroupSummary) {
4002 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4003 return this;
4004 }
4005
4006 /**
4007 * Set a sort key that orders this notification among other notifications from the
4008 * same package. This can be useful if an external sort was already applied and an app
4009 * would like to preserve this. Notifications will be sorted lexicographically using this
4010 * value, although providing different priorities in addition to providing sort key may
4011 * cause this value to be ignored.
4012 *
4013 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004014 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004015 *
4016 * @see String#compareTo(String)
4017 */
4018 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004019 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004020 return this;
4021 }
4022
4023 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004024 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004025 *
Griff Hazen720042b2014-02-24 15:46:56 -08004026 * <p>Values within the Bundle will replace existing extras values in this Builder.
4027 *
4028 * @see Notification#extras
4029 */
Griff Hazen959591e2014-05-15 22:26:18 -07004030 public Builder addExtras(Bundle extras) {
4031 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004032 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004033 }
4034 return this;
4035 }
4036
4037 /**
4038 * Set metadata for this notification.
4039 *
4040 * <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 -04004041 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004042 * called.
4043 *
Griff Hazen720042b2014-02-24 15:46:56 -08004044 * <p>Replaces any existing extras values with those from the provided Bundle.
4045 * Use {@link #addExtras} to merge in metadata instead.
4046 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004047 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004048 */
Griff Hazen959591e2014-05-15 22:26:18 -07004049 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004050 if (extras != null) {
4051 mUserExtras = extras;
4052 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004053 return this;
4054 }
4055
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004056 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004057 * Get the current metadata Bundle used by this notification Builder.
4058 *
4059 * <p>The returned Bundle is shared with this Builder.
4060 *
4061 * <p>The current contents of this Bundle are copied into the Notification each time
4062 * {@link #build()} is called.
4063 *
4064 * @see Notification#extras
4065 */
4066 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004067 return mUserExtras;
4068 }
4069
4070 private Bundle getAllExtras() {
4071 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4072 saveExtras.putAll(mN.extras);
4073 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004074 }
4075
4076 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004077 * Add an action to this notification. Actions are typically displayed by
4078 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004079 * <p>
4080 * Every action must have an icon (32dp square and matching the
4081 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4082 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4083 * <p>
4084 * A notification in its expanded form can display up to 3 actions, from left to right in
4085 * the order they were added. Actions will not be displayed when the notification is
4086 * collapsed, however, so be sure that any essential functions may be accessed by the user
4087 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004088 *
4089 * @param icon Resource ID of a drawable that represents the action.
4090 * @param title Text describing the action.
4091 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004092 *
4093 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004094 */
Dan Sandler86647982015-05-13 23:41:13 -04004095 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004096 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004097 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004098 return this;
4099 }
4100
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004101 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004102 * Add an action to this notification. Actions are typically displayed by
4103 * the system as a button adjacent to the notification content.
4104 * <p>
4105 * Every action must have an icon (32dp square and matching the
4106 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4107 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4108 * <p>
4109 * A notification in its expanded form can display up to 3 actions, from left to right in
4110 * the order they were added. Actions will not be displayed when the notification is
4111 * collapsed, however, so be sure that any essential functions may be accessed by the user
4112 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4113 *
4114 * @param action The action to add.
4115 */
4116 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004117 if (action != null) {
4118 mActions.add(action);
4119 }
Griff Hazen959591e2014-05-15 22:26:18 -07004120 return this;
4121 }
4122
4123 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004124 * Alter the complete list of actions attached to this notification.
4125 * @see #addAction(Action).
4126 *
4127 * @param actions
4128 * @return
4129 */
4130 public Builder setActions(Action... actions) {
4131 mActions.clear();
4132 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004133 if (actions[i] != null) {
4134 mActions.add(actions[i]);
4135 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004136 }
4137 return this;
4138 }
4139
4140 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004141 * Add a rich notification style to be applied at build time.
4142 *
4143 * @param style Object responsible for modifying the notification style.
4144 */
4145 public Builder setStyle(Style style) {
4146 if (mStyle != style) {
4147 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004148 if (mStyle != null) {
4149 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004150 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4151 } else {
4152 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004153 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004154 }
4155 return this;
4156 }
4157
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004158 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004159 * Returns the style set by {@link #setStyle(Style)}.
4160 */
4161 public Style getStyle() {
4162 return mStyle;
4163 }
4164
4165 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004166 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004167 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004168 * @return The same Builder.
4169 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004170 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004171 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004172 return this;
4173 }
4174
4175 /**
4176 * Supply a replacement Notification whose contents should be shown in insecure contexts
4177 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4178 * @param n A replacement notification, presumably with some or all info redacted.
4179 * @return The same Builder.
4180 */
4181 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004182 if (n != null) {
4183 mN.publicVersion = new Notification();
4184 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4185 } else {
4186 mN.publicVersion = null;
4187 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004188 return this;
4189 }
4190
Griff Hazenb720abe2014-05-20 13:15:30 -07004191 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004192 * Apply an extender to this notification builder. Extenders may be used to add
4193 * metadata or change options on this builder.
4194 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004195 public Builder extend(Extender extender) {
4196 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004197 return this;
4198 }
4199
Dan Sandler4e787062015-06-17 15:09:48 -04004200 /**
4201 * @hide
4202 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004203 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004204 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004205 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004206 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004207 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004208 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004209 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004210 }
4211
Dan Sandler26e81cf2014-05-06 10:01:27 -04004212 /**
4213 * Sets {@link Notification#color}.
4214 *
4215 * @param argb The accent color to use
4216 *
4217 * @return The same Builder.
4218 */
Tor Norbye80756e32015-03-02 09:39:27 -08004219 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004220 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004221 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004222 return this;
4223 }
4224
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004225 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004226 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4227 // This user can never be a badged profile,
4228 // and also includes USER_ALL system notifications.
4229 return null;
4230 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004231 // Note: This assumes that the current user can read the profile badge of the
4232 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004233 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004234 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004235 }
4236
4237 private Bitmap getProfileBadge() {
4238 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004239 if (badge == null) {
4240 return null;
4241 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004242 final int size = mContext.getResources().getDimensionPixelSize(
4243 R.dimen.notification_badge_size);
4244 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004245 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004246 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004247 badge.draw(canvas);
4248 return bitmap;
4249 }
4250
Selim Cinekc848c3a2016-01-13 15:27:30 -08004251 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004252 Bitmap profileBadge = getProfileBadge();
4253
Kenny Guy98193ea2014-07-24 19:54:37 +01004254 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004255 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4256 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004257 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004258 contentView.setDrawableTint(R.id.profile_badge, false,
4259 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004260 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004261 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004262 }
4263
Julia Reynoldsfc640012018-02-21 12:25:27 -05004264 /**
4265 * @hide
4266 */
4267 public boolean usesStandardHeader() {
4268 if (mN.mUsesStandardHeader) {
4269 return true;
4270 }
4271 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4272 if (mN.contentView == null && mN.bigContentView == null) {
4273 return true;
4274 }
4275 }
4276 boolean contentViewUsesHeader = mN.contentView == null
4277 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4278 boolean bigContentViewUsesHeader = mN.bigContentView == null
4279 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4280 return contentViewUsesHeader && bigContentViewUsesHeader;
4281 }
4282
Christoph Studerfe718432014-09-01 18:21:18 +02004283 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004284 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004285 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004286 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004287 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004288 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004289 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004290 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004291 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004292 }
4293
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004294 /**
4295 * Resets the notification header to its original state
4296 */
4297 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004298 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4299 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004300 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004301 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004302 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004303 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004304 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004305 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4306 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004307 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004308 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004309 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004310 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004311 contentView.setImageViewIcon(R.id.profile_badge, null);
4312 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004313 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004314 }
4315
Selim Cinek384804b2018-04-18 14:31:07 +08004316 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4317 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4318 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004319 }
4320
4321 /**
4322 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004323 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004324 */
Selim Cinek384804b2018-04-18 14:31:07 +08004325 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4326 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004327 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004328 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004329 }
4330
Selim Cinek384804b2018-04-18 14:31:07 +08004331 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4332 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004333 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004334
Christoph Studerfe718432014-09-01 18:21:18 +02004335 resetStandardTemplate(contentView);
4336
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004337 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004338 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004339 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004340 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004341 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4342 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004343 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004344 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004345 if (!p.ambient) {
4346 setTextViewColorPrimary(contentView, R.id.title);
4347 }
Selim Cinek954cc232016-05-20 13:29:23 -07004348 contentView.setViewLayoutWidth(R.id.title, showProgress
4349 ? ViewGroup.LayoutParams.WRAP_CONTENT
4350 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004351 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004352 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004353 int textId = showProgress ? com.android.internal.R.id.text_line_1
4354 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004355 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004356 if (!p.ambient) {
4357 setTextViewColorSecondary(contentView, textId);
4358 }
Selim Cinek41598732016-01-11 16:58:37 -08004359 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004360 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004361
Selim Cinek279fa862016-06-14 10:57:25 -07004362 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004363
Selim Cinek29603462015-11-17 19:04:39 -08004364 return contentView;
4365 }
4366
Selim Cinek48f66b72017-08-18 16:17:51 -07004367 private CharSequence processTextSpans(CharSequence text) {
4368 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004369 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004370 }
4371 return text;
4372 }
4373
Selim Cinek7b9605b2017-01-19 17:36:00 -08004374 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4375 ensureColors();
4376 contentView.setTextColor(id, mPrimaryTextColor);
4377 }
4378
Selim Cinek48f66b72017-08-18 16:17:51 -07004379 private boolean hasForegroundColor() {
4380 return mForegroundColor != COLOR_INVALID;
4381 }
4382
Selim Cinek389edcd2017-05-11 19:16:44 -07004383 /**
4384 * @return the primary text color
4385 * @hide
4386 */
4387 @VisibleForTesting
4388 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004389 ensureColors();
4390 return mPrimaryTextColor;
4391 }
4392
Selim Cinek389edcd2017-05-11 19:16:44 -07004393 /**
4394 * @return the secondary text color
4395 * @hide
4396 */
4397 @VisibleForTesting
4398 public int getSecondaryTextColor() {
4399 ensureColors();
4400 return mSecondaryTextColor;
4401 }
4402
Selim Cinek7b9605b2017-01-19 17:36:00 -08004403 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4404 ensureColors();
4405 contentView.setTextColor(id, mSecondaryTextColor);
4406 }
4407
4408 private void ensureColors() {
4409 int backgroundColor = getBackgroundColor();
4410 if (mPrimaryTextColor == COLOR_INVALID
4411 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004412 || mTextColorsAreForBackground != backgroundColor) {
4413 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004414 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004415 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4416 backgroundColor);
4417 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4418 backgroundColor);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004419 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Selim Cinekac5f0272017-05-02 16:05:41 -07004420 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4421 mPrimaryTextColor, backgroundColor, 4.5);
4422 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4423 mSecondaryTextColor, backgroundColor, 4.5);
4424 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004425 } else {
4426 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4427 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4428 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4429 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004430 // We only respect the given colors if worst case Black or White still has
4431 // contrast
4432 boolean backgroundLight = backLum > textLum
4433 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4434 || backLum <= textLum
4435 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004436 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004437 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004438 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4439 mForegroundColor,
4440 backgroundColor,
4441 true /* findFG */,
4442 4.5f);
4443 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004444 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004445 } else {
4446 mSecondaryTextColor =
4447 NotificationColorUtil.findContrastColorAgainstDark(
4448 mForegroundColor,
4449 backgroundColor,
4450 true /* findFG */,
4451 4.5f);
4452 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004453 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004454 }
4455 } else {
4456 mPrimaryTextColor = mForegroundColor;
4457 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004458 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4459 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004460 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4461 backgroundColor) < 4.5f) {
4462 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004463 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004464 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4465 mSecondaryTextColor,
4466 backgroundColor,
4467 true /* findFG */,
4468 4.5f);
4469 } else {
4470 mSecondaryTextColor
4471 = NotificationColorUtil.findContrastColorAgainstDark(
4472 mSecondaryTextColor,
4473 backgroundColor,
4474 true /* findFG */,
4475 4.5f);
4476 }
4477 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004478 mSecondaryTextColor, backgroundLight
4479 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4480 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004481 }
4482 }
4483 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004484 }
4485 }
4486
4487 private void updateBackgroundColor(RemoteViews contentView) {
4488 if (isColorized()) {
4489 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4490 getBackgroundColor());
4491 } else {
4492 // Clear it!
4493 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4494 0);
4495 }
4496 }
4497
Selim Cinek860b6da2015-12-16 19:02:19 -08004498 /**
4499 * @param remoteView the remote view to update the minheight in
4500 * @param hasMinHeight does it have a mimHeight
4501 * @hide
4502 */
4503 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4504 int minHeight = 0;
4505 if (hasMinHeight) {
4506 // we need to set the minHeight of the notification
4507 minHeight = mContext.getResources().getDimensionPixelSize(
4508 com.android.internal.R.dimen.notification_min_content_height);
4509 }
4510 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4511 }
4512
Selim Cinek29603462015-11-17 19:04:39 -08004513 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004514 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4515 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4516 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4517 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004518 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004519 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004520 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004521 contentView.setProgressBackgroundTintList(
4522 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4523 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004524 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004525 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004526 contentView.setProgressTintList(R.id.progress, colorStateList);
4527 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004528 }
Selim Cinek29603462015-11-17 19:04:39 -08004529 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004530 } else {
4531 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004532 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004533 }
Joe Onorato561d3852010-11-20 18:09:34 -08004534 }
4535
Selim Cinek384804b2018-04-18 14:31:07 +08004536 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4537 TemplateBindResult result) {
4538 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
4539 boolean showReplyIcon = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
4540 contentView.setViewVisibility(R.id.right_icon_container,
4541 largeIconShown || showReplyIcon ? View.VISIBLE : View.GONE);
4542 int marginEndDimen = 0;
4543 if (largeIconShown && showReplyIcon) {
4544 marginEndDimen = R.dimen.notification_content_picture_and_reply_margin;
4545 } else if (largeIconShown || showReplyIcon) {
4546 marginEndDimen = R.dimen.notification_content_picture_margin;
4547 }
4548 contentView.setViewLayoutMarginEndDimen(R.id.line1, marginEndDimen);
4549 contentView.setViewLayoutMarginEndDimen(R.id.text, marginEndDimen);
4550 contentView.setViewLayoutMarginEndDimen(R.id.progress, marginEndDimen);
4551 if (result != null) {
4552 result.setIconMarginEndDimen(marginEndDimen);
4553 }
4554 }
4555
4556 /**
4557 * Bind the large icon.
4558 * @return if the largeIcon is visible
4559 */
4560 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004561 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4562 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4563 }
Selim Cinek88188f22017-09-19 16:46:56 -07004564 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4565 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004566 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4567 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4568 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004569 }
Selim Cinek384804b2018-04-18 14:31:07 +08004570 return showLargeIcon;
4571 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004572
Selim Cinek384804b2018-04-18 14:31:07 +08004573 /**
4574 * Bind the reply icon.
4575 * @return if the reply icon is visible
4576 */
4577 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4578 boolean actionVisible = !hideReplyIcon;
4579 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004580 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004581 action = findReplyAction();
4582 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004583 }
Selim Cinek384804b2018-04-18 14:31:07 +08004584 if (actionVisible) {
4585 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4586 contentView.setDrawableTint(R.id.reply_icon_action,
4587 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004588 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004589 PorterDuff.Mode.SRC_ATOP);
4590 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4591 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4592 } else {
4593 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4594 }
4595 contentView.setViewVisibility(R.id.reply_icon_action,
4596 actionVisible ? View.VISIBLE : View.GONE);
4597 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004598 }
4599
4600 private Action findReplyAction() {
4601 ArrayList<Action> actions = mActions;
4602 if (mOriginalActions != null) {
4603 actions = mOriginalActions;
4604 }
4605 int numActions = actions.size();
4606 for (int i = 0; i < numActions; i++) {
4607 Action action = actions.get(i);
4608 if (hasValidRemoteInput(action)) {
4609 return action;
4610 }
4611 }
4612 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004613 }
4614
Selim Cinekafeed292017-12-12 17:32:44 -08004615 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4616 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004617 bindSmallIcon(contentView, ambient);
4618 bindHeaderAppName(contentView, ambient);
4619 if (!ambient) {
4620 // Ambient view does not have these
4621 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004622 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004623 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004624 bindProfileBadge(contentView);
4625 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004626 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004627 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004628 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004629 }
4630
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004631 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004632 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004633 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4634 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4635 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4636 }
4637
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004638 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004639 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004640 contentView.setDrawableTint(R.id.expand_button, false, color,
4641 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004642 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004643 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004644 }
4645
4646 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4647 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004648 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004649 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004650 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4651 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4652 contentView.setLong(R.id.chronometer, "setBase",
4653 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4654 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004655 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004656 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004657 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004658 } else {
4659 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4660 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004661 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004662 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004663 } else {
4664 // We still want a time to be set but gone, such that we can show and hide it
4665 // on demand in case it's a child notification without anything in the header
4666 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004667 }
4668 }
4669
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004670 private void bindHeaderText(RemoteViews contentView) {
4671 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4672 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004673 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004674 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004675 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004676 if (headerText == null
4677 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4678 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4679 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4680 }
4681 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004682 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004683 contentView.setTextViewText(R.id.header_text, processTextSpans(
4684 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004685 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004686 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4687 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004688 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004689 }
4690 }
4691
Selim Cinekafeed292017-12-12 17:32:44 -08004692 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4693 if (!TextUtils.isEmpty(secondaryText)) {
4694 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4695 processLegacyText(secondaryText)));
4696 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4697 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4698 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4699 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4700 }
4701 }
4702
Adrian Rooseba05822016-04-22 17:09:27 -07004703 /**
4704 * @hide
4705 */
4706 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004707 CharSequence name = null;
4708 final PackageManager pm = mContext.getPackageManager();
4709 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4710 // only system packages which lump together a bunch of unrelated stuff
4711 // may substitute a different name to make the purpose of the
4712 // notification more clear. the correct package label should always
4713 // be accessible via SystemUI.
4714 final String pkg = mContext.getPackageName();
4715 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4716 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4717 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4718 name = subName;
4719 } else {
4720 Log.w(TAG, "warning: pkg "
4721 + pkg + " attempting to substitute app name '" + subName
4722 + "' without holding perm "
4723 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4724 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004725 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004726 if (TextUtils.isEmpty(name)) {
4727 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4728 }
4729 if (TextUtils.isEmpty(name)) {
4730 // still nothing?
4731 return null;
4732 }
4733
4734 return String.valueOf(name);
4735 }
Adrian Roos487374f2017-01-11 15:48:14 -08004736 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004737 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004738 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004739 setTextViewColorPrimary(contentView, R.id.app_name_text);
4740 } else {
4741 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004742 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004743 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004744 }
4745
Adrian Roos487374f2017-01-11 15:48:14 -08004746 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004747 if (mN.mSmallIcon == null && mN.icon != 0) {
4748 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4749 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004750 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004751 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004752 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004753 }
4754
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004755 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004756 * @return true if the built notification will show the time or the chronometer; false
4757 * otherwise
4758 */
4759 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004760 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004761 }
4762
Christoph Studerfe718432014-09-01 18:21:18 +02004763 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004764 // actions_container is only reset when there are no actions to avoid focus issues with
4765 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004766 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004767 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004768
4769 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4770 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4771
4772 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4773 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4774 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4775 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004776
Selim Cineked64a142018-02-06 18:06:01 -08004777 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4778 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004779 }
4780
Selim Cinek384804b2018-04-18 14:31:07 +08004781 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4782 TemplateBindResult result) {
4783 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4784 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004785 }
4786
Adrian Roos70d7aa32017-01-11 15:39:06 -08004787 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004788 StandardTemplateParams p, TemplateBindResult result) {
4789 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004790
Christoph Studerfe718432014-09-01 18:21:18 +02004791 resetStandardTemplateWithActions(big);
4792
Adrian Roose458aa82015-12-08 16:17:19 -08004793 boolean validRemoteInput = false;
4794
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004795 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004796 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004797 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004798 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004799 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004800 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004801 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004802 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004803 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004804 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004805 boolean actionHasValidInput = hasValidRemoteInput(action);
4806 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004807
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004808 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004809 p.ambient);
4810 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004811 // Clear the drawable
4812 button.setInt(R.id.action0, "setBackgroundResource", 0);
4813 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004814 big.addView(R.id.actions, button);
4815 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004816 } else {
4817 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004818 }
Adrian Roose458aa82015-12-08 16:17:19 -08004819
4820 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004821 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004822 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4823 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004824 big.setTextViewText(R.id.notification_material_reply_text_1,
4825 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004826 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004827
4828 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4829 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004830 big.setTextViewText(R.id.notification_material_reply_text_2,
4831 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004832 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004833
4834 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4835 big.setViewVisibility(
4836 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004837 big.setTextViewText(R.id.notification_material_reply_text_3,
4838 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004839 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004840 }
4841 }
4842 }
4843
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004844 return big;
4845 }
4846
Adrian Roose458aa82015-12-08 16:17:19 -08004847 private boolean hasValidRemoteInput(Action action) {
4848 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4849 // Weird actions
4850 return false;
4851 }
4852
4853 RemoteInput[] remoteInputs = action.getRemoteInputs();
4854 if (remoteInputs == null) {
4855 return false;
4856 }
4857
4858 for (RemoteInput r : remoteInputs) {
4859 CharSequence[] choices = r.getChoices();
4860 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4861 return true;
4862 }
4863 }
4864 return false;
4865 }
4866
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004867 /**
4868 * Construct a RemoteViews for the final 1U notification layout. In order:
4869 * 1. Custom contentView from the caller
4870 * 2. Style's proposed content view
4871 * 3. Standard template view
4872 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004873 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004874 return createContentView(false /* increasedheight */ );
4875 }
4876
4877 /**
4878 * Construct a RemoteViews for the smaller content view.
4879 *
4880 * @param increasedHeight true if this layout be created with an increased height. Some
4881 * styles may support showing more then just that basic 1U size
4882 * and the system may decide to render important notifications
4883 * slightly bigger even when collapsed.
4884 *
4885 * @hide
4886 */
4887 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004888 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004889 return mN.contentView;
4890 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004891 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004892 if (styleView != null) {
4893 return styleView;
4894 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004895 }
Selim Cinek384804b2018-04-18 14:31:07 +08004896 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08004897 }
4898
Selim Cineka7679b62017-05-10 16:33:25 -07004899 private boolean useExistingRemoteView() {
4900 return mStyle == null || (!mStyle.displayCustomViewInline()
4901 && !mRebuildStyledRemoteViews);
4902 }
4903
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004904 /**
4905 * Construct a RemoteViews for the final big notification layout.
4906 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004907 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004908 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004909 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004910 return mN.bigContentView;
4911 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004912 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004913 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004914 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08004915 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
4916 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05004917 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004918 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004919 return result;
4920 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004921
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004922 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004923 * Construct a RemoteViews for the final notification header only. This will not be
4924 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004925 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004926 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004927 * @hide
4928 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004929 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004930 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4931 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004932 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004933 ambient ? R.layout.notification_template_ambient_header
4934 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004935 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004936 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004937 if (colorized != null) {
4938 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4939 } else {
4940 mN.extras.remove(EXTRA_COLORIZED);
4941 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004942 return header;
4943 }
4944
Adrian Roos487374f2017-01-11 15:48:14 -08004945 /**
4946 * Construct a RemoteViews for the ambient version of the notification.
4947 *
4948 * @hide
4949 */
4950 public RemoteViews makeAmbientNotification() {
4951 RemoteViews ambient = applyStandardTemplateWithActions(
4952 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08004953 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
4954 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08004955 return ambient;
4956 }
4957
Selim Cinek29603462015-11-17 19:04:39 -08004958 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004959 if (result != null) {
4960 result.setViewVisibility(R.id.text_line_1, View.GONE);
4961 }
Selim Cinek29603462015-11-17 19:04:39 -08004962 }
4963
Selim Cinek6743c0b2017-01-18 18:24:01 -08004964 /**
4965 * Adapt the Notification header if this view is used as an expanded view.
4966 *
4967 * @hide
4968 */
4969 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004970 if (result != null) {
4971 result.setBoolean(R.id.notification_header, "setExpanded", true);
4972 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004973 }
4974
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004975 /**
4976 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004977 *
4978 * @param increasedHeight true if this layout be created with an increased height. Some
4979 * styles may support showing more then just that basic 1U size
4980 * and the system may decide to render important notifications
4981 * slightly bigger even when collapsed.
4982 *
4983 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004984 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004985 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004986 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004987 return mN.headsUpContentView;
4988 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004989 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4990 if (styleView != null) {
4991 return styleView;
4992 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004993 } else if (mActions.size() == 0) {
4994 return null;
4995 }
4996
Selim Cinek384804b2018-04-18 14:31:07 +08004997 return applyStandardTemplateWithActions(getBigBaseLayoutResource(), null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05004998 }
4999
Selim Cinek624c02db2015-12-14 21:00:02 -08005000 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005001 * Construct a RemoteViews for the final heads-up notification layout.
5002 */
5003 public RemoteViews createHeadsUpContentView() {
5004 return createHeadsUpContentView(false /* useIncreasedHeight */);
5005 }
5006
5007 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005008 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5009 *
5010 * @hide
5011 */
5012 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005013 return makePublicView(false /* ambient */);
5014 }
5015
5016 /**
5017 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5018 *
5019 * @hide
5020 */
5021 public RemoteViews makePublicAmbientNotification() {
5022 return makePublicView(true /* ambient */);
5023 }
5024
5025 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005026 if (mN.publicVersion != null) {
5027 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005028 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005029 }
5030 Bundle savedBundle = mN.extras;
5031 Style style = mStyle;
5032 mStyle = null;
5033 Icon largeIcon = mN.mLargeIcon;
5034 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005035 Bitmap largeIconLegacy = mN.largeIcon;
5036 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005037 ArrayList<Action> actions = mActions;
5038 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005039 Bundle publicExtras = new Bundle();
5040 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5041 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5042 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5043 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005044 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5045 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08005046 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005047 RemoteViews view;
5048 if (ambient) {
5049 publicExtras.putCharSequence(EXTRA_TITLE,
5050 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5051 view = makeAmbientNotification();
5052 } else{
5053 view = makeNotificationHeader(false /* ambient */);
5054 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5055 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005056 mN.extras = savedBundle;
5057 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005058 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005059 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005060 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005061 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005062 }
5063
Selim Cinek6743c0b2017-01-18 18:24:01 -08005064 /**
5065 * Construct a content view for the display when low - priority
5066 *
5067 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5068 * a new subtext is created consisting of the content of the
5069 * notification.
5070 * @hide
5071 */
5072 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5073 int color = mN.color;
5074 mN.color = COLOR_DEFAULT;
5075 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5076 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5077 CharSequence newSummary = createSummaryText();
5078 if (!TextUtils.isEmpty(newSummary)) {
5079 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5080 }
5081 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005082
Adrian Roos6f6e1592017-05-02 16:22:53 -07005083 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005084 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005085 if (summary != null) {
5086 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5087 } else {
5088 mN.extras.remove(EXTRA_SUB_TEXT);
5089 }
5090 mN.color = color;
5091 return header;
5092 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005093
Selim Cinek6743c0b2017-01-18 18:24:01 -08005094 private CharSequence createSummaryText() {
5095 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5096 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5097 return titleText;
5098 }
5099 SpannableStringBuilder summary = new SpannableStringBuilder();
5100 if (titleText == null) {
5101 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5102 }
5103 BidiFormatter bidi = BidiFormatter.getInstance();
5104 if (titleText != null) {
5105 summary.append(bidi.unicodeWrap(titleText));
5106 }
5107 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5108 if (titleText != null && contentText != null) {
5109 summary.append(bidi.unicodeWrap(mContext.getText(
5110 R.string.notification_header_divider_symbol_with_spaces)));
5111 }
5112 if (contentText != null) {
5113 summary.append(bidi.unicodeWrap(contentText));
5114 }
5115 return summary;
5116 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005117
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005118 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005119 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005120 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005121 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005122 emphazisedMode ? getEmphasizedActionLayoutResource()
5123 : tombstone ? getActionTombstoneLayoutResource()
5124 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005125 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005126 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005127 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005128 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005129 if (action.mRemoteInputs != null) {
5130 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5131 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005132 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005133 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005134 CharSequence title = action.title;
5135 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005136 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005137 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07005138 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005139 } else {
5140 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005141 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005142 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005143 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005144 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005145 int rippleColor;
5146 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5147 if (hasColorOverride) {
5148 // There's a span spanning the full text, let's take it and use it as the
5149 // background color
5150 background = outResultColor[0].getDefaultColor();
5151 int textColor = NotificationColorUtil.resolvePrimaryColor(mContext,
5152 background);
5153 button.setTextColor(R.id.action0, textColor);
5154 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005155 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005156 rippleColor = resolveContrastColor();
5157 button.setTextColor(R.id.action0, rippleColor);
5158 } else {
5159 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005160 }
Selim Cinek396caca2018-04-10 17:46:46 -07005161 // We only want about 20% alpha for the ripple
5162 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5163 button.setColorStateList(R.id.action0, "setRippleColor",
5164 ColorStateList.valueOf(rippleColor));
5165 button.setColorStateList(R.id.action0, "setButtonBackground",
5166 ColorStateList.valueOf(background));
5167 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005168 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005169 button.setTextViewText(R.id.action0, processTextSpans(
5170 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005171 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005172 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005173 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005174 button.setTextColor(R.id.action0,
5175 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005176 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005177 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005178 return button;
5179 }
5180
Joe Onoratocb109a02011-01-18 17:57:41 -08005181 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005182 * Ensures contrast on color spans against a background color. also returns the color of the
5183 * text if a span was found that spans over the whole text.
5184 *
5185 * @param charSequence the charSequence on which the spans are
5186 * @param background the background color to ensure the contrast against
5187 * @param outResultColor an array in which a color will be returned as the first element if
5188 * there exists a full length color span.
5189 * @return the contrasted charSequence
5190 */
5191 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5192 ColorStateList[] outResultColor) {
5193 if (charSequence instanceof Spanned) {
5194 Spanned ss = (Spanned) charSequence;
5195 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5196 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5197 for (Object span : spans) {
5198 Object resultSpan = span;
5199 int spanStart = ss.getSpanStart(span);
5200 int spanEnd = ss.getSpanEnd(span);
5201 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5202 if (resultSpan instanceof CharacterStyle) {
5203 resultSpan = ((CharacterStyle) span).getUnderlying();
5204 }
5205 if (resultSpan instanceof TextAppearanceSpan) {
5206 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5207 ColorStateList textColor = originalSpan.getTextColor();
5208 if (textColor != null) {
5209 int[] colors = textColor.getColors();
5210 int[] newColors = new int[colors.length];
5211 for (int i = 0; i < newColors.length; i++) {
5212 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005213 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005214 }
5215 textColor = new ColorStateList(textColor.getStates().clone(),
5216 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005217 if (fullLength) {
5218 outResultColor[0] = textColor;
5219 // Let's drop the color from the span
5220 textColor = null;
5221 }
Selim Cinek981962e2016-07-20 20:41:58 -07005222 resultSpan = new TextAppearanceSpan(
5223 originalSpan.getFamily(),
5224 originalSpan.getTextStyle(),
5225 originalSpan.getTextSize(),
5226 textColor,
5227 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005228 }
5229 } else if (resultSpan instanceof ForegroundColorSpan) {
5230 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5231 int foregroundColor = originalSpan.getForegroundColor();
5232 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005233 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005234 if (fullLength) {
5235 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005236 resultSpan = null;
5237 } else {
5238 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005239 }
5240 } else {
5241 resultSpan = span;
5242 }
Selim Cinek396caca2018-04-10 17:46:46 -07005243 if (resultSpan != null) {
5244 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5245 }
Selim Cinek981962e2016-07-20 20:41:58 -07005246 }
5247 return builder;
5248 }
5249 return charSequence;
5250 }
5251
5252 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005253 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005254 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005255 */
5256 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005257 if (!mIsLegacyInitialized) {
5258 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5259 < Build.VERSION_CODES.LOLLIPOP;
5260 mIsLegacyInitialized = true;
5261 }
5262 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005263 }
5264
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005265 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005266 return processLegacyText(charSequence, false /* ambient */);
5267 }
5268
5269 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5270 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5271 boolean wantLightText = ambient;
5272 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005273 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005274 } else {
5275 return charSequence;
5276 }
5277 }
5278
Dan Sandler26e81cf2014-05-06 10:01:27 -04005279 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005280 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005281 */
Adrian Roos487374f2017-01-11 15:48:14 -08005282 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5283 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005284 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005285 int color;
5286 if (ambient) {
5287 color = resolveAmbientColor();
5288 } else if (isColorized()) {
5289 color = getPrimaryTextColor();
5290 } else {
5291 color = resolveContrastColor();
5292 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005293 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005294 contentView.setDrawableTint(R.id.icon, false, color,
5295 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005296
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005297 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005298 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005299 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005300 }
5301
Dan Sandler26e81cf2014-05-06 10:01:27 -04005302 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005303 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005304 * if it's grayscale).
5305 */
5306 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005307 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5308 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005309 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005310 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005311 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5312 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005313 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005314 }
5315
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005316 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005317 if (mN.color != COLOR_DEFAULT) {
5318 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005319 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005320 }
5321
Adrian Roos4ff3b122016-02-01 12:26:13 -08005322 int resolveContrastColor() {
5323 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5324 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005325 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005326
Selim Cinekac5f0272017-05-02 16:05:41 -07005327 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005328 int background = mContext.getColor(
5329 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005330 if (mN.color == COLOR_DEFAULT) {
5331 ensureColors();
Selim Cinekc7f5a822018-03-20 19:32:06 -07005332 color = NotificationColorUtil.resolveDefaultColor(mContext, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005333 } else {
5334 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005335 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005336 }
5337 if (Color.alpha(color) < 255) {
5338 // alpha doesn't go well for color filters, so let's blend it manually
5339 color = NotificationColorUtil.compositeColors(color, background);
5340 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005341 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005342 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005343 }
5344
Selim Cinek4717d862018-04-19 09:19:15 +08005345 int resolveNeutralColor() {
5346 if (mNeutralColor != COLOR_INVALID) {
5347 return mNeutralColor;
5348 }
5349 int background = mContext.getColor(
5350 com.android.internal.R.color.notification_material_background_color);
5351 mNeutralColor = NotificationColorUtil.resolveDefaultColor(mContext, background);
5352 if (Color.alpha(mNeutralColor) < 255) {
5353 // alpha doesn't go well for color filters, so let's blend it manually
5354 mNeutralColor = NotificationColorUtil.compositeColors(mNeutralColor, background);
5355 }
5356 return mNeutralColor;
5357 }
5358
Adrian Roos487374f2017-01-11 15:48:14 -08005359 int resolveAmbientColor() {
5360 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5361 return mCachedAmbientColor;
5362 }
5363 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5364
5365 mCachedAmbientColorIsFor = mN.color;
5366 return mCachedAmbientColor = contrasted;
5367 }
5368
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005369 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005370 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005371 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005372 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005373 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005374 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005375 mN.actions = new Action[mActions.size()];
5376 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005377 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005378 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005379 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005380 }
Selim Cinek247fa012016-02-18 09:50:48 -08005381 if (mN.bigContentView != null || mN.contentView != null
5382 || mN.headsUpContentView != null) {
5383 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5384 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005385 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005386 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005387
Julia Reynolds3b848122016-02-26 10:45:32 -05005388 /**
5389 * Creates a Builder from an existing notification so further changes can be made.
5390 * @param context The context for your application / activity.
5391 * @param n The notification to create a Builder from.
5392 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005393 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005394 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005395 ApplicationInfo applicationInfo = n.extras.getParcelable(
5396 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005397 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005398 if (applicationInfo != null) {
5399 try {
5400 builderContext = context.createApplicationContext(applicationInfo,
5401 Context.CONTEXT_RESTRICTED);
5402 } catch (NameNotFoundException e) {
5403 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5404 builderContext = context; // try with our context
5405 }
5406 } else {
5407 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005408 }
5409
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005410 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005411 }
5412
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005413 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005414 * @deprecated Use {@link #build()} instead.
5415 */
5416 @Deprecated
5417 public Notification getNotification() {
5418 return build();
5419 }
5420
5421 /**
5422 * Combine all of the options that have been set and return a new {@link Notification}
5423 * object.
5424 */
5425 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005426 // first, add any extras from the calling code
5427 if (mUserExtras != null) {
5428 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005429 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005430
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005431 mN.creationTime = System.currentTimeMillis();
5432
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005433 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005434 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005435
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005436 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005437
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005438 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005439 mStyle.reduceImageSizes(mContext);
5440 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005441 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005442 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005443 }
Selim Cinekd0426622017-07-11 13:19:59 +02005444 mN.reduceImageSizes(mContext);
5445
Adrian Roos5081c0d2016-02-26 16:04:19 -08005446 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005447 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005448 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005449 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005450 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5451 mN.contentView.getSequenceNumber());
5452 }
5453 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005454 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005455 if (mN.bigContentView != null) {
5456 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5457 mN.bigContentView.getSequenceNumber());
5458 }
5459 }
5460 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005461 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005462 if (mN.headsUpContentView != null) {
5463 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5464 mN.headsUpContentView.getSequenceNumber());
5465 }
5466 }
5467 }
5468
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005469 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5470 mN.flags |= FLAG_SHOW_LIGHTS;
5471 }
5472
Adrian Roosfb921842017-10-26 14:49:56 +02005473 mN.allPendingIntents = null;
5474
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005475 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005476 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005477
5478 /**
5479 * Apply this Builder to an existing {@link Notification} object.
5480 *
5481 * @hide
5482 */
5483 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005484 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005485 return n;
5486 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005487
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005488 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005489 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005490 * change. Also removes extenders on low ram devices, as
5491 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005492 *
5493 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5494 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005495 * @hide
5496 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005497 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005498 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005499
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005500 // Only strip views for known Styles because we won't know how to
5501 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005502 if (!isLowRam
5503 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005504 && getNotificationStyleClass(templateClass) == null) {
5505 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005506 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005507
5508 // Only strip unmodified BuilderRemoteViews.
5509 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005510 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005511 n.contentView.getSequenceNumber();
5512 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005513 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005514 n.bigContentView.getSequenceNumber();
5515 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005516 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005517 n.headsUpContentView.getSequenceNumber();
5518
5519 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005520 if (!isLowRam
5521 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005522 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005523 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005524
5525 Notification clone = n.clone();
5526 if (stripContentView) {
5527 clone.contentView = null;
5528 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5529 }
5530 if (stripBigContentView) {
5531 clone.bigContentView = null;
5532 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5533 }
5534 if (stripHeadsUpContentView) {
5535 clone.headsUpContentView = null;
5536 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5537 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005538 if (isLowRam) {
5539 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5540 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5541 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5542 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005543 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005544 }
5545
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005546 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005547 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005548 }
5549
5550 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005551 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005552 }
5553
5554 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005555 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005556 }
5557
5558 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005559 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005560 }
5561
5562 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005563 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005564 }
5565
Adrian Roosc1a80b02016-04-05 14:54:55 -07005566 private int getMessagingLayoutResource() {
5567 return R.layout.notification_template_material_messaging;
5568 }
5569
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005570 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005571 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005572 }
5573
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005574 private int getEmphasizedActionLayoutResource() {
5575 return R.layout.notification_material_action_emphasized;
5576 }
5577
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005578 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005579 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005580 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005581
5582 private int getBackgroundColor() {
5583 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005584 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005585 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005586 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005587 }
5588 }
5589
Selim Cinek396caca2018-04-10 17:46:46 -07005590 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005591 * Gets a neutral color that can be used for icons or similar that should not stand out.
5592 */
5593 private int getNeutralColor() {
5594 if (isColorized()) {
5595 return getSecondaryTextColor();
5596 } else {
5597 return resolveNeutralColor();
5598 }
5599 }
5600
5601 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005602 * Same as getBackgroundColor but also resolved the default color to the background.
5603 */
5604 private int resolveBackgroundColor() {
5605 int backgroundColor = getBackgroundColor();
5606 if (backgroundColor == COLOR_DEFAULT) {
5607 backgroundColor = mContext.getColor(
5608 com.android.internal.R.color.notification_material_background_color);
5609 }
5610 return backgroundColor;
5611 }
5612
Selim Cinek7b9605b2017-01-19 17:36:00 -08005613 private boolean isColorized() {
5614 return mN.isColorized();
5615 }
Selim Cinek99104832017-01-25 14:47:33 -08005616
Anthony Chenad4d1582017-04-10 16:07:58 -07005617 private boolean shouldTintActionButtons() {
5618 return mTintActionButtons;
5619 }
5620
Selim Cinek99104832017-01-25 14:47:33 -08005621 private boolean textColorsNeedInversion() {
5622 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5623 return false;
5624 }
5625 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5626 return targetSdkVersion > Build.VERSION_CODES.M
5627 && targetSdkVersion < Build.VERSION_CODES.O;
5628 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005629
5630 /**
5631 * Set a color palette to be used as the background and textColors
5632 *
5633 * @param backgroundColor the color to be used as the background
5634 * @param foregroundColor the color to be used as the foreground
5635 *
5636 * @hide
5637 */
5638 public void setColorPalette(int backgroundColor, int foregroundColor) {
5639 mBackgroundColor = backgroundColor;
5640 mForegroundColor = foregroundColor;
5641 mTextColorsAreForBackground = COLOR_INVALID;
5642 ensureColors();
5643 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005644
5645 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005646 * Forces all styled remoteViews to be built from scratch and not use any cached
5647 * RemoteViews.
5648 * This is needed for legacy apps that are baking in their remoteviews into the
5649 * notification.
5650 *
5651 * @hide
5652 */
5653 public void setRebuildStyledRemoteViews(boolean rebuild) {
5654 mRebuildStyledRemoteViews = rebuild;
5655 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005656
5657 /**
5658 * Get the text that should be displayed in the statusBar when heads upped. This is
5659 * usually just the app name, but may be different depending on the style.
5660 *
5661 * @param publicMode If true, return a text that is safe to display in public.
5662 *
5663 * @hide
5664 */
5665 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5666 if (mStyle != null && !publicMode) {
5667 CharSequence text = mStyle.getHeadsUpStatusBarText();
5668 if (!TextUtils.isEmpty(text)) {
5669 return text;
5670 }
5671 }
5672 return loadHeaderAppName();
5673 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005674 }
5675
5676 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005677 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5678 * remote views.
5679 *
5680 * @hide
5681 */
5682 void reduceImageSizes(Context context) {
5683 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5684 return;
5685 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005686 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005687 if (mLargeIcon != null || largeIcon != null) {
5688 Resources resources = context.getResources();
5689 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005690 int maxWidth = resources.getDimensionPixelSize(isLowRam
5691 ? R.dimen.notification_right_icon_size_low_ram
5692 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005693 int maxHeight = maxWidth;
5694 if (MediaStyle.class.equals(style)
5695 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005696 maxHeight = resources.getDimensionPixelSize(isLowRam
5697 ? R.dimen.notification_media_image_max_height_low_ram
5698 : R.dimen.notification_media_image_max_height);
5699 maxWidth = resources.getDimensionPixelSize(isLowRam
5700 ? R.dimen.notification_media_image_max_width_low_ram
5701 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005702 }
5703 if (mLargeIcon != null) {
5704 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5705 }
5706 if (largeIcon != null) {
5707 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5708 }
5709 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005710 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5711 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5712 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005713 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5714 }
5715
Selim Cineka8cb1262017-08-15 16:53:44 -07005716 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5717 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005718 if (remoteView != null) {
5719 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005720 int maxWidth = resources.getDimensionPixelSize(isLowRam
5721 ? R.dimen.notification_custom_view_max_image_width_low_ram
5722 : R.dimen.notification_custom_view_max_image_width);
5723 int maxHeight = resources.getDimensionPixelSize(isLowRam
5724 ? R.dimen.notification_custom_view_max_image_height_low_ram
5725 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005726 remoteView.reduceImageSizes(maxWidth, maxHeight);
5727 }
5728 }
5729
5730 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005731 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005732 */
Selim Cinek22714f12017-04-13 16:23:53 -07005733 private boolean isForegroundService() {
5734 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005735 }
5736
5737 /**
Selim Cinek99104832017-01-25 14:47:33 -08005738 * @return whether this notification has a media session attached
5739 * @hide
5740 */
5741 public boolean hasMediaSession() {
5742 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5743 }
5744
5745 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005746 * @return the style class of this notification
5747 * @hide
5748 */
5749 public Class<? extends Notification.Style> getNotificationStyle() {
5750 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5751
5752 if (!TextUtils.isEmpty(templateClass)) {
5753 return Notification.getNotificationStyleClass(templateClass);
5754 }
5755 return null;
5756 }
5757
5758 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005759 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005760 *
5761 * @hide
5762 */
5763 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005764 if (isColorizedMedia()) {
5765 return true;
5766 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005767 return extras.getBoolean(EXTRA_COLORIZED)
5768 && (hasColorizedPermission() || isForegroundService());
5769 }
5770
5771 /**
5772 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5773 * permission. The permission is checked when a notification is enqueued.
5774 */
5775 private boolean hasColorizedPermission() {
5776 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005777 }
5778
5779 /**
5780 * @return true if this notification is colorized and it is a media notification
5781 *
5782 * @hide
5783 */
5784 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005785 Class<? extends Style> style = getNotificationStyle();
5786 if (MediaStyle.class.equals(style)) {
5787 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5788 if ((colorized == null || colorized) && hasMediaSession()) {
5789 return true;
5790 }
5791 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5792 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5793 return true;
5794 }
5795 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005796 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005797 }
5798
Selim Cinek0847acd2017-04-24 19:48:29 -07005799
5800 /**
5801 * @return true if this is a media notification
5802 *
5803 * @hide
5804 */
5805 public boolean isMediaNotification() {
5806 Class<? extends Style> style = getNotificationStyle();
5807 if (MediaStyle.class.equals(style)) {
5808 return true;
5809 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5810 return true;
5811 }
5812 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005813 }
5814
Selim Cinek279fa862016-06-14 10:57:25 -07005815 private boolean hasLargeIcon() {
5816 return mLargeIcon != null || largeIcon != null;
5817 }
5818
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005819 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005820 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005821 * @hide
5822 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005823 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005824 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5825 }
5826
5827 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005828 * @return true if the notification will show a chronometer; false otherwise
5829 * @hide
5830 */
5831 public boolean showsChronometer() {
5832 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5833 }
5834
5835 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005836 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005837 */
5838 @SystemApi
5839 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5840 Class<? extends Style>[] classes = new Class[] {
5841 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5842 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5843 MessagingStyle.class };
5844 for (Class<? extends Style> innerClass : classes) {
5845 if (templateClass.equals(innerClass.getName())) {
5846 return innerClass;
5847 }
5848 }
5849 return null;
5850 }
5851
5852 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005853 * An object that can apply a rich notification style to a {@link Notification.Builder}
5854 * object.
5855 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005856 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005857 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005858
5859 /**
5860 * @hide
5861 */
5862 protected CharSequence mSummaryText = null;
5863
5864 /**
5865 * @hide
5866 */
5867 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005868
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005869 protected Builder mBuilder;
5870
Chris Wrend6297db2012-05-03 16:20:13 -04005871 /**
5872 * Overrides ContentTitle in the big form of the template.
5873 * This defaults to the value passed to setContentTitle().
5874 */
5875 protected void internalSetBigContentTitle(CharSequence title) {
5876 mBigContentTitle = title;
5877 }
5878
5879 /**
5880 * Set the first line of text after the detail section in the big form of the template.
5881 */
5882 protected void internalSetSummaryText(CharSequence cs) {
5883 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005884 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005885 }
5886
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005887 public void setBuilder(Builder builder) {
5888 if (mBuilder != builder) {
5889 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005890 if (mBuilder != null) {
5891 mBuilder.setStyle(this);
5892 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005893 }
5894 }
5895
Chris Wrend6297db2012-05-03 16:20:13 -04005896 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005897 if (mBuilder == null) {
5898 throw new IllegalArgumentException("Style requires a valid Builder object");
5899 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005900 }
Chris Wrend6297db2012-05-03 16:20:13 -04005901
5902 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08005903 return getStandardView(layoutId, null);
5904 }
5905
5906 /**
5907 * Get the standard view for this style.
5908 *
5909 * @param layoutId The layout id to use
5910 * @param result The result where template bind information is saved.
5911 * @return A remoteView for this style.
5912 * @hide
5913 */
5914 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04005915 checkBuilder();
5916
Christoph Studer4600f9b2014-07-22 22:44:43 +02005917 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005918 CharSequence oldBuilderContentTitle =
5919 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005920 if (mBigContentTitle != null) {
5921 mBuilder.setContentTitle(mBigContentTitle);
5922 }
5923
Selim Cinek384804b2018-04-18 14:31:07 +08005924 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04005925
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005926 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005927
Chris Wrend6297db2012-05-03 16:20:13 -04005928 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5929 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005930 } else {
5931 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005932 }
5933
Chris Wrend6297db2012-05-03 16:20:13 -04005934 return contentView;
5935 }
5936
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005937 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005938 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005939 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005940 *
5941 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005942 * @hide
5943 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005944 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005945 return null;
5946 }
5947
5948 /**
5949 * Construct a Style-specific RemoteViews for the final big notification layout.
5950 * @hide
5951 */
5952 public RemoteViews makeBigContentView() {
5953 return null;
5954 }
5955
5956 /**
5957 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005958 *
5959 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005960 * @hide
5961 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005962 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005963 return null;
5964 }
5965
5966 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005967 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005968 * @hide
5969 */
5970 public void addExtras(Bundle extras) {
5971 if (mSummaryTextSet) {
5972 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5973 }
5974 if (mBigContentTitle != null) {
5975 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5976 }
Chris Wren91ad5632013-06-05 15:05:57 -04005977 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005978 }
5979
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005980 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005981 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005982 * @hide
5983 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005984 protected void restoreFromExtras(Bundle extras) {
5985 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5986 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5987 mSummaryTextSet = true;
5988 }
5989 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5990 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5991 }
5992 }
5993
5994
5995 /**
5996 * @hide
5997 */
5998 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005999 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006000 return wip;
6001 }
6002
Daniel Sandler0ec46202015-06-24 01:27:05 -04006003 /**
6004 * @hide
6005 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006006 public void purgeResources() {}
6007
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006008 /**
6009 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6010 * attached to.
6011 *
6012 * @return the fully constructed Notification.
6013 */
6014 public Notification build() {
6015 checkBuilder();
6016 return mBuilder.build();
6017 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006018
6019 /**
6020 * @hide
6021 * @return true if the style positions the progress bar on the second line; false if the
6022 * style hides the progress bar
6023 */
6024 protected boolean hasProgress() {
6025 return true;
6026 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006027
6028 /**
6029 * @hide
6030 * @return Whether we should put the summary be put into the notification header
6031 */
6032 public boolean hasSummaryInHeader() {
6033 return true;
6034 }
Selim Cinek593610c2016-02-16 18:42:57 -08006035
6036 /**
6037 * @hide
6038 * @return Whether custom content views are displayed inline in the style
6039 */
6040 public boolean displayCustomViewInline() {
6041 return false;
6042 }
Selim Cinekd0426622017-07-11 13:19:59 +02006043
6044 /**
6045 * Reduces the image sizes contained in this style.
6046 *
6047 * @hide
6048 */
6049 public void reduceImageSizes(Context context) {
6050 }
Selim Cinek90343862018-02-01 11:07:11 -08006051
6052 /**
6053 * Validate that this style was properly composed. This is called at build time.
6054 * @hide
6055 */
6056 public void validate(Context context) {
6057 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006058
6059 /**
6060 * @hide
6061 */
6062 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006063
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006064 /**
6065 * @return the the text that should be displayed in the statusBar when heads-upped.
6066 * If {@code null} is returned, the default implementation will be used.
6067 *
6068 * @hide
6069 */
6070 public CharSequence getHeadsUpStatusBarText() {
6071 return null;
6072 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006073 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006074
6075 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006076 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006077 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006078 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006079 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006080 * Notification notif = new Notification.Builder(mContext)
6081 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6082 * .setContentText(subject)
6083 * .setSmallIcon(R.drawable.new_post)
6084 * .setLargeIcon(aBitmap)
6085 * .setStyle(new Notification.BigPictureStyle()
6086 * .bigPicture(aBigBitmap))
6087 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006088 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006089 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006090 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006091 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006092 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006093 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006094 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006095 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006096
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006097 public BigPictureStyle() {
6098 }
6099
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006100 /**
6101 * @deprecated use {@code BigPictureStyle()}.
6102 */
6103 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006104 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006105 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006106 }
6107
Chris Wrend6297db2012-05-03 16:20:13 -04006108 /**
6109 * Overrides ContentTitle in the big form of the template.
6110 * This defaults to the value passed to setContentTitle().
6111 */
6112 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006113 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006114 return this;
6115 }
6116
6117 /**
6118 * Set the first line of text after the detail section in the big form of the template.
6119 */
6120 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006121 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006122 return this;
6123 }
6124
Chris Wren0bd664d2012-08-01 13:56:56 -04006125 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006126 * @hide
6127 */
6128 public Bitmap getBigPicture() {
6129 return mPicture;
6130 }
6131
6132 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006133 * Provide the bitmap to be used as the payload for the BigPicture notification.
6134 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006135 public BigPictureStyle bigPicture(Bitmap b) {
6136 mPicture = b;
6137 return this;
6138 }
6139
Chris Wren3745a3d2012-05-22 15:11:52 -04006140 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006141 * Override the large icon when the big notification is shown.
6142 */
6143 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006144 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6145 }
6146
6147 /**
6148 * Override the large icon when the big notification is shown.
6149 */
6150 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006151 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006152 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006153 return this;
6154 }
6155
Riley Andrews0394a0c2015-11-03 23:36:52 -08006156 /** @hide */
6157 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6158
Daniel Sandler0ec46202015-06-24 01:27:05 -04006159 /**
6160 * @hide
6161 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006162 @Override
6163 public void purgeResources() {
6164 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006165 if (mPicture != null &&
6166 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006167 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006168 mPicture = mPicture.createAshmemBitmap();
6169 }
6170 if (mBigLargeIcon != null) {
6171 mBigLargeIcon.convertToAshmem();
6172 }
6173 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006174
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006175 /**
6176 * @hide
6177 */
Selim Cinekd0426622017-07-11 13:19:59 +02006178 @Override
6179 public void reduceImageSizes(Context context) {
6180 super.reduceImageSizes(context);
6181 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006182 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006183 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006184 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6185 ? R.dimen.notification_big_picture_max_height_low_ram
6186 : R.dimen.notification_big_picture_max_height);
6187 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6188 ? R.dimen.notification_big_picture_max_width_low_ram
6189 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006190 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6191 }
6192 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006193 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6194 ? R.dimen.notification_right_icon_size_low_ram
6195 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006196 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6197 }
6198 }
6199
6200 /**
6201 * @hide
6202 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006203 public RemoteViews makeBigContentView() {
6204 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006205 // This covers the following cases:
6206 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006207 // mN.mLargeIcon
6208 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006209 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006210 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006211 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006212 oldLargeIcon = mBuilder.mN.mLargeIcon;
6213 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006214 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6215 // replacement if the other one is null. Because we're restoring these legacy icons
6216 // for old listeners, this is in general non-null.
6217 largeIconLegacy = mBuilder.mN.largeIcon;
6218 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006219 }
6220
Selim Cinek384804b2018-04-18 14:31:07 +08006221 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6222 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006223 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006224 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6225 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006226 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006227 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006228 }
Selim Cinek279fa862016-06-14 10:57:25 -07006229 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006230
Christoph Studer5c510ee2014-12-15 16:32:27 +01006231 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006232 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006233 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006234 }
6235
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006236 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006237 return contentView;
6238 }
6239
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006240 /**
6241 * @hide
6242 */
6243 public void addExtras(Bundle extras) {
6244 super.addExtras(extras);
6245
6246 if (mBigLargeIconSet) {
6247 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6248 }
6249 extras.putParcelable(EXTRA_PICTURE, mPicture);
6250 }
6251
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006252 /**
6253 * @hide
6254 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006255 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006256 protected void restoreFromExtras(Bundle extras) {
6257 super.restoreFromExtras(extras);
6258
6259 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006260 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006261 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006262 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006263 mPicture = extras.getParcelable(EXTRA_PICTURE);
6264 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006265
6266 /**
6267 * @hide
6268 */
6269 @Override
6270 public boolean hasSummaryInHeader() {
6271 return false;
6272 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006273
6274 /**
6275 * @hide
6276 */
6277 @Override
6278 public boolean areNotificationsVisiblyDifferent(Style other) {
6279 if (other == null || getClass() != other.getClass()) {
6280 return true;
6281 }
6282 BigPictureStyle otherS = (BigPictureStyle) other;
6283 return !Objects.equals(getBigPicture(), otherS.getBigPicture());
6284 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006285 }
6286
6287 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006288 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006289 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006290 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006291 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006292 * Notification notif = new Notification.Builder(mContext)
6293 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6294 * .setContentText(subject)
6295 * .setSmallIcon(R.drawable.new_mail)
6296 * .setLargeIcon(aBitmap)
6297 * .setStyle(new Notification.BigTextStyle()
6298 * .bigText(aVeryLongString))
6299 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006300 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006301 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006302 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006303 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006304 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006305
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006306 private CharSequence mBigText;
6307
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006308 public BigTextStyle() {
6309 }
6310
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006311 /**
6312 * @deprecated use {@code BigTextStyle()}.
6313 */
6314 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006315 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006316 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006317 }
6318
Chris Wrend6297db2012-05-03 16:20:13 -04006319 /**
6320 * Overrides ContentTitle in the big form of the template.
6321 * This defaults to the value passed to setContentTitle().
6322 */
6323 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006324 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006325 return this;
6326 }
6327
6328 /**
6329 * Set the first line of text after the detail section in the big form of the template.
6330 */
6331 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006332 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006333 return this;
6334 }
6335
Chris Wren0bd664d2012-08-01 13:56:56 -04006336 /**
6337 * Provide the longer text to be displayed in the big form of the
6338 * template in place of the content text.
6339 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006340 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006341 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006342 return this;
6343 }
6344
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006345 /**
6346 * @hide
6347 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006348 public CharSequence getBigText() {
6349 return mBigText;
6350 }
6351
6352 /**
6353 * @hide
6354 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006355 public void addExtras(Bundle extras) {
6356 super.addExtras(extras);
6357
Christoph Studer4600f9b2014-07-22 22:44:43 +02006358 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6359 }
6360
6361 /**
6362 * @hide
6363 */
6364 @Override
6365 protected void restoreFromExtras(Bundle extras) {
6366 super.restoreFromExtras(extras);
6367
6368 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006369 }
6370
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006371 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006372 * @param increasedHeight true if this layout be created with an increased height.
6373 *
6374 * @hide
6375 */
6376 @Override
6377 public RemoteViews makeContentView(boolean increasedHeight) {
6378 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006379 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006380 mBuilder.mActions = new ArrayList<>();
6381 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006382 mBuilder.mActions = mBuilder.mOriginalActions;
6383 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006384 return remoteViews;
6385 }
6386 return super.makeContentView(increasedHeight);
6387 }
6388
6389 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006390 * @hide
6391 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006392 @Override
6393 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6394 if (increasedHeight && mBuilder.mActions.size() > 0) {
6395 return makeBigContentView();
6396 }
6397 return super.makeHeadsUpContentView(increasedHeight);
6398 }
6399
6400 /**
6401 * @hide
6402 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006403 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006404
6405 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006406 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006407 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006408
Selim Cinek384804b2018-04-18 14:31:07 +08006409 TemplateBindResult result = new TemplateBindResult();
6410 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
6411 contentView.setInt(R.id.big_text, "setImageEndMarginDimen", result.getIconMarginEndDimen());
Joe Malin8d40d042012-11-05 11:36:40 -08006412
Selim Cinek75998782016-04-26 10:39:17 -07006413 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006414
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006415 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006416 if (TextUtils.isEmpty(bigTextText)) {
6417 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6418 // experience
6419 bigTextText = mBuilder.processLegacyText(text);
6420 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006421 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006422
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006423 return contentView;
6424 }
6425
Julia Reynolds7217dc92018-03-07 12:12:09 -05006426 /**
6427 * @hide
6428 */
6429 @Override
6430 public boolean areNotificationsVisiblyDifferent(Style other) {
6431 if (other == null || getClass() != other.getClass()) {
6432 return true;
6433 }
6434 BigTextStyle newS = (BigTextStyle) other;
6435 return !Objects.equals(getBigText(), newS.getBigText());
6436 }
6437
Adrian Roosb1f427c2016-05-26 12:27:15 -07006438 static void applyBigTextContentView(Builder builder,
6439 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006440 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006441 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006442 contentView.setViewVisibility(R.id.big_text,
6443 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006444 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006445 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006446 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006447
6448 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006449 * Helper class for generating large-format notifications that include multiple back-and-forth
6450 * messages of varying types between any number of people.
6451 *
6452 * <br>
6453 * If the platform does not provide large-format notifications, this method has no effect. The
6454 * user will always see the normal notification view.
6455 * <br>
6456 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6457 * so:
6458 * <pre class="prettyprint">
6459 *
6460 * Notification noti = new Notification.Builder()
6461 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6462 * .setContentText(subject)
6463 * .setSmallIcon(R.drawable.new_message)
6464 * .setLargeIcon(aBitmap)
6465 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6466 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6467 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6468 * .build();
6469 * </pre>
6470 */
6471 public static class MessagingStyle extends Style {
6472
6473 /**
6474 * The maximum number of messages that will be retained in the Notification itself (the
6475 * number displayed is up to the platform).
6476 */
6477 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6478
Selim Cinekcb8b9852017-12-15 18:01:52 -08006479 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006480 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006481 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006482 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006483 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006484
6485 MessagingStyle() {
6486 }
6487
6488 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006489 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6490 * user before the posting app reposts the notification with those messages after they've
6491 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006492 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006493 *
6494 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006495 */
Alex Hillsfd590442016-10-07 09:52:44 -04006496 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006497 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006498 }
6499
6500 /**
6501 * @param user Required - The person displayed for any messages that are sent by the
6502 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6503 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006504 * by this user. The user also needs to have a valid name associated with it, which will
6505 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006506 */
6507 public MessagingStyle(@NonNull Person user) {
6508 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006509 }
6510
6511 /**
6512 * Validate that this style was properly composed. This is called at build time.
6513 * @hide
6514 */
6515 @Override
6516 public void validate(Context context) {
6517 super.validate(context);
6518 if (context.getApplicationInfo().targetSdkVersion
6519 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6520 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006521 }
6522 }
6523
6524 /**
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006525 * @return the the text that should be displayed in the statusBar when heads upped.
6526 * If {@code null} is returned, the default implementation will be used.
6527 *
6528 * @hide
6529 */
6530 @Override
6531 public CharSequence getHeadsUpStatusBarText() {
6532 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6533 ? super.mBigContentTitle
6534 : mConversationTitle;
6535 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6536 return conversationTitle;
6537 }
6538 return null;
6539 }
6540
6541 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006542 * @return the user to be displayed for any replies sent by the user
6543 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006544 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006545 public Person getUser() {
6546 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006547 }
6548
6549 /**
6550 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006551 *
6552 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006553 */
6554 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006555 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006556 }
6557
6558 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006559 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6560 *
Kodlee Yin14656422017-12-22 17:00:46 -08006561 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6562 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6563 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6564 * {@code true} and passing {@code null} will make it return {@code false}. In
6565 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6566 * to set group conversation status.
6567 *
6568 * @param conversationTitle Title displayed for this conversation
6569 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006570 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006571 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006572 mConversationTitle = conversationTitle;
6573 return this;
6574 }
6575
6576 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006577 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006578 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006579 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006580 public CharSequence getConversationTitle() {
6581 return mConversationTitle;
6582 }
6583
6584 /**
6585 * Adds a message for display by this notification. Convenience call for a simple
6586 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6587 * @param text A {@link CharSequence} to be displayed as the message content
6588 * @param timestamp Time at which the message arrived
6589 * @param sender A {@link CharSequence} to be used for displaying the name of the
6590 * sender. Should be <code>null</code> for messages by the current user, in which case
6591 * the platform will insert {@link #getUserDisplayName()}.
6592 * Should be unique amongst all individuals in the conversation, and should be
6593 * consistent during re-posts of the notification.
6594 *
6595 * @see Message#Message(CharSequence, long, CharSequence)
6596 *
6597 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006598 *
6599 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006600 */
6601 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006602 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006603 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006604 }
6605
6606 /**
6607 * Adds a message for display by this notification. Convenience call for a simple
6608 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6609 * @param text A {@link CharSequence} to be displayed as the message content
6610 * @param timestamp Time at which the message arrived
6611 * @param sender The {@link Person} who sent the message.
6612 * Should be <code>null</code> for messages by the current user, in which case
6613 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6614 *
6615 * @see Message#Message(CharSequence, long, CharSequence)
6616 *
6617 * @return this object for method chaining
6618 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006619 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6620 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006621 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006622 }
6623
6624 /**
6625 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006626 *
6627 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6628 * the newest last.
6629 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006630 * @param message The {@link Message} to be displayed
6631 * @return this object for method chaining
6632 */
6633 public MessagingStyle addMessage(Message message) {
6634 mMessages.add(message);
6635 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6636 mMessages.remove(0);
6637 }
6638 return this;
6639 }
6640
6641 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006642 * Adds a {@link Message} for historic context in this notification.
6643 *
6644 * <p>Messages should be added as historic if they are not the main subject of the
6645 * notification but may give context to a conversation. The system may choose to present
6646 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6647 *
6648 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6649 * the newest last.
6650 *
6651 * @param message The historic {@link Message} to be added
6652 * @return this object for method chaining
6653 */
6654 public MessagingStyle addHistoricMessage(Message message) {
6655 mHistoricMessages.add(message);
6656 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6657 mHistoricMessages.remove(0);
6658 }
6659 return this;
6660 }
6661
6662 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006663 * Gets the list of {@code Message} objects that represent the notification
6664 */
6665 public List<Message> getMessages() {
6666 return mMessages;
6667 }
6668
6669 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006670 * Gets the list of historic {@code Message}s in the notification.
6671 */
6672 public List<Message> getHistoricMessages() {
6673 return mHistoricMessages;
6674 }
6675
6676 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006677 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006678 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006679 * @param isGroupConversation {@code true} if the conversation represents a group,
6680 * {@code false} otherwise.
6681 * @return this object for method chaining
6682 */
6683 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6684 mIsGroupConversation = isGroupConversation;
6685 return this;
6686 }
6687
6688 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006689 * Returns {@code true} if this notification represents a group conversation, otherwise
6690 * {@code false}.
6691 *
6692 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6693 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6694 * not the conversation title is set; returning {@code true} if the conversation title is
6695 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6696 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6697 * named, non-group conversations.
6698 *
6699 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006700 */
6701 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006702 // When target SDK version is < P, a non-null conversation title dictates if this is
6703 // as group conversation.
6704 if (mBuilder != null
6705 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6706 < Build.VERSION_CODES.P) {
6707 return mConversationTitle != null;
6708 }
6709
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006710 return mIsGroupConversation;
6711 }
6712
6713 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006714 * @hide
6715 */
6716 @Override
6717 public void addExtras(Bundle extras) {
6718 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006719 if (mUser != null) {
6720 // For legacy usages
6721 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6722 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006723 }
6724 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006725 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006726 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006727 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6728 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006729 }
Adrian Roos437cd562017-01-18 15:47:03 -08006730 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6731 Message.getBundleArrayForMessages(mHistoricMessages));
6732 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006733
6734 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006735 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006736 }
6737
6738 private void fixTitleAndTextExtras(Bundle extras) {
6739 Message m = findLatestIncomingMessage();
6740 CharSequence text = (m == null) ? null : m.mText;
6741 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006742 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6743 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006744 CharSequence title;
6745 if (!TextUtils.isEmpty(mConversationTitle)) {
6746 if (!TextUtils.isEmpty(sender)) {
6747 BidiFormatter bidi = BidiFormatter.getInstance();
6748 title = mBuilder.mContext.getString(
6749 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006750 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006751 } else {
6752 title = mConversationTitle;
6753 }
6754 } else {
6755 title = sender;
6756 }
6757
6758 if (title != null) {
6759 extras.putCharSequence(EXTRA_TITLE, title);
6760 }
6761 if (text != null) {
6762 extras.putCharSequence(EXTRA_TEXT, text);
6763 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006764 }
6765
6766 /**
6767 * @hide
6768 */
6769 @Override
6770 protected void restoreFromExtras(Bundle extras) {
6771 super.restoreFromExtras(extras);
6772
Selim Cinekcb8b9852017-12-15 18:01:52 -08006773 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6774 if (mUser == null) {
6775 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07006776 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08006777 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006778 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006779 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006780 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006781 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006782 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006783 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006784 }
6785
6786 /**
6787 * @hide
6788 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006789 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006790 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006791 mBuilder.mOriginalActions = mBuilder.mActions;
6792 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006793 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006794 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006795 mBuilder.mActions = mBuilder.mOriginalActions;
6796 mBuilder.mOriginalActions = null;
6797 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006798 }
6799
Julia Reynolds7217dc92018-03-07 12:12:09 -05006800 /**
6801 * @hide
6802 */
6803 @Override
6804 public boolean areNotificationsVisiblyDifferent(Style other) {
6805 if (other == null || getClass() != other.getClass()) {
6806 return true;
6807 }
6808 MessagingStyle newS = (MessagingStyle) other;
6809 List<MessagingStyle.Message> oldMs = getMessages();
6810 List<MessagingStyle.Message> newMs = newS.getMessages();
6811
6812 if (oldMs == null) {
6813 oldMs = new ArrayList<>();
6814 }
6815 if (newMs == null) {
6816 newMs = new ArrayList<>();
6817 }
6818
6819 int n = oldMs.size();
6820 if (n != newMs.size()) {
6821 return true;
6822 }
6823 for (int i = 0; i < n; i++) {
6824 MessagingStyle.Message oldM = oldMs.get(i);
6825 MessagingStyle.Message newM = newMs.get(i);
6826 if (!Objects.equals(oldM.getText(), newM.getText())) {
6827 return true;
6828 }
6829 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
6830 return true;
6831 }
6832 CharSequence oldSender = oldM.getSenderPerson() == null ? oldM.getSender()
6833 : oldM.getSenderPerson().getName();
6834 CharSequence newSender = newM.getSenderPerson() == null ? newM.getSender()
6835 : newM.getSenderPerson().getName();
6836 if (!Objects.equals(oldSender, newSender)) {
6837 return true;
6838 }
6839
6840 String oldKey = oldM.getSenderPerson() == null
6841 ? null : oldM.getSenderPerson().getKey();
6842 String newKey = newM.getSenderPerson() == null
6843 ? null : newM.getSenderPerson().getKey();
6844 if (!Objects.equals(oldKey, newKey)) {
6845 return true;
6846 }
6847 // Other fields (like timestamp) intentionally excluded
6848 }
6849 return false;
6850 }
6851
Adrian Roosc1a80b02016-04-05 14:54:55 -07006852 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006853 return findLatestIncomingMessage(mMessages);
6854 }
6855
6856 /**
6857 * @hide
6858 */
6859 @Nullable
6860 public static Message findLatestIncomingMessage(
6861 List<Message> messages) {
6862 for (int i = messages.size() - 1; i >= 0; i--) {
6863 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006864 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006865 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006866 return m;
6867 }
6868 }
Selim Cinek88188f22017-09-19 16:46:56 -07006869 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006870 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006871 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006872 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006873 return null;
6874 }
6875
6876 /**
6877 * @hide
6878 */
6879 @Override
6880 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08006881 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08006882 }
6883
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006884 /**
6885 * Create a messaging layout.
6886 *
6887 * @param displayImagesAtEnd should images be displayed at the end of the content instead
6888 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08006889 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006890 * @return the created remoteView.
6891 */
Selim Cinekafeed292017-12-12 17:32:44 -08006892 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08006893 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07006894 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006895 ? super.mBigContentTitle
6896 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006897 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006898 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006899 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006900 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006901 nameReplacement = conversationTitle;
6902 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006903 }
Selim Cinek384804b2018-04-18 14:31:07 +08006904 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07006905 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006906 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006907 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08006908 .hideLargeIcon(hideRightIcons || isOneToOne)
6909 .hideReplyIcon(hideRightIcons)
6910 .headerTextSecondary(conversationTitle),
6911 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07006912 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006913 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08006914 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column,
6915 bindResult.getIconMarginEndDimen());
Selim Cinek88188f22017-09-19 16:46:56 -07006916 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6917 mBuilder.resolveContrastColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006918 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
6919 displayImagesAtEnd);
Selim Cinek88188f22017-09-19 16:46:56 -07006920 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6921 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006922 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6923 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006924 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6925 isOneToOne);
6926 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6927 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006928 return contentView;
6929 }
6930
Selim Cinekf7409db2017-10-24 16:17:14 -07006931 private boolean hasOnlyWhiteSpaceSenders() {
6932 for (int i = 0; i < mMessages.size(); i++) {
6933 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006934 Person sender = m.getSenderPerson();
6935 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006936 return false;
6937 }
6938 }
6939 return true;
6940 }
6941
6942 private boolean isWhiteSpace(CharSequence sender) {
6943 if (TextUtils.isEmpty(sender)) {
6944 return true;
6945 }
6946 if (sender.toString().matches("^\\s*$")) {
6947 return true;
6948 }
6949 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6950 // For the presentation that we had.
6951 for (int i = 0; i < sender.length(); i++) {
6952 char c = sender.charAt(i);
6953 if (c != '\u200B') {
6954 return false;
6955 }
6956 }
6957 return true;
6958 }
6959
Selim Cinek88188f22017-09-19 16:46:56 -07006960 private CharSequence createConversationTitleFromMessages() {
6961 ArraySet<CharSequence> names = new ArraySet<>();
6962 for (int i = 0; i < mMessages.size(); i++) {
6963 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006964 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006965 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006966 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006967 }
6968 }
6969 SpannableStringBuilder title = new SpannableStringBuilder();
6970 int size = names.size();
6971 for (int i = 0; i < size; i++) {
6972 CharSequence name = names.valueAt(i);
6973 if (!TextUtils.isEmpty(title)) {
6974 title.append(", ");
6975 }
6976 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6977 }
6978 return title;
6979 }
6980
Adrian Roosdedd1df2016-04-26 16:38:47 -07006981 /**
6982 * @hide
6983 */
6984 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006985 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006986 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006987 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006988 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6989 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006990 }
6991
Adrian Roosc1a80b02016-04-05 14:54:55 -07006992 private static TextAppearanceSpan makeFontColorSpan(int color) {
6993 return new TextAppearanceSpan(null, 0, 0,
6994 ColorStateList.valueOf(color), null);
6995 }
6996
Alex Hillsd9b04d92016-04-11 16:38:16 -04006997 public static final class Message {
6998
6999 static final String KEY_TEXT = "text";
7000 static final String KEY_TIMESTAMP = "time";
7001 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007002 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007003 static final String KEY_DATA_MIME_TYPE = "type";
7004 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007005 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04007006
7007 private final CharSequence mText;
7008 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007009 @Nullable
7010 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04007011
Shane Brennan5a871862017-03-11 13:14:17 -08007012 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007013 private String mDataMimeType;
7014 private Uri mDataUri;
7015
7016 /**
7017 * Constructor
7018 * @param text A {@link CharSequence} to be displayed as the message content
7019 * @param timestamp Time at which the message arrived
7020 * @param sender A {@link CharSequence} to be used for displaying the name of the
7021 * sender. Should be <code>null</code> for messages by the current user, in which case
7022 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7023 * Should be unique amongst all individuals in the conversation, and should be
7024 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007025 *
7026 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007027 */
7028 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007029 this(text, timestamp, sender == null ? null
7030 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007031 }
7032
7033 /**
7034 * Constructor
7035 * @param text A {@link CharSequence} to be displayed as the message content
7036 * @param timestamp Time at which the message arrived
7037 * @param sender The {@link Person} who sent the message.
7038 * Should be <code>null</code> for messages by the current user, in which case
7039 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7040 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007041 * The person provided should contain an Icon, set with
7042 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7043 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7044 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7045 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007046 * </p>
7047 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007048 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007049 mText = text;
7050 mTimestamp = timestamp;
7051 mSender = sender;
7052 }
7053
7054 /**
7055 * Sets a binary blob of data and an associated MIME type for a message. In the case
7056 * where the platform doesn't support the MIME type, the original text provided in the
7057 * constructor will be used.
7058 * @param dataMimeType The MIME type of the content. See
7059 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7060 * types on Android and Android Wear.
7061 * @param dataUri The uri containing the content whose type is given by the MIME type.
7062 * <p class="note">
7063 * <ol>
7064 * <li>Notification Listeners including the System UI need permission to access the
7065 * data the Uri points to. The recommended ways to do this are:</li>
7066 * <li>Store the data in your own ContentProvider, making sure that other apps have
7067 * the correct permission to access your provider. The preferred mechanism for
7068 * providing access is to use per-URI permissions which are temporary and only
7069 * grant access to the receiving application. An easy way to create a
7070 * ContentProvider like this is to use the FileProvider helper class.</li>
7071 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7072 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7073 * also store non-media types (see MediaStore.Files for more info). Files can be
7074 * inserted into the MediaStore using scanFile() after which a content:// style
7075 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7076 * Note that once added to the system MediaStore the content is accessible to any
7077 * app on the device.</li>
7078 * </ol>
7079 * @return this object for method chaining
7080 */
7081 public Message setData(String dataMimeType, Uri dataUri) {
7082 mDataMimeType = dataMimeType;
7083 mDataUri = dataUri;
7084 return this;
7085 }
7086
Alex Hillsfc737de2016-03-23 17:33:02 -04007087 /**
7088 * Get the text to be used for this message, or the fallback text if a type and content
7089 * Uri have been set
7090 */
7091 public CharSequence getText() {
7092 return mText;
7093 }
7094
7095 /**
7096 * Get the time at which this message arrived
7097 */
7098 public long getTimestamp() {
7099 return mTimestamp;
7100 }
7101
7102 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007103 * Get the extras Bundle for this message.
7104 */
7105 public Bundle getExtras() {
7106 return mExtras;
7107 }
7108
7109 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007110 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007111 *
7112 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007113 */
7114 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007115 return mSender == null ? null : mSender.getName();
7116 }
7117
7118 /**
7119 * Get the sender associated with this message.
7120 */
7121 @Nullable
7122 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007123 return mSender;
7124 }
7125
7126 /**
7127 * Get the MIME type of the data pointed to by the Uri
7128 */
7129 public String getDataMimeType() {
7130 return mDataMimeType;
7131 }
7132
7133 /**
7134 * Get the the Uri pointing to the content of the message. Can be null, in which case
7135 * {@see #getText()} is used.
7136 */
7137 public Uri getDataUri() {
7138 return mDataUri;
7139 }
7140
Alex Hillsd9b04d92016-04-11 16:38:16 -04007141 private Bundle toBundle() {
7142 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007143 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007144 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007145 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007146 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007147 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007148 // Legacy listeners need this
7149 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7150 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007151 }
7152 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007153 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007154 }
7155 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007156 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007157 }
Shane Brennan5a871862017-03-11 13:14:17 -08007158 if (mExtras != null) {
7159 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7160 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007161 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007162 }
7163
Alex Hillsd9b04d92016-04-11 16:38:16 -04007164 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7165 Bundle[] bundles = new Bundle[messages.size()];
7166 final int N = messages.size();
7167 for (int i = 0; i < N; i++) {
7168 bundles[i] = messages.get(i).toBundle();
7169 }
7170 return bundles;
7171 }
7172
Selim Cinek88188f22017-09-19 16:46:56 -07007173 /**
7174 * @return A list of messages read from the bundles.
7175 *
7176 * @hide
7177 */
7178 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7179 if (bundles == null) {
7180 return new ArrayList<>();
7181 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007182 List<Message> messages = new ArrayList<>(bundles.length);
7183 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007184 if (bundles[i] instanceof Bundle) {
7185 Message message = getMessageFromBundle((Bundle)bundles[i]);
7186 if (message != null) {
7187 messages.add(message);
7188 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007189 }
7190 }
7191 return messages;
7192 }
7193
7194 static Message getMessageFromBundle(Bundle bundle) {
7195 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007196 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007197 return null;
7198 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007199
7200 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7201 if (senderPerson == null) {
7202 // Legacy apps that use compat don't actually provide the sender objects
7203 // We need to fix the compat version to provide people / use
7204 // the native api instead
7205 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7206 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007207 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007208 }
7209 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007210 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007211 bundle.getLong(KEY_TIMESTAMP),
7212 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04007213 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7214 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007215 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7216 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007217 }
Shane Brennan5a871862017-03-11 13:14:17 -08007218 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7219 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7220 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007221 return message;
7222 }
7223 } catch (ClassCastException e) {
7224 return null;
7225 }
7226 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007227 }
7228 }
7229
7230 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007231 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007232 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007233 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007234 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007235 * Notification notif = new Notification.Builder(mContext)
7236 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7237 * .setContentText(subject)
7238 * .setSmallIcon(R.drawable.new_mail)
7239 * .setLargeIcon(aBitmap)
7240 * .setStyle(new Notification.InboxStyle()
7241 * .addLine(str1)
7242 * .addLine(str2)
7243 * .setContentTitle(&quot;&quot;)
7244 * .setSummaryText(&quot;+3 more&quot;))
7245 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007246 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007247 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007248 * @see Notification#bigContentView
7249 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007250 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007251 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7252
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007253 public InboxStyle() {
7254 }
7255
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007256 /**
7257 * @deprecated use {@code InboxStyle()}.
7258 */
7259 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007260 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007261 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007262 }
7263
Chris Wrend6297db2012-05-03 16:20:13 -04007264 /**
7265 * Overrides ContentTitle in the big form of the template.
7266 * This defaults to the value passed to setContentTitle().
7267 */
7268 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007269 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007270 return this;
7271 }
7272
7273 /**
7274 * Set the first line of text after the detail section in the big form of the template.
7275 */
7276 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007277 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007278 return this;
7279 }
7280
Chris Wren0bd664d2012-08-01 13:56:56 -04007281 /**
7282 * Append a line to the digest section of the Inbox notification.
7283 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007284 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007285 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007286 return this;
7287 }
7288
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007289 /**
7290 * @hide
7291 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007292 public ArrayList<CharSequence> getLines() {
7293 return mTexts;
7294 }
7295
7296 /**
7297 * @hide
7298 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007299 public void addExtras(Bundle extras) {
7300 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007301
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007302 CharSequence[] a = new CharSequence[mTexts.size()];
7303 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7304 }
7305
Christoph Studer4600f9b2014-07-22 22:44:43 +02007306 /**
7307 * @hide
7308 */
7309 @Override
7310 protected void restoreFromExtras(Bundle extras) {
7311 super.restoreFromExtras(extras);
7312
7313 mTexts.clear();
7314 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7315 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7316 }
7317 }
7318
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007319 /**
7320 * @hide
7321 */
7322 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007323 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007324 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007325 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7326 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007327
Selim Cinek384804b2018-04-18 14:31:07 +08007328 TemplateBindResult result = new TemplateBindResult();
7329 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007330
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007331 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007332
Chris Wrend6297db2012-05-03 16:20:13 -04007333 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 -04007334 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007335
Chris Wren4ed80d52012-05-17 09:30:03 -04007336 // Make sure all rows are gone in case we reuse a view.
7337 for (int rowId : rowIds) {
7338 contentView.setViewVisibility(rowId, View.GONE);
7339 }
7340
Daniel Sandler879c5e02012-04-17 16:46:51 -04007341 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007342 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7343 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007344 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007345 int onlyViewId = 0;
7346 int maxRows = rowIds.length;
7347 if (mBuilder.mActions.size() > 0) {
7348 maxRows--;
7349 }
7350 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007351 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007352 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007353 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007354 contentView.setTextViewText(rowIds[i],
7355 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007356 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007357 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007358 handleInboxImageMargin(contentView, rowIds[i], first,
7359 result.getIconMarginEndDimen());
Selim Cinek07c80172016-04-21 16:40:47 -07007360 if (first) {
7361 onlyViewId = rowIds[i];
7362 } else {
7363 onlyViewId = 0;
7364 }
Selim Cinek247fa012016-02-18 09:50:48 -08007365 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007366 }
7367 i++;
7368 }
Selim Cinek07c80172016-04-21 16:40:47 -07007369 if (onlyViewId != 0) {
7370 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7371 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7372 R.dimen.notification_text_margin_top);
7373 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7374 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007375
Daniel Sandler879c5e02012-04-17 16:46:51 -04007376 return contentView;
7377 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007378
Julia Reynolds7217dc92018-03-07 12:12:09 -05007379 /**
7380 * @hide
7381 */
7382 @Override
7383 public boolean areNotificationsVisiblyDifferent(Style other) {
7384 if (other == null || getClass() != other.getClass()) {
7385 return true;
7386 }
7387 InboxStyle newS = (InboxStyle) other;
7388 return !Objects.equals(getLines(), newS.getLines());
7389 }
7390
Selim Cinek384804b2018-04-18 14:31:07 +08007391 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
7392 int marginEndDimen) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007393 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007394 if (first) {
7395 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7396 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7397 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007398 if (!hasProgress) {
7399 endMargin = marginEndDimen;
Selim Cinek247fa012016-02-18 09:50:48 -08007400 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007401 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007402 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007403 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007404 }
Dan Sandler842dd772014-05-15 09:36:47 -04007405
7406 /**
7407 * Notification style for media playback notifications.
7408 *
7409 * In the expanded form, {@link Notification#bigContentView}, up to 5
7410 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007411 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007412 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7413 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7414 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007415 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007416 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7417 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007418 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007419 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007420 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007421 * Notifications created with MediaStyle will have their category set to
7422 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7423 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007424 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007425 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7426 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007427 * the System UI can identify this as a notification representing an active media session
7428 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7429 *
Selim Cinek99104832017-01-25 14:47:33 -08007430 * <p>
7431 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7432 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7433 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7434 * <p>
7435 *
Dan Sandler842dd772014-05-15 09:36:47 -04007436 * To use this style with your Notification, feed it to
7437 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7438 * <pre class="prettyprint">
7439 * Notification noti = new Notification.Builder()
7440 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007441 * .setContentTitle(&quot;Track title&quot;)
7442 * .setContentText(&quot;Artist - Album&quot;)
7443 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007444 * .setStyle(<b>new Notification.MediaStyle()</b>
7445 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007446 * .build();
7447 * </pre>
7448 *
7449 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007450 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007451 */
7452 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007453 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007454 static final int MAX_MEDIA_BUTTONS = 5;
7455
7456 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007457 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007458
7459 public MediaStyle() {
7460 }
7461
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007462 /**
7463 * @deprecated use {@code MediaStyle()}.
7464 */
7465 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007466 public MediaStyle(Builder builder) {
7467 setBuilder(builder);
7468 }
7469
7470 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007471 * 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 -04007472 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007473 *
7474 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007475 */
7476 public MediaStyle setShowActionsInCompactView(int...actions) {
7477 mActionsToShowInCompact = actions;
7478 return this;
7479 }
7480
7481 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007482 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7483 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007484 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007485 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007486 mToken = token;
7487 return this;
7488 }
7489
Christoph Studer4600f9b2014-07-22 22:44:43 +02007490 /**
7491 * @hide
7492 */
Dan Sandler842dd772014-05-15 09:36:47 -04007493 @Override
7494 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007495 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007496 if (wip.category == null) {
7497 wip.category = Notification.CATEGORY_TRANSPORT;
7498 }
Dan Sandler842dd772014-05-15 09:36:47 -04007499 return wip;
7500 }
7501
Christoph Studer4600f9b2014-07-22 22:44:43 +02007502 /**
7503 * @hide
7504 */
7505 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007506 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007507 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007508 }
7509
7510 /**
7511 * @hide
7512 */
7513 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007514 public RemoteViews makeBigContentView() {
7515 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007516 }
7517
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007518 /**
7519 * @hide
7520 */
7521 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007522 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007523 RemoteViews expanded = makeMediaBigContentView();
7524 return expanded != null ? expanded : makeMediaContentView();
7525 }
7526
Dan Sandler842dd772014-05-15 09:36:47 -04007527 /** @hide */
7528 @Override
7529 public void addExtras(Bundle extras) {
7530 super.addExtras(extras);
7531
7532 if (mToken != null) {
7533 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7534 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007535 if (mActionsToShowInCompact != null) {
7536 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7537 }
Dan Sandler842dd772014-05-15 09:36:47 -04007538 }
7539
Christoph Studer4600f9b2014-07-22 22:44:43 +02007540 /**
7541 * @hide
7542 */
7543 @Override
7544 protected void restoreFromExtras(Bundle extras) {
7545 super.restoreFromExtras(extras);
7546
7547 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7548 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7549 }
7550 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7551 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7552 }
7553 }
7554
Julia Reynolds7217dc92018-03-07 12:12:09 -05007555 /**
7556 * @hide
7557 */
7558 @Override
7559 public boolean areNotificationsVisiblyDifferent(Style other) {
7560 if (other == null || getClass() != other.getClass()) {
7561 return true;
7562 }
7563 // All fields to compare are on the Notification object
7564 return false;
7565 }
7566
Selim Cinek5bf069a2015-11-10 19:14:27 -05007567 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007568 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007569 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007570 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007571 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007572
7573 // If the action buttons should not be tinted, then just use the default
7574 // notification color. Otherwise, just use the passed-in color.
7575 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7576 ? color
7577 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7578 Notification.COLOR_DEFAULT);
7579
Sunny Goyal5b153922017-09-21 21:00:36 -07007580 button.setDrawableTint(R.id.action0, false, tintColor,
7581 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007582 if (!tombstone) {
7583 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7584 }
7585 button.setContentDescription(R.id.action0, action.title);
7586 return button;
7587 }
7588
7589 private RemoteViews makeMediaContentView() {
7590 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007591 R.layout.notification_template_material_media, false, /* hasProgress */
7592 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007593
7594 final int numActions = mBuilder.mActions.size();
7595 final int N = mActionsToShowInCompact == null
7596 ? 0
7597 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7598 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007599 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007600 for (int i = 0; i < N; i++) {
7601 if (i >= numActions) {
7602 throw new IllegalArgumentException(String.format(
7603 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7604 i, numActions - 1));
7605 }
7606
7607 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinekc7f5a822018-03-20 19:32:06 -07007608 final RemoteViews button = generateMediaActionButton(action, getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007609 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007610 }
7611 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007612 handleImage(view);
7613 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007614 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007615 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007616 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007617 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007618 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007619 return view;
7620 }
7621
Selim Cinekc7f5a822018-03-20 19:32:06 -07007622 private int getActionColor() {
7623 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7624 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007625 }
7626
Dan Sandler842dd772014-05-15 09:36:47 -04007627 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007628 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007629 // Dont add an expanded view if there is no more content to be revealed
7630 int actionsInCompact = mActionsToShowInCompact == null
7631 ? 0
7632 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007633 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007634 return null;
7635 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007636 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007637 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007638
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007639 if (actionCount > 0) {
7640 big.removeAllViews(com.android.internal.R.id.media_actions);
7641 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007642 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07007643 getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007644 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007645 }
7646 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007647 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007648 return big;
7649 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007650
Selim Cinek5bf069a2015-11-10 19:14:27 -05007651 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007652 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007653 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7654 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007655 }
7656 }
7657
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007658 /**
7659 * @hide
7660 */
7661 @Override
7662 protected boolean hasProgress() {
7663 return false;
7664 }
Dan Sandler842dd772014-05-15 09:36:47 -04007665 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007666
Selim Cinek593610c2016-02-16 18:42:57 -08007667 /**
7668 * Notification style for custom views that are decorated by the system
7669 *
7670 * <p>Instead of providing a notification that is completely custom, a developer can set this
7671 * style and still obtain system decorations like the notification header with the expand
7672 * affordance and actions.
7673 *
7674 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7675 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7676 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7677 * corresponding custom views to display.
7678 *
7679 * To use this style with your Notification, feed it to
7680 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7681 * <pre class="prettyprint">
7682 * Notification noti = new Notification.Builder()
7683 * .setSmallIcon(R.drawable.ic_stat_player)
7684 * .setLargeIcon(albumArtBitmap))
7685 * .setCustomContentView(contentView);
7686 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7687 * .build();
7688 * </pre>
7689 */
7690 public static class DecoratedCustomViewStyle extends Style {
7691
7692 public DecoratedCustomViewStyle() {
7693 }
7694
Selim Cinek593610c2016-02-16 18:42:57 -08007695 /**
7696 * @hide
7697 */
7698 public boolean displayCustomViewInline() {
7699 return true;
7700 }
7701
7702 /**
7703 * @hide
7704 */
7705 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007706 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007707 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7708 }
7709
7710 /**
7711 * @hide
7712 */
7713 @Override
7714 public RemoteViews makeBigContentView() {
7715 return makeDecoratedBigContentView();
7716 }
7717
7718 /**
7719 * @hide
7720 */
7721 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007722 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007723 return makeDecoratedHeadsUpContentView();
7724 }
7725
Selim Cinek593610c2016-02-16 18:42:57 -08007726 private RemoteViews makeDecoratedHeadsUpContentView() {
7727 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7728 ? mBuilder.mN.contentView
7729 : mBuilder.mN.headsUpContentView;
7730 if (mBuilder.mActions.size() == 0) {
7731 return makeStandardTemplateWithCustomContent(headsUpContentView);
7732 }
Selim Cinek384804b2018-04-18 14:31:07 +08007733 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08007734 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08007735 mBuilder.getBigBaseLayoutResource(), result);
7736 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08007737 return remoteViews;
7738 }
7739
Selim Cinek593610c2016-02-16 18:42:57 -08007740 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08007741 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08007742 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007743 mBuilder.getBaseLayoutResource(), result);
7744 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08007745 return remoteViews;
7746 }
7747
Selim Cinek593610c2016-02-16 18:42:57 -08007748 private RemoteViews makeDecoratedBigContentView() {
7749 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7750 ? mBuilder.mN.contentView
7751 : mBuilder.mN.bigContentView;
7752 if (mBuilder.mActions.size() == 0) {
7753 return makeStandardTemplateWithCustomContent(bigContentView);
7754 }
Selim Cinek384804b2018-04-18 14:31:07 +08007755 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08007756 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08007757 mBuilder.getBigBaseLayoutResource(), result);
7758 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08007759 return remoteViews;
7760 }
Selim Cinek247fa012016-02-18 09:50:48 -08007761
7762 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08007763 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007764 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007765 // Need to clone customContent before adding, because otherwise it can no longer be
7766 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007767 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007768 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7769 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007770 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007771 }
Selim Cinek247fa012016-02-18 09:50:48 -08007772 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08007773 Resources resources = mBuilder.mContext.getResources();
7774 int endMargin = resources.getDimensionPixelSize(
7775 R.dimen.notification_content_margin_end);
7776 int marginEndDimen = result.getIconMarginEndDimen();
7777 if (marginEndDimen != 0) {
7778 endMargin += resources.getDimensionPixelSize(marginEndDimen);
Selim Cinek247fa012016-02-18 09:50:48 -08007779 }
Selim Cinek384804b2018-04-18 14:31:07 +08007780 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007781 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05007782
7783 /**
7784 * @hide
7785 */
7786 @Override
7787 public boolean areNotificationsVisiblyDifferent(Style other) {
7788 if (other == null || getClass() != other.getClass()) {
7789 return true;
7790 }
7791 // Comparison done for all custom RemoteViews, independent of style
7792 return false;
7793 }
Selim Cinek593610c2016-02-16 18:42:57 -08007794 }
7795
Selim Cinek03eb3b72016-02-18 10:39:45 -08007796 /**
7797 * Notification style for media custom views that are decorated by the system
7798 *
7799 * <p>Instead of providing a media notification that is completely custom, a developer can set
7800 * this style and still obtain system decorations like the notification header with the expand
7801 * affordance and actions.
7802 *
7803 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7804 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7805 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7806 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007807 * <p>
7808 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7809 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7810 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007811 * To use this style with your Notification, feed it to
7812 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7813 * <pre class="prettyprint">
7814 * Notification noti = new Notification.Builder()
7815 * .setSmallIcon(R.drawable.ic_stat_player)
7816 * .setLargeIcon(albumArtBitmap))
7817 * .setCustomContentView(contentView);
7818 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7819 * .setMediaSession(mySession))
7820 * .build();
7821 * </pre>
7822 *
7823 * @see android.app.Notification.DecoratedCustomViewStyle
7824 * @see android.app.Notification.MediaStyle
7825 */
7826 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7827
7828 public DecoratedMediaCustomViewStyle() {
7829 }
7830
Selim Cinek03eb3b72016-02-18 10:39:45 -08007831 /**
7832 * @hide
7833 */
7834 public boolean displayCustomViewInline() {
7835 return true;
7836 }
7837
7838 /**
7839 * @hide
7840 */
7841 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007842 public RemoteViews makeContentView(boolean increasedHeight) {
7843 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007844 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7845 mBuilder.mN.contentView);
7846 }
7847
7848 /**
7849 * @hide
7850 */
7851 @Override
7852 public RemoteViews makeBigContentView() {
7853 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7854 ? mBuilder.mN.bigContentView
7855 : mBuilder.mN.contentView;
7856 return makeBigContentViewWithCustomContent(customRemoteView);
7857 }
7858
7859 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7860 RemoteViews remoteViews = super.makeBigContentView();
7861 if (remoteViews != null) {
7862 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7863 customRemoteView);
7864 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007865 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007866 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7867 customRemoteView);
7868 } else {
7869 return null;
7870 }
7871 }
7872
7873 /**
7874 * @hide
7875 */
7876 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007877 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007878 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7879 ? mBuilder.mN.headsUpContentView
7880 : mBuilder.mN.contentView;
7881 return makeBigContentViewWithCustomContent(customRemoteView);
7882 }
7883
Julia Reynolds7217dc92018-03-07 12:12:09 -05007884 /**
7885 * @hide
7886 */
7887 @Override
7888 public boolean areNotificationsVisiblyDifferent(Style other) {
7889 if (other == null || getClass() != other.getClass()) {
7890 return true;
7891 }
7892 // Comparison done for all custom RemoteViews, independent of style
7893 return false;
7894 }
7895
Selim Cinek03eb3b72016-02-18 10:39:45 -08007896 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7897 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007898 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007899 // Need to clone customContent before adding, because otherwise it can no longer be
7900 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007901 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007902 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007903 remoteViews.removeAllViews(id);
7904 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007905 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007906 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007907 return remoteViews;
7908 }
7909 }
7910
Christoph Studer4600f9b2014-07-22 22:44:43 +02007911 // When adding a new Style subclass here, don't forget to update
7912 // Builder.getNotificationStyleClass.
7913
Griff Hazen61a9e862014-05-22 16:05:19 -07007914 /**
7915 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7916 * metadata or change options on a notification builder.
7917 */
7918 public interface Extender {
7919 /**
7920 * Apply this extender to a notification builder.
7921 * @param builder the builder to be modified.
7922 * @return the build object for chaining.
7923 */
7924 public Builder extend(Builder builder);
7925 }
7926
7927 /**
7928 * Helper class to add wearable extensions to notifications.
7929 * <p class="note"> See
7930 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7931 * for Android Wear</a> for more information on how to use this class.
7932 * <p>
7933 * To create a notification with wearable extensions:
7934 * <ol>
7935 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7936 * properties.
7937 * <li>Create a {@link android.app.Notification.WearableExtender}.
7938 * <li>Set wearable-specific properties using the
7939 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7940 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7941 * notification.
7942 * <li>Post the notification to the notification system with the
7943 * {@code NotificationManager.notify(...)} methods.
7944 * </ol>
7945 *
7946 * <pre class="prettyprint">
7947 * Notification notif = new Notification.Builder(mContext)
7948 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7949 * .setContentText(subject)
7950 * .setSmallIcon(R.drawable.new_mail)
7951 * .extend(new Notification.WearableExtender()
7952 * .setContentIcon(R.drawable.new_mail))
7953 * .build();
7954 * NotificationManager notificationManger =
7955 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7956 * notificationManger.notify(0, notif);</pre>
7957 *
7958 * <p>Wearable extensions can be accessed on an existing notification by using the
7959 * {@code WearableExtender(Notification)} constructor,
7960 * and then using the {@code get} methods to access values.
7961 *
7962 * <pre class="prettyprint">
7963 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7964 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007965 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007966 */
7967 public static final class WearableExtender implements Extender {
7968 /**
7969 * Sentinel value for an action index that is unset.
7970 */
7971 public static final int UNSET_ACTION_INDEX = -1;
7972
7973 /**
7974 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7975 * default sizing.
7976 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007977 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007978 * on their content.
7979 */
7980 public static final int SIZE_DEFAULT = 0;
7981
7982 /**
7983 * Size value for use with {@link #setCustomSizePreset} to show this notification
7984 * with an extra small size.
7985 * <p>This value is only applicable for custom display notifications created using
7986 * {@link #setDisplayIntent}.
7987 */
7988 public static final int SIZE_XSMALL = 1;
7989
7990 /**
7991 * Size value for use with {@link #setCustomSizePreset} to show this notification
7992 * with a small size.
7993 * <p>This value is only applicable for custom display notifications created using
7994 * {@link #setDisplayIntent}.
7995 */
7996 public static final int SIZE_SMALL = 2;
7997
7998 /**
7999 * Size value for use with {@link #setCustomSizePreset} to show this notification
8000 * with a medium size.
8001 * <p>This value is only applicable for custom display notifications created using
8002 * {@link #setDisplayIntent}.
8003 */
8004 public static final int SIZE_MEDIUM = 3;
8005
8006 /**
8007 * Size value for use with {@link #setCustomSizePreset} to show this notification
8008 * with a large size.
8009 * <p>This value is only applicable for custom display notifications created using
8010 * {@link #setDisplayIntent}.
8011 */
8012 public static final int SIZE_LARGE = 4;
8013
Griff Hazend5f11f92014-05-27 15:40:09 -07008014 /**
8015 * Size value for use with {@link #setCustomSizePreset} to show this notification
8016 * full screen.
8017 * <p>This value is only applicable for custom display notifications created using
8018 * {@link #setDisplayIntent}.
8019 */
8020 public static final int SIZE_FULL_SCREEN = 5;
8021
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008022 /**
8023 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8024 * short amount of time when this notification is displayed on the screen. This
8025 * is the default value.
8026 */
8027 public static final int SCREEN_TIMEOUT_SHORT = 0;
8028
8029 /**
8030 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8031 * for a longer amount of time when this notification is displayed on the screen.
8032 */
8033 public static final int SCREEN_TIMEOUT_LONG = -1;
8034
Griff Hazen61a9e862014-05-22 16:05:19 -07008035 /** Notification extra which contains wearable extensions */
8036 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8037
Pete Gastaf6781d2014-10-07 15:17:05 -04008038 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008039 private static final String KEY_ACTIONS = "actions";
8040 private static final String KEY_FLAGS = "flags";
8041 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8042 private static final String KEY_PAGES = "pages";
8043 private static final String KEY_BACKGROUND = "background";
8044 private static final String KEY_CONTENT_ICON = "contentIcon";
8045 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8046 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8047 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8048 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8049 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008050 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008051 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008052 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008053
8054 // Flags bitwise-ored to mFlags
8055 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8056 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8057 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8058 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008059 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008060 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008061 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008062
8063 // Default value for flags integer
8064 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8065
8066 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8067 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8068
8069 private ArrayList<Action> mActions = new ArrayList<Action>();
8070 private int mFlags = DEFAULT_FLAGS;
8071 private PendingIntent mDisplayIntent;
8072 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8073 private Bitmap mBackground;
8074 private int mContentIcon;
8075 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8076 private int mContentActionIndex = UNSET_ACTION_INDEX;
8077 private int mCustomSizePreset = SIZE_DEFAULT;
8078 private int mCustomContentHeight;
8079 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008080 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008081 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008082 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008083
8084 /**
8085 * Create a {@link android.app.Notification.WearableExtender} with default
8086 * options.
8087 */
8088 public WearableExtender() {
8089 }
8090
8091 public WearableExtender(Notification notif) {
8092 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8093 if (wearableBundle != null) {
8094 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8095 if (actions != null) {
8096 mActions.addAll(actions);
8097 }
8098
8099 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8100 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8101
8102 Notification[] pages = getNotificationArrayFromBundle(
8103 wearableBundle, KEY_PAGES);
8104 if (pages != null) {
8105 Collections.addAll(mPages, pages);
8106 }
8107
8108 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8109 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8110 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8111 DEFAULT_CONTENT_ICON_GRAVITY);
8112 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8113 UNSET_ACTION_INDEX);
8114 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8115 SIZE_DEFAULT);
8116 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8117 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008118 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008119 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008120 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008121 }
8122 }
8123
8124 /**
8125 * Apply wearable extensions to a notification that is being built. This is typically
8126 * called by the {@link android.app.Notification.Builder#extend} method of
8127 * {@link android.app.Notification.Builder}.
8128 */
8129 @Override
8130 public Notification.Builder extend(Notification.Builder builder) {
8131 Bundle wearableBundle = new Bundle();
8132
8133 if (!mActions.isEmpty()) {
8134 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8135 }
8136 if (mFlags != DEFAULT_FLAGS) {
8137 wearableBundle.putInt(KEY_FLAGS, mFlags);
8138 }
8139 if (mDisplayIntent != null) {
8140 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8141 }
8142 if (!mPages.isEmpty()) {
8143 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8144 new Notification[mPages.size()]));
8145 }
8146 if (mBackground != null) {
8147 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8148 }
8149 if (mContentIcon != 0) {
8150 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8151 }
8152 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8153 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8154 }
8155 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8156 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8157 mContentActionIndex);
8158 }
8159 if (mCustomSizePreset != SIZE_DEFAULT) {
8160 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8161 }
8162 if (mCustomContentHeight != 0) {
8163 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8164 }
8165 if (mGravity != DEFAULT_GRAVITY) {
8166 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8167 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008168 if (mHintScreenTimeout != 0) {
8169 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8170 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008171 if (mDismissalId != null) {
8172 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8173 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008174 if (mBridgeTag != null) {
8175 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8176 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008177
8178 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8179 return builder;
8180 }
8181
8182 @Override
8183 public WearableExtender clone() {
8184 WearableExtender that = new WearableExtender();
8185 that.mActions = new ArrayList<Action>(this.mActions);
8186 that.mFlags = this.mFlags;
8187 that.mDisplayIntent = this.mDisplayIntent;
8188 that.mPages = new ArrayList<Notification>(this.mPages);
8189 that.mBackground = this.mBackground;
8190 that.mContentIcon = this.mContentIcon;
8191 that.mContentIconGravity = this.mContentIconGravity;
8192 that.mContentActionIndex = this.mContentActionIndex;
8193 that.mCustomSizePreset = this.mCustomSizePreset;
8194 that.mCustomContentHeight = this.mCustomContentHeight;
8195 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008196 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008197 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008198 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008199 return that;
8200 }
8201
8202 /**
8203 * Add a wearable action to this notification.
8204 *
8205 * <p>When wearable actions are added using this method, the set of actions that
8206 * show on a wearable device splits from devices that only show actions added
8207 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8208 * of which actions display on different devices.
8209 *
8210 * @param action the action to add to this notification
8211 * @return this object for method chaining
8212 * @see android.app.Notification.Action
8213 */
8214 public WearableExtender addAction(Action action) {
8215 mActions.add(action);
8216 return this;
8217 }
8218
8219 /**
8220 * Adds wearable actions to this notification.
8221 *
8222 * <p>When wearable actions are added using this method, the set of actions that
8223 * show on a wearable device splits from devices that only show actions added
8224 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8225 * of which actions display on different devices.
8226 *
8227 * @param actions the actions to add to this notification
8228 * @return this object for method chaining
8229 * @see android.app.Notification.Action
8230 */
8231 public WearableExtender addActions(List<Action> actions) {
8232 mActions.addAll(actions);
8233 return this;
8234 }
8235
8236 /**
8237 * Clear all wearable actions present on this builder.
8238 * @return this object for method chaining.
8239 * @see #addAction
8240 */
8241 public WearableExtender clearActions() {
8242 mActions.clear();
8243 return this;
8244 }
8245
8246 /**
8247 * Get the wearable actions present on this notification.
8248 */
8249 public List<Action> getActions() {
8250 return mActions;
8251 }
8252
8253 /**
8254 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008255 * this notification. The {@link PendingIntent} provided should be for an activity.
8256 *
8257 * <pre class="prettyprint">
8258 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8259 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8260 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8261 * Notification notif = new Notification.Builder(context)
8262 * .extend(new Notification.WearableExtender()
8263 * .setDisplayIntent(displayPendingIntent)
8264 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8265 * .build();</pre>
8266 *
8267 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008268 * should have an empty task affinity. It is also recommended to use the device
8269 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008270 *
8271 * <p>Example AndroidManifest.xml entry:
8272 * <pre class="prettyprint">
8273 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8274 * android:exported=&quot;true&quot;
8275 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008276 * android:taskAffinity=&quot;&quot;
8277 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008278 *
8279 * @param intent the {@link PendingIntent} for an activity
8280 * @return this object for method chaining
8281 * @see android.app.Notification.WearableExtender#getDisplayIntent
8282 */
8283 public WearableExtender setDisplayIntent(PendingIntent intent) {
8284 mDisplayIntent = intent;
8285 return this;
8286 }
8287
8288 /**
8289 * Get the intent to launch inside of an activity view when displaying this
8290 * notification. This {@code PendingIntent} should be for an activity.
8291 */
8292 public PendingIntent getDisplayIntent() {
8293 return mDisplayIntent;
8294 }
8295
8296 /**
8297 * Add an additional page of content to display with this notification. The current
8298 * notification forms the first page, and pages added using this function form
8299 * subsequent pages. This field can be used to separate a notification into multiple
8300 * sections.
8301 *
8302 * @param page the notification to add as another page
8303 * @return this object for method chaining
8304 * @see android.app.Notification.WearableExtender#getPages
8305 */
8306 public WearableExtender addPage(Notification page) {
8307 mPages.add(page);
8308 return this;
8309 }
8310
8311 /**
8312 * Add additional pages of content to display with this notification. The current
8313 * notification forms the first page, and pages added using this function form
8314 * subsequent pages. This field can be used to separate a notification into multiple
8315 * sections.
8316 *
8317 * @param pages a list of notifications
8318 * @return this object for method chaining
8319 * @see android.app.Notification.WearableExtender#getPages
8320 */
8321 public WearableExtender addPages(List<Notification> pages) {
8322 mPages.addAll(pages);
8323 return this;
8324 }
8325
8326 /**
8327 * Clear all additional pages present on this builder.
8328 * @return this object for method chaining.
8329 * @see #addPage
8330 */
8331 public WearableExtender clearPages() {
8332 mPages.clear();
8333 return this;
8334 }
8335
8336 /**
8337 * Get the array of additional pages of content for displaying this notification. The
8338 * current notification forms the first page, and elements within this array form
8339 * subsequent pages. This field can be used to separate a notification into multiple
8340 * sections.
8341 * @return the pages for this notification
8342 */
8343 public List<Notification> getPages() {
8344 return mPages;
8345 }
8346
8347 /**
8348 * Set a background image to be displayed behind the notification content.
8349 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8350 * will work with any notification style.
8351 *
8352 * @param background the background bitmap
8353 * @return this object for method chaining
8354 * @see android.app.Notification.WearableExtender#getBackground
8355 */
8356 public WearableExtender setBackground(Bitmap background) {
8357 mBackground = background;
8358 return this;
8359 }
8360
8361 /**
8362 * Get a background image to be displayed behind the notification content.
8363 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8364 * will work with any notification style.
8365 *
8366 * @return the background image
8367 * @see android.app.Notification.WearableExtender#setBackground
8368 */
8369 public Bitmap getBackground() {
8370 return mBackground;
8371 }
8372
8373 /**
8374 * Set an icon that goes with the content of this notification.
8375 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008376 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008377 public WearableExtender setContentIcon(int icon) {
8378 mContentIcon = icon;
8379 return this;
8380 }
8381
8382 /**
8383 * Get an icon that goes with the content of this notification.
8384 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008385 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008386 public int getContentIcon() {
8387 return mContentIcon;
8388 }
8389
8390 /**
8391 * Set the gravity that the content icon should have within the notification display.
8392 * Supported values include {@link android.view.Gravity#START} and
8393 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8394 * @see #setContentIcon
8395 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008396 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008397 public WearableExtender setContentIconGravity(int contentIconGravity) {
8398 mContentIconGravity = contentIconGravity;
8399 return this;
8400 }
8401
8402 /**
8403 * Get the gravity that the content icon should have within the notification display.
8404 * Supported values include {@link android.view.Gravity#START} and
8405 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8406 * @see #getContentIcon
8407 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008408 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008409 public int getContentIconGravity() {
8410 return mContentIconGravity;
8411 }
8412
8413 /**
8414 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008415 * this notification. This action will no longer display separately from the
8416 * notification's content.
8417 *
Griff Hazenca48d352014-05-28 22:37:13 -07008418 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008419 * set, although the list of available actions comes from the main notification and not
8420 * from the child page's notification.
8421 *
8422 * @param actionIndex The index of the action to hoist onto the current notification page.
8423 * If wearable actions were added to the main notification, this index
8424 * will apply to that list, otherwise it will apply to the regular
8425 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008426 */
8427 public WearableExtender setContentAction(int actionIndex) {
8428 mContentActionIndex = actionIndex;
8429 return this;
8430 }
8431
8432 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008433 * Get the index of the notification action, if any, that was specified as being clickable
8434 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008435 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008436 *
Griff Hazenca48d352014-05-28 22:37:13 -07008437 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008438 * set, although the list of available actions comes from the main notification and not
8439 * from the child page's notification.
8440 *
8441 * <p>If wearable specific actions were added to the main notification, this index will
8442 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008443 *
8444 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008445 */
8446 public int getContentAction() {
8447 return mContentActionIndex;
8448 }
8449
8450 /**
8451 * Set the gravity that this notification should have within the available viewport space.
8452 * Supported values include {@link android.view.Gravity#TOP},
8453 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8454 * The default value is {@link android.view.Gravity#BOTTOM}.
8455 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008456 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008457 public WearableExtender setGravity(int gravity) {
8458 mGravity = gravity;
8459 return this;
8460 }
8461
8462 /**
8463 * Get the gravity that this notification should have within the available viewport space.
8464 * Supported values include {@link android.view.Gravity#TOP},
8465 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8466 * The default value is {@link android.view.Gravity#BOTTOM}.
8467 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008468 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008469 public int getGravity() {
8470 return mGravity;
8471 }
8472
8473 /**
8474 * Set the custom size preset for the display of this notification out of the available
8475 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8476 * {@link #SIZE_LARGE}.
8477 * <p>Some custom size presets are only applicable for custom display notifications created
8478 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8479 * documentation for the preset in question. See also
8480 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8481 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008482 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008483 public WearableExtender setCustomSizePreset(int sizePreset) {
8484 mCustomSizePreset = sizePreset;
8485 return this;
8486 }
8487
8488 /**
8489 * Get the custom size preset for the display of this notification out of the available
8490 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8491 * {@link #SIZE_LARGE}.
8492 * <p>Some custom size presets are only applicable for custom display notifications created
8493 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8494 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8495 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008496 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008497 public int getCustomSizePreset() {
8498 return mCustomSizePreset;
8499 }
8500
8501 /**
8502 * Set the custom height in pixels for the display of this notification's content.
8503 * <p>This option is only available for custom display notifications created
8504 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8505 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8506 * {@link #getCustomContentHeight}.
8507 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008508 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008509 public WearableExtender setCustomContentHeight(int height) {
8510 mCustomContentHeight = height;
8511 return this;
8512 }
8513
8514 /**
8515 * Get the custom height in pixels for the display of this notification's content.
8516 * <p>This option is only available for custom display notifications created
8517 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8518 * {@link #setCustomContentHeight}.
8519 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008520 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008521 public int getCustomContentHeight() {
8522 return mCustomContentHeight;
8523 }
8524
8525 /**
8526 * Set whether the scrolling position for the contents of this notification should start
8527 * at the bottom of the contents instead of the top when the contents are too long to
8528 * display within the screen. Default is false (start scroll at the top).
8529 */
8530 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8531 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8532 return this;
8533 }
8534
8535 /**
8536 * Get whether the scrolling position for the contents of this notification should start
8537 * at the bottom of the contents instead of the top when the contents are too long to
8538 * display within the screen. Default is false (start scroll at the top).
8539 */
8540 public boolean getStartScrollBottom() {
8541 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8542 }
8543
8544 /**
8545 * Set whether the content intent is available when the wearable device is not connected
8546 * to a companion device. The user can still trigger this intent when the wearable device
8547 * is offline, but a visual hint will indicate that the content intent may not be available.
8548 * Defaults to true.
8549 */
8550 public WearableExtender setContentIntentAvailableOffline(
8551 boolean contentIntentAvailableOffline) {
8552 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8553 return this;
8554 }
8555
8556 /**
8557 * Get whether the content intent is available when the wearable device is not connected
8558 * to a companion device. The user can still trigger this intent when the wearable device
8559 * is offline, but a visual hint will indicate that the content intent may not be available.
8560 * Defaults to true.
8561 */
8562 public boolean getContentIntentAvailableOffline() {
8563 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8564 }
8565
8566 /**
8567 * Set a hint that this notification's icon should not be displayed.
8568 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8569 * @return this object for method chaining
8570 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008571 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008572 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8573 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8574 return this;
8575 }
8576
8577 /**
8578 * Get a hint that this notification's icon should not be displayed.
8579 * @return {@code true} if this icon should not be displayed, false otherwise.
8580 * The default value is {@code false} if this was never set.
8581 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008582 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008583 public boolean getHintHideIcon() {
8584 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8585 }
8586
8587 /**
8588 * Set a visual hint that only the background image of this notification should be
8589 * displayed, and other semantic content should be hidden. This hint is only applicable
8590 * to sub-pages added using {@link #addPage}.
8591 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008592 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008593 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8594 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8595 return this;
8596 }
8597
8598 /**
8599 * Get a visual hint that only the background image of this notification should be
8600 * displayed, and other semantic content should be hidden. This hint is only applicable
8601 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8602 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008603 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008604 public boolean getHintShowBackgroundOnly() {
8605 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8606 }
8607
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008608 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008609 * Set a hint that this notification's background should not be clipped if possible,
8610 * and should instead be resized to fully display on the screen, retaining the aspect
8611 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008612 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8613 * @return this object for method chaining
8614 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008615 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008616 public WearableExtender setHintAvoidBackgroundClipping(
8617 boolean hintAvoidBackgroundClipping) {
8618 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8619 return this;
8620 }
8621
8622 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008623 * Get a hint that this notification's background should not be clipped if possible,
8624 * and should instead be resized to fully display on the screen, retaining the aspect
8625 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008626 * @return {@code true} if it's ok if the background is clipped on the screen, false
8627 * otherwise. The default value is {@code false} if this was never set.
8628 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008629 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008630 public boolean getHintAvoidBackgroundClipping() {
8631 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8632 }
8633
8634 /**
8635 * Set a hint that the screen should remain on for at least this duration when
8636 * this notification is displayed on the screen.
8637 * @param timeout The requested screen timeout in milliseconds. Can also be either
8638 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8639 * @return this object for method chaining
8640 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008641 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008642 public WearableExtender setHintScreenTimeout(int timeout) {
8643 mHintScreenTimeout = timeout;
8644 return this;
8645 }
8646
8647 /**
8648 * Get the duration, in milliseconds, that the screen should remain on for
8649 * when this notification is displayed.
8650 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8651 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8652 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008653 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008654 public int getHintScreenTimeout() {
8655 return mHintScreenTimeout;
8656 }
8657
Alex Hills9ab3a232016-04-05 14:54:56 -04008658 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008659 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8660 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8661 * qr codes, as well as other simple black-and-white tickets.
8662 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8663 * @return this object for method chaining
8664 */
8665 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8666 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8667 return this;
8668 }
8669
8670 /**
8671 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8672 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8673 * qr codes, as well as other simple black-and-white tickets.
8674 * @return {@code true} if it should be displayed in ambient, false otherwise
8675 * otherwise. The default value is {@code false} if this was never set.
8676 */
8677 public boolean getHintAmbientBigPicture() {
8678 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8679 }
8680
8681 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008682 * Set a hint that this notification's content intent will launch an {@link Activity}
8683 * directly, telling the platform that it can generate the appropriate transitions.
8684 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8685 * an activity and transitions should be generated, false otherwise.
8686 * @return this object for method chaining
8687 */
8688 public WearableExtender setHintContentIntentLaunchesActivity(
8689 boolean hintContentIntentLaunchesActivity) {
8690 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8691 return this;
8692 }
8693
8694 /**
8695 * Get a hint that this notification's content intent will launch an {@link Activity}
8696 * directly, telling the platform that it can generate the appropriate transitions
8697 * @return {@code true} if the content intent will launch an activity and transitions should
8698 * be generated, false otherwise. The default value is {@code false} if this was never set.
8699 */
8700 public boolean getHintContentIntentLaunchesActivity() {
8701 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8702 }
8703
Nadia Benbernou948627e2016-04-14 14:41:08 -04008704 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008705 * Sets the dismissal id for this notification. If a notification is posted with a
8706 * dismissal id, then when that notification is canceled, notifications on other wearables
8707 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008708 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008709 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008710 * @param dismissalId the dismissal id of the notification.
8711 * @return this object for method chaining
8712 */
8713 public WearableExtender setDismissalId(String dismissalId) {
8714 mDismissalId = dismissalId;
8715 return this;
8716 }
8717
8718 /**
8719 * Returns the dismissal id of the notification.
8720 * @return the dismissal id of the notification or null if it has not been set.
8721 */
8722 public String getDismissalId() {
8723 return mDismissalId;
8724 }
8725
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008726 /**
8727 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8728 * posted from a phone to provide finer-grained control on what notifications are bridged
8729 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8730 * Features to Notifications</a> for more information.
8731 * @param bridgeTag the bridge tag of the notification.
8732 * @return this object for method chaining
8733 */
8734 public WearableExtender setBridgeTag(String bridgeTag) {
8735 mBridgeTag = bridgeTag;
8736 return this;
8737 }
8738
8739 /**
8740 * Returns the bridge tag of the notification.
8741 * @return the bridge tag or null if not present.
8742 */
8743 public String getBridgeTag() {
8744 return mBridgeTag;
8745 }
8746
Griff Hazen61a9e862014-05-22 16:05:19 -07008747 private void setFlag(int mask, boolean value) {
8748 if (value) {
8749 mFlags |= mask;
8750 } else {
8751 mFlags &= ~mask;
8752 }
8753 }
8754 }
8755
8756 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008757 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8758 * with car extensions:
8759 *
8760 * <ol>
8761 * <li>Create an {@link Notification.Builder}, setting any desired
8762 * properties.
8763 * <li>Create a {@link CarExtender}.
8764 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8765 * {@link CarExtender}.
8766 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8767 * to apply the extensions to a notification.
8768 * </ol>
8769 *
8770 * <pre class="prettyprint">
8771 * Notification notification = new Notification.Builder(context)
8772 * ...
8773 * .extend(new CarExtender()
8774 * .set*(...))
8775 * .build();
8776 * </pre>
8777 *
8778 * <p>Car extensions can be accessed on an existing notification by using the
8779 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8780 * to access values.
8781 */
8782 public static final class CarExtender implements Extender {
8783 private static final String TAG = "CarExtender";
8784
8785 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8786 private static final String EXTRA_LARGE_ICON = "large_icon";
8787 private static final String EXTRA_CONVERSATION = "car_conversation";
8788 private static final String EXTRA_COLOR = "app_color";
8789
8790 private Bitmap mLargeIcon;
8791 private UnreadConversation mUnreadConversation;
8792 private int mColor = Notification.COLOR_DEFAULT;
8793
8794 /**
8795 * Create a {@link CarExtender} with default options.
8796 */
8797 public CarExtender() {
8798 }
8799
8800 /**
8801 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8802 *
8803 * @param notif The notification from which to copy options.
8804 */
8805 public CarExtender(Notification notif) {
8806 Bundle carBundle = notif.extras == null ?
8807 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8808 if (carBundle != null) {
8809 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8810 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8811
8812 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8813 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8814 }
8815 }
8816
8817 /**
8818 * Apply car extensions to a notification that is being built. This is typically called by
8819 * the {@link Notification.Builder#extend(Notification.Extender)}
8820 * method of {@link Notification.Builder}.
8821 */
8822 @Override
8823 public Notification.Builder extend(Notification.Builder builder) {
8824 Bundle carExtensions = new Bundle();
8825
8826 if (mLargeIcon != null) {
8827 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8828 }
8829 if (mColor != Notification.COLOR_DEFAULT) {
8830 carExtensions.putInt(EXTRA_COLOR, mColor);
8831 }
8832
8833 if (mUnreadConversation != null) {
8834 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8835 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8836 }
8837
8838 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8839 return builder;
8840 }
8841
8842 /**
8843 * Sets the accent color to use when Android Auto presents the notification.
8844 *
8845 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8846 * to accent the displayed notification. However, not all colors are acceptable in an
8847 * automotive setting. This method can be used to override the color provided in the
8848 * notification in such a situation.
8849 */
Tor Norbye80756e32015-03-02 09:39:27 -08008850 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008851 mColor = color;
8852 return this;
8853 }
8854
8855 /**
8856 * Gets the accent color.
8857 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008858 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008859 */
Tor Norbye80756e32015-03-02 09:39:27 -08008860 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008861 public int getColor() {
8862 return mColor;
8863 }
8864
8865 /**
8866 * Sets the large icon of the car notification.
8867 *
8868 * If no large icon is set in the extender, Android Auto will display the icon
8869 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8870 *
8871 * @param largeIcon The large icon to use in the car notification.
8872 * @return This object for method chaining.
8873 */
8874 public CarExtender setLargeIcon(Bitmap largeIcon) {
8875 mLargeIcon = largeIcon;
8876 return this;
8877 }
8878
8879 /**
8880 * Gets the large icon used in this car notification, or null if no icon has been set.
8881 *
8882 * @return The large icon for the car notification.
8883 * @see CarExtender#setLargeIcon
8884 */
8885 public Bitmap getLargeIcon() {
8886 return mLargeIcon;
8887 }
8888
8889 /**
8890 * Sets the unread conversation in a message notification.
8891 *
8892 * @param unreadConversation The unread part of the conversation this notification conveys.
8893 * @return This object for method chaining.
8894 */
8895 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8896 mUnreadConversation = unreadConversation;
8897 return this;
8898 }
8899
8900 /**
8901 * Returns the unread conversation conveyed by this notification.
8902 * @see #setUnreadConversation(UnreadConversation)
8903 */
8904 public UnreadConversation getUnreadConversation() {
8905 return mUnreadConversation;
8906 }
8907
8908 /**
8909 * A class which holds the unread messages from a conversation.
8910 */
8911 public static class UnreadConversation {
8912 private static final String KEY_AUTHOR = "author";
8913 private static final String KEY_TEXT = "text";
8914 private static final String KEY_MESSAGES = "messages";
8915 private static final String KEY_REMOTE_INPUT = "remote_input";
8916 private static final String KEY_ON_REPLY = "on_reply";
8917 private static final String KEY_ON_READ = "on_read";
8918 private static final String KEY_PARTICIPANTS = "participants";
8919 private static final String KEY_TIMESTAMP = "timestamp";
8920
8921 private final String[] mMessages;
8922 private final RemoteInput mRemoteInput;
8923 private final PendingIntent mReplyPendingIntent;
8924 private final PendingIntent mReadPendingIntent;
8925 private final String[] mParticipants;
8926 private final long mLatestTimestamp;
8927
8928 UnreadConversation(String[] messages, RemoteInput remoteInput,
8929 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8930 String[] participants, long latestTimestamp) {
8931 mMessages = messages;
8932 mRemoteInput = remoteInput;
8933 mReadPendingIntent = readPendingIntent;
8934 mReplyPendingIntent = replyPendingIntent;
8935 mParticipants = participants;
8936 mLatestTimestamp = latestTimestamp;
8937 }
8938
8939 /**
8940 * Gets the list of messages conveyed by this notification.
8941 */
8942 public String[] getMessages() {
8943 return mMessages;
8944 }
8945
8946 /**
8947 * Gets the remote input that will be used to convey the response to a message list, or
8948 * null if no such remote input exists.
8949 */
8950 public RemoteInput getRemoteInput() {
8951 return mRemoteInput;
8952 }
8953
8954 /**
8955 * Gets the pending intent that will be triggered when the user replies to this
8956 * notification.
8957 */
8958 public PendingIntent getReplyPendingIntent() {
8959 return mReplyPendingIntent;
8960 }
8961
8962 /**
8963 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8964 * in this object's message list.
8965 */
8966 public PendingIntent getReadPendingIntent() {
8967 return mReadPendingIntent;
8968 }
8969
8970 /**
8971 * Gets the participants in the conversation.
8972 */
8973 public String[] getParticipants() {
8974 return mParticipants;
8975 }
8976
8977 /**
8978 * Gets the firs participant in the conversation.
8979 */
8980 public String getParticipant() {
8981 return mParticipants.length > 0 ? mParticipants[0] : null;
8982 }
8983
8984 /**
8985 * Gets the timestamp of the conversation.
8986 */
8987 public long getLatestTimestamp() {
8988 return mLatestTimestamp;
8989 }
8990
8991 Bundle getBundleForUnreadConversation() {
8992 Bundle b = new Bundle();
8993 String author = null;
8994 if (mParticipants != null && mParticipants.length > 1) {
8995 author = mParticipants[0];
8996 }
8997 Parcelable[] messages = new Parcelable[mMessages.length];
8998 for (int i = 0; i < messages.length; i++) {
8999 Bundle m = new Bundle();
9000 m.putString(KEY_TEXT, mMessages[i]);
9001 m.putString(KEY_AUTHOR, author);
9002 messages[i] = m;
9003 }
9004 b.putParcelableArray(KEY_MESSAGES, messages);
9005 if (mRemoteInput != null) {
9006 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9007 }
9008 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9009 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9010 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9011 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9012 return b;
9013 }
9014
9015 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9016 if (b == null) {
9017 return null;
9018 }
9019 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9020 String[] messages = null;
9021 if (parcelableMessages != null) {
9022 String[] tmp = new String[parcelableMessages.length];
9023 boolean success = true;
9024 for (int i = 0; i < tmp.length; i++) {
9025 if (!(parcelableMessages[i] instanceof Bundle)) {
9026 success = false;
9027 break;
9028 }
9029 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9030 if (tmp[i] == null) {
9031 success = false;
9032 break;
9033 }
9034 }
9035 if (success) {
9036 messages = tmp;
9037 } else {
9038 return null;
9039 }
9040 }
9041
9042 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9043 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9044
9045 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9046
9047 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9048 if (participants == null || participants.length != 1) {
9049 return null;
9050 }
9051
9052 return new UnreadConversation(messages,
9053 remoteInput,
9054 onReply,
9055 onRead,
9056 participants, b.getLong(KEY_TIMESTAMP));
9057 }
9058 };
9059
9060 /**
9061 * Builder class for {@link CarExtender.UnreadConversation} objects.
9062 */
9063 public static class Builder {
9064 private final List<String> mMessages = new ArrayList<String>();
9065 private final String mParticipant;
9066 private RemoteInput mRemoteInput;
9067 private PendingIntent mReadPendingIntent;
9068 private PendingIntent mReplyPendingIntent;
9069 private long mLatestTimestamp;
9070
9071 /**
9072 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9073 *
9074 * @param name The name of the other participant in the conversation.
9075 */
9076 public Builder(String name) {
9077 mParticipant = name;
9078 }
9079
9080 /**
9081 * Appends a new unread message to the list of messages for this conversation.
9082 *
9083 * The messages should be added from oldest to newest.
9084 *
9085 * @param message The text of the new unread message.
9086 * @return This object for method chaining.
9087 */
9088 public Builder addMessage(String message) {
9089 mMessages.add(message);
9090 return this;
9091 }
9092
9093 /**
9094 * Sets the pending intent and remote input which will convey the reply to this
9095 * notification.
9096 *
9097 * @param pendingIntent The pending intent which will be triggered on a reply.
9098 * @param remoteInput The remote input parcelable which will carry the reply.
9099 * @return This object for method chaining.
9100 *
9101 * @see CarExtender.UnreadConversation#getRemoteInput
9102 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9103 */
9104 public Builder setReplyAction(
9105 PendingIntent pendingIntent, RemoteInput remoteInput) {
9106 mRemoteInput = remoteInput;
9107 mReplyPendingIntent = pendingIntent;
9108
9109 return this;
9110 }
9111
9112 /**
9113 * Sets the pending intent that will be sent once the messages in this notification
9114 * are read.
9115 *
9116 * @param pendingIntent The pending intent to use.
9117 * @return This object for method chaining.
9118 */
9119 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9120 mReadPendingIntent = pendingIntent;
9121 return this;
9122 }
9123
9124 /**
9125 * Sets the timestamp of the most recent message in an unread conversation.
9126 *
9127 * If a messaging notification has been posted by your application and has not
9128 * yet been cancelled, posting a later notification with the same id and tag
9129 * but without a newer timestamp may result in Android Auto not displaying a
9130 * heads up notification for the later notification.
9131 *
9132 * @param timestamp The timestamp of the most recent message in the conversation.
9133 * @return This object for method chaining.
9134 */
9135 public Builder setLatestTimestamp(long timestamp) {
9136 mLatestTimestamp = timestamp;
9137 return this;
9138 }
9139
9140 /**
9141 * Builds a new unread conversation object.
9142 *
9143 * @return The new unread conversation object.
9144 */
9145 public UnreadConversation build() {
9146 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9147 String[] participants = { mParticipant };
9148 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9149 mReadPendingIntent, participants, mLatestTimestamp);
9150 }
9151 }
9152 }
9153
9154 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009155 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9156 * with a TV extension:
9157 *
9158 * <ol>
9159 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9160 * <li>Create a {@link TvExtender}.
9161 * <li>Set TV-specific properties using the {@code set} methods of
9162 * {@link TvExtender}.
9163 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9164 * to apply the extension to a notification.
9165 * </ol>
9166 *
9167 * <pre class="prettyprint">
9168 * Notification notification = new Notification.Builder(context)
9169 * ...
9170 * .extend(new TvExtender()
9171 * .set*(...))
9172 * .build();
9173 * </pre>
9174 *
9175 * <p>TV extensions can be accessed on an existing notification by using the
9176 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9177 * to access values.
9178 *
9179 * @hide
9180 */
9181 @SystemApi
9182 public static final class TvExtender implements Extender {
9183 private static final String TAG = "TvExtender";
9184
9185 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9186 private static final String EXTRA_FLAGS = "flags";
9187 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9188 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009189 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009190 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009191
9192 // Flags bitwise-ored to mFlags
9193 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9194
9195 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009196 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009197 private PendingIntent mContentIntent;
9198 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009199 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009200
9201 /**
9202 * Create a {@link TvExtender} with default options.
9203 */
9204 public TvExtender() {
9205 mFlags = FLAG_AVAILABLE_ON_TV;
9206 }
9207
9208 /**
9209 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9210 *
9211 * @param notif The notification from which to copy options.
9212 */
9213 public TvExtender(Notification notif) {
9214 Bundle bundle = notif.extras == null ?
9215 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9216 if (bundle != null) {
9217 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009218 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009219 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009220 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9221 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9222 }
9223 }
9224
9225 /**
9226 * Apply a TV extension to a notification that is being built. This is typically called by
9227 * the {@link Notification.Builder#extend(Notification.Extender)}
9228 * method of {@link Notification.Builder}.
9229 */
9230 @Override
9231 public Notification.Builder extend(Notification.Builder builder) {
9232 Bundle bundle = new Bundle();
9233
9234 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009235 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009236 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009237 if (mContentIntent != null) {
9238 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9239 }
9240
9241 if (mDeleteIntent != null) {
9242 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9243 }
9244
9245 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9246 return builder;
9247 }
9248
9249 /**
9250 * Returns true if this notification should be shown on TV. This method return true
9251 * if the notification was extended with a TvExtender.
9252 */
9253 public boolean isAvailableOnTv() {
9254 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9255 }
9256
9257 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009258 * Specifies the channel the notification should be delivered on when shown on TV.
9259 * It can be different from the channel that the notification is delivered to when
9260 * posting on a non-TV device.
9261 */
9262 public TvExtender setChannel(String channelId) {
9263 mChannelId = channelId;
9264 return this;
9265 }
9266
9267 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009268 * Specifies the channel the notification should be delivered on when shown on TV.
9269 * It can be different from the channel that the notification is delivered to when
9270 * posting on a non-TV device.
9271 */
9272 public TvExtender setChannelId(String channelId) {
9273 mChannelId = channelId;
9274 return this;
9275 }
9276
Jeff Sharkey000ce802017-04-29 13:13:27 -06009277 /** @removed */
9278 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009279 public String getChannel() {
9280 return mChannelId;
9281 }
9282
9283 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009284 * Returns the id of the channel this notification posts to on TV.
9285 */
9286 public String getChannelId() {
9287 return mChannelId;
9288 }
9289
9290 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009291 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9292 * If provided, it is used instead of the content intent specified
9293 * at the level of Notification.
9294 */
9295 public TvExtender setContentIntent(PendingIntent intent) {
9296 mContentIntent = intent;
9297 return this;
9298 }
9299
9300 /**
9301 * Returns the TV-specific content intent. If this method returns null, the
9302 * main content intent on the notification should be used.
9303 *
9304 * @see {@link Notification#contentIntent}
9305 */
9306 public PendingIntent getContentIntent() {
9307 return mContentIntent;
9308 }
9309
9310 /**
9311 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9312 * by the user on TV. If provided, it is used instead of the delete intent specified
9313 * at the level of Notification.
9314 */
9315 public TvExtender setDeleteIntent(PendingIntent intent) {
9316 mDeleteIntent = intent;
9317 return this;
9318 }
9319
9320 /**
9321 * Returns the TV-specific delete intent. If this method returns null, the
9322 * main delete intent on the notification should be used.
9323 *
9324 * @see {@link Notification#deleteIntent}
9325 */
9326 public PendingIntent getDeleteIntent() {
9327 return mDeleteIntent;
9328 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009329
9330 /**
9331 * Specifies whether this notification should suppress showing a message over top of apps
9332 * outside of the launcher.
9333 */
9334 public TvExtender setSuppressShowOverApps(boolean suppress) {
9335 mSuppressShowOverApps = suppress;
9336 return this;
9337 }
9338
9339 /**
9340 * Returns true if this notification should not show messages over top of apps
9341 * outside of the launcher.
9342 */
9343 public boolean getSuppressShowOverApps() {
9344 return mSuppressShowOverApps;
9345 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009346 }
9347
9348 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009349 * Get an array of Notification objects from a parcelable array bundle field.
9350 * Update the bundle to have a typed array so fetches in the future don't need
9351 * to do an array copy.
9352 */
9353 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9354 Parcelable[] array = bundle.getParcelableArray(key);
9355 if (array instanceof Notification[] || array == null) {
9356 return (Notification[]) array;
9357 }
9358 Notification[] typedArray = Arrays.copyOf(array, array.length,
9359 Notification[].class);
9360 bundle.putParcelableArray(key, typedArray);
9361 return typedArray;
9362 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009363
9364 private static class BuilderRemoteViews extends RemoteViews {
9365 public BuilderRemoteViews(Parcel parcel) {
9366 super(parcel);
9367 }
9368
Kenny Guy77320062014-08-27 21:37:15 +01009369 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9370 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009371 }
9372
9373 @Override
9374 public BuilderRemoteViews clone() {
9375 Parcel p = Parcel.obtain();
9376 writeToParcel(p, 0);
9377 p.setDataPosition(0);
9378 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9379 p.recycle();
9380 return brv;
9381 }
9382 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009383
Selim Cinek384804b2018-04-18 14:31:07 +08009384 /**
9385 * A result object where information about the template that was created is saved.
9386 */
9387 private static class TemplateBindResult {
9388 int mIconMarginEndDimen;
9389
9390 /**
9391 * Get the margin end dimension that needs to be added to any fields that may overlap
9392 * with the right actions.
9393 */
9394 public int getIconMarginEndDimen() {
9395 return mIconMarginEndDimen;
9396 }
9397
9398 public void setIconMarginEndDimen(int iconMarginEndDimen) {
9399 this.mIconMarginEndDimen = iconMarginEndDimen;
9400 }
9401 }
9402
Adrian Roos70d7aa32017-01-11 15:39:06 -08009403 private static class StandardTemplateParams {
9404 boolean hasProgress = true;
9405 boolean ambient = false;
9406 CharSequence title;
9407 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009408 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009409 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009410 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009411
9412 final StandardTemplateParams reset() {
9413 hasProgress = true;
9414 ambient = false;
9415 title = null;
9416 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009417 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009418 return this;
9419 }
9420
9421 final StandardTemplateParams hasProgress(boolean hasProgress) {
9422 this.hasProgress = hasProgress;
9423 return this;
9424 }
9425
9426 final StandardTemplateParams title(CharSequence title) {
9427 this.title = title;
9428 return this;
9429 }
9430
9431 final StandardTemplateParams text(CharSequence text) {
9432 this.text = text;
9433 return this;
9434 }
9435
Selim Cinekafeed292017-12-12 17:32:44 -08009436 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9437 this.headerTextSecondary = text;
9438 return this;
9439 }
9440
Selim Cinek384804b2018-04-18 14:31:07 +08009441 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9442 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009443 return this;
9444 }
9445
Selim Cinek384804b2018-04-18 14:31:07 +08009446 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9447 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009448 return this;
9449 }
9450
Adrian Roos70d7aa32017-01-11 15:39:06 -08009451 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009452 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009453 this.ambient = ambient;
9454 return this;
9455 }
9456
9457 final StandardTemplateParams fillTextsFrom(Builder b) {
9458 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009459 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9460
9461 // Big text notifications should contain their content when viewed in ambient mode.
9462 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9463 if (!ambient || TextUtils.isEmpty(text)) {
9464 text = extras.getCharSequence(EXTRA_TEXT);
9465 }
9466 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009467 return this;
9468 }
9469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470}