blob: 42c1347e265dc384e3e11145ad5b3f9d24309127 [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 */
580 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
581 @Retention(RetentionPolicy.SOURCE)
582 public @interface Priority {}
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 * Default notification {@link #priority}. If your application does not prioritize its own
586 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500587 *
588 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500590 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 public static final int PRIORITY_DEFAULT = 0;
592
593 /**
594 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
595 * items smaller, or at a different position in the list, compared with your app's
596 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500597 *
598 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500600 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601 public static final int PRIORITY_LOW = -1;
602
603 /**
604 * Lowest {@link #priority}; these items might not be shown to the user except under special
605 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 *
607 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500609 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 public static final int PRIORITY_MIN = -2;
611
612 /**
613 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
614 * show these items larger, or at a different position in notification lists, compared with
615 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500616 *
617 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500619 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 public static final int PRIORITY_HIGH = 1;
621
622 /**
623 * Highest {@link #priority}, for your application's most important items that require the
624 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 *
626 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500628 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 public static final int PRIORITY_MAX = 2;
630
631 /**
632 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800633 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 * Priority is an indication of how much of the user's valuable attention should be consumed by
635 * this notification. Low-priority notifications may be hidden from the user in certain
636 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500637 * system will make a determination about how to interpret this priority when presenting
638 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400639 *
640 * <p>
641 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
642 * as a heads-up notification.
643 * </p>
644 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700647 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500648 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500649 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800650
Dan Sandler26e81cf2014-05-06 10:01:27 -0400651 /**
652 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
653 * to be applied by the standard Style templates when presenting this notification.
654 *
655 * The current template design constructs a colorful header image by overlaying the
656 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
657 * ignored.
658 */
Tor Norbye80756e32015-03-02 09:39:27 -0800659 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400660 public int color = COLOR_DEFAULT;
661
662 /**
663 * Special value of {@link #color} telling the system not to decorate this notification with
664 * any special color but instead use default colors when presenting this notification.
665 */
Tor Norbye80756e32015-03-02 09:39:27 -0800666 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400667 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600668
669 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800670 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800671 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800672 */
673 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800674 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800675
676 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700677 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
678 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600679 * lockscreen).
680 *
681 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
682 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
683 * shown in all situations, but the contents are only available if the device is unlocked for
684 * the appropriate user.
685 *
686 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
687 * can be read even in an "insecure" context (that is, above a secure lockscreen).
688 * To modify the public version of this notification—for example, to redact some portions—see
689 * {@link Builder#setPublicVersion(Notification)}.
690 *
691 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
692 * and ticker until the user has bypassed the lockscreen.
693 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600694 public @Visibility int visibility;
695
696 /** @hide */
697 @IntDef(prefix = { "VISIBILITY_" }, value = {
698 VISIBILITY_PUBLIC,
699 VISIBILITY_PRIVATE,
700 VISIBILITY_SECRET,
701 })
702 @Retention(RetentionPolicy.SOURCE)
703 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600704
Griff Hazenfc3922d2014-08-20 11:56:44 -0700705 /**
706 * Notification visibility: Show this notification in its entirety on all lockscreens.
707 *
708 * {@see #visibility}
709 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700711
712 /**
713 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
714 * private information on secure lockscreens.
715 *
716 * {@see #visibility}
717 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600718 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700719
720 /**
721 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
722 *
723 * {@see #visibility}
724 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600725 public static final int VISIBILITY_SECRET = -1;
726
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736
737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500741
742 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400743 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500744 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400745 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500746
747 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400748 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500749 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400750 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500751
752 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400753 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500754 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400755 public static final String CATEGORY_ALARM = "alarm";
756
757 /**
758 * Notification category: progress of a long-running background operation.
759 */
760 public static final String CATEGORY_PROGRESS = "progress";
761
762 /**
763 * Notification category: social network or sharing update.
764 */
765 public static final String CATEGORY_SOCIAL = "social";
766
767 /**
768 * Notification category: error in background operation or authentication status.
769 */
770 public static final String CATEGORY_ERROR = "err";
771
772 /**
773 * Notification category: media transport control for playback.
774 */
775 public static final String CATEGORY_TRANSPORT = "transport";
776
777 /**
778 * Notification category: system or device status update. Reserved for system use.
779 */
780 public static final String CATEGORY_SYSTEM = "sys";
781
782 /**
783 * Notification category: indication of running background service.
784 */
785 public static final String CATEGORY_SERVICE = "service";
786
787 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400788 * Notification category: a specific, timely recommendation for a single thing.
789 * For example, a news app might want to recommend a news story it believes the user will
790 * want to read next.
791 */
792 public static final String CATEGORY_RECOMMENDATION = "recommendation";
793
794 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 * Notification category: ongoing information about device or contextual status.
796 */
797 public static final String CATEGORY_STATUS = "status";
798
799 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400800 * Notification category: user-scheduled reminder.
801 */
802 public static final String CATEGORY_REMINDER = "reminder";
803
804 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400805 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
806 * that best describes this Notification. May be used by the system for ranking and filtering.
807 */
808 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500809
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700810 private String mGroupKey;
811
812 /**
813 * Get the key used to group this notification into a cluster or stack
814 * with other notifications on devices which support such rendering.
815 */
816 public String getGroup() {
817 return mGroupKey;
818 }
819
820 private String mSortKey;
821
822 /**
823 * Get a sort key that orders this notification among other notifications from the
824 * same package. This can be useful if an external sort was already applied and an app
825 * would like to preserve this. Notifications will be sorted lexicographically using this
826 * value, although providing different priorities in addition to providing sort key may
827 * cause this value to be ignored.
828 *
829 * <p>This sort key can also be used to order members of a notification group. See
830 * {@link Builder#setGroup}.
831 *
832 * @see String#compareTo(String)
833 */
834 public String getSortKey() {
835 return mSortKey;
836 }
837
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500838 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400839 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400840 * <p>
841 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
842 * APIs, and are intended to be used by
843 * {@link android.service.notification.NotificationListenerService} implementations to extract
844 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500845 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400846 public Bundle extras = new Bundle();
847
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400848 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700849 * All pending intents in the notification as the system needs to be able to access them but
850 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700851 * custom parcelable objects.
852 *
853 * @hide
854 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700855 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700856
857 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700858 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
859 * pending intents inside of it, so only those will get the behavior.
860 *
861 * @hide
862 */
Adrian Roosfb921842017-10-26 14:49:56 +0200863 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700864
865 /**
866 * Must be set by a process to start associating tokens with Notification objects
867 * coming in to it. This is set by NotificationManagerService.
868 *
869 * @hide
870 */
871 static public IBinder processWhitelistToken;
872
873 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874 * {@link #extras} key: this is the title of the notification,
875 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
876 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500877 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
880 * {@link #extras} key: this is the title of the notification when shown in expanded form,
881 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
882 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400883 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: this is the main text payload, as supplied to
887 * {@link Builder#setContentText(CharSequence)}.
888 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500889 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890
891 /**
892 * {@link #extras} key: this is a third line of text, as supplied to
893 * {@link Builder#setSubText(CharSequence)}.
894 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400895 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896
897 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800898 * {@link #extras} key: this is the remote input history, as supplied to
899 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700900 *
901 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
902 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
903 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
904 * notifications once the other party has responded).
905 *
906 * The extra with this key is of type CharSequence[] and contains the most recent entry at
907 * the 0 index, the second most recent at the 1 index, etc.
908 *
909 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800910 */
911 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
912
913 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 * {@link #extras} key: this is a small piece of additional text as supplied to
915 * {@link Builder#setContentInfo(CharSequence)}.
916 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400917 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400918
919 /**
920 * {@link #extras} key: this is a line of summary information intended to be shown
921 * alongside expanded notifications, as supplied to (e.g.)
922 * {@link BigTextStyle#setSummaryText(CharSequence)}.
923 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400924 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400925
926 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200927 * {@link #extras} key: this is the longer text shown in the big form of a
928 * {@link BigTextStyle} notification, as supplied to
929 * {@link BigTextStyle#bigText(CharSequence)}.
930 */
931 public static final String EXTRA_BIG_TEXT = "android.bigText";
932
933 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
935 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400936 *
937 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400938 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400939 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500940 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
943 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
944 * notification payload, as
945 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400946 *
947 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400949 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400950 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is a bitmap to be used instead of the one from
954 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
955 * shown in its expanded form, as supplied to
956 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
957 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400958 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400959
960 /**
961 * {@link #extras} key: this is the progress value supplied to
962 * {@link Builder#setProgress(int, int, boolean)}.
963 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400964 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965
966 /**
967 * {@link #extras} key: this is the maximum 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_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971
972 /**
973 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
974 * {@link Builder#setProgress(int, int, boolean)}.
975 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400976 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977
978 /**
979 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
980 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
981 * {@link Builder#setUsesChronometer(boolean)}.
982 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400983 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400984
985 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800986 * {@link #extras} key: whether the chronometer set on the notification should count down
987 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700988 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800989 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700990 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800991
992 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993 * {@link #extras} key: whether {@link #when} should be shown,
994 * as supplied to {@link Builder#setShowWhen(boolean)}.
995 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400996 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400997
998 /**
999 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1000 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1001 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001002 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003
1004 /**
1005 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1006 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1007 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001008 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001009
1010 /**
1011 * {@link #extras} key: A string representing the name of the specific
1012 * {@link android.app.Notification.Style} used to create this notification.
1013 */
Chris Wren91ad5632013-06-05 15:05:57 -04001014 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001015
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001016 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001017 * {@link #extras} key: A String array containing the people that this notification relates to,
1018 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001020 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001021
1022 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001023 * Allow certain system-generated notifications to appear before the device is provisioned.
1024 * Only available to notifications coming from the android package.
1025 * @hide
1026 */
Maurice Lam96c10032017-03-29 15:42:38 -07001027 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001028 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001029 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1030
1031 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001032 * {@link #extras} key: A
1033 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1034 * in the background when the notification is selected. The URI must point to an image stream
1035 * suitable for passing into
1036 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1037 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1038 * URI used for this purpose must require no permissions to read the image data.
1039 */
1040 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1041
1042 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001043 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001044 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001045 * {@link android.app.Notification.MediaStyle} notification.
1046 */
1047 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1048
1049 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001050 * {@link #extras} key: the indices of actions to be shown in the compact view,
1051 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1052 */
1053 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1054
Christoph Studer943aa672014-08-03 20:31:16 +02001055 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001056 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1057 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001058 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1059 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001060 */
1061 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1062
1063 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001064 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001065 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001066 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001067 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001068
1069 /**
1070 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1071 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001072 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1073 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001074 */
1075 public static final String EXTRA_MESSAGES = "android.messages";
1076
1077 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001078 * {@link #extras} key: an array of
1079 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1080 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1081 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1082 * array of bundles.
1083 */
1084 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1085
1086 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001087 * {@link #extras} key: whether the notification should be colorized as
1088 * supplied to {@link Builder#setColorized(boolean)}}.
1089 */
1090 public static final String EXTRA_COLORIZED = "android.colorized";
1091
1092 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001093 * @hide
1094 */
1095 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1096
Selim Cinek247fa012016-02-18 09:50:48 -08001097 /**
1098 * @hide
1099 */
1100 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1101
Shane Brennan472a3b32016-12-12 15:28:10 -08001102 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001103 * @hide
1104 */
1105 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1106
1107 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001108 * {@link #extras} key: the audio contents of this notification.
1109 *
1110 * This is for use when rendering the notification on an audio-focused interface;
1111 * the audio contents are a complete sound sample that contains the contents/body of the
1112 * notification. This may be used in substitute of a Text-to-Speech reading of the
1113 * notification. For example if the notification represents a voice message this should point
1114 * to the audio of that message.
1115 *
1116 * The data stored under this key should be a String representation of a Uri that contains the
1117 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1118 *
1119 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1120 * has a field for holding data URI. That field can be used for audio.
1121 * See {@code Message#setData}.
1122 *
1123 * Example usage:
1124 * <pre>
1125 * {@code
1126 * Notification.Builder myBuilder = (build your Notification as normal);
1127 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1128 * }
1129 * </pre>
1130 */
1131 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1132
Dan Sandler80eaa592016-04-14 23:34:54 -04001133 /** @hide */
1134 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001135 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001136 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1137
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001138 /**
1139 * This is set on the notification shown by the activity manager about all apps
1140 * running in the background. It indicates that the notification should be shown
1141 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1142 * notification currently visible to the user. This is a string array of all
1143 * package names of the apps.
1144 * @hide
1145 */
1146 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1147
Dan Sandlerd63f9322015-05-06 15:18:49 -04001148 private Icon mSmallIcon;
1149 private Icon mLargeIcon;
1150
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001151 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001152 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001153
Julia Reynolds13d898c2017-02-02 12:22:05 -05001154 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001155 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001156
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001157 /** @hide */
1158 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1159 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1160 })
1161 @Retention(RetentionPolicy.SOURCE)
1162 public @interface GroupAlertBehavior {}
1163
1164 /**
1165 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1166 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1167 * notification will not be muted when it is in a group.
1168 */
1169 public static final int GROUP_ALERT_ALL = 0;
1170
1171 /**
1172 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1173 * notification in a group should be silenced (no sound or vibration) even if they are posted
1174 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001175 * mute this notification if this notification is a group child. This must be applied to all
1176 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001177 *
1178 * <p> For example, you might want to use this constant if you post a number of children
1179 * notifications at once (say, after a periodic sync), and only need to notify the user
1180 * audibly once.
1181 */
1182 public static final int GROUP_ALERT_SUMMARY = 1;
1183
1184 /**
1185 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1186 * notification in a group should be silenced (no sound or vibration) even if they are
1187 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1188 * to mute this notification if this notification is a group summary.
1189 *
1190 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001191 * in your group have content and the summary is only used to visually group notifications
1192 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001193 */
1194 public static final int GROUP_ALERT_CHILDREN = 2;
1195
Julia Reynolds2f431e22017-06-07 14:12:09 +00001196 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001197
Julia Reynolds13d898c2017-02-02 12:22:05 -05001198 /**
1199 * If this notification is being shown as a badge, always show as a number.
1200 */
1201 public static final int BADGE_ICON_NONE = 0;
1202
1203 /**
1204 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1205 * represent this notification.
1206 */
1207 public static final int BADGE_ICON_SMALL = 1;
1208
1209 /**
1210 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1211 * represent this notification.
1212 */
1213 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001214 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001215
Chris Wren51c75102013-07-16 20:49:17 -04001216 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001217 * Structure to encapsulate a named action that can be shown as part of this notification.
1218 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1219 * selected by the user.
1220 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001221 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1222 * or {@link Notification.Builder#addAction(Notification.Action)}
1223 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001224 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001225 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 /**
1227 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1228 * {@link RemoteInput}s.
1229 *
1230 * This is intended for {@link RemoteInput}s that only accept data, meaning
1231 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1232 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1233 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1234 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1235 *
1236 * You can test if a RemoteInput matches these constraints using
1237 * {@link RemoteInput#isDataOnly}.
1238 */
1239 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1240
Griff Hazen959591e2014-05-15 22:26:18 -07001241 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001242 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001243 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001244 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001245
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001246 /**
1247 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001248 *
1249 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001250 */
Dan Sandler86647982015-05-13 23:41:13 -04001251 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001252 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001253
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001254 /**
1255 * Title of the action.
1256 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001257 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001258
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001259 /**
1260 * Intent to send when the user invokes this action. May be null, in which case the action
1261 * may be rendered in a disabled presentation by the system UI.
1262 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001264
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001265 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001266 if (in.readInt() != 0) {
1267 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001268 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1269 icon = mIcon.getResId();
1270 }
Dan Sandler86647982015-05-13 23:41:13 -04001271 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001272 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1273 if (in.readInt() == 1) {
1274 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1275 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001276 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001277 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001278 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001279 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001280
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001281 /**
Dan Sandler86647982015-05-13 23:41:13 -04001282 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001283 */
Dan Sandler86647982015-05-13 23:41:13 -04001284 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001285 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001286 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001287 }
1288
Adrian Roos7af53622016-10-12 13:44:05 -07001289 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001290 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001291 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001292 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001293 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1294 this.icon = icon.getResId();
1295 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001296 this.title = title;
1297 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001298 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001299 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001300 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001301 }
1302
1303 /**
Dan Sandler86647982015-05-13 23:41:13 -04001304 * Return an icon representing the action.
1305 */
1306 public Icon getIcon() {
1307 if (mIcon == null && icon != 0) {
1308 // you snuck an icon in here without using the builder; let's try to keep it
1309 mIcon = Icon.createWithResource("", icon);
1310 }
1311 return mIcon;
1312 }
1313
1314 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001315 * Get additional metadata carried around with this Action.
1316 */
1317 public Bundle getExtras() {
1318 return mExtras;
1319 }
1320
1321 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001322 * Return whether the platform should automatically generate possible replies for this
1323 * {@link Action}
1324 */
1325 public boolean getAllowGeneratedReplies() {
1326 return mAllowGeneratedReplies;
1327 }
1328
1329 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001330 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001331 * May return null if no remote inputs were added. Only returns inputs which accept
1332 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001333 */
1334 public RemoteInput[] getRemoteInputs() {
1335 return mRemoteInputs;
1336 }
1337
1338 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001339 * Get the list of inputs to be collected from the user that ONLY accept data when this
1340 * action is sent. These remote inputs are guaranteed to return true on a call to
1341 * {@link RemoteInput#isDataOnly}.
1342 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001343 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001344 *
1345 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1346 * of non-textual RemoteInputs do not access these remote inputs.
1347 */
1348 public RemoteInput[] getDataOnlyRemoteInputs() {
1349 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1350 }
1351
1352 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001353 * Builder class for {@link Action} objects.
1354 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001355 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001356 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001357 private final CharSequence mTitle;
1358 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001359 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001360 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001361 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001362
1363 /**
1364 * Construct a new builder for {@link Action} object.
1365 * @param icon icon to show for this action
1366 * @param title the title of the action
1367 * @param intent the {@link PendingIntent} to fire when users trigger this action
1368 */
Dan Sandler86647982015-05-13 23:41:13 -04001369 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001370 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001371 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001372 }
1373
1374 /**
1375 * Construct a new builder for {@link Action} object.
1376 * @param icon icon to show for this action
1377 * @param title the title of the action
1378 * @param intent the {@link PendingIntent} to fire when users trigger this action
1379 */
1380 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001381 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001382 }
1383
1384 /**
1385 * Construct a new builder for {@link Action} object using the fields from an
1386 * {@link Action}.
1387 * @param action the action to read fields from.
1388 */
1389 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001390 this(action.getIcon(), action.title, action.actionIntent,
1391 new Bundle(action.mExtras), action.getRemoteInputs(),
1392 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001393 }
1394
Dan Sandler86647982015-05-13 23:41:13 -04001395 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001396 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001397 mIcon = icon;
1398 mTitle = title;
1399 mIntent = intent;
1400 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001401 if (remoteInputs != null) {
1402 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1403 Collections.addAll(mRemoteInputs, remoteInputs);
1404 }
Adrian Roos7af53622016-10-12 13:44:05 -07001405 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001406 }
1407
1408 /**
1409 * Merge additional metadata into this builder.
1410 *
1411 * <p>Values within the Bundle will replace existing extras values in this Builder.
1412 *
1413 * @see Notification.Action#extras
1414 */
1415 public Builder addExtras(Bundle extras) {
1416 if (extras != null) {
1417 mExtras.putAll(extras);
1418 }
1419 return this;
1420 }
1421
1422 /**
1423 * Get the metadata Bundle used by this Builder.
1424 *
1425 * <p>The returned Bundle is shared with this Builder.
1426 */
1427 public Bundle getExtras() {
1428 return mExtras;
1429 }
1430
1431 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001432 * Add an input to be collected from the user when this action is sent.
1433 * Response values can be retrieved from the fired intent by using the
1434 * {@link RemoteInput#getResultsFromIntent} function.
1435 * @param remoteInput a {@link RemoteInput} to add to the action
1436 * @return this object for method chaining
1437 */
1438 public Builder addRemoteInput(RemoteInput remoteInput) {
1439 if (mRemoteInputs == null) {
1440 mRemoteInputs = new ArrayList<RemoteInput>();
1441 }
1442 mRemoteInputs.add(remoteInput);
1443 return this;
1444 }
1445
1446 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001447 * Set whether the platform should automatically generate possible replies to add to
1448 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1449 * {@link RemoteInput}, this has no effect.
1450 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1451 * otherwise
1452 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001453 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001454 */
1455 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1456 mAllowGeneratedReplies = allowGeneratedReplies;
1457 return this;
1458 }
1459
1460 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001461 * Apply an extender to this action builder. Extenders may be used to add
1462 * metadata or change options on this builder.
1463 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001464 public Builder extend(Extender extender) {
1465 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001466 return this;
1467 }
1468
1469 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001470 * Combine all of the options that have been set and return a new {@link Action}
1471 * object.
1472 * @return the built action
1473 */
1474 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001475 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1476 RemoteInput[] previousDataInputs =
1477 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001478 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001479 for (RemoteInput input : previousDataInputs) {
1480 dataOnlyInputs.add(input);
1481 }
1482 }
1483 List<RemoteInput> textInputs = new ArrayList<>();
1484 if (mRemoteInputs != null) {
1485 for (RemoteInput input : mRemoteInputs) {
1486 if (input.isDataOnly()) {
1487 dataOnlyInputs.add(input);
1488 } else {
1489 textInputs.add(input);
1490 }
1491 }
1492 }
1493 if (!dataOnlyInputs.isEmpty()) {
1494 RemoteInput[] dataInputsArr =
1495 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1496 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1497 }
1498 RemoteInput[] textInputsArr = textInputs.isEmpty()
1499 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1500 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001501 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001502 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001503 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001504
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001505 @Override
1506 public Action clone() {
1507 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001508 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001509 title,
1510 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001511 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001512 getRemoteInputs(),
1513 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001514 }
1515 @Override
1516 public int describeContents() {
1517 return 0;
1518 }
1519 @Override
1520 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001521 final Icon ic = getIcon();
1522 if (ic != null) {
1523 out.writeInt(1);
1524 ic.writeToParcel(out, 0);
1525 } else {
1526 out.writeInt(0);
1527 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001528 TextUtils.writeToParcel(title, out, flags);
1529 if (actionIntent != null) {
1530 out.writeInt(1);
1531 actionIntent.writeToParcel(out, flags);
1532 } else {
1533 out.writeInt(0);
1534 }
Griff Hazen959591e2014-05-15 22:26:18 -07001535 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001536 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001537 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001538 }
Griff Hazen959591e2014-05-15 22:26:18 -07001539 public static final Parcelable.Creator<Action> CREATOR =
1540 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001541 public Action createFromParcel(Parcel in) {
1542 return new Action(in);
1543 }
1544 public Action[] newArray(int size) {
1545 return new Action[size];
1546 }
1547 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001548
1549 /**
1550 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1551 * metadata or change options on an action builder.
1552 */
1553 public interface Extender {
1554 /**
1555 * Apply this extender to a notification action builder.
1556 * @param builder the builder to be modified.
1557 * @return the build object for chaining.
1558 */
1559 public Builder extend(Builder builder);
1560 }
1561
1562 /**
1563 * Wearable extender for notification actions. To add extensions to an action,
1564 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1565 * the {@code WearableExtender()} constructor and apply it to a
1566 * {@link android.app.Notification.Action.Builder} using
1567 * {@link android.app.Notification.Action.Builder#extend}.
1568 *
1569 * <pre class="prettyprint">
1570 * Notification.Action action = new Notification.Action.Builder(
1571 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001572 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001573 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001574 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001575 */
1576 public static final class WearableExtender implements Extender {
1577 /** Notification action extra which contains wearable extensions */
1578 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1579
Pete Gastaf6781d2014-10-07 15:17:05 -04001580 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001581 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001582 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1583 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1584 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001585
1586 // Flags bitwise-ored to mFlags
1587 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001588 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001589 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001590
1591 // Default value for flags integer
1592 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1593
1594 private int mFlags = DEFAULT_FLAGS;
1595
Pete Gastaf6781d2014-10-07 15:17:05 -04001596 private CharSequence mInProgressLabel;
1597 private CharSequence mConfirmLabel;
1598 private CharSequence mCancelLabel;
1599
Griff Hazen61a9e862014-05-22 16:05:19 -07001600 /**
1601 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1602 * options.
1603 */
1604 public WearableExtender() {
1605 }
1606
1607 /**
1608 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1609 * wearable options present in an existing notification action.
1610 * @param action the notification action to inspect.
1611 */
1612 public WearableExtender(Action action) {
1613 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1614 if (wearableBundle != null) {
1615 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001616 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1617 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1618 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001619 }
1620 }
1621
1622 /**
1623 * Apply wearable extensions to a notification action that is being built. This is
1624 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1625 * method of {@link android.app.Notification.Action.Builder}.
1626 */
1627 @Override
1628 public Action.Builder extend(Action.Builder builder) {
1629 Bundle wearableBundle = new Bundle();
1630
1631 if (mFlags != DEFAULT_FLAGS) {
1632 wearableBundle.putInt(KEY_FLAGS, mFlags);
1633 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001634 if (mInProgressLabel != null) {
1635 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1636 }
1637 if (mConfirmLabel != null) {
1638 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1639 }
1640 if (mCancelLabel != null) {
1641 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1642 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001643
1644 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1645 return builder;
1646 }
1647
1648 @Override
1649 public WearableExtender clone() {
1650 WearableExtender that = new WearableExtender();
1651 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001652 that.mInProgressLabel = this.mInProgressLabel;
1653 that.mConfirmLabel = this.mConfirmLabel;
1654 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001655 return that;
1656 }
1657
1658 /**
1659 * Set whether this action is available when the wearable device is not connected to
1660 * a companion device. The user can still trigger this action when the wearable device is
1661 * offline, but a visual hint will indicate that the action may not be available.
1662 * Defaults to true.
1663 */
1664 public WearableExtender setAvailableOffline(boolean availableOffline) {
1665 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1666 return this;
1667 }
1668
1669 /**
1670 * Get whether this action is available when the wearable device is not connected to
1671 * a companion device. The user can still trigger this action when the wearable device is
1672 * offline, but a visual hint will indicate that the action may not be available.
1673 * Defaults to true.
1674 */
1675 public boolean isAvailableOffline() {
1676 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1677 }
1678
1679 private void setFlag(int mask, boolean value) {
1680 if (value) {
1681 mFlags |= mask;
1682 } else {
1683 mFlags &= ~mask;
1684 }
1685 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001686
1687 /**
1688 * Set a label to display while the wearable is preparing to automatically execute the
1689 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1690 *
1691 * @param label the label to display while the action is being prepared to execute
1692 * @return this object for method chaining
1693 */
1694 public WearableExtender setInProgressLabel(CharSequence label) {
1695 mInProgressLabel = label;
1696 return this;
1697 }
1698
1699 /**
1700 * Get the label to display while the wearable is preparing to automatically execute
1701 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1702 *
1703 * @return the label to display while the action is being prepared to execute
1704 */
1705 public CharSequence getInProgressLabel() {
1706 return mInProgressLabel;
1707 }
1708
1709 /**
1710 * Set a label to display to confirm that the action should be executed.
1711 * This is usually an imperative verb like "Send".
1712 *
1713 * @param label the label to confirm the action should be executed
1714 * @return this object for method chaining
1715 */
1716 public WearableExtender setConfirmLabel(CharSequence label) {
1717 mConfirmLabel = label;
1718 return this;
1719 }
1720
1721 /**
1722 * Get the label to display to confirm that the action should be executed.
1723 * This is usually an imperative verb like "Send".
1724 *
1725 * @return the label to confirm the action should be executed
1726 */
1727 public CharSequence getConfirmLabel() {
1728 return mConfirmLabel;
1729 }
1730
1731 /**
1732 * Set a label to display to cancel the action.
1733 * This is usually an imperative verb, like "Cancel".
1734 *
1735 * @param label the label to display to cancel the action
1736 * @return this object for method chaining
1737 */
1738 public WearableExtender setCancelLabel(CharSequence label) {
1739 mCancelLabel = label;
1740 return this;
1741 }
1742
1743 /**
1744 * Get the label to display to cancel the action.
1745 * This is usually an imperative verb like "Cancel".
1746 *
1747 * @return the label to display to cancel the action
1748 */
1749 public CharSequence getCancelLabel() {
1750 return mCancelLabel;
1751 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001752
1753 /**
1754 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1755 * platform that it can generate the appropriate transitions.
1756 * @param hintLaunchesActivity {@code true} if the content intent will launch
1757 * an activity and transitions should be generated, false otherwise.
1758 * @return this object for method chaining
1759 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001760 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001761 boolean hintLaunchesActivity) {
1762 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1763 return this;
1764 }
1765
1766 /**
1767 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1768 * platform that it can generate the appropriate transitions
1769 * @return {@code true} if the content intent will launch an activity and transitions
1770 * should be generated, false otherwise. The default value is {@code false} if this was
1771 * never set.
1772 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001773 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001774 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1775 }
Alex Hills9f087612016-06-07 09:08:59 -04001776
1777 /**
1778 * Set a hint that this Action should be displayed inline.
1779 *
1780 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1781 * otherwise
1782 * @return this object for method chaining
1783 */
1784 public WearableExtender setHintDisplayActionInline(
1785 boolean hintDisplayInline) {
1786 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1787 return this;
1788 }
1789
1790 /**
1791 * Get a hint that this Action should be displayed inline.
1792 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001793 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001794 * otherwise. The default value is {@code false} if this was never set.
1795 */
1796 public boolean getHintDisplayActionInline() {
1797 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1798 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001799 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001800 }
1801
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001802 /**
1803 * Array of all {@link Action} structures attached to this notification by
1804 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1805 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1806 * interface for invoking actions.
1807 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001808 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001809
1810 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001811 * Replacement version of this notification whose content will be shown
1812 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1813 * and {@link #VISIBILITY_PUBLIC}.
1814 */
1815 public Notification publicVersion;
1816
1817 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001818 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001819 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 */
1821 public Notification()
1822 {
1823 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001824 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001825 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 }
1827
1828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 * @hide
1830 */
1831 public Notification(Context context, int icon, CharSequence tickerText, long when,
1832 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1833 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001834 new Builder(context)
1835 .setWhen(when)
1836 .setSmallIcon(icon)
1837 .setTicker(tickerText)
1838 .setContentTitle(contentTitle)
1839 .setContentText(contentText)
1840 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1841 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843
1844 /**
1845 * Constructs a Notification object with the information needed to
1846 * have a status bar icon without the standard expanded view.
1847 *
1848 * @param icon The resource id of the icon to put in the status bar.
1849 * @param tickerText The text that flows by in the status bar when the notification first
1850 * activates.
1851 * @param when The time to show in the time field. In the System.currentTimeMillis
1852 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001853 *
1854 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001856 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 public Notification(int icon, CharSequence tickerText, long when)
1858 {
1859 this.icon = icon;
1860 this.tickerText = tickerText;
1861 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001862 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
1864
1865 /**
1866 * Unflatten the notification from a parcel.
1867 */
Svet Ganovddb94882016-06-23 19:55:24 -07001868 @SuppressWarnings("unchecked")
1869 public Notification(Parcel parcel) {
1870 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1871 // intents in extras are always written as the last entry.
1872 readFromParcelImpl(parcel);
1873 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001874 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001875 }
1876
1877 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 {
1879 int version = parcel.readInt();
1880
Adrian Roosfb921842017-10-26 14:49:56 +02001881 mWhitelistToken = parcel.readStrongBinder();
1882 if (mWhitelistToken == null) {
1883 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07001884 }
1885 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02001886 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07001887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001889 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001890 if (parcel.readInt() != 0) {
1891 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001892 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1893 icon = mSmallIcon.getResId();
1894 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 number = parcel.readInt();
1897 if (parcel.readInt() != 0) {
1898 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1899 }
1900 if (parcel.readInt() != 0) {
1901 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1902 }
1903 if (parcel.readInt() != 0) {
1904 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1905 }
1906 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001907 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001908 }
1909 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1911 }
Joe Onorato561d3852010-11-20 18:09:34 -08001912 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001913 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 defaults = parcel.readInt();
1916 flags = parcel.readInt();
1917 if (parcel.readInt() != 0) {
1918 sound = Uri.CREATOR.createFromParcel(parcel);
1919 }
1920
1921 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001922 if (parcel.readInt() != 0) {
1923 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 vibrate = parcel.createLongArray();
1926 ledARGB = parcel.readInt();
1927 ledOnMS = parcel.readInt();
1928 ledOffMS = parcel.readInt();
1929 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001930
1931 if (parcel.readInt() != 0) {
1932 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1933 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001934
1935 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001936
John Spurlockfd7f1e02014-03-18 16:41:57 -04001937 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001938
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001939 mGroupKey = parcel.readString();
1940
1941 mSortKey = parcel.readString();
1942
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001943 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001944
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001945 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1946
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001947 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001948 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1949 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001950
Chris Wren8fd39ec2014-02-27 17:43:26 -05001951 if (parcel.readInt() != 0) {
1952 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1953 }
1954
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001955 visibility = parcel.readInt();
1956
1957 if (parcel.readInt() != 0) {
1958 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1959 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001960
1961 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001962
1963 if (parcel.readInt() != 0) {
1964 mChannelId = parcel.readString();
1965 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001966 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001967
1968 if (parcel.readInt() != 0) {
1969 mShortcutId = parcel.readString();
1970 }
1971
1972 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001973
1974 if (parcel.readInt() != 0) {
1975 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1976 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001977
1978 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980
Andy Stadler110988c2010-12-03 14:29:16 -08001981 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001982 public Notification clone() {
1983 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001984 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001985 return that;
1986 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001987
Daniel Sandler1a497d32013-04-18 14:52:45 -04001988 /**
1989 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1990 * of this into that.
1991 * @hide
1992 */
1993 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02001994 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001995 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001996 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001997 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001998 that.number = this.number;
1999
2000 // PendingIntents are global, so there's no reason (or way) to clone them.
2001 that.contentIntent = this.contentIntent;
2002 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002003 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002004
2005 if (this.tickerText != null) {
2006 that.tickerText = this.tickerText.toString();
2007 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002008 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002009 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002010 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002011 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002012 that.contentView = this.contentView.clone();
2013 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002014 if (heavy && this.mLargeIcon != null) {
2015 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002016 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002017 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002018 that.sound = this.sound; // android.net.Uri is immutable
2019 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002020 if (this.audioAttributes != null) {
2021 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2022 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002023
2024 final long[] vibrate = this.vibrate;
2025 if (vibrate != null) {
2026 final int N = vibrate.length;
2027 final long[] vib = that.vibrate = new long[N];
2028 System.arraycopy(vibrate, 0, vib, 0, N);
2029 }
2030
2031 that.ledARGB = this.ledARGB;
2032 that.ledOnMS = this.ledOnMS;
2033 that.ledOffMS = this.ledOffMS;
2034 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002035
Joe Onorato18e69df2010-05-17 22:26:12 -07002036 that.flags = this.flags;
2037
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002038 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002039
John Spurlockfd7f1e02014-03-18 16:41:57 -04002040 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002041
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002042 that.mGroupKey = this.mGroupKey;
2043
2044 that.mSortKey = this.mSortKey;
2045
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002046 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002047 try {
2048 that.extras = new Bundle(this.extras);
2049 // will unparcel
2050 that.extras.size();
2051 } catch (BadParcelableException e) {
2052 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2053 that.extras = null;
2054 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002055 }
2056
Felipe Lemedd85da62016-06-28 11:29:54 -07002057 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2058 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002059 }
2060
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002061 if (this.actions != null) {
2062 that.actions = new Action[this.actions.length];
2063 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002064 if ( this.actions[i] != null) {
2065 that.actions[i] = this.actions[i].clone();
2066 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002067 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002068 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002069
Daniel Sandler1a497d32013-04-18 14:52:45 -04002070 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002071 that.bigContentView = this.bigContentView.clone();
2072 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002073
Chris Wren8fd39ec2014-02-27 17:43:26 -05002074 if (heavy && this.headsUpContentView != null) {
2075 that.headsUpContentView = this.headsUpContentView.clone();
2076 }
2077
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002078 that.visibility = this.visibility;
2079
2080 if (this.publicVersion != null) {
2081 that.publicVersion = new Notification();
2082 this.publicVersion.cloneInto(that.publicVersion, heavy);
2083 }
2084
Dan Sandler26e81cf2014-05-06 10:01:27 -04002085 that.color = this.color;
2086
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002087 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002088 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002089 that.mShortcutId = this.mShortcutId;
2090 that.mBadgeIcon = this.mBadgeIcon;
2091 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002092 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002093
Daniel Sandler1a497d32013-04-18 14:52:45 -04002094 if (!heavy) {
2095 that.lightenPayload(); // will clean out extras
2096 }
2097 }
2098
2099 /**
2100 * Removes heavyweight parts of the Notification object for archival or for sending to
2101 * listeners when the full contents are not necessary.
2102 * @hide
2103 */
2104 public final void lightenPayload() {
2105 tickerView = null;
2106 contentView = null;
2107 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002108 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002109 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002110 if (extras != null && !extras.isEmpty()) {
2111 final Set<String> keyset = extras.keySet();
2112 final int N = keyset.size();
2113 final String[] keys = keyset.toArray(new String[N]);
2114 for (int i=0; i<N; i++) {
2115 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002116 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2117 continue;
2118 }
Dan Sandler50128532015-12-08 15:42:41 -05002119 final Object obj = extras.get(key);
2120 if (obj != null &&
2121 ( obj instanceof Parcelable
2122 || obj instanceof Parcelable[]
2123 || obj instanceof SparseArray
2124 || obj instanceof ArrayList)) {
2125 extras.remove(key);
2126 }
2127 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002128 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002129 }
2130
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002131 /**
2132 * Make sure this CharSequence is safe to put into a bundle, which basically
2133 * means it had better not be some custom Parcelable implementation.
2134 * @hide
2135 */
2136 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002137 if (cs == null) return cs;
2138 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2139 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2140 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002141 if (cs instanceof Parcelable) {
2142 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2143 + " instance is a custom Parcelable and not allowed in Notification");
2144 return cs.toString();
2145 }
Selim Cinek60a54252016-02-26 17:03:25 -08002146 return removeTextSizeSpans(cs);
2147 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002148
Selim Cinek60a54252016-02-26 17:03:25 -08002149 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2150 if (charSequence instanceof Spanned) {
2151 Spanned ss = (Spanned) charSequence;
2152 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2153 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2154 for (Object span : spans) {
2155 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002156 if (resultSpan instanceof CharacterStyle) {
2157 resultSpan = ((CharacterStyle) span).getUnderlying();
2158 }
2159 if (resultSpan instanceof TextAppearanceSpan) {
2160 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002161 resultSpan = new TextAppearanceSpan(
2162 originalSpan.getFamily(),
2163 originalSpan.getTextStyle(),
2164 -1,
2165 originalSpan.getTextColor(),
2166 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002167 } else if (resultSpan instanceof RelativeSizeSpan
2168 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002169 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002170 } else {
2171 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002172 }
2173 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2174 ss.getSpanFlags(span));
2175 }
2176 return builder;
2177 }
2178 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002179 }
2180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 public int describeContents() {
2182 return 0;
2183 }
2184
2185 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002186 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 */
Svet Ganovddb94882016-06-23 19:55:24 -07002188 public void writeToParcel(Parcel parcel, int flags) {
2189 // We need to mark all pending intents getting into the notification
2190 // system as being put there to later allow the notification ranker
2191 // to launch them and by doing so add the app to the battery saver white
2192 // list for a short period of time. The problem is that the system
2193 // cannot look into the extras as there may be parcelables there that
2194 // the platform does not know how to handle. To go around that we have
2195 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002196 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002197 if (collectPendingIntents) {
2198 PendingIntent.setOnMarshaledListener(
2199 (PendingIntent intent, Parcel out, int outFlags) -> {
2200 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002201 if (allPendingIntents == null) {
2202 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002203 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002204 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002205 }
2206 });
2207 }
2208 try {
2209 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002210 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002211 writeToParcelImpl(parcel, flags);
2212 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002213 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002214 } finally {
2215 if (collectPendingIntents) {
2216 PendingIntent.setOnMarshaledListener(null);
2217 }
2218 }
2219 }
2220
2221 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 parcel.writeInt(1);
2223
Adrian Roosfb921842017-10-26 14:49:56 +02002224 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002226 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002227 if (mSmallIcon == null && icon != 0) {
2228 // you snuck an icon in here without using the builder; let's try to keep it
2229 mSmallIcon = Icon.createWithResource("", icon);
2230 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002231 if (mSmallIcon != null) {
2232 parcel.writeInt(1);
2233 mSmallIcon.writeToParcel(parcel, 0);
2234 } else {
2235 parcel.writeInt(0);
2236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 parcel.writeInt(number);
2238 if (contentIntent != null) {
2239 parcel.writeInt(1);
2240 contentIntent.writeToParcel(parcel, 0);
2241 } else {
2242 parcel.writeInt(0);
2243 }
2244 if (deleteIntent != null) {
2245 parcel.writeInt(1);
2246 deleteIntent.writeToParcel(parcel, 0);
2247 } else {
2248 parcel.writeInt(0);
2249 }
2250 if (tickerText != null) {
2251 parcel.writeInt(1);
2252 TextUtils.writeToParcel(tickerText, parcel, flags);
2253 } else {
2254 parcel.writeInt(0);
2255 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002256 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002257 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002258 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002259 } else {
2260 parcel.writeInt(0);
2261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 if (contentView != null) {
2263 parcel.writeInt(1);
2264 contentView.writeToParcel(parcel, 0);
2265 } else {
2266 parcel.writeInt(0);
2267 }
Selim Cinek279fa862016-06-14 10:57:25 -07002268 if (mLargeIcon == null && largeIcon != null) {
2269 // you snuck an icon in here without using the builder; let's try to keep it
2270 mLargeIcon = Icon.createWithBitmap(largeIcon);
2271 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002272 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002273 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002274 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002275 } else {
2276 parcel.writeInt(0);
2277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278
2279 parcel.writeInt(defaults);
2280 parcel.writeInt(this.flags);
2281
2282 if (sound != null) {
2283 parcel.writeInt(1);
2284 sound.writeToParcel(parcel, 0);
2285 } else {
2286 parcel.writeInt(0);
2287 }
2288 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002289
2290 if (audioAttributes != null) {
2291 parcel.writeInt(1);
2292 audioAttributes.writeToParcel(parcel, 0);
2293 } else {
2294 parcel.writeInt(0);
2295 }
2296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 parcel.writeLongArray(vibrate);
2298 parcel.writeInt(ledARGB);
2299 parcel.writeInt(ledOnMS);
2300 parcel.writeInt(ledOffMS);
2301 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002302
2303 if (fullScreenIntent != null) {
2304 parcel.writeInt(1);
2305 fullScreenIntent.writeToParcel(parcel, 0);
2306 } else {
2307 parcel.writeInt(0);
2308 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309
2310 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002311
John Spurlockfd7f1e02014-03-18 16:41:57 -04002312 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002313
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002314 parcel.writeString(mGroupKey);
2315
2316 parcel.writeString(mSortKey);
2317
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002318 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002319
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002320 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002321
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002322 if (bigContentView != null) {
2323 parcel.writeInt(1);
2324 bigContentView.writeToParcel(parcel, 0);
2325 } else {
2326 parcel.writeInt(0);
2327 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002328
Chris Wren8fd39ec2014-02-27 17:43:26 -05002329 if (headsUpContentView != null) {
2330 parcel.writeInt(1);
2331 headsUpContentView.writeToParcel(parcel, 0);
2332 } else {
2333 parcel.writeInt(0);
2334 }
2335
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002336 parcel.writeInt(visibility);
2337
2338 if (publicVersion != null) {
2339 parcel.writeInt(1);
2340 publicVersion.writeToParcel(parcel, 0);
2341 } else {
2342 parcel.writeInt(0);
2343 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002344
2345 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002346
2347 if (mChannelId != null) {
2348 parcel.writeInt(1);
2349 parcel.writeString(mChannelId);
2350 } else {
2351 parcel.writeInt(0);
2352 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002353 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002354
2355 if (mShortcutId != null) {
2356 parcel.writeInt(1);
2357 parcel.writeString(mShortcutId);
2358 } else {
2359 parcel.writeInt(0);
2360 }
2361
2362 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002363
2364 if (mSettingsText != null) {
2365 parcel.writeInt(1);
2366 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2367 } else {
2368 parcel.writeInt(0);
2369 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002370
2371 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373
2374 /**
2375 * Parcelable.Creator that instantiates Notification objects
2376 */
2377 public static final Parcelable.Creator<Notification> CREATOR
2378 = new Parcelable.Creator<Notification>()
2379 {
2380 public Notification createFromParcel(Parcel parcel)
2381 {
2382 return new Notification(parcel);
2383 }
2384
2385 public Notification[] newArray(int size)
2386 {
2387 return new Notification[size];
2388 }
2389 };
2390
2391 /**
2392 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2393 * layout.
2394 *
2395 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2396 * in the view.</p>
2397 * @param context The context for your application / activity.
2398 * @param contentTitle The title that goes in the expanded entry.
2399 * @param contentText The text that goes in the expanded entry.
2400 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2401 * If this is an activity, it must include the
2402 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002403 * that you take care of task management as described in the
2404 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2405 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002406 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002407 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002408 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002410 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 public void setLatestEventInfo(Context context,
2412 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002413 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2414 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2415 new Throwable());
2416 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002417
Selim Cinek4ac6f602016-06-13 15:47:03 -07002418 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2419 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2420 }
2421
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002422 // ensure that any information already set directly is preserved
2423 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002424
2425 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002427 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 }
2429 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002430 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002432 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002433
2434 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436
Julia Reynoldsda303542015-11-23 14:00:20 -05002437 /**
2438 * @hide
2439 */
2440 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002441 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002442 }
2443
2444 /**
2445 * @hide
2446 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002447 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002448 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002449 }
2450
Yi Jin6b514142017-10-30 14:54:12 -07002451 /**
2452 * @hide
2453 */
2454 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2455 long token = proto.start(fieldId);
2456 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2457 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2458 proto.write(NotificationProto.FLAGS, this.flags);
2459 proto.write(NotificationProto.COLOR, this.color);
2460 proto.write(NotificationProto.CATEGORY, this.category);
2461 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2462 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2463 if (this.actions != null) {
2464 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2465 }
2466 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2467 proto.write(NotificationProto.VISIBILITY, this.visibility);
2468 }
2469 if (publicVersion != null) {
2470 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2471 }
2472 proto.end(token);
2473 }
2474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 @Override
2476 public String toString() {
2477 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002478 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002479 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002480 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002481 sb.append(priority);
2482 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002483 if (contentView != null) {
2484 sb.append(contentView.getPackage());
2485 sb.append("/0x");
2486 sb.append(Integer.toHexString(contentView.getLayoutId()));
2487 } else {
2488 sb.append("null");
2489 }
2490 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002491 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2492 sb.append("default");
2493 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 int N = this.vibrate.length-1;
2495 sb.append("[");
2496 for (int i=0; i<N; i++) {
2497 sb.append(this.vibrate[i]);
2498 sb.append(',');
2499 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002500 if (N != -1) {
2501 sb.append(this.vibrate[N]);
2502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 } else {
2505 sb.append("null");
2506 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002507 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002508 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002510 } else if (this.sound != null) {
2511 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 } else {
2513 sb.append("null");
2514 }
Chris Wren365b6d32015-07-16 10:39:26 -04002515 if (this.tickerText != null) {
2516 sb.append(" tick");
2517 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002518 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002520 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002521 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002522 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002523 if (this.category != null) {
2524 sb.append(" category=");
2525 sb.append(this.category);
2526 }
2527 if (this.mGroupKey != null) {
2528 sb.append(" groupKey=");
2529 sb.append(this.mGroupKey);
2530 }
2531 if (this.mSortKey != null) {
2532 sb.append(" sortKey=");
2533 sb.append(this.mSortKey);
2534 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002535 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002536 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002537 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002538 }
2539 sb.append(" vis=");
2540 sb.append(visibilityToString(this.visibility));
2541 if (this.publicVersion != null) {
2542 sb.append(" publicVersion=");
2543 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002544 }
2545 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 return sb.toString();
2547 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002548
Dan Sandler1b718782014-07-18 12:43:45 -04002549 /**
2550 * {@hide}
2551 */
2552 public static String visibilityToString(int vis) {
2553 switch (vis) {
2554 case VISIBILITY_PRIVATE:
2555 return "PRIVATE";
2556 case VISIBILITY_PUBLIC:
2557 return "PUBLIC";
2558 case VISIBILITY_SECRET:
2559 return "SECRET";
2560 default:
2561 return "UNKNOWN(" + String.valueOf(vis) + ")";
2562 }
2563 }
2564
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002566 * {@hide}
2567 */
2568 public static String priorityToString(@Priority int pri) {
2569 switch (pri) {
2570 case PRIORITY_MIN:
2571 return "MIN";
2572 case PRIORITY_LOW:
2573 return "LOW";
2574 case PRIORITY_DEFAULT:
2575 return "DEFAULT";
2576 case PRIORITY_HIGH:
2577 return "HIGH";
2578 case PRIORITY_MAX:
2579 return "MAX";
2580 default:
2581 return "UNKNOWN(" + String.valueOf(pri) + ")";
2582 }
2583 }
2584
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002585 /**
2586 * @hide
2587 */
2588 public boolean hasCompletedProgress() {
2589 // not a progress notification; can't be complete
2590 if (!extras.containsKey(EXTRA_PROGRESS)
2591 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2592 return false;
2593 }
2594 // many apps use max 0 for 'indeterminate'; not complete
2595 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2596 return false;
2597 }
2598 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2599 }
2600
Jeff Sharkey000ce802017-04-29 13:13:27 -06002601 /** @removed */
2602 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002603 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002604 return mChannelId;
2605 }
2606
2607 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002608 * Returns the id of the channel this notification posts to.
2609 */
2610 public String getChannelId() {
2611 return mChannelId;
2612 }
2613
Jeff Sharkey000ce802017-04-29 13:13:27 -06002614 /** @removed */
2615 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002616 public long getTimeout() {
2617 return mTimeout;
2618 }
2619
2620 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002621 * Returns the duration from posting after which this notification should be canceled by the
2622 * system, if it's not canceled already.
2623 */
2624 public long getTimeoutAfter() {
2625 return mTimeout;
2626 }
2627
2628 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002629 * Returns what icon should be shown for this notification if it is being displayed in a
2630 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2631 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2632 */
2633 public int getBadgeIconType() {
2634 return mBadgeIcon;
2635 }
2636
2637 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002638 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002639 *
2640 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2641 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002642 */
2643 public String getShortcutId() {
2644 return mShortcutId;
2645 }
2646
Julia Reynolds3aedded2017-03-31 14:42:09 -04002647
2648 /**
2649 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2650 */
2651 public CharSequence getSettingsText() {
2652 return mSettingsText;
2653 }
2654
Julia Reynolds13d898c2017-02-02 12:22:05 -05002655 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002656 * Returns which type of notifications in a group are responsible for audibly alerting the
2657 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2658 * {@link #GROUP_ALERT_SUMMARY}.
2659 */
2660 public @GroupAlertBehavior int getGroupAlertBehavior() {
2661 return mGroupAlertBehavior;
2662 }
2663
2664 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002665 * The small icon representing this notification in the status bar and content view.
2666 *
2667 * @return the small icon representing this notification.
2668 *
2669 * @see Builder#getSmallIcon()
2670 * @see Builder#setSmallIcon(Icon)
2671 */
2672 public Icon getSmallIcon() {
2673 return mSmallIcon;
2674 }
2675
2676 /**
2677 * Used when notifying to clean up legacy small icons.
2678 * @hide
2679 */
2680 public void setSmallIcon(Icon icon) {
2681 mSmallIcon = icon;
2682 }
2683
2684 /**
2685 * The large icon shown in this notification's content view.
2686 * @see Builder#getLargeIcon()
2687 * @see Builder#setLargeIcon(Icon)
2688 */
2689 public Icon getLargeIcon() {
2690 return mLargeIcon;
2691 }
2692
2693 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002694 * @hide
2695 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002696 public boolean isGroupSummary() {
2697 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2698 }
2699
2700 /**
2701 * @hide
2702 */
2703 public boolean isGroupChild() {
2704 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2705 }
2706
2707 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002708 * @hide
2709 */
2710 public boolean suppressAlertingDueToGrouping() {
2711 if (isGroupSummary()
2712 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2713 return true;
2714 } else if (isGroupChild()
2715 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2716 return true;
2717 }
2718 return false;
2719 }
2720
2721 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002722 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002723 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002724 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002725 * content views using the platform's notification layout template. If your app supports
2726 * versions of Android as old as API level 4, you can instead use
2727 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2728 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2729 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002730 *
Scott Main183bf112012-08-13 19:12:13 -07002731 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002732 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002733 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002734 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002735 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2736 * .setContentText(subject)
2737 * .setSmallIcon(R.drawable.new_mail)
2738 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002739 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002740 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002741 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002742 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002743 /**
2744 * @hide
2745 */
2746 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2747 "android.rebuild.contentViewActionCount";
2748 /**
2749 * @hide
2750 */
2751 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2752 = "android.rebuild.bigViewActionCount";
2753 /**
2754 * @hide
2755 */
2756 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2757 = "android.rebuild.hudViewActionCount";
2758
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002759 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002760
Selim Cinek6743c0b2017-01-18 18:24:01 -08002761 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2762 SystemProperties.getBoolean("notifications.only_title", true);
2763
Selim Cinek389edcd2017-05-11 19:16:44 -07002764 /**
2765 * The lightness difference that has to be added to the primary text color to obtain the
2766 * secondary text color when the background is light.
2767 */
2768 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2769
2770 /**
2771 * The lightness difference that has to be added to the primary text color to obtain the
2772 * secondary text color when the background is dark.
2773 * A bit less then the above value, since it looks better on dark backgrounds.
2774 */
2775 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2776
Joe Onorato46439ce2010-11-19 13:56:21 -08002777 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002778 private Notification mN;
2779 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002780 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002781 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2782 private ArrayList<String> mPersonList = new ArrayList<String>();
2783 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002784 private boolean mIsLegacy;
2785 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002786
2787 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002788 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2789 */
2790 private int mCachedContrastColor = COLOR_INVALID;
2791 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002792 /**
2793 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2794 */
2795 private int mCachedAmbientColor = COLOR_INVALID;
2796 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002797
2798 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002799 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2800 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2801 */
2802 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002803 private int mTextColorsAreForBackground = COLOR_INVALID;
2804 private int mPrimaryTextColor = COLOR_INVALID;
2805 private int mSecondaryTextColor = COLOR_INVALID;
2806 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002807 private int mBackgroundColor = COLOR_INVALID;
2808 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002809 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002810 /**
2811 * A temporary location where actions are stored. If != null the view originally has action
2812 * but doesn't have any for this inflation.
2813 */
2814 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002815 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002816
Anthony Chenad4d1582017-04-10 16:07:58 -07002817 private boolean mTintActionButtons;
2818 private boolean mInNightMode;
2819
Adrian Roos70d7aa32017-01-11 15:39:06 -08002820 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002821 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002822 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002823 * @param context
2824 * A {@link Context} that will be used by the Builder to construct the
2825 * RemoteViews. The Context will not be held past the lifetime of this Builder
2826 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002827 * @param channelId
2828 * The constructed Notification will be posted on this
2829 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2830 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002832 public Builder(Context context, String channelId) {
2833 this(context, (Notification) null);
2834 mN.mChannelId = channelId;
2835 }
2836
2837 /**
2838 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2839 * instead. All posted Notifications must specify a NotificationChannel Id.
2840 */
2841 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002842 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002843 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002844 }
2845
Joe Onoratocb109a02011-01-18 17:57:41 -08002846 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002847 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002848 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002849 public Builder(Context context, Notification toAdopt) {
2850 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002851 Resources res = mContext.getResources();
2852 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2853
2854 if (res.getBoolean(R.bool.config_enableNightMode)) {
2855 Configuration currentConfig = res.getConfiguration();
2856 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2857 == Configuration.UI_MODE_NIGHT_YES;
2858 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002859
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002860 if (toAdopt == null) {
2861 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002862 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2863 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2864 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002865 mN.priority = PRIORITY_DEFAULT;
2866 mN.visibility = VISIBILITY_PRIVATE;
2867 } else {
2868 mN = toAdopt;
2869 if (mN.actions != null) {
2870 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002871 }
2872
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002873 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2874 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2875 }
2876
Selim Cinek4ac6f602016-06-13 15:47:03 -07002877 if (mN.getSmallIcon() == null && mN.icon != 0) {
2878 setSmallIcon(mN.icon);
2879 }
2880
2881 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2882 setLargeIcon(mN.largeIcon);
2883 }
2884
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002885 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2886 if (!TextUtils.isEmpty(templateClass)) {
2887 final Class<? extends Style> styleClass
2888 = getNotificationStyleClass(templateClass);
2889 if (styleClass == null) {
2890 Log.d(TAG, "Unknown style class: " + templateClass);
2891 } else {
2892 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002893 final Constructor<? extends Style> ctor =
2894 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002895 ctor.setAccessible(true);
2896 final Style style = ctor.newInstance();
2897 style.restoreFromExtras(mN.extras);
2898
2899 if (style != null) {
2900 setStyle(style);
2901 }
2902 } catch (Throwable t) {
2903 Log.e(TAG, "Could not create Style", t);
2904 }
2905 }
2906 }
2907
2908 }
2909 }
2910
2911 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002912 if (mColorUtil == null) {
2913 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002914 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002915 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002916 }
2917
2918 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002919 * If this notification is duplicative of a Launcher shortcut, sets the
2920 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2921 * the shortcut.
2922 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002923 * This field will be ignored by Launchers that don't support badging, don't show
2924 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002925 *
2926 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2927 * supersedes
2928 */
2929 public Builder setShortcutId(String shortcutId) {
2930 mN.mShortcutId = shortcutId;
2931 return this;
2932 }
2933
2934 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002935 * Sets which icon to display as a badge for this notification.
2936 *
2937 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2938 * {@link #BADGE_ICON_LARGE}.
2939 *
2940 * Note: This value might be ignored, for launchers that don't support badge icons.
2941 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002942 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002943 mN.mBadgeIcon = icon;
2944 return this;
2945 }
2946
2947 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002948 * Sets the group alert behavior for this notification. Use this method to mute this
2949 * notification if alerts for this notification's group should be handled by a different
2950 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04002951 * {@link #setGroup(String) group}. This must be called on all notifications you want to
2952 * mute. For example, if you want only the summary of your group to make noise, all
2953 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002954 *
2955 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2956 */
2957 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2958 mN.mGroupAlertBehavior = groupAlertBehavior;
2959 return this;
2960 }
2961
Jeff Sharkey000ce802017-04-29 13:13:27 -06002962 /** @removed */
2963 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002964 public Builder setChannel(String channelId) {
2965 mN.mChannelId = channelId;
2966 return this;
2967 }
2968
2969 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002970 * Specifies the channel the notification should be delivered on.
2971 */
2972 public Builder setChannelId(String channelId) {
2973 mN.mChannelId = channelId;
2974 return this;
2975 }
2976
Jeff Sharkey000ce802017-04-29 13:13:27 -06002977 /** @removed */
2978 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002979 public Builder setTimeout(long durationMs) {
2980 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002981 return this;
2982 }
2983
2984 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002985 * Specifies a duration in milliseconds after which this notification should be canceled,
2986 * if it is not already canceled.
2987 */
2988 public Builder setTimeoutAfter(long durationMs) {
2989 mN.mTimeout = durationMs;
2990 return this;
2991 }
2992
2993 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002994 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002995 *
2996 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2997 * shown anymore by default and must be opted into by using
2998 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002999 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003000 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003001 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003002 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003003 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003004 return this;
3005 }
3006
Joe Onoratocb109a02011-01-18 17:57:41 -08003007 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003008 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003009 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003010 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3011 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003012 */
3013 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003014 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003015 return this;
3016 }
3017
3018 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003019 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003020 *
3021 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003022 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003023 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003024 * Useful when showing an elapsed time (like an ongoing phone call).
3025 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003026 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003027 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003028 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003029 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003030 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003031 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003032 */
3033 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003034 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003035 return this;
3036 }
3037
3038 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003039 * Sets the Chronometer to count down instead of counting up.
3040 *
3041 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3042 * If it isn't set the chronometer will count up.
3043 *
3044 * @see #setUsesChronometer(boolean)
3045 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003046 public Builder setChronometerCountDown(boolean countDown) {
3047 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003048 return this;
3049 }
3050
3051 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003052 * Set the small icon resource, which will be used to represent the notification in the
3053 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003054 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003055
3056 * The platform template for the expanded view will draw this icon in the left, unless a
3057 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3058 * icon will be moved to the right-hand side.
3059 *
3060
3061 * @param icon
3062 * A resource ID in the application's package of the drawable to use.
3063 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003064 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003065 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003066 return setSmallIcon(icon != 0
3067 ? Icon.createWithResource(mContext, icon)
3068 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003069 }
3070
Joe Onoratocb109a02011-01-18 17:57:41 -08003071 /**
3072 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3073 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3074 * LevelListDrawable}.
3075 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003076 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003077 * @param level The level to use for the icon.
3078 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003079 * @see Notification#icon
3080 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003081 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003082 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003083 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003084 return setSmallIcon(icon);
3085 }
3086
3087 /**
3088 * Set the small icon, which will be used to represent the notification in the
3089 * status bar and content view (unless overriden there by a
3090 * {@link #setLargeIcon(Bitmap) large icon}).
3091 *
3092 * @param icon An Icon object to use.
3093 * @see Notification#icon
3094 */
3095 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003096 mN.setSmallIcon(icon);
3097 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3098 mN.icon = icon.getResId();
3099 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003100 return this;
3101 }
3102
Joe Onoratocb109a02011-01-18 17:57:41 -08003103 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003104 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003105 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003106 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003107 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003108 return this;
3109 }
3110
Joe Onoratocb109a02011-01-18 17:57:41 -08003111 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003112 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003113 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003114 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003115 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003116 return this;
3117 }
3118
Joe Onoratocb109a02011-01-18 17:57:41 -08003119 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003120 * This provides some additional information that is displayed in the notification. No
3121 * guarantees are given where exactly it is displayed.
3122 *
3123 * <p>This information should only be provided if it provides an essential
3124 * benefit to the understanding of the notification. The more text you provide the
3125 * less readable it becomes. For example, an email client should only provide the account
3126 * name here if more than one email account has been added.</p>
3127 *
3128 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3129 * notification header area.
3130 *
3131 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3132 * this will be shown in the third line of text in the platform notification template.
3133 * You should not be using {@link #setProgress(int, int, boolean)} at the
3134 * same time on those versions; they occupy the same place.
3135 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003136 */
3137 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003138 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003139 return this;
3140 }
3141
3142 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003143 * Provides text that will appear as a link to your application's settings.
3144 *
3145 * <p>This text does not appear within notification {@link Style templates} but may
3146 * appear when the user uses an affordance to learn more about the notification.
3147 * Additionally, this text will not appear unless you provide a valid link target by
3148 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3149 *
3150 * <p>This text is meant to be concise description about what the user can customize
3151 * when they click on this link. The recommended maximum length is 40 characters.
3152 * @param text
3153 * @return
3154 */
3155 public Builder setSettingsText(CharSequence text) {
3156 mN.mSettingsText = safeCharSequence(text);
3157 return this;
3158 }
3159
3160 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003161 * Set the remote input history.
3162 *
3163 * This should be set to the most recent inputs that have been sent
3164 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3165 * longer relevant (e.g. for chat notifications once the other party has responded).
3166 *
3167 * The most recent input must be stored at the 0 index, the second most recent at the
3168 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3169 * and how much of each individual input is shown.
3170 *
3171 * <p>Note: The reply text will only be shown on notifications that have least one action
3172 * with a {@code RemoteInput}.</p>
3173 */
3174 public Builder setRemoteInputHistory(CharSequence[] text) {
3175 if (text == null) {
3176 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3177 } else {
3178 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3179 CharSequence[] safe = new CharSequence[N];
3180 for (int i = 0; i < N; i++) {
3181 safe[i] = safeCharSequence(text[i]);
3182 }
3183 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3184 }
3185 return this;
3186 }
3187
3188 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003189 * Sets the number of items this notification represents. May be displayed as a badge count
3190 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003191 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003192 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003193 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003194 return this;
3195 }
3196
Joe Onoratocb109a02011-01-18 17:57:41 -08003197 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003198 * A small piece of additional information pertaining to this notification.
3199 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003200 * The platform template will draw this on the last line of the notification, at the far
3201 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003202 *
3203 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3204 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3205 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003206 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003207 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003209 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003210 return this;
3211 }
3212
Joe Onoratocb109a02011-01-18 17:57:41 -08003213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003214 * Set the progress this notification represents.
3215 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003216 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003217 */
3218 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003219 mN.extras.putInt(EXTRA_PROGRESS, progress);
3220 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3221 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003222 return this;
3223 }
3224
3225 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003226 * Supply a custom RemoteViews to use instead of the platform template.
3227 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003228 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003229 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003230 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003231 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003232 return setCustomContentView(views);
3233 }
3234
3235 /**
3236 * Supply custom RemoteViews to use instead of the platform template.
3237 *
3238 * This will override the layout that would otherwise be constructed by this Builder
3239 * object.
3240 */
3241 public Builder setCustomContentView(RemoteViews contentView) {
3242 mN.contentView = contentView;
3243 return this;
3244 }
3245
3246 /**
3247 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3248 *
3249 * This will override the expanded layout that would otherwise be constructed by this
3250 * Builder object.
3251 */
3252 public Builder setCustomBigContentView(RemoteViews contentView) {
3253 mN.bigContentView = contentView;
3254 return this;
3255 }
3256
3257 /**
3258 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3259 *
3260 * This will override the heads-up layout that would otherwise be constructed by this
3261 * Builder object.
3262 */
3263 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3264 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003265 return this;
3266 }
3267
Joe Onoratocb109a02011-01-18 17:57:41 -08003268 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003269 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3270 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003271 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3272 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3273 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003274 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003275 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003276 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003277 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003278 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003279 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003280 mN.contentIntent = intent;
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 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3286 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003287 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 return this;
3292 }
3293
Joe Onoratocb109a02011-01-18 17:57:41 -08003294 /**
3295 * An intent to launch instead of posting the notification to the status bar.
3296 * Only for use with extremely high-priority notifications demanding the user's
3297 * <strong>immediate</strong> attention, such as an incoming phone call or
3298 * alarm clock that the user has explicitly set to a particular time.
3299 * If this facility is used for something else, please give the user an option
3300 * to turn it off and use a normal notification, as this can be extremely
3301 * disruptive.
3302 *
Chris Wren47c20a12014-06-18 17:27:29 -04003303 * <p>
3304 * The system UI may choose to display a heads-up notification, instead of
3305 * launching this intent, while the user is using the device.
3306 * </p>
3307 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003308 * @param intent The pending intent to launch.
3309 * @param highPriority Passing true will cause this notification to be sent
3310 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003311 *
3312 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003313 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003314 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003315 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003316 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3317 return this;
3318 }
3319
Joe Onoratocb109a02011-01-18 17:57:41 -08003320 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003321 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003322 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003323 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003324 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003325 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003326 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003327 return this;
3328 }
3329
Joe Onoratocb109a02011-01-18 17:57:41 -08003330 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003331 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003332 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003333 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003335 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 setTicker(tickerText);
3337 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 return this;
3339 }
3340
Joe Onoratocb109a02011-01-18 17:57:41 -08003341 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003342 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003343 *
3344 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003345 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3346 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003347 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003348 public Builder setLargeIcon(Bitmap b) {
3349 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3350 }
3351
3352 /**
3353 * Add a large icon to the notification content view.
3354 *
3355 * In the platform template, this image will be shown on the left of the notification view
3356 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3357 * badge atop the large icon).
3358 */
3359 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003360 mN.mLargeIcon = icon;
3361 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003362 return this;
3363 }
3364
Joe Onoratocb109a02011-01-18 17:57:41 -08003365 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003366 * Set the sound to play.
3367 *
John Spurlockc0650f022014-07-19 13:22:39 -04003368 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3369 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003370 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003371 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003372 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003373 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003374 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003375 mN.sound = sound;
3376 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003377 return this;
3378 }
3379
Joe Onoratocb109a02011-01-18 17:57:41 -08003380 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003381 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003383 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3384 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003385 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003386 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003387 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003388 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003389 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003390 mN.sound = sound;
3391 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003392 return this;
3393 }
3394
Joe Onoratocb109a02011-01-18 17:57:41 -08003395 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003396 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3397 * use during playback.
3398 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003399 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003400 * @see Notification#sound
3401 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003402 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003403 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003404 mN.sound = sound;
3405 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003406 return this;
3407 }
3408
3409 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003410 * Set the vibration pattern to use.
3411 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003412 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3413 * <code>pattern</code> parameter.
3414 *
Chris Wren47c20a12014-06-18 17:27:29 -04003415 * <p>
3416 * A notification that vibrates is more likely to be presented as a heads-up notification.
3417 * </p>
3418 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003419 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003420 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003421 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003422 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003423 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003425 return this;
3426 }
3427
Joe Onoratocb109a02011-01-18 17:57:41 -08003428 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003429 * Set the desired color for the indicator LED on the device, as well as the
3430 * blink duty cycle (specified in milliseconds).
3431 *
3432
3433 * Not all devices will honor all (or even any) of these values.
3434 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003435 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003436 * @see Notification#ledARGB
3437 * @see Notification#ledOnMS
3438 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003439 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003440 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003441 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 mN.ledARGB = argb;
3443 mN.ledOnMS = onMs;
3444 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003445 if (onMs != 0 || offMs != 0) {
3446 mN.flags |= FLAG_SHOW_LIGHTS;
3447 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003448 return this;
3449 }
3450
Joe Onoratocb109a02011-01-18 17:57:41 -08003451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003452 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003453 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003454
3455 * Ongoing notifications cannot be dismissed by the user, so your application or service
3456 * must take care of canceling them.
3457 *
3458
3459 * They are typically used to indicate a background task that the user is actively engaged
3460 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3461 * (e.g., a file download, sync operation, active network connection).
3462 *
3463
3464 * @see Notification#FLAG_ONGOING_EVENT
3465 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003466 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003467 public Builder setOngoing(boolean ongoing) {
3468 setFlag(FLAG_ONGOING_EVENT, ongoing);
3469 return this;
3470 }
3471
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003473 * Set whether this notification should be colorized. When set, the color set with
3474 * {@link #setColor(int)} will be used as the background color of this notification.
3475 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003476 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3477 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003478 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003479 * For most styles, the coloring will only be applied if the notification is for a
3480 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003481 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003482 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003483 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003484 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003485 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003486 */
3487 public Builder setColorized(boolean colorize) {
3488 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3489 return this;
3490 }
3491
3492 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003493 * Set this flag if you would only like the sound, vibrate
3494 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003495 *
3496 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003497 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003498 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3499 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3500 return this;
3501 }
3502
Joe Onoratocb109a02011-01-18 17:57:41 -08003503 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003504 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 *
3506 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003507 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003509 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003510 return this;
3511 }
3512
Joe Onoratocb109a02011-01-18 17:57:41 -08003513 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003514 * Set whether or not this notification should not bridge to other devices.
3515 *
3516 * <p>Some notifications can be bridged to other devices for remote display.
3517 * This hint can be set to recommend this notification not be bridged.
3518 */
3519 public Builder setLocalOnly(boolean localOnly) {
3520 setFlag(FLAG_LOCAL_ONLY, localOnly);
3521 return this;
3522 }
3523
3524 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003525 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003526 * <p>
3527 * The value should be one or more of the following fields combined with
3528 * bitwise-or:
3529 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3530 * <p>
3531 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003532 *
3533 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003534 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003535 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003536 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003537 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003538 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003539 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003540 return this;
3541 }
3542
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003543 /**
3544 * Set the priority of this notification.
3545 *
3546 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003547 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003548 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003549 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003550 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003551 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003552 return this;
3553 }
Joe Malin8d40d042012-11-05 11:36:40 -08003554
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003555 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003556 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003557 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003558 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003559 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003560 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003561 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003562 return this;
3563 }
3564
3565 /**
Chris Wrendde75302014-03-26 17:24:15 -04003566 * Add a person that is relevant to this notification.
3567 *
Chris Wrene6c48932014-09-29 17:19:27 -04003568 * <P>
3569 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003570 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3571 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3572 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003573 * </P>
3574 *
3575 * <P>
3576 * The person should be specified by the {@code String} representation of a
3577 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3578 * </P>
3579 *
3580 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3581 * URIs. The path part of these URIs must exist in the contacts database, in the
3582 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3583 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3584 * </P>
3585 *
3586 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003587 * @see Notification#EXTRA_PEOPLE
3588 */
Chris Wrene6c48932014-09-29 17:19:27 -04003589 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003590 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003591 return this;
3592 }
3593
3594 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003595 * Set this notification to be part of a group of notifications sharing the same key.
3596 * Grouped notifications may display in a cluster or stack on devices which
3597 * support such rendering.
3598 *
3599 * <p>To make this notification the summary for its group, also call
3600 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3601 * {@link #setSortKey}.
3602 * @param groupKey The group key of the group.
3603 * @return this object for method chaining
3604 */
3605 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003606 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003607 return this;
3608 }
3609
3610 /**
3611 * Set this notification to be the group summary for a group of notifications.
3612 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003613 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3614 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003615 * @param isGroupSummary Whether this notification should be a group summary.
3616 * @return this object for method chaining
3617 */
3618 public Builder setGroupSummary(boolean isGroupSummary) {
3619 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3620 return this;
3621 }
3622
3623 /**
3624 * Set a sort key that orders this notification among other notifications from the
3625 * same package. This can be useful if an external sort was already applied and an app
3626 * would like to preserve this. Notifications will be sorted lexicographically using this
3627 * value, although providing different priorities in addition to providing sort key may
3628 * cause this value to be ignored.
3629 *
3630 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003631 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003632 *
3633 * @see String#compareTo(String)
3634 */
3635 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003636 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003637 return this;
3638 }
3639
3640 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003641 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003642 *
Griff Hazen720042b2014-02-24 15:46:56 -08003643 * <p>Values within the Bundle will replace existing extras values in this Builder.
3644 *
3645 * @see Notification#extras
3646 */
Griff Hazen959591e2014-05-15 22:26:18 -07003647 public Builder addExtras(Bundle extras) {
3648 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003650 }
3651 return this;
3652 }
3653
3654 /**
3655 * Set metadata for this notification.
3656 *
3657 * <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 -04003658 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003659 * called.
3660 *
Griff Hazen720042b2014-02-24 15:46:56 -08003661 * <p>Replaces any existing extras values with those from the provided Bundle.
3662 * Use {@link #addExtras} to merge in metadata instead.
3663 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003664 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003665 */
Griff Hazen959591e2014-05-15 22:26:18 -07003666 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003667 if (extras != null) {
3668 mUserExtras = extras;
3669 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003670 return this;
3671 }
3672
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003673 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003674 * Get the current metadata Bundle used by this notification Builder.
3675 *
3676 * <p>The returned Bundle is shared with this Builder.
3677 *
3678 * <p>The current contents of this Bundle are copied into the Notification each time
3679 * {@link #build()} is called.
3680 *
3681 * @see Notification#extras
3682 */
3683 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003684 return mUserExtras;
3685 }
3686
3687 private Bundle getAllExtras() {
3688 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3689 saveExtras.putAll(mN.extras);
3690 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003691 }
3692
3693 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003694 * Add an action to this notification. Actions are typically displayed by
3695 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003696 * <p>
3697 * Every action must have an icon (32dp square and matching the
3698 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3699 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3700 * <p>
3701 * A notification in its expanded form can display up to 3 actions, from left to right in
3702 * the order they were added. Actions will not be displayed when the notification is
3703 * collapsed, however, so be sure that any essential functions may be accessed by the user
3704 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003705 *
3706 * @param icon Resource ID of a drawable that represents the action.
3707 * @param title Text describing the action.
3708 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003709 *
3710 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003711 */
Dan Sandler86647982015-05-13 23:41:13 -04003712 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003713 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003714 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003715 return this;
3716 }
3717
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003718 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003719 * Add an action to this notification. Actions are typically displayed by
3720 * the system as a button adjacent to the notification content.
3721 * <p>
3722 * Every action must have an icon (32dp square and matching the
3723 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3724 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3725 * <p>
3726 * A notification in its expanded form can display up to 3 actions, from left to right in
3727 * the order they were added. Actions will not be displayed when the notification is
3728 * collapsed, however, so be sure that any essential functions may be accessed by the user
3729 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3730 *
3731 * @param action The action to add.
3732 */
3733 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003734 if (action != null) {
3735 mActions.add(action);
3736 }
Griff Hazen959591e2014-05-15 22:26:18 -07003737 return this;
3738 }
3739
3740 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 * Alter the complete list of actions attached to this notification.
3742 * @see #addAction(Action).
3743 *
3744 * @param actions
3745 * @return
3746 */
3747 public Builder setActions(Action... actions) {
3748 mActions.clear();
3749 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003750 if (actions[i] != null) {
3751 mActions.add(actions[i]);
3752 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003753 }
3754 return this;
3755 }
3756
3757 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003758 * Add a rich notification style to be applied at build time.
3759 *
3760 * @param style Object responsible for modifying the notification style.
3761 */
3762 public Builder setStyle(Style style) {
3763 if (mStyle != style) {
3764 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003765 if (mStyle != null) {
3766 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003767 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3768 } else {
3769 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003770 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003771 }
3772 return this;
3773 }
3774
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003775 /**
3776 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003777 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003778 * @return The same Builder.
3779 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003780 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003781 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003782 return this;
3783 }
3784
3785 /**
3786 * Supply a replacement Notification whose contents should be shown in insecure contexts
3787 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3788 * @param n A replacement notification, presumably with some or all info redacted.
3789 * @return The same Builder.
3790 */
3791 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003792 if (n != null) {
3793 mN.publicVersion = new Notification();
3794 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3795 } else {
3796 mN.publicVersion = null;
3797 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003798 return this;
3799 }
3800
Griff Hazenb720abe2014-05-20 13:15:30 -07003801 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003802 * Apply an extender to this notification builder. Extenders may be used to add
3803 * metadata or change options on this builder.
3804 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003805 public Builder extend(Extender extender) {
3806 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003807 return this;
3808 }
3809
Dan Sandler4e787062015-06-17 15:09:48 -04003810 /**
3811 * @hide
3812 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003813 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003814 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003815 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003816 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003817 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003818 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003819 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003820 }
3821
Dan Sandler26e81cf2014-05-06 10:01:27 -04003822 /**
3823 * Sets {@link Notification#color}.
3824 *
3825 * @param argb The accent color to use
3826 *
3827 * @return The same Builder.
3828 */
Tor Norbye80756e32015-03-02 09:39:27 -08003829 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003830 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003831 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003832 return this;
3833 }
3834
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003835 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003836 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3837 // This user can never be a badged profile,
3838 // and also includes USER_ALL system notifications.
3839 return null;
3840 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003841 // Note: This assumes that the current user can read the profile badge of the
3842 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003843 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003844 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003845 }
3846
3847 private Bitmap getProfileBadge() {
3848 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003849 if (badge == null) {
3850 return null;
3851 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003852 final int size = mContext.getResources().getDimensionPixelSize(
3853 R.dimen.notification_badge_size);
3854 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003855 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003856 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003857 badge.draw(canvas);
3858 return bitmap;
3859 }
3860
Selim Cinekc848c3a2016-01-13 15:27:30 -08003861 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003862 Bitmap profileBadge = getProfileBadge();
3863
Kenny Guy98193ea2014-07-24 19:54:37 +01003864 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003865 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3866 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003867 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003868 contentView.setDrawableTint(R.id.profile_badge, false,
3869 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003870 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003871 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003872 }
3873
Christoph Studerfe718432014-09-01 18:21:18 +02003874 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003875 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003876 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003877 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003878 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003879 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003880 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003881 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003882 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003883 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003884 }
3885
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003886 /**
3887 * Resets the notification header to its original state
3888 */
3889 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003890 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3891 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003892 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003893 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003894 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003895 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003896 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003897 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003898 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003899 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003900 contentView.setImageViewIcon(R.id.profile_badge, null);
3901 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003902 }
3903
3904 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003905 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3906 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003907 }
3908
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003909 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003910 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003911 }
3912
3913 /**
3914 * @param hasProgress whether the progress bar should be shown and set
3915 */
3916 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003917 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3918 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003919 }
3920
Adrian Roos70d7aa32017-01-11 15:39:06 -08003921 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003922 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003923
Christoph Studerfe718432014-09-01 18:21:18 +02003924 resetStandardTemplate(contentView);
3925
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003926 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003927 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003928 bindNotificationHeader(contentView, p.ambient);
Selim Cinek88188f22017-09-19 16:46:56 -07003929 bindLargeIcon(contentView, p.hideLargeIcon, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003930 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3931 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003932 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07003933 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08003934 if (!p.ambient) {
3935 setTextViewColorPrimary(contentView, R.id.title);
3936 }
Selim Cinek954cc232016-05-20 13:29:23 -07003937 contentView.setViewLayoutWidth(R.id.title, showProgress
3938 ? ViewGroup.LayoutParams.WRAP_CONTENT
3939 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003940 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003941 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003942 int textId = showProgress ? com.android.internal.R.id.text_line_1
3943 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07003944 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08003945 if (!p.ambient) {
3946 setTextViewColorSecondary(contentView, textId);
3947 }
Selim Cinek41598732016-01-11 16:58:37 -08003948 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003949 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003950
Selim Cinek279fa862016-06-14 10:57:25 -07003951 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003952
Selim Cinek29603462015-11-17 19:04:39 -08003953 return contentView;
3954 }
3955
Selim Cinek48f66b72017-08-18 16:17:51 -07003956 private CharSequence processTextSpans(CharSequence text) {
3957 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07003958 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07003959 }
3960 return text;
3961 }
3962
Selim Cinek7b9605b2017-01-19 17:36:00 -08003963 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3964 ensureColors();
3965 contentView.setTextColor(id, mPrimaryTextColor);
3966 }
3967
Selim Cinek48f66b72017-08-18 16:17:51 -07003968 private boolean hasForegroundColor() {
3969 return mForegroundColor != COLOR_INVALID;
3970 }
3971
Selim Cinek389edcd2017-05-11 19:16:44 -07003972 /**
3973 * @return the primary text color
3974 * @hide
3975 */
3976 @VisibleForTesting
3977 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003978 ensureColors();
3979 return mPrimaryTextColor;
3980 }
3981
Selim Cinek389edcd2017-05-11 19:16:44 -07003982 /**
3983 * @return the secondary text color
3984 * @hide
3985 */
3986 @VisibleForTesting
3987 public int getSecondaryTextColor() {
3988 ensureColors();
3989 return mSecondaryTextColor;
3990 }
3991
Selim Cinek7b9605b2017-01-19 17:36:00 -08003992 private int getActionBarColor() {
3993 ensureColors();
3994 return mActionBarColor;
3995 }
3996
Selim Cinek622c64a2017-04-17 17:10:05 -07003997 private int getActionBarColorDeEmphasized() {
3998 int backgroundColor = getBackgroundColor();
3999 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4000 }
4001
Selim Cinek7b9605b2017-01-19 17:36:00 -08004002 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4003 ensureColors();
4004 contentView.setTextColor(id, mSecondaryTextColor);
4005 }
4006
4007 private void ensureColors() {
4008 int backgroundColor = getBackgroundColor();
4009 if (mPrimaryTextColor == COLOR_INVALID
4010 || mSecondaryTextColor == COLOR_INVALID
4011 || mActionBarColor == COLOR_INVALID
4012 || mTextColorsAreForBackground != backgroundColor) {
4013 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004014 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004015 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4016 backgroundColor);
4017 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4018 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004019 if (backgroundColor != COLOR_DEFAULT
4020 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4021 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4022 mPrimaryTextColor, backgroundColor, 4.5);
4023 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4024 mSecondaryTextColor, backgroundColor, 4.5);
4025 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004026 } else {
4027 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4028 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4029 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4030 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004031 // We only respect the given colors if worst case Black or White still has
4032 // contrast
4033 boolean backgroundLight = backLum > textLum
4034 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4035 || backLum <= textLum
4036 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004037 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004038 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004039 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4040 mForegroundColor,
4041 backgroundColor,
4042 true /* findFG */,
4043 4.5f);
4044 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004045 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004046 } else {
4047 mSecondaryTextColor =
4048 NotificationColorUtil.findContrastColorAgainstDark(
4049 mForegroundColor,
4050 backgroundColor,
4051 true /* findFG */,
4052 4.5f);
4053 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004054 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004055 }
4056 } else {
4057 mPrimaryTextColor = mForegroundColor;
4058 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004059 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4060 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004061 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4062 backgroundColor) < 4.5f) {
4063 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004064 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004065 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4066 mSecondaryTextColor,
4067 backgroundColor,
4068 true /* findFG */,
4069 4.5f);
4070 } else {
4071 mSecondaryTextColor
4072 = NotificationColorUtil.findContrastColorAgainstDark(
4073 mSecondaryTextColor,
4074 backgroundColor,
4075 true /* findFG */,
4076 4.5f);
4077 }
4078 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004079 mSecondaryTextColor, backgroundLight
4080 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4081 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004082 }
4083 }
4084 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004085 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4086 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004087 }
4088 }
4089
4090 private void updateBackgroundColor(RemoteViews contentView) {
4091 if (isColorized()) {
4092 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4093 getBackgroundColor());
4094 } else {
4095 // Clear it!
4096 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4097 0);
4098 }
4099 }
4100
Selim Cinek860b6da2015-12-16 19:02:19 -08004101 /**
4102 * @param remoteView the remote view to update the minheight in
4103 * @param hasMinHeight does it have a mimHeight
4104 * @hide
4105 */
4106 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4107 int minHeight = 0;
4108 if (hasMinHeight) {
4109 // we need to set the minHeight of the notification
4110 minHeight = mContext.getResources().getDimensionPixelSize(
4111 com.android.internal.R.dimen.notification_min_content_height);
4112 }
4113 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4114 }
4115
Selim Cinek29603462015-11-17 19:04:39 -08004116 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004117 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4118 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4119 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4120 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004121 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004122 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004123 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004124 contentView.setProgressBackgroundTintList(
4125 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4126 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004127 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004128 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004129 contentView.setProgressTintList(R.id.progress, colorStateList);
4130 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004131 }
Selim Cinek29603462015-11-17 19:04:39 -08004132 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004133 } else {
4134 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004135 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004136 }
Joe Onorato561d3852010-11-20 18:09:34 -08004137 }
4138
Selim Cinek88188f22017-09-19 16:46:56 -07004139 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4140 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004141 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4142 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4143 }
Selim Cinek88188f22017-09-19 16:46:56 -07004144 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4145 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004146 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4147 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4148 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004149 int endMargin = R.dimen.notification_content_picture_margin;
4150 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4151 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4152 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004153 }
4154 // Bind the reply action
4155 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004156
Selim Cinek88188f22017-09-19 16:46:56 -07004157 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4158 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4159 if (actionVisible) {
4160 // We're only showing the icon as big if we're hiding the large icon
4161 int contrastColor = resolveContrastColor();
4162 int iconColor;
4163 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004164 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004165 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004166 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004167 contentView.setOnClickPendingIntent(R.id.right_icon,
4168 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004169 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004170 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4171 ? Color.BLACK : Color.WHITE;
4172 } else {
4173 contentView.setImageViewResource(R.id.right_icon,
4174 R.drawable.ic_reply_notification_large);
4175 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4176 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004177 }
Selim Cinek88188f22017-09-19 16:46:56 -07004178 contentView.setDrawableTint(replyId,
4179 false /* targetBackground */,
4180 iconColor,
4181 PorterDuff.Mode.SRC_ATOP);
4182 contentView.setOnClickPendingIntent(replyId,
4183 action.actionIntent);
4184 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4185 } else {
4186 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004187 }
Selim Cinek88188f22017-09-19 16:46:56 -07004188 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4189 ? View.VISIBLE
4190 : View.GONE);
4191 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004192 ? View.VISIBLE
4193 : View.GONE);
4194 }
4195
4196 private Action findReplyAction() {
4197 ArrayList<Action> actions = mActions;
4198 if (mOriginalActions != null) {
4199 actions = mOriginalActions;
4200 }
4201 int numActions = actions.size();
4202 for (int i = 0; i < numActions; i++) {
4203 Action action = actions.get(i);
4204 if (hasValidRemoteInput(action)) {
4205 return action;
4206 }
4207 }
4208 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004209 }
4210
Adrian Roos487374f2017-01-11 15:48:14 -08004211 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4212 bindSmallIcon(contentView, ambient);
4213 bindHeaderAppName(contentView, ambient);
4214 if (!ambient) {
4215 // Ambient view does not have these
4216 bindHeaderText(contentView);
4217 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004218 bindProfileBadge(contentView);
4219 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004220 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004221 }
4222
4223 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004224 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004225 contentView.setDrawableTint(R.id.expand_button, false, color,
4226 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004227 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004228 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004229 }
4230
Selim Cinek99104832017-01-25 14:47:33 -08004231 /**
4232 * @return the color that is used as the first primary highlight color. This is applied
4233 * in several places like the action buttons or the app name in the header.
4234 */
4235 private int getPrimaryHighlightColor() {
4236 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4237 }
4238
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004239 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4240 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004241 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004242 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004243 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4244 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4245 contentView.setLong(R.id.chronometer, "setBase",
4246 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4247 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004248 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004249 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004250 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004251 } else {
4252 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4253 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004254 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004255 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004256 } else {
4257 // We still want a time to be set but gone, such that we can show and hide it
4258 // on demand in case it's a child notification without anything in the header
4259 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004260 }
4261 }
4262
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004263 private void bindHeaderText(RemoteViews contentView) {
4264 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4265 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004266 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004267 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004268 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004269 if (headerText == null
4270 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4271 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4272 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4273 }
4274 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004275 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004276 contentView.setTextViewText(R.id.header_text, processTextSpans(
4277 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004278 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004279 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4280 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004281 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004282 }
4283 }
4284
Adrian Rooseba05822016-04-22 17:09:27 -07004285 /**
4286 * @hide
4287 */
4288 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004289 CharSequence name = null;
4290 final PackageManager pm = mContext.getPackageManager();
4291 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4292 // only system packages which lump together a bunch of unrelated stuff
4293 // may substitute a different name to make the purpose of the
4294 // notification more clear. the correct package label should always
4295 // be accessible via SystemUI.
4296 final String pkg = mContext.getPackageName();
4297 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4298 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4299 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4300 name = subName;
4301 } else {
4302 Log.w(TAG, "warning: pkg "
4303 + pkg + " attempting to substitute app name '" + subName
4304 + "' without holding perm "
4305 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4306 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004307 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004308 if (TextUtils.isEmpty(name)) {
4309 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4310 }
4311 if (TextUtils.isEmpty(name)) {
4312 // still nothing?
4313 return null;
4314 }
4315
4316 return String.valueOf(name);
4317 }
Adrian Roos487374f2017-01-11 15:48:14 -08004318 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004319 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004320 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004321 setTextViewColorPrimary(contentView, R.id.app_name_text);
4322 } else {
4323 contentView.setTextColor(R.id.app_name_text,
4324 ambient ? resolveAmbientColor() : resolveContrastColor());
4325 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004326 }
4327
Adrian Roos487374f2017-01-11 15:48:14 -08004328 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004329 if (mN.mSmallIcon == null && mN.icon != 0) {
4330 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4331 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004332 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004333 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004334 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004335 }
4336
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004337 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004338 * @return true if the built notification will show the time or the chronometer; false
4339 * otherwise
4340 */
4341 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004342 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004343 }
4344
Christoph Studerfe718432014-09-01 18:21:18 +02004345 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004346 // actions_container is only reset when there are no actions to avoid focus issues with
4347 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004348 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004349 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004350
4351 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4352 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4353
4354 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4355 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4356 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4357 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004358
4359 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004360 }
4361
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004362 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004363 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004364 }
4365
Adrian Roos70d7aa32017-01-11 15:39:06 -08004366 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4367 StandardTemplateParams p) {
4368 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004369
Christoph Studerfe718432014-09-01 18:21:18 +02004370 resetStandardTemplateWithActions(big);
4371
Adrian Roose458aa82015-12-08 16:17:19 -08004372 boolean validRemoteInput = false;
4373
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004374 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004375 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004376 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004377 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004378 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004379 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004380 if (p.ambient) {
4381 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004382 } else if (isColorized()) {
4383 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4384 } else {
4385 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4386 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004387 }
Adrian Roosf852a422016-06-03 13:33:43 -07004388 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4389 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004390 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004391 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004392 Action action = mActions.get(i);
4393 validRemoteInput |= hasValidRemoteInput(action);
4394
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004395 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004396 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004397 big.addView(R.id.actions, button);
4398 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004399 } else {
4400 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004401 }
Adrian Roose458aa82015-12-08 16:17:19 -08004402
4403 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004404 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004405 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4406 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004407 big.setTextViewText(R.id.notification_material_reply_text_1,
4408 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004409 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004410
4411 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4412 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004413 big.setTextViewText(R.id.notification_material_reply_text_2,
4414 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004415 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004416
4417 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4418 big.setViewVisibility(
4419 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004420 big.setTextViewText(R.id.notification_material_reply_text_3,
4421 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004422 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004423 }
4424 }
4425 }
4426
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004427 return big;
4428 }
4429
Adrian Roose458aa82015-12-08 16:17:19 -08004430 private boolean hasValidRemoteInput(Action action) {
4431 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4432 // Weird actions
4433 return false;
4434 }
4435
4436 RemoteInput[] remoteInputs = action.getRemoteInputs();
4437 if (remoteInputs == null) {
4438 return false;
4439 }
4440
4441 for (RemoteInput r : remoteInputs) {
4442 CharSequence[] choices = r.getChoices();
4443 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4444 return true;
4445 }
4446 }
4447 return false;
4448 }
4449
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004450 /**
4451 * Construct a RemoteViews for the final 1U notification layout. In order:
4452 * 1. Custom contentView from the caller
4453 * 2. Style's proposed content view
4454 * 3. Standard template view
4455 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004456 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004457 return createContentView(false /* increasedheight */ );
4458 }
4459
4460 /**
4461 * Construct a RemoteViews for the smaller content view.
4462 *
4463 * @param increasedHeight true if this layout be created with an increased height. Some
4464 * styles may support showing more then just that basic 1U size
4465 * and the system may decide to render important notifications
4466 * slightly bigger even when collapsed.
4467 *
4468 * @hide
4469 */
4470 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004471 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004472 return mN.contentView;
4473 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004474 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004475 if (styleView != null) {
4476 return styleView;
4477 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004478 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004479 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004480 }
4481
Selim Cineka7679b62017-05-10 16:33:25 -07004482 private boolean useExistingRemoteView() {
4483 return mStyle == null || (!mStyle.displayCustomViewInline()
4484 && !mRebuildStyledRemoteViews);
4485 }
4486
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004487 /**
4488 * Construct a RemoteViews for the final big notification layout.
4489 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004490 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004491 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004492 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004493 return mN.bigContentView;
4494 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004495 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004496 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004497 } else if (mActions.size() != 0) {
4498 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004499 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004500 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004501 return result;
4502 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004503
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004504 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004505 * Construct a RemoteViews for the final notification header only. This will not be
4506 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004507 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004508 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004509 * @hide
4510 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004511 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004512 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4513 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004514 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004515 ambient ? R.layout.notification_template_ambient_header
4516 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004517 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004518 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004519 if (colorized != null) {
4520 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4521 } else {
4522 mN.extras.remove(EXTRA_COLORIZED);
4523 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004524 return header;
4525 }
4526
Adrian Roos487374f2017-01-11 15:48:14 -08004527 /**
4528 * Construct a RemoteViews for the ambient version of the notification.
4529 *
4530 * @hide
4531 */
4532 public RemoteViews makeAmbientNotification() {
4533 RemoteViews ambient = applyStandardTemplateWithActions(
4534 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004535 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004536 return ambient;
4537 }
4538
Selim Cinek29603462015-11-17 19:04:39 -08004539 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004540 if (result != null) {
4541 result.setViewVisibility(R.id.text_line_1, View.GONE);
4542 }
Selim Cinek29603462015-11-17 19:04:39 -08004543 }
4544
Selim Cinek6743c0b2017-01-18 18:24:01 -08004545 /**
4546 * Adapt the Notification header if this view is used as an expanded view.
4547 *
4548 * @hide
4549 */
4550 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004551 if (result != null) {
4552 result.setBoolean(R.id.notification_header, "setExpanded", true);
4553 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004554 }
4555
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004556 /**
4557 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004558 *
4559 * @param increasedHeight true if this layout be created with an increased height. Some
4560 * styles may support showing more then just that basic 1U size
4561 * and the system may decide to render important notifications
4562 * slightly bigger even when collapsed.
4563 *
4564 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004565 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004566 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004567 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004568 return mN.headsUpContentView;
4569 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004570 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4571 if (styleView != null) {
4572 return styleView;
4573 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004574 } else if (mActions.size() == 0) {
4575 return null;
4576 }
4577
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004578 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004579 }
4580
Selim Cinek624c02db2015-12-14 21:00:02 -08004581 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004582 * Construct a RemoteViews for the final heads-up notification layout.
4583 */
4584 public RemoteViews createHeadsUpContentView() {
4585 return createHeadsUpContentView(false /* useIncreasedHeight */);
4586 }
4587
4588 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004589 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4590 *
4591 * @hide
4592 */
4593 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004594 return makePublicView(false /* ambient */);
4595 }
4596
4597 /**
4598 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4599 *
4600 * @hide
4601 */
4602 public RemoteViews makePublicAmbientNotification() {
4603 return makePublicView(true /* ambient */);
4604 }
4605
4606 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004607 if (mN.publicVersion != null) {
4608 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004609 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004610 }
4611 Bundle savedBundle = mN.extras;
4612 Style style = mStyle;
4613 mStyle = null;
4614 Icon largeIcon = mN.mLargeIcon;
4615 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004616 Bitmap largeIconLegacy = mN.largeIcon;
4617 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004618 ArrayList<Action> actions = mActions;
4619 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004620 Bundle publicExtras = new Bundle();
4621 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4622 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4623 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4624 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004625 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4626 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004627 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004628 RemoteViews view;
4629 if (ambient) {
4630 publicExtras.putCharSequence(EXTRA_TITLE,
4631 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4632 view = makeAmbientNotification();
4633 } else{
4634 view = makeNotificationHeader(false /* ambient */);
4635 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4636 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004637 mN.extras = savedBundle;
4638 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004639 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004640 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004641 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004642 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004643 }
4644
Selim Cinek6743c0b2017-01-18 18:24:01 -08004645 /**
4646 * Construct a content view for the display when low - priority
4647 *
4648 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4649 * a new subtext is created consisting of the content of the
4650 * notification.
4651 * @hide
4652 */
4653 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4654 int color = mN.color;
4655 mN.color = COLOR_DEFAULT;
4656 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4657 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4658 CharSequence newSummary = createSummaryText();
4659 if (!TextUtils.isEmpty(newSummary)) {
4660 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4661 }
4662 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004663
Adrian Roos6f6e1592017-05-02 16:22:53 -07004664 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004665 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004666 if (summary != null) {
4667 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4668 } else {
4669 mN.extras.remove(EXTRA_SUB_TEXT);
4670 }
4671 mN.color = color;
4672 return header;
4673 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004674
Selim Cinek6743c0b2017-01-18 18:24:01 -08004675 private CharSequence createSummaryText() {
4676 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4677 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4678 return titleText;
4679 }
4680 SpannableStringBuilder summary = new SpannableStringBuilder();
4681 if (titleText == null) {
4682 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4683 }
4684 BidiFormatter bidi = BidiFormatter.getInstance();
4685 if (titleText != null) {
4686 summary.append(bidi.unicodeWrap(titleText));
4687 }
4688 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4689 if (titleText != null && contentText != null) {
4690 summary.append(bidi.unicodeWrap(mContext.getText(
4691 R.string.notification_header_divider_symbol_with_spaces)));
4692 }
4693 if (contentText != null) {
4694 summary.append(bidi.unicodeWrap(contentText));
4695 }
4696 return summary;
4697 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004698
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004699 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004700 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004701 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004702 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004703 emphazisedMode ? getEmphasizedActionLayoutResource()
4704 : tombstone ? getActionTombstoneLayoutResource()
4705 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004706 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004707 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004708 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004709 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004710 if (action.mRemoteInputs != null) {
4711 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4712 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004713 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004714 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004715 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004716 int bgColor;
4717 if (isColorized()) {
4718 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4719 } else {
4720 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4721 : R.color.notification_action_list_dark);
4722 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004723 button.setDrawableTint(R.id.button_holder, true,
4724 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004725 CharSequence title = action.title;
4726 ColorStateList[] outResultColor = null;
4727 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004728 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004729 } else {
4730 outResultColor = new ColorStateList[1];
4731 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4732 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004733 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004734 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004735 if (outResultColor != null && outResultColor[0] != null) {
4736 // We need to set the text color as well since changing a text to uppercase
4737 // clears its spans.
4738 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004739 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004740 button.setTextColor(R.id.action0,resolveContrastColor());
4741 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004742 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004743 button.setTextViewText(R.id.action0, processTextSpans(
4744 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004745 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004746 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004747 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004748 button.setTextColor(R.id.action0,
4749 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004750 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004751 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004752 return button;
4753 }
4754
Joe Onoratocb109a02011-01-18 17:57:41 -08004755 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004756 * Ensures contrast on color spans against a background color. also returns the color of the
4757 * text if a span was found that spans over the whole text.
4758 *
4759 * @param charSequence the charSequence on which the spans are
4760 * @param background the background color to ensure the contrast against
4761 * @param outResultColor an array in which a color will be returned as the first element if
4762 * there exists a full length color span.
4763 * @return the contrasted charSequence
4764 */
4765 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4766 ColorStateList[] outResultColor) {
4767 if (charSequence instanceof Spanned) {
4768 Spanned ss = (Spanned) charSequence;
4769 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4770 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4771 for (Object span : spans) {
4772 Object resultSpan = span;
4773 int spanStart = ss.getSpanStart(span);
4774 int spanEnd = ss.getSpanEnd(span);
4775 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4776 if (resultSpan instanceof CharacterStyle) {
4777 resultSpan = ((CharacterStyle) span).getUnderlying();
4778 }
4779 if (resultSpan instanceof TextAppearanceSpan) {
4780 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4781 ColorStateList textColor = originalSpan.getTextColor();
4782 if (textColor != null) {
4783 int[] colors = textColor.getColors();
4784 int[] newColors = new int[colors.length];
4785 for (int i = 0; i < newColors.length; i++) {
4786 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004787 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004788 }
4789 textColor = new ColorStateList(textColor.getStates().clone(),
4790 newColors);
4791 resultSpan = new TextAppearanceSpan(
4792 originalSpan.getFamily(),
4793 originalSpan.getTextStyle(),
4794 originalSpan.getTextSize(),
4795 textColor,
4796 originalSpan.getLinkTextColor());
4797 if (fullLength) {
4798 outResultColor[0] = new ColorStateList(
4799 textColor.getStates().clone(), newColors);
4800 }
4801 }
4802 } else if (resultSpan instanceof ForegroundColorSpan) {
4803 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4804 int foregroundColor = originalSpan.getForegroundColor();
4805 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004806 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004807 resultSpan = new ForegroundColorSpan(foregroundColor);
4808 if (fullLength) {
4809 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4810 }
4811 } else {
4812 resultSpan = span;
4813 }
4814
4815 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4816 }
4817 return builder;
4818 }
4819 return charSequence;
4820 }
4821
4822 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004823 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004824 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004825 */
4826 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004827 if (!mIsLegacyInitialized) {
4828 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4829 < Build.VERSION_CODES.LOLLIPOP;
4830 mIsLegacyInitialized = true;
4831 }
4832 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004833 }
4834
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004835 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004836 return processLegacyText(charSequence, false /* ambient */);
4837 }
4838
4839 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4840 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4841 boolean wantLightText = ambient;
4842 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004843 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004844 } else {
4845 return charSequence;
4846 }
4847 }
4848
Dan Sandler26e81cf2014-05-06 10:01:27 -04004849 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004850 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004851 */
Adrian Roos487374f2017-01-11 15:48:14 -08004852 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4853 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004854 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004855 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004856 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004857 contentView.setDrawableTint(R.id.icon, false, color,
4858 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004859
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004860 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004861 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004862 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004863 }
4864
Dan Sandler26e81cf2014-05-06 10:01:27 -04004865 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004866 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004867 * if it's grayscale).
4868 */
4869 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004870 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4871 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004872 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004873 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004874 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4875 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004876 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004877 }
4878
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004879 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004880 if (mN.color != COLOR_DEFAULT) {
4881 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004882 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004883 }
4884
Adrian Roos4ff3b122016-02-01 12:26:13 -08004885 int resolveContrastColor() {
4886 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4887 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004888 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004889
Selim Cinekac5f0272017-05-02 16:05:41 -07004890 int color;
4891 int background = mBackgroundColorHint;
4892 if (mBackgroundColorHint == COLOR_INVALID) {
4893 background = mContext.getColor(
4894 com.android.internal.R.color.notification_material_background_color);
4895 }
4896 if (mN.color == COLOR_DEFAULT) {
4897 ensureColors();
4898 color = mSecondaryTextColor;
4899 } else {
4900 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004901 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004902 }
4903 if (Color.alpha(color) < 255) {
4904 // alpha doesn't go well for color filters, so let's blend it manually
4905 color = NotificationColorUtil.compositeColors(color, background);
4906 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004907 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004908 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004909 }
4910
Adrian Roos487374f2017-01-11 15:48:14 -08004911 int resolveAmbientColor() {
4912 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4913 return mCachedAmbientColor;
4914 }
4915 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4916
4917 mCachedAmbientColorIsFor = mN.color;
4918 return mCachedAmbientColor = contrasted;
4919 }
4920
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004921 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004922 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004923 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004924 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004925 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004926 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004927 mN.actions = new Action[mActions.size()];
4928 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004929 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004930 if (!mPersonList.isEmpty()) {
4931 mN.extras.putStringArray(EXTRA_PEOPLE,
4932 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004933 }
Selim Cinek247fa012016-02-18 09:50:48 -08004934 if (mN.bigContentView != null || mN.contentView != null
4935 || mN.headsUpContentView != null) {
4936 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4937 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004938 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004939 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004940
Julia Reynolds3b848122016-02-26 10:45:32 -05004941 /**
4942 * Creates a Builder from an existing notification so further changes can be made.
4943 * @param context The context for your application / activity.
4944 * @param n The notification to create a Builder from.
4945 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004946 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004947 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004948 ApplicationInfo applicationInfo = n.extras.getParcelable(
4949 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004950 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004951 if (applicationInfo != null) {
4952 try {
4953 builderContext = context.createApplicationContext(applicationInfo,
4954 Context.CONTEXT_RESTRICTED);
4955 } catch (NameNotFoundException e) {
4956 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4957 builderContext = context; // try with our context
4958 }
4959 } else {
4960 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004961 }
4962
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004963 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004964 }
4965
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004966 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004967 * @deprecated Use {@link #build()} instead.
4968 */
4969 @Deprecated
4970 public Notification getNotification() {
4971 return build();
4972 }
4973
4974 /**
4975 * Combine all of the options that have been set and return a new {@link Notification}
4976 * object.
4977 */
4978 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004979 // first, add any extras from the calling code
4980 if (mUserExtras != null) {
4981 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004982 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004983
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004984 mN.creationTime = System.currentTimeMillis();
4985
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004986 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004987 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004988
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004989 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004990
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004991 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02004992 mStyle.reduceImageSizes(mContext);
4993 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004994 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004995 }
Selim Cinekd0426622017-07-11 13:19:59 +02004996 mN.reduceImageSizes(mContext);
4997
Adrian Roos5081c0d2016-02-26 16:04:19 -08004998 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004999 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005000 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005001 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005002 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5003 mN.contentView.getSequenceNumber());
5004 }
5005 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005006 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005007 if (mN.bigContentView != null) {
5008 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5009 mN.bigContentView.getSequenceNumber());
5010 }
5011 }
5012 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005013 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005014 if (mN.headsUpContentView != null) {
5015 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5016 mN.headsUpContentView.getSequenceNumber());
5017 }
5018 }
5019 }
5020
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005021 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5022 mN.flags |= FLAG_SHOW_LIGHTS;
5023 }
5024
Adrian Roosfb921842017-10-26 14:49:56 +02005025 mN.allPendingIntents = null;
5026
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005027 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005028 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005029
5030 /**
5031 * Apply this Builder to an existing {@link Notification} object.
5032 *
5033 * @hide
5034 */
5035 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005036 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005037 return n;
5038 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005039
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005040 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005041 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005042 * change. Also removes extenders on low ram devices, as
5043 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005044 *
5045 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5046 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005047 * @hide
5048 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005049 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005050 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005051
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005052 // Only strip views for known Styles because we won't know how to
5053 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005054 if (!isLowRam
5055 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005056 && getNotificationStyleClass(templateClass) == null) {
5057 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005058 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005059
5060 // Only strip unmodified BuilderRemoteViews.
5061 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005062 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005063 n.contentView.getSequenceNumber();
5064 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005065 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005066 n.bigContentView.getSequenceNumber();
5067 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005068 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005069 n.headsUpContentView.getSequenceNumber();
5070
5071 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005072 if (!isLowRam
5073 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005074 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005075 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005076
5077 Notification clone = n.clone();
5078 if (stripContentView) {
5079 clone.contentView = null;
5080 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5081 }
5082 if (stripBigContentView) {
5083 clone.bigContentView = null;
5084 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5085 }
5086 if (stripHeadsUpContentView) {
5087 clone.headsUpContentView = null;
5088 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5089 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005090 if (isLowRam) {
5091 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5092 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5093 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5094 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005095 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005096 }
5097
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005098 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005099 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005100 }
5101
5102 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005103 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005104 }
5105
5106 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005107 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005108 }
5109
5110 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005111 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005112 }
5113
5114 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005115 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005116 }
5117
Adrian Roosc1a80b02016-04-05 14:54:55 -07005118 private int getMessagingLayoutResource() {
5119 return R.layout.notification_template_material_messaging;
5120 }
5121
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005122 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005123 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005124 }
5125
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005126 private int getEmphasizedActionLayoutResource() {
5127 return R.layout.notification_material_action_emphasized;
5128 }
5129
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005130 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005131 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005132 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005133
5134 private int getBackgroundColor() {
5135 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005136 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005137 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005138 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5139 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005140 }
5141 }
5142
5143 private boolean isColorized() {
5144 return mN.isColorized();
5145 }
Selim Cinek99104832017-01-25 14:47:33 -08005146
Anthony Chenad4d1582017-04-10 16:07:58 -07005147 private boolean shouldTintActionButtons() {
5148 return mTintActionButtons;
5149 }
5150
Selim Cinek99104832017-01-25 14:47:33 -08005151 private boolean textColorsNeedInversion() {
5152 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5153 return false;
5154 }
5155 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5156 return targetSdkVersion > Build.VERSION_CODES.M
5157 && targetSdkVersion < Build.VERSION_CODES.O;
5158 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005159
5160 /**
5161 * Set a color palette to be used as the background and textColors
5162 *
5163 * @param backgroundColor the color to be used as the background
5164 * @param foregroundColor the color to be used as the foreground
5165 *
5166 * @hide
5167 */
5168 public void setColorPalette(int backgroundColor, int foregroundColor) {
5169 mBackgroundColor = backgroundColor;
5170 mForegroundColor = foregroundColor;
5171 mTextColorsAreForBackground = COLOR_INVALID;
5172 ensureColors();
5173 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005174
5175 /**
5176 * Sets the background color for this notification to be a different one then the default.
5177 * This is mainly used to calculate contrast and won't necessarily be applied to the
5178 * background.
5179 *
5180 * @hide
5181 */
5182 public void setBackgroundColorHint(int backgroundColor) {
5183 mBackgroundColorHint = backgroundColor;
5184 }
Selim Cineka7679b62017-05-10 16:33:25 -07005185
5186
5187 /**
5188 * Forces all styled remoteViews to be built from scratch and not use any cached
5189 * RemoteViews.
5190 * This is needed for legacy apps that are baking in their remoteviews into the
5191 * notification.
5192 *
5193 * @hide
5194 */
5195 public void setRebuildStyledRemoteViews(boolean rebuild) {
5196 mRebuildStyledRemoteViews = rebuild;
5197 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005198 }
5199
5200 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005201 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5202 * remote views.
5203 *
5204 * @hide
5205 */
5206 void reduceImageSizes(Context context) {
5207 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5208 return;
5209 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005210 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005211 if (mLargeIcon != null || largeIcon != null) {
5212 Resources resources = context.getResources();
5213 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005214 int maxWidth = resources.getDimensionPixelSize(isLowRam
5215 ? R.dimen.notification_right_icon_size_low_ram
5216 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005217 int maxHeight = maxWidth;
5218 if (MediaStyle.class.equals(style)
5219 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005220 maxHeight = resources.getDimensionPixelSize(isLowRam
5221 ? R.dimen.notification_media_image_max_height_low_ram
5222 : R.dimen.notification_media_image_max_height);
5223 maxWidth = resources.getDimensionPixelSize(isLowRam
5224 ? R.dimen.notification_media_image_max_width_low_ram
5225 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005226 }
5227 if (mLargeIcon != null) {
5228 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5229 }
5230 if (largeIcon != null) {
5231 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5232 }
5233 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005234 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5235 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5236 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005237 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5238 }
5239
Selim Cineka8cb1262017-08-15 16:53:44 -07005240 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5241 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005242 if (remoteView != null) {
5243 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005244 int maxWidth = resources.getDimensionPixelSize(isLowRam
5245 ? R.dimen.notification_custom_view_max_image_width_low_ram
5246 : R.dimen.notification_custom_view_max_image_width);
5247 int maxHeight = resources.getDimensionPixelSize(isLowRam
5248 ? R.dimen.notification_custom_view_max_image_height_low_ram
5249 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005250 remoteView.reduceImageSizes(maxWidth, maxHeight);
5251 }
5252 }
5253
5254 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005255 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005256 */
Selim Cinek22714f12017-04-13 16:23:53 -07005257 private boolean isForegroundService() {
5258 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005259 }
5260
5261 /**
Selim Cinek99104832017-01-25 14:47:33 -08005262 * @return whether this notification has a media session attached
5263 * @hide
5264 */
5265 public boolean hasMediaSession() {
5266 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5267 }
5268
5269 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005270 * @return the style class of this notification
5271 * @hide
5272 */
5273 public Class<? extends Notification.Style> getNotificationStyle() {
5274 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5275
5276 if (!TextUtils.isEmpty(templateClass)) {
5277 return Notification.getNotificationStyleClass(templateClass);
5278 }
5279 return null;
5280 }
5281
5282 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005283 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005284 *
5285 * @hide
5286 */
5287 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005288 if (isColorizedMedia()) {
5289 return true;
5290 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005291 return extras.getBoolean(EXTRA_COLORIZED)
5292 && (hasColorizedPermission() || isForegroundService());
5293 }
5294
5295 /**
5296 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5297 * permission. The permission is checked when a notification is enqueued.
5298 */
5299 private boolean hasColorizedPermission() {
5300 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005301 }
5302
5303 /**
5304 * @return true if this notification is colorized and it is a media notification
5305 *
5306 * @hide
5307 */
5308 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005309 Class<? extends Style> style = getNotificationStyle();
5310 if (MediaStyle.class.equals(style)) {
5311 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5312 if ((colorized == null || colorized) && hasMediaSession()) {
5313 return true;
5314 }
5315 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5316 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5317 return true;
5318 }
5319 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005320 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005321 }
5322
Selim Cinek0847acd2017-04-24 19:48:29 -07005323
5324 /**
5325 * @return true if this is a media notification
5326 *
5327 * @hide
5328 */
5329 public boolean isMediaNotification() {
5330 Class<? extends Style> style = getNotificationStyle();
5331 if (MediaStyle.class.equals(style)) {
5332 return true;
5333 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5334 return true;
5335 }
5336 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005337 }
5338
Selim Cinek279fa862016-06-14 10:57:25 -07005339 private boolean hasLargeIcon() {
5340 return mLargeIcon != null || largeIcon != null;
5341 }
5342
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005343 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005344 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005345 * @hide
5346 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005347 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005348 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5349 }
5350
5351 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005352 * @return true if the notification will show a chronometer; false otherwise
5353 * @hide
5354 */
5355 public boolean showsChronometer() {
5356 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5357 }
5358
5359 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005360 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005361 */
5362 @SystemApi
5363 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5364 Class<? extends Style>[] classes = new Class[] {
5365 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5366 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5367 MessagingStyle.class };
5368 for (Class<? extends Style> innerClass : classes) {
5369 if (templateClass.equals(innerClass.getName())) {
5370 return innerClass;
5371 }
5372 }
5373 return null;
5374 }
5375
5376 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005377 * An object that can apply a rich notification style to a {@link Notification.Builder}
5378 * object.
5379 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005380 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005381 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005382
5383 /**
5384 * @hide
5385 */
5386 protected CharSequence mSummaryText = null;
5387
5388 /**
5389 * @hide
5390 */
5391 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005392
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005393 protected Builder mBuilder;
5394
Chris Wrend6297db2012-05-03 16:20:13 -04005395 /**
5396 * Overrides ContentTitle in the big form of the template.
5397 * This defaults to the value passed to setContentTitle().
5398 */
5399 protected void internalSetBigContentTitle(CharSequence title) {
5400 mBigContentTitle = title;
5401 }
5402
5403 /**
5404 * Set the first line of text after the detail section in the big form of the template.
5405 */
5406 protected void internalSetSummaryText(CharSequence cs) {
5407 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005408 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005409 }
5410
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005411 public void setBuilder(Builder builder) {
5412 if (mBuilder != builder) {
5413 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005414 if (mBuilder != null) {
5415 mBuilder.setStyle(this);
5416 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005417 }
5418 }
5419
Chris Wrend6297db2012-05-03 16:20:13 -04005420 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005421 if (mBuilder == null) {
5422 throw new IllegalArgumentException("Style requires a valid Builder object");
5423 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005424 }
Chris Wrend6297db2012-05-03 16:20:13 -04005425
5426 protected RemoteViews getStandardView(int layoutId) {
5427 checkBuilder();
5428
Christoph Studer4600f9b2014-07-22 22:44:43 +02005429 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005430 CharSequence oldBuilderContentTitle =
5431 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005432 if (mBigContentTitle != null) {
5433 mBuilder.setContentTitle(mBigContentTitle);
5434 }
5435
Chris Wrend6297db2012-05-03 16:20:13 -04005436 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5437
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005438 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005439
Chris Wrend6297db2012-05-03 16:20:13 -04005440 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5441 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005442 } else {
5443 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005444 }
5445
Chris Wrend6297db2012-05-03 16:20:13 -04005446 return contentView;
5447 }
5448
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005449 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005450 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005451 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005452 *
5453 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005454 * @hide
5455 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005456 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005457 return null;
5458 }
5459
5460 /**
5461 * Construct a Style-specific RemoteViews for the final big notification layout.
5462 * @hide
5463 */
5464 public RemoteViews makeBigContentView() {
5465 return null;
5466 }
5467
5468 /**
5469 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005470 *
5471 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005472 * @hide
5473 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005474 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005475 return null;
5476 }
5477
5478 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005479 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005480 * @hide
5481 */
5482 public void addExtras(Bundle extras) {
5483 if (mSummaryTextSet) {
5484 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5485 }
5486 if (mBigContentTitle != null) {
5487 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5488 }
Chris Wren91ad5632013-06-05 15:05:57 -04005489 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005490 }
5491
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005492 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005493 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005494 * @hide
5495 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005496 protected void restoreFromExtras(Bundle extras) {
5497 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5498 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5499 mSummaryTextSet = true;
5500 }
5501 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5502 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5503 }
5504 }
5505
5506
5507 /**
5508 * @hide
5509 */
5510 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005511 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005512 return wip;
5513 }
5514
Daniel Sandler0ec46202015-06-24 01:27:05 -04005515 /**
5516 * @hide
5517 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005518 public void purgeResources() {}
5519
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005520 /**
5521 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5522 * attached to.
5523 *
5524 * @return the fully constructed Notification.
5525 */
5526 public Notification build() {
5527 checkBuilder();
5528 return mBuilder.build();
5529 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005530
5531 /**
5532 * @hide
5533 * @return true if the style positions the progress bar on the second line; false if the
5534 * style hides the progress bar
5535 */
5536 protected boolean hasProgress() {
5537 return true;
5538 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005539
5540 /**
5541 * @hide
5542 * @return Whether we should put the summary be put into the notification header
5543 */
5544 public boolean hasSummaryInHeader() {
5545 return true;
5546 }
Selim Cinek593610c2016-02-16 18:42:57 -08005547
5548 /**
5549 * @hide
5550 * @return Whether custom content views are displayed inline in the style
5551 */
5552 public boolean displayCustomViewInline() {
5553 return false;
5554 }
Selim Cinekd0426622017-07-11 13:19:59 +02005555
5556 /**
5557 * Reduces the image sizes contained in this style.
5558 *
5559 * @hide
5560 */
5561 public void reduceImageSizes(Context context) {
5562 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005563 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005564
5565 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005566 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005567 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005568 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005569 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005570 * Notification notif = new Notification.Builder(mContext)
5571 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5572 * .setContentText(subject)
5573 * .setSmallIcon(R.drawable.new_post)
5574 * .setLargeIcon(aBitmap)
5575 * .setStyle(new Notification.BigPictureStyle()
5576 * .bigPicture(aBigBitmap))
5577 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005578 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005579 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005580 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005581 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005582 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005583 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005584 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005585 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005586
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005587 public BigPictureStyle() {
5588 }
5589
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005590 /**
5591 * @deprecated use {@code BigPictureStyle()}.
5592 */
5593 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005594 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005595 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005596 }
5597
Chris Wrend6297db2012-05-03 16:20:13 -04005598 /**
5599 * Overrides ContentTitle in the big form of the template.
5600 * This defaults to the value passed to setContentTitle().
5601 */
5602 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005603 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005604 return this;
5605 }
5606
5607 /**
5608 * Set the first line of text after the detail section in the big form of the template.
5609 */
5610 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005611 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005612 return this;
5613 }
5614
Chris Wren0bd664d2012-08-01 13:56:56 -04005615 /**
5616 * Provide the bitmap to be used as the payload for the BigPicture notification.
5617 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005618 public BigPictureStyle bigPicture(Bitmap b) {
5619 mPicture = b;
5620 return this;
5621 }
5622
Chris Wren3745a3d2012-05-22 15:11:52 -04005623 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005624 * Override the large icon when the big notification is shown.
5625 */
5626 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005627 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5628 }
5629
5630 /**
5631 * Override the large icon when the big notification is shown.
5632 */
5633 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005634 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005635 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005636 return this;
5637 }
5638
Riley Andrews0394a0c2015-11-03 23:36:52 -08005639 /** @hide */
5640 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5641
Daniel Sandler0ec46202015-06-24 01:27:05 -04005642 /**
5643 * @hide
5644 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005645 @Override
5646 public void purgeResources() {
5647 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005648 if (mPicture != null &&
5649 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005650 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005651 mPicture = mPicture.createAshmemBitmap();
5652 }
5653 if (mBigLargeIcon != null) {
5654 mBigLargeIcon.convertToAshmem();
5655 }
5656 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005657
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005658 /**
5659 * @hide
5660 */
Selim Cinekd0426622017-07-11 13:19:59 +02005661 @Override
5662 public void reduceImageSizes(Context context) {
5663 super.reduceImageSizes(context);
5664 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005665 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005666 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005667 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5668 ? R.dimen.notification_big_picture_max_height_low_ram
5669 : R.dimen.notification_big_picture_max_height);
5670 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5671 ? R.dimen.notification_big_picture_max_width_low_ram
5672 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005673 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5674 }
5675 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005676 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5677 ? R.dimen.notification_right_icon_size_low_ram
5678 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005679 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5680 }
5681 }
5682
5683 /**
5684 * @hide
5685 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005686 public RemoteViews makeBigContentView() {
5687 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005688 // This covers the following cases:
5689 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005690 // mN.mLargeIcon
5691 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005692 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005693 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005694 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005695 oldLargeIcon = mBuilder.mN.mLargeIcon;
5696 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005697 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5698 // replacement if the other one is null. Because we're restoring these legacy icons
5699 // for old listeners, this is in general non-null.
5700 largeIconLegacy = mBuilder.mN.largeIcon;
5701 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005702 }
5703
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005704 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005705 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005706 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5707 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005708 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005709 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005710 }
Selim Cinek279fa862016-06-14 10:57:25 -07005711 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005712
Christoph Studer5c510ee2014-12-15 16:32:27 +01005713 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005714 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005715 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005716 }
5717
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005718 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005719 return contentView;
5720 }
5721
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005722 /**
5723 * @hide
5724 */
5725 public void addExtras(Bundle extras) {
5726 super.addExtras(extras);
5727
5728 if (mBigLargeIconSet) {
5729 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5730 }
5731 extras.putParcelable(EXTRA_PICTURE, mPicture);
5732 }
5733
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005734 /**
5735 * @hide
5736 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005737 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005738 protected void restoreFromExtras(Bundle extras) {
5739 super.restoreFromExtras(extras);
5740
5741 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005742 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005743 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005744 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005745 mPicture = extras.getParcelable(EXTRA_PICTURE);
5746 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005747
5748 /**
5749 * @hide
5750 */
5751 @Override
5752 public boolean hasSummaryInHeader() {
5753 return false;
5754 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005755 }
5756
5757 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005758 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005759 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005760 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005761 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005762 * Notification notif = new Notification.Builder(mContext)
5763 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5764 * .setContentText(subject)
5765 * .setSmallIcon(R.drawable.new_mail)
5766 * .setLargeIcon(aBitmap)
5767 * .setStyle(new Notification.BigTextStyle()
5768 * .bigText(aVeryLongString))
5769 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005770 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005771 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005772 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005773 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005774 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005775
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005776 private CharSequence mBigText;
5777
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005778 public BigTextStyle() {
5779 }
5780
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005781 /**
5782 * @deprecated use {@code BigTextStyle()}.
5783 */
5784 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005785 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005786 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005787 }
5788
Chris Wrend6297db2012-05-03 16:20:13 -04005789 /**
5790 * Overrides ContentTitle in the big form of the template.
5791 * This defaults to the value passed to setContentTitle().
5792 */
5793 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005794 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005795 return this;
5796 }
5797
5798 /**
5799 * Set the first line of text after the detail section in the big form of the template.
5800 */
5801 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005802 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005803 return this;
5804 }
5805
Chris Wren0bd664d2012-08-01 13:56:56 -04005806 /**
5807 * Provide the longer text to be displayed in the big form of the
5808 * template in place of the content text.
5809 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005810 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005811 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005812 return this;
5813 }
5814
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005815 /**
5816 * @hide
5817 */
5818 public void addExtras(Bundle extras) {
5819 super.addExtras(extras);
5820
Christoph Studer4600f9b2014-07-22 22:44:43 +02005821 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5822 }
5823
5824 /**
5825 * @hide
5826 */
5827 @Override
5828 protected void restoreFromExtras(Bundle extras) {
5829 super.restoreFromExtras(extras);
5830
5831 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005832 }
5833
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005834 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005835 * @param increasedHeight true if this layout be created with an increased height.
5836 *
5837 * @hide
5838 */
5839 @Override
5840 public RemoteViews makeContentView(boolean increasedHeight) {
5841 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005842 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005843 mBuilder.mActions = new ArrayList<>();
5844 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005845 mBuilder.mActions = mBuilder.mOriginalActions;
5846 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005847 return remoteViews;
5848 }
5849 return super.makeContentView(increasedHeight);
5850 }
5851
5852 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005853 * @hide
5854 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005855 @Override
5856 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5857 if (increasedHeight && mBuilder.mActions.size() > 0) {
5858 return makeBigContentView();
5859 }
5860 return super.makeHeadsUpContentView(increasedHeight);
5861 }
5862
5863 /**
5864 * @hide
5865 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005866 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005867
5868 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005869 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005870 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005871
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005872 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005873
Selim Cinek75998782016-04-26 10:39:17 -07005874 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005875
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005876 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005877 if (TextUtils.isEmpty(bigTextText)) {
5878 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5879 // experience
5880 bigTextText = mBuilder.processLegacyText(text);
5881 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005882 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005883
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005884 return contentView;
5885 }
5886
Adrian Roosb1f427c2016-05-26 12:27:15 -07005887 static void applyBigTextContentView(Builder builder,
5888 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005889 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005890 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005891 contentView.setViewVisibility(R.id.big_text,
5892 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005893 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005894 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005895 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005896
5897 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005898 * Helper class for generating large-format notifications that include multiple back-and-forth
5899 * messages of varying types between any number of people.
5900 *
5901 * <br>
5902 * If the platform does not provide large-format notifications, this method has no effect. The
5903 * user will always see the normal notification view.
5904 * <br>
5905 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5906 * so:
5907 * <pre class="prettyprint">
5908 *
5909 * Notification noti = new Notification.Builder()
5910 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5911 * .setContentText(subject)
5912 * .setSmallIcon(R.drawable.new_message)
5913 * .setLargeIcon(aBitmap)
5914 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5915 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5916 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5917 * .build();
5918 * </pre>
5919 */
5920 public static class MessagingStyle extends Style {
5921
5922 /**
5923 * The maximum number of messages that will be retained in the Notification itself (the
5924 * number displayed is up to the platform).
5925 */
5926 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5927
5928 CharSequence mUserDisplayName;
5929 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005930 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005931 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005932
5933 MessagingStyle() {
5934 }
5935
5936 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005937 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5938 * user before the posting app reposts the notification with those messages after they've
5939 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005940 * {@link #addMessage(Notification.MessagingStyle.Message)}
5941 */
Alex Hillsfd590442016-10-07 09:52:44 -04005942 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005943 mUserDisplayName = userDisplayName;
5944 }
5945
5946 /**
5947 * Returns the name to be displayed for any replies sent by the user
5948 */
5949 public CharSequence getUserDisplayName() {
5950 return mUserDisplayName;
5951 }
5952
5953 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005954 * Sets the title to be displayed on this conversation. This should only be used for
5955 * group messaging and left unset for one-on-one conversations.
5956 * @param conversationTitle
5957 * @return this object for method chaining.
5958 */
5959 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5960 mConversationTitle = conversationTitle;
5961 return this;
5962 }
5963
5964 /**
5965 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5966 * should be for one-on-one conversations
5967 */
5968 public CharSequence getConversationTitle() {
5969 return mConversationTitle;
5970 }
5971
5972 /**
5973 * Adds a message for display by this notification. Convenience call for a simple
5974 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5975 * @param text A {@link CharSequence} to be displayed as the message content
5976 * @param timestamp Time at which the message arrived
5977 * @param sender A {@link CharSequence} to be used for displaying the name of the
5978 * sender. Should be <code>null</code> for messages by the current user, in which case
5979 * the platform will insert {@link #getUserDisplayName()}.
5980 * Should be unique amongst all individuals in the conversation, and should be
5981 * consistent during re-posts of the notification.
5982 *
5983 * @see Message#Message(CharSequence, long, CharSequence)
5984 *
5985 * @return this object for method chaining
5986 */
5987 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005988 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005989 }
5990
5991 /**
5992 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005993 *
5994 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5995 * the newest last.
5996 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005997 * @param message The {@link Message} to be displayed
5998 * @return this object for method chaining
5999 */
6000 public MessagingStyle addMessage(Message message) {
6001 mMessages.add(message);
6002 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6003 mMessages.remove(0);
6004 }
6005 return this;
6006 }
6007
6008 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006009 * Adds a {@link Message} for historic context in this notification.
6010 *
6011 * <p>Messages should be added as historic if they are not the main subject of the
6012 * notification but may give context to a conversation. The system may choose to present
6013 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6014 *
6015 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6016 * the newest last.
6017 *
6018 * @param message The historic {@link Message} to be added
6019 * @return this object for method chaining
6020 */
6021 public MessagingStyle addHistoricMessage(Message message) {
6022 mHistoricMessages.add(message);
6023 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6024 mHistoricMessages.remove(0);
6025 }
6026 return this;
6027 }
6028
6029 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006030 * Gets the list of {@code Message} objects that represent the notification
6031 */
6032 public List<Message> getMessages() {
6033 return mMessages;
6034 }
6035
6036 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006037 * Gets the list of historic {@code Message}s in the notification.
6038 */
6039 public List<Message> getHistoricMessages() {
6040 return mHistoricMessages;
6041 }
6042
6043 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006044 * @hide
6045 */
6046 @Override
6047 public void addExtras(Bundle extras) {
6048 super.addExtras(extras);
6049 if (mUserDisplayName != null) {
6050 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6051 }
6052 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006053 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006054 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006055 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6056 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006057 }
Adrian Roos437cd562017-01-18 15:47:03 -08006058 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6059 Message.getBundleArrayForMessages(mHistoricMessages));
6060 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006061
6062 fixTitleAndTextExtras(extras);
6063 }
6064
6065 private void fixTitleAndTextExtras(Bundle extras) {
6066 Message m = findLatestIncomingMessage();
6067 CharSequence text = (m == null) ? null : m.mText;
6068 CharSequence sender = m == null ? null
6069 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6070 CharSequence title;
6071 if (!TextUtils.isEmpty(mConversationTitle)) {
6072 if (!TextUtils.isEmpty(sender)) {
6073 BidiFormatter bidi = BidiFormatter.getInstance();
6074 title = mBuilder.mContext.getString(
6075 com.android.internal.R.string.notification_messaging_title_template,
6076 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6077 } else {
6078 title = mConversationTitle;
6079 }
6080 } else {
6081 title = sender;
6082 }
6083
6084 if (title != null) {
6085 extras.putCharSequence(EXTRA_TITLE, title);
6086 }
6087 if (text != null) {
6088 extras.putCharSequence(EXTRA_TEXT, text);
6089 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006090 }
6091
6092 /**
6093 * @hide
6094 */
6095 @Override
6096 protected void restoreFromExtras(Bundle extras) {
6097 super.restoreFromExtras(extras);
6098
Adrian Roos96b7e202016-05-17 13:50:38 -07006099 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6100 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006101 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006102 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006103 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006104 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04006105 }
6106
6107 /**
6108 * @hide
6109 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006110 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006111 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006112 mBuilder.mOriginalActions = mBuilder.mActions;
6113 mBuilder.mActions = new ArrayList<>();
6114 RemoteViews remoteViews = makeBigContentView();
6115 mBuilder.mActions = mBuilder.mOriginalActions;
6116 mBuilder.mOriginalActions = null;
6117 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006118 }
6119
6120 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006121 return findLatestIncomingMessage(mMessages);
6122 }
6123
6124 /**
6125 * @hide
6126 */
6127 @Nullable
6128 public static Message findLatestIncomingMessage(
6129 List<Message> messages) {
6130 for (int i = messages.size() - 1; i >= 0; i--) {
6131 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006132 // Incoming messages have a non-empty sender.
6133 if (!TextUtils.isEmpty(m.mSender)) {
6134 return m;
6135 }
6136 }
Selim Cinek88188f22017-09-19 16:46:56 -07006137 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006138 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006139 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006140 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006141 return null;
6142 }
6143
6144 /**
6145 * @hide
6146 */
6147 @Override
6148 public RemoteViews makeBigContentView() {
Selim Cinek88188f22017-09-19 16:46:56 -07006149 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006150 ? super.mBigContentTitle
6151 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006152 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
6153 if (isOneToOne) {
6154 // Let's add the conversationTitle in case we didn't have one before and all
6155 // messages are from the same sender
6156 conversationTitle = createConversationTitleFromMessages();
Selim Cinekf7409db2017-10-24 16:17:14 -07006157 } else if (hasOnlyWhiteSpaceSenders()) {
6158 isOneToOne = true;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006159 }
Selim Cinek88188f22017-09-19 16:46:56 -07006160 boolean hasTitle = !TextUtils.isEmpty(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07006161 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006162 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006163 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
6164 .hideLargeIcon(isOneToOne).alwaysShowReply(true));
Selim Cinek88188f22017-09-19 16:46:56 -07006165 addExtras(mBuilder.mN.extras);
6166 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6167 mBuilder.resolveContrastColor());
6168 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6169 mBuilder.mN.mLargeIcon);
6170 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6171 isOneToOne);
6172 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6173 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006174 return contentView;
6175 }
6176
Selim Cinekf7409db2017-10-24 16:17:14 -07006177 private boolean hasOnlyWhiteSpaceSenders() {
6178 for (int i = 0; i < mMessages.size(); i++) {
6179 Message m = mMessages.get(i);
6180 CharSequence sender = m.getSender();
6181 if (!isWhiteSpace(sender)) {
6182 return false;
6183 }
6184 }
6185 return true;
6186 }
6187
6188 private boolean isWhiteSpace(CharSequence sender) {
6189 if (TextUtils.isEmpty(sender)) {
6190 return true;
6191 }
6192 if (sender.toString().matches("^\\s*$")) {
6193 return true;
6194 }
6195 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6196 // For the presentation that we had.
6197 for (int i = 0; i < sender.length(); i++) {
6198 char c = sender.charAt(i);
6199 if (c != '\u200B') {
6200 return false;
6201 }
6202 }
6203 return true;
6204 }
6205
Selim Cinek88188f22017-09-19 16:46:56 -07006206 private CharSequence createConversationTitleFromMessages() {
6207 ArraySet<CharSequence> names = new ArraySet<>();
6208 for (int i = 0; i < mMessages.size(); i++) {
6209 Message m = mMessages.get(i);
6210 CharSequence sender = m.getSender();
6211 if (sender != null) {
6212 names.add(sender);
6213 }
6214 }
6215 SpannableStringBuilder title = new SpannableStringBuilder();
6216 int size = names.size();
6217 for (int i = 0; i < size; i++) {
6218 CharSequence name = names.valueAt(i);
6219 if (!TextUtils.isEmpty(title)) {
6220 title.append(", ");
6221 }
6222 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6223 }
6224 return title;
6225 }
6226
Adrian Roosdedd1df2016-04-26 16:38:47 -07006227 /**
6228 * @hide
6229 */
6230 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006231 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006232 RemoteViews remoteViews = makeBigContentView();
6233 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6234 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006235 }
6236
Adrian Roosc1a80b02016-04-05 14:54:55 -07006237 private static TextAppearanceSpan makeFontColorSpan(int color) {
6238 return new TextAppearanceSpan(null, 0, 0,
6239 ColorStateList.valueOf(color), null);
6240 }
6241
Alex Hillsd9b04d92016-04-11 16:38:16 -04006242 public static final class Message {
6243
6244 static final String KEY_TEXT = "text";
6245 static final String KEY_TIMESTAMP = "time";
6246 static final String KEY_SENDER = "sender";
6247 static final String KEY_DATA_MIME_TYPE = "type";
6248 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006249 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006250
6251 private final CharSequence mText;
6252 private final long mTimestamp;
6253 private final CharSequence mSender;
6254
Shane Brennan5a871862017-03-11 13:14:17 -08006255 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006256 private String mDataMimeType;
6257 private Uri mDataUri;
6258
6259 /**
6260 * Constructor
6261 * @param text A {@link CharSequence} to be displayed as the message content
6262 * @param timestamp Time at which the message arrived
6263 * @param sender A {@link CharSequence} to be used for displaying the name of the
6264 * sender. Should be <code>null</code> for messages by the current user, in which case
6265 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6266 * Should be unique amongst all individuals in the conversation, and should be
6267 * consistent during re-posts of the notification.
6268 */
6269 public Message(CharSequence text, long timestamp, CharSequence sender){
6270 mText = text;
6271 mTimestamp = timestamp;
6272 mSender = sender;
6273 }
6274
6275 /**
6276 * Sets a binary blob of data and an associated MIME type for a message. In the case
6277 * where the platform doesn't support the MIME type, the original text provided in the
6278 * constructor will be used.
6279 * @param dataMimeType The MIME type of the content. See
6280 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6281 * types on Android and Android Wear.
6282 * @param dataUri The uri containing the content whose type is given by the MIME type.
6283 * <p class="note">
6284 * <ol>
6285 * <li>Notification Listeners including the System UI need permission to access the
6286 * data the Uri points to. The recommended ways to do this are:</li>
6287 * <li>Store the data in your own ContentProvider, making sure that other apps have
6288 * the correct permission to access your provider. The preferred mechanism for
6289 * providing access is to use per-URI permissions which are temporary and only
6290 * grant access to the receiving application. An easy way to create a
6291 * ContentProvider like this is to use the FileProvider helper class.</li>
6292 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6293 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6294 * also store non-media types (see MediaStore.Files for more info). Files can be
6295 * inserted into the MediaStore using scanFile() after which a content:// style
6296 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6297 * Note that once added to the system MediaStore the content is accessible to any
6298 * app on the device.</li>
6299 * </ol>
6300 * @return this object for method chaining
6301 */
6302 public Message setData(String dataMimeType, Uri dataUri) {
6303 mDataMimeType = dataMimeType;
6304 mDataUri = dataUri;
6305 return this;
6306 }
6307
Alex Hillsfc737de2016-03-23 17:33:02 -04006308 /**
6309 * Get the text to be used for this message, or the fallback text if a type and content
6310 * Uri have been set
6311 */
6312 public CharSequence getText() {
6313 return mText;
6314 }
6315
6316 /**
6317 * Get the time at which this message arrived
6318 */
6319 public long getTimestamp() {
6320 return mTimestamp;
6321 }
6322
6323 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006324 * Get the extras Bundle for this message.
6325 */
6326 public Bundle getExtras() {
6327 return mExtras;
6328 }
6329
6330 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006331 * Get the text used to display the contact's name in the messaging experience
6332 */
6333 public CharSequence getSender() {
6334 return mSender;
6335 }
6336
6337 /**
6338 * Get the MIME type of the data pointed to by the Uri
6339 */
6340 public String getDataMimeType() {
6341 return mDataMimeType;
6342 }
6343
6344 /**
6345 * Get the the Uri pointing to the content of the message. Can be null, in which case
6346 * {@see #getText()} is used.
6347 */
6348 public Uri getDataUri() {
6349 return mDataUri;
6350 }
6351
Alex Hillsd9b04d92016-04-11 16:38:16 -04006352 private Bundle toBundle() {
6353 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006354 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006355 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006356 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006357 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006358 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006359 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006360 }
6361 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006362 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006363 }
6364 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006365 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006366 }
Shane Brennan5a871862017-03-11 13:14:17 -08006367 if (mExtras != null) {
6368 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6369 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006370 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006371 }
6372
Alex Hillsd9b04d92016-04-11 16:38:16 -04006373 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6374 Bundle[] bundles = new Bundle[messages.size()];
6375 final int N = messages.size();
6376 for (int i = 0; i < N; i++) {
6377 bundles[i] = messages.get(i).toBundle();
6378 }
6379 return bundles;
6380 }
6381
Selim Cinek88188f22017-09-19 16:46:56 -07006382 /**
6383 * @return A list of messages read from the bundles.
6384 *
6385 * @hide
6386 */
6387 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6388 if (bundles == null) {
6389 return new ArrayList<>();
6390 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006391 List<Message> messages = new ArrayList<>(bundles.length);
6392 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006393 if (bundles[i] instanceof Bundle) {
6394 Message message = getMessageFromBundle((Bundle)bundles[i]);
6395 if (message != null) {
6396 messages.add(message);
6397 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006398 }
6399 }
6400 return messages;
6401 }
6402
6403 static Message getMessageFromBundle(Bundle bundle) {
6404 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006405 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006406 return null;
6407 } else {
6408 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6409 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6410 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6411 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006412 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6413 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006414 }
Shane Brennan5a871862017-03-11 13:14:17 -08006415 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6416 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6417 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006418 return message;
6419 }
6420 } catch (ClassCastException e) {
6421 return null;
6422 }
6423 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006424 }
6425 }
6426
6427 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006428 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006429 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006430 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006431 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006432 * Notification notif = new Notification.Builder(mContext)
6433 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6434 * .setContentText(subject)
6435 * .setSmallIcon(R.drawable.new_mail)
6436 * .setLargeIcon(aBitmap)
6437 * .setStyle(new Notification.InboxStyle()
6438 * .addLine(str1)
6439 * .addLine(str2)
6440 * .setContentTitle(&quot;&quot;)
6441 * .setSummaryText(&quot;+3 more&quot;))
6442 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006443 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006444 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006445 * @see Notification#bigContentView
6446 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006447 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006448 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6449
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006450 public InboxStyle() {
6451 }
6452
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006453 /**
6454 * @deprecated use {@code InboxStyle()}.
6455 */
6456 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006457 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006458 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006459 }
6460
Chris Wrend6297db2012-05-03 16:20:13 -04006461 /**
6462 * Overrides ContentTitle in the big form of the template.
6463 * This defaults to the value passed to setContentTitle().
6464 */
6465 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006466 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006467 return this;
6468 }
6469
6470 /**
6471 * Set the first line of text after the detail section in the big form of the template.
6472 */
6473 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006474 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006475 return this;
6476 }
6477
Chris Wren0bd664d2012-08-01 13:56:56 -04006478 /**
6479 * Append a line to the digest section of the Inbox notification.
6480 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006481 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006482 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006483 return this;
6484 }
6485
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006486 /**
6487 * @hide
6488 */
6489 public void addExtras(Bundle extras) {
6490 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006491
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006492 CharSequence[] a = new CharSequence[mTexts.size()];
6493 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6494 }
6495
Christoph Studer4600f9b2014-07-22 22:44:43 +02006496 /**
6497 * @hide
6498 */
6499 @Override
6500 protected void restoreFromExtras(Bundle extras) {
6501 super.restoreFromExtras(extras);
6502
6503 mTexts.clear();
6504 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6505 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6506 }
6507 }
6508
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006509 /**
6510 * @hide
6511 */
6512 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006513 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006514 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006515 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6516 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006517
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006518 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006519
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006520 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006521
Chris Wrend6297db2012-05-03 16:20:13 -04006522 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 -04006523 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006524
Chris Wren4ed80d52012-05-17 09:30:03 -04006525 // Make sure all rows are gone in case we reuse a view.
6526 for (int rowId : rowIds) {
6527 contentView.setViewVisibility(rowId, View.GONE);
6528 }
6529
Daniel Sandler879c5e02012-04-17 16:46:51 -04006530 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006531 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6532 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006533 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006534 int onlyViewId = 0;
6535 int maxRows = rowIds.length;
6536 if (mBuilder.mActions.size() > 0) {
6537 maxRows--;
6538 }
6539 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006540 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006541 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006542 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006543 contentView.setTextViewText(rowIds[i],
6544 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006545 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006546 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006547 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006548 if (first) {
6549 onlyViewId = rowIds[i];
6550 } else {
6551 onlyViewId = 0;
6552 }
Selim Cinek247fa012016-02-18 09:50:48 -08006553 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006554 }
6555 i++;
6556 }
Selim Cinek07c80172016-04-21 16:40:47 -07006557 if (onlyViewId != 0) {
6558 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6559 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6560 R.dimen.notification_text_margin_top);
6561 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6562 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006563
Daniel Sandler879c5e02012-04-17 16:46:51 -04006564 return contentView;
6565 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006566
Selim Cinek247fa012016-02-18 09:50:48 -08006567 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006568 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006569 if (first) {
6570 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6571 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6572 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006573 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006574 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006575 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006576 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006577 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006578 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006579 }
Dan Sandler842dd772014-05-15 09:36:47 -04006580
6581 /**
6582 * Notification style for media playback notifications.
6583 *
6584 * In the expanded form, {@link Notification#bigContentView}, up to 5
6585 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006586 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006587 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6588 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6589 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006590 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006591 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6592 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006593 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006594 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006595 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006596 * Notifications created with MediaStyle will have their category set to
6597 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6598 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006599 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006600 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6601 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006602 * the System UI can identify this as a notification representing an active media session
6603 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6604 *
Selim Cinek99104832017-01-25 14:47:33 -08006605 * <p>
6606 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6607 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6608 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6609 * <p>
6610 *
Dan Sandler842dd772014-05-15 09:36:47 -04006611 * To use this style with your Notification, feed it to
6612 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6613 * <pre class="prettyprint">
6614 * Notification noti = new Notification.Builder()
6615 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006616 * .setContentTitle(&quot;Track title&quot;)
6617 * .setContentText(&quot;Artist - Album&quot;)
6618 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006619 * .setStyle(<b>new Notification.MediaStyle()</b>
6620 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006621 * .build();
6622 * </pre>
6623 *
6624 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006625 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006626 */
6627 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006628 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006629 static final int MAX_MEDIA_BUTTONS = 5;
6630
6631 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006632 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006633
6634 public MediaStyle() {
6635 }
6636
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006637 /**
6638 * @deprecated use {@code MediaStyle()}.
6639 */
6640 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006641 public MediaStyle(Builder builder) {
6642 setBuilder(builder);
6643 }
6644
6645 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006646 * 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 -04006647 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006648 *
6649 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006650 */
6651 public MediaStyle setShowActionsInCompactView(int...actions) {
6652 mActionsToShowInCompact = actions;
6653 return this;
6654 }
6655
6656 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006657 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6658 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006659 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006660 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006661 mToken = token;
6662 return this;
6663 }
6664
Christoph Studer4600f9b2014-07-22 22:44:43 +02006665 /**
6666 * @hide
6667 */
Dan Sandler842dd772014-05-15 09:36:47 -04006668 @Override
6669 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006670 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006671 if (wip.category == null) {
6672 wip.category = Notification.CATEGORY_TRANSPORT;
6673 }
Dan Sandler842dd772014-05-15 09:36:47 -04006674 return wip;
6675 }
6676
Christoph Studer4600f9b2014-07-22 22:44:43 +02006677 /**
6678 * @hide
6679 */
6680 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006681 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006682 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006683 }
6684
6685 /**
6686 * @hide
6687 */
6688 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006689 public RemoteViews makeBigContentView() {
6690 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006691 }
6692
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006693 /**
6694 * @hide
6695 */
6696 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006697 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006698 RemoteViews expanded = makeMediaBigContentView();
6699 return expanded != null ? expanded : makeMediaContentView();
6700 }
6701
Dan Sandler842dd772014-05-15 09:36:47 -04006702 /** @hide */
6703 @Override
6704 public void addExtras(Bundle extras) {
6705 super.addExtras(extras);
6706
6707 if (mToken != null) {
6708 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6709 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006710 if (mActionsToShowInCompact != null) {
6711 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6712 }
Dan Sandler842dd772014-05-15 09:36:47 -04006713 }
6714
Christoph Studer4600f9b2014-07-22 22:44:43 +02006715 /**
6716 * @hide
6717 */
6718 @Override
6719 protected void restoreFromExtras(Bundle extras) {
6720 super.restoreFromExtras(extras);
6721
6722 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6723 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6724 }
6725 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6726 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6727 }
6728 }
6729
Selim Cinek5bf069a2015-11-10 19:14:27 -05006730 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006731 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006732 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006733 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006734 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006735
6736 // If the action buttons should not be tinted, then just use the default
6737 // notification color. Otherwise, just use the passed-in color.
6738 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6739 ? color
6740 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6741 Notification.COLOR_DEFAULT);
6742
Sunny Goyal5b153922017-09-21 21:00:36 -07006743 button.setDrawableTint(R.id.action0, false, tintColor,
6744 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006745 if (!tombstone) {
6746 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6747 }
6748 button.setContentDescription(R.id.action0, action.title);
6749 return button;
6750 }
6751
6752 private RemoteViews makeMediaContentView() {
6753 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006754 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006755
6756 final int numActions = mBuilder.mActions.size();
6757 final int N = mActionsToShowInCompact == null
6758 ? 0
6759 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6760 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006761 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006762 for (int i = 0; i < N; i++) {
6763 if (i >= numActions) {
6764 throw new IllegalArgumentException(String.format(
6765 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6766 i, numActions - 1));
6767 }
6768
6769 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006770 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006771 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006772 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006773 }
6774 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006775 handleImage(view);
6776 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006777 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006778 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006779 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006780 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006781 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006782 return view;
6783 }
6784
Selim Cinek99104832017-01-25 14:47:33 -08006785 private int getPrimaryHighlightColor() {
6786 return mBuilder.getPrimaryHighlightColor();
6787 }
6788
Dan Sandler842dd772014-05-15 09:36:47 -04006789 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006790 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006791 // Dont add an expanded view if there is no more content to be revealed
6792 int actionsInCompact = mActionsToShowInCompact == null
6793 ? 0
6794 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006795 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006796 return null;
6797 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006798 RemoteViews big = mBuilder.applyStandardTemplate(
6799 R.layout.notification_template_material_big_media,
6800 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006801
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006802 if (actionCount > 0) {
6803 big.removeAllViews(com.android.internal.R.id.media_actions);
6804 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006805 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006806 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006807 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006808 }
6809 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006810 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006811 return big;
6812 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006813
Selim Cinek5bf069a2015-11-10 19:14:27 -05006814 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006815 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006816 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6817 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006818 }
6819 }
6820
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006821 /**
6822 * @hide
6823 */
6824 @Override
6825 protected boolean hasProgress() {
6826 return false;
6827 }
Dan Sandler842dd772014-05-15 09:36:47 -04006828 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006829
Selim Cinek593610c2016-02-16 18:42:57 -08006830 /**
6831 * Notification style for custom views that are decorated by the system
6832 *
6833 * <p>Instead of providing a notification that is completely custom, a developer can set this
6834 * style and still obtain system decorations like the notification header with the expand
6835 * affordance and actions.
6836 *
6837 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6838 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6839 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6840 * corresponding custom views to display.
6841 *
6842 * To use this style with your Notification, feed it to
6843 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6844 * <pre class="prettyprint">
6845 * Notification noti = new Notification.Builder()
6846 * .setSmallIcon(R.drawable.ic_stat_player)
6847 * .setLargeIcon(albumArtBitmap))
6848 * .setCustomContentView(contentView);
6849 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6850 * .build();
6851 * </pre>
6852 */
6853 public static class DecoratedCustomViewStyle extends Style {
6854
6855 public DecoratedCustomViewStyle() {
6856 }
6857
Selim Cinek593610c2016-02-16 18:42:57 -08006858 /**
6859 * @hide
6860 */
6861 public boolean displayCustomViewInline() {
6862 return true;
6863 }
6864
6865 /**
6866 * @hide
6867 */
6868 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006869 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006870 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6871 }
6872
6873 /**
6874 * @hide
6875 */
6876 @Override
6877 public RemoteViews makeBigContentView() {
6878 return makeDecoratedBigContentView();
6879 }
6880
6881 /**
6882 * @hide
6883 */
6884 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006885 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006886 return makeDecoratedHeadsUpContentView();
6887 }
6888
Selim Cinek593610c2016-02-16 18:42:57 -08006889 private RemoteViews makeDecoratedHeadsUpContentView() {
6890 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6891 ? mBuilder.mN.contentView
6892 : mBuilder.mN.headsUpContentView;
6893 if (mBuilder.mActions.size() == 0) {
6894 return makeStandardTemplateWithCustomContent(headsUpContentView);
6895 }
6896 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6897 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006898 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006899 return remoteViews;
6900 }
6901
Selim Cinek593610c2016-02-16 18:42:57 -08006902 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6903 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6904 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006905 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006906 return remoteViews;
6907 }
6908
Selim Cinek593610c2016-02-16 18:42:57 -08006909 private RemoteViews makeDecoratedBigContentView() {
6910 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6911 ? mBuilder.mN.contentView
6912 : mBuilder.mN.bigContentView;
6913 if (mBuilder.mActions.size() == 0) {
6914 return makeStandardTemplateWithCustomContent(bigContentView);
6915 }
6916 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6917 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006918 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006919 return remoteViews;
6920 }
Selim Cinek247fa012016-02-18 09:50:48 -08006921
6922 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6923 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006924 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006925 // Need to clone customContent before adding, because otherwise it can no longer be
6926 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006927 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006928 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6929 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07006930 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08006931 }
Selim Cinek247fa012016-02-18 09:50:48 -08006932 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006933 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006934 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006935 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006936 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006937 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006938 }
Selim Cinek593610c2016-02-16 18:42:57 -08006939 }
6940
Selim Cinek03eb3b72016-02-18 10:39:45 -08006941 /**
6942 * Notification style for media custom views that are decorated by the system
6943 *
6944 * <p>Instead of providing a media notification that is completely custom, a developer can set
6945 * this style and still obtain system decorations like the notification header with the expand
6946 * affordance and actions.
6947 *
6948 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6949 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6950 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6951 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006952 * <p>
6953 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6954 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6955 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006956 * To use this style with your Notification, feed it to
6957 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6958 * <pre class="prettyprint">
6959 * Notification noti = new Notification.Builder()
6960 * .setSmallIcon(R.drawable.ic_stat_player)
6961 * .setLargeIcon(albumArtBitmap))
6962 * .setCustomContentView(contentView);
6963 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6964 * .setMediaSession(mySession))
6965 * .build();
6966 * </pre>
6967 *
6968 * @see android.app.Notification.DecoratedCustomViewStyle
6969 * @see android.app.Notification.MediaStyle
6970 */
6971 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6972
6973 public DecoratedMediaCustomViewStyle() {
6974 }
6975
Selim Cinek03eb3b72016-02-18 10:39:45 -08006976 /**
6977 * @hide
6978 */
6979 public boolean displayCustomViewInline() {
6980 return true;
6981 }
6982
6983 /**
6984 * @hide
6985 */
6986 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006987 public RemoteViews makeContentView(boolean increasedHeight) {
6988 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006989 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6990 mBuilder.mN.contentView);
6991 }
6992
6993 /**
6994 * @hide
6995 */
6996 @Override
6997 public RemoteViews makeBigContentView() {
6998 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6999 ? mBuilder.mN.bigContentView
7000 : mBuilder.mN.contentView;
7001 return makeBigContentViewWithCustomContent(customRemoteView);
7002 }
7003
7004 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7005 RemoteViews remoteViews = super.makeBigContentView();
7006 if (remoteViews != null) {
7007 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7008 customRemoteView);
7009 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007010 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007011 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7012 customRemoteView);
7013 } else {
7014 return null;
7015 }
7016 }
7017
7018 /**
7019 * @hide
7020 */
7021 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007022 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007023 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7024 ? mBuilder.mN.headsUpContentView
7025 : mBuilder.mN.contentView;
7026 return makeBigContentViewWithCustomContent(customRemoteView);
7027 }
7028
7029 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7030 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007031 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007032 // Need to clone customContent before adding, because otherwise it can no longer be
7033 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007034 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007035 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007036 remoteViews.removeAllViews(id);
7037 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007038 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007039 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007040 return remoteViews;
7041 }
7042 }
7043
Christoph Studer4600f9b2014-07-22 22:44:43 +02007044 // When adding a new Style subclass here, don't forget to update
7045 // Builder.getNotificationStyleClass.
7046
Griff Hazen61a9e862014-05-22 16:05:19 -07007047 /**
7048 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7049 * metadata or change options on a notification builder.
7050 */
7051 public interface Extender {
7052 /**
7053 * Apply this extender to a notification builder.
7054 * @param builder the builder to be modified.
7055 * @return the build object for chaining.
7056 */
7057 public Builder extend(Builder builder);
7058 }
7059
7060 /**
7061 * Helper class to add wearable extensions to notifications.
7062 * <p class="note"> See
7063 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7064 * for Android Wear</a> for more information on how to use this class.
7065 * <p>
7066 * To create a notification with wearable extensions:
7067 * <ol>
7068 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7069 * properties.
7070 * <li>Create a {@link android.app.Notification.WearableExtender}.
7071 * <li>Set wearable-specific properties using the
7072 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7073 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7074 * notification.
7075 * <li>Post the notification to the notification system with the
7076 * {@code NotificationManager.notify(...)} methods.
7077 * </ol>
7078 *
7079 * <pre class="prettyprint">
7080 * Notification notif = new Notification.Builder(mContext)
7081 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7082 * .setContentText(subject)
7083 * .setSmallIcon(R.drawable.new_mail)
7084 * .extend(new Notification.WearableExtender()
7085 * .setContentIcon(R.drawable.new_mail))
7086 * .build();
7087 * NotificationManager notificationManger =
7088 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7089 * notificationManger.notify(0, notif);</pre>
7090 *
7091 * <p>Wearable extensions can be accessed on an existing notification by using the
7092 * {@code WearableExtender(Notification)} constructor,
7093 * and then using the {@code get} methods to access values.
7094 *
7095 * <pre class="prettyprint">
7096 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7097 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007098 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007099 */
7100 public static final class WearableExtender implements Extender {
7101 /**
7102 * Sentinel value for an action index that is unset.
7103 */
7104 public static final int UNSET_ACTION_INDEX = -1;
7105
7106 /**
7107 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7108 * default sizing.
7109 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007110 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007111 * on their content.
7112 */
7113 public static final int SIZE_DEFAULT = 0;
7114
7115 /**
7116 * Size value for use with {@link #setCustomSizePreset} to show this notification
7117 * with an extra small size.
7118 * <p>This value is only applicable for custom display notifications created using
7119 * {@link #setDisplayIntent}.
7120 */
7121 public static final int SIZE_XSMALL = 1;
7122
7123 /**
7124 * Size value for use with {@link #setCustomSizePreset} to show this notification
7125 * with a small size.
7126 * <p>This value is only applicable for custom display notifications created using
7127 * {@link #setDisplayIntent}.
7128 */
7129 public static final int SIZE_SMALL = 2;
7130
7131 /**
7132 * Size value for use with {@link #setCustomSizePreset} to show this notification
7133 * with a medium size.
7134 * <p>This value is only applicable for custom display notifications created using
7135 * {@link #setDisplayIntent}.
7136 */
7137 public static final int SIZE_MEDIUM = 3;
7138
7139 /**
7140 * Size value for use with {@link #setCustomSizePreset} to show this notification
7141 * with a large size.
7142 * <p>This value is only applicable for custom display notifications created using
7143 * {@link #setDisplayIntent}.
7144 */
7145 public static final int SIZE_LARGE = 4;
7146
Griff Hazend5f11f92014-05-27 15:40:09 -07007147 /**
7148 * Size value for use with {@link #setCustomSizePreset} to show this notification
7149 * full screen.
7150 * <p>This value is only applicable for custom display notifications created using
7151 * {@link #setDisplayIntent}.
7152 */
7153 public static final int SIZE_FULL_SCREEN = 5;
7154
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007155 /**
7156 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7157 * short amount of time when this notification is displayed on the screen. This
7158 * is the default value.
7159 */
7160 public static final int SCREEN_TIMEOUT_SHORT = 0;
7161
7162 /**
7163 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7164 * for a longer amount of time when this notification is displayed on the screen.
7165 */
7166 public static final int SCREEN_TIMEOUT_LONG = -1;
7167
Griff Hazen61a9e862014-05-22 16:05:19 -07007168 /** Notification extra which contains wearable extensions */
7169 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7170
Pete Gastaf6781d2014-10-07 15:17:05 -04007171 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007172 private static final String KEY_ACTIONS = "actions";
7173 private static final String KEY_FLAGS = "flags";
7174 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7175 private static final String KEY_PAGES = "pages";
7176 private static final String KEY_BACKGROUND = "background";
7177 private static final String KEY_CONTENT_ICON = "contentIcon";
7178 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7179 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7180 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7181 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7182 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007183 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007184 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007185 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007186
7187 // Flags bitwise-ored to mFlags
7188 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7189 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7190 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7191 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007192 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007193 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007194 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007195
7196 // Default value for flags integer
7197 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7198
7199 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7200 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7201
7202 private ArrayList<Action> mActions = new ArrayList<Action>();
7203 private int mFlags = DEFAULT_FLAGS;
7204 private PendingIntent mDisplayIntent;
7205 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7206 private Bitmap mBackground;
7207 private int mContentIcon;
7208 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7209 private int mContentActionIndex = UNSET_ACTION_INDEX;
7210 private int mCustomSizePreset = SIZE_DEFAULT;
7211 private int mCustomContentHeight;
7212 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007213 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007214 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007215 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007216
7217 /**
7218 * Create a {@link android.app.Notification.WearableExtender} with default
7219 * options.
7220 */
7221 public WearableExtender() {
7222 }
7223
7224 public WearableExtender(Notification notif) {
7225 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7226 if (wearableBundle != null) {
7227 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7228 if (actions != null) {
7229 mActions.addAll(actions);
7230 }
7231
7232 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7233 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7234
7235 Notification[] pages = getNotificationArrayFromBundle(
7236 wearableBundle, KEY_PAGES);
7237 if (pages != null) {
7238 Collections.addAll(mPages, pages);
7239 }
7240
7241 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7242 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7243 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7244 DEFAULT_CONTENT_ICON_GRAVITY);
7245 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7246 UNSET_ACTION_INDEX);
7247 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7248 SIZE_DEFAULT);
7249 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7250 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007251 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007252 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007253 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007254 }
7255 }
7256
7257 /**
7258 * Apply wearable extensions to a notification that is being built. This is typically
7259 * called by the {@link android.app.Notification.Builder#extend} method of
7260 * {@link android.app.Notification.Builder}.
7261 */
7262 @Override
7263 public Notification.Builder extend(Notification.Builder builder) {
7264 Bundle wearableBundle = new Bundle();
7265
7266 if (!mActions.isEmpty()) {
7267 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7268 }
7269 if (mFlags != DEFAULT_FLAGS) {
7270 wearableBundle.putInt(KEY_FLAGS, mFlags);
7271 }
7272 if (mDisplayIntent != null) {
7273 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7274 }
7275 if (!mPages.isEmpty()) {
7276 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7277 new Notification[mPages.size()]));
7278 }
7279 if (mBackground != null) {
7280 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7281 }
7282 if (mContentIcon != 0) {
7283 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7284 }
7285 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7286 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7287 }
7288 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7289 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7290 mContentActionIndex);
7291 }
7292 if (mCustomSizePreset != SIZE_DEFAULT) {
7293 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7294 }
7295 if (mCustomContentHeight != 0) {
7296 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7297 }
7298 if (mGravity != DEFAULT_GRAVITY) {
7299 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7300 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007301 if (mHintScreenTimeout != 0) {
7302 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7303 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007304 if (mDismissalId != null) {
7305 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7306 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007307 if (mBridgeTag != null) {
7308 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7309 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007310
7311 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7312 return builder;
7313 }
7314
7315 @Override
7316 public WearableExtender clone() {
7317 WearableExtender that = new WearableExtender();
7318 that.mActions = new ArrayList<Action>(this.mActions);
7319 that.mFlags = this.mFlags;
7320 that.mDisplayIntent = this.mDisplayIntent;
7321 that.mPages = new ArrayList<Notification>(this.mPages);
7322 that.mBackground = this.mBackground;
7323 that.mContentIcon = this.mContentIcon;
7324 that.mContentIconGravity = this.mContentIconGravity;
7325 that.mContentActionIndex = this.mContentActionIndex;
7326 that.mCustomSizePreset = this.mCustomSizePreset;
7327 that.mCustomContentHeight = this.mCustomContentHeight;
7328 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007329 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007330 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007331 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007332 return that;
7333 }
7334
7335 /**
7336 * Add a wearable action to this notification.
7337 *
7338 * <p>When wearable actions are added using this method, the set of actions that
7339 * show on a wearable device splits from devices that only show actions added
7340 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7341 * of which actions display on different devices.
7342 *
7343 * @param action the action to add to this notification
7344 * @return this object for method chaining
7345 * @see android.app.Notification.Action
7346 */
7347 public WearableExtender addAction(Action action) {
7348 mActions.add(action);
7349 return this;
7350 }
7351
7352 /**
7353 * Adds wearable actions to this notification.
7354 *
7355 * <p>When wearable actions are added using this method, the set of actions that
7356 * show on a wearable device splits from devices that only show actions added
7357 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7358 * of which actions display on different devices.
7359 *
7360 * @param actions the actions to add to this notification
7361 * @return this object for method chaining
7362 * @see android.app.Notification.Action
7363 */
7364 public WearableExtender addActions(List<Action> actions) {
7365 mActions.addAll(actions);
7366 return this;
7367 }
7368
7369 /**
7370 * Clear all wearable actions present on this builder.
7371 * @return this object for method chaining.
7372 * @see #addAction
7373 */
7374 public WearableExtender clearActions() {
7375 mActions.clear();
7376 return this;
7377 }
7378
7379 /**
7380 * Get the wearable actions present on this notification.
7381 */
7382 public List<Action> getActions() {
7383 return mActions;
7384 }
7385
7386 /**
7387 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007388 * this notification. The {@link PendingIntent} provided should be for an activity.
7389 *
7390 * <pre class="prettyprint">
7391 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7392 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7393 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7394 * Notification notif = new Notification.Builder(context)
7395 * .extend(new Notification.WearableExtender()
7396 * .setDisplayIntent(displayPendingIntent)
7397 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7398 * .build();</pre>
7399 *
7400 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007401 * should have an empty task affinity. It is also recommended to use the device
7402 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007403 *
7404 * <p>Example AndroidManifest.xml entry:
7405 * <pre class="prettyprint">
7406 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7407 * android:exported=&quot;true&quot;
7408 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007409 * android:taskAffinity=&quot;&quot;
7410 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007411 *
7412 * @param intent the {@link PendingIntent} for an activity
7413 * @return this object for method chaining
7414 * @see android.app.Notification.WearableExtender#getDisplayIntent
7415 */
7416 public WearableExtender setDisplayIntent(PendingIntent intent) {
7417 mDisplayIntent = intent;
7418 return this;
7419 }
7420
7421 /**
7422 * Get the intent to launch inside of an activity view when displaying this
7423 * notification. This {@code PendingIntent} should be for an activity.
7424 */
7425 public PendingIntent getDisplayIntent() {
7426 return mDisplayIntent;
7427 }
7428
7429 /**
7430 * Add an additional page of content to display with this notification. The current
7431 * notification forms the first page, and pages added using this function form
7432 * subsequent pages. This field can be used to separate a notification into multiple
7433 * sections.
7434 *
7435 * @param page the notification to add as another page
7436 * @return this object for method chaining
7437 * @see android.app.Notification.WearableExtender#getPages
7438 */
7439 public WearableExtender addPage(Notification page) {
7440 mPages.add(page);
7441 return this;
7442 }
7443
7444 /**
7445 * Add additional pages of content to display with this notification. The current
7446 * notification forms the first page, and pages added using this function form
7447 * subsequent pages. This field can be used to separate a notification into multiple
7448 * sections.
7449 *
7450 * @param pages a list of notifications
7451 * @return this object for method chaining
7452 * @see android.app.Notification.WearableExtender#getPages
7453 */
7454 public WearableExtender addPages(List<Notification> pages) {
7455 mPages.addAll(pages);
7456 return this;
7457 }
7458
7459 /**
7460 * Clear all additional pages present on this builder.
7461 * @return this object for method chaining.
7462 * @see #addPage
7463 */
7464 public WearableExtender clearPages() {
7465 mPages.clear();
7466 return this;
7467 }
7468
7469 /**
7470 * Get the array of additional pages of content for displaying this notification. The
7471 * current notification forms the first page, and elements within this array form
7472 * subsequent pages. This field can be used to separate a notification into multiple
7473 * sections.
7474 * @return the pages for this notification
7475 */
7476 public List<Notification> getPages() {
7477 return mPages;
7478 }
7479
7480 /**
7481 * Set a background image to be displayed behind the notification content.
7482 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7483 * will work with any notification style.
7484 *
7485 * @param background the background bitmap
7486 * @return this object for method chaining
7487 * @see android.app.Notification.WearableExtender#getBackground
7488 */
7489 public WearableExtender setBackground(Bitmap background) {
7490 mBackground = background;
7491 return this;
7492 }
7493
7494 /**
7495 * Get a background image to be displayed behind the notification content.
7496 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7497 * will work with any notification style.
7498 *
7499 * @return the background image
7500 * @see android.app.Notification.WearableExtender#setBackground
7501 */
7502 public Bitmap getBackground() {
7503 return mBackground;
7504 }
7505
7506 /**
7507 * Set an icon that goes with the content of this notification.
7508 */
7509 public WearableExtender setContentIcon(int icon) {
7510 mContentIcon = icon;
7511 return this;
7512 }
7513
7514 /**
7515 * Get an icon that goes with the content of this notification.
7516 */
7517 public int getContentIcon() {
7518 return mContentIcon;
7519 }
7520
7521 /**
7522 * Set the gravity that the content icon should have within the notification display.
7523 * Supported values include {@link android.view.Gravity#START} and
7524 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7525 * @see #setContentIcon
7526 */
7527 public WearableExtender setContentIconGravity(int contentIconGravity) {
7528 mContentIconGravity = contentIconGravity;
7529 return this;
7530 }
7531
7532 /**
7533 * Get the gravity that the content icon should have within the notification display.
7534 * Supported values include {@link android.view.Gravity#START} and
7535 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7536 * @see #getContentIcon
7537 */
7538 public int getContentIconGravity() {
7539 return mContentIconGravity;
7540 }
7541
7542 /**
7543 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007544 * this notification. This action will no longer display separately from the
7545 * notification's content.
7546 *
Griff Hazenca48d352014-05-28 22:37:13 -07007547 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007548 * set, although the list of available actions comes from the main notification and not
7549 * from the child page's notification.
7550 *
7551 * @param actionIndex The index of the action to hoist onto the current notification page.
7552 * If wearable actions were added to the main notification, this index
7553 * will apply to that list, otherwise it will apply to the regular
7554 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007555 */
7556 public WearableExtender setContentAction(int actionIndex) {
7557 mContentActionIndex = actionIndex;
7558 return this;
7559 }
7560
7561 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007562 * Get the index of the notification action, if any, that was specified as being clickable
7563 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007564 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007565 *
Griff Hazenca48d352014-05-28 22:37:13 -07007566 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007567 * set, although the list of available actions comes from the main notification and not
7568 * from the child page's notification.
7569 *
7570 * <p>If wearable specific actions were added to the main notification, this index will
7571 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007572 *
7573 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007574 */
7575 public int getContentAction() {
7576 return mContentActionIndex;
7577 }
7578
7579 /**
7580 * Set the gravity that this notification should have within the available viewport space.
7581 * Supported values include {@link android.view.Gravity#TOP},
7582 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7583 * The default value is {@link android.view.Gravity#BOTTOM}.
7584 */
7585 public WearableExtender setGravity(int gravity) {
7586 mGravity = gravity;
7587 return this;
7588 }
7589
7590 /**
7591 * Get the gravity that this notification should have within the available viewport space.
7592 * Supported values include {@link android.view.Gravity#TOP},
7593 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7594 * The default value is {@link android.view.Gravity#BOTTOM}.
7595 */
7596 public int getGravity() {
7597 return mGravity;
7598 }
7599
7600 /**
7601 * Set the custom size preset for the display of this notification out of the available
7602 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7603 * {@link #SIZE_LARGE}.
7604 * <p>Some custom size presets are only applicable for custom display notifications created
7605 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7606 * documentation for the preset in question. See also
7607 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7608 */
7609 public WearableExtender setCustomSizePreset(int sizePreset) {
7610 mCustomSizePreset = sizePreset;
7611 return this;
7612 }
7613
7614 /**
7615 * Get the custom size preset for the display of this notification out of the available
7616 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7617 * {@link #SIZE_LARGE}.
7618 * <p>Some custom size presets are only applicable for custom display notifications created
7619 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7620 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7621 */
7622 public int getCustomSizePreset() {
7623 return mCustomSizePreset;
7624 }
7625
7626 /**
7627 * Set the custom height in pixels for the display of this notification's content.
7628 * <p>This option is only available for custom display notifications created
7629 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7630 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7631 * {@link #getCustomContentHeight}.
7632 */
7633 public WearableExtender setCustomContentHeight(int height) {
7634 mCustomContentHeight = height;
7635 return this;
7636 }
7637
7638 /**
7639 * Get the custom height in pixels for the display of this notification's content.
7640 * <p>This option is only available for custom display notifications created
7641 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7642 * {@link #setCustomContentHeight}.
7643 */
7644 public int getCustomContentHeight() {
7645 return mCustomContentHeight;
7646 }
7647
7648 /**
7649 * Set whether the scrolling position for the contents of this notification should start
7650 * at the bottom of the contents instead of the top when the contents are too long to
7651 * display within the screen. Default is false (start scroll at the top).
7652 */
7653 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7654 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7655 return this;
7656 }
7657
7658 /**
7659 * Get whether the scrolling position for the contents of this notification should start
7660 * at the bottom of the contents instead of the top when the contents are too long to
7661 * display within the screen. Default is false (start scroll at the top).
7662 */
7663 public boolean getStartScrollBottom() {
7664 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7665 }
7666
7667 /**
7668 * Set whether the content intent is available when the wearable device is not connected
7669 * to a companion device. The user can still trigger this intent when the wearable device
7670 * is offline, but a visual hint will indicate that the content intent may not be available.
7671 * Defaults to true.
7672 */
7673 public WearableExtender setContentIntentAvailableOffline(
7674 boolean contentIntentAvailableOffline) {
7675 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7676 return this;
7677 }
7678
7679 /**
7680 * Get whether the content intent is available when the wearable device is not connected
7681 * to a companion device. The user can still trigger this intent when the wearable device
7682 * is offline, but a visual hint will indicate that the content intent may not be available.
7683 * Defaults to true.
7684 */
7685 public boolean getContentIntentAvailableOffline() {
7686 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7687 }
7688
7689 /**
7690 * Set a hint that this notification's icon should not be displayed.
7691 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7692 * @return this object for method chaining
7693 */
7694 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7695 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7696 return this;
7697 }
7698
7699 /**
7700 * Get a hint that this notification's icon should not be displayed.
7701 * @return {@code true} if this icon should not be displayed, false otherwise.
7702 * The default value is {@code false} if this was never set.
7703 */
7704 public boolean getHintHideIcon() {
7705 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7706 }
7707
7708 /**
7709 * Set a visual hint that only the background image of this notification should be
7710 * displayed, and other semantic content should be hidden. This hint is only applicable
7711 * to sub-pages added using {@link #addPage}.
7712 */
7713 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7714 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7715 return this;
7716 }
7717
7718 /**
7719 * Get a visual hint that only the background image of this notification should be
7720 * displayed, and other semantic content should be hidden. This hint is only applicable
7721 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7722 */
7723 public boolean getHintShowBackgroundOnly() {
7724 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7725 }
7726
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007727 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007728 * Set a hint that this notification's background should not be clipped if possible,
7729 * and should instead be resized to fully display on the screen, retaining the aspect
7730 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007731 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7732 * @return this object for method chaining
7733 */
7734 public WearableExtender setHintAvoidBackgroundClipping(
7735 boolean hintAvoidBackgroundClipping) {
7736 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7737 return this;
7738 }
7739
7740 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007741 * Get a hint that this notification's background should not be clipped if possible,
7742 * and should instead be resized to fully display on the screen, retaining the aspect
7743 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007744 * @return {@code true} if it's ok if the background is clipped on the screen, false
7745 * otherwise. The default value is {@code false} if this was never set.
7746 */
7747 public boolean getHintAvoidBackgroundClipping() {
7748 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7749 }
7750
7751 /**
7752 * Set a hint that the screen should remain on for at least this duration when
7753 * this notification is displayed on the screen.
7754 * @param timeout The requested screen timeout in milliseconds. Can also be either
7755 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7756 * @return this object for method chaining
7757 */
7758 public WearableExtender setHintScreenTimeout(int timeout) {
7759 mHintScreenTimeout = timeout;
7760 return this;
7761 }
7762
7763 /**
7764 * Get the duration, in milliseconds, that the screen should remain on for
7765 * when this notification is displayed.
7766 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7767 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7768 */
7769 public int getHintScreenTimeout() {
7770 return mHintScreenTimeout;
7771 }
7772
Alex Hills9ab3a232016-04-05 14:54:56 -04007773 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007774 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7775 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7776 * qr codes, as well as other simple black-and-white tickets.
7777 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7778 * @return this object for method chaining
7779 */
7780 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7781 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7782 return this;
7783 }
7784
7785 /**
7786 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7787 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7788 * qr codes, as well as other simple black-and-white tickets.
7789 * @return {@code true} if it should be displayed in ambient, false otherwise
7790 * otherwise. The default value is {@code false} if this was never set.
7791 */
7792 public boolean getHintAmbientBigPicture() {
7793 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7794 }
7795
7796 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007797 * Set a hint that this notification's content intent will launch an {@link Activity}
7798 * directly, telling the platform that it can generate the appropriate transitions.
7799 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7800 * an activity and transitions should be generated, false otherwise.
7801 * @return this object for method chaining
7802 */
7803 public WearableExtender setHintContentIntentLaunchesActivity(
7804 boolean hintContentIntentLaunchesActivity) {
7805 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7806 return this;
7807 }
7808
7809 /**
7810 * Get a hint that this notification's content intent will launch an {@link Activity}
7811 * directly, telling the platform that it can generate the appropriate transitions
7812 * @return {@code true} if the content intent will launch an activity and transitions should
7813 * be generated, false otherwise. The default value is {@code false} if this was never set.
7814 */
7815 public boolean getHintContentIntentLaunchesActivity() {
7816 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7817 }
7818
Nadia Benbernou948627e2016-04-14 14:41:08 -04007819 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007820 * Sets the dismissal id for this notification. If a notification is posted with a
7821 * dismissal id, then when that notification is canceled, notifications on other wearables
7822 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007823 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007824 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007825 * @param dismissalId the dismissal id of the notification.
7826 * @return this object for method chaining
7827 */
7828 public WearableExtender setDismissalId(String dismissalId) {
7829 mDismissalId = dismissalId;
7830 return this;
7831 }
7832
7833 /**
7834 * Returns the dismissal id of the notification.
7835 * @return the dismissal id of the notification or null if it has not been set.
7836 */
7837 public String getDismissalId() {
7838 return mDismissalId;
7839 }
7840
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007841 /**
7842 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7843 * posted from a phone to provide finer-grained control on what notifications are bridged
7844 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7845 * Features to Notifications</a> for more information.
7846 * @param bridgeTag the bridge tag of the notification.
7847 * @return this object for method chaining
7848 */
7849 public WearableExtender setBridgeTag(String bridgeTag) {
7850 mBridgeTag = bridgeTag;
7851 return this;
7852 }
7853
7854 /**
7855 * Returns the bridge tag of the notification.
7856 * @return the bridge tag or null if not present.
7857 */
7858 public String getBridgeTag() {
7859 return mBridgeTag;
7860 }
7861
Griff Hazen61a9e862014-05-22 16:05:19 -07007862 private void setFlag(int mask, boolean value) {
7863 if (value) {
7864 mFlags |= mask;
7865 } else {
7866 mFlags &= ~mask;
7867 }
7868 }
7869 }
7870
7871 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007872 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7873 * with car extensions:
7874 *
7875 * <ol>
7876 * <li>Create an {@link Notification.Builder}, setting any desired
7877 * properties.
7878 * <li>Create a {@link CarExtender}.
7879 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7880 * {@link CarExtender}.
7881 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7882 * to apply the extensions to a notification.
7883 * </ol>
7884 *
7885 * <pre class="prettyprint">
7886 * Notification notification = new Notification.Builder(context)
7887 * ...
7888 * .extend(new CarExtender()
7889 * .set*(...))
7890 * .build();
7891 * </pre>
7892 *
7893 * <p>Car extensions can be accessed on an existing notification by using the
7894 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7895 * to access values.
7896 */
7897 public static final class CarExtender implements Extender {
7898 private static final String TAG = "CarExtender";
7899
7900 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7901 private static final String EXTRA_LARGE_ICON = "large_icon";
7902 private static final String EXTRA_CONVERSATION = "car_conversation";
7903 private static final String EXTRA_COLOR = "app_color";
7904
7905 private Bitmap mLargeIcon;
7906 private UnreadConversation mUnreadConversation;
7907 private int mColor = Notification.COLOR_DEFAULT;
7908
7909 /**
7910 * Create a {@link CarExtender} with default options.
7911 */
7912 public CarExtender() {
7913 }
7914
7915 /**
7916 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7917 *
7918 * @param notif The notification from which to copy options.
7919 */
7920 public CarExtender(Notification notif) {
7921 Bundle carBundle = notif.extras == null ?
7922 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7923 if (carBundle != null) {
7924 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7925 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7926
7927 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7928 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7929 }
7930 }
7931
7932 /**
7933 * Apply car extensions to a notification that is being built. This is typically called by
7934 * the {@link Notification.Builder#extend(Notification.Extender)}
7935 * method of {@link Notification.Builder}.
7936 */
7937 @Override
7938 public Notification.Builder extend(Notification.Builder builder) {
7939 Bundle carExtensions = new Bundle();
7940
7941 if (mLargeIcon != null) {
7942 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7943 }
7944 if (mColor != Notification.COLOR_DEFAULT) {
7945 carExtensions.putInt(EXTRA_COLOR, mColor);
7946 }
7947
7948 if (mUnreadConversation != null) {
7949 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7950 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7951 }
7952
7953 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7954 return builder;
7955 }
7956
7957 /**
7958 * Sets the accent color to use when Android Auto presents the notification.
7959 *
7960 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7961 * to accent the displayed notification. However, not all colors are acceptable in an
7962 * automotive setting. This method can be used to override the color provided in the
7963 * notification in such a situation.
7964 */
Tor Norbye80756e32015-03-02 09:39:27 -08007965 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007966 mColor = color;
7967 return this;
7968 }
7969
7970 /**
7971 * Gets the accent color.
7972 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007973 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007974 */
Tor Norbye80756e32015-03-02 09:39:27 -08007975 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007976 public int getColor() {
7977 return mColor;
7978 }
7979
7980 /**
7981 * Sets the large icon of the car notification.
7982 *
7983 * If no large icon is set in the extender, Android Auto will display the icon
7984 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7985 *
7986 * @param largeIcon The large icon to use in the car notification.
7987 * @return This object for method chaining.
7988 */
7989 public CarExtender setLargeIcon(Bitmap largeIcon) {
7990 mLargeIcon = largeIcon;
7991 return this;
7992 }
7993
7994 /**
7995 * Gets the large icon used in this car notification, or null if no icon has been set.
7996 *
7997 * @return The large icon for the car notification.
7998 * @see CarExtender#setLargeIcon
7999 */
8000 public Bitmap getLargeIcon() {
8001 return mLargeIcon;
8002 }
8003
8004 /**
8005 * Sets the unread conversation in a message notification.
8006 *
8007 * @param unreadConversation The unread part of the conversation this notification conveys.
8008 * @return This object for method chaining.
8009 */
8010 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8011 mUnreadConversation = unreadConversation;
8012 return this;
8013 }
8014
8015 /**
8016 * Returns the unread conversation conveyed by this notification.
8017 * @see #setUnreadConversation(UnreadConversation)
8018 */
8019 public UnreadConversation getUnreadConversation() {
8020 return mUnreadConversation;
8021 }
8022
8023 /**
8024 * A class which holds the unread messages from a conversation.
8025 */
8026 public static class UnreadConversation {
8027 private static final String KEY_AUTHOR = "author";
8028 private static final String KEY_TEXT = "text";
8029 private static final String KEY_MESSAGES = "messages";
8030 private static final String KEY_REMOTE_INPUT = "remote_input";
8031 private static final String KEY_ON_REPLY = "on_reply";
8032 private static final String KEY_ON_READ = "on_read";
8033 private static final String KEY_PARTICIPANTS = "participants";
8034 private static final String KEY_TIMESTAMP = "timestamp";
8035
8036 private final String[] mMessages;
8037 private final RemoteInput mRemoteInput;
8038 private final PendingIntent mReplyPendingIntent;
8039 private final PendingIntent mReadPendingIntent;
8040 private final String[] mParticipants;
8041 private final long mLatestTimestamp;
8042
8043 UnreadConversation(String[] messages, RemoteInput remoteInput,
8044 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8045 String[] participants, long latestTimestamp) {
8046 mMessages = messages;
8047 mRemoteInput = remoteInput;
8048 mReadPendingIntent = readPendingIntent;
8049 mReplyPendingIntent = replyPendingIntent;
8050 mParticipants = participants;
8051 mLatestTimestamp = latestTimestamp;
8052 }
8053
8054 /**
8055 * Gets the list of messages conveyed by this notification.
8056 */
8057 public String[] getMessages() {
8058 return mMessages;
8059 }
8060
8061 /**
8062 * Gets the remote input that will be used to convey the response to a message list, or
8063 * null if no such remote input exists.
8064 */
8065 public RemoteInput getRemoteInput() {
8066 return mRemoteInput;
8067 }
8068
8069 /**
8070 * Gets the pending intent that will be triggered when the user replies to this
8071 * notification.
8072 */
8073 public PendingIntent getReplyPendingIntent() {
8074 return mReplyPendingIntent;
8075 }
8076
8077 /**
8078 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8079 * in this object's message list.
8080 */
8081 public PendingIntent getReadPendingIntent() {
8082 return mReadPendingIntent;
8083 }
8084
8085 /**
8086 * Gets the participants in the conversation.
8087 */
8088 public String[] getParticipants() {
8089 return mParticipants;
8090 }
8091
8092 /**
8093 * Gets the firs participant in the conversation.
8094 */
8095 public String getParticipant() {
8096 return mParticipants.length > 0 ? mParticipants[0] : null;
8097 }
8098
8099 /**
8100 * Gets the timestamp of the conversation.
8101 */
8102 public long getLatestTimestamp() {
8103 return mLatestTimestamp;
8104 }
8105
8106 Bundle getBundleForUnreadConversation() {
8107 Bundle b = new Bundle();
8108 String author = null;
8109 if (mParticipants != null && mParticipants.length > 1) {
8110 author = mParticipants[0];
8111 }
8112 Parcelable[] messages = new Parcelable[mMessages.length];
8113 for (int i = 0; i < messages.length; i++) {
8114 Bundle m = new Bundle();
8115 m.putString(KEY_TEXT, mMessages[i]);
8116 m.putString(KEY_AUTHOR, author);
8117 messages[i] = m;
8118 }
8119 b.putParcelableArray(KEY_MESSAGES, messages);
8120 if (mRemoteInput != null) {
8121 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8122 }
8123 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8124 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8125 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8126 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8127 return b;
8128 }
8129
8130 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8131 if (b == null) {
8132 return null;
8133 }
8134 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8135 String[] messages = null;
8136 if (parcelableMessages != null) {
8137 String[] tmp = new String[parcelableMessages.length];
8138 boolean success = true;
8139 for (int i = 0; i < tmp.length; i++) {
8140 if (!(parcelableMessages[i] instanceof Bundle)) {
8141 success = false;
8142 break;
8143 }
8144 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8145 if (tmp[i] == null) {
8146 success = false;
8147 break;
8148 }
8149 }
8150 if (success) {
8151 messages = tmp;
8152 } else {
8153 return null;
8154 }
8155 }
8156
8157 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8158 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8159
8160 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8161
8162 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8163 if (participants == null || participants.length != 1) {
8164 return null;
8165 }
8166
8167 return new UnreadConversation(messages,
8168 remoteInput,
8169 onReply,
8170 onRead,
8171 participants, b.getLong(KEY_TIMESTAMP));
8172 }
8173 };
8174
8175 /**
8176 * Builder class for {@link CarExtender.UnreadConversation} objects.
8177 */
8178 public static class Builder {
8179 private final List<String> mMessages = new ArrayList<String>();
8180 private final String mParticipant;
8181 private RemoteInput mRemoteInput;
8182 private PendingIntent mReadPendingIntent;
8183 private PendingIntent mReplyPendingIntent;
8184 private long mLatestTimestamp;
8185
8186 /**
8187 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8188 *
8189 * @param name The name of the other participant in the conversation.
8190 */
8191 public Builder(String name) {
8192 mParticipant = name;
8193 }
8194
8195 /**
8196 * Appends a new unread message to the list of messages for this conversation.
8197 *
8198 * The messages should be added from oldest to newest.
8199 *
8200 * @param message The text of the new unread message.
8201 * @return This object for method chaining.
8202 */
8203 public Builder addMessage(String message) {
8204 mMessages.add(message);
8205 return this;
8206 }
8207
8208 /**
8209 * Sets the pending intent and remote input which will convey the reply to this
8210 * notification.
8211 *
8212 * @param pendingIntent The pending intent which will be triggered on a reply.
8213 * @param remoteInput The remote input parcelable which will carry the reply.
8214 * @return This object for method chaining.
8215 *
8216 * @see CarExtender.UnreadConversation#getRemoteInput
8217 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8218 */
8219 public Builder setReplyAction(
8220 PendingIntent pendingIntent, RemoteInput remoteInput) {
8221 mRemoteInput = remoteInput;
8222 mReplyPendingIntent = pendingIntent;
8223
8224 return this;
8225 }
8226
8227 /**
8228 * Sets the pending intent that will be sent once the messages in this notification
8229 * are read.
8230 *
8231 * @param pendingIntent The pending intent to use.
8232 * @return This object for method chaining.
8233 */
8234 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8235 mReadPendingIntent = pendingIntent;
8236 return this;
8237 }
8238
8239 /**
8240 * Sets the timestamp of the most recent message in an unread conversation.
8241 *
8242 * If a messaging notification has been posted by your application and has not
8243 * yet been cancelled, posting a later notification with the same id and tag
8244 * but without a newer timestamp may result in Android Auto not displaying a
8245 * heads up notification for the later notification.
8246 *
8247 * @param timestamp The timestamp of the most recent message in the conversation.
8248 * @return This object for method chaining.
8249 */
8250 public Builder setLatestTimestamp(long timestamp) {
8251 mLatestTimestamp = timestamp;
8252 return this;
8253 }
8254
8255 /**
8256 * Builds a new unread conversation object.
8257 *
8258 * @return The new unread conversation object.
8259 */
8260 public UnreadConversation build() {
8261 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8262 String[] participants = { mParticipant };
8263 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8264 mReadPendingIntent, participants, mLatestTimestamp);
8265 }
8266 }
8267 }
8268
8269 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008270 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8271 * with a TV extension:
8272 *
8273 * <ol>
8274 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8275 * <li>Create a {@link TvExtender}.
8276 * <li>Set TV-specific properties using the {@code set} methods of
8277 * {@link TvExtender}.
8278 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8279 * to apply the extension to a notification.
8280 * </ol>
8281 *
8282 * <pre class="prettyprint">
8283 * Notification notification = new Notification.Builder(context)
8284 * ...
8285 * .extend(new TvExtender()
8286 * .set*(...))
8287 * .build();
8288 * </pre>
8289 *
8290 * <p>TV extensions can be accessed on an existing notification by using the
8291 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8292 * to access values.
8293 *
8294 * @hide
8295 */
8296 @SystemApi
8297 public static final class TvExtender implements Extender {
8298 private static final String TAG = "TvExtender";
8299
8300 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8301 private static final String EXTRA_FLAGS = "flags";
8302 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8303 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008304 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008305
8306 // Flags bitwise-ored to mFlags
8307 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8308
8309 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008310 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008311 private PendingIntent mContentIntent;
8312 private PendingIntent mDeleteIntent;
8313
8314 /**
8315 * Create a {@link TvExtender} with default options.
8316 */
8317 public TvExtender() {
8318 mFlags = FLAG_AVAILABLE_ON_TV;
8319 }
8320
8321 /**
8322 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8323 *
8324 * @param notif The notification from which to copy options.
8325 */
8326 public TvExtender(Notification notif) {
8327 Bundle bundle = notif.extras == null ?
8328 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8329 if (bundle != null) {
8330 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008331 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008332 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8333 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8334 }
8335 }
8336
8337 /**
8338 * Apply a TV extension to a notification that is being built. This is typically called by
8339 * the {@link Notification.Builder#extend(Notification.Extender)}
8340 * method of {@link Notification.Builder}.
8341 */
8342 @Override
8343 public Notification.Builder extend(Notification.Builder builder) {
8344 Bundle bundle = new Bundle();
8345
8346 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008347 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008348 if (mContentIntent != null) {
8349 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8350 }
8351
8352 if (mDeleteIntent != null) {
8353 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8354 }
8355
8356 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8357 return builder;
8358 }
8359
8360 /**
8361 * Returns true if this notification should be shown on TV. This method return true
8362 * if the notification was extended with a TvExtender.
8363 */
8364 public boolean isAvailableOnTv() {
8365 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8366 }
8367
8368 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008369 * Specifies the channel the notification should be delivered on when shown on TV.
8370 * It can be different from the channel that the notification is delivered to when
8371 * posting on a non-TV device.
8372 */
8373 public TvExtender setChannel(String channelId) {
8374 mChannelId = channelId;
8375 return this;
8376 }
8377
8378 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008379 * Specifies the channel the notification should be delivered on when shown on TV.
8380 * It can be different from the channel that the notification is delivered to when
8381 * posting on a non-TV device.
8382 */
8383 public TvExtender setChannelId(String channelId) {
8384 mChannelId = channelId;
8385 return this;
8386 }
8387
Jeff Sharkey000ce802017-04-29 13:13:27 -06008388 /** @removed */
8389 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008390 public String getChannel() {
8391 return mChannelId;
8392 }
8393
8394 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008395 * Returns the id of the channel this notification posts to on TV.
8396 */
8397 public String getChannelId() {
8398 return mChannelId;
8399 }
8400
8401 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008402 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8403 * If provided, it is used instead of the content intent specified
8404 * at the level of Notification.
8405 */
8406 public TvExtender setContentIntent(PendingIntent intent) {
8407 mContentIntent = intent;
8408 return this;
8409 }
8410
8411 /**
8412 * Returns the TV-specific content intent. If this method returns null, the
8413 * main content intent on the notification should be used.
8414 *
8415 * @see {@link Notification#contentIntent}
8416 */
8417 public PendingIntent getContentIntent() {
8418 return mContentIntent;
8419 }
8420
8421 /**
8422 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8423 * by the user on TV. If provided, it is used instead of the delete intent specified
8424 * at the level of Notification.
8425 */
8426 public TvExtender setDeleteIntent(PendingIntent intent) {
8427 mDeleteIntent = intent;
8428 return this;
8429 }
8430
8431 /**
8432 * Returns the TV-specific delete intent. If this method returns null, the
8433 * main delete intent on the notification should be used.
8434 *
8435 * @see {@link Notification#deleteIntent}
8436 */
8437 public PendingIntent getDeleteIntent() {
8438 return mDeleteIntent;
8439 }
8440 }
8441
8442 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008443 * Get an array of Notification objects from a parcelable array bundle field.
8444 * Update the bundle to have a typed array so fetches in the future don't need
8445 * to do an array copy.
8446 */
8447 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8448 Parcelable[] array = bundle.getParcelableArray(key);
8449 if (array instanceof Notification[] || array == null) {
8450 return (Notification[]) array;
8451 }
8452 Notification[] typedArray = Arrays.copyOf(array, array.length,
8453 Notification[].class);
8454 bundle.putParcelableArray(key, typedArray);
8455 return typedArray;
8456 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008457
8458 private static class BuilderRemoteViews extends RemoteViews {
8459 public BuilderRemoteViews(Parcel parcel) {
8460 super(parcel);
8461 }
8462
Kenny Guy77320062014-08-27 21:37:15 +01008463 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8464 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008465 }
8466
8467 @Override
8468 public BuilderRemoteViews clone() {
8469 Parcel p = Parcel.obtain();
8470 writeToParcel(p, 0);
8471 p.setDataPosition(0);
8472 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8473 p.recycle();
8474 return brv;
8475 }
8476 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008477
8478 private static class StandardTemplateParams {
8479 boolean hasProgress = true;
8480 boolean ambient = false;
8481 CharSequence title;
8482 CharSequence text;
Selim Cinek88188f22017-09-19 16:46:56 -07008483 boolean hideLargeIcon;
8484 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008485
8486 final StandardTemplateParams reset() {
8487 hasProgress = true;
8488 ambient = false;
8489 title = null;
8490 text = null;
8491 return this;
8492 }
8493
8494 final StandardTemplateParams hasProgress(boolean hasProgress) {
8495 this.hasProgress = hasProgress;
8496 return this;
8497 }
8498
8499 final StandardTemplateParams title(CharSequence title) {
8500 this.title = title;
8501 return this;
8502 }
8503
8504 final StandardTemplateParams text(CharSequence text) {
8505 this.text = text;
8506 return this;
8507 }
8508
Selim Cinek88188f22017-09-19 16:46:56 -07008509 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
8510 this.alwaysShowReply = alwaysShowReply;
8511 return this;
8512 }
8513
8514 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
8515 this.hideLargeIcon = hideLargeIcon;
8516 return this;
8517 }
8518
Adrian Roos70d7aa32017-01-11 15:39:06 -08008519 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008520 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008521 this.ambient = ambient;
8522 return this;
8523 }
8524
8525 final StandardTemplateParams fillTextsFrom(Builder b) {
8526 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008527 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8528
8529 // Big text notifications should contain their content when viewed in ambient mode.
8530 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8531 if (!ambient || TextUtils.isEmpty(text)) {
8532 text = extras.getCharSequence(EXTRA_TEXT);
8533 }
8534 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008535 return this;
8536 }
8537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008538}