blob: d6fddfca986ea152e23447dcdefcf2be061f8226 [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;
Dan Sandler50128532015-12-08 15:42:41 -050092import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094/**
95 * A class that represents how a persistent notification is to be presented to
96 * the user using the {@link android.app.NotificationManager}.
97 *
Joe Onoratocb109a02011-01-18 17:57:41 -080098 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
99 * easier to construct Notifications.</p>
100 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For a guide to creating notifications, read the
104 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
105 * developer guide.</p>
106 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
108public class Notification implements Parcelable
109{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400110 private static final String TAG = "Notification";
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400114 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 */
116 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
117 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
118 = "android.intent.category.NOTIFICATION_PREFERENCES";
119
120 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
122 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 */
125 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
126
127 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400129 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
130 * what settings should be shown in the target app.
131 */
132 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
133
134 /**
135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
137 * that can be used to narrow down what settings should be shown in the target app.
138 */
139 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
143 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Use all default values (where applicable).
150 */
151 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Use the default notification sound. This will ignore any given
155 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
Chris Wren47c20a12014-06-18 17:27:29 -0400157 * <p>
158 * A notification that is noisy is more likely to be presented as a heads-up notification.
159 * </p>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 */
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int DEFAULT_SOUND = 1;
165
166 /**
167 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700169 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 *
Chris Wren47c20a12014-06-18 17:27:29 -0400171 * <p>
172 * A notification that vibrates is more likely to be presented as a heads-up notification.
173 * </p>
174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * Use the default notification lights. This will ignore the
182 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
183 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 */
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200191 * Maximum length of CharSequences accepted by Builder and friends.
192 *
193 * <p>
194 * Avoids spamming the system with overly large strings such as full e-mails.
195 */
196 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
197
198 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800199 * Maximum entries of reply text that are accepted by Builder and friends.
200 */
201 private static final int MAX_REPLY_HISTORY = 5;
202
203 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * Default value: {@link System#currentTimeMillis() Now}.
207 *
208 * Choose a timestamp that will be most relevant to the user. For most finite events, this
209 * corresponds to the time the event happened (or will happen, in the case of events that have
210 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800211 * timestamped according to when the activity began.
212 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800214 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * <ul>
216 * <li>Notification of a new chat message should be stamped when the message was received.</li>
217 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
218 * <li>Notification of a completed file download should be stamped when the download finished.</li>
219 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
220 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
221 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 * </ul>
223 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700224 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
225 * anymore by default and must be opted into by using
226 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public long when;
229
230 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700231 * The creation time of the notification
232 */
233 private long creationTime;
234
235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400237 *
238 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
Dan Sandler86647982015-05-13 23:41:13 -0400240 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700241 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public int icon;
243
244 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800245 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
246 * leave it at its default value of 0.
247 *
248 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700249 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 */
251 public int iconLevel;
252
253 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500254 * The number of events that this notification represents. For example, in a new mail
255 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800256 *
Julia Reynolds30331982017-04-27 10:12:50 -0400257 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500258 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
259 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Julia Reynolds30331982017-04-27 10:12:50 -0400261 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 /**
264 * The intent to execute when the expanded status entry is clicked. If
265 * this is an activity, it must include the
266 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800267 * that you take care of task management as described in the
268 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800269 * Stack</a> document. In particular, make sure to read the notification section
270 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
271 * Notifications</a> for the correct ways to launch an application from a
272 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
274 public PendingIntent contentIntent;
275
276 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500277 * The intent to execute when the notification is explicitly dismissed by the user, either with
278 * the "Clear All" button or by swiping it away individually.
279 *
280 * This probably shouldn't be launching an activity since several of those will be sent
281 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public PendingIntent deleteIntent;
284
285 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700286 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800287 *
Chris Wren47c20a12014-06-18 17:27:29 -0400288 * <p>
289 * The system UI may choose to display a heads-up notification, instead of
290 * launching this intent, while the user is using the device.
291 * </p>
292 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800293 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400294 */
295 public PendingIntent fullScreenIntent;
296
297 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400298 * Text that summarizes this notification for accessibility services.
299 *
300 * As of the L release, this text is no longer shown on screen, but it is still useful to
301 * accessibility services (where it serves as an audible announcement of the notification's
302 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400303 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public CharSequence tickerText;
307
308 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400309 * Formerly, a view showing the {@link #tickerText}.
310 *
311 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400312 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400313 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400315
316 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 * The view that will represent this notification in the notification list (which is pulled
318 * down from the status bar).
319 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500320 * As of N, this field may be null. The notification view is determined by the inputs
321 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400322 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400324 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public RemoteViews contentView;
326
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400327 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400328 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400329 * opportunity to show more detail. The system UI may choose to show this
330 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400331 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500332 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400333 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
334 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400336 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400337 public RemoteViews bigContentView;
338
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339
340 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400341 * A medium-format version of {@link #contentView}, providing the Notification an
342 * opportunity to add action buttons to contentView. At its discretion, the system UI may
343 * choose to show this as a heads-up notification, which will pop up so the user can see
344 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500346 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
348 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500351 public RemoteViews headsUpContentView;
352
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400353 /**
Dan Sandler86647982015-05-13 23:41:13 -0400354 * A large bitmap to be shown in the notification content area.
355 *
356 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Dan Sandler86647982015-05-13 23:41:13 -0400358 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800359 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
361 /**
362 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400365 * A notification that is noisy is more likely to be presented as a heads-up notification.
366 * </p>
367 *
368 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public Uri sound;
375
376 /**
377 * Use this constant as the value for audioStreamType to request that
378 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700379 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public static final int STREAM_DEFAULT = -1;
385
386 /**
387 * The audio stream type to use when playing the sound.
388 * Should be one of the STREAM_ constants from
389 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400390 *
391 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public int audioStreamType = STREAM_DEFAULT;
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400397 * The default value of {@link #audioAttributes}.
398 */
399 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
400 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
401 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
402 .build();
403
404 /**
405 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500406 *
407 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400408 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400410 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
411
412 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500413 * The pattern with which to vibrate.
414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * <p>
416 * To vibrate the default pattern, see {@link #defaults}.
417 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public long[] vibrate;
424
425 /**
426 * The color of the led. The hardware will do its best approximation.
427 *
428 * @see #FLAG_SHOW_LIGHTS
429 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Tor Norbye80756e32015-03-02 09:39:27 -0800432 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public int ledARGB;
435
436 /**
437 * The number of milliseconds for the LED to be on while it's flashing.
438 * The hardware will do its best approximation.
439 *
440 * @see #FLAG_SHOW_LIGHTS
441 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public int ledOnMS;
446
447 /**
448 * The number of milliseconds for the LED to be off while it's flashing.
449 * The hardware will do its best approximation.
450 *
451 * @see #FLAG_SHOW_LIGHTS
452 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500453 *
454 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public int ledOffMS;
458
459 /**
460 * Specifies which values should be taken from the defaults.
461 * <p>
462 * To set, OR the desired from {@link #DEFAULT_SOUND},
463 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
464 * values, use {@link #DEFAULT_ALL}.
465 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 *
467 * @deprecated use {@link NotificationChannel#getSound()} and
468 * {@link NotificationChannel#shouldShowLights()} and
469 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int defaults;
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
475 * Bit to be bitwise-ored into the {@link #flags} field that should be
476 * set if you want the LED on for this notification.
477 * <ul>
478 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
479 * or 0 for both ledOnMS and ledOffMS.</li>
480 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
481 * <li>To flash the LED, pass the number of milliseconds that it should
482 * be on and off to ledOnMS and ledOffMS.</li>
483 * </ul>
484 * <p>
485 * Since hardware varies, you are not guaranteed that any of the values
486 * you pass are honored exactly. Use the system defaults (TODO) if possible
487 * because they will be set to values that work on any given hardware.
488 * <p>
489 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
495
496 /**
497 * Bit to be bitwise-ored into the {@link #flags} field that should be
498 * set if this notification is in reference to something that is ongoing,
499 * like a phone call. It should not be set if this notification is in
500 * reference to something that happened at a particular point in time,
501 * like a missed phone call.
502 */
503 public static final int FLAG_ONGOING_EVENT = 0x00000002;
504
505 /**
506 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700507 * the audio will be repeated until the notification is
508 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
510 public static final int FLAG_INSISTENT = 0x00000004;
511
512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700514 * set if you would only like the sound, vibrate and ticker to be played
515 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
517 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
518
519 /**
520 * Bit to be bitwise-ored into the {@link #flags} field that should be
521 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500522 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 */
524 public static final int FLAG_AUTO_CANCEL = 0x00000010;
525
526 /**
527 * Bit to be bitwise-ored into the {@link #flags} field that should be
528 * set if the notification should not be canceled when the user clicks
529 * the Clear all button.
530 */
531 public static final int FLAG_NO_CLEAR = 0x00000020;
532
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700533 /**
534 * Bit to be bitwise-ored into the {@link #flags} field that should be
535 * set if this notification represents a currently running service. This
536 * will normally be set for you by {@link Service#startForeground}.
537 */
538 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
539
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400540 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500543 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400544 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700545 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400547
Griff Hazendfcb0802014-02-11 12:00:00 -0800548 /**
549 * Bit to be bitswise-ored into the {@link #flags} field that should be
550 * set if this notification is relevant to the current device only
551 * and it is not recommended that it bridge to other devices.
552 */
553 public static final int FLAG_LOCAL_ONLY = 0x00000100;
554
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700555 /**
556 * Bit to be bitswise-ored into the {@link #flags} field that should be
557 * set if this notification is the group summary for a group of notifications.
558 * Grouped notifications may display in a cluster or stack on devices which
559 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
560 */
561 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
562
Julia Reynoldse46bb372016-03-17 11:05:58 -0400563 /**
564 * Bit to be bitswise-ored into the {@link #flags} field that should be
565 * set if this notification is the group summary for an auto-group of notifications.
566 *
567 * @hide
568 */
569 @SystemApi
570 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
571
Julia Reynolds4db59552017-06-30 13:34:01 -0400572 /**
573 * @hide
574 */
575 public static final int FLAG_CAN_COLORIZE = 0x00000800;
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public int flags;
578
Tor Norbyed9273d62013-05-30 15:59:53 -0700579 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700580 @IntDef(prefix = { "PRIORITY_" }, value = {
581 PRIORITY_DEFAULT,
582 PRIORITY_LOW,
583 PRIORITY_MIN,
584 PRIORITY_HIGH,
585 PRIORITY_MAX
586 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700587 @Retention(RetentionPolicy.SOURCE)
588 public @interface Priority {}
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 * Default notification {@link #priority}. If your application does not prioritize its own
592 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500593 *
594 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500597 public static final int PRIORITY_DEFAULT = 0;
598
599 /**
600 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
601 * items smaller, or at a different position in the list, compared with your app's
602 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500603 *
604 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 public static final int PRIORITY_LOW = -1;
608
609 /**
610 * Lowest {@link #priority}; these items might not be shown to the user except under special
611 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500612 *
613 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500616 public static final int PRIORITY_MIN = -2;
617
618 /**
619 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
620 * show these items larger, or at a different position in notification lists, compared with
621 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} 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_HIGH = 1;
627
628 /**
629 * Highest {@link #priority}, for your application's most important items that require the
630 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_MAX = 2;
636
637 /**
638 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800639 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640 * Priority is an indication of how much of the user's valuable attention should be consumed by
641 * this notification. Low-priority notifications may be hidden from the user in certain
642 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500643 * system will make a determination about how to interpret this priority when presenting
644 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400645 *
646 * <p>
647 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
648 * as a heads-up notification.
649 * </p>
650 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700653 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800656
Dan Sandler26e81cf2014-05-06 10:01:27 -0400657 /**
658 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
659 * to be applied by the standard Style templates when presenting this notification.
660 *
661 * The current template design constructs a colorful header image by overlaying the
662 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
663 * ignored.
664 */
Tor Norbye80756e32015-03-02 09:39:27 -0800665 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400666 public int color = COLOR_DEFAULT;
667
668 /**
669 * Special value of {@link #color} telling the system not to decorate this notification with
670 * any special color but instead use default colors when presenting this notification.
671 */
Tor Norbye80756e32015-03-02 09:39:27 -0800672 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400673 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600674
675 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800676 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800677 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800678 */
679 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800680 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800681
682 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700683 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
684 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600685 * lockscreen).
686 *
687 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
688 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
689 * shown in all situations, but the contents are only available if the device is unlocked for
690 * the appropriate user.
691 *
692 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
693 * can be read even in an "insecure" context (that is, above a secure lockscreen).
694 * To modify the public version of this notification—for example, to redact some portions—see
695 * {@link Builder#setPublicVersion(Notification)}.
696 *
697 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
698 * and ticker until the user has bypassed the lockscreen.
699 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600700 public @Visibility int visibility;
701
702 /** @hide */
703 @IntDef(prefix = { "VISIBILITY_" }, value = {
704 VISIBILITY_PUBLIC,
705 VISIBILITY_PRIVATE,
706 VISIBILITY_SECRET,
707 })
708 @Retention(RetentionPolicy.SOURCE)
709 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710
Griff Hazenfc3922d2014-08-20 11:56:44 -0700711 /**
712 * Notification visibility: Show this notification in its entirety on all lockscreens.
713 *
714 * {@see #visibility}
715 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600716 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700717
718 /**
719 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
720 * private information on secure lockscreens.
721 *
722 * {@see #visibility}
723 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700725
726 /**
727 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
728 *
729 * {@see #visibility}
730 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600731 public static final int VISIBILITY_SECRET = -1;
732
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400734 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400736 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500737
738 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400739 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500740 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400741 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500742
743 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747
748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400756 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757
758 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400759 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 public static final String CATEGORY_ALARM = "alarm";
762
763 /**
764 * Notification category: progress of a long-running background operation.
765 */
766 public static final String CATEGORY_PROGRESS = "progress";
767
768 /**
769 * Notification category: social network or sharing update.
770 */
771 public static final String CATEGORY_SOCIAL = "social";
772
773 /**
774 * Notification category: error in background operation or authentication status.
775 */
776 public static final String CATEGORY_ERROR = "err";
777
778 /**
779 * Notification category: media transport control for playback.
780 */
781 public static final String CATEGORY_TRANSPORT = "transport";
782
783 /**
784 * Notification category: system or device status update. Reserved for system use.
785 */
786 public static final String CATEGORY_SYSTEM = "sys";
787
788 /**
789 * Notification category: indication of running background service.
790 */
791 public static final String CATEGORY_SERVICE = "service";
792
793 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400794 * Notification category: a specific, timely recommendation for a single thing.
795 * For example, a news app might want to recommend a news story it believes the user will
796 * want to read next.
797 */
798 public static final String CATEGORY_RECOMMENDATION = "recommendation";
799
800 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 * Notification category: ongoing information about device or contextual status.
802 */
803 public static final String CATEGORY_STATUS = "status";
804
805 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400806 * Notification category: user-scheduled reminder.
807 */
808 public static final String CATEGORY_REMINDER = "reminder";
809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
812 * that best describes this Notification. May be used by the system for ranking and filtering.
813 */
814 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500815
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700816 private String mGroupKey;
817
818 /**
819 * Get the key used to group this notification into a cluster or stack
820 * with other notifications on devices which support such rendering.
821 */
822 public String getGroup() {
823 return mGroupKey;
824 }
825
826 private String mSortKey;
827
828 /**
829 * Get a sort key that orders this notification among other notifications from the
830 * same package. This can be useful if an external sort was already applied and an app
831 * would like to preserve this. Notifications will be sorted lexicographically using this
832 * value, although providing different priorities in addition to providing sort key may
833 * cause this value to be ignored.
834 *
835 * <p>This sort key can also be used to order members of a notification group. See
836 * {@link Builder#setGroup}.
837 *
838 * @see String#compareTo(String)
839 */
840 public String getSortKey() {
841 return mSortKey;
842 }
843
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500844 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846 * <p>
847 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
848 * APIs, and are intended to be used by
849 * {@link android.service.notification.NotificationListenerService} implementations to extract
850 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public Bundle extras = new Bundle();
853
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700855 * All pending intents in the notification as the system needs to be able to access them but
856 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700857 * custom parcelable objects.
858 *
859 * @hide
860 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700861 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700862
863 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700864 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
865 * pending intents inside of it, so only those will get the behavior.
866 *
867 * @hide
868 */
Adrian Roosfb921842017-10-26 14:49:56 +0200869 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700870
871 /**
872 * Must be set by a process to start associating tokens with Notification objects
873 * coming in to it. This is set by NotificationManagerService.
874 *
875 * @hide
876 */
877 static public IBinder processWhitelistToken;
878
879 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880 * {@link #extras} key: this is the title of the notification,
881 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
882 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500883 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: this is the title of the notification when shown in expanded form,
887 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
888 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400889 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890
891 /**
892 * {@link #extras} key: this is the main text payload, as supplied to
893 * {@link Builder#setContentText(CharSequence)}.
894 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500895 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896
897 /**
898 * {@link #extras} key: this is a third line of text, as supplied to
899 * {@link Builder#setSubText(CharSequence)}.
900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902
903 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800904 * {@link #extras} key: this is the remote input history, as supplied to
905 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700906 *
907 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
908 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
909 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
910 * notifications once the other party has responded).
911 *
912 * The extra with this key is of type CharSequence[] and contains the most recent entry at
913 * the 0 index, the second most recent at the 1 index, etc.
914 *
915 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800916 */
917 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
918
919 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 * {@link #extras} key: this is a small piece of additional text as supplied to
921 * {@link Builder#setContentInfo(CharSequence)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
926 * {@link #extras} key: this is a line of summary information intended to be shown
927 * alongside expanded notifications, as supplied to (e.g.)
928 * {@link BigTextStyle#setSummaryText(CharSequence)}.
929 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400930 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200933 * {@link #extras} key: this is the longer text shown in the big form of a
934 * {@link BigTextStyle} notification, as supplied to
935 * {@link BigTextStyle#bigText(CharSequence)}.
936 */
937 public static final String EXTRA_BIG_TEXT = "android.bigText";
938
939 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
941 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400942 *
943 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400945 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500946 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947
948 /**
949 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
950 * notification payload, as
951 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400952 *
953 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400955 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be used instead of the one from
960 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
961 * shown in its expanded form, as supplied to
962 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
963 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400964 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965
966 /**
967 * {@link #extras} key: this is the progress value supplied to
968 * {@link Builder#setProgress(int, int, boolean)}.
969 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400970 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971
972 /**
973 * {@link #extras} key: this is the maximum value supplied to
974 * {@link Builder#setProgress(int, int, boolean)}.
975 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400976 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977
978 /**
979 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
980 * {@link Builder#setProgress(int, int, boolean)}.
981 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400982 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983
984 /**
985 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
986 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
987 * {@link Builder#setUsesChronometer(boolean)}.
988 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400989 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400990
991 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800992 * {@link #extras} key: whether the chronometer set on the notification should count down
993 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700994 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800995 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700996 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800997
998 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 * {@link #extras} key: whether {@link #when} should be shown,
1000 * as supplied to {@link Builder#setShowWhen(boolean)}.
1001 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001002 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003
1004 /**
1005 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1006 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1007 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001008 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009
1010 /**
1011 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1012 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1013 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001014 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001015
1016 /**
1017 * {@link #extras} key: A string representing the name of the specific
1018 * {@link android.app.Notification.Style} used to create this notification.
1019 */
Chris Wren91ad5632013-06-05 15:05:57 -04001020 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001023 * {@link #extras} key: A String array containing the people that this notification relates to,
1024 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001025 *
1026 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001028 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001029
1030 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001031 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1032 * this notification relates to.
1033 */
1034 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1035
1036 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001037 * Allow certain system-generated notifications to appear before the device is provisioned.
1038 * Only available to notifications coming from the android package.
1039 * @hide
1040 */
Maurice Lam96c10032017-03-29 15:42:38 -07001041 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001042 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001043 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1044
1045 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001046 * {@link #extras} key: A
1047 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1048 * in the background when the notification is selected. The URI must point to an image stream
1049 * suitable for passing into
1050 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1051 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1052 * URI used for this purpose must require no permissions to read the image data.
1053 */
1054 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1055
1056 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001057 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001058 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001059 * {@link android.app.Notification.MediaStyle} notification.
1060 */
1061 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1062
1063 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001064 * {@link #extras} key: the indices of actions to be shown in the compact view,
1065 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1066 */
1067 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1068
Christoph Studer943aa672014-08-03 20:31:16 +02001069 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001070 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1071 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001072 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1073 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001074 *
1075 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001076 */
1077 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1078
1079 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001080 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1081 * direct replies
1082 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1083 * {@link Person}
1084 */
1085 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1086
1087 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001088 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001089 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001090 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001091 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001092
1093 /**
1094 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1095 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001096 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1097 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001098 */
1099 public static final String EXTRA_MESSAGES = "android.messages";
1100
1101 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001102 * {@link #extras} key: an array of
1103 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1104 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1105 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1106 * array of bundles.
1107 */
1108 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1109
1110 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001111 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1112 * represents a group conversation.
1113 */
1114 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1115
1116 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001117 * {@link #extras} key: whether the notification should be colorized as
1118 * supplied to {@link Builder#setColorized(boolean)}}.
1119 */
1120 public static final String EXTRA_COLORIZED = "android.colorized";
1121
1122 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001123 * @hide
1124 */
1125 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1126
Selim Cinek247fa012016-02-18 09:50:48 -08001127 /**
1128 * @hide
1129 */
1130 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1131
Shane Brennan472a3b32016-12-12 15:28:10 -08001132 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001133 * @hide
1134 */
1135 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1136
1137 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001138 * {@link #extras} key: the audio contents of this notification.
1139 *
1140 * This is for use when rendering the notification on an audio-focused interface;
1141 * the audio contents are a complete sound sample that contains the contents/body of the
1142 * notification. This may be used in substitute of a Text-to-Speech reading of the
1143 * notification. For example if the notification represents a voice message this should point
1144 * to the audio of that message.
1145 *
1146 * The data stored under this key should be a String representation of a Uri that contains the
1147 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1148 *
1149 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1150 * has a field for holding data URI. That field can be used for audio.
1151 * See {@code Message#setData}.
1152 *
1153 * Example usage:
1154 * <pre>
1155 * {@code
1156 * Notification.Builder myBuilder = (build your Notification as normal);
1157 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1158 * }
1159 * </pre>
1160 */
1161 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1162
Dan Sandler80eaa592016-04-14 23:34:54 -04001163 /** @hide */
1164 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001165 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001166 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1167
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001168 /**
1169 * This is set on the notification shown by the activity manager about all apps
1170 * running in the background. It indicates that the notification should be shown
1171 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1172 * notification currently visible to the user. This is a string array of all
1173 * package names of the apps.
1174 * @hide
1175 */
1176 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1177
Dan Sandlerd63f9322015-05-06 15:18:49 -04001178 private Icon mSmallIcon;
1179 private Icon mLargeIcon;
1180
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001181 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001182 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001183
Julia Reynolds13d898c2017-02-02 12:22:05 -05001184 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001185 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001186
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001187 /** @hide */
1188 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1189 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1190 })
1191 @Retention(RetentionPolicy.SOURCE)
1192 public @interface GroupAlertBehavior {}
1193
1194 /**
1195 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1196 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1197 * notification will not be muted when it is in a group.
1198 */
1199 public static final int GROUP_ALERT_ALL = 0;
1200
1201 /**
1202 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1203 * notification in a group should be silenced (no sound or vibration) even if they are posted
1204 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001205 * mute this notification if this notification is a group child. This must be applied to all
1206 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001207 *
1208 * <p> For example, you might want to use this constant if you post a number of children
1209 * notifications at once (say, after a periodic sync), and only need to notify the user
1210 * audibly once.
1211 */
1212 public static final int GROUP_ALERT_SUMMARY = 1;
1213
1214 /**
1215 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1216 * notification in a group should be silenced (no sound or vibration) even if they are
1217 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1218 * to mute this notification if this notification is a group summary.
1219 *
1220 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001221 * in your group have content and the summary is only used to visually group notifications
1222 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001223 */
1224 public static final int GROUP_ALERT_CHILDREN = 2;
1225
Julia Reynolds2f431e22017-06-07 14:12:09 +00001226 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001227
Julia Reynolds13d898c2017-02-02 12:22:05 -05001228 /**
1229 * If this notification is being shown as a badge, always show as a number.
1230 */
1231 public static final int BADGE_ICON_NONE = 0;
1232
1233 /**
1234 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1235 * represent this notification.
1236 */
1237 public static final int BADGE_ICON_SMALL = 1;
1238
1239 /**
1240 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1241 * represent this notification.
1242 */
1243 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001244 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001245
Chris Wren51c75102013-07-16 20:49:17 -04001246 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001247 * Structure to encapsulate a named action that can be shown as part of this notification.
1248 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1249 * selected by the user.
1250 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001251 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1252 * or {@link Notification.Builder#addAction(Notification.Action)}
1253 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001254 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001255 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001256 /**
1257 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1258 * {@link RemoteInput}s.
1259 *
1260 * This is intended for {@link RemoteInput}s that only accept data, meaning
1261 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1262 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1263 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1264 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1265 *
1266 * You can test if a RemoteInput matches these constraints using
1267 * {@link RemoteInput#isDataOnly}.
1268 */
1269 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1270
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001271 /**
1272 * {@link }: No semantic action defined.
1273 */
1274 public static final int SEMANTIC_ACTION_NONE = 0;
1275
1276 /**
1277 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1278 * may be appropriate.
1279 */
1280 public static final int SEMANTIC_ACTION_REPLY = 1;
1281
1282 /**
1283 * {@code SemanticAction}: Mark content as read.
1284 */
1285 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1286
1287 /**
1288 * {@code SemanticAction}: Mark content as unread.
1289 */
1290 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1291
1292 /**
1293 * {@code SemanticAction}: Delete the content associated with the notification. This
1294 * could mean deleting an email, message, etc.
1295 */
1296 public static final int SEMANTIC_ACTION_DELETE = 4;
1297
1298 /**
1299 * {@code SemanticAction}: Archive the content associated with the notification. This
1300 * could mean archiving an email, message, etc.
1301 */
1302 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1303
1304 /**
1305 * {@code SemanticAction}: Mute the content associated with the notification. This could
1306 * mean silencing a conversation or currently playing media.
1307 */
1308 public static final int SEMANTIC_ACTION_MUTE = 6;
1309
1310 /**
1311 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1312 * mean un-silencing a conversation or currently playing media.
1313 */
1314 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1315
1316 /**
1317 * {@code SemanticAction}: Mark content with a thumbs up.
1318 */
1319 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1320
1321 /**
1322 * {@code SemanticAction}: Mark content with a thumbs down.
1323 */
1324 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1325
1326
Griff Hazen959591e2014-05-15 22:26:18 -07001327 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001328 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001329 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001330 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001331 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001332
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001333 /**
1334 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001335 *
1336 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001337 */
Dan Sandler86647982015-05-13 23:41:13 -04001338 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001339 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001340
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001341 /**
1342 * Title of the action.
1343 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001344 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001345
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 /**
1347 * Intent to send when the user invokes this action. May be null, in which case the action
1348 * may be rendered in a disabled presentation by the system UI.
1349 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001350 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001351
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001352 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001353 if (in.readInt() != 0) {
1354 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001355 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1356 icon = mIcon.getResId();
1357 }
Dan Sandler86647982015-05-13 23:41:13 -04001358 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001359 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1360 if (in.readInt() == 1) {
1361 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1362 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001363 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001364 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001365 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001366 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001367 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001368
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001369 /**
Dan Sandler86647982015-05-13 23:41:13 -04001370 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001371 */
Dan Sandler86647982015-05-13 23:41:13 -04001372 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001373 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001374 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1375 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001376 }
1377
Adrian Roos7af53622016-10-12 13:44:05 -07001378 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001379 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001380 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1381 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001382 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001383 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1384 this.icon = icon.getResId();
1385 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001386 this.title = title;
1387 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001388 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001389 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001390 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001391 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001392 }
1393
1394 /**
Dan Sandler86647982015-05-13 23:41:13 -04001395 * Return an icon representing the action.
1396 */
1397 public Icon getIcon() {
1398 if (mIcon == null && icon != 0) {
1399 // you snuck an icon in here without using the builder; let's try to keep it
1400 mIcon = Icon.createWithResource("", icon);
1401 }
1402 return mIcon;
1403 }
1404
1405 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001406 * Get additional metadata carried around with this Action.
1407 */
1408 public Bundle getExtras() {
1409 return mExtras;
1410 }
1411
1412 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001413 * Return whether the platform should automatically generate possible replies for this
1414 * {@link Action}
1415 */
1416 public boolean getAllowGeneratedReplies() {
1417 return mAllowGeneratedReplies;
1418 }
1419
1420 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001421 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001422 * May return null if no remote inputs were added. Only returns inputs which accept
1423 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001424 */
1425 public RemoteInput[] getRemoteInputs() {
1426 return mRemoteInputs;
1427 }
1428
1429 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001430 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1431 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1432 * (eg. reply, mark as read, delete, etc).
1433 */
1434 public @SemanticAction int getSemanticAction() {
1435 return mSemanticAction;
1436 }
1437
1438 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001439 * Get the list of inputs to be collected from the user that ONLY accept data when this
1440 * action is sent. These remote inputs are guaranteed to return true on a call to
1441 * {@link RemoteInput#isDataOnly}.
1442 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001443 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001444 *
1445 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1446 * of non-textual RemoteInputs do not access these remote inputs.
1447 */
1448 public RemoteInput[] getDataOnlyRemoteInputs() {
1449 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1450 }
1451
1452 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001453 * Builder class for {@link Action} objects.
1454 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001455 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001456 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001457 private final CharSequence mTitle;
1458 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001459 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001460 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001461 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001462 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001463
1464 /**
1465 * Construct a new builder for {@link Action} object.
1466 * @param icon icon to show for this action
1467 * @param title the title of the action
1468 * @param intent the {@link PendingIntent} to fire when users trigger this action
1469 */
Dan Sandler86647982015-05-13 23:41:13 -04001470 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001471 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001472 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001473 }
1474
1475 /**
1476 * Construct a new builder for {@link Action} object.
1477 * @param icon icon to show for this action
1478 * @param title the title of the action
1479 * @param intent the {@link PendingIntent} to fire when users trigger this action
1480 */
1481 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001482 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001483 }
1484
1485 /**
1486 * Construct a new builder for {@link Action} object using the fields from an
1487 * {@link Action}.
1488 * @param action the action to read fields from.
1489 */
1490 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001491 this(action.getIcon(), action.title, action.actionIntent,
1492 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001493 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001494 }
1495
Dan Sandler86647982015-05-13 23:41:13 -04001496 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001497 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1498 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001499 mIcon = icon;
1500 mTitle = title;
1501 mIntent = intent;
1502 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001503 if (remoteInputs != null) {
1504 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1505 Collections.addAll(mRemoteInputs, remoteInputs);
1506 }
Adrian Roos7af53622016-10-12 13:44:05 -07001507 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001508 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001509 }
1510
1511 /**
1512 * Merge additional metadata into this builder.
1513 *
1514 * <p>Values within the Bundle will replace existing extras values in this Builder.
1515 *
1516 * @see Notification.Action#extras
1517 */
1518 public Builder addExtras(Bundle extras) {
1519 if (extras != null) {
1520 mExtras.putAll(extras);
1521 }
1522 return this;
1523 }
1524
1525 /**
1526 * Get the metadata Bundle used by this Builder.
1527 *
1528 * <p>The returned Bundle is shared with this Builder.
1529 */
1530 public Bundle getExtras() {
1531 return mExtras;
1532 }
1533
1534 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001535 * Add an input to be collected from the user when this action is sent.
1536 * Response values can be retrieved from the fired intent by using the
1537 * {@link RemoteInput#getResultsFromIntent} function.
1538 * @param remoteInput a {@link RemoteInput} to add to the action
1539 * @return this object for method chaining
1540 */
1541 public Builder addRemoteInput(RemoteInput remoteInput) {
1542 if (mRemoteInputs == null) {
1543 mRemoteInputs = new ArrayList<RemoteInput>();
1544 }
1545 mRemoteInputs.add(remoteInput);
1546 return this;
1547 }
1548
1549 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001550 * Set whether the platform should automatically generate possible replies to add to
1551 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1552 * {@link RemoteInput}, this has no effect.
1553 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1554 * otherwise
1555 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001556 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001557 */
1558 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1559 mAllowGeneratedReplies = allowGeneratedReplies;
1560 return this;
1561 }
1562
1563 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001564 * Sets the {@code SemanticAction} for this {@link Action}. A
1565 * {@code SemanticAction} denotes what an {@link Action}'s
1566 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1567 * @param semanticAction a SemanticAction defined within {@link Action} with
1568 * {@code SEMANTIC_ACTION_} prefixes
1569 * @return this object for method chaining
1570 */
1571 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1572 mSemanticAction = semanticAction;
1573 return this;
1574 }
1575
1576 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001577 * Apply an extender to this action builder. Extenders may be used to add
1578 * metadata or change options on this builder.
1579 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001580 public Builder extend(Extender extender) {
1581 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001582 return this;
1583 }
1584
1585 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001586 * Combine all of the options that have been set and return a new {@link Action}
1587 * object.
1588 * @return the built action
1589 */
1590 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001591 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1592 RemoteInput[] previousDataInputs =
1593 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001594 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001595 for (RemoteInput input : previousDataInputs) {
1596 dataOnlyInputs.add(input);
1597 }
1598 }
1599 List<RemoteInput> textInputs = new ArrayList<>();
1600 if (mRemoteInputs != null) {
1601 for (RemoteInput input : mRemoteInputs) {
1602 if (input.isDataOnly()) {
1603 dataOnlyInputs.add(input);
1604 } else {
1605 textInputs.add(input);
1606 }
1607 }
1608 }
1609 if (!dataOnlyInputs.isEmpty()) {
1610 RemoteInput[] dataInputsArr =
1611 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1612 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1613 }
1614 RemoteInput[] textInputsArr = textInputs.isEmpty()
1615 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1616 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001617 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001618 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001619 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001620
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001621 @Override
1622 public Action clone() {
1623 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001624 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001625 title,
1626 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001627 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001628 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001629 getAllowGeneratedReplies(),
1630 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001631 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001632
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001633 @Override
1634 public int describeContents() {
1635 return 0;
1636 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001637
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001638 @Override
1639 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001640 final Icon ic = getIcon();
1641 if (ic != null) {
1642 out.writeInt(1);
1643 ic.writeToParcel(out, 0);
1644 } else {
1645 out.writeInt(0);
1646 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001647 TextUtils.writeToParcel(title, out, flags);
1648 if (actionIntent != null) {
1649 out.writeInt(1);
1650 actionIntent.writeToParcel(out, flags);
1651 } else {
1652 out.writeInt(0);
1653 }
Griff Hazen959591e2014-05-15 22:26:18 -07001654 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001655 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001656 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001657 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001658 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001659
Griff Hazen959591e2014-05-15 22:26:18 -07001660 public static final Parcelable.Creator<Action> CREATOR =
1661 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001662 public Action createFromParcel(Parcel in) {
1663 return new Action(in);
1664 }
1665 public Action[] newArray(int size) {
1666 return new Action[size];
1667 }
1668 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001669
1670 /**
1671 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1672 * metadata or change options on an action builder.
1673 */
1674 public interface Extender {
1675 /**
1676 * Apply this extender to a notification action builder.
1677 * @param builder the builder to be modified.
1678 * @return the build object for chaining.
1679 */
1680 public Builder extend(Builder builder);
1681 }
1682
1683 /**
1684 * Wearable extender for notification actions. To add extensions to an action,
1685 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1686 * the {@code WearableExtender()} constructor and apply it to a
1687 * {@link android.app.Notification.Action.Builder} using
1688 * {@link android.app.Notification.Action.Builder#extend}.
1689 *
1690 * <pre class="prettyprint">
1691 * Notification.Action action = new Notification.Action.Builder(
1692 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001693 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001694 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001695 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001696 */
1697 public static final class WearableExtender implements Extender {
1698 /** Notification action extra which contains wearable extensions */
1699 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1700
Pete Gastaf6781d2014-10-07 15:17:05 -04001701 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001702 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001703 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1704 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1705 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001706
1707 // Flags bitwise-ored to mFlags
1708 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001709 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001710 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001711
1712 // Default value for flags integer
1713 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1714
1715 private int mFlags = DEFAULT_FLAGS;
1716
Pete Gastaf6781d2014-10-07 15:17:05 -04001717 private CharSequence mInProgressLabel;
1718 private CharSequence mConfirmLabel;
1719 private CharSequence mCancelLabel;
1720
Griff Hazen61a9e862014-05-22 16:05:19 -07001721 /**
1722 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1723 * options.
1724 */
1725 public WearableExtender() {
1726 }
1727
1728 /**
1729 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1730 * wearable options present in an existing notification action.
1731 * @param action the notification action to inspect.
1732 */
1733 public WearableExtender(Action action) {
1734 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1735 if (wearableBundle != null) {
1736 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001737 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1738 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1739 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001740 }
1741 }
1742
1743 /**
1744 * Apply wearable extensions to a notification action that is being built. This is
1745 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1746 * method of {@link android.app.Notification.Action.Builder}.
1747 */
1748 @Override
1749 public Action.Builder extend(Action.Builder builder) {
1750 Bundle wearableBundle = new Bundle();
1751
1752 if (mFlags != DEFAULT_FLAGS) {
1753 wearableBundle.putInt(KEY_FLAGS, mFlags);
1754 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001755 if (mInProgressLabel != null) {
1756 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1757 }
1758 if (mConfirmLabel != null) {
1759 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1760 }
1761 if (mCancelLabel != null) {
1762 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1763 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001764
1765 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1766 return builder;
1767 }
1768
1769 @Override
1770 public WearableExtender clone() {
1771 WearableExtender that = new WearableExtender();
1772 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001773 that.mInProgressLabel = this.mInProgressLabel;
1774 that.mConfirmLabel = this.mConfirmLabel;
1775 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001776 return that;
1777 }
1778
1779 /**
1780 * Set whether this action is available when the wearable device is not connected to
1781 * a companion device. The user can still trigger this action when the wearable device is
1782 * offline, but a visual hint will indicate that the action may not be available.
1783 * Defaults to true.
1784 */
1785 public WearableExtender setAvailableOffline(boolean availableOffline) {
1786 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1787 return this;
1788 }
1789
1790 /**
1791 * Get whether this action is available when the wearable device is not connected to
1792 * a companion device. The user can still trigger this action when the wearable device is
1793 * offline, but a visual hint will indicate that the action may not be available.
1794 * Defaults to true.
1795 */
1796 public boolean isAvailableOffline() {
1797 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1798 }
1799
1800 private void setFlag(int mask, boolean value) {
1801 if (value) {
1802 mFlags |= mask;
1803 } else {
1804 mFlags &= ~mask;
1805 }
1806 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001807
1808 /**
1809 * Set a label to display while the wearable is preparing to automatically execute the
1810 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1811 *
1812 * @param label the label to display while the action is being prepared to execute
1813 * @return this object for method chaining
1814 */
1815 public WearableExtender setInProgressLabel(CharSequence label) {
1816 mInProgressLabel = label;
1817 return this;
1818 }
1819
1820 /**
1821 * Get the label to display while the wearable is preparing to automatically execute
1822 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1823 *
1824 * @return the label to display while the action is being prepared to execute
1825 */
1826 public CharSequence getInProgressLabel() {
1827 return mInProgressLabel;
1828 }
1829
1830 /**
1831 * Set a label to display to confirm that the action should be executed.
1832 * This is usually an imperative verb like "Send".
1833 *
1834 * @param label the label to confirm the action should be executed
1835 * @return this object for method chaining
1836 */
1837 public WearableExtender setConfirmLabel(CharSequence label) {
1838 mConfirmLabel = label;
1839 return this;
1840 }
1841
1842 /**
1843 * Get the label to display to confirm that the action should be executed.
1844 * This is usually an imperative verb like "Send".
1845 *
1846 * @return the label to confirm the action should be executed
1847 */
1848 public CharSequence getConfirmLabel() {
1849 return mConfirmLabel;
1850 }
1851
1852 /**
1853 * Set a label to display to cancel the action.
1854 * This is usually an imperative verb, like "Cancel".
1855 *
1856 * @param label the label to display to cancel the action
1857 * @return this object for method chaining
1858 */
1859 public WearableExtender setCancelLabel(CharSequence label) {
1860 mCancelLabel = label;
1861 return this;
1862 }
1863
1864 /**
1865 * Get the label to display to cancel the action.
1866 * This is usually an imperative verb like "Cancel".
1867 *
1868 * @return the label to display to cancel the action
1869 */
1870 public CharSequence getCancelLabel() {
1871 return mCancelLabel;
1872 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001873
1874 /**
1875 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1876 * platform that it can generate the appropriate transitions.
1877 * @param hintLaunchesActivity {@code true} if the content intent will launch
1878 * an activity and transitions should be generated, false otherwise.
1879 * @return this object for method chaining
1880 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001881 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001882 boolean hintLaunchesActivity) {
1883 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1884 return this;
1885 }
1886
1887 /**
1888 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1889 * platform that it can generate the appropriate transitions
1890 * @return {@code true} if the content intent will launch an activity and transitions
1891 * should be generated, false otherwise. The default value is {@code false} if this was
1892 * never set.
1893 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001894 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001895 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1896 }
Alex Hills9f087612016-06-07 09:08:59 -04001897
1898 /**
1899 * Set a hint that this Action should be displayed inline.
1900 *
1901 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1902 * otherwise
1903 * @return this object for method chaining
1904 */
1905 public WearableExtender setHintDisplayActionInline(
1906 boolean hintDisplayInline) {
1907 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1908 return this;
1909 }
1910
1911 /**
1912 * Get a hint that this Action should be displayed inline.
1913 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001914 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001915 * otherwise. The default value is {@code false} if this was never set.
1916 */
1917 public boolean getHintDisplayActionInline() {
1918 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1919 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001920 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001921
1922 /**
1923 * Provides meaning to an {@link Action} that hints at what the associated
1924 * {@link PendingIntent} will do. For example, an {@link Action} with a
1925 * {@link PendingIntent} that replies to a text message notification may have the
1926 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1927 *
1928 * @hide
1929 */
1930 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1931 SEMANTIC_ACTION_NONE,
1932 SEMANTIC_ACTION_REPLY,
1933 SEMANTIC_ACTION_MARK_AS_READ,
1934 SEMANTIC_ACTION_MARK_AS_UNREAD,
1935 SEMANTIC_ACTION_DELETE,
1936 SEMANTIC_ACTION_ARCHIVE,
1937 SEMANTIC_ACTION_MUTE,
1938 SEMANTIC_ACTION_UNMUTE,
1939 SEMANTIC_ACTION_THUMBS_UP,
1940 SEMANTIC_ACTION_THUMBS_DOWN
1941 })
1942 @Retention(RetentionPolicy.SOURCE)
1943 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001944 }
1945
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001946 /**
1947 * Array of all {@link Action} structures attached to this notification by
1948 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1949 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1950 * interface for invoking actions.
1951 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001952 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001953
1954 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001955 * Replacement version of this notification whose content will be shown
1956 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1957 * and {@link #VISIBILITY_PUBLIC}.
1958 */
1959 public Notification publicVersion;
1960
1961 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001962 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001963 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 */
1965 public Notification()
1966 {
1967 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001968 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001969 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971
1972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 * @hide
1974 */
1975 public Notification(Context context, int icon, CharSequence tickerText, long when,
1976 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1977 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001978 new Builder(context)
1979 .setWhen(when)
1980 .setSmallIcon(icon)
1981 .setTicker(tickerText)
1982 .setContentTitle(contentTitle)
1983 .setContentText(contentText)
1984 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1985 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
1987
1988 /**
1989 * Constructs a Notification object with the information needed to
1990 * have a status bar icon without the standard expanded view.
1991 *
1992 * @param icon The resource id of the icon to put in the status bar.
1993 * @param tickerText The text that flows by in the status bar when the notification first
1994 * activates.
1995 * @param when The time to show in the time field. In the System.currentTimeMillis
1996 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001997 *
1998 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002000 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 public Notification(int icon, CharSequence tickerText, long when)
2002 {
2003 this.icon = icon;
2004 this.tickerText = tickerText;
2005 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002006 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008
2009 /**
2010 * Unflatten the notification from a parcel.
2011 */
Svet Ganovddb94882016-06-23 19:55:24 -07002012 @SuppressWarnings("unchecked")
2013 public Notification(Parcel parcel) {
2014 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2015 // intents in extras are always written as the last entry.
2016 readFromParcelImpl(parcel);
2017 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002018 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002019 }
2020
2021 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 {
2023 int version = parcel.readInt();
2024
Adrian Roosfb921842017-10-26 14:49:56 +02002025 mWhitelistToken = parcel.readStrongBinder();
2026 if (mWhitelistToken == null) {
2027 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002028 }
2029 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002030 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002033 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002034 if (parcel.readInt() != 0) {
2035 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002036 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2037 icon = mSmallIcon.getResId();
2038 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 number = parcel.readInt();
2041 if (parcel.readInt() != 0) {
2042 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2043 }
2044 if (parcel.readInt() != 0) {
2045 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2046 }
2047 if (parcel.readInt() != 0) {
2048 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2049 }
2050 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002051 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002052 }
2053 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2055 }
Joe Onorato561d3852010-11-20 18:09:34 -08002056 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002057 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 defaults = parcel.readInt();
2060 flags = parcel.readInt();
2061 if (parcel.readInt() != 0) {
2062 sound = Uri.CREATOR.createFromParcel(parcel);
2063 }
2064
2065 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002066 if (parcel.readInt() != 0) {
2067 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 vibrate = parcel.createLongArray();
2070 ledARGB = parcel.readInt();
2071 ledOnMS = parcel.readInt();
2072 ledOffMS = parcel.readInt();
2073 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002074
2075 if (parcel.readInt() != 0) {
2076 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2077 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002078
2079 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002080
John Spurlockfd7f1e02014-03-18 16:41:57 -04002081 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002082
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002083 mGroupKey = parcel.readString();
2084
2085 mSortKey = parcel.readString();
2086
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002087 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002088 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002089
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002090 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2091
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002092 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002093 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2094 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002095
Chris Wren8fd39ec2014-02-27 17:43:26 -05002096 if (parcel.readInt() != 0) {
2097 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2098 }
2099
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002100 visibility = parcel.readInt();
2101
2102 if (parcel.readInt() != 0) {
2103 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2104 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002105
2106 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002107
2108 if (parcel.readInt() != 0) {
2109 mChannelId = parcel.readString();
2110 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002111 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002112
2113 if (parcel.readInt() != 0) {
2114 mShortcutId = parcel.readString();
2115 }
2116
2117 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002118
2119 if (parcel.readInt() != 0) {
2120 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2121 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002122
2123 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
2125
Andy Stadler110988c2010-12-03 14:29:16 -08002126 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002127 public Notification clone() {
2128 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002129 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002130 return that;
2131 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002132
Daniel Sandler1a497d32013-04-18 14:52:45 -04002133 /**
2134 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2135 * of this into that.
2136 * @hide
2137 */
2138 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002139 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002140 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002141 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002142 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002143 that.number = this.number;
2144
2145 // PendingIntents are global, so there's no reason (or way) to clone them.
2146 that.contentIntent = this.contentIntent;
2147 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002148 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002149
2150 if (this.tickerText != null) {
2151 that.tickerText = this.tickerText.toString();
2152 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002153 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002154 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002155 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002156 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002157 that.contentView = this.contentView.clone();
2158 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002159 if (heavy && this.mLargeIcon != null) {
2160 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002161 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002162 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002163 that.sound = this.sound; // android.net.Uri is immutable
2164 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002165 if (this.audioAttributes != null) {
2166 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2167 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002168
2169 final long[] vibrate = this.vibrate;
2170 if (vibrate != null) {
2171 final int N = vibrate.length;
2172 final long[] vib = that.vibrate = new long[N];
2173 System.arraycopy(vibrate, 0, vib, 0, N);
2174 }
2175
2176 that.ledARGB = this.ledARGB;
2177 that.ledOnMS = this.ledOnMS;
2178 that.ledOffMS = this.ledOffMS;
2179 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002180
Joe Onorato18e69df2010-05-17 22:26:12 -07002181 that.flags = this.flags;
2182
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002183 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002184
John Spurlockfd7f1e02014-03-18 16:41:57 -04002185 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002186
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002187 that.mGroupKey = this.mGroupKey;
2188
2189 that.mSortKey = this.mSortKey;
2190
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002191 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002192 try {
2193 that.extras = new Bundle(this.extras);
2194 // will unparcel
2195 that.extras.size();
2196 } catch (BadParcelableException e) {
2197 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2198 that.extras = null;
2199 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002200 }
2201
Felipe Lemedd85da62016-06-28 11:29:54 -07002202 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2203 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002204 }
2205
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002206 if (this.actions != null) {
2207 that.actions = new Action[this.actions.length];
2208 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002209 if ( this.actions[i] != null) {
2210 that.actions[i] = this.actions[i].clone();
2211 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002212 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002213 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002214
Daniel Sandler1a497d32013-04-18 14:52:45 -04002215 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002216 that.bigContentView = this.bigContentView.clone();
2217 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002218
Chris Wren8fd39ec2014-02-27 17:43:26 -05002219 if (heavy && this.headsUpContentView != null) {
2220 that.headsUpContentView = this.headsUpContentView.clone();
2221 }
2222
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002223 that.visibility = this.visibility;
2224
2225 if (this.publicVersion != null) {
2226 that.publicVersion = new Notification();
2227 this.publicVersion.cloneInto(that.publicVersion, heavy);
2228 }
2229
Dan Sandler26e81cf2014-05-06 10:01:27 -04002230 that.color = this.color;
2231
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002232 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002233 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002234 that.mShortcutId = this.mShortcutId;
2235 that.mBadgeIcon = this.mBadgeIcon;
2236 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002237 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002238
Daniel Sandler1a497d32013-04-18 14:52:45 -04002239 if (!heavy) {
2240 that.lightenPayload(); // will clean out extras
2241 }
2242 }
2243
2244 /**
2245 * Removes heavyweight parts of the Notification object for archival or for sending to
2246 * listeners when the full contents are not necessary.
2247 * @hide
2248 */
2249 public final void lightenPayload() {
2250 tickerView = null;
2251 contentView = null;
2252 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002253 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002254 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002255 if (extras != null && !extras.isEmpty()) {
2256 final Set<String> keyset = extras.keySet();
2257 final int N = keyset.size();
2258 final String[] keys = keyset.toArray(new String[N]);
2259 for (int i=0; i<N; i++) {
2260 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002261 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2262 continue;
2263 }
Dan Sandler50128532015-12-08 15:42:41 -05002264 final Object obj = extras.get(key);
2265 if (obj != null &&
2266 ( obj instanceof Parcelable
2267 || obj instanceof Parcelable[]
2268 || obj instanceof SparseArray
2269 || obj instanceof ArrayList)) {
2270 extras.remove(key);
2271 }
2272 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002273 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002274 }
2275
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002276 /**
2277 * Make sure this CharSequence is safe to put into a bundle, which basically
2278 * means it had better not be some custom Parcelable implementation.
2279 * @hide
2280 */
2281 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002282 if (cs == null) return cs;
2283 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2284 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2285 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002286 if (cs instanceof Parcelable) {
2287 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2288 + " instance is a custom Parcelable and not allowed in Notification");
2289 return cs.toString();
2290 }
Selim Cinek60a54252016-02-26 17:03:25 -08002291 return removeTextSizeSpans(cs);
2292 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002293
Selim Cinek60a54252016-02-26 17:03:25 -08002294 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2295 if (charSequence instanceof Spanned) {
2296 Spanned ss = (Spanned) charSequence;
2297 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2298 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2299 for (Object span : spans) {
2300 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002301 if (resultSpan instanceof CharacterStyle) {
2302 resultSpan = ((CharacterStyle) span).getUnderlying();
2303 }
2304 if (resultSpan instanceof TextAppearanceSpan) {
2305 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002306 resultSpan = new TextAppearanceSpan(
2307 originalSpan.getFamily(),
2308 originalSpan.getTextStyle(),
2309 -1,
2310 originalSpan.getTextColor(),
2311 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002312 } else if (resultSpan instanceof RelativeSizeSpan
2313 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002314 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002315 } else {
2316 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002317 }
2318 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2319 ss.getSpanFlags(span));
2320 }
2321 return builder;
2322 }
2323 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002324 }
2325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 public int describeContents() {
2327 return 0;
2328 }
2329
2330 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002331 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 */
Svet Ganovddb94882016-06-23 19:55:24 -07002333 public void writeToParcel(Parcel parcel, int flags) {
2334 // We need to mark all pending intents getting into the notification
2335 // system as being put there to later allow the notification ranker
2336 // to launch them and by doing so add the app to the battery saver white
2337 // list for a short period of time. The problem is that the system
2338 // cannot look into the extras as there may be parcelables there that
2339 // the platform does not know how to handle. To go around that we have
2340 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002341 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002342 if (collectPendingIntents) {
2343 PendingIntent.setOnMarshaledListener(
2344 (PendingIntent intent, Parcel out, int outFlags) -> {
2345 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002346 if (allPendingIntents == null) {
2347 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002348 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002349 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002350 }
2351 });
2352 }
2353 try {
2354 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002355 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002356 writeToParcelImpl(parcel, flags);
2357 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002358 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002359 } finally {
2360 if (collectPendingIntents) {
2361 PendingIntent.setOnMarshaledListener(null);
2362 }
2363 }
2364 }
2365
2366 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 parcel.writeInt(1);
2368
Adrian Roosfb921842017-10-26 14:49:56 +02002369 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002371 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002372 if (mSmallIcon == null && icon != 0) {
2373 // you snuck an icon in here without using the builder; let's try to keep it
2374 mSmallIcon = Icon.createWithResource("", icon);
2375 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002376 if (mSmallIcon != null) {
2377 parcel.writeInt(1);
2378 mSmallIcon.writeToParcel(parcel, 0);
2379 } else {
2380 parcel.writeInt(0);
2381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 parcel.writeInt(number);
2383 if (contentIntent != null) {
2384 parcel.writeInt(1);
2385 contentIntent.writeToParcel(parcel, 0);
2386 } else {
2387 parcel.writeInt(0);
2388 }
2389 if (deleteIntent != null) {
2390 parcel.writeInt(1);
2391 deleteIntent.writeToParcel(parcel, 0);
2392 } else {
2393 parcel.writeInt(0);
2394 }
2395 if (tickerText != null) {
2396 parcel.writeInt(1);
2397 TextUtils.writeToParcel(tickerText, parcel, flags);
2398 } else {
2399 parcel.writeInt(0);
2400 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002401 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002402 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002403 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002404 } else {
2405 parcel.writeInt(0);
2406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 if (contentView != null) {
2408 parcel.writeInt(1);
2409 contentView.writeToParcel(parcel, 0);
2410 } else {
2411 parcel.writeInt(0);
2412 }
Selim Cinek279fa862016-06-14 10:57:25 -07002413 if (mLargeIcon == null && largeIcon != null) {
2414 // you snuck an icon in here without using the builder; let's try to keep it
2415 mLargeIcon = Icon.createWithBitmap(largeIcon);
2416 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002417 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002418 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002419 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002420 } else {
2421 parcel.writeInt(0);
2422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423
2424 parcel.writeInt(defaults);
2425 parcel.writeInt(this.flags);
2426
2427 if (sound != null) {
2428 parcel.writeInt(1);
2429 sound.writeToParcel(parcel, 0);
2430 } else {
2431 parcel.writeInt(0);
2432 }
2433 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002434
2435 if (audioAttributes != null) {
2436 parcel.writeInt(1);
2437 audioAttributes.writeToParcel(parcel, 0);
2438 } else {
2439 parcel.writeInt(0);
2440 }
2441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 parcel.writeLongArray(vibrate);
2443 parcel.writeInt(ledARGB);
2444 parcel.writeInt(ledOnMS);
2445 parcel.writeInt(ledOffMS);
2446 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002447
2448 if (fullScreenIntent != null) {
2449 parcel.writeInt(1);
2450 fullScreenIntent.writeToParcel(parcel, 0);
2451 } else {
2452 parcel.writeInt(0);
2453 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002454
2455 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002456
John Spurlockfd7f1e02014-03-18 16:41:57 -04002457 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002458
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002459 parcel.writeString(mGroupKey);
2460
2461 parcel.writeString(mSortKey);
2462
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002463 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002464
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002465 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002466
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002467 if (bigContentView != null) {
2468 parcel.writeInt(1);
2469 bigContentView.writeToParcel(parcel, 0);
2470 } else {
2471 parcel.writeInt(0);
2472 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002473
Chris Wren8fd39ec2014-02-27 17:43:26 -05002474 if (headsUpContentView != null) {
2475 parcel.writeInt(1);
2476 headsUpContentView.writeToParcel(parcel, 0);
2477 } else {
2478 parcel.writeInt(0);
2479 }
2480
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002481 parcel.writeInt(visibility);
2482
2483 if (publicVersion != null) {
2484 parcel.writeInt(1);
2485 publicVersion.writeToParcel(parcel, 0);
2486 } else {
2487 parcel.writeInt(0);
2488 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002489
2490 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002491
2492 if (mChannelId != null) {
2493 parcel.writeInt(1);
2494 parcel.writeString(mChannelId);
2495 } else {
2496 parcel.writeInt(0);
2497 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002498 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002499
2500 if (mShortcutId != null) {
2501 parcel.writeInt(1);
2502 parcel.writeString(mShortcutId);
2503 } else {
2504 parcel.writeInt(0);
2505 }
2506
2507 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002508
2509 if (mSettingsText != null) {
2510 parcel.writeInt(1);
2511 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2512 } else {
2513 parcel.writeInt(0);
2514 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002515
2516 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518
2519 /**
2520 * Parcelable.Creator that instantiates Notification objects
2521 */
2522 public static final Parcelable.Creator<Notification> CREATOR
2523 = new Parcelable.Creator<Notification>()
2524 {
2525 public Notification createFromParcel(Parcel parcel)
2526 {
2527 return new Notification(parcel);
2528 }
2529
2530 public Notification[] newArray(int size)
2531 {
2532 return new Notification[size];
2533 }
2534 };
2535
2536 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002537 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2538 * <p>
2539 * For backwards compatibility {@code extras} holds some references to "real" member data such
2540 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2541 * fine as long as the object stays in one process.
2542 * <p>
2543 * However, once the notification goes into a parcel each reference gets marshalled separately,
2544 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2545 */
2546 private void fixDuplicateExtras() {
2547 if (extras != null) {
2548 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2549 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2550 }
2551 }
2552
2553 /**
2554 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2555 * separate object, replace it with the field's version to avoid holding duplicate copies.
2556 */
2557 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2558 if (original != null && extras.getParcelable(extraName) != null) {
2559 extras.putParcelable(extraName, original);
2560 }
2561 }
2562
2563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2565 * layout.
2566 *
2567 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2568 * in the view.</p>
2569 * @param context The context for your application / activity.
2570 * @param contentTitle The title that goes in the expanded entry.
2571 * @param contentText The text that goes in the expanded entry.
2572 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2573 * If this is an activity, it must include the
2574 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002575 * that you take care of task management as described in the
2576 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2577 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002578 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002579 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002580 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002582 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 public void setLatestEventInfo(Context context,
2584 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002585 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2586 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2587 new Throwable());
2588 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002589
Selim Cinek4ac6f602016-06-13 15:47:03 -07002590 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2591 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2592 }
2593
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002594 // ensure that any information already set directly is preserved
2595 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002596
2597 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002599 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 }
2601 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002602 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002604 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002605
2606 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 }
2608
Julia Reynoldsda303542015-11-23 14:00:20 -05002609 /**
2610 * @hide
2611 */
2612 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002613 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002614 }
2615
2616 /**
2617 * @hide
2618 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002619 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002620 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002621 }
2622
Yi Jin6b514142017-10-30 14:54:12 -07002623 /**
2624 * @hide
2625 */
2626 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2627 long token = proto.start(fieldId);
2628 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2629 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2630 proto.write(NotificationProto.FLAGS, this.flags);
2631 proto.write(NotificationProto.COLOR, this.color);
2632 proto.write(NotificationProto.CATEGORY, this.category);
2633 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2634 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2635 if (this.actions != null) {
2636 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2637 }
2638 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2639 proto.write(NotificationProto.VISIBILITY, this.visibility);
2640 }
2641 if (publicVersion != null) {
2642 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2643 }
2644 proto.end(token);
2645 }
2646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 @Override
2648 public String toString() {
2649 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002650 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002651 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002652 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002653 sb.append(priority);
2654 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002655 if (contentView != null) {
2656 sb.append(contentView.getPackage());
2657 sb.append("/0x");
2658 sb.append(Integer.toHexString(contentView.getLayoutId()));
2659 } else {
2660 sb.append("null");
2661 }
2662 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002663 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2664 sb.append("default");
2665 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 int N = this.vibrate.length-1;
2667 sb.append("[");
2668 for (int i=0; i<N; i++) {
2669 sb.append(this.vibrate[i]);
2670 sb.append(',');
2671 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002672 if (N != -1) {
2673 sb.append(this.vibrate[N]);
2674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 } else {
2677 sb.append("null");
2678 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002679 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002680 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002682 } else if (this.sound != null) {
2683 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 } else {
2685 sb.append("null");
2686 }
Chris Wren365b6d32015-07-16 10:39:26 -04002687 if (this.tickerText != null) {
2688 sb.append(" tick");
2689 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002690 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002692 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002693 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002694 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002695 if (this.category != null) {
2696 sb.append(" category=");
2697 sb.append(this.category);
2698 }
2699 if (this.mGroupKey != null) {
2700 sb.append(" groupKey=");
2701 sb.append(this.mGroupKey);
2702 }
2703 if (this.mSortKey != null) {
2704 sb.append(" sortKey=");
2705 sb.append(this.mSortKey);
2706 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002707 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002708 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002709 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002710 }
2711 sb.append(" vis=");
2712 sb.append(visibilityToString(this.visibility));
2713 if (this.publicVersion != null) {
2714 sb.append(" publicVersion=");
2715 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002716 }
2717 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 return sb.toString();
2719 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002720
Dan Sandler1b718782014-07-18 12:43:45 -04002721 /**
2722 * {@hide}
2723 */
2724 public static String visibilityToString(int vis) {
2725 switch (vis) {
2726 case VISIBILITY_PRIVATE:
2727 return "PRIVATE";
2728 case VISIBILITY_PUBLIC:
2729 return "PUBLIC";
2730 case VISIBILITY_SECRET:
2731 return "SECRET";
2732 default:
2733 return "UNKNOWN(" + String.valueOf(vis) + ")";
2734 }
2735 }
2736
Joe Onoratocb109a02011-01-18 17:57:41 -08002737 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002738 * {@hide}
2739 */
2740 public static String priorityToString(@Priority int pri) {
2741 switch (pri) {
2742 case PRIORITY_MIN:
2743 return "MIN";
2744 case PRIORITY_LOW:
2745 return "LOW";
2746 case PRIORITY_DEFAULT:
2747 return "DEFAULT";
2748 case PRIORITY_HIGH:
2749 return "HIGH";
2750 case PRIORITY_MAX:
2751 return "MAX";
2752 default:
2753 return "UNKNOWN(" + String.valueOf(pri) + ")";
2754 }
2755 }
2756
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002757 /**
2758 * @hide
2759 */
2760 public boolean hasCompletedProgress() {
2761 // not a progress notification; can't be complete
2762 if (!extras.containsKey(EXTRA_PROGRESS)
2763 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2764 return false;
2765 }
2766 // many apps use max 0 for 'indeterminate'; not complete
2767 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2768 return false;
2769 }
2770 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2771 }
2772
Jeff Sharkey000ce802017-04-29 13:13:27 -06002773 /** @removed */
2774 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002775 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002776 return mChannelId;
2777 }
2778
2779 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002780 * Returns the id of the channel this notification posts to.
2781 */
2782 public String getChannelId() {
2783 return mChannelId;
2784 }
2785
Jeff Sharkey000ce802017-04-29 13:13:27 -06002786 /** @removed */
2787 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002788 public long getTimeout() {
2789 return mTimeout;
2790 }
2791
2792 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002793 * Returns the duration from posting after which this notification should be canceled by the
2794 * system, if it's not canceled already.
2795 */
2796 public long getTimeoutAfter() {
2797 return mTimeout;
2798 }
2799
2800 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002801 * Returns what icon should be shown for this notification if it is being displayed in a
2802 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2803 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2804 */
2805 public int getBadgeIconType() {
2806 return mBadgeIcon;
2807 }
2808
2809 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002810 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002811 *
2812 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2813 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002814 */
2815 public String getShortcutId() {
2816 return mShortcutId;
2817 }
2818
Julia Reynolds3aedded2017-03-31 14:42:09 -04002819
2820 /**
2821 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2822 */
2823 public CharSequence getSettingsText() {
2824 return mSettingsText;
2825 }
2826
Julia Reynolds13d898c2017-02-02 12:22:05 -05002827 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002828 * Returns which type of notifications in a group are responsible for audibly alerting the
2829 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2830 * {@link #GROUP_ALERT_SUMMARY}.
2831 */
2832 public @GroupAlertBehavior int getGroupAlertBehavior() {
2833 return mGroupAlertBehavior;
2834 }
2835
2836 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002837 * The small icon representing this notification in the status bar and content view.
2838 *
2839 * @return the small icon representing this notification.
2840 *
2841 * @see Builder#getSmallIcon()
2842 * @see Builder#setSmallIcon(Icon)
2843 */
2844 public Icon getSmallIcon() {
2845 return mSmallIcon;
2846 }
2847
2848 /**
2849 * Used when notifying to clean up legacy small icons.
2850 * @hide
2851 */
2852 public void setSmallIcon(Icon icon) {
2853 mSmallIcon = icon;
2854 }
2855
2856 /**
2857 * The large icon shown in this notification's content view.
2858 * @see Builder#getLargeIcon()
2859 * @see Builder#setLargeIcon(Icon)
2860 */
2861 public Icon getLargeIcon() {
2862 return mLargeIcon;
2863 }
2864
2865 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002866 * @hide
2867 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002868 public boolean isGroupSummary() {
2869 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2870 }
2871
2872 /**
2873 * @hide
2874 */
2875 public boolean isGroupChild() {
2876 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2877 }
2878
2879 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002880 * @hide
2881 */
2882 public boolean suppressAlertingDueToGrouping() {
2883 if (isGroupSummary()
2884 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2885 return true;
2886 } else if (isGroupChild()
2887 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2888 return true;
2889 }
2890 return false;
2891 }
2892
2893 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002894 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002895 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002896 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002897 * content views using the platform's notification layout template. If your app supports
2898 * versions of Android as old as API level 4, you can instead use
2899 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2900 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2901 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002902 *
Scott Main183bf112012-08-13 19:12:13 -07002903 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002904 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002905 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002906 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002907 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2908 * .setContentText(subject)
2909 * .setSmallIcon(R.drawable.new_mail)
2910 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002911 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002912 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002913 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002914 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002915 /**
2916 * @hide
2917 */
2918 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2919 "android.rebuild.contentViewActionCount";
2920 /**
2921 * @hide
2922 */
2923 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2924 = "android.rebuild.bigViewActionCount";
2925 /**
2926 * @hide
2927 */
2928 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2929 = "android.rebuild.hudViewActionCount";
2930
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002931 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002932
Selim Cinek6743c0b2017-01-18 18:24:01 -08002933 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2934 SystemProperties.getBoolean("notifications.only_title", true);
2935
Selim Cinek389edcd2017-05-11 19:16:44 -07002936 /**
2937 * The lightness difference that has to be added to the primary text color to obtain the
2938 * secondary text color when the background is light.
2939 */
2940 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2941
2942 /**
2943 * The lightness difference that has to be added to the primary text color to obtain the
2944 * secondary text color when the background is dark.
2945 * A bit less then the above value, since it looks better on dark backgrounds.
2946 */
2947 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2948
Joe Onorato46439ce2010-11-19 13:56:21 -08002949 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002950 private Notification mN;
2951 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002952 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002953 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002954 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002955 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002956 private boolean mIsLegacy;
2957 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002958
2959 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002960 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2961 */
2962 private int mCachedContrastColor = COLOR_INVALID;
2963 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002964 /**
2965 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2966 */
2967 private int mCachedAmbientColor = COLOR_INVALID;
2968 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002969
2970 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002971 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2972 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2973 */
2974 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002975 private int mTextColorsAreForBackground = COLOR_INVALID;
2976 private int mPrimaryTextColor = COLOR_INVALID;
2977 private int mSecondaryTextColor = COLOR_INVALID;
2978 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002979 private int mBackgroundColor = COLOR_INVALID;
2980 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002981 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002982 /**
2983 * A temporary location where actions are stored. If != null the view originally has action
2984 * but doesn't have any for this inflation.
2985 */
2986 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002987 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002988
Anthony Chenad4d1582017-04-10 16:07:58 -07002989 private boolean mTintActionButtons;
2990 private boolean mInNightMode;
2991
Adrian Roos70d7aa32017-01-11 15:39:06 -08002992 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002993 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002994 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002995 * @param context
2996 * A {@link Context} that will be used by the Builder to construct the
2997 * RemoteViews. The Context will not be held past the lifetime of this Builder
2998 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002999 * @param channelId
3000 * The constructed Notification will be posted on this
3001 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3002 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003003 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003004 public Builder(Context context, String channelId) {
3005 this(context, (Notification) null);
3006 mN.mChannelId = channelId;
3007 }
3008
3009 /**
3010 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3011 * instead. All posted Notifications must specify a NotificationChannel Id.
3012 */
3013 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003014 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003015 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003016 }
3017
Joe Onoratocb109a02011-01-18 17:57:41 -08003018 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003019 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003020 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003021 public Builder(Context context, Notification toAdopt) {
3022 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003023 Resources res = mContext.getResources();
3024 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3025
3026 if (res.getBoolean(R.bool.config_enableNightMode)) {
3027 Configuration currentConfig = res.getConfiguration();
3028 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3029 == Configuration.UI_MODE_NIGHT_YES;
3030 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003031
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003032 if (toAdopt == null) {
3033 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003034 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3035 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3036 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003037 mN.priority = PRIORITY_DEFAULT;
3038 mN.visibility = VISIBILITY_PRIVATE;
3039 } else {
3040 mN = toAdopt;
3041 if (mN.actions != null) {
3042 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003043 }
3044
Selim Cineke7238dd2017-12-14 17:48:32 -08003045 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3046 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3047 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003048 }
3049
Selim Cinek4ac6f602016-06-13 15:47:03 -07003050 if (mN.getSmallIcon() == null && mN.icon != 0) {
3051 setSmallIcon(mN.icon);
3052 }
3053
3054 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3055 setLargeIcon(mN.largeIcon);
3056 }
3057
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3059 if (!TextUtils.isEmpty(templateClass)) {
3060 final Class<? extends Style> styleClass
3061 = getNotificationStyleClass(templateClass);
3062 if (styleClass == null) {
3063 Log.d(TAG, "Unknown style class: " + templateClass);
3064 } else {
3065 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003066 final Constructor<? extends Style> ctor =
3067 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003068 ctor.setAccessible(true);
3069 final Style style = ctor.newInstance();
3070 style.restoreFromExtras(mN.extras);
3071
3072 if (style != null) {
3073 setStyle(style);
3074 }
3075 } catch (Throwable t) {
3076 Log.e(TAG, "Could not create Style", t);
3077 }
3078 }
3079 }
3080
3081 }
3082 }
3083
3084 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003085 if (mColorUtil == null) {
3086 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003087 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003088 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003089 }
3090
3091 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003092 * If this notification is duplicative of a Launcher shortcut, sets the
3093 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3094 * the shortcut.
3095 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003096 * This field will be ignored by Launchers that don't support badging, don't show
3097 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003098 *
3099 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3100 * supersedes
3101 */
3102 public Builder setShortcutId(String shortcutId) {
3103 mN.mShortcutId = shortcutId;
3104 return this;
3105 }
3106
3107 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003108 * Sets which icon to display as a badge for this notification.
3109 *
3110 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3111 * {@link #BADGE_ICON_LARGE}.
3112 *
3113 * Note: This value might be ignored, for launchers that don't support badge icons.
3114 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003115 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003116 mN.mBadgeIcon = icon;
3117 return this;
3118 }
3119
3120 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003121 * Sets the group alert behavior for this notification. Use this method to mute this
3122 * notification if alerts for this notification's group should be handled by a different
3123 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003124 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3125 * mute. For example, if you want only the summary of your group to make noise, all
3126 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003127 *
3128 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3129 */
3130 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3131 mN.mGroupAlertBehavior = groupAlertBehavior;
3132 return this;
3133 }
3134
Jeff Sharkey000ce802017-04-29 13:13:27 -06003135 /** @removed */
3136 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003137 public Builder setChannel(String channelId) {
3138 mN.mChannelId = channelId;
3139 return this;
3140 }
3141
3142 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003143 * Specifies the channel the notification should be delivered on.
3144 */
3145 public Builder setChannelId(String channelId) {
3146 mN.mChannelId = channelId;
3147 return this;
3148 }
3149
Jeff Sharkey000ce802017-04-29 13:13:27 -06003150 /** @removed */
3151 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003152 public Builder setTimeout(long durationMs) {
3153 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003154 return this;
3155 }
3156
3157 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003158 * Specifies a duration in milliseconds after which this notification should be canceled,
3159 * if it is not already canceled.
3160 */
3161 public Builder setTimeoutAfter(long durationMs) {
3162 mN.mTimeout = durationMs;
3163 return this;
3164 }
3165
3166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003167 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003168 *
3169 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3170 * shown anymore by default and must be opted into by using
3171 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003172 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003173 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003174 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003175 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003176 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 return this;
3178 }
3179
Joe Onoratocb109a02011-01-18 17:57:41 -08003180 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003181 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003182 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003183 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3184 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003185 */
3186 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003187 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003188 return this;
3189 }
3190
3191 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003192 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003193 *
3194 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003195 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003196 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003197 * Useful when showing an elapsed time (like an ongoing phone call).
3198 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003199 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003200 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003201 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003202 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003203 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003204 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003205 */
3206 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003207 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003208 return this;
3209 }
3210
3211 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003212 * Sets the Chronometer to count down instead of counting up.
3213 *
3214 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3215 * If it isn't set the chronometer will count up.
3216 *
3217 * @see #setUsesChronometer(boolean)
3218 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003219 public Builder setChronometerCountDown(boolean countDown) {
3220 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003221 return this;
3222 }
3223
3224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003225 * Set the small icon resource, which will be used to represent the notification in the
3226 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003228
3229 * The platform template for the expanded view will draw this icon in the left, unless a
3230 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3231 * icon will be moved to the right-hand side.
3232 *
3233
3234 * @param icon
3235 * A resource ID in the application's package of the drawable to use.
3236 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003238 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003239 return setSmallIcon(icon != 0
3240 ? Icon.createWithResource(mContext, icon)
3241 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003242 }
3243
Joe Onoratocb109a02011-01-18 17:57:41 -08003244 /**
3245 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3246 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3247 * LevelListDrawable}.
3248 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003249 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003250 * @param level The level to use for the icon.
3251 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003252 * @see Notification#icon
3253 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003254 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003255 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003256 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003257 return setSmallIcon(icon);
3258 }
3259
3260 /**
3261 * Set the small icon, which will be used to represent the notification in the
3262 * status bar and content view (unless overriden there by a
3263 * {@link #setLargeIcon(Bitmap) large icon}).
3264 *
3265 * @param icon An Icon object to use.
3266 * @see Notification#icon
3267 */
3268 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003269 mN.setSmallIcon(icon);
3270 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3271 mN.icon = icon.getResId();
3272 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003273 return this;
3274 }
3275
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003277 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003278 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003279 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003280 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 return this;
3282 }
3283
Joe Onoratocb109a02011-01-18 17:57:41 -08003284 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003285 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003286 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003287 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003288 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 return this;
3290 }
3291
Joe Onoratocb109a02011-01-18 17:57:41 -08003292 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003293 * This provides some additional information that is displayed in the notification. No
3294 * guarantees are given where exactly it is displayed.
3295 *
3296 * <p>This information should only be provided if it provides an essential
3297 * benefit to the understanding of the notification. The more text you provide the
3298 * less readable it becomes. For example, an email client should only provide the account
3299 * name here if more than one email account has been added.</p>
3300 *
3301 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3302 * notification header area.
3303 *
3304 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3305 * this will be shown in the third line of text in the platform notification template.
3306 * You should not be using {@link #setProgress(int, int, boolean)} at the
3307 * same time on those versions; they occupy the same place.
3308 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003309 */
3310 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003311 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003312 return this;
3313 }
3314
3315 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003316 * Provides text that will appear as a link to your application's settings.
3317 *
3318 * <p>This text does not appear within notification {@link Style templates} but may
3319 * appear when the user uses an affordance to learn more about the notification.
3320 * Additionally, this text will not appear unless you provide a valid link target by
3321 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3322 *
3323 * <p>This text is meant to be concise description about what the user can customize
3324 * when they click on this link. The recommended maximum length is 40 characters.
3325 * @param text
3326 * @return
3327 */
3328 public Builder setSettingsText(CharSequence text) {
3329 mN.mSettingsText = safeCharSequence(text);
3330 return this;
3331 }
3332
3333 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003334 * Set the remote input history.
3335 *
3336 * This should be set to the most recent inputs that have been sent
3337 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3338 * longer relevant (e.g. for chat notifications once the other party has responded).
3339 *
3340 * The most recent input must be stored at the 0 index, the second most recent at the
3341 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3342 * and how much of each individual input is shown.
3343 *
3344 * <p>Note: The reply text will only be shown on notifications that have least one action
3345 * with a {@code RemoteInput}.</p>
3346 */
3347 public Builder setRemoteInputHistory(CharSequence[] text) {
3348 if (text == null) {
3349 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3350 } else {
3351 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3352 CharSequence[] safe = new CharSequence[N];
3353 for (int i = 0; i < N; i++) {
3354 safe[i] = safeCharSequence(text[i]);
3355 }
3356 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3357 }
3358 return this;
3359 }
3360
3361 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003362 * Sets the number of items this notification represents. May be displayed as a badge count
3363 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003364 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003365 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003366 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003367 return this;
3368 }
3369
Joe Onoratocb109a02011-01-18 17:57:41 -08003370 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003371 * A small piece of additional information pertaining to this notification.
3372 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003373 * The platform template will draw this on the last line of the notification, at the far
3374 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003375 *
3376 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3377 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3378 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003379 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003380 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003381 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003382 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003383 return this;
3384 }
3385
Joe Onoratocb109a02011-01-18 17:57:41 -08003386 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003387 * Set the progress this notification represents.
3388 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003389 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003390 */
3391 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003392 mN.extras.putInt(EXTRA_PROGRESS, progress);
3393 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3394 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003395 return this;
3396 }
3397
3398 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003399 * Supply a custom RemoteViews to use instead of the platform template.
3400 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003401 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003402 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003403 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003404 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003405 return setCustomContentView(views);
3406 }
3407
3408 /**
3409 * Supply custom RemoteViews to use instead of the platform template.
3410 *
3411 * This will override the layout that would otherwise be constructed by this Builder
3412 * object.
3413 */
3414 public Builder setCustomContentView(RemoteViews contentView) {
3415 mN.contentView = contentView;
3416 return this;
3417 }
3418
3419 /**
3420 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3421 *
3422 * This will override the expanded layout that would otherwise be constructed by this
3423 * Builder object.
3424 */
3425 public Builder setCustomBigContentView(RemoteViews contentView) {
3426 mN.bigContentView = contentView;
3427 return this;
3428 }
3429
3430 /**
3431 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3432 *
3433 * This will override the heads-up layout that would otherwise be constructed by this
3434 * Builder object.
3435 */
3436 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3437 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003438 return this;
3439 }
3440
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003442 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3443 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003444 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3445 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3446 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003447 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003448 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003449 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003450 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003451 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003452 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003453 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003454 return this;
3455 }
3456
Joe Onoratocb109a02011-01-18 17:57:41 -08003457 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003458 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3459 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003460 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003462 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003464 return this;
3465 }
3466
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 /**
3468 * An intent to launch instead of posting the notification to the status bar.
3469 * Only for use with extremely high-priority notifications demanding the user's
3470 * <strong>immediate</strong> attention, such as an incoming phone call or
3471 * alarm clock that the user has explicitly set to a particular time.
3472 * If this facility is used for something else, please give the user an option
3473 * to turn it off and use a normal notification, as this can be extremely
3474 * disruptive.
3475 *
Chris Wren47c20a12014-06-18 17:27:29 -04003476 * <p>
3477 * The system UI may choose to display a heads-up notification, instead of
3478 * launching this intent, while the user is using the device.
3479 * </p>
3480 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003481 * @param intent The pending intent to launch.
3482 * @param highPriority Passing true will cause this notification to be sent
3483 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003484 *
3485 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003486 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003487 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003488 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003489 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3490 return this;
3491 }
3492
Joe Onoratocb109a02011-01-18 17:57:41 -08003493 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003494 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003495 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003496 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003497 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003498 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003500 return this;
3501 }
3502
Joe Onoratocb109a02011-01-18 17:57:41 -08003503 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003504 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003507 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003509 setTicker(tickerText);
3510 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003511 return this;
3512 }
3513
Joe Onoratocb109a02011-01-18 17:57:41 -08003514 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003515 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003516 *
3517 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003518 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3519 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003520 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003521 public Builder setLargeIcon(Bitmap b) {
3522 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3523 }
3524
3525 /**
3526 * Add a large icon to the notification content view.
3527 *
3528 * In the platform template, this image will be shown on the left of the notification view
3529 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3530 * badge atop the large icon).
3531 */
3532 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003533 mN.mLargeIcon = icon;
3534 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003535 return this;
3536 }
3537
Joe Onoratocb109a02011-01-18 17:57:41 -08003538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003539 * Set the sound to play.
3540 *
John Spurlockc0650f022014-07-19 13:22:39 -04003541 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3542 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003543 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003544 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003545 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003546 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003547 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003548 mN.sound = sound;
3549 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003550 return this;
3551 }
3552
Joe Onoratocb109a02011-01-18 17:57:41 -08003553 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003554 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003555 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003556 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3557 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003558 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003559 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003560 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003561 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003562 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003563 mN.sound = sound;
3564 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003565 return this;
3566 }
3567
Joe Onoratocb109a02011-01-18 17:57:41 -08003568 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003569 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3570 * use during playback.
3571 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003572 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003573 * @see Notification#sound
3574 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003575 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003576 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003577 mN.sound = sound;
3578 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003579 return this;
3580 }
3581
3582 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003583 * Set the vibration pattern to use.
3584 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003585 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3586 * <code>pattern</code> parameter.
3587 *
Chris Wren47c20a12014-06-18 17:27:29 -04003588 * <p>
3589 * A notification that vibrates is more likely to be presented as a heads-up notification.
3590 * </p>
3591 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003592 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003593 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003594 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003595 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003596 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003597 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003598 return this;
3599 }
3600
Joe Onoratocb109a02011-01-18 17:57:41 -08003601 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 * Set the desired color for the indicator LED on the device, as well as the
3603 * blink duty cycle (specified in milliseconds).
3604 *
3605
3606 * Not all devices will honor all (or even any) of these values.
3607 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003608 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003609 * @see Notification#ledARGB
3610 * @see Notification#ledOnMS
3611 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003612 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003613 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003614 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003615 mN.ledARGB = argb;
3616 mN.ledOnMS = onMs;
3617 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003618 if (onMs != 0 || offMs != 0) {
3619 mN.flags |= FLAG_SHOW_LIGHTS;
3620 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003621 return this;
3622 }
3623
Joe Onoratocb109a02011-01-18 17:57:41 -08003624 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003625 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003627
3628 * Ongoing notifications cannot be dismissed by the user, so your application or service
3629 * must take care of canceling them.
3630 *
3631
3632 * They are typically used to indicate a background task that the user is actively engaged
3633 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3634 * (e.g., a file download, sync operation, active network connection).
3635 *
3636
3637 * @see Notification#FLAG_ONGOING_EVENT
3638 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003640 public Builder setOngoing(boolean ongoing) {
3641 setFlag(FLAG_ONGOING_EVENT, ongoing);
3642 return this;
3643 }
3644
Joe Onoratocb109a02011-01-18 17:57:41 -08003645 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003646 * Set whether this notification should be colorized. When set, the color set with
3647 * {@link #setColor(int)} will be used as the background color of this notification.
3648 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003649 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3650 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003651 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003652 * For most styles, the coloring will only be applied if the notification is for a
3653 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003654 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003655 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003656 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003657 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003658 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003659 */
3660 public Builder setColorized(boolean colorize) {
3661 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3662 return this;
3663 }
3664
3665 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003666 * Set this flag if you would only like the sound, vibrate
3667 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003668 *
3669 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003670 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003671 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3672 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3673 return this;
3674 }
3675
Joe Onoratocb109a02011-01-18 17:57:41 -08003676 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003677 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003678 *
3679 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003680 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003681 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003682 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003683 return this;
3684 }
3685
Joe Onoratocb109a02011-01-18 17:57:41 -08003686 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003687 * Set whether or not this notification should not bridge to other devices.
3688 *
3689 * <p>Some notifications can be bridged to other devices for remote display.
3690 * This hint can be set to recommend this notification not be bridged.
3691 */
3692 public Builder setLocalOnly(boolean localOnly) {
3693 setFlag(FLAG_LOCAL_ONLY, localOnly);
3694 return this;
3695 }
3696
3697 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003699 * <p>
3700 * The value should be one or more of the following fields combined with
3701 * bitwise-or:
3702 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3703 * <p>
3704 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003705 *
3706 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003707 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003708 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003710 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003711 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003712 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003713 return this;
3714 }
3715
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003716 /**
3717 * Set the priority of this notification.
3718 *
3719 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003720 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003721 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003722 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003723 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003725 return this;
3726 }
Joe Malin8d40d042012-11-05 11:36:40 -08003727
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003728 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003729 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003730 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003731 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003732 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003733 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003735 return this;
3736 }
3737
3738 /**
Chris Wrendde75302014-03-26 17:24:15 -04003739 * Add a person that is relevant to this notification.
3740 *
Chris Wrene6c48932014-09-29 17:19:27 -04003741 * <P>
3742 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003743 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3744 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3745 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003746 * </P>
3747 *
3748 * <P>
3749 * The person should be specified by the {@code String} representation of a
3750 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3751 * </P>
3752 *
3753 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3754 * URIs. The path part of these URIs must exist in the contacts database, in the
3755 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3756 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003757 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3758 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003759 * </P>
3760 *
3761 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003762 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003763 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003764 */
Chris Wrene6c48932014-09-29 17:19:27 -04003765 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003766 addPerson(new Person().setUri(uri));
3767 return this;
3768 }
3769
3770 /**
3771 * Add a person that is relevant to this notification.
3772 *
3773 * <P>
3774 * Depending on user preferences, this annotation may allow the notification to pass
3775 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3776 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3777 * appear more prominently in the user interface.
3778 * </P>
3779 *
3780 * <P>
3781 * A person should usually contain a uri in order to benefit from the ranking boost.
3782 * However, even if no uri is provided, it's beneficial to provide other people in the
3783 * notification, such that listeners and voice only devices can announce and handle them
3784 * properly.
3785 * </P>
3786 *
3787 * @param person the person to add.
3788 * @see Notification#EXTRA_PEOPLE_LIST
3789 */
3790 public Builder addPerson(Person person) {
3791 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003792 return this;
3793 }
3794
3795 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003796 * Set this notification to be part of a group of notifications sharing the same key.
3797 * Grouped notifications may display in a cluster or stack on devices which
3798 * support such rendering.
3799 *
3800 * <p>To make this notification the summary for its group, also call
3801 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3802 * {@link #setSortKey}.
3803 * @param groupKey The group key of the group.
3804 * @return this object for method chaining
3805 */
3806 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003807 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003808 return this;
3809 }
3810
3811 /**
3812 * Set this notification to be the group summary for a group of notifications.
3813 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003814 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3815 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003816 * @param isGroupSummary Whether this notification should be a group summary.
3817 * @return this object for method chaining
3818 */
3819 public Builder setGroupSummary(boolean isGroupSummary) {
3820 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3821 return this;
3822 }
3823
3824 /**
3825 * Set a sort key that orders this notification among other notifications from the
3826 * same package. This can be useful if an external sort was already applied and an app
3827 * would like to preserve this. Notifications will be sorted lexicographically using this
3828 * value, although providing different priorities in addition to providing sort key may
3829 * cause this value to be ignored.
3830 *
3831 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003832 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003833 *
3834 * @see String#compareTo(String)
3835 */
3836 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003837 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003838 return this;
3839 }
3840
3841 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003842 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003843 *
Griff Hazen720042b2014-02-24 15:46:56 -08003844 * <p>Values within the Bundle will replace existing extras values in this Builder.
3845 *
3846 * @see Notification#extras
3847 */
Griff Hazen959591e2014-05-15 22:26:18 -07003848 public Builder addExtras(Bundle extras) {
3849 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003850 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003851 }
3852 return this;
3853 }
3854
3855 /**
3856 * Set metadata for this notification.
3857 *
3858 * <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 -04003859 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003860 * called.
3861 *
Griff Hazen720042b2014-02-24 15:46:56 -08003862 * <p>Replaces any existing extras values with those from the provided Bundle.
3863 * Use {@link #addExtras} to merge in metadata instead.
3864 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003865 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003866 */
Griff Hazen959591e2014-05-15 22:26:18 -07003867 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003868 if (extras != null) {
3869 mUserExtras = extras;
3870 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003871 return this;
3872 }
3873
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003874 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003875 * Get the current metadata Bundle used by this notification Builder.
3876 *
3877 * <p>The returned Bundle is shared with this Builder.
3878 *
3879 * <p>The current contents of this Bundle are copied into the Notification each time
3880 * {@link #build()} is called.
3881 *
3882 * @see Notification#extras
3883 */
3884 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003885 return mUserExtras;
3886 }
3887
3888 private Bundle getAllExtras() {
3889 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3890 saveExtras.putAll(mN.extras);
3891 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003892 }
3893
3894 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003895 * Add an action to this notification. Actions are typically displayed by
3896 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003897 * <p>
3898 * Every action must have an icon (32dp square and matching the
3899 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3900 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3901 * <p>
3902 * A notification in its expanded form can display up to 3 actions, from left to right in
3903 * the order they were added. Actions will not be displayed when the notification is
3904 * collapsed, however, so be sure that any essential functions may be accessed by the user
3905 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003906 *
3907 * @param icon Resource ID of a drawable that represents the action.
3908 * @param title Text describing the action.
3909 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003910 *
3911 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003912 */
Dan Sandler86647982015-05-13 23:41:13 -04003913 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003914 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003915 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003916 return this;
3917 }
3918
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003919 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003920 * Add an action to this notification. Actions are typically displayed by
3921 * the system as a button adjacent to the notification content.
3922 * <p>
3923 * Every action must have an icon (32dp square and matching the
3924 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3925 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3926 * <p>
3927 * A notification in its expanded form can display up to 3 actions, from left to right in
3928 * the order they were added. Actions will not be displayed when the notification is
3929 * collapsed, however, so be sure that any essential functions may be accessed by the user
3930 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3931 *
3932 * @param action The action to add.
3933 */
3934 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003935 if (action != null) {
3936 mActions.add(action);
3937 }
Griff Hazen959591e2014-05-15 22:26:18 -07003938 return this;
3939 }
3940
3941 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003942 * Alter the complete list of actions attached to this notification.
3943 * @see #addAction(Action).
3944 *
3945 * @param actions
3946 * @return
3947 */
3948 public Builder setActions(Action... actions) {
3949 mActions.clear();
3950 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003951 if (actions[i] != null) {
3952 mActions.add(actions[i]);
3953 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003954 }
3955 return this;
3956 }
3957
3958 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003959 * Add a rich notification style to be applied at build time.
3960 *
3961 * @param style Object responsible for modifying the notification style.
3962 */
3963 public Builder setStyle(Style style) {
3964 if (mStyle != style) {
3965 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003966 if (mStyle != null) {
3967 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3969 } else {
3970 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003971 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003972 }
3973 return this;
3974 }
3975
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003976 /**
3977 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003978 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003979 * @return The same Builder.
3980 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003981 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003983 return this;
3984 }
3985
3986 /**
3987 * Supply a replacement Notification whose contents should be shown in insecure contexts
3988 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3989 * @param n A replacement notification, presumably with some or all info redacted.
3990 * @return The same Builder.
3991 */
3992 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 if (n != null) {
3994 mN.publicVersion = new Notification();
3995 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3996 } else {
3997 mN.publicVersion = null;
3998 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003999 return this;
4000 }
4001
Griff Hazenb720abe2014-05-20 13:15:30 -07004002 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004003 * Apply an extender to this notification builder. Extenders may be used to add
4004 * metadata or change options on this builder.
4005 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004006 public Builder extend(Extender extender) {
4007 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004008 return this;
4009 }
4010
Dan Sandler4e787062015-06-17 15:09:48 -04004011 /**
4012 * @hide
4013 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004014 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004015 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004016 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004017 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004018 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004019 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004020 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004021 }
4022
Dan Sandler26e81cf2014-05-06 10:01:27 -04004023 /**
4024 * Sets {@link Notification#color}.
4025 *
4026 * @param argb The accent color to use
4027 *
4028 * @return The same Builder.
4029 */
Tor Norbye80756e32015-03-02 09:39:27 -08004030 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004031 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004032 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004033 return this;
4034 }
4035
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004036 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004037 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4038 // This user can never be a badged profile,
4039 // and also includes USER_ALL system notifications.
4040 return null;
4041 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004042 // Note: This assumes that the current user can read the profile badge of the
4043 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004044 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004045 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004046 }
4047
4048 private Bitmap getProfileBadge() {
4049 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004050 if (badge == null) {
4051 return null;
4052 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004053 final int size = mContext.getResources().getDimensionPixelSize(
4054 R.dimen.notification_badge_size);
4055 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004056 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004057 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004058 badge.draw(canvas);
4059 return bitmap;
4060 }
4061
Selim Cinekc848c3a2016-01-13 15:27:30 -08004062 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004063 Bitmap profileBadge = getProfileBadge();
4064
Kenny Guy98193ea2014-07-24 19:54:37 +01004065 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004066 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4067 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004068 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004069 contentView.setDrawableTint(R.id.profile_badge, false,
4070 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004071 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004072 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004073 }
4074
Christoph Studerfe718432014-09-01 18:21:18 +02004075 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004076 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004077 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004078 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004079 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004080 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004081 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004082 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004083 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004084 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004085 }
4086
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004087 /**
4088 * Resets the notification header to its original state
4089 */
4090 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004091 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4092 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004093 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004094 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004095 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004096 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004097 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004098 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4099 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004100 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004101 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004102 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004103 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004104 contentView.setImageViewIcon(R.id.profile_badge, null);
4105 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004106 }
4107
4108 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004109 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4110 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004111 }
4112
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004113 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004114 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004115 }
4116
4117 /**
4118 * @param hasProgress whether the progress bar should be shown and set
4119 */
4120 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004121 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4122 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004123 }
4124
Adrian Roos70d7aa32017-01-11 15:39:06 -08004125 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004126 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004127
Christoph Studerfe718432014-09-01 18:21:18 +02004128 resetStandardTemplate(contentView);
4129
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004130 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004131 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004132 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004133 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004134 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4135 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004136 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004137 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004138 if (!p.ambient) {
4139 setTextViewColorPrimary(contentView, R.id.title);
4140 }
Selim Cinek954cc232016-05-20 13:29:23 -07004141 contentView.setViewLayoutWidth(R.id.title, showProgress
4142 ? ViewGroup.LayoutParams.WRAP_CONTENT
4143 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004144 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004145 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004146 int textId = showProgress ? com.android.internal.R.id.text_line_1
4147 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004148 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004149 if (!p.ambient) {
4150 setTextViewColorSecondary(contentView, textId);
4151 }
Selim Cinek41598732016-01-11 16:58:37 -08004152 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004153 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004154
Selim Cinek279fa862016-06-14 10:57:25 -07004155 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004156
Selim Cinek29603462015-11-17 19:04:39 -08004157 return contentView;
4158 }
4159
Selim Cinek48f66b72017-08-18 16:17:51 -07004160 private CharSequence processTextSpans(CharSequence text) {
4161 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004162 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004163 }
4164 return text;
4165 }
4166
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4168 ensureColors();
4169 contentView.setTextColor(id, mPrimaryTextColor);
4170 }
4171
Selim Cinek48f66b72017-08-18 16:17:51 -07004172 private boolean hasForegroundColor() {
4173 return mForegroundColor != COLOR_INVALID;
4174 }
4175
Selim Cinek389edcd2017-05-11 19:16:44 -07004176 /**
4177 * @return the primary text color
4178 * @hide
4179 */
4180 @VisibleForTesting
4181 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004182 ensureColors();
4183 return mPrimaryTextColor;
4184 }
4185
Selim Cinek389edcd2017-05-11 19:16:44 -07004186 /**
4187 * @return the secondary text color
4188 * @hide
4189 */
4190 @VisibleForTesting
4191 public int getSecondaryTextColor() {
4192 ensureColors();
4193 return mSecondaryTextColor;
4194 }
4195
Selim Cinek7b9605b2017-01-19 17:36:00 -08004196 private int getActionBarColor() {
4197 ensureColors();
4198 return mActionBarColor;
4199 }
4200
Selim Cinek622c64a2017-04-17 17:10:05 -07004201 private int getActionBarColorDeEmphasized() {
4202 int backgroundColor = getBackgroundColor();
4203 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4204 }
4205
Selim Cinek7b9605b2017-01-19 17:36:00 -08004206 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4207 ensureColors();
4208 contentView.setTextColor(id, mSecondaryTextColor);
4209 }
4210
4211 private void ensureColors() {
4212 int backgroundColor = getBackgroundColor();
4213 if (mPrimaryTextColor == COLOR_INVALID
4214 || mSecondaryTextColor == COLOR_INVALID
4215 || mActionBarColor == COLOR_INVALID
4216 || mTextColorsAreForBackground != backgroundColor) {
4217 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004218 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004219 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4220 backgroundColor);
4221 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4222 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004223 if (backgroundColor != COLOR_DEFAULT
4224 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4225 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4226 mPrimaryTextColor, backgroundColor, 4.5);
4227 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4228 mSecondaryTextColor, backgroundColor, 4.5);
4229 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004230 } else {
4231 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4232 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4233 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4234 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004235 // We only respect the given colors if worst case Black or White still has
4236 // contrast
4237 boolean backgroundLight = backLum > textLum
4238 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4239 || backLum <= textLum
4240 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004241 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004242 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004243 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4244 mForegroundColor,
4245 backgroundColor,
4246 true /* findFG */,
4247 4.5f);
4248 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004249 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004250 } else {
4251 mSecondaryTextColor =
4252 NotificationColorUtil.findContrastColorAgainstDark(
4253 mForegroundColor,
4254 backgroundColor,
4255 true /* findFG */,
4256 4.5f);
4257 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004258 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004259 }
4260 } else {
4261 mPrimaryTextColor = mForegroundColor;
4262 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004263 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4264 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004265 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4266 backgroundColor) < 4.5f) {
4267 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004268 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004269 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4270 mSecondaryTextColor,
4271 backgroundColor,
4272 true /* findFG */,
4273 4.5f);
4274 } else {
4275 mSecondaryTextColor
4276 = NotificationColorUtil.findContrastColorAgainstDark(
4277 mSecondaryTextColor,
4278 backgroundColor,
4279 true /* findFG */,
4280 4.5f);
4281 }
4282 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004283 mSecondaryTextColor, backgroundLight
4284 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4285 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004286 }
4287 }
4288 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004289 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4290 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004291 }
4292 }
4293
4294 private void updateBackgroundColor(RemoteViews contentView) {
4295 if (isColorized()) {
4296 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4297 getBackgroundColor());
4298 } else {
4299 // Clear it!
4300 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4301 0);
4302 }
4303 }
4304
Selim Cinek860b6da2015-12-16 19:02:19 -08004305 /**
4306 * @param remoteView the remote view to update the minheight in
4307 * @param hasMinHeight does it have a mimHeight
4308 * @hide
4309 */
4310 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4311 int minHeight = 0;
4312 if (hasMinHeight) {
4313 // we need to set the minHeight of the notification
4314 minHeight = mContext.getResources().getDimensionPixelSize(
4315 com.android.internal.R.dimen.notification_min_content_height);
4316 }
4317 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4318 }
4319
Selim Cinek29603462015-11-17 19:04:39 -08004320 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004321 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4322 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4323 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4324 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004325 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004326 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004327 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004328 contentView.setProgressBackgroundTintList(
4329 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4330 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004331 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004332 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004333 contentView.setProgressTintList(R.id.progress, colorStateList);
4334 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004335 }
Selim Cinek29603462015-11-17 19:04:39 -08004336 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004337 } else {
4338 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004339 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004340 }
Joe Onorato561d3852010-11-20 18:09:34 -08004341 }
4342
Selim Cinek88188f22017-09-19 16:46:56 -07004343 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4344 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004345 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4346 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4347 }
Selim Cinek88188f22017-09-19 16:46:56 -07004348 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4349 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004350 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4351 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4352 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004353 int endMargin = R.dimen.notification_content_picture_margin;
4354 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4355 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4356 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004357 }
4358 // Bind the reply action
4359 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004360
Selim Cinek88188f22017-09-19 16:46:56 -07004361 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4362 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4363 if (actionVisible) {
4364 // We're only showing the icon as big if we're hiding the large icon
4365 int contrastColor = resolveContrastColor();
4366 int iconColor;
4367 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004368 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004369 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004370 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004371 contentView.setOnClickPendingIntent(R.id.right_icon,
4372 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004373 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004374 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4375 ? Color.BLACK : Color.WHITE;
4376 } else {
4377 contentView.setImageViewResource(R.id.right_icon,
4378 R.drawable.ic_reply_notification_large);
4379 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4380 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004381 }
Selim Cinek88188f22017-09-19 16:46:56 -07004382 contentView.setDrawableTint(replyId,
4383 false /* targetBackground */,
4384 iconColor,
4385 PorterDuff.Mode.SRC_ATOP);
4386 contentView.setOnClickPendingIntent(replyId,
4387 action.actionIntent);
4388 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4389 } else {
4390 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004391 }
Selim Cinek88188f22017-09-19 16:46:56 -07004392 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4393 ? View.VISIBLE
4394 : View.GONE);
4395 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004396 ? View.VISIBLE
4397 : View.GONE);
4398 }
4399
4400 private Action findReplyAction() {
4401 ArrayList<Action> actions = mActions;
4402 if (mOriginalActions != null) {
4403 actions = mOriginalActions;
4404 }
4405 int numActions = actions.size();
4406 for (int i = 0; i < numActions; i++) {
4407 Action action = actions.get(i);
4408 if (hasValidRemoteInput(action)) {
4409 return action;
4410 }
4411 }
4412 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004413 }
4414
Selim Cinekafeed292017-12-12 17:32:44 -08004415 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4416 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004417 bindSmallIcon(contentView, ambient);
4418 bindHeaderAppName(contentView, ambient);
4419 if (!ambient) {
4420 // Ambient view does not have these
4421 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004422 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004423 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004424 bindProfileBadge(contentView);
4425 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004426 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004427 }
4428
4429 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004430 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004431 contentView.setDrawableTint(R.id.expand_button, false, color,
4432 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004433 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004434 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004435 }
4436
Selim Cinek99104832017-01-25 14:47:33 -08004437 /**
4438 * @return the color that is used as the first primary highlight color. This is applied
4439 * in several places like the action buttons or the app name in the header.
4440 */
4441 private int getPrimaryHighlightColor() {
4442 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4443 }
4444
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004445 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4446 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004447 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004448 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004449 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4450 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4451 contentView.setLong(R.id.chronometer, "setBase",
4452 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4453 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004454 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004455 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004456 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004457 } else {
4458 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4459 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004460 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004461 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004462 } else {
4463 // We still want a time to be set but gone, such that we can show and hide it
4464 // on demand in case it's a child notification without anything in the header
4465 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004466 }
4467 }
4468
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004469 private void bindHeaderText(RemoteViews contentView) {
4470 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4471 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004472 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004473 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004474 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004475 if (headerText == null
4476 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4477 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4478 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4479 }
4480 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004481 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004482 contentView.setTextViewText(R.id.header_text, processTextSpans(
4483 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004484 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004485 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4486 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004487 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004488 }
4489 }
4490
Selim Cinekafeed292017-12-12 17:32:44 -08004491 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4492 if (!TextUtils.isEmpty(secondaryText)) {
4493 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4494 processLegacyText(secondaryText)));
4495 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4496 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4497 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4498 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4499 }
4500 }
4501
Adrian Rooseba05822016-04-22 17:09:27 -07004502 /**
4503 * @hide
4504 */
4505 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004506 CharSequence name = null;
4507 final PackageManager pm = mContext.getPackageManager();
4508 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4509 // only system packages which lump together a bunch of unrelated stuff
4510 // may substitute a different name to make the purpose of the
4511 // notification more clear. the correct package label should always
4512 // be accessible via SystemUI.
4513 final String pkg = mContext.getPackageName();
4514 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4515 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4516 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4517 name = subName;
4518 } else {
4519 Log.w(TAG, "warning: pkg "
4520 + pkg + " attempting to substitute app name '" + subName
4521 + "' without holding perm "
4522 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4523 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004524 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004525 if (TextUtils.isEmpty(name)) {
4526 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4527 }
4528 if (TextUtils.isEmpty(name)) {
4529 // still nothing?
4530 return null;
4531 }
4532
4533 return String.valueOf(name);
4534 }
Adrian Roos487374f2017-01-11 15:48:14 -08004535 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004536 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004537 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004538 setTextViewColorPrimary(contentView, R.id.app_name_text);
4539 } else {
4540 contentView.setTextColor(R.id.app_name_text,
4541 ambient ? resolveAmbientColor() : resolveContrastColor());
4542 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004543 }
4544
Adrian Roos487374f2017-01-11 15:48:14 -08004545 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004546 if (mN.mSmallIcon == null && mN.icon != 0) {
4547 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4548 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004549 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004550 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004551 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004552 }
4553
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004554 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004555 * @return true if the built notification will show the time or the chronometer; false
4556 * otherwise
4557 */
4558 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004559 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004560 }
4561
Christoph Studerfe718432014-09-01 18:21:18 +02004562 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004563 // actions_container is only reset when there are no actions to avoid focus issues with
4564 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004565 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004566 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004567
4568 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4569 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4570
4571 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4572 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4573 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4574 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004575
4576 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004577 }
4578
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004579 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004580 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004581 }
4582
Adrian Roos70d7aa32017-01-11 15:39:06 -08004583 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4584 StandardTemplateParams p) {
4585 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004586
Christoph Studerfe718432014-09-01 18:21:18 +02004587 resetStandardTemplateWithActions(big);
4588
Adrian Roose458aa82015-12-08 16:17:19 -08004589 boolean validRemoteInput = false;
4590
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004591 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004592 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004593 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004594 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004595 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004596 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004597 if (p.ambient) {
4598 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004599 } else if (isColorized()) {
4600 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4601 } else {
4602 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4603 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004604 }
Adrian Roosf852a422016-06-03 13:33:43 -07004605 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4606 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004607 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004608 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004609 Action action = mActions.get(i);
4610 validRemoteInput |= hasValidRemoteInput(action);
4611
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004612 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004613 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004614 big.addView(R.id.actions, button);
4615 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004616 } else {
4617 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004618 }
Adrian Roose458aa82015-12-08 16:17:19 -08004619
4620 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004621 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004622 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4623 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004624 big.setTextViewText(R.id.notification_material_reply_text_1,
4625 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004626 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004627
4628 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4629 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004630 big.setTextViewText(R.id.notification_material_reply_text_2,
4631 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004632 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004633
4634 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4635 big.setViewVisibility(
4636 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004637 big.setTextViewText(R.id.notification_material_reply_text_3,
4638 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004639 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004640 }
4641 }
4642 }
4643
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004644 return big;
4645 }
4646
Adrian Roose458aa82015-12-08 16:17:19 -08004647 private boolean hasValidRemoteInput(Action action) {
4648 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4649 // Weird actions
4650 return false;
4651 }
4652
4653 RemoteInput[] remoteInputs = action.getRemoteInputs();
4654 if (remoteInputs == null) {
4655 return false;
4656 }
4657
4658 for (RemoteInput r : remoteInputs) {
4659 CharSequence[] choices = r.getChoices();
4660 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4661 return true;
4662 }
4663 }
4664 return false;
4665 }
4666
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004667 /**
4668 * Construct a RemoteViews for the final 1U notification layout. In order:
4669 * 1. Custom contentView from the caller
4670 * 2. Style's proposed content view
4671 * 3. Standard template view
4672 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004673 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004674 return createContentView(false /* increasedheight */ );
4675 }
4676
4677 /**
4678 * Construct a RemoteViews for the smaller content view.
4679 *
4680 * @param increasedHeight true if this layout be created with an increased height. Some
4681 * styles may support showing more then just that basic 1U size
4682 * and the system may decide to render important notifications
4683 * slightly bigger even when collapsed.
4684 *
4685 * @hide
4686 */
4687 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004688 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004689 return mN.contentView;
4690 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004691 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004692 if (styleView != null) {
4693 return styleView;
4694 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004695 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004696 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004697 }
4698
Selim Cineka7679b62017-05-10 16:33:25 -07004699 private boolean useExistingRemoteView() {
4700 return mStyle == null || (!mStyle.displayCustomViewInline()
4701 && !mRebuildStyledRemoteViews);
4702 }
4703
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004704 /**
4705 * Construct a RemoteViews for the final big notification layout.
4706 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004707 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004708 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004709 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004710 return mN.bigContentView;
4711 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004712 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004713 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004714 } else if (mActions.size() != 0) {
4715 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004716 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004717 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004718 return result;
4719 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004720
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004721 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004722 * Construct a RemoteViews for the final notification header only. This will not be
4723 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004724 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004725 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004726 * @hide
4727 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004728 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004729 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4730 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004731 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004732 ambient ? R.layout.notification_template_ambient_header
4733 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004734 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004735 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004736 if (colorized != null) {
4737 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4738 } else {
4739 mN.extras.remove(EXTRA_COLORIZED);
4740 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004741 return header;
4742 }
4743
Adrian Roos487374f2017-01-11 15:48:14 -08004744 /**
4745 * Construct a RemoteViews for the ambient version of the notification.
4746 *
4747 * @hide
4748 */
4749 public RemoteViews makeAmbientNotification() {
4750 RemoteViews ambient = applyStandardTemplateWithActions(
4751 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004752 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004753 return ambient;
4754 }
4755
Selim Cinek29603462015-11-17 19:04:39 -08004756 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004757 if (result != null) {
4758 result.setViewVisibility(R.id.text_line_1, View.GONE);
4759 }
Selim Cinek29603462015-11-17 19:04:39 -08004760 }
4761
Selim Cinek6743c0b2017-01-18 18:24:01 -08004762 /**
4763 * Adapt the Notification header if this view is used as an expanded view.
4764 *
4765 * @hide
4766 */
4767 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004768 if (result != null) {
4769 result.setBoolean(R.id.notification_header, "setExpanded", true);
4770 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004771 }
4772
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004773 /**
4774 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004775 *
4776 * @param increasedHeight true if this layout be created with an increased height. Some
4777 * styles may support showing more then just that basic 1U size
4778 * and the system may decide to render important notifications
4779 * slightly bigger even when collapsed.
4780 *
4781 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004782 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004783 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004784 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004785 return mN.headsUpContentView;
4786 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004787 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4788 if (styleView != null) {
4789 return styleView;
4790 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004791 } else if (mActions.size() == 0) {
4792 return null;
4793 }
4794
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004795 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004796 }
4797
Selim Cinek624c02db2015-12-14 21:00:02 -08004798 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004799 * Construct a RemoteViews for the final heads-up notification layout.
4800 */
4801 public RemoteViews createHeadsUpContentView() {
4802 return createHeadsUpContentView(false /* useIncreasedHeight */);
4803 }
4804
4805 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004806 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4807 *
4808 * @hide
4809 */
4810 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004811 return makePublicView(false /* ambient */);
4812 }
4813
4814 /**
4815 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4816 *
4817 * @hide
4818 */
4819 public RemoteViews makePublicAmbientNotification() {
4820 return makePublicView(true /* ambient */);
4821 }
4822
4823 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004824 if (mN.publicVersion != null) {
4825 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004826 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004827 }
4828 Bundle savedBundle = mN.extras;
4829 Style style = mStyle;
4830 mStyle = null;
4831 Icon largeIcon = mN.mLargeIcon;
4832 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004833 Bitmap largeIconLegacy = mN.largeIcon;
4834 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004835 ArrayList<Action> actions = mActions;
4836 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004837 Bundle publicExtras = new Bundle();
4838 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4839 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4840 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4841 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004842 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4843 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004844 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004845 RemoteViews view;
4846 if (ambient) {
4847 publicExtras.putCharSequence(EXTRA_TITLE,
4848 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4849 view = makeAmbientNotification();
4850 } else{
4851 view = makeNotificationHeader(false /* ambient */);
4852 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4853 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004854 mN.extras = savedBundle;
4855 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004856 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004857 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004858 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004859 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004860 }
4861
Selim Cinek6743c0b2017-01-18 18:24:01 -08004862 /**
4863 * Construct a content view for the display when low - priority
4864 *
4865 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4866 * a new subtext is created consisting of the content of the
4867 * notification.
4868 * @hide
4869 */
4870 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4871 int color = mN.color;
4872 mN.color = COLOR_DEFAULT;
4873 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4874 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4875 CharSequence newSummary = createSummaryText();
4876 if (!TextUtils.isEmpty(newSummary)) {
4877 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4878 }
4879 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004880
Adrian Roos6f6e1592017-05-02 16:22:53 -07004881 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004882 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004883 if (summary != null) {
4884 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4885 } else {
4886 mN.extras.remove(EXTRA_SUB_TEXT);
4887 }
4888 mN.color = color;
4889 return header;
4890 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004891
Selim Cinek6743c0b2017-01-18 18:24:01 -08004892 private CharSequence createSummaryText() {
4893 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4894 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4895 return titleText;
4896 }
4897 SpannableStringBuilder summary = new SpannableStringBuilder();
4898 if (titleText == null) {
4899 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4900 }
4901 BidiFormatter bidi = BidiFormatter.getInstance();
4902 if (titleText != null) {
4903 summary.append(bidi.unicodeWrap(titleText));
4904 }
4905 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4906 if (titleText != null && contentText != null) {
4907 summary.append(bidi.unicodeWrap(mContext.getText(
4908 R.string.notification_header_divider_symbol_with_spaces)));
4909 }
4910 if (contentText != null) {
4911 summary.append(bidi.unicodeWrap(contentText));
4912 }
4913 return summary;
4914 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004915
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004916 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004917 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004918 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004919 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004920 emphazisedMode ? getEmphasizedActionLayoutResource()
4921 : tombstone ? getActionTombstoneLayoutResource()
4922 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004923 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004924 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004925 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004926 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004927 if (action.mRemoteInputs != null) {
4928 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4929 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004930 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004931 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004932 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004933 int bgColor;
4934 if (isColorized()) {
4935 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4936 } else {
4937 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4938 : R.color.notification_action_list_dark);
4939 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004940 button.setDrawableTint(R.id.button_holder, true,
4941 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004942 CharSequence title = action.title;
4943 ColorStateList[] outResultColor = null;
4944 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004945 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004946 } else {
4947 outResultColor = new ColorStateList[1];
4948 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4949 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004950 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004951 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004952 if (outResultColor != null && outResultColor[0] != null) {
4953 // We need to set the text color as well since changing a text to uppercase
4954 // clears its spans.
4955 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004956 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004957 button.setTextColor(R.id.action0,resolveContrastColor());
4958 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004959 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004960 button.setTextViewText(R.id.action0, processTextSpans(
4961 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004962 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004963 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004964 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004965 button.setTextColor(R.id.action0,
4966 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004967 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004968 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004969 return button;
4970 }
4971
Joe Onoratocb109a02011-01-18 17:57:41 -08004972 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004973 * Ensures contrast on color spans against a background color. also returns the color of the
4974 * text if a span was found that spans over the whole text.
4975 *
4976 * @param charSequence the charSequence on which the spans are
4977 * @param background the background color to ensure the contrast against
4978 * @param outResultColor an array in which a color will be returned as the first element if
4979 * there exists a full length color span.
4980 * @return the contrasted charSequence
4981 */
4982 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4983 ColorStateList[] outResultColor) {
4984 if (charSequence instanceof Spanned) {
4985 Spanned ss = (Spanned) charSequence;
4986 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4987 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4988 for (Object span : spans) {
4989 Object resultSpan = span;
4990 int spanStart = ss.getSpanStart(span);
4991 int spanEnd = ss.getSpanEnd(span);
4992 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4993 if (resultSpan instanceof CharacterStyle) {
4994 resultSpan = ((CharacterStyle) span).getUnderlying();
4995 }
4996 if (resultSpan instanceof TextAppearanceSpan) {
4997 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4998 ColorStateList textColor = originalSpan.getTextColor();
4999 if (textColor != null) {
5000 int[] colors = textColor.getColors();
5001 int[] newColors = new int[colors.length];
5002 for (int i = 0; i < newColors.length; i++) {
5003 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005004 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005005 }
5006 textColor = new ColorStateList(textColor.getStates().clone(),
5007 newColors);
5008 resultSpan = new TextAppearanceSpan(
5009 originalSpan.getFamily(),
5010 originalSpan.getTextStyle(),
5011 originalSpan.getTextSize(),
5012 textColor,
5013 originalSpan.getLinkTextColor());
5014 if (fullLength) {
5015 outResultColor[0] = new ColorStateList(
5016 textColor.getStates().clone(), newColors);
5017 }
5018 }
5019 } else if (resultSpan instanceof ForegroundColorSpan) {
5020 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5021 int foregroundColor = originalSpan.getForegroundColor();
5022 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005023 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005024 resultSpan = new ForegroundColorSpan(foregroundColor);
5025 if (fullLength) {
5026 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5027 }
5028 } else {
5029 resultSpan = span;
5030 }
5031
5032 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5033 }
5034 return builder;
5035 }
5036 return charSequence;
5037 }
5038
5039 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005040 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005041 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005042 */
5043 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005044 if (!mIsLegacyInitialized) {
5045 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5046 < Build.VERSION_CODES.LOLLIPOP;
5047 mIsLegacyInitialized = true;
5048 }
5049 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005050 }
5051
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005052 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005053 return processLegacyText(charSequence, false /* ambient */);
5054 }
5055
5056 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5057 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5058 boolean wantLightText = ambient;
5059 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005060 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005061 } else {
5062 return charSequence;
5063 }
5064 }
5065
Dan Sandler26e81cf2014-05-06 10:01:27 -04005066 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005067 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005068 */
Adrian Roos487374f2017-01-11 15:48:14 -08005069 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5070 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005071 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005072 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005073 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005074 contentView.setDrawableTint(R.id.icon, false, color,
5075 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005076
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005077 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005078 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005079 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005080 }
5081
Dan Sandler26e81cf2014-05-06 10:01:27 -04005082 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005083 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005084 * if it's grayscale).
5085 */
5086 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005087 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5088 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005089 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005090 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005091 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5092 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005093 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005094 }
5095
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005096 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005097 if (mN.color != COLOR_DEFAULT) {
5098 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005099 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005100 }
5101
Adrian Roos4ff3b122016-02-01 12:26:13 -08005102 int resolveContrastColor() {
5103 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5104 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005105 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005106
Selim Cinekac5f0272017-05-02 16:05:41 -07005107 int color;
5108 int background = mBackgroundColorHint;
5109 if (mBackgroundColorHint == COLOR_INVALID) {
5110 background = mContext.getColor(
5111 com.android.internal.R.color.notification_material_background_color);
5112 }
5113 if (mN.color == COLOR_DEFAULT) {
5114 ensureColors();
5115 color = mSecondaryTextColor;
5116 } else {
5117 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005118 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005119 }
5120 if (Color.alpha(color) < 255) {
5121 // alpha doesn't go well for color filters, so let's blend it manually
5122 color = NotificationColorUtil.compositeColors(color, background);
5123 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005124 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005125 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005126 }
5127
Adrian Roos487374f2017-01-11 15:48:14 -08005128 int resolveAmbientColor() {
5129 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5130 return mCachedAmbientColor;
5131 }
5132 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5133
5134 mCachedAmbientColorIsFor = mN.color;
5135 return mCachedAmbientColor = contrasted;
5136 }
5137
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005138 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005139 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005140 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005141 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005142 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005143 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005144 mN.actions = new Action[mActions.size()];
5145 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005146 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005147 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005148 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005149 }
Selim Cinek247fa012016-02-18 09:50:48 -08005150 if (mN.bigContentView != null || mN.contentView != null
5151 || mN.headsUpContentView != null) {
5152 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5153 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005154 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005155 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005156
Julia Reynolds3b848122016-02-26 10:45:32 -05005157 /**
5158 * Creates a Builder from an existing notification so further changes can be made.
5159 * @param context The context for your application / activity.
5160 * @param n The notification to create a Builder from.
5161 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005162 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005163 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005164 ApplicationInfo applicationInfo = n.extras.getParcelable(
5165 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005166 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005167 if (applicationInfo != null) {
5168 try {
5169 builderContext = context.createApplicationContext(applicationInfo,
5170 Context.CONTEXT_RESTRICTED);
5171 } catch (NameNotFoundException e) {
5172 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5173 builderContext = context; // try with our context
5174 }
5175 } else {
5176 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005177 }
5178
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005179 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005180 }
5181
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005182 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005183 * @deprecated Use {@link #build()} instead.
5184 */
5185 @Deprecated
5186 public Notification getNotification() {
5187 return build();
5188 }
5189
5190 /**
5191 * Combine all of the options that have been set and return a new {@link Notification}
5192 * object.
5193 */
5194 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005195 // first, add any extras from the calling code
5196 if (mUserExtras != null) {
5197 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005198 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005199
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005200 mN.creationTime = System.currentTimeMillis();
5201
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005202 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005203 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005204
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005205 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005206
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005207 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005208 mStyle.reduceImageSizes(mContext);
5209 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005210 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005211 }
Selim Cinekd0426622017-07-11 13:19:59 +02005212 mN.reduceImageSizes(mContext);
5213
Adrian Roos5081c0d2016-02-26 16:04:19 -08005214 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005215 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005216 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005217 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005218 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5219 mN.contentView.getSequenceNumber());
5220 }
5221 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005222 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005223 if (mN.bigContentView != null) {
5224 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5225 mN.bigContentView.getSequenceNumber());
5226 }
5227 }
5228 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005229 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005230 if (mN.headsUpContentView != null) {
5231 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5232 mN.headsUpContentView.getSequenceNumber());
5233 }
5234 }
5235 }
5236
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005237 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5238 mN.flags |= FLAG_SHOW_LIGHTS;
5239 }
5240
Adrian Roosfb921842017-10-26 14:49:56 +02005241 mN.allPendingIntents = null;
5242
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005243 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005244 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005245
5246 /**
5247 * Apply this Builder to an existing {@link Notification} object.
5248 *
5249 * @hide
5250 */
5251 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005252 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005253 return n;
5254 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005255
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005256 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005257 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005258 * change. Also removes extenders on low ram devices, as
5259 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005260 *
5261 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5262 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005263 * @hide
5264 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005265 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005266 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005267
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005268 // Only strip views for known Styles because we won't know how to
5269 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005270 if (!isLowRam
5271 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005272 && getNotificationStyleClass(templateClass) == null) {
5273 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005274 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005275
5276 // Only strip unmodified BuilderRemoteViews.
5277 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005278 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005279 n.contentView.getSequenceNumber();
5280 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005281 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005282 n.bigContentView.getSequenceNumber();
5283 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005284 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005285 n.headsUpContentView.getSequenceNumber();
5286
5287 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005288 if (!isLowRam
5289 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005290 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005291 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005292
5293 Notification clone = n.clone();
5294 if (stripContentView) {
5295 clone.contentView = null;
5296 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5297 }
5298 if (stripBigContentView) {
5299 clone.bigContentView = null;
5300 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5301 }
5302 if (stripHeadsUpContentView) {
5303 clone.headsUpContentView = null;
5304 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5305 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005306 if (isLowRam) {
5307 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5308 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5309 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5310 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005311 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005312 }
5313
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005314 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005315 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005316 }
5317
5318 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005319 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005320 }
5321
5322 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005323 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005324 }
5325
5326 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005327 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005328 }
5329
5330 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005331 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005332 }
5333
Adrian Roosc1a80b02016-04-05 14:54:55 -07005334 private int getMessagingLayoutResource() {
5335 return R.layout.notification_template_material_messaging;
5336 }
5337
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005338 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005339 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005340 }
5341
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005342 private int getEmphasizedActionLayoutResource() {
5343 return R.layout.notification_material_action_emphasized;
5344 }
5345
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005346 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005347 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005348 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005349
5350 private int getBackgroundColor() {
5351 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005352 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005353 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005354 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5355 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005356 }
5357 }
5358
5359 private boolean isColorized() {
5360 return mN.isColorized();
5361 }
Selim Cinek99104832017-01-25 14:47:33 -08005362
Anthony Chenad4d1582017-04-10 16:07:58 -07005363 private boolean shouldTintActionButtons() {
5364 return mTintActionButtons;
5365 }
5366
Selim Cinek99104832017-01-25 14:47:33 -08005367 private boolean textColorsNeedInversion() {
5368 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5369 return false;
5370 }
5371 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5372 return targetSdkVersion > Build.VERSION_CODES.M
5373 && targetSdkVersion < Build.VERSION_CODES.O;
5374 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005375
5376 /**
5377 * Set a color palette to be used as the background and textColors
5378 *
5379 * @param backgroundColor the color to be used as the background
5380 * @param foregroundColor the color to be used as the foreground
5381 *
5382 * @hide
5383 */
5384 public void setColorPalette(int backgroundColor, int foregroundColor) {
5385 mBackgroundColor = backgroundColor;
5386 mForegroundColor = foregroundColor;
5387 mTextColorsAreForBackground = COLOR_INVALID;
5388 ensureColors();
5389 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005390
5391 /**
5392 * Sets the background color for this notification to be a different one then the default.
5393 * This is mainly used to calculate contrast and won't necessarily be applied to the
5394 * background.
5395 *
5396 * @hide
5397 */
5398 public void setBackgroundColorHint(int backgroundColor) {
5399 mBackgroundColorHint = backgroundColor;
5400 }
Selim Cineka7679b62017-05-10 16:33:25 -07005401
5402
5403 /**
5404 * Forces all styled remoteViews to be built from scratch and not use any cached
5405 * RemoteViews.
5406 * This is needed for legacy apps that are baking in their remoteviews into the
5407 * notification.
5408 *
5409 * @hide
5410 */
5411 public void setRebuildStyledRemoteViews(boolean rebuild) {
5412 mRebuildStyledRemoteViews = rebuild;
5413 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005414 }
5415
5416 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005417 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5418 * remote views.
5419 *
5420 * @hide
5421 */
5422 void reduceImageSizes(Context context) {
5423 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5424 return;
5425 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005426 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005427 if (mLargeIcon != null || largeIcon != null) {
5428 Resources resources = context.getResources();
5429 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005430 int maxWidth = resources.getDimensionPixelSize(isLowRam
5431 ? R.dimen.notification_right_icon_size_low_ram
5432 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005433 int maxHeight = maxWidth;
5434 if (MediaStyle.class.equals(style)
5435 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005436 maxHeight = resources.getDimensionPixelSize(isLowRam
5437 ? R.dimen.notification_media_image_max_height_low_ram
5438 : R.dimen.notification_media_image_max_height);
5439 maxWidth = resources.getDimensionPixelSize(isLowRam
5440 ? R.dimen.notification_media_image_max_width_low_ram
5441 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005442 }
5443 if (mLargeIcon != null) {
5444 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5445 }
5446 if (largeIcon != null) {
5447 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5448 }
5449 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005450 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5451 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5452 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005453 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5454 }
5455
Selim Cineka8cb1262017-08-15 16:53:44 -07005456 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5457 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005458 if (remoteView != null) {
5459 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005460 int maxWidth = resources.getDimensionPixelSize(isLowRam
5461 ? R.dimen.notification_custom_view_max_image_width_low_ram
5462 : R.dimen.notification_custom_view_max_image_width);
5463 int maxHeight = resources.getDimensionPixelSize(isLowRam
5464 ? R.dimen.notification_custom_view_max_image_height_low_ram
5465 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005466 remoteView.reduceImageSizes(maxWidth, maxHeight);
5467 }
5468 }
5469
5470 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005471 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005472 */
Selim Cinek22714f12017-04-13 16:23:53 -07005473 private boolean isForegroundService() {
5474 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005475 }
5476
5477 /**
Selim Cinek99104832017-01-25 14:47:33 -08005478 * @return whether this notification has a media session attached
5479 * @hide
5480 */
5481 public boolean hasMediaSession() {
5482 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5483 }
5484
5485 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005486 * @return the style class of this notification
5487 * @hide
5488 */
5489 public Class<? extends Notification.Style> getNotificationStyle() {
5490 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5491
5492 if (!TextUtils.isEmpty(templateClass)) {
5493 return Notification.getNotificationStyleClass(templateClass);
5494 }
5495 return null;
5496 }
5497
5498 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005499 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005500 *
5501 * @hide
5502 */
5503 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005504 if (isColorizedMedia()) {
5505 return true;
5506 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005507 return extras.getBoolean(EXTRA_COLORIZED)
5508 && (hasColorizedPermission() || isForegroundService());
5509 }
5510
5511 /**
5512 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5513 * permission. The permission is checked when a notification is enqueued.
5514 */
5515 private boolean hasColorizedPermission() {
5516 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005517 }
5518
5519 /**
5520 * @return true if this notification is colorized and it is a media notification
5521 *
5522 * @hide
5523 */
5524 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005525 Class<? extends Style> style = getNotificationStyle();
5526 if (MediaStyle.class.equals(style)) {
5527 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5528 if ((colorized == null || colorized) && hasMediaSession()) {
5529 return true;
5530 }
5531 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5532 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5533 return true;
5534 }
5535 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005536 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005537 }
5538
Selim Cinek0847acd2017-04-24 19:48:29 -07005539
5540 /**
5541 * @return true if this is a media notification
5542 *
5543 * @hide
5544 */
5545 public boolean isMediaNotification() {
5546 Class<? extends Style> style = getNotificationStyle();
5547 if (MediaStyle.class.equals(style)) {
5548 return true;
5549 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5550 return true;
5551 }
5552 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005553 }
5554
Selim Cinek279fa862016-06-14 10:57:25 -07005555 private boolean hasLargeIcon() {
5556 return mLargeIcon != null || largeIcon != null;
5557 }
5558
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005559 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005560 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005561 * @hide
5562 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005563 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005564 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5565 }
5566
5567 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005568 * @return true if the notification will show a chronometer; false otherwise
5569 * @hide
5570 */
5571 public boolean showsChronometer() {
5572 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5573 }
5574
5575 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005576 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005577 */
5578 @SystemApi
5579 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5580 Class<? extends Style>[] classes = new Class[] {
5581 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5582 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5583 MessagingStyle.class };
5584 for (Class<? extends Style> innerClass : classes) {
5585 if (templateClass.equals(innerClass.getName())) {
5586 return innerClass;
5587 }
5588 }
5589 return null;
5590 }
5591
5592 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005593 * An object that can apply a rich notification style to a {@link Notification.Builder}
5594 * object.
5595 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005596 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005597 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005598
5599 /**
5600 * @hide
5601 */
5602 protected CharSequence mSummaryText = null;
5603
5604 /**
5605 * @hide
5606 */
5607 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005608
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005609 protected Builder mBuilder;
5610
Chris Wrend6297db2012-05-03 16:20:13 -04005611 /**
5612 * Overrides ContentTitle in the big form of the template.
5613 * This defaults to the value passed to setContentTitle().
5614 */
5615 protected void internalSetBigContentTitle(CharSequence title) {
5616 mBigContentTitle = title;
5617 }
5618
5619 /**
5620 * Set the first line of text after the detail section in the big form of the template.
5621 */
5622 protected void internalSetSummaryText(CharSequence cs) {
5623 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005624 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005625 }
5626
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005627 public void setBuilder(Builder builder) {
5628 if (mBuilder != builder) {
5629 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005630 if (mBuilder != null) {
5631 mBuilder.setStyle(this);
5632 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005633 }
5634 }
5635
Chris Wrend6297db2012-05-03 16:20:13 -04005636 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005637 if (mBuilder == null) {
5638 throw new IllegalArgumentException("Style requires a valid Builder object");
5639 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005640 }
Chris Wrend6297db2012-05-03 16:20:13 -04005641
5642 protected RemoteViews getStandardView(int layoutId) {
5643 checkBuilder();
5644
Christoph Studer4600f9b2014-07-22 22:44:43 +02005645 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005646 CharSequence oldBuilderContentTitle =
5647 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005648 if (mBigContentTitle != null) {
5649 mBuilder.setContentTitle(mBigContentTitle);
5650 }
5651
Chris Wrend6297db2012-05-03 16:20:13 -04005652 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5653
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005654 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005655
Chris Wrend6297db2012-05-03 16:20:13 -04005656 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5657 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005658 } else {
5659 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005660 }
5661
Chris Wrend6297db2012-05-03 16:20:13 -04005662 return contentView;
5663 }
5664
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005665 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005666 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005667 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005668 *
5669 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005670 * @hide
5671 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005672 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005673 return null;
5674 }
5675
5676 /**
5677 * Construct a Style-specific RemoteViews for the final big notification layout.
5678 * @hide
5679 */
5680 public RemoteViews makeBigContentView() {
5681 return null;
5682 }
5683
5684 /**
5685 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005686 *
5687 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005688 * @hide
5689 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005690 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005691 return null;
5692 }
5693
5694 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005695 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005696 * @hide
5697 */
5698 public void addExtras(Bundle extras) {
5699 if (mSummaryTextSet) {
5700 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5701 }
5702 if (mBigContentTitle != null) {
5703 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5704 }
Chris Wren91ad5632013-06-05 15:05:57 -04005705 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005706 }
5707
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005708 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005709 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005710 * @hide
5711 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005712 protected void restoreFromExtras(Bundle extras) {
5713 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5714 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5715 mSummaryTextSet = true;
5716 }
5717 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5718 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5719 }
5720 }
5721
5722
5723 /**
5724 * @hide
5725 */
5726 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005727 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005728 return wip;
5729 }
5730
Daniel Sandler0ec46202015-06-24 01:27:05 -04005731 /**
5732 * @hide
5733 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005734 public void purgeResources() {}
5735
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005736 /**
5737 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5738 * attached to.
5739 *
5740 * @return the fully constructed Notification.
5741 */
5742 public Notification build() {
5743 checkBuilder();
5744 return mBuilder.build();
5745 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005746
5747 /**
5748 * @hide
5749 * @return true if the style positions the progress bar on the second line; false if the
5750 * style hides the progress bar
5751 */
5752 protected boolean hasProgress() {
5753 return true;
5754 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005755
5756 /**
5757 * @hide
5758 * @return Whether we should put the summary be put into the notification header
5759 */
5760 public boolean hasSummaryInHeader() {
5761 return true;
5762 }
Selim Cinek593610c2016-02-16 18:42:57 -08005763
5764 /**
5765 * @hide
5766 * @return Whether custom content views are displayed inline in the style
5767 */
5768 public boolean displayCustomViewInline() {
5769 return false;
5770 }
Selim Cinekd0426622017-07-11 13:19:59 +02005771
5772 /**
5773 * Reduces the image sizes contained in this style.
5774 *
5775 * @hide
5776 */
5777 public void reduceImageSizes(Context context) {
5778 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005779 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005780
5781 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005782 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005783 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005784 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005785 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005786 * Notification notif = new Notification.Builder(mContext)
5787 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5788 * .setContentText(subject)
5789 * .setSmallIcon(R.drawable.new_post)
5790 * .setLargeIcon(aBitmap)
5791 * .setStyle(new Notification.BigPictureStyle()
5792 * .bigPicture(aBigBitmap))
5793 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005794 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005795 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005796 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005797 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005798 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005799 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005800 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005801 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005802
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005803 public BigPictureStyle() {
5804 }
5805
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005806 /**
5807 * @deprecated use {@code BigPictureStyle()}.
5808 */
5809 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005810 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005811 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005812 }
5813
Chris Wrend6297db2012-05-03 16:20:13 -04005814 /**
5815 * Overrides ContentTitle in the big form of the template.
5816 * This defaults to the value passed to setContentTitle().
5817 */
5818 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005819 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005820 return this;
5821 }
5822
5823 /**
5824 * Set the first line of text after the detail section in the big form of the template.
5825 */
5826 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005827 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005828 return this;
5829 }
5830
Chris Wren0bd664d2012-08-01 13:56:56 -04005831 /**
5832 * Provide the bitmap to be used as the payload for the BigPicture notification.
5833 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005834 public BigPictureStyle bigPicture(Bitmap b) {
5835 mPicture = b;
5836 return this;
5837 }
5838
Chris Wren3745a3d2012-05-22 15:11:52 -04005839 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005840 * Override the large icon when the big notification is shown.
5841 */
5842 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005843 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5844 }
5845
5846 /**
5847 * Override the large icon when the big notification is shown.
5848 */
5849 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005850 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005851 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005852 return this;
5853 }
5854
Riley Andrews0394a0c2015-11-03 23:36:52 -08005855 /** @hide */
5856 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5857
Daniel Sandler0ec46202015-06-24 01:27:05 -04005858 /**
5859 * @hide
5860 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005861 @Override
5862 public void purgeResources() {
5863 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005864 if (mPicture != null &&
5865 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005866 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005867 mPicture = mPicture.createAshmemBitmap();
5868 }
5869 if (mBigLargeIcon != null) {
5870 mBigLargeIcon.convertToAshmem();
5871 }
5872 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005873
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005874 /**
5875 * @hide
5876 */
Selim Cinekd0426622017-07-11 13:19:59 +02005877 @Override
5878 public void reduceImageSizes(Context context) {
5879 super.reduceImageSizes(context);
5880 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005881 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005882 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005883 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5884 ? R.dimen.notification_big_picture_max_height_low_ram
5885 : R.dimen.notification_big_picture_max_height);
5886 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5887 ? R.dimen.notification_big_picture_max_width_low_ram
5888 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005889 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5890 }
5891 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005892 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5893 ? R.dimen.notification_right_icon_size_low_ram
5894 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005895 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5896 }
5897 }
5898
5899 /**
5900 * @hide
5901 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005902 public RemoteViews makeBigContentView() {
5903 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005904 // This covers the following cases:
5905 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005906 // mN.mLargeIcon
5907 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005908 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005909 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005910 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005911 oldLargeIcon = mBuilder.mN.mLargeIcon;
5912 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005913 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5914 // replacement if the other one is null. Because we're restoring these legacy icons
5915 // for old listeners, this is in general non-null.
5916 largeIconLegacy = mBuilder.mN.largeIcon;
5917 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005918 }
5919
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005920 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005921 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005922 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5923 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005924 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005925 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005926 }
Selim Cinek279fa862016-06-14 10:57:25 -07005927 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005928
Christoph Studer5c510ee2014-12-15 16:32:27 +01005929 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005930 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005931 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005932 }
5933
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005934 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005935 return contentView;
5936 }
5937
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005938 /**
5939 * @hide
5940 */
5941 public void addExtras(Bundle extras) {
5942 super.addExtras(extras);
5943
5944 if (mBigLargeIconSet) {
5945 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5946 }
5947 extras.putParcelable(EXTRA_PICTURE, mPicture);
5948 }
5949
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005950 /**
5951 * @hide
5952 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005953 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005954 protected void restoreFromExtras(Bundle extras) {
5955 super.restoreFromExtras(extras);
5956
5957 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005958 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005959 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005960 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005961 mPicture = extras.getParcelable(EXTRA_PICTURE);
5962 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005963
5964 /**
5965 * @hide
5966 */
5967 @Override
5968 public boolean hasSummaryInHeader() {
5969 return false;
5970 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005971 }
5972
5973 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005974 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005975 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005976 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005977 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005978 * Notification notif = new Notification.Builder(mContext)
5979 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5980 * .setContentText(subject)
5981 * .setSmallIcon(R.drawable.new_mail)
5982 * .setLargeIcon(aBitmap)
5983 * .setStyle(new Notification.BigTextStyle()
5984 * .bigText(aVeryLongString))
5985 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005986 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005987 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005988 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005989 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005990 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005991
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005992 private CharSequence mBigText;
5993
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005994 public BigTextStyle() {
5995 }
5996
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005997 /**
5998 * @deprecated use {@code BigTextStyle()}.
5999 */
6000 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006001 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006002 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006003 }
6004
Chris Wrend6297db2012-05-03 16:20:13 -04006005 /**
6006 * Overrides ContentTitle in the big form of the template.
6007 * This defaults to the value passed to setContentTitle().
6008 */
6009 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006010 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006011 return this;
6012 }
6013
6014 /**
6015 * Set the first line of text after the detail section in the big form of the template.
6016 */
6017 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006018 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006019 return this;
6020 }
6021
Chris Wren0bd664d2012-08-01 13:56:56 -04006022 /**
6023 * Provide the longer text to be displayed in the big form of the
6024 * template in place of the content text.
6025 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006026 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006027 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006028 return this;
6029 }
6030
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006031 /**
6032 * @hide
6033 */
6034 public void addExtras(Bundle extras) {
6035 super.addExtras(extras);
6036
Christoph Studer4600f9b2014-07-22 22:44:43 +02006037 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6038 }
6039
6040 /**
6041 * @hide
6042 */
6043 @Override
6044 protected void restoreFromExtras(Bundle extras) {
6045 super.restoreFromExtras(extras);
6046
6047 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006048 }
6049
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006050 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006051 * @param increasedHeight true if this layout be created with an increased height.
6052 *
6053 * @hide
6054 */
6055 @Override
6056 public RemoteViews makeContentView(boolean increasedHeight) {
6057 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006058 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006059 mBuilder.mActions = new ArrayList<>();
6060 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006061 mBuilder.mActions = mBuilder.mOriginalActions;
6062 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006063 return remoteViews;
6064 }
6065 return super.makeContentView(increasedHeight);
6066 }
6067
6068 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006069 * @hide
6070 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006071 @Override
6072 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6073 if (increasedHeight && mBuilder.mActions.size() > 0) {
6074 return makeBigContentView();
6075 }
6076 return super.makeHeadsUpContentView(increasedHeight);
6077 }
6078
6079 /**
6080 * @hide
6081 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006082 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006083
6084 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006085 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006086 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006087
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006088 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006089
Selim Cinek75998782016-04-26 10:39:17 -07006090 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006091
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006092 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006093 if (TextUtils.isEmpty(bigTextText)) {
6094 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6095 // experience
6096 bigTextText = mBuilder.processLegacyText(text);
6097 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006098 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006099
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006100 return contentView;
6101 }
6102
Adrian Roosb1f427c2016-05-26 12:27:15 -07006103 static void applyBigTextContentView(Builder builder,
6104 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006105 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006106 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006107 contentView.setViewVisibility(R.id.big_text,
6108 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006109 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006110 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006111 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006112
6113 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006114 * Helper class for generating large-format notifications that include multiple back-and-forth
6115 * messages of varying types between any number of people.
6116 *
6117 * <br>
6118 * If the platform does not provide large-format notifications, this method has no effect. The
6119 * user will always see the normal notification view.
6120 * <br>
6121 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6122 * so:
6123 * <pre class="prettyprint">
6124 *
6125 * Notification noti = new Notification.Builder()
6126 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6127 * .setContentText(subject)
6128 * .setSmallIcon(R.drawable.new_message)
6129 * .setLargeIcon(aBitmap)
6130 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6131 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6132 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6133 * .build();
6134 * </pre>
6135 */
6136 public static class MessagingStyle extends Style {
6137
6138 /**
6139 * The maximum number of messages that will be retained in the Notification itself (the
6140 * number displayed is up to the platform).
6141 */
6142 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6143
Selim Cinekcb8b9852017-12-15 18:01:52 -08006144 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006145 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006146 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006147 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006148 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006149
6150 MessagingStyle() {
6151 }
6152
6153 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006154 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6155 * user before the posting app reposts the notification with those messages after they've
6156 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006157 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006158 *
6159 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006160 */
Alex Hillsfd590442016-10-07 09:52:44 -04006161 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006162 this(new Person().setName(userDisplayName));
6163 }
6164
6165 /**
6166 * @param user Required - The person displayed for any messages that are sent by the
6167 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6168 * who don't have a Person associated with it will be displayed as if they were sent
6169 * by this user. The user also needs to have a valid name associated with it.
6170 */
6171 public MessagingStyle(@NonNull Person user) {
6172 mUser = user;
6173 if (user == null || user.getName() == null) {
6174 throw new RuntimeException("user must be valid and have a name");
6175 }
6176 }
6177
6178 /**
6179 * @return the user to be displayed for any replies sent by the user
6180 */
6181 public Person getUser() {
6182 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006183 }
6184
6185 /**
6186 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006187 *
6188 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006189 */
6190 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006191 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006192 }
6193
6194 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006195 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6196 *
Kodlee Yin14656422017-12-22 17:00:46 -08006197 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6198 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6199 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6200 * {@code true} and passing {@code null} will make it return {@code false}. In
6201 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6202 * to set group conversation status.
6203 *
6204 * @param conversationTitle Title displayed for this conversation
6205 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006206 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006207 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006208 mConversationTitle = conversationTitle;
6209 return this;
6210 }
6211
6212 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006213 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006214 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006215 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006216 public CharSequence getConversationTitle() {
6217 return mConversationTitle;
6218 }
6219
6220 /**
6221 * Adds a message for display by this notification. Convenience call for a simple
6222 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6223 * @param text A {@link CharSequence} to be displayed as the message content
6224 * @param timestamp Time at which the message arrived
6225 * @param sender A {@link CharSequence} to be used for displaying the name of the
6226 * sender. Should be <code>null</code> for messages by the current user, in which case
6227 * the platform will insert {@link #getUserDisplayName()}.
6228 * Should be unique amongst all individuals in the conversation, and should be
6229 * consistent during re-posts of the notification.
6230 *
6231 * @see Message#Message(CharSequence, long, CharSequence)
6232 *
6233 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006234 *
6235 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006236 */
6237 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006238 return addMessage(text, timestamp,
6239 sender == null ? null : new Person().setName(sender));
6240 }
6241
6242 /**
6243 * Adds a message for display by this notification. Convenience call for a simple
6244 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6245 * @param text A {@link CharSequence} to be displayed as the message content
6246 * @param timestamp Time at which the message arrived
6247 * @param sender The {@link Person} who sent the message.
6248 * Should be <code>null</code> for messages by the current user, in which case
6249 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6250 *
6251 * @see Message#Message(CharSequence, long, CharSequence)
6252 *
6253 * @return this object for method chaining
6254 */
6255 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006256 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006257 }
6258
6259 /**
6260 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006261 *
6262 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6263 * the newest last.
6264 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006265 * @param message The {@link Message} to be displayed
6266 * @return this object for method chaining
6267 */
6268 public MessagingStyle addMessage(Message message) {
6269 mMessages.add(message);
6270 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6271 mMessages.remove(0);
6272 }
6273 return this;
6274 }
6275
6276 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006277 * Adds a {@link Message} for historic context in this notification.
6278 *
6279 * <p>Messages should be added as historic if they are not the main subject of the
6280 * notification but may give context to a conversation. The system may choose to present
6281 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6282 *
6283 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6284 * the newest last.
6285 *
6286 * @param message The historic {@link Message} to be added
6287 * @return this object for method chaining
6288 */
6289 public MessagingStyle addHistoricMessage(Message message) {
6290 mHistoricMessages.add(message);
6291 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6292 mHistoricMessages.remove(0);
6293 }
6294 return this;
6295 }
6296
6297 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006298 * Gets the list of {@code Message} objects that represent the notification
6299 */
6300 public List<Message> getMessages() {
6301 return mMessages;
6302 }
6303
6304 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006305 * Gets the list of historic {@code Message}s in the notification.
6306 */
6307 public List<Message> getHistoricMessages() {
6308 return mHistoricMessages;
6309 }
6310
6311 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006312 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006313 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006314 * @param isGroupConversation {@code true} if the conversation represents a group,
6315 * {@code false} otherwise.
6316 * @return this object for method chaining
6317 */
6318 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6319 mIsGroupConversation = isGroupConversation;
6320 return this;
6321 }
6322
6323 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006324 * Returns {@code true} if this notification represents a group conversation, otherwise
6325 * {@code false}.
6326 *
6327 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6328 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6329 * not the conversation title is set; returning {@code true} if the conversation title is
6330 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6331 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6332 * named, non-group conversations.
6333 *
6334 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006335 */
6336 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006337 // When target SDK version is < P, a non-null conversation title dictates if this is
6338 // as group conversation.
6339 if (mBuilder != null
6340 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6341 < Build.VERSION_CODES.P) {
6342 return mConversationTitle != null;
6343 }
6344
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006345 return mIsGroupConversation;
6346 }
6347
6348 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006349 * @hide
6350 */
6351 @Override
6352 public void addExtras(Bundle extras) {
6353 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006354 if (mUser != null) {
6355 // For legacy usages
6356 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6357 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006358 }
6359 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006360 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006361 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006362 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6363 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006364 }
Adrian Roos437cd562017-01-18 15:47:03 -08006365 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6366 Message.getBundleArrayForMessages(mHistoricMessages));
6367 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006368
6369 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006370 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006371 }
6372
6373 private void fixTitleAndTextExtras(Bundle extras) {
6374 Message m = findLatestIncomingMessage();
6375 CharSequence text = (m == null) ? null : m.mText;
6376 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006377 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6378 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006379 CharSequence title;
6380 if (!TextUtils.isEmpty(mConversationTitle)) {
6381 if (!TextUtils.isEmpty(sender)) {
6382 BidiFormatter bidi = BidiFormatter.getInstance();
6383 title = mBuilder.mContext.getString(
6384 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006385 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006386 } else {
6387 title = mConversationTitle;
6388 }
6389 } else {
6390 title = sender;
6391 }
6392
6393 if (title != null) {
6394 extras.putCharSequence(EXTRA_TITLE, title);
6395 }
6396 if (text != null) {
6397 extras.putCharSequence(EXTRA_TEXT, text);
6398 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006399 }
6400
6401 /**
6402 * @hide
6403 */
6404 @Override
6405 protected void restoreFromExtras(Bundle extras) {
6406 super.restoreFromExtras(extras);
6407
Selim Cinekcb8b9852017-12-15 18:01:52 -08006408 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6409 if (mUser == null) {
6410 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6411 mUser = new Person().setName(displayName);
6412 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006413 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006414 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006415 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006416 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006417 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006418 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006419 }
6420
6421 /**
6422 * @hide
6423 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006424 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006425 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006426 mBuilder.mOriginalActions = mBuilder.mActions;
6427 mBuilder.mActions = new ArrayList<>();
Selim Cinekafeed292017-12-12 17:32:44 -08006428 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006429 mBuilder.mActions = mBuilder.mOriginalActions;
6430 mBuilder.mOriginalActions = null;
6431 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006432 }
6433
6434 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006435 return findLatestIncomingMessage(mMessages);
6436 }
6437
6438 /**
6439 * @hide
6440 */
6441 @Nullable
6442 public static Message findLatestIncomingMessage(
6443 List<Message> messages) {
6444 for (int i = messages.size() - 1; i >= 0; i--) {
6445 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006446 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006447 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006448 return m;
6449 }
6450 }
Selim Cinek88188f22017-09-19 16:46:56 -07006451 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006452 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006453 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006454 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006455 return null;
6456 }
6457
6458 /**
6459 * @hide
6460 */
6461 @Override
6462 public RemoteViews makeBigContentView() {
Selim Cinekafeed292017-12-12 17:32:44 -08006463 return makeBigContentView(false /* showRightIcon */);
6464 }
6465
6466 @NonNull
6467 private RemoteViews makeBigContentView(boolean showRightIcon) {
Selim Cinek88188f22017-09-19 16:46:56 -07006468 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006469 ? super.mBigContentTitle
6470 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006471 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006472 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006473 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006474 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006475 nameReplacement = conversationTitle;
6476 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006477 }
Adrian Roos48d746a2016-04-12 14:57:28 -07006478 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006479 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006480 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinekafeed292017-12-12 17:32:44 -08006481 .hideLargeIcon(!showRightIcon || isOneToOne)
6482 .headerTextSecondary(conversationTitle)
6483 .alwaysShowReply(showRightIcon));
Selim Cinek88188f22017-09-19 16:46:56 -07006484 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006485 // also update the end margin if there is an image
6486 int endMargin = R.dimen.notification_content_margin_end;
6487 if (mBuilder.mN.hasLargeIcon() && showRightIcon) {
6488 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6489 }
6490 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006491 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6492 mBuilder.resolveContrastColor());
6493 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6494 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006495 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6496 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006497 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6498 isOneToOne);
6499 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6500 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006501 return contentView;
6502 }
6503
Selim Cinekf7409db2017-10-24 16:17:14 -07006504 private boolean hasOnlyWhiteSpaceSenders() {
6505 for (int i = 0; i < mMessages.size(); i++) {
6506 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006507 Person sender = m.getSenderPerson();
6508 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006509 return false;
6510 }
6511 }
6512 return true;
6513 }
6514
6515 private boolean isWhiteSpace(CharSequence sender) {
6516 if (TextUtils.isEmpty(sender)) {
6517 return true;
6518 }
6519 if (sender.toString().matches("^\\s*$")) {
6520 return true;
6521 }
6522 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6523 // For the presentation that we had.
6524 for (int i = 0; i < sender.length(); i++) {
6525 char c = sender.charAt(i);
6526 if (c != '\u200B') {
6527 return false;
6528 }
6529 }
6530 return true;
6531 }
6532
Selim Cinek88188f22017-09-19 16:46:56 -07006533 private CharSequence createConversationTitleFromMessages() {
6534 ArraySet<CharSequence> names = new ArraySet<>();
6535 for (int i = 0; i < mMessages.size(); i++) {
6536 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006537 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006538 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006539 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006540 }
6541 }
6542 SpannableStringBuilder title = new SpannableStringBuilder();
6543 int size = names.size();
6544 for (int i = 0; i < size; i++) {
6545 CharSequence name = names.valueAt(i);
6546 if (!TextUtils.isEmpty(title)) {
6547 title.append(", ");
6548 }
6549 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6550 }
6551 return title;
6552 }
6553
Adrian Roosdedd1df2016-04-26 16:38:47 -07006554 /**
6555 * @hide
6556 */
6557 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006558 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekafeed292017-12-12 17:32:44 -08006559 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006560 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6561 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006562 }
6563
Adrian Roosc1a80b02016-04-05 14:54:55 -07006564 private static TextAppearanceSpan makeFontColorSpan(int color) {
6565 return new TextAppearanceSpan(null, 0, 0,
6566 ColorStateList.valueOf(color), null);
6567 }
6568
Alex Hillsd9b04d92016-04-11 16:38:16 -04006569 public static final class Message {
6570
6571 static final String KEY_TEXT = "text";
6572 static final String KEY_TIMESTAMP = "time";
6573 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006574 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006575 static final String KEY_DATA_MIME_TYPE = "type";
6576 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006577 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006578
6579 private final CharSequence mText;
6580 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006581 @Nullable
6582 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006583
Shane Brennan5a871862017-03-11 13:14:17 -08006584 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006585 private String mDataMimeType;
6586 private Uri mDataUri;
6587
6588 /**
6589 * Constructor
6590 * @param text A {@link CharSequence} to be displayed as the message content
6591 * @param timestamp Time at which the message arrived
6592 * @param sender A {@link CharSequence} to be used for displaying the name of the
6593 * sender. Should be <code>null</code> for messages by the current user, in which case
6594 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6595 * Should be unique amongst all individuals in the conversation, and should be
6596 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006597 *
6598 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006599 */
6600 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006601 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6602 }
6603
6604 /**
6605 * Constructor
6606 * @param text A {@link CharSequence} to be displayed as the message content
6607 * @param timestamp Time at which the message arrived
6608 * @param sender The {@link Person} who sent the message.
6609 * Should be <code>null</code> for messages by the current user, in which case
6610 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6611 * <p>
6612 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6613 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6614 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6615 * in order to differentiate between the different users.
6616 * </p>
6617 */
6618 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006619 mText = text;
6620 mTimestamp = timestamp;
6621 mSender = sender;
6622 }
6623
6624 /**
6625 * Sets a binary blob of data and an associated MIME type for a message. In the case
6626 * where the platform doesn't support the MIME type, the original text provided in the
6627 * constructor will be used.
6628 * @param dataMimeType The MIME type of the content. See
6629 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6630 * types on Android and Android Wear.
6631 * @param dataUri The uri containing the content whose type is given by the MIME type.
6632 * <p class="note">
6633 * <ol>
6634 * <li>Notification Listeners including the System UI need permission to access the
6635 * data the Uri points to. The recommended ways to do this are:</li>
6636 * <li>Store the data in your own ContentProvider, making sure that other apps have
6637 * the correct permission to access your provider. The preferred mechanism for
6638 * providing access is to use per-URI permissions which are temporary and only
6639 * grant access to the receiving application. An easy way to create a
6640 * ContentProvider like this is to use the FileProvider helper class.</li>
6641 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6642 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6643 * also store non-media types (see MediaStore.Files for more info). Files can be
6644 * inserted into the MediaStore using scanFile() after which a content:// style
6645 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6646 * Note that once added to the system MediaStore the content is accessible to any
6647 * app on the device.</li>
6648 * </ol>
6649 * @return this object for method chaining
6650 */
6651 public Message setData(String dataMimeType, Uri dataUri) {
6652 mDataMimeType = dataMimeType;
6653 mDataUri = dataUri;
6654 return this;
6655 }
6656
Alex Hillsfc737de2016-03-23 17:33:02 -04006657 /**
6658 * Get the text to be used for this message, or the fallback text if a type and content
6659 * Uri have been set
6660 */
6661 public CharSequence getText() {
6662 return mText;
6663 }
6664
6665 /**
6666 * Get the time at which this message arrived
6667 */
6668 public long getTimestamp() {
6669 return mTimestamp;
6670 }
6671
6672 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006673 * Get the extras Bundle for this message.
6674 */
6675 public Bundle getExtras() {
6676 return mExtras;
6677 }
6678
6679 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006680 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006681 *
6682 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006683 */
6684 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006685 return mSender == null ? null : mSender.getName();
6686 }
6687
6688 /**
6689 * Get the sender associated with this message.
6690 */
6691 @Nullable
6692 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006693 return mSender;
6694 }
6695
6696 /**
6697 * Get the MIME type of the data pointed to by the Uri
6698 */
6699 public String getDataMimeType() {
6700 return mDataMimeType;
6701 }
6702
6703 /**
6704 * Get the the Uri pointing to the content of the message. Can be null, in which case
6705 * {@see #getText()} is used.
6706 */
6707 public Uri getDataUri() {
6708 return mDataUri;
6709 }
6710
Alex Hillsd9b04d92016-04-11 16:38:16 -04006711 private Bundle toBundle() {
6712 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006713 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006714 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006715 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006716 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006717 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006718 // Legacy listeners need this
6719 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6720 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006721 }
6722 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006723 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006724 }
6725 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006726 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006727 }
Shane Brennan5a871862017-03-11 13:14:17 -08006728 if (mExtras != null) {
6729 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6730 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006731 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006732 }
6733
Alex Hillsd9b04d92016-04-11 16:38:16 -04006734 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6735 Bundle[] bundles = new Bundle[messages.size()];
6736 final int N = messages.size();
6737 for (int i = 0; i < N; i++) {
6738 bundles[i] = messages.get(i).toBundle();
6739 }
6740 return bundles;
6741 }
6742
Selim Cinek88188f22017-09-19 16:46:56 -07006743 /**
6744 * @return A list of messages read from the bundles.
6745 *
6746 * @hide
6747 */
6748 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6749 if (bundles == null) {
6750 return new ArrayList<>();
6751 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006752 List<Message> messages = new ArrayList<>(bundles.length);
6753 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006754 if (bundles[i] instanceof Bundle) {
6755 Message message = getMessageFromBundle((Bundle)bundles[i]);
6756 if (message != null) {
6757 messages.add(message);
6758 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006759 }
6760 }
6761 return messages;
6762 }
6763
6764 static Message getMessageFromBundle(Bundle bundle) {
6765 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006766 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006767 return null;
6768 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006769
6770 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6771 if (senderPerson == null) {
6772 // Legacy apps that use compat don't actually provide the sender objects
6773 // We need to fix the compat version to provide people / use
6774 // the native api instead
6775 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6776 if (senderName != null) {
6777 senderPerson = new Person().setName(senderName);
6778 }
6779 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006780 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006781 bundle.getLong(KEY_TIMESTAMP),
6782 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006783 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6784 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006785 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6786 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006787 }
Shane Brennan5a871862017-03-11 13:14:17 -08006788 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6789 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6790 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006791 return message;
6792 }
6793 } catch (ClassCastException e) {
6794 return null;
6795 }
6796 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006797 }
6798 }
6799
6800 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006801 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006802 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006803 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006804 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006805 * Notification notif = new Notification.Builder(mContext)
6806 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6807 * .setContentText(subject)
6808 * .setSmallIcon(R.drawable.new_mail)
6809 * .setLargeIcon(aBitmap)
6810 * .setStyle(new Notification.InboxStyle()
6811 * .addLine(str1)
6812 * .addLine(str2)
6813 * .setContentTitle(&quot;&quot;)
6814 * .setSummaryText(&quot;+3 more&quot;))
6815 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006816 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006817 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006818 * @see Notification#bigContentView
6819 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006820 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006821 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6822
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006823 public InboxStyle() {
6824 }
6825
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006826 /**
6827 * @deprecated use {@code InboxStyle()}.
6828 */
6829 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006830 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006831 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006832 }
6833
Chris Wrend6297db2012-05-03 16:20:13 -04006834 /**
6835 * Overrides ContentTitle in the big form of the template.
6836 * This defaults to the value passed to setContentTitle().
6837 */
6838 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006839 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006840 return this;
6841 }
6842
6843 /**
6844 * Set the first line of text after the detail section in the big form of the template.
6845 */
6846 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006847 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006848 return this;
6849 }
6850
Chris Wren0bd664d2012-08-01 13:56:56 -04006851 /**
6852 * Append a line to the digest section of the Inbox notification.
6853 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006854 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006855 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006856 return this;
6857 }
6858
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006859 /**
6860 * @hide
6861 */
6862 public void addExtras(Bundle extras) {
6863 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006864
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006865 CharSequence[] a = new CharSequence[mTexts.size()];
6866 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6867 }
6868
Christoph Studer4600f9b2014-07-22 22:44:43 +02006869 /**
6870 * @hide
6871 */
6872 @Override
6873 protected void restoreFromExtras(Bundle extras) {
6874 super.restoreFromExtras(extras);
6875
6876 mTexts.clear();
6877 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6878 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6879 }
6880 }
6881
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006882 /**
6883 * @hide
6884 */
6885 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006886 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006887 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006888 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6889 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006890
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006891 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006892
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006893 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006894
Chris Wrend6297db2012-05-03 16:20:13 -04006895 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 -04006896 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006897
Chris Wren4ed80d52012-05-17 09:30:03 -04006898 // Make sure all rows are gone in case we reuse a view.
6899 for (int rowId : rowIds) {
6900 contentView.setViewVisibility(rowId, View.GONE);
6901 }
6902
Daniel Sandler879c5e02012-04-17 16:46:51 -04006903 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006904 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6905 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006906 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006907 int onlyViewId = 0;
6908 int maxRows = rowIds.length;
6909 if (mBuilder.mActions.size() > 0) {
6910 maxRows--;
6911 }
6912 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006913 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006914 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006915 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006916 contentView.setTextViewText(rowIds[i],
6917 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006918 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006919 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006920 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006921 if (first) {
6922 onlyViewId = rowIds[i];
6923 } else {
6924 onlyViewId = 0;
6925 }
Selim Cinek247fa012016-02-18 09:50:48 -08006926 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006927 }
6928 i++;
6929 }
Selim Cinek07c80172016-04-21 16:40:47 -07006930 if (onlyViewId != 0) {
6931 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6932 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6933 R.dimen.notification_text_margin_top);
6934 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6935 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006936
Daniel Sandler879c5e02012-04-17 16:46:51 -04006937 return contentView;
6938 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006939
Selim Cinek247fa012016-02-18 09:50:48 -08006940 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006941 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006942 if (first) {
6943 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6944 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6945 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006946 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006947 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006948 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006949 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006950 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006951 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006952 }
Dan Sandler842dd772014-05-15 09:36:47 -04006953
6954 /**
6955 * Notification style for media playback notifications.
6956 *
6957 * In the expanded form, {@link Notification#bigContentView}, up to 5
6958 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006959 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006960 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6961 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6962 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006963 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006964 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6965 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006966 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006967 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006968 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006969 * Notifications created with MediaStyle will have their category set to
6970 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6971 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006972 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006973 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6974 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006975 * the System UI can identify this as a notification representing an active media session
6976 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6977 *
Selim Cinek99104832017-01-25 14:47:33 -08006978 * <p>
6979 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6980 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6981 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6982 * <p>
6983 *
Dan Sandler842dd772014-05-15 09:36:47 -04006984 * To use this style with your Notification, feed it to
6985 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6986 * <pre class="prettyprint">
6987 * Notification noti = new Notification.Builder()
6988 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006989 * .setContentTitle(&quot;Track title&quot;)
6990 * .setContentText(&quot;Artist - Album&quot;)
6991 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006992 * .setStyle(<b>new Notification.MediaStyle()</b>
6993 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006994 * .build();
6995 * </pre>
6996 *
6997 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006998 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006999 */
7000 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007001 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007002 static final int MAX_MEDIA_BUTTONS = 5;
7003
7004 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007005 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007006
7007 public MediaStyle() {
7008 }
7009
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007010 /**
7011 * @deprecated use {@code MediaStyle()}.
7012 */
7013 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007014 public MediaStyle(Builder builder) {
7015 setBuilder(builder);
7016 }
7017
7018 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007019 * 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 -04007020 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007021 *
7022 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007023 */
7024 public MediaStyle setShowActionsInCompactView(int...actions) {
7025 mActionsToShowInCompact = actions;
7026 return this;
7027 }
7028
7029 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007030 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7031 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007032 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007033 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007034 mToken = token;
7035 return this;
7036 }
7037
Christoph Studer4600f9b2014-07-22 22:44:43 +02007038 /**
7039 * @hide
7040 */
Dan Sandler842dd772014-05-15 09:36:47 -04007041 @Override
7042 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007043 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007044 if (wip.category == null) {
7045 wip.category = Notification.CATEGORY_TRANSPORT;
7046 }
Dan Sandler842dd772014-05-15 09:36:47 -04007047 return wip;
7048 }
7049
Christoph Studer4600f9b2014-07-22 22:44:43 +02007050 /**
7051 * @hide
7052 */
7053 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007054 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007055 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007056 }
7057
7058 /**
7059 * @hide
7060 */
7061 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007062 public RemoteViews makeBigContentView() {
7063 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007064 }
7065
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007066 /**
7067 * @hide
7068 */
7069 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007070 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007071 RemoteViews expanded = makeMediaBigContentView();
7072 return expanded != null ? expanded : makeMediaContentView();
7073 }
7074
Dan Sandler842dd772014-05-15 09:36:47 -04007075 /** @hide */
7076 @Override
7077 public void addExtras(Bundle extras) {
7078 super.addExtras(extras);
7079
7080 if (mToken != null) {
7081 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7082 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007083 if (mActionsToShowInCompact != null) {
7084 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7085 }
Dan Sandler842dd772014-05-15 09:36:47 -04007086 }
7087
Christoph Studer4600f9b2014-07-22 22:44:43 +02007088 /**
7089 * @hide
7090 */
7091 @Override
7092 protected void restoreFromExtras(Bundle extras) {
7093 super.restoreFromExtras(extras);
7094
7095 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7096 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7097 }
7098 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7099 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7100 }
7101 }
7102
Selim Cinek5bf069a2015-11-10 19:14:27 -05007103 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007104 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007105 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007106 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007107 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007108
7109 // If the action buttons should not be tinted, then just use the default
7110 // notification color. Otherwise, just use the passed-in color.
7111 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7112 ? color
7113 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7114 Notification.COLOR_DEFAULT);
7115
Sunny Goyal5b153922017-09-21 21:00:36 -07007116 button.setDrawableTint(R.id.action0, false, tintColor,
7117 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007118 if (!tombstone) {
7119 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7120 }
7121 button.setContentDescription(R.id.action0, action.title);
7122 return button;
7123 }
7124
7125 private RemoteViews makeMediaContentView() {
7126 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007127 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007128
7129 final int numActions = mBuilder.mActions.size();
7130 final int N = mActionsToShowInCompact == null
7131 ? 0
7132 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7133 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007134 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007135 for (int i = 0; i < N; i++) {
7136 if (i >= numActions) {
7137 throw new IllegalArgumentException(String.format(
7138 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7139 i, numActions - 1));
7140 }
7141
7142 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007143 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007144 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007145 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007146 }
7147 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007148 handleImage(view);
7149 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007150 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007151 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007152 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007153 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007154 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007155 return view;
7156 }
7157
Selim Cinek99104832017-01-25 14:47:33 -08007158 private int getPrimaryHighlightColor() {
7159 return mBuilder.getPrimaryHighlightColor();
7160 }
7161
Dan Sandler842dd772014-05-15 09:36:47 -04007162 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007163 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007164 // Dont add an expanded view if there is no more content to be revealed
7165 int actionsInCompact = mActionsToShowInCompact == null
7166 ? 0
7167 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007168 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007169 return null;
7170 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007171 RemoteViews big = mBuilder.applyStandardTemplate(
7172 R.layout.notification_template_material_big_media,
7173 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007174
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007175 if (actionCount > 0) {
7176 big.removeAllViews(com.android.internal.R.id.media_actions);
7177 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007178 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007179 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007180 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007181 }
7182 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007183 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007184 return big;
7185 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007186
Selim Cinek5bf069a2015-11-10 19:14:27 -05007187 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007188 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007189 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7190 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007191 }
7192 }
7193
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007194 /**
7195 * @hide
7196 */
7197 @Override
7198 protected boolean hasProgress() {
7199 return false;
7200 }
Dan Sandler842dd772014-05-15 09:36:47 -04007201 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007202
Selim Cinek593610c2016-02-16 18:42:57 -08007203 /**
7204 * Notification style for custom views that are decorated by the system
7205 *
7206 * <p>Instead of providing a notification that is completely custom, a developer can set this
7207 * style and still obtain system decorations like the notification header with the expand
7208 * affordance and actions.
7209 *
7210 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7211 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7212 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7213 * corresponding custom views to display.
7214 *
7215 * To use this style with your Notification, feed it to
7216 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7217 * <pre class="prettyprint">
7218 * Notification noti = new Notification.Builder()
7219 * .setSmallIcon(R.drawable.ic_stat_player)
7220 * .setLargeIcon(albumArtBitmap))
7221 * .setCustomContentView(contentView);
7222 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7223 * .build();
7224 * </pre>
7225 */
7226 public static class DecoratedCustomViewStyle extends Style {
7227
7228 public DecoratedCustomViewStyle() {
7229 }
7230
Selim Cinek593610c2016-02-16 18:42:57 -08007231 /**
7232 * @hide
7233 */
7234 public boolean displayCustomViewInline() {
7235 return true;
7236 }
7237
7238 /**
7239 * @hide
7240 */
7241 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007242 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007243 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7244 }
7245
7246 /**
7247 * @hide
7248 */
7249 @Override
7250 public RemoteViews makeBigContentView() {
7251 return makeDecoratedBigContentView();
7252 }
7253
7254 /**
7255 * @hide
7256 */
7257 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007258 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007259 return makeDecoratedHeadsUpContentView();
7260 }
7261
Selim Cinek593610c2016-02-16 18:42:57 -08007262 private RemoteViews makeDecoratedHeadsUpContentView() {
7263 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7264 ? mBuilder.mN.contentView
7265 : mBuilder.mN.headsUpContentView;
7266 if (mBuilder.mActions.size() == 0) {
7267 return makeStandardTemplateWithCustomContent(headsUpContentView);
7268 }
7269 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7270 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007271 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007272 return remoteViews;
7273 }
7274
Selim Cinek593610c2016-02-16 18:42:57 -08007275 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7276 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7277 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007278 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007279 return remoteViews;
7280 }
7281
Selim Cinek593610c2016-02-16 18:42:57 -08007282 private RemoteViews makeDecoratedBigContentView() {
7283 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7284 ? mBuilder.mN.contentView
7285 : mBuilder.mN.bigContentView;
7286 if (mBuilder.mActions.size() == 0) {
7287 return makeStandardTemplateWithCustomContent(bigContentView);
7288 }
7289 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7290 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007291 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007292 return remoteViews;
7293 }
Selim Cinek247fa012016-02-18 09:50:48 -08007294
7295 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7296 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007297 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007298 // Need to clone customContent before adding, because otherwise it can no longer be
7299 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007300 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007301 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7302 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007303 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007304 }
Selim Cinek247fa012016-02-18 09:50:48 -08007305 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007306 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007307 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007308 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007309 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007310 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007311 }
Selim Cinek593610c2016-02-16 18:42:57 -08007312 }
7313
Selim Cinek03eb3b72016-02-18 10:39:45 -08007314 /**
7315 * Notification style for media custom views that are decorated by the system
7316 *
7317 * <p>Instead of providing a media notification that is completely custom, a developer can set
7318 * this style and still obtain system decorations like the notification header with the expand
7319 * affordance and actions.
7320 *
7321 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7322 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7323 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7324 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007325 * <p>
7326 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7327 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7328 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007329 * To use this style with your Notification, feed it to
7330 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7331 * <pre class="prettyprint">
7332 * Notification noti = new Notification.Builder()
7333 * .setSmallIcon(R.drawable.ic_stat_player)
7334 * .setLargeIcon(albumArtBitmap))
7335 * .setCustomContentView(contentView);
7336 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7337 * .setMediaSession(mySession))
7338 * .build();
7339 * </pre>
7340 *
7341 * @see android.app.Notification.DecoratedCustomViewStyle
7342 * @see android.app.Notification.MediaStyle
7343 */
7344 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7345
7346 public DecoratedMediaCustomViewStyle() {
7347 }
7348
Selim Cinek03eb3b72016-02-18 10:39:45 -08007349 /**
7350 * @hide
7351 */
7352 public boolean displayCustomViewInline() {
7353 return true;
7354 }
7355
7356 /**
7357 * @hide
7358 */
7359 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007360 public RemoteViews makeContentView(boolean increasedHeight) {
7361 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007362 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7363 mBuilder.mN.contentView);
7364 }
7365
7366 /**
7367 * @hide
7368 */
7369 @Override
7370 public RemoteViews makeBigContentView() {
7371 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7372 ? mBuilder.mN.bigContentView
7373 : mBuilder.mN.contentView;
7374 return makeBigContentViewWithCustomContent(customRemoteView);
7375 }
7376
7377 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7378 RemoteViews remoteViews = super.makeBigContentView();
7379 if (remoteViews != null) {
7380 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7381 customRemoteView);
7382 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007383 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007384 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7385 customRemoteView);
7386 } else {
7387 return null;
7388 }
7389 }
7390
7391 /**
7392 * @hide
7393 */
7394 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007395 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007396 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7397 ? mBuilder.mN.headsUpContentView
7398 : mBuilder.mN.contentView;
7399 return makeBigContentViewWithCustomContent(customRemoteView);
7400 }
7401
7402 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7403 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007404 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007405 // Need to clone customContent before adding, because otherwise it can no longer be
7406 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007407 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007408 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007409 remoteViews.removeAllViews(id);
7410 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007411 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007412 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007413 return remoteViews;
7414 }
7415 }
7416
Selim Cineke7238dd2017-12-14 17:48:32 -08007417 /**
7418 * A Person associated with this Notification.
7419 */
7420 public static final class Person implements Parcelable {
7421 @Nullable private CharSequence mName;
7422 @Nullable private Icon mIcon;
7423 @Nullable private String mUri;
7424 @Nullable private String mKey;
7425
7426 protected Person(Parcel in) {
7427 mName = in.readCharSequence();
7428 if (in.readInt() != 0) {
7429 mIcon = Icon.CREATOR.createFromParcel(in);
7430 }
7431 mUri = in.readString();
7432 mKey = in.readString();
7433 }
7434
7435 /**
7436 * Create a new person.
7437 */
7438 public Person() {
7439 }
7440
7441 /**
7442 * Give this person a name.
7443 *
7444 * @param name the name of this person
7445 */
7446 public Person setName(@Nullable CharSequence name) {
7447 this.mName = name;
7448 return this;
7449 }
7450
7451 /**
7452 * Add an icon for this person.
7453 * <br />
7454 * This is currently only used for {@link MessagingStyle} notifications and should not be
7455 * provided otherwise, in order to save memory. The system will prefer this icon over any
7456 * images that are resolved from the URI.
7457 *
7458 * @param icon the icon of the person
7459 */
7460 public Person setIcon(@Nullable Icon icon) {
7461 this.mIcon = icon;
7462 return this;
7463 }
7464
7465 /**
7466 * Set a URI associated with this person.
7467 *
7468 * <P>
7469 * Depending on user preferences, adding a URI to a Person may allow the notification to
7470 * pass through interruption filters, if this notification is of
7471 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7472 * The addition of people may also cause this notification to appear more prominently in
7473 * the user interface.
7474 * </P>
7475 *
7476 * <P>
7477 * The person should be specified by the {@code String} representation of a
7478 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7479 * </P>
7480 *
7481 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7482 * URIs. The path part of these URIs must exist in the contacts database, in the
7483 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7484 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7485 * </P>
7486 *
7487 * @param uri a URI for the person
7488 */
7489 public Person setUri(@Nullable String uri) {
7490 mUri = uri;
7491 return this;
7492 }
7493
7494 /**
7495 * Add a key to this person in order to uniquely identify it.
7496 * This is especially useful if the name doesn't uniquely identify this person or if the
7497 * display name is a short handle of the actual name.
7498 *
7499 * <P>If no key is provided, the name serves as as the key for the purpose of
7500 * identification.</P>
7501 *
7502 * @param key the key that uniquely identifies this person
7503 */
7504 public Person setKey(@Nullable String key) {
7505 mKey = key;
7506 return this;
7507 }
7508
7509
7510 /**
7511 * @return the uri provided for this person or {@code null} if no Uri was provided
7512 */
7513 @Nullable
7514 public String getUri() {
7515 return mUri;
7516 }
7517
7518 /**
7519 * @return the name provided for this person or {@code null} if no name was provided
7520 */
7521 @Nullable
7522 public CharSequence getName() {
7523 return mName;
7524 }
7525
7526 /**
7527 * @return the icon provided for this person or {@code null} if no icon was provided
7528 */
7529 @Nullable
7530 public Icon getIcon() {
7531 return mIcon;
7532 }
7533
7534 /**
7535 * @return the key provided for this person or {@code null} if no key was provided
7536 */
7537 @Nullable
7538 public String getKey() {
7539 return mKey;
7540 }
7541
7542 /**
7543 * @return the URI associated with this person, or "name:mName" otherwise
7544 * @hide
7545 */
7546 public String resolveToLegacyUri() {
7547 if (mUri != null) {
7548 return mUri;
7549 }
7550 if (mName != null) {
7551 return "name:" + mName;
7552 }
7553 return "";
7554 }
7555
7556 @Override
7557 public int describeContents() {
7558 return 0;
7559 }
7560
7561 @Override
7562 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7563 dest.writeCharSequence(mName);
7564 if (mIcon != null) {
7565 dest.writeInt(1);
7566 mIcon.writeToParcel(dest, 0);
7567 } else {
7568 dest.writeInt(0);
7569 }
7570 dest.writeString(mUri);
7571 dest.writeString(mKey);
7572 }
7573
7574 public static final Creator<Person> CREATOR = new Creator<Person>() {
7575 @Override
7576 public Person createFromParcel(Parcel in) {
7577 return new Person(in);
7578 }
7579
7580 @Override
7581 public Person[] newArray(int size) {
7582 return new Person[size];
7583 }
7584 };
7585 }
7586
Christoph Studer4600f9b2014-07-22 22:44:43 +02007587 // When adding a new Style subclass here, don't forget to update
7588 // Builder.getNotificationStyleClass.
7589
Griff Hazen61a9e862014-05-22 16:05:19 -07007590 /**
7591 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7592 * metadata or change options on a notification builder.
7593 */
7594 public interface Extender {
7595 /**
7596 * Apply this extender to a notification builder.
7597 * @param builder the builder to be modified.
7598 * @return the build object for chaining.
7599 */
7600 public Builder extend(Builder builder);
7601 }
7602
7603 /**
7604 * Helper class to add wearable extensions to notifications.
7605 * <p class="note"> See
7606 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7607 * for Android Wear</a> for more information on how to use this class.
7608 * <p>
7609 * To create a notification with wearable extensions:
7610 * <ol>
7611 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7612 * properties.
7613 * <li>Create a {@link android.app.Notification.WearableExtender}.
7614 * <li>Set wearable-specific properties using the
7615 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7616 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7617 * notification.
7618 * <li>Post the notification to the notification system with the
7619 * {@code NotificationManager.notify(...)} methods.
7620 * </ol>
7621 *
7622 * <pre class="prettyprint">
7623 * Notification notif = new Notification.Builder(mContext)
7624 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7625 * .setContentText(subject)
7626 * .setSmallIcon(R.drawable.new_mail)
7627 * .extend(new Notification.WearableExtender()
7628 * .setContentIcon(R.drawable.new_mail))
7629 * .build();
7630 * NotificationManager notificationManger =
7631 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7632 * notificationManger.notify(0, notif);</pre>
7633 *
7634 * <p>Wearable extensions can be accessed on an existing notification by using the
7635 * {@code WearableExtender(Notification)} constructor,
7636 * and then using the {@code get} methods to access values.
7637 *
7638 * <pre class="prettyprint">
7639 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7640 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007641 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007642 */
7643 public static final class WearableExtender implements Extender {
7644 /**
7645 * Sentinel value for an action index that is unset.
7646 */
7647 public static final int UNSET_ACTION_INDEX = -1;
7648
7649 /**
7650 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7651 * default sizing.
7652 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007653 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007654 * on their content.
7655 */
7656 public static final int SIZE_DEFAULT = 0;
7657
7658 /**
7659 * Size value for use with {@link #setCustomSizePreset} to show this notification
7660 * with an extra small size.
7661 * <p>This value is only applicable for custom display notifications created using
7662 * {@link #setDisplayIntent}.
7663 */
7664 public static final int SIZE_XSMALL = 1;
7665
7666 /**
7667 * Size value for use with {@link #setCustomSizePreset} to show this notification
7668 * with a small size.
7669 * <p>This value is only applicable for custom display notifications created using
7670 * {@link #setDisplayIntent}.
7671 */
7672 public static final int SIZE_SMALL = 2;
7673
7674 /**
7675 * Size value for use with {@link #setCustomSizePreset} to show this notification
7676 * with a medium size.
7677 * <p>This value is only applicable for custom display notifications created using
7678 * {@link #setDisplayIntent}.
7679 */
7680 public static final int SIZE_MEDIUM = 3;
7681
7682 /**
7683 * Size value for use with {@link #setCustomSizePreset} to show this notification
7684 * with a large size.
7685 * <p>This value is only applicable for custom display notifications created using
7686 * {@link #setDisplayIntent}.
7687 */
7688 public static final int SIZE_LARGE = 4;
7689
Griff Hazend5f11f92014-05-27 15:40:09 -07007690 /**
7691 * Size value for use with {@link #setCustomSizePreset} to show this notification
7692 * full screen.
7693 * <p>This value is only applicable for custom display notifications created using
7694 * {@link #setDisplayIntent}.
7695 */
7696 public static final int SIZE_FULL_SCREEN = 5;
7697
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007698 /**
7699 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7700 * short amount of time when this notification is displayed on the screen. This
7701 * is the default value.
7702 */
7703 public static final int SCREEN_TIMEOUT_SHORT = 0;
7704
7705 /**
7706 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7707 * for a longer amount of time when this notification is displayed on the screen.
7708 */
7709 public static final int SCREEN_TIMEOUT_LONG = -1;
7710
Griff Hazen61a9e862014-05-22 16:05:19 -07007711 /** Notification extra which contains wearable extensions */
7712 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7713
Pete Gastaf6781d2014-10-07 15:17:05 -04007714 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007715 private static final String KEY_ACTIONS = "actions";
7716 private static final String KEY_FLAGS = "flags";
7717 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7718 private static final String KEY_PAGES = "pages";
7719 private static final String KEY_BACKGROUND = "background";
7720 private static final String KEY_CONTENT_ICON = "contentIcon";
7721 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7722 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7723 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7724 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7725 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007726 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007727 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007728 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007729
7730 // Flags bitwise-ored to mFlags
7731 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7732 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7733 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7734 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007735 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007736 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007737 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007738
7739 // Default value for flags integer
7740 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7741
7742 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7743 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7744
7745 private ArrayList<Action> mActions = new ArrayList<Action>();
7746 private int mFlags = DEFAULT_FLAGS;
7747 private PendingIntent mDisplayIntent;
7748 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7749 private Bitmap mBackground;
7750 private int mContentIcon;
7751 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7752 private int mContentActionIndex = UNSET_ACTION_INDEX;
7753 private int mCustomSizePreset = SIZE_DEFAULT;
7754 private int mCustomContentHeight;
7755 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007756 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007757 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007758 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007759
7760 /**
7761 * Create a {@link android.app.Notification.WearableExtender} with default
7762 * options.
7763 */
7764 public WearableExtender() {
7765 }
7766
7767 public WearableExtender(Notification notif) {
7768 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7769 if (wearableBundle != null) {
7770 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7771 if (actions != null) {
7772 mActions.addAll(actions);
7773 }
7774
7775 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7776 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7777
7778 Notification[] pages = getNotificationArrayFromBundle(
7779 wearableBundle, KEY_PAGES);
7780 if (pages != null) {
7781 Collections.addAll(mPages, pages);
7782 }
7783
7784 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7785 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7786 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7787 DEFAULT_CONTENT_ICON_GRAVITY);
7788 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7789 UNSET_ACTION_INDEX);
7790 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7791 SIZE_DEFAULT);
7792 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7793 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007794 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007795 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007796 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007797 }
7798 }
7799
7800 /**
7801 * Apply wearable extensions to a notification that is being built. This is typically
7802 * called by the {@link android.app.Notification.Builder#extend} method of
7803 * {@link android.app.Notification.Builder}.
7804 */
7805 @Override
7806 public Notification.Builder extend(Notification.Builder builder) {
7807 Bundle wearableBundle = new Bundle();
7808
7809 if (!mActions.isEmpty()) {
7810 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7811 }
7812 if (mFlags != DEFAULT_FLAGS) {
7813 wearableBundle.putInt(KEY_FLAGS, mFlags);
7814 }
7815 if (mDisplayIntent != null) {
7816 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7817 }
7818 if (!mPages.isEmpty()) {
7819 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7820 new Notification[mPages.size()]));
7821 }
7822 if (mBackground != null) {
7823 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7824 }
7825 if (mContentIcon != 0) {
7826 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7827 }
7828 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7829 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7830 }
7831 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7832 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7833 mContentActionIndex);
7834 }
7835 if (mCustomSizePreset != SIZE_DEFAULT) {
7836 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7837 }
7838 if (mCustomContentHeight != 0) {
7839 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7840 }
7841 if (mGravity != DEFAULT_GRAVITY) {
7842 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7843 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007844 if (mHintScreenTimeout != 0) {
7845 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7846 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007847 if (mDismissalId != null) {
7848 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7849 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007850 if (mBridgeTag != null) {
7851 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7852 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007853
7854 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7855 return builder;
7856 }
7857
7858 @Override
7859 public WearableExtender clone() {
7860 WearableExtender that = new WearableExtender();
7861 that.mActions = new ArrayList<Action>(this.mActions);
7862 that.mFlags = this.mFlags;
7863 that.mDisplayIntent = this.mDisplayIntent;
7864 that.mPages = new ArrayList<Notification>(this.mPages);
7865 that.mBackground = this.mBackground;
7866 that.mContentIcon = this.mContentIcon;
7867 that.mContentIconGravity = this.mContentIconGravity;
7868 that.mContentActionIndex = this.mContentActionIndex;
7869 that.mCustomSizePreset = this.mCustomSizePreset;
7870 that.mCustomContentHeight = this.mCustomContentHeight;
7871 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007872 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007873 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007874 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007875 return that;
7876 }
7877
7878 /**
7879 * Add a wearable action to this notification.
7880 *
7881 * <p>When wearable actions are added using this method, the set of actions that
7882 * show on a wearable device splits from devices that only show actions added
7883 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7884 * of which actions display on different devices.
7885 *
7886 * @param action the action to add to this notification
7887 * @return this object for method chaining
7888 * @see android.app.Notification.Action
7889 */
7890 public WearableExtender addAction(Action action) {
7891 mActions.add(action);
7892 return this;
7893 }
7894
7895 /**
7896 * Adds wearable actions to this notification.
7897 *
7898 * <p>When wearable actions are added using this method, the set of actions that
7899 * show on a wearable device splits from devices that only show actions added
7900 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7901 * of which actions display on different devices.
7902 *
7903 * @param actions the actions to add to this notification
7904 * @return this object for method chaining
7905 * @see android.app.Notification.Action
7906 */
7907 public WearableExtender addActions(List<Action> actions) {
7908 mActions.addAll(actions);
7909 return this;
7910 }
7911
7912 /**
7913 * Clear all wearable actions present on this builder.
7914 * @return this object for method chaining.
7915 * @see #addAction
7916 */
7917 public WearableExtender clearActions() {
7918 mActions.clear();
7919 return this;
7920 }
7921
7922 /**
7923 * Get the wearable actions present on this notification.
7924 */
7925 public List<Action> getActions() {
7926 return mActions;
7927 }
7928
7929 /**
7930 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007931 * this notification. The {@link PendingIntent} provided should be for an activity.
7932 *
7933 * <pre class="prettyprint">
7934 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7935 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7936 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7937 * Notification notif = new Notification.Builder(context)
7938 * .extend(new Notification.WearableExtender()
7939 * .setDisplayIntent(displayPendingIntent)
7940 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7941 * .build();</pre>
7942 *
7943 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007944 * should have an empty task affinity. It is also recommended to use the device
7945 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007946 *
7947 * <p>Example AndroidManifest.xml entry:
7948 * <pre class="prettyprint">
7949 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7950 * android:exported=&quot;true&quot;
7951 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007952 * android:taskAffinity=&quot;&quot;
7953 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007954 *
7955 * @param intent the {@link PendingIntent} for an activity
7956 * @return this object for method chaining
7957 * @see android.app.Notification.WearableExtender#getDisplayIntent
7958 */
7959 public WearableExtender setDisplayIntent(PendingIntent intent) {
7960 mDisplayIntent = intent;
7961 return this;
7962 }
7963
7964 /**
7965 * Get the intent to launch inside of an activity view when displaying this
7966 * notification. This {@code PendingIntent} should be for an activity.
7967 */
7968 public PendingIntent getDisplayIntent() {
7969 return mDisplayIntent;
7970 }
7971
7972 /**
7973 * Add an additional page of content to display with this notification. The current
7974 * notification forms the first page, and pages added using this function form
7975 * subsequent pages. This field can be used to separate a notification into multiple
7976 * sections.
7977 *
7978 * @param page the notification to add as another page
7979 * @return this object for method chaining
7980 * @see android.app.Notification.WearableExtender#getPages
7981 */
7982 public WearableExtender addPage(Notification page) {
7983 mPages.add(page);
7984 return this;
7985 }
7986
7987 /**
7988 * Add additional pages of content to display with this notification. The current
7989 * notification forms the first page, and pages added using this function form
7990 * subsequent pages. This field can be used to separate a notification into multiple
7991 * sections.
7992 *
7993 * @param pages a list of notifications
7994 * @return this object for method chaining
7995 * @see android.app.Notification.WearableExtender#getPages
7996 */
7997 public WearableExtender addPages(List<Notification> pages) {
7998 mPages.addAll(pages);
7999 return this;
8000 }
8001
8002 /**
8003 * Clear all additional pages present on this builder.
8004 * @return this object for method chaining.
8005 * @see #addPage
8006 */
8007 public WearableExtender clearPages() {
8008 mPages.clear();
8009 return this;
8010 }
8011
8012 /**
8013 * Get the array of additional pages of content for displaying this notification. The
8014 * current notification forms the first page, and elements within this array form
8015 * subsequent pages. This field can be used to separate a notification into multiple
8016 * sections.
8017 * @return the pages for this notification
8018 */
8019 public List<Notification> getPages() {
8020 return mPages;
8021 }
8022
8023 /**
8024 * Set a background image to be displayed behind the notification content.
8025 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8026 * will work with any notification style.
8027 *
8028 * @param background the background bitmap
8029 * @return this object for method chaining
8030 * @see android.app.Notification.WearableExtender#getBackground
8031 */
8032 public WearableExtender setBackground(Bitmap background) {
8033 mBackground = background;
8034 return this;
8035 }
8036
8037 /**
8038 * Get a background image to be displayed behind the notification content.
8039 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8040 * will work with any notification style.
8041 *
8042 * @return the background image
8043 * @see android.app.Notification.WearableExtender#setBackground
8044 */
8045 public Bitmap getBackground() {
8046 return mBackground;
8047 }
8048
8049 /**
8050 * Set an icon that goes with the content of this notification.
8051 */
8052 public WearableExtender setContentIcon(int icon) {
8053 mContentIcon = icon;
8054 return this;
8055 }
8056
8057 /**
8058 * Get an icon that goes with the content of this notification.
8059 */
8060 public int getContentIcon() {
8061 return mContentIcon;
8062 }
8063
8064 /**
8065 * Set the gravity that the content icon should have within the notification display.
8066 * Supported values include {@link android.view.Gravity#START} and
8067 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8068 * @see #setContentIcon
8069 */
8070 public WearableExtender setContentIconGravity(int contentIconGravity) {
8071 mContentIconGravity = contentIconGravity;
8072 return this;
8073 }
8074
8075 /**
8076 * Get the gravity that the content icon should have within the notification display.
8077 * Supported values include {@link android.view.Gravity#START} and
8078 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8079 * @see #getContentIcon
8080 */
8081 public int getContentIconGravity() {
8082 return mContentIconGravity;
8083 }
8084
8085 /**
8086 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008087 * this notification. This action will no longer display separately from the
8088 * notification's content.
8089 *
Griff Hazenca48d352014-05-28 22:37:13 -07008090 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008091 * set, although the list of available actions comes from the main notification and not
8092 * from the child page's notification.
8093 *
8094 * @param actionIndex The index of the action to hoist onto the current notification page.
8095 * If wearable actions were added to the main notification, this index
8096 * will apply to that list, otherwise it will apply to the regular
8097 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008098 */
8099 public WearableExtender setContentAction(int actionIndex) {
8100 mContentActionIndex = actionIndex;
8101 return this;
8102 }
8103
8104 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008105 * Get the index of the notification action, if any, that was specified as being clickable
8106 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008107 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008108 *
Griff Hazenca48d352014-05-28 22:37:13 -07008109 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008110 * set, although the list of available actions comes from the main notification and not
8111 * from the child page's notification.
8112 *
8113 * <p>If wearable specific actions were added to the main notification, this index will
8114 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008115 *
8116 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008117 */
8118 public int getContentAction() {
8119 return mContentActionIndex;
8120 }
8121
8122 /**
8123 * Set the gravity that this notification should have within the available viewport space.
8124 * Supported values include {@link android.view.Gravity#TOP},
8125 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8126 * The default value is {@link android.view.Gravity#BOTTOM}.
8127 */
8128 public WearableExtender setGravity(int gravity) {
8129 mGravity = gravity;
8130 return this;
8131 }
8132
8133 /**
8134 * Get the gravity that this notification should have within the available viewport space.
8135 * Supported values include {@link android.view.Gravity#TOP},
8136 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8137 * The default value is {@link android.view.Gravity#BOTTOM}.
8138 */
8139 public int getGravity() {
8140 return mGravity;
8141 }
8142
8143 /**
8144 * Set the custom size preset for the display of this notification out of the available
8145 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8146 * {@link #SIZE_LARGE}.
8147 * <p>Some custom size presets are only applicable for custom display notifications created
8148 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8149 * documentation for the preset in question. See also
8150 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8151 */
8152 public WearableExtender setCustomSizePreset(int sizePreset) {
8153 mCustomSizePreset = sizePreset;
8154 return this;
8155 }
8156
8157 /**
8158 * Get the custom size preset for the display of this notification out of the available
8159 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8160 * {@link #SIZE_LARGE}.
8161 * <p>Some custom size presets are only applicable for custom display notifications created
8162 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8163 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8164 */
8165 public int getCustomSizePreset() {
8166 return mCustomSizePreset;
8167 }
8168
8169 /**
8170 * Set the custom height in pixels for the display of this notification's content.
8171 * <p>This option is only available for custom display notifications created
8172 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8173 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8174 * {@link #getCustomContentHeight}.
8175 */
8176 public WearableExtender setCustomContentHeight(int height) {
8177 mCustomContentHeight = height;
8178 return this;
8179 }
8180
8181 /**
8182 * Get the custom height in pixels for the display of this notification's content.
8183 * <p>This option is only available for custom display notifications created
8184 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8185 * {@link #setCustomContentHeight}.
8186 */
8187 public int getCustomContentHeight() {
8188 return mCustomContentHeight;
8189 }
8190
8191 /**
8192 * Set whether the scrolling position for the contents of this notification should start
8193 * at the bottom of the contents instead of the top when the contents are too long to
8194 * display within the screen. Default is false (start scroll at the top).
8195 */
8196 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8197 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8198 return this;
8199 }
8200
8201 /**
8202 * Get whether the scrolling position for the contents of this notification should start
8203 * at the bottom of the contents instead of the top when the contents are too long to
8204 * display within the screen. Default is false (start scroll at the top).
8205 */
8206 public boolean getStartScrollBottom() {
8207 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8208 }
8209
8210 /**
8211 * Set whether the content intent is available when the wearable device is not connected
8212 * to a companion device. The user can still trigger this intent when the wearable device
8213 * is offline, but a visual hint will indicate that the content intent may not be available.
8214 * Defaults to true.
8215 */
8216 public WearableExtender setContentIntentAvailableOffline(
8217 boolean contentIntentAvailableOffline) {
8218 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8219 return this;
8220 }
8221
8222 /**
8223 * Get whether the content intent is available when the wearable device is not connected
8224 * to a companion device. The user can still trigger this intent when the wearable device
8225 * is offline, but a visual hint will indicate that the content intent may not be available.
8226 * Defaults to true.
8227 */
8228 public boolean getContentIntentAvailableOffline() {
8229 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8230 }
8231
8232 /**
8233 * Set a hint that this notification's icon should not be displayed.
8234 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8235 * @return this object for method chaining
8236 */
8237 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8238 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8239 return this;
8240 }
8241
8242 /**
8243 * Get a hint that this notification's icon should not be displayed.
8244 * @return {@code true} if this icon should not be displayed, false otherwise.
8245 * The default value is {@code false} if this was never set.
8246 */
8247 public boolean getHintHideIcon() {
8248 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8249 }
8250
8251 /**
8252 * Set a visual hint that only the background image of this notification should be
8253 * displayed, and other semantic content should be hidden. This hint is only applicable
8254 * to sub-pages added using {@link #addPage}.
8255 */
8256 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8257 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8258 return this;
8259 }
8260
8261 /**
8262 * Get a visual hint that only the background image of this notification should be
8263 * displayed, and other semantic content should be hidden. This hint is only applicable
8264 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8265 */
8266 public boolean getHintShowBackgroundOnly() {
8267 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8268 }
8269
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008270 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008271 * Set a hint that this notification's background should not be clipped if possible,
8272 * and should instead be resized to fully display on the screen, retaining the aspect
8273 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008274 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8275 * @return this object for method chaining
8276 */
8277 public WearableExtender setHintAvoidBackgroundClipping(
8278 boolean hintAvoidBackgroundClipping) {
8279 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8280 return this;
8281 }
8282
8283 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008284 * Get a hint that this notification's background should not be clipped if possible,
8285 * and should instead be resized to fully display on the screen, retaining the aspect
8286 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008287 * @return {@code true} if it's ok if the background is clipped on the screen, false
8288 * otherwise. The default value is {@code false} if this was never set.
8289 */
8290 public boolean getHintAvoidBackgroundClipping() {
8291 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8292 }
8293
8294 /**
8295 * Set a hint that the screen should remain on for at least this duration when
8296 * this notification is displayed on the screen.
8297 * @param timeout The requested screen timeout in milliseconds. Can also be either
8298 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8299 * @return this object for method chaining
8300 */
8301 public WearableExtender setHintScreenTimeout(int timeout) {
8302 mHintScreenTimeout = timeout;
8303 return this;
8304 }
8305
8306 /**
8307 * Get the duration, in milliseconds, that the screen should remain on for
8308 * when this notification is displayed.
8309 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8310 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8311 */
8312 public int getHintScreenTimeout() {
8313 return mHintScreenTimeout;
8314 }
8315
Alex Hills9ab3a232016-04-05 14:54:56 -04008316 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008317 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8318 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8319 * qr codes, as well as other simple black-and-white tickets.
8320 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8321 * @return this object for method chaining
8322 */
8323 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8324 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8325 return this;
8326 }
8327
8328 /**
8329 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8330 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8331 * qr codes, as well as other simple black-and-white tickets.
8332 * @return {@code true} if it should be displayed in ambient, false otherwise
8333 * otherwise. The default value is {@code false} if this was never set.
8334 */
8335 public boolean getHintAmbientBigPicture() {
8336 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8337 }
8338
8339 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008340 * Set a hint that this notification's content intent will launch an {@link Activity}
8341 * directly, telling the platform that it can generate the appropriate transitions.
8342 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8343 * an activity and transitions should be generated, false otherwise.
8344 * @return this object for method chaining
8345 */
8346 public WearableExtender setHintContentIntentLaunchesActivity(
8347 boolean hintContentIntentLaunchesActivity) {
8348 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8349 return this;
8350 }
8351
8352 /**
8353 * Get a hint that this notification's content intent will launch an {@link Activity}
8354 * directly, telling the platform that it can generate the appropriate transitions
8355 * @return {@code true} if the content intent will launch an activity and transitions should
8356 * be generated, false otherwise. The default value is {@code false} if this was never set.
8357 */
8358 public boolean getHintContentIntentLaunchesActivity() {
8359 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8360 }
8361
Nadia Benbernou948627e2016-04-14 14:41:08 -04008362 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008363 * Sets the dismissal id for this notification. If a notification is posted with a
8364 * dismissal id, then when that notification is canceled, notifications on other wearables
8365 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008366 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008367 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008368 * @param dismissalId the dismissal id of the notification.
8369 * @return this object for method chaining
8370 */
8371 public WearableExtender setDismissalId(String dismissalId) {
8372 mDismissalId = dismissalId;
8373 return this;
8374 }
8375
8376 /**
8377 * Returns the dismissal id of the notification.
8378 * @return the dismissal id of the notification or null if it has not been set.
8379 */
8380 public String getDismissalId() {
8381 return mDismissalId;
8382 }
8383
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008384 /**
8385 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8386 * posted from a phone to provide finer-grained control on what notifications are bridged
8387 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8388 * Features to Notifications</a> for more information.
8389 * @param bridgeTag the bridge tag of the notification.
8390 * @return this object for method chaining
8391 */
8392 public WearableExtender setBridgeTag(String bridgeTag) {
8393 mBridgeTag = bridgeTag;
8394 return this;
8395 }
8396
8397 /**
8398 * Returns the bridge tag of the notification.
8399 * @return the bridge tag or null if not present.
8400 */
8401 public String getBridgeTag() {
8402 return mBridgeTag;
8403 }
8404
Griff Hazen61a9e862014-05-22 16:05:19 -07008405 private void setFlag(int mask, boolean value) {
8406 if (value) {
8407 mFlags |= mask;
8408 } else {
8409 mFlags &= ~mask;
8410 }
8411 }
8412 }
8413
8414 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008415 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8416 * with car extensions:
8417 *
8418 * <ol>
8419 * <li>Create an {@link Notification.Builder}, setting any desired
8420 * properties.
8421 * <li>Create a {@link CarExtender}.
8422 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8423 * {@link CarExtender}.
8424 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8425 * to apply the extensions to a notification.
8426 * </ol>
8427 *
8428 * <pre class="prettyprint">
8429 * Notification notification = new Notification.Builder(context)
8430 * ...
8431 * .extend(new CarExtender()
8432 * .set*(...))
8433 * .build();
8434 * </pre>
8435 *
8436 * <p>Car extensions can be accessed on an existing notification by using the
8437 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8438 * to access values.
8439 */
8440 public static final class CarExtender implements Extender {
8441 private static final String TAG = "CarExtender";
8442
8443 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8444 private static final String EXTRA_LARGE_ICON = "large_icon";
8445 private static final String EXTRA_CONVERSATION = "car_conversation";
8446 private static final String EXTRA_COLOR = "app_color";
8447
8448 private Bitmap mLargeIcon;
8449 private UnreadConversation mUnreadConversation;
8450 private int mColor = Notification.COLOR_DEFAULT;
8451
8452 /**
8453 * Create a {@link CarExtender} with default options.
8454 */
8455 public CarExtender() {
8456 }
8457
8458 /**
8459 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8460 *
8461 * @param notif The notification from which to copy options.
8462 */
8463 public CarExtender(Notification notif) {
8464 Bundle carBundle = notif.extras == null ?
8465 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8466 if (carBundle != null) {
8467 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8468 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8469
8470 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8471 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8472 }
8473 }
8474
8475 /**
8476 * Apply car extensions to a notification that is being built. This is typically called by
8477 * the {@link Notification.Builder#extend(Notification.Extender)}
8478 * method of {@link Notification.Builder}.
8479 */
8480 @Override
8481 public Notification.Builder extend(Notification.Builder builder) {
8482 Bundle carExtensions = new Bundle();
8483
8484 if (mLargeIcon != null) {
8485 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8486 }
8487 if (mColor != Notification.COLOR_DEFAULT) {
8488 carExtensions.putInt(EXTRA_COLOR, mColor);
8489 }
8490
8491 if (mUnreadConversation != null) {
8492 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8493 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8494 }
8495
8496 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8497 return builder;
8498 }
8499
8500 /**
8501 * Sets the accent color to use when Android Auto presents the notification.
8502 *
8503 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8504 * to accent the displayed notification. However, not all colors are acceptable in an
8505 * automotive setting. This method can be used to override the color provided in the
8506 * notification in such a situation.
8507 */
Tor Norbye80756e32015-03-02 09:39:27 -08008508 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008509 mColor = color;
8510 return this;
8511 }
8512
8513 /**
8514 * Gets the accent color.
8515 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008516 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008517 */
Tor Norbye80756e32015-03-02 09:39:27 -08008518 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008519 public int getColor() {
8520 return mColor;
8521 }
8522
8523 /**
8524 * Sets the large icon of the car notification.
8525 *
8526 * If no large icon is set in the extender, Android Auto will display the icon
8527 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8528 *
8529 * @param largeIcon The large icon to use in the car notification.
8530 * @return This object for method chaining.
8531 */
8532 public CarExtender setLargeIcon(Bitmap largeIcon) {
8533 mLargeIcon = largeIcon;
8534 return this;
8535 }
8536
8537 /**
8538 * Gets the large icon used in this car notification, or null if no icon has been set.
8539 *
8540 * @return The large icon for the car notification.
8541 * @see CarExtender#setLargeIcon
8542 */
8543 public Bitmap getLargeIcon() {
8544 return mLargeIcon;
8545 }
8546
8547 /**
8548 * Sets the unread conversation in a message notification.
8549 *
8550 * @param unreadConversation The unread part of the conversation this notification conveys.
8551 * @return This object for method chaining.
8552 */
8553 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8554 mUnreadConversation = unreadConversation;
8555 return this;
8556 }
8557
8558 /**
8559 * Returns the unread conversation conveyed by this notification.
8560 * @see #setUnreadConversation(UnreadConversation)
8561 */
8562 public UnreadConversation getUnreadConversation() {
8563 return mUnreadConversation;
8564 }
8565
8566 /**
8567 * A class which holds the unread messages from a conversation.
8568 */
8569 public static class UnreadConversation {
8570 private static final String KEY_AUTHOR = "author";
8571 private static final String KEY_TEXT = "text";
8572 private static final String KEY_MESSAGES = "messages";
8573 private static final String KEY_REMOTE_INPUT = "remote_input";
8574 private static final String KEY_ON_REPLY = "on_reply";
8575 private static final String KEY_ON_READ = "on_read";
8576 private static final String KEY_PARTICIPANTS = "participants";
8577 private static final String KEY_TIMESTAMP = "timestamp";
8578
8579 private final String[] mMessages;
8580 private final RemoteInput mRemoteInput;
8581 private final PendingIntent mReplyPendingIntent;
8582 private final PendingIntent mReadPendingIntent;
8583 private final String[] mParticipants;
8584 private final long mLatestTimestamp;
8585
8586 UnreadConversation(String[] messages, RemoteInput remoteInput,
8587 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8588 String[] participants, long latestTimestamp) {
8589 mMessages = messages;
8590 mRemoteInput = remoteInput;
8591 mReadPendingIntent = readPendingIntent;
8592 mReplyPendingIntent = replyPendingIntent;
8593 mParticipants = participants;
8594 mLatestTimestamp = latestTimestamp;
8595 }
8596
8597 /**
8598 * Gets the list of messages conveyed by this notification.
8599 */
8600 public String[] getMessages() {
8601 return mMessages;
8602 }
8603
8604 /**
8605 * Gets the remote input that will be used to convey the response to a message list, or
8606 * null if no such remote input exists.
8607 */
8608 public RemoteInput getRemoteInput() {
8609 return mRemoteInput;
8610 }
8611
8612 /**
8613 * Gets the pending intent that will be triggered when the user replies to this
8614 * notification.
8615 */
8616 public PendingIntent getReplyPendingIntent() {
8617 return mReplyPendingIntent;
8618 }
8619
8620 /**
8621 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8622 * in this object's message list.
8623 */
8624 public PendingIntent getReadPendingIntent() {
8625 return mReadPendingIntent;
8626 }
8627
8628 /**
8629 * Gets the participants in the conversation.
8630 */
8631 public String[] getParticipants() {
8632 return mParticipants;
8633 }
8634
8635 /**
8636 * Gets the firs participant in the conversation.
8637 */
8638 public String getParticipant() {
8639 return mParticipants.length > 0 ? mParticipants[0] : null;
8640 }
8641
8642 /**
8643 * Gets the timestamp of the conversation.
8644 */
8645 public long getLatestTimestamp() {
8646 return mLatestTimestamp;
8647 }
8648
8649 Bundle getBundleForUnreadConversation() {
8650 Bundle b = new Bundle();
8651 String author = null;
8652 if (mParticipants != null && mParticipants.length > 1) {
8653 author = mParticipants[0];
8654 }
8655 Parcelable[] messages = new Parcelable[mMessages.length];
8656 for (int i = 0; i < messages.length; i++) {
8657 Bundle m = new Bundle();
8658 m.putString(KEY_TEXT, mMessages[i]);
8659 m.putString(KEY_AUTHOR, author);
8660 messages[i] = m;
8661 }
8662 b.putParcelableArray(KEY_MESSAGES, messages);
8663 if (mRemoteInput != null) {
8664 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8665 }
8666 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8667 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8668 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8669 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8670 return b;
8671 }
8672
8673 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8674 if (b == null) {
8675 return null;
8676 }
8677 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8678 String[] messages = null;
8679 if (parcelableMessages != null) {
8680 String[] tmp = new String[parcelableMessages.length];
8681 boolean success = true;
8682 for (int i = 0; i < tmp.length; i++) {
8683 if (!(parcelableMessages[i] instanceof Bundle)) {
8684 success = false;
8685 break;
8686 }
8687 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8688 if (tmp[i] == null) {
8689 success = false;
8690 break;
8691 }
8692 }
8693 if (success) {
8694 messages = tmp;
8695 } else {
8696 return null;
8697 }
8698 }
8699
8700 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8701 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8702
8703 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8704
8705 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8706 if (participants == null || participants.length != 1) {
8707 return null;
8708 }
8709
8710 return new UnreadConversation(messages,
8711 remoteInput,
8712 onReply,
8713 onRead,
8714 participants, b.getLong(KEY_TIMESTAMP));
8715 }
8716 };
8717
8718 /**
8719 * Builder class for {@link CarExtender.UnreadConversation} objects.
8720 */
8721 public static class Builder {
8722 private final List<String> mMessages = new ArrayList<String>();
8723 private final String mParticipant;
8724 private RemoteInput mRemoteInput;
8725 private PendingIntent mReadPendingIntent;
8726 private PendingIntent mReplyPendingIntent;
8727 private long mLatestTimestamp;
8728
8729 /**
8730 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8731 *
8732 * @param name The name of the other participant in the conversation.
8733 */
8734 public Builder(String name) {
8735 mParticipant = name;
8736 }
8737
8738 /**
8739 * Appends a new unread message to the list of messages for this conversation.
8740 *
8741 * The messages should be added from oldest to newest.
8742 *
8743 * @param message The text of the new unread message.
8744 * @return This object for method chaining.
8745 */
8746 public Builder addMessage(String message) {
8747 mMessages.add(message);
8748 return this;
8749 }
8750
8751 /**
8752 * Sets the pending intent and remote input which will convey the reply to this
8753 * notification.
8754 *
8755 * @param pendingIntent The pending intent which will be triggered on a reply.
8756 * @param remoteInput The remote input parcelable which will carry the reply.
8757 * @return This object for method chaining.
8758 *
8759 * @see CarExtender.UnreadConversation#getRemoteInput
8760 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8761 */
8762 public Builder setReplyAction(
8763 PendingIntent pendingIntent, RemoteInput remoteInput) {
8764 mRemoteInput = remoteInput;
8765 mReplyPendingIntent = pendingIntent;
8766
8767 return this;
8768 }
8769
8770 /**
8771 * Sets the pending intent that will be sent once the messages in this notification
8772 * are read.
8773 *
8774 * @param pendingIntent The pending intent to use.
8775 * @return This object for method chaining.
8776 */
8777 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8778 mReadPendingIntent = pendingIntent;
8779 return this;
8780 }
8781
8782 /**
8783 * Sets the timestamp of the most recent message in an unread conversation.
8784 *
8785 * If a messaging notification has been posted by your application and has not
8786 * yet been cancelled, posting a later notification with the same id and tag
8787 * but without a newer timestamp may result in Android Auto not displaying a
8788 * heads up notification for the later notification.
8789 *
8790 * @param timestamp The timestamp of the most recent message in the conversation.
8791 * @return This object for method chaining.
8792 */
8793 public Builder setLatestTimestamp(long timestamp) {
8794 mLatestTimestamp = timestamp;
8795 return this;
8796 }
8797
8798 /**
8799 * Builds a new unread conversation object.
8800 *
8801 * @return The new unread conversation object.
8802 */
8803 public UnreadConversation build() {
8804 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8805 String[] participants = { mParticipant };
8806 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8807 mReadPendingIntent, participants, mLatestTimestamp);
8808 }
8809 }
8810 }
8811
8812 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008813 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8814 * with a TV extension:
8815 *
8816 * <ol>
8817 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8818 * <li>Create a {@link TvExtender}.
8819 * <li>Set TV-specific properties using the {@code set} methods of
8820 * {@link TvExtender}.
8821 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8822 * to apply the extension to a notification.
8823 * </ol>
8824 *
8825 * <pre class="prettyprint">
8826 * Notification notification = new Notification.Builder(context)
8827 * ...
8828 * .extend(new TvExtender()
8829 * .set*(...))
8830 * .build();
8831 * </pre>
8832 *
8833 * <p>TV extensions can be accessed on an existing notification by using the
8834 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8835 * to access values.
8836 *
8837 * @hide
8838 */
8839 @SystemApi
8840 public static final class TvExtender implements Extender {
8841 private static final String TAG = "TvExtender";
8842
8843 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8844 private static final String EXTRA_FLAGS = "flags";
8845 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8846 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008847 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008848
8849 // Flags bitwise-ored to mFlags
8850 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8851
8852 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008853 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008854 private PendingIntent mContentIntent;
8855 private PendingIntent mDeleteIntent;
8856
8857 /**
8858 * Create a {@link TvExtender} with default options.
8859 */
8860 public TvExtender() {
8861 mFlags = FLAG_AVAILABLE_ON_TV;
8862 }
8863
8864 /**
8865 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8866 *
8867 * @param notif The notification from which to copy options.
8868 */
8869 public TvExtender(Notification notif) {
8870 Bundle bundle = notif.extras == null ?
8871 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8872 if (bundle != null) {
8873 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008874 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008875 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8876 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8877 }
8878 }
8879
8880 /**
8881 * Apply a TV extension to a notification that is being built. This is typically called by
8882 * the {@link Notification.Builder#extend(Notification.Extender)}
8883 * method of {@link Notification.Builder}.
8884 */
8885 @Override
8886 public Notification.Builder extend(Notification.Builder builder) {
8887 Bundle bundle = new Bundle();
8888
8889 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008890 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008891 if (mContentIntent != null) {
8892 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8893 }
8894
8895 if (mDeleteIntent != null) {
8896 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8897 }
8898
8899 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8900 return builder;
8901 }
8902
8903 /**
8904 * Returns true if this notification should be shown on TV. This method return true
8905 * if the notification was extended with a TvExtender.
8906 */
8907 public boolean isAvailableOnTv() {
8908 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8909 }
8910
8911 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008912 * Specifies the channel the notification should be delivered on when shown on TV.
8913 * It can be different from the channel that the notification is delivered to when
8914 * posting on a non-TV device.
8915 */
8916 public TvExtender setChannel(String channelId) {
8917 mChannelId = channelId;
8918 return this;
8919 }
8920
8921 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008922 * Specifies the channel the notification should be delivered on when shown on TV.
8923 * It can be different from the channel that the notification is delivered to when
8924 * posting on a non-TV device.
8925 */
8926 public TvExtender setChannelId(String channelId) {
8927 mChannelId = channelId;
8928 return this;
8929 }
8930
Jeff Sharkey000ce802017-04-29 13:13:27 -06008931 /** @removed */
8932 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008933 public String getChannel() {
8934 return mChannelId;
8935 }
8936
8937 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008938 * Returns the id of the channel this notification posts to on TV.
8939 */
8940 public String getChannelId() {
8941 return mChannelId;
8942 }
8943
8944 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008945 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8946 * If provided, it is used instead of the content intent specified
8947 * at the level of Notification.
8948 */
8949 public TvExtender setContentIntent(PendingIntent intent) {
8950 mContentIntent = intent;
8951 return this;
8952 }
8953
8954 /**
8955 * Returns the TV-specific content intent. If this method returns null, the
8956 * main content intent on the notification should be used.
8957 *
8958 * @see {@link Notification#contentIntent}
8959 */
8960 public PendingIntent getContentIntent() {
8961 return mContentIntent;
8962 }
8963
8964 /**
8965 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8966 * by the user on TV. If provided, it is used instead of the delete intent specified
8967 * at the level of Notification.
8968 */
8969 public TvExtender setDeleteIntent(PendingIntent intent) {
8970 mDeleteIntent = intent;
8971 return this;
8972 }
8973
8974 /**
8975 * Returns the TV-specific delete intent. If this method returns null, the
8976 * main delete intent on the notification should be used.
8977 *
8978 * @see {@link Notification#deleteIntent}
8979 */
8980 public PendingIntent getDeleteIntent() {
8981 return mDeleteIntent;
8982 }
8983 }
8984
8985 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008986 * Get an array of Notification objects from a parcelable array bundle field.
8987 * Update the bundle to have a typed array so fetches in the future don't need
8988 * to do an array copy.
8989 */
8990 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8991 Parcelable[] array = bundle.getParcelableArray(key);
8992 if (array instanceof Notification[] || array == null) {
8993 return (Notification[]) array;
8994 }
8995 Notification[] typedArray = Arrays.copyOf(array, array.length,
8996 Notification[].class);
8997 bundle.putParcelableArray(key, typedArray);
8998 return typedArray;
8999 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009000
9001 private static class BuilderRemoteViews extends RemoteViews {
9002 public BuilderRemoteViews(Parcel parcel) {
9003 super(parcel);
9004 }
9005
Kenny Guy77320062014-08-27 21:37:15 +01009006 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9007 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009008 }
9009
9010 @Override
9011 public BuilderRemoteViews clone() {
9012 Parcel p = Parcel.obtain();
9013 writeToParcel(p, 0);
9014 p.setDataPosition(0);
9015 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9016 p.recycle();
9017 return brv;
9018 }
9019 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009020
9021 private static class StandardTemplateParams {
9022 boolean hasProgress = true;
9023 boolean ambient = false;
9024 CharSequence title;
9025 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009026 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009027 boolean hideLargeIcon;
9028 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009029
9030 final StandardTemplateParams reset() {
9031 hasProgress = true;
9032 ambient = false;
9033 title = null;
9034 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009035 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009036 return this;
9037 }
9038
9039 final StandardTemplateParams hasProgress(boolean hasProgress) {
9040 this.hasProgress = hasProgress;
9041 return this;
9042 }
9043
9044 final StandardTemplateParams title(CharSequence title) {
9045 this.title = title;
9046 return this;
9047 }
9048
9049 final StandardTemplateParams text(CharSequence text) {
9050 this.text = text;
9051 return this;
9052 }
9053
Selim Cinekafeed292017-12-12 17:32:44 -08009054 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9055 this.headerTextSecondary = text;
9056 return this;
9057 }
9058
Selim Cinek88188f22017-09-19 16:46:56 -07009059 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9060 this.alwaysShowReply = alwaysShowReply;
9061 return this;
9062 }
9063
9064 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9065 this.hideLargeIcon = hideLargeIcon;
9066 return this;
9067 }
9068
Adrian Roos70d7aa32017-01-11 15:39:06 -08009069 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009070 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009071 this.ambient = ambient;
9072 return this;
9073 }
9074
9075 final StandardTemplateParams fillTextsFrom(Builder b) {
9076 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009077 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9078
9079 // Big text notifications should contain their content when viewed in ambient mode.
9080 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9081 if (!ambient || TextUtils.isEmpty(text)) {
9082 text = extras.getCharSequence(EXTRA_TEXT);
9083 }
9084 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009085 return this;
9086 }
9087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088}