blob: 046a128cc81ef4ae243381771d7096a22ab5dc8c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050092import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094/**
95 * A class that represents how a persistent notification is to be presented to
96 * the user using the {@link android.app.NotificationManager}.
97 *
Joe Onoratocb109a02011-01-18 17:57:41 -080098 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
99 * easier to construct Notifications.</p>
100 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For a guide to creating notifications, read the
104 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
105 * developer guide.</p>
106 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
108public class Notification implements Parcelable
109{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400110 private static final String TAG = "Notification";
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400114 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 */
116 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
117 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
118 = "android.intent.category.NOTIFICATION_PREFERENCES";
119
120 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
122 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 */
125 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
126
127 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400129 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
130 * what settings should be shown in the target app.
131 */
132 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
133
134 /**
135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
137 * that can be used to narrow down what settings should be shown in the target app.
138 */
139 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
143 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Use all default values (where applicable).
150 */
151 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Use the default notification sound. This will ignore any given
155 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
Chris Wren47c20a12014-06-18 17:27:29 -0400157 * <p>
158 * A notification that is noisy is more likely to be presented as a heads-up notification.
159 * </p>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 */
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int DEFAULT_SOUND = 1;
165
166 /**
167 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700169 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 *
Chris Wren47c20a12014-06-18 17:27:29 -0400171 * <p>
172 * A notification that vibrates is more likely to be presented as a heads-up notification.
173 * </p>
174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * Use the default notification lights. This will ignore the
182 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
183 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 */
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200191 * Maximum length of CharSequences accepted by Builder and friends.
192 *
193 * <p>
194 * Avoids spamming the system with overly large strings such as full e-mails.
195 */
196 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
197
198 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800199 * Maximum entries of reply text that are accepted by Builder and friends.
200 */
201 private static final int MAX_REPLY_HISTORY = 5;
202
203 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * Default value: {@link System#currentTimeMillis() Now}.
207 *
208 * Choose a timestamp that will be most relevant to the user. For most finite events, this
209 * corresponds to the time the event happened (or will happen, in the case of events that have
210 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800211 * timestamped according to when the activity began.
212 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800214 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * <ul>
216 * <li>Notification of a new chat message should be stamped when the message was received.</li>
217 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
218 * <li>Notification of a completed file download should be stamped when the download finished.</li>
219 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
220 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
221 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 * </ul>
223 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700224 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
225 * anymore by default and must be opted into by using
226 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public long when;
229
230 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700231 * The creation time of the notification
232 */
233 private long creationTime;
234
235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400237 *
238 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
Dan Sandler86647982015-05-13 23:41:13 -0400240 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700241 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public int icon;
243
244 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800245 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
246 * leave it at its default value of 0.
247 *
248 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700249 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 */
251 public int iconLevel;
252
253 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500254 * The number of events that this notification represents. For example, in a new mail
255 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800256 *
Julia Reynolds30331982017-04-27 10:12:50 -0400257 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500258 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
259 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Julia Reynolds30331982017-04-27 10:12:50 -0400261 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 /**
264 * The intent to execute when the expanded status entry is clicked. If
265 * this is an activity, it must include the
266 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800267 * that you take care of task management as described in the
268 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800269 * Stack</a> document. In particular, make sure to read the notification section
270 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
271 * Notifications</a> for the correct ways to launch an application from a
272 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
274 public PendingIntent contentIntent;
275
276 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500277 * The intent to execute when the notification is explicitly dismissed by the user, either with
278 * the "Clear All" button or by swiping it away individually.
279 *
280 * This probably shouldn't be launching an activity since several of those will be sent
281 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public PendingIntent deleteIntent;
284
285 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700286 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800287 *
Chris Wren47c20a12014-06-18 17:27:29 -0400288 * <p>
289 * The system UI may choose to display a heads-up notification, instead of
290 * launching this intent, while the user is using the device.
291 * </p>
292 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800293 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400294 */
295 public PendingIntent fullScreenIntent;
296
297 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400298 * Text that summarizes this notification for accessibility services.
299 *
300 * As of the L release, this text is no longer shown on screen, but it is still useful to
301 * accessibility services (where it serves as an audible announcement of the notification's
302 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400303 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public CharSequence tickerText;
307
308 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400309 * Formerly, a view showing the {@link #tickerText}.
310 *
311 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400312 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400313 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400315
316 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 * The view that will represent this notification in the notification list (which is pulled
318 * down from the status bar).
319 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500320 * As of N, this field may be null. The notification view is determined by the inputs
321 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400322 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400324 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public RemoteViews contentView;
326
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400327 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400328 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400329 * opportunity to show more detail. The system UI may choose to show this
330 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400331 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500332 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400333 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
334 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400336 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400337 public RemoteViews bigContentView;
338
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339
340 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400341 * A medium-format version of {@link #contentView}, providing the Notification an
342 * opportunity to add action buttons to contentView. At its discretion, the system UI may
343 * choose to show this as a heads-up notification, which will pop up so the user can see
344 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500346 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
348 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500351 public RemoteViews headsUpContentView;
352
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400353 /**
Dan Sandler86647982015-05-13 23:41:13 -0400354 * A large bitmap to be shown in the notification content area.
355 *
356 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Dan Sandler86647982015-05-13 23:41:13 -0400358 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800359 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
361 /**
362 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400365 * A notification that is noisy is more likely to be presented as a heads-up notification.
366 * </p>
367 *
368 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public Uri sound;
375
376 /**
377 * Use this constant as the value for audioStreamType to request that
378 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700379 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public static final int STREAM_DEFAULT = -1;
385
386 /**
387 * The audio stream type to use when playing the sound.
388 * Should be one of the STREAM_ constants from
389 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400390 *
391 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public int audioStreamType = STREAM_DEFAULT;
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400397 * The default value of {@link #audioAttributes}.
398 */
399 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
400 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
401 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
402 .build();
403
404 /**
405 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500406 *
407 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400408 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400410 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
411
412 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500413 * The pattern with which to vibrate.
414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * <p>
416 * To vibrate the default pattern, see {@link #defaults}.
417 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public long[] vibrate;
424
425 /**
426 * The color of the led. The hardware will do its best approximation.
427 *
428 * @see #FLAG_SHOW_LIGHTS
429 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Tor Norbye80756e32015-03-02 09:39:27 -0800432 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public int ledARGB;
435
436 /**
437 * The number of milliseconds for the LED to be on while it's flashing.
438 * The hardware will do its best approximation.
439 *
440 * @see #FLAG_SHOW_LIGHTS
441 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public int ledOnMS;
446
447 /**
448 * The number of milliseconds for the LED to be off while it's flashing.
449 * The hardware will do its best approximation.
450 *
451 * @see #FLAG_SHOW_LIGHTS
452 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500453 *
454 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public int ledOffMS;
458
459 /**
460 * Specifies which values should be taken from the defaults.
461 * <p>
462 * To set, OR the desired from {@link #DEFAULT_SOUND},
463 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
464 * values, use {@link #DEFAULT_ALL}.
465 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 *
467 * @deprecated use {@link NotificationChannel#getSound()} and
468 * {@link NotificationChannel#shouldShowLights()} and
469 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int defaults;
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
475 * Bit to be bitwise-ored into the {@link #flags} field that should be
476 * set if you want the LED on for this notification.
477 * <ul>
478 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
479 * or 0 for both ledOnMS and ledOffMS.</li>
480 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
481 * <li>To flash the LED, pass the number of milliseconds that it should
482 * be on and off to ledOnMS and ledOffMS.</li>
483 * </ul>
484 * <p>
485 * Since hardware varies, you are not guaranteed that any of the values
486 * you pass are honored exactly. Use the system defaults (TODO) if possible
487 * because they will be set to values that work on any given hardware.
488 * <p>
489 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
495
496 /**
497 * Bit to be bitwise-ored into the {@link #flags} field that should be
498 * set if this notification is in reference to something that is ongoing,
499 * like a phone call. It should not be set if this notification is in
500 * reference to something that happened at a particular point in time,
501 * like a missed phone call.
502 */
503 public static final int FLAG_ONGOING_EVENT = 0x00000002;
504
505 /**
506 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700507 * the audio will be repeated until the notification is
508 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
510 public static final int FLAG_INSISTENT = 0x00000004;
511
512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700514 * set if you would only like the sound, vibrate and ticker to be played
515 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
517 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
518
519 /**
520 * Bit to be bitwise-ored into the {@link #flags} field that should be
521 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500522 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 */
524 public static final int FLAG_AUTO_CANCEL = 0x00000010;
525
526 /**
527 * Bit to be bitwise-ored into the {@link #flags} field that should be
528 * set if the notification should not be canceled when the user clicks
529 * the Clear all button.
530 */
531 public static final int FLAG_NO_CLEAR = 0x00000020;
532
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700533 /**
534 * Bit to be bitwise-ored into the {@link #flags} field that should be
535 * set if this notification represents a currently running service. This
536 * will normally be set for you by {@link Service#startForeground}.
537 */
538 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
539
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400540 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500543 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400544 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700545 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400547
Griff Hazendfcb0802014-02-11 12:00:00 -0800548 /**
549 * Bit to be bitswise-ored into the {@link #flags} field that should be
550 * set if this notification is relevant to the current device only
551 * and it is not recommended that it bridge to other devices.
552 */
553 public static final int FLAG_LOCAL_ONLY = 0x00000100;
554
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700555 /**
556 * Bit to be bitswise-ored into the {@link #flags} field that should be
557 * set if this notification is the group summary for a group of notifications.
558 * Grouped notifications may display in a cluster or stack on devices which
559 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
560 */
561 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
562
Julia Reynoldse46bb372016-03-17 11:05:58 -0400563 /**
564 * Bit to be bitswise-ored into the {@link #flags} field that should be
565 * set if this notification is the group summary for an auto-group of notifications.
566 *
567 * @hide
568 */
569 @SystemApi
570 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
571
Julia Reynolds4db59552017-06-30 13:34:01 -0400572 /**
573 * @hide
574 */
575 public static final int FLAG_CAN_COLORIZE = 0x00000800;
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public int flags;
578
Tor Norbyed9273d62013-05-30 15:59:53 -0700579 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700580 @IntDef(prefix = { "PRIORITY_" }, value = {
581 PRIORITY_DEFAULT,
582 PRIORITY_LOW,
583 PRIORITY_MIN,
584 PRIORITY_HIGH,
585 PRIORITY_MAX
586 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700587 @Retention(RetentionPolicy.SOURCE)
588 public @interface Priority {}
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 * Default notification {@link #priority}. If your application does not prioritize its own
592 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500593 *
594 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500597 public static final int PRIORITY_DEFAULT = 0;
598
599 /**
600 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
601 * items smaller, or at a different position in the list, compared with your app's
602 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500603 *
604 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 public static final int PRIORITY_LOW = -1;
608
609 /**
610 * Lowest {@link #priority}; these items might not be shown to the user except under special
611 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500612 *
613 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500616 public static final int PRIORITY_MIN = -2;
617
618 /**
619 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
620 * show these items larger, or at a different position in notification lists, compared with
621 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 public static final int PRIORITY_HIGH = 1;
627
628 /**
629 * Highest {@link #priority}, for your application's most important items that require the
630 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_MAX = 2;
636
637 /**
638 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800639 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640 * Priority is an indication of how much of the user's valuable attention should be consumed by
641 * this notification. Low-priority notifications may be hidden from the user in certain
642 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500643 * system will make a determination about how to interpret this priority when presenting
644 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400645 *
646 * <p>
647 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
648 * as a heads-up notification.
649 * </p>
650 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700653 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800656
Dan Sandler26e81cf2014-05-06 10:01:27 -0400657 /**
658 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
659 * to be applied by the standard Style templates when presenting this notification.
660 *
661 * The current template design constructs a colorful header image by overlaying the
662 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
663 * ignored.
664 */
Tor Norbye80756e32015-03-02 09:39:27 -0800665 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400666 public int color = COLOR_DEFAULT;
667
668 /**
669 * Special value of {@link #color} telling the system not to decorate this notification with
670 * any special color but instead use default colors when presenting this notification.
671 */
Tor Norbye80756e32015-03-02 09:39:27 -0800672 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400673 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600674
675 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800676 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800677 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800678 */
679 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800680 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800681
682 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700683 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
684 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600685 * lockscreen).
686 *
687 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
688 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
689 * shown in all situations, but the contents are only available if the device is unlocked for
690 * the appropriate user.
691 *
692 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
693 * can be read even in an "insecure" context (that is, above a secure lockscreen).
694 * To modify the public version of this notification—for example, to redact some portions—see
695 * {@link Builder#setPublicVersion(Notification)}.
696 *
697 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
698 * and ticker until the user has bypassed the lockscreen.
699 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600700 public @Visibility int visibility;
701
702 /** @hide */
703 @IntDef(prefix = { "VISIBILITY_" }, value = {
704 VISIBILITY_PUBLIC,
705 VISIBILITY_PRIVATE,
706 VISIBILITY_SECRET,
707 })
708 @Retention(RetentionPolicy.SOURCE)
709 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710
Griff Hazenfc3922d2014-08-20 11:56:44 -0700711 /**
712 * Notification visibility: Show this notification in its entirety on all lockscreens.
713 *
714 * {@see #visibility}
715 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600716 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700717
718 /**
719 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
720 * private information on secure lockscreens.
721 *
722 * {@see #visibility}
723 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700725
726 /**
727 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
728 *
729 * {@see #visibility}
730 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600731 public static final int VISIBILITY_SECRET = -1;
732
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400734 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400736 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500737
738 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400739 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500740 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400741 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500742
743 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747
748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400756 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757
758 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400759 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 public static final String CATEGORY_ALARM = "alarm";
762
763 /**
764 * Notification category: progress of a long-running background operation.
765 */
766 public static final String CATEGORY_PROGRESS = "progress";
767
768 /**
769 * Notification category: social network or sharing update.
770 */
771 public static final String CATEGORY_SOCIAL = "social";
772
773 /**
774 * Notification category: error in background operation or authentication status.
775 */
776 public static final String CATEGORY_ERROR = "err";
777
778 /**
779 * Notification category: media transport control for playback.
780 */
781 public static final String CATEGORY_TRANSPORT = "transport";
782
783 /**
784 * Notification category: system or device status update. Reserved for system use.
785 */
786 public static final String CATEGORY_SYSTEM = "sys";
787
788 /**
789 * Notification category: indication of running background service.
790 */
791 public static final String CATEGORY_SERVICE = "service";
792
793 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400794 * Notification category: a specific, timely recommendation for a single thing.
795 * For example, a news app might want to recommend a news story it believes the user will
796 * want to read next.
797 */
798 public static final String CATEGORY_RECOMMENDATION = "recommendation";
799
800 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 * Notification category: ongoing information about device or contextual status.
802 */
803 public static final String CATEGORY_STATUS = "status";
804
805 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400806 * Notification category: user-scheduled reminder.
807 */
808 public static final String CATEGORY_REMINDER = "reminder";
809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
812 * that best describes this Notification. May be used by the system for ranking and filtering.
813 */
814 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500815
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700816 private String mGroupKey;
817
818 /**
819 * Get the key used to group this notification into a cluster or stack
820 * with other notifications on devices which support such rendering.
821 */
822 public String getGroup() {
823 return mGroupKey;
824 }
825
826 private String mSortKey;
827
828 /**
829 * Get a sort key that orders this notification among other notifications from the
830 * same package. This can be useful if an external sort was already applied and an app
831 * would like to preserve this. Notifications will be sorted lexicographically using this
832 * value, although providing different priorities in addition to providing sort key may
833 * cause this value to be ignored.
834 *
835 * <p>This sort key can also be used to order members of a notification group. See
836 * {@link Builder#setGroup}.
837 *
838 * @see String#compareTo(String)
839 */
840 public String getSortKey() {
841 return mSortKey;
842 }
843
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500844 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846 * <p>
847 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
848 * APIs, and are intended to be used by
849 * {@link android.service.notification.NotificationListenerService} implementations to extract
850 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public Bundle extras = new Bundle();
853
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700855 * All pending intents in the notification as the system needs to be able to access them but
856 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700857 * custom parcelable objects.
858 *
859 * @hide
860 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700861 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700862
863 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700864 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
865 * pending intents inside of it, so only those will get the behavior.
866 *
867 * @hide
868 */
Adrian Roosfb921842017-10-26 14:49:56 +0200869 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700870
871 /**
872 * Must be set by a process to start associating tokens with Notification objects
873 * coming in to it. This is set by NotificationManagerService.
874 *
875 * @hide
876 */
877 static public IBinder processWhitelistToken;
878
879 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880 * {@link #extras} key: this is the title of the notification,
881 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
882 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500883 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: this is the title of the notification when shown in expanded form,
887 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
888 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400889 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890
891 /**
892 * {@link #extras} key: this is the main text payload, as supplied to
893 * {@link Builder#setContentText(CharSequence)}.
894 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500895 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896
897 /**
898 * {@link #extras} key: this is a third line of text, as supplied to
899 * {@link Builder#setSubText(CharSequence)}.
900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902
903 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800904 * {@link #extras} key: this is the remote input history, as supplied to
905 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700906 *
907 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
908 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
909 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
910 * notifications once the other party has responded).
911 *
912 * The extra with this key is of type CharSequence[] and contains the most recent entry at
913 * the 0 index, the second most recent at the 1 index, etc.
914 *
915 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800916 */
917 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
918
919 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 * {@link #extras} key: this is a small piece of additional text as supplied to
921 * {@link Builder#setContentInfo(CharSequence)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
926 * {@link #extras} key: this is a line of summary information intended to be shown
927 * alongside expanded notifications, as supplied to (e.g.)
928 * {@link BigTextStyle#setSummaryText(CharSequence)}.
929 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400930 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200933 * {@link #extras} key: this is the longer text shown in the big form of a
934 * {@link BigTextStyle} notification, as supplied to
935 * {@link BigTextStyle#bigText(CharSequence)}.
936 */
937 public static final String EXTRA_BIG_TEXT = "android.bigText";
938
939 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
941 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400942 *
943 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400945 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500946 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947
948 /**
949 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
950 * notification payload, as
951 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400952 *
953 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400955 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be used instead of the one from
960 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
961 * shown in its expanded form, as supplied to
962 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
963 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400964 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965
966 /**
967 * {@link #extras} key: this is the progress value supplied to
968 * {@link Builder#setProgress(int, int, boolean)}.
969 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400970 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971
972 /**
973 * {@link #extras} key: this is the maximum value supplied to
974 * {@link Builder#setProgress(int, int, boolean)}.
975 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400976 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977
978 /**
979 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
980 * {@link Builder#setProgress(int, int, boolean)}.
981 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400982 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983
984 /**
985 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
986 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
987 * {@link Builder#setUsesChronometer(boolean)}.
988 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400989 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400990
991 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800992 * {@link #extras} key: whether the chronometer set on the notification should count down
993 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700994 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800995 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700996 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800997
998 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 * {@link #extras} key: whether {@link #when} should be shown,
1000 * as supplied to {@link Builder#setShowWhen(boolean)}.
1001 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001002 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003
1004 /**
1005 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1006 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1007 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001008 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009
1010 /**
1011 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1012 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1013 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001014 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001015
1016 /**
1017 * {@link #extras} key: A string representing the name of the specific
1018 * {@link android.app.Notification.Style} used to create this notification.
1019 */
Chris Wren91ad5632013-06-05 15:05:57 -04001020 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001023 * {@link #extras} key: A String array containing the people that this notification relates to,
1024 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001026 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001027
1028 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001029 * Allow certain system-generated notifications to appear before the device is provisioned.
1030 * Only available to notifications coming from the android package.
1031 * @hide
1032 */
Maurice Lam96c10032017-03-29 15:42:38 -07001033 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001034 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001035 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1036
1037 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001038 * {@link #extras} key: A
1039 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1040 * in the background when the notification is selected. The URI must point to an image stream
1041 * suitable for passing into
1042 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1043 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1044 * URI used for this purpose must require no permissions to read the image data.
1045 */
1046 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1047
1048 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001049 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001050 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001051 * {@link android.app.Notification.MediaStyle} notification.
1052 */
1053 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1054
1055 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001056 * {@link #extras} key: the indices of actions to be shown in the compact view,
1057 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1058 */
1059 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1060
Christoph Studer943aa672014-08-03 20:31:16 +02001061 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001062 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1063 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001064 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1065 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001066 */
1067 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1068
1069 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001070 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001071 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001072 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001073 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001074
1075 /**
1076 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1077 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001078 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1079 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001080 */
1081 public static final String EXTRA_MESSAGES = "android.messages";
1082
1083 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001084 * {@link #extras} key: an array of
1085 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1086 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1087 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1088 * array of bundles.
1089 */
1090 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1091
1092 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001093 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1094 * represents a group conversation.
1095 */
1096 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1097
1098 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001099 * {@link #extras} key: whether the notification should be colorized as
1100 * supplied to {@link Builder#setColorized(boolean)}}.
1101 */
1102 public static final String EXTRA_COLORIZED = "android.colorized";
1103
1104 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001105 * @hide
1106 */
1107 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1108
Selim Cinek247fa012016-02-18 09:50:48 -08001109 /**
1110 * @hide
1111 */
1112 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1113
Shane Brennan472a3b32016-12-12 15:28:10 -08001114 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001115 * @hide
1116 */
1117 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1118
1119 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001120 * {@link #extras} key: the audio contents of this notification.
1121 *
1122 * This is for use when rendering the notification on an audio-focused interface;
1123 * the audio contents are a complete sound sample that contains the contents/body of the
1124 * notification. This may be used in substitute of a Text-to-Speech reading of the
1125 * notification. For example if the notification represents a voice message this should point
1126 * to the audio of that message.
1127 *
1128 * The data stored under this key should be a String representation of a Uri that contains the
1129 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1130 *
1131 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1132 * has a field for holding data URI. That field can be used for audio.
1133 * See {@code Message#setData}.
1134 *
1135 * Example usage:
1136 * <pre>
1137 * {@code
1138 * Notification.Builder myBuilder = (build your Notification as normal);
1139 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1140 * }
1141 * </pre>
1142 */
1143 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1144
Dan Sandler80eaa592016-04-14 23:34:54 -04001145 /** @hide */
1146 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001147 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001148 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1149
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001150 /**
1151 * This is set on the notification shown by the activity manager about all apps
1152 * running in the background. It indicates that the notification should be shown
1153 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1154 * notification currently visible to the user. This is a string array of all
1155 * package names of the apps.
1156 * @hide
1157 */
1158 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1159
Dan Sandlerd63f9322015-05-06 15:18:49 -04001160 private Icon mSmallIcon;
1161 private Icon mLargeIcon;
1162
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001163 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001164 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001165
Julia Reynolds13d898c2017-02-02 12:22:05 -05001166 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001167 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001168
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001169 /** @hide */
1170 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1171 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1172 })
1173 @Retention(RetentionPolicy.SOURCE)
1174 public @interface GroupAlertBehavior {}
1175
1176 /**
1177 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1178 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1179 * notification will not be muted when it is in a group.
1180 */
1181 public static final int GROUP_ALERT_ALL = 0;
1182
1183 /**
1184 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1185 * notification in a group should be silenced (no sound or vibration) even if they are posted
1186 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001187 * mute this notification if this notification is a group child. This must be applied to all
1188 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001189 *
1190 * <p> For example, you might want to use this constant if you post a number of children
1191 * notifications at once (say, after a periodic sync), and only need to notify the user
1192 * audibly once.
1193 */
1194 public static final int GROUP_ALERT_SUMMARY = 1;
1195
1196 /**
1197 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1198 * notification in a group should be silenced (no sound or vibration) even if they are
1199 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1200 * to mute this notification if this notification is a group summary.
1201 *
1202 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001203 * in your group have content and the summary is only used to visually group notifications
1204 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001205 */
1206 public static final int GROUP_ALERT_CHILDREN = 2;
1207
Julia Reynolds2f431e22017-06-07 14:12:09 +00001208 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001209
Julia Reynolds13d898c2017-02-02 12:22:05 -05001210 /**
1211 * If this notification is being shown as a badge, always show as a number.
1212 */
1213 public static final int BADGE_ICON_NONE = 0;
1214
1215 /**
1216 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1217 * represent this notification.
1218 */
1219 public static final int BADGE_ICON_SMALL = 1;
1220
1221 /**
1222 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1223 * represent this notification.
1224 */
1225 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001226 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001227
Chris Wren51c75102013-07-16 20:49:17 -04001228 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001229 * Structure to encapsulate a named action that can be shown as part of this notification.
1230 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1231 * selected by the user.
1232 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001233 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1234 * or {@link Notification.Builder#addAction(Notification.Action)}
1235 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001236 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001237 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001238 /**
1239 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1240 * {@link RemoteInput}s.
1241 *
1242 * This is intended for {@link RemoteInput}s that only accept data, meaning
1243 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1244 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1245 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1246 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1247 *
1248 * You can test if a RemoteInput matches these constraints using
1249 * {@link RemoteInput#isDataOnly}.
1250 */
1251 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1252
Griff Hazen959591e2014-05-15 22:26:18 -07001253 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001254 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001255 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001256 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001257
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001258 /**
1259 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001260 *
1261 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001262 */
Dan Sandler86647982015-05-13 23:41:13 -04001263 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001265
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001266 /**
1267 * Title of the action.
1268 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001269 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001270
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001271 /**
1272 * Intent to send when the user invokes this action. May be null, in which case the action
1273 * may be rendered in a disabled presentation by the system UI.
1274 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001275 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001276
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001277 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001278 if (in.readInt() != 0) {
1279 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001280 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1281 icon = mIcon.getResId();
1282 }
Dan Sandler86647982015-05-13 23:41:13 -04001283 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001284 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1285 if (in.readInt() == 1) {
1286 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1287 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001288 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001289 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001290 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001291 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001292
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001293 /**
Dan Sandler86647982015-05-13 23:41:13 -04001294 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001295 */
Dan Sandler86647982015-05-13 23:41:13 -04001296 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001297 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001298 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001299 }
1300
Adrian Roos7af53622016-10-12 13:44:05 -07001301 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001302 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001303 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001304 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001305 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1306 this.icon = icon.getResId();
1307 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001308 this.title = title;
1309 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001310 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001311 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001312 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001313 }
1314
1315 /**
Dan Sandler86647982015-05-13 23:41:13 -04001316 * Return an icon representing the action.
1317 */
1318 public Icon getIcon() {
1319 if (mIcon == null && icon != 0) {
1320 // you snuck an icon in here without using the builder; let's try to keep it
1321 mIcon = Icon.createWithResource("", icon);
1322 }
1323 return mIcon;
1324 }
1325
1326 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001327 * Get additional metadata carried around with this Action.
1328 */
1329 public Bundle getExtras() {
1330 return mExtras;
1331 }
1332
1333 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001334 * Return whether the platform should automatically generate possible replies for this
1335 * {@link Action}
1336 */
1337 public boolean getAllowGeneratedReplies() {
1338 return mAllowGeneratedReplies;
1339 }
1340
1341 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001342 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001343 * May return null if no remote inputs were added. Only returns inputs which accept
1344 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001345 */
1346 public RemoteInput[] getRemoteInputs() {
1347 return mRemoteInputs;
1348 }
1349
1350 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001351 * Get the list of inputs to be collected from the user that ONLY accept data when this
1352 * action is sent. These remote inputs are guaranteed to return true on a call to
1353 * {@link RemoteInput#isDataOnly}.
1354 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001355 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001356 *
1357 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1358 * of non-textual RemoteInputs do not access these remote inputs.
1359 */
1360 public RemoteInput[] getDataOnlyRemoteInputs() {
1361 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1362 }
1363
1364 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001365 * Builder class for {@link Action} objects.
1366 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001367 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001368 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001369 private final CharSequence mTitle;
1370 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001371 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001372 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001373 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001374
1375 /**
1376 * Construct a new builder for {@link Action} object.
1377 * @param icon icon to show for this action
1378 * @param title the title of the action
1379 * @param intent the {@link PendingIntent} to fire when users trigger this action
1380 */
Dan Sandler86647982015-05-13 23:41:13 -04001381 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001382 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001383 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001384 }
1385
1386 /**
1387 * Construct a new builder for {@link Action} object.
1388 * @param icon icon to show for this action
1389 * @param title the title of the action
1390 * @param intent the {@link PendingIntent} to fire when users trigger this action
1391 */
1392 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001393 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001394 }
1395
1396 /**
1397 * Construct a new builder for {@link Action} object using the fields from an
1398 * {@link Action}.
1399 * @param action the action to read fields from.
1400 */
1401 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001402 this(action.getIcon(), action.title, action.actionIntent,
1403 new Bundle(action.mExtras), action.getRemoteInputs(),
1404 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001405 }
1406
Dan Sandler86647982015-05-13 23:41:13 -04001407 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001408 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001409 mIcon = icon;
1410 mTitle = title;
1411 mIntent = intent;
1412 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001413 if (remoteInputs != null) {
1414 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1415 Collections.addAll(mRemoteInputs, remoteInputs);
1416 }
Adrian Roos7af53622016-10-12 13:44:05 -07001417 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001418 }
1419
1420 /**
1421 * Merge additional metadata into this builder.
1422 *
1423 * <p>Values within the Bundle will replace existing extras values in this Builder.
1424 *
1425 * @see Notification.Action#extras
1426 */
1427 public Builder addExtras(Bundle extras) {
1428 if (extras != null) {
1429 mExtras.putAll(extras);
1430 }
1431 return this;
1432 }
1433
1434 /**
1435 * Get the metadata Bundle used by this Builder.
1436 *
1437 * <p>The returned Bundle is shared with this Builder.
1438 */
1439 public Bundle getExtras() {
1440 return mExtras;
1441 }
1442
1443 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001444 * Add an input to be collected from the user when this action is sent.
1445 * Response values can be retrieved from the fired intent by using the
1446 * {@link RemoteInput#getResultsFromIntent} function.
1447 * @param remoteInput a {@link RemoteInput} to add to the action
1448 * @return this object for method chaining
1449 */
1450 public Builder addRemoteInput(RemoteInput remoteInput) {
1451 if (mRemoteInputs == null) {
1452 mRemoteInputs = new ArrayList<RemoteInput>();
1453 }
1454 mRemoteInputs.add(remoteInput);
1455 return this;
1456 }
1457
1458 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001459 * Set whether the platform should automatically generate possible replies to add to
1460 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1461 * {@link RemoteInput}, this has no effect.
1462 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1463 * otherwise
1464 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001465 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001466 */
1467 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1468 mAllowGeneratedReplies = allowGeneratedReplies;
1469 return this;
1470 }
1471
1472 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001473 * Apply an extender to this action builder. Extenders may be used to add
1474 * metadata or change options on this builder.
1475 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001476 public Builder extend(Extender extender) {
1477 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001478 return this;
1479 }
1480
1481 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001482 * Combine all of the options that have been set and return a new {@link Action}
1483 * object.
1484 * @return the built action
1485 */
1486 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001487 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1488 RemoteInput[] previousDataInputs =
1489 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001490 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001491 for (RemoteInput input : previousDataInputs) {
1492 dataOnlyInputs.add(input);
1493 }
1494 }
1495 List<RemoteInput> textInputs = new ArrayList<>();
1496 if (mRemoteInputs != null) {
1497 for (RemoteInput input : mRemoteInputs) {
1498 if (input.isDataOnly()) {
1499 dataOnlyInputs.add(input);
1500 } else {
1501 textInputs.add(input);
1502 }
1503 }
1504 }
1505 if (!dataOnlyInputs.isEmpty()) {
1506 RemoteInput[] dataInputsArr =
1507 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1508 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1509 }
1510 RemoteInput[] textInputsArr = textInputs.isEmpty()
1511 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1512 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001513 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001514 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001515 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001516
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001517 @Override
1518 public Action clone() {
1519 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001520 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001521 title,
1522 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001523 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001524 getRemoteInputs(),
1525 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001526 }
1527 @Override
1528 public int describeContents() {
1529 return 0;
1530 }
1531 @Override
1532 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001533 final Icon ic = getIcon();
1534 if (ic != null) {
1535 out.writeInt(1);
1536 ic.writeToParcel(out, 0);
1537 } else {
1538 out.writeInt(0);
1539 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001540 TextUtils.writeToParcel(title, out, flags);
1541 if (actionIntent != null) {
1542 out.writeInt(1);
1543 actionIntent.writeToParcel(out, flags);
1544 } else {
1545 out.writeInt(0);
1546 }
Griff Hazen959591e2014-05-15 22:26:18 -07001547 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001548 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001549 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001550 }
Griff Hazen959591e2014-05-15 22:26:18 -07001551 public static final Parcelable.Creator<Action> CREATOR =
1552 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001553 public Action createFromParcel(Parcel in) {
1554 return new Action(in);
1555 }
1556 public Action[] newArray(int size) {
1557 return new Action[size];
1558 }
1559 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001560
1561 /**
1562 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1563 * metadata or change options on an action builder.
1564 */
1565 public interface Extender {
1566 /**
1567 * Apply this extender to a notification action builder.
1568 * @param builder the builder to be modified.
1569 * @return the build object for chaining.
1570 */
1571 public Builder extend(Builder builder);
1572 }
1573
1574 /**
1575 * Wearable extender for notification actions. To add extensions to an action,
1576 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1577 * the {@code WearableExtender()} constructor and apply it to a
1578 * {@link android.app.Notification.Action.Builder} using
1579 * {@link android.app.Notification.Action.Builder#extend}.
1580 *
1581 * <pre class="prettyprint">
1582 * Notification.Action action = new Notification.Action.Builder(
1583 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001584 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001585 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001586 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001587 */
1588 public static final class WearableExtender implements Extender {
1589 /** Notification action extra which contains wearable extensions */
1590 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1591
Pete Gastaf6781d2014-10-07 15:17:05 -04001592 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001593 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001594 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1595 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1596 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001597
1598 // Flags bitwise-ored to mFlags
1599 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001600 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001601 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001602
1603 // Default value for flags integer
1604 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1605
1606 private int mFlags = DEFAULT_FLAGS;
1607
Pete Gastaf6781d2014-10-07 15:17:05 -04001608 private CharSequence mInProgressLabel;
1609 private CharSequence mConfirmLabel;
1610 private CharSequence mCancelLabel;
1611
Griff Hazen61a9e862014-05-22 16:05:19 -07001612 /**
1613 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1614 * options.
1615 */
1616 public WearableExtender() {
1617 }
1618
1619 /**
1620 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1621 * wearable options present in an existing notification action.
1622 * @param action the notification action to inspect.
1623 */
1624 public WearableExtender(Action action) {
1625 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1626 if (wearableBundle != null) {
1627 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001628 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1629 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1630 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001631 }
1632 }
1633
1634 /**
1635 * Apply wearable extensions to a notification action that is being built. This is
1636 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1637 * method of {@link android.app.Notification.Action.Builder}.
1638 */
1639 @Override
1640 public Action.Builder extend(Action.Builder builder) {
1641 Bundle wearableBundle = new Bundle();
1642
1643 if (mFlags != DEFAULT_FLAGS) {
1644 wearableBundle.putInt(KEY_FLAGS, mFlags);
1645 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001646 if (mInProgressLabel != null) {
1647 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1648 }
1649 if (mConfirmLabel != null) {
1650 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1651 }
1652 if (mCancelLabel != null) {
1653 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1654 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001655
1656 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1657 return builder;
1658 }
1659
1660 @Override
1661 public WearableExtender clone() {
1662 WearableExtender that = new WearableExtender();
1663 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001664 that.mInProgressLabel = this.mInProgressLabel;
1665 that.mConfirmLabel = this.mConfirmLabel;
1666 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001667 return that;
1668 }
1669
1670 /**
1671 * Set whether this action is available when the wearable device is not connected to
1672 * a companion device. The user can still trigger this action when the wearable device is
1673 * offline, but a visual hint will indicate that the action may not be available.
1674 * Defaults to true.
1675 */
1676 public WearableExtender setAvailableOffline(boolean availableOffline) {
1677 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1678 return this;
1679 }
1680
1681 /**
1682 * Get whether this action is available when the wearable device is not connected to
1683 * a companion device. The user can still trigger this action when the wearable device is
1684 * offline, but a visual hint will indicate that the action may not be available.
1685 * Defaults to true.
1686 */
1687 public boolean isAvailableOffline() {
1688 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1689 }
1690
1691 private void setFlag(int mask, boolean value) {
1692 if (value) {
1693 mFlags |= mask;
1694 } else {
1695 mFlags &= ~mask;
1696 }
1697 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001698
1699 /**
1700 * Set a label to display while the wearable is preparing to automatically execute the
1701 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1702 *
1703 * @param label the label to display while the action is being prepared to execute
1704 * @return this object for method chaining
1705 */
1706 public WearableExtender setInProgressLabel(CharSequence label) {
1707 mInProgressLabel = label;
1708 return this;
1709 }
1710
1711 /**
1712 * Get the label to display while the wearable is preparing to automatically execute
1713 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1714 *
1715 * @return the label to display while the action is being prepared to execute
1716 */
1717 public CharSequence getInProgressLabel() {
1718 return mInProgressLabel;
1719 }
1720
1721 /**
1722 * Set a label to display to confirm that the action should be executed.
1723 * This is usually an imperative verb like "Send".
1724 *
1725 * @param label the label to confirm the action should be executed
1726 * @return this object for method chaining
1727 */
1728 public WearableExtender setConfirmLabel(CharSequence label) {
1729 mConfirmLabel = label;
1730 return this;
1731 }
1732
1733 /**
1734 * Get the label to display to confirm that the action should be executed.
1735 * This is usually an imperative verb like "Send".
1736 *
1737 * @return the label to confirm the action should be executed
1738 */
1739 public CharSequence getConfirmLabel() {
1740 return mConfirmLabel;
1741 }
1742
1743 /**
1744 * Set a label to display to cancel the action.
1745 * This is usually an imperative verb, like "Cancel".
1746 *
1747 * @param label the label to display to cancel the action
1748 * @return this object for method chaining
1749 */
1750 public WearableExtender setCancelLabel(CharSequence label) {
1751 mCancelLabel = label;
1752 return this;
1753 }
1754
1755 /**
1756 * Get the label to display to cancel the action.
1757 * This is usually an imperative verb like "Cancel".
1758 *
1759 * @return the label to display to cancel the action
1760 */
1761 public CharSequence getCancelLabel() {
1762 return mCancelLabel;
1763 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001764
1765 /**
1766 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1767 * platform that it can generate the appropriate transitions.
1768 * @param hintLaunchesActivity {@code true} if the content intent will launch
1769 * an activity and transitions should be generated, false otherwise.
1770 * @return this object for method chaining
1771 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001772 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001773 boolean hintLaunchesActivity) {
1774 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1775 return this;
1776 }
1777
1778 /**
1779 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1780 * platform that it can generate the appropriate transitions
1781 * @return {@code true} if the content intent will launch an activity and transitions
1782 * should be generated, false otherwise. The default value is {@code false} if this was
1783 * never set.
1784 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001785 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001786 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1787 }
Alex Hills9f087612016-06-07 09:08:59 -04001788
1789 /**
1790 * Set a hint that this Action should be displayed inline.
1791 *
1792 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1793 * otherwise
1794 * @return this object for method chaining
1795 */
1796 public WearableExtender setHintDisplayActionInline(
1797 boolean hintDisplayInline) {
1798 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1799 return this;
1800 }
1801
1802 /**
1803 * Get a hint that this Action should be displayed inline.
1804 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001805 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001806 * otherwise. The default value is {@code false} if this was never set.
1807 */
1808 public boolean getHintDisplayActionInline() {
1809 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1810 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001811 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001812 }
1813
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001814 /**
1815 * Array of all {@link Action} structures attached to this notification by
1816 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1817 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1818 * interface for invoking actions.
1819 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001820 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001821
1822 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001823 * Replacement version of this notification whose content will be shown
1824 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1825 * and {@link #VISIBILITY_PUBLIC}.
1826 */
1827 public Notification publicVersion;
1828
1829 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001830 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001831 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 */
1833 public Notification()
1834 {
1835 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001836 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001837 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 * @hide
1842 */
1843 public Notification(Context context, int icon, CharSequence tickerText, long when,
1844 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1845 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001846 new Builder(context)
1847 .setWhen(when)
1848 .setSmallIcon(icon)
1849 .setTicker(tickerText)
1850 .setContentTitle(contentTitle)
1851 .setContentText(contentText)
1852 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1853 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855
1856 /**
1857 * Constructs a Notification object with the information needed to
1858 * have a status bar icon without the standard expanded view.
1859 *
1860 * @param icon The resource id of the icon to put in the status bar.
1861 * @param tickerText The text that flows by in the status bar when the notification first
1862 * activates.
1863 * @param when The time to show in the time field. In the System.currentTimeMillis
1864 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001865 *
1866 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001868 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 public Notification(int icon, CharSequence tickerText, long when)
1870 {
1871 this.icon = icon;
1872 this.tickerText = tickerText;
1873 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001874 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876
1877 /**
1878 * Unflatten the notification from a parcel.
1879 */
Svet Ganovddb94882016-06-23 19:55:24 -07001880 @SuppressWarnings("unchecked")
1881 public Notification(Parcel parcel) {
1882 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1883 // intents in extras are always written as the last entry.
1884 readFromParcelImpl(parcel);
1885 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001886 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001887 }
1888
1889 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 {
1891 int version = parcel.readInt();
1892
Adrian Roosfb921842017-10-26 14:49:56 +02001893 mWhitelistToken = parcel.readStrongBinder();
1894 if (mWhitelistToken == null) {
1895 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07001896 }
1897 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02001898 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07001899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001901 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001902 if (parcel.readInt() != 0) {
1903 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001904 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1905 icon = mSmallIcon.getResId();
1906 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 number = parcel.readInt();
1909 if (parcel.readInt() != 0) {
1910 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1911 }
1912 if (parcel.readInt() != 0) {
1913 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1914 }
1915 if (parcel.readInt() != 0) {
1916 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1917 }
1918 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001919 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001920 }
1921 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1923 }
Joe Onorato561d3852010-11-20 18:09:34 -08001924 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001925 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 defaults = parcel.readInt();
1928 flags = parcel.readInt();
1929 if (parcel.readInt() != 0) {
1930 sound = Uri.CREATOR.createFromParcel(parcel);
1931 }
1932
1933 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001934 if (parcel.readInt() != 0) {
1935 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 vibrate = parcel.createLongArray();
1938 ledARGB = parcel.readInt();
1939 ledOnMS = parcel.readInt();
1940 ledOffMS = parcel.readInt();
1941 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001942
1943 if (parcel.readInt() != 0) {
1944 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1945 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001946
1947 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001948
John Spurlockfd7f1e02014-03-18 16:41:57 -04001949 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001950
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001951 mGroupKey = parcel.readString();
1952
1953 mSortKey = parcel.readString();
1954
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001955 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01001956 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001957
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001958 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1959
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001960 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001961 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1962 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001963
Chris Wren8fd39ec2014-02-27 17:43:26 -05001964 if (parcel.readInt() != 0) {
1965 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1966 }
1967
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001968 visibility = parcel.readInt();
1969
1970 if (parcel.readInt() != 0) {
1971 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1972 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001973
1974 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001975
1976 if (parcel.readInt() != 0) {
1977 mChannelId = parcel.readString();
1978 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001979 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001980
1981 if (parcel.readInt() != 0) {
1982 mShortcutId = parcel.readString();
1983 }
1984
1985 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001986
1987 if (parcel.readInt() != 0) {
1988 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1989 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001990
1991 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993
Andy Stadler110988c2010-12-03 14:29:16 -08001994 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001995 public Notification clone() {
1996 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001997 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001998 return that;
1999 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002000
Daniel Sandler1a497d32013-04-18 14:52:45 -04002001 /**
2002 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2003 * of this into that.
2004 * @hide
2005 */
2006 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002007 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002008 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002009 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002010 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002011 that.number = this.number;
2012
2013 // PendingIntents are global, so there's no reason (or way) to clone them.
2014 that.contentIntent = this.contentIntent;
2015 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002016 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002017
2018 if (this.tickerText != null) {
2019 that.tickerText = this.tickerText.toString();
2020 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002021 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002022 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002023 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002024 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002025 that.contentView = this.contentView.clone();
2026 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002027 if (heavy && this.mLargeIcon != null) {
2028 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002029 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002030 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002031 that.sound = this.sound; // android.net.Uri is immutable
2032 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002033 if (this.audioAttributes != null) {
2034 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2035 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002036
2037 final long[] vibrate = this.vibrate;
2038 if (vibrate != null) {
2039 final int N = vibrate.length;
2040 final long[] vib = that.vibrate = new long[N];
2041 System.arraycopy(vibrate, 0, vib, 0, N);
2042 }
2043
2044 that.ledARGB = this.ledARGB;
2045 that.ledOnMS = this.ledOnMS;
2046 that.ledOffMS = this.ledOffMS;
2047 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002048
Joe Onorato18e69df2010-05-17 22:26:12 -07002049 that.flags = this.flags;
2050
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002052
John Spurlockfd7f1e02014-03-18 16:41:57 -04002053 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002054
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002055 that.mGroupKey = this.mGroupKey;
2056
2057 that.mSortKey = this.mSortKey;
2058
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002059 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002060 try {
2061 that.extras = new Bundle(this.extras);
2062 // will unparcel
2063 that.extras.size();
2064 } catch (BadParcelableException e) {
2065 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2066 that.extras = null;
2067 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002068 }
2069
Felipe Lemedd85da62016-06-28 11:29:54 -07002070 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2071 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002072 }
2073
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002074 if (this.actions != null) {
2075 that.actions = new Action[this.actions.length];
2076 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002077 if ( this.actions[i] != null) {
2078 that.actions[i] = this.actions[i].clone();
2079 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002080 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002081 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002082
Daniel Sandler1a497d32013-04-18 14:52:45 -04002083 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002084 that.bigContentView = this.bigContentView.clone();
2085 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002086
Chris Wren8fd39ec2014-02-27 17:43:26 -05002087 if (heavy && this.headsUpContentView != null) {
2088 that.headsUpContentView = this.headsUpContentView.clone();
2089 }
2090
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002091 that.visibility = this.visibility;
2092
2093 if (this.publicVersion != null) {
2094 that.publicVersion = new Notification();
2095 this.publicVersion.cloneInto(that.publicVersion, heavy);
2096 }
2097
Dan Sandler26e81cf2014-05-06 10:01:27 -04002098 that.color = this.color;
2099
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002100 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002101 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002102 that.mShortcutId = this.mShortcutId;
2103 that.mBadgeIcon = this.mBadgeIcon;
2104 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002105 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002106
Daniel Sandler1a497d32013-04-18 14:52:45 -04002107 if (!heavy) {
2108 that.lightenPayload(); // will clean out extras
2109 }
2110 }
2111
2112 /**
2113 * Removes heavyweight parts of the Notification object for archival or for sending to
2114 * listeners when the full contents are not necessary.
2115 * @hide
2116 */
2117 public final void lightenPayload() {
2118 tickerView = null;
2119 contentView = null;
2120 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002121 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002122 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002123 if (extras != null && !extras.isEmpty()) {
2124 final Set<String> keyset = extras.keySet();
2125 final int N = keyset.size();
2126 final String[] keys = keyset.toArray(new String[N]);
2127 for (int i=0; i<N; i++) {
2128 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002129 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2130 continue;
2131 }
Dan Sandler50128532015-12-08 15:42:41 -05002132 final Object obj = extras.get(key);
2133 if (obj != null &&
2134 ( obj instanceof Parcelable
2135 || obj instanceof Parcelable[]
2136 || obj instanceof SparseArray
2137 || obj instanceof ArrayList)) {
2138 extras.remove(key);
2139 }
2140 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002141 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002142 }
2143
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002144 /**
2145 * Make sure this CharSequence is safe to put into a bundle, which basically
2146 * means it had better not be some custom Parcelable implementation.
2147 * @hide
2148 */
2149 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002150 if (cs == null) return cs;
2151 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2152 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2153 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002154 if (cs instanceof Parcelable) {
2155 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2156 + " instance is a custom Parcelable and not allowed in Notification");
2157 return cs.toString();
2158 }
Selim Cinek60a54252016-02-26 17:03:25 -08002159 return removeTextSizeSpans(cs);
2160 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002161
Selim Cinek60a54252016-02-26 17:03:25 -08002162 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2163 if (charSequence instanceof Spanned) {
2164 Spanned ss = (Spanned) charSequence;
2165 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2166 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2167 for (Object span : spans) {
2168 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002169 if (resultSpan instanceof CharacterStyle) {
2170 resultSpan = ((CharacterStyle) span).getUnderlying();
2171 }
2172 if (resultSpan instanceof TextAppearanceSpan) {
2173 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002174 resultSpan = new TextAppearanceSpan(
2175 originalSpan.getFamily(),
2176 originalSpan.getTextStyle(),
2177 -1,
2178 originalSpan.getTextColor(),
2179 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002180 } else if (resultSpan instanceof RelativeSizeSpan
2181 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002182 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002183 } else {
2184 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002185 }
2186 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2187 ss.getSpanFlags(span));
2188 }
2189 return builder;
2190 }
2191 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002192 }
2193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 public int describeContents() {
2195 return 0;
2196 }
2197
2198 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002199 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 */
Svet Ganovddb94882016-06-23 19:55:24 -07002201 public void writeToParcel(Parcel parcel, int flags) {
2202 // We need to mark all pending intents getting into the notification
2203 // system as being put there to later allow the notification ranker
2204 // to launch them and by doing so add the app to the battery saver white
2205 // list for a short period of time. The problem is that the system
2206 // cannot look into the extras as there may be parcelables there that
2207 // the platform does not know how to handle. To go around that we have
2208 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002209 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002210 if (collectPendingIntents) {
2211 PendingIntent.setOnMarshaledListener(
2212 (PendingIntent intent, Parcel out, int outFlags) -> {
2213 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002214 if (allPendingIntents == null) {
2215 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002216 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002217 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002218 }
2219 });
2220 }
2221 try {
2222 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002223 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002224 writeToParcelImpl(parcel, flags);
2225 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002226 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002227 } finally {
2228 if (collectPendingIntents) {
2229 PendingIntent.setOnMarshaledListener(null);
2230 }
2231 }
2232 }
2233
2234 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 parcel.writeInt(1);
2236
Adrian Roosfb921842017-10-26 14:49:56 +02002237 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002239 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002240 if (mSmallIcon == null && icon != 0) {
2241 // you snuck an icon in here without using the builder; let's try to keep it
2242 mSmallIcon = Icon.createWithResource("", icon);
2243 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002244 if (mSmallIcon != null) {
2245 parcel.writeInt(1);
2246 mSmallIcon.writeToParcel(parcel, 0);
2247 } else {
2248 parcel.writeInt(0);
2249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 parcel.writeInt(number);
2251 if (contentIntent != null) {
2252 parcel.writeInt(1);
2253 contentIntent.writeToParcel(parcel, 0);
2254 } else {
2255 parcel.writeInt(0);
2256 }
2257 if (deleteIntent != null) {
2258 parcel.writeInt(1);
2259 deleteIntent.writeToParcel(parcel, 0);
2260 } else {
2261 parcel.writeInt(0);
2262 }
2263 if (tickerText != null) {
2264 parcel.writeInt(1);
2265 TextUtils.writeToParcel(tickerText, parcel, flags);
2266 } else {
2267 parcel.writeInt(0);
2268 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002269 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002270 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002271 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002272 } else {
2273 parcel.writeInt(0);
2274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 if (contentView != null) {
2276 parcel.writeInt(1);
2277 contentView.writeToParcel(parcel, 0);
2278 } else {
2279 parcel.writeInt(0);
2280 }
Selim Cinek279fa862016-06-14 10:57:25 -07002281 if (mLargeIcon == null && largeIcon != null) {
2282 // you snuck an icon in here without using the builder; let's try to keep it
2283 mLargeIcon = Icon.createWithBitmap(largeIcon);
2284 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002285 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002286 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002287 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002288 } else {
2289 parcel.writeInt(0);
2290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 parcel.writeInt(defaults);
2293 parcel.writeInt(this.flags);
2294
2295 if (sound != null) {
2296 parcel.writeInt(1);
2297 sound.writeToParcel(parcel, 0);
2298 } else {
2299 parcel.writeInt(0);
2300 }
2301 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002302
2303 if (audioAttributes != null) {
2304 parcel.writeInt(1);
2305 audioAttributes.writeToParcel(parcel, 0);
2306 } else {
2307 parcel.writeInt(0);
2308 }
2309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 parcel.writeLongArray(vibrate);
2311 parcel.writeInt(ledARGB);
2312 parcel.writeInt(ledOnMS);
2313 parcel.writeInt(ledOffMS);
2314 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002315
2316 if (fullScreenIntent != null) {
2317 parcel.writeInt(1);
2318 fullScreenIntent.writeToParcel(parcel, 0);
2319 } else {
2320 parcel.writeInt(0);
2321 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002322
2323 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002324
John Spurlockfd7f1e02014-03-18 16:41:57 -04002325 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002326
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002327 parcel.writeString(mGroupKey);
2328
2329 parcel.writeString(mSortKey);
2330
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002331 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002332
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002333 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002334
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002335 if (bigContentView != null) {
2336 parcel.writeInt(1);
2337 bigContentView.writeToParcel(parcel, 0);
2338 } else {
2339 parcel.writeInt(0);
2340 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002341
Chris Wren8fd39ec2014-02-27 17:43:26 -05002342 if (headsUpContentView != null) {
2343 parcel.writeInt(1);
2344 headsUpContentView.writeToParcel(parcel, 0);
2345 } else {
2346 parcel.writeInt(0);
2347 }
2348
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002349 parcel.writeInt(visibility);
2350
2351 if (publicVersion != null) {
2352 parcel.writeInt(1);
2353 publicVersion.writeToParcel(parcel, 0);
2354 } else {
2355 parcel.writeInt(0);
2356 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002357
2358 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002359
2360 if (mChannelId != null) {
2361 parcel.writeInt(1);
2362 parcel.writeString(mChannelId);
2363 } else {
2364 parcel.writeInt(0);
2365 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002366 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002367
2368 if (mShortcutId != null) {
2369 parcel.writeInt(1);
2370 parcel.writeString(mShortcutId);
2371 } else {
2372 parcel.writeInt(0);
2373 }
2374
2375 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002376
2377 if (mSettingsText != null) {
2378 parcel.writeInt(1);
2379 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2380 } else {
2381 parcel.writeInt(0);
2382 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002383
2384 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386
2387 /**
2388 * Parcelable.Creator that instantiates Notification objects
2389 */
2390 public static final Parcelable.Creator<Notification> CREATOR
2391 = new Parcelable.Creator<Notification>()
2392 {
2393 public Notification createFromParcel(Parcel parcel)
2394 {
2395 return new Notification(parcel);
2396 }
2397
2398 public Notification[] newArray(int size)
2399 {
2400 return new Notification[size];
2401 }
2402 };
2403
2404 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002405 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2406 * <p>
2407 * For backwards compatibility {@code extras} holds some references to "real" member data such
2408 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2409 * fine as long as the object stays in one process.
2410 * <p>
2411 * However, once the notification goes into a parcel each reference gets marshalled separately,
2412 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2413 */
2414 private void fixDuplicateExtras() {
2415 if (extras != null) {
2416 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2417 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2418 }
2419 }
2420
2421 /**
2422 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2423 * separate object, replace it with the field's version to avoid holding duplicate copies.
2424 */
2425 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2426 if (original != null && extras.getParcelable(extraName) != null) {
2427 extras.putParcelable(extraName, original);
2428 }
2429 }
2430
2431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2433 * layout.
2434 *
2435 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2436 * in the view.</p>
2437 * @param context The context for your application / activity.
2438 * @param contentTitle The title that goes in the expanded entry.
2439 * @param contentText The text that goes in the expanded entry.
2440 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2441 * If this is an activity, it must include the
2442 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002443 * that you take care of task management as described in the
2444 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2445 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002446 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002447 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002448 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002450 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 public void setLatestEventInfo(Context context,
2452 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002453 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2454 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2455 new Throwable());
2456 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002457
Selim Cinek4ac6f602016-06-13 15:47:03 -07002458 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2459 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2460 }
2461
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002462 // ensure that any information already set directly is preserved
2463 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002464
2465 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002467 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
2469 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002470 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002472 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002473
2474 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476
Julia Reynoldsda303542015-11-23 14:00:20 -05002477 /**
2478 * @hide
2479 */
2480 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002481 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002482 }
2483
2484 /**
2485 * @hide
2486 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002487 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002488 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002489 }
2490
Yi Jin6b514142017-10-30 14:54:12 -07002491 /**
2492 * @hide
2493 */
2494 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2495 long token = proto.start(fieldId);
2496 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2497 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2498 proto.write(NotificationProto.FLAGS, this.flags);
2499 proto.write(NotificationProto.COLOR, this.color);
2500 proto.write(NotificationProto.CATEGORY, this.category);
2501 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2502 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2503 if (this.actions != null) {
2504 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2505 }
2506 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2507 proto.write(NotificationProto.VISIBILITY, this.visibility);
2508 }
2509 if (publicVersion != null) {
2510 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2511 }
2512 proto.end(token);
2513 }
2514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 @Override
2516 public String toString() {
2517 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002518 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002519 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002520 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002521 sb.append(priority);
2522 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002523 if (contentView != null) {
2524 sb.append(contentView.getPackage());
2525 sb.append("/0x");
2526 sb.append(Integer.toHexString(contentView.getLayoutId()));
2527 } else {
2528 sb.append("null");
2529 }
2530 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002531 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2532 sb.append("default");
2533 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 int N = this.vibrate.length-1;
2535 sb.append("[");
2536 for (int i=0; i<N; i++) {
2537 sb.append(this.vibrate[i]);
2538 sb.append(',');
2539 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002540 if (N != -1) {
2541 sb.append(this.vibrate[N]);
2542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 } else {
2545 sb.append("null");
2546 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002547 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002548 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002550 } else if (this.sound != null) {
2551 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 } else {
2553 sb.append("null");
2554 }
Chris Wren365b6d32015-07-16 10:39:26 -04002555 if (this.tickerText != null) {
2556 sb.append(" tick");
2557 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002558 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002560 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002561 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002562 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002563 if (this.category != null) {
2564 sb.append(" category=");
2565 sb.append(this.category);
2566 }
2567 if (this.mGroupKey != null) {
2568 sb.append(" groupKey=");
2569 sb.append(this.mGroupKey);
2570 }
2571 if (this.mSortKey != null) {
2572 sb.append(" sortKey=");
2573 sb.append(this.mSortKey);
2574 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002575 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002576 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002577 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002578 }
2579 sb.append(" vis=");
2580 sb.append(visibilityToString(this.visibility));
2581 if (this.publicVersion != null) {
2582 sb.append(" publicVersion=");
2583 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002584 }
2585 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 return sb.toString();
2587 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002588
Dan Sandler1b718782014-07-18 12:43:45 -04002589 /**
2590 * {@hide}
2591 */
2592 public static String visibilityToString(int vis) {
2593 switch (vis) {
2594 case VISIBILITY_PRIVATE:
2595 return "PRIVATE";
2596 case VISIBILITY_PUBLIC:
2597 return "PUBLIC";
2598 case VISIBILITY_SECRET:
2599 return "SECRET";
2600 default:
2601 return "UNKNOWN(" + String.valueOf(vis) + ")";
2602 }
2603 }
2604
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002606 * {@hide}
2607 */
2608 public static String priorityToString(@Priority int pri) {
2609 switch (pri) {
2610 case PRIORITY_MIN:
2611 return "MIN";
2612 case PRIORITY_LOW:
2613 return "LOW";
2614 case PRIORITY_DEFAULT:
2615 return "DEFAULT";
2616 case PRIORITY_HIGH:
2617 return "HIGH";
2618 case PRIORITY_MAX:
2619 return "MAX";
2620 default:
2621 return "UNKNOWN(" + String.valueOf(pri) + ")";
2622 }
2623 }
2624
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002625 /**
2626 * @hide
2627 */
2628 public boolean hasCompletedProgress() {
2629 // not a progress notification; can't be complete
2630 if (!extras.containsKey(EXTRA_PROGRESS)
2631 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2632 return false;
2633 }
2634 // many apps use max 0 for 'indeterminate'; not complete
2635 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2636 return false;
2637 }
2638 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2639 }
2640
Jeff Sharkey000ce802017-04-29 13:13:27 -06002641 /** @removed */
2642 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002643 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002644 return mChannelId;
2645 }
2646
2647 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002648 * Returns the id of the channel this notification posts to.
2649 */
2650 public String getChannelId() {
2651 return mChannelId;
2652 }
2653
Jeff Sharkey000ce802017-04-29 13:13:27 -06002654 /** @removed */
2655 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002656 public long getTimeout() {
2657 return mTimeout;
2658 }
2659
2660 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002661 * Returns the duration from posting after which this notification should be canceled by the
2662 * system, if it's not canceled already.
2663 */
2664 public long getTimeoutAfter() {
2665 return mTimeout;
2666 }
2667
2668 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002669 * Returns what icon should be shown for this notification if it is being displayed in a
2670 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2671 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2672 */
2673 public int getBadgeIconType() {
2674 return mBadgeIcon;
2675 }
2676
2677 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002678 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002679 *
2680 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2681 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002682 */
2683 public String getShortcutId() {
2684 return mShortcutId;
2685 }
2686
Julia Reynolds3aedded2017-03-31 14:42:09 -04002687
2688 /**
2689 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2690 */
2691 public CharSequence getSettingsText() {
2692 return mSettingsText;
2693 }
2694
Julia Reynolds13d898c2017-02-02 12:22:05 -05002695 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002696 * Returns which type of notifications in a group are responsible for audibly alerting the
2697 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2698 * {@link #GROUP_ALERT_SUMMARY}.
2699 */
2700 public @GroupAlertBehavior int getGroupAlertBehavior() {
2701 return mGroupAlertBehavior;
2702 }
2703
2704 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002705 * The small icon representing this notification in the status bar and content view.
2706 *
2707 * @return the small icon representing this notification.
2708 *
2709 * @see Builder#getSmallIcon()
2710 * @see Builder#setSmallIcon(Icon)
2711 */
2712 public Icon getSmallIcon() {
2713 return mSmallIcon;
2714 }
2715
2716 /**
2717 * Used when notifying to clean up legacy small icons.
2718 * @hide
2719 */
2720 public void setSmallIcon(Icon icon) {
2721 mSmallIcon = icon;
2722 }
2723
2724 /**
2725 * The large icon shown in this notification's content view.
2726 * @see Builder#getLargeIcon()
2727 * @see Builder#setLargeIcon(Icon)
2728 */
2729 public Icon getLargeIcon() {
2730 return mLargeIcon;
2731 }
2732
2733 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002734 * @hide
2735 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002736 public boolean isGroupSummary() {
2737 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2738 }
2739
2740 /**
2741 * @hide
2742 */
2743 public boolean isGroupChild() {
2744 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2745 }
2746
2747 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002748 * @hide
2749 */
2750 public boolean suppressAlertingDueToGrouping() {
2751 if (isGroupSummary()
2752 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2753 return true;
2754 } else if (isGroupChild()
2755 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2756 return true;
2757 }
2758 return false;
2759 }
2760
2761 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002762 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002763 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002764 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002765 * content views using the platform's notification layout template. If your app supports
2766 * versions of Android as old as API level 4, you can instead use
2767 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2768 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2769 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002770 *
Scott Main183bf112012-08-13 19:12:13 -07002771 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002772 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002773 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002774 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002775 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2776 * .setContentText(subject)
2777 * .setSmallIcon(R.drawable.new_mail)
2778 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002779 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002780 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002781 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002782 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002783 /**
2784 * @hide
2785 */
2786 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2787 "android.rebuild.contentViewActionCount";
2788 /**
2789 * @hide
2790 */
2791 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2792 = "android.rebuild.bigViewActionCount";
2793 /**
2794 * @hide
2795 */
2796 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2797 = "android.rebuild.hudViewActionCount";
2798
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002799 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002800
Selim Cinek6743c0b2017-01-18 18:24:01 -08002801 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2802 SystemProperties.getBoolean("notifications.only_title", true);
2803
Selim Cinek389edcd2017-05-11 19:16:44 -07002804 /**
2805 * The lightness difference that has to be added to the primary text color to obtain the
2806 * secondary text color when the background is light.
2807 */
2808 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2809
2810 /**
2811 * The lightness difference that has to be added to the primary text color to obtain the
2812 * secondary text color when the background is dark.
2813 * A bit less then the above value, since it looks better on dark backgrounds.
2814 */
2815 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2816
Joe Onorato46439ce2010-11-19 13:56:21 -08002817 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002818 private Notification mN;
2819 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002820 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2822 private ArrayList<String> mPersonList = new ArrayList<String>();
2823 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002824 private boolean mIsLegacy;
2825 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002826
2827 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002828 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2829 */
2830 private int mCachedContrastColor = COLOR_INVALID;
2831 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002832 /**
2833 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2834 */
2835 private int mCachedAmbientColor = COLOR_INVALID;
2836 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002837
2838 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002839 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2840 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2841 */
2842 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002843 private int mTextColorsAreForBackground = COLOR_INVALID;
2844 private int mPrimaryTextColor = COLOR_INVALID;
2845 private int mSecondaryTextColor = COLOR_INVALID;
2846 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002847 private int mBackgroundColor = COLOR_INVALID;
2848 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002849 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002850 /**
2851 * A temporary location where actions are stored. If != null the view originally has action
2852 * but doesn't have any for this inflation.
2853 */
2854 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002855 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002856
Anthony Chenad4d1582017-04-10 16:07:58 -07002857 private boolean mTintActionButtons;
2858 private boolean mInNightMode;
2859
Adrian Roos70d7aa32017-01-11 15:39:06 -08002860 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002861 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002862 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002863 * @param context
2864 * A {@link Context} that will be used by the Builder to construct the
2865 * RemoteViews. The Context will not be held past the lifetime of this Builder
2866 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002867 * @param channelId
2868 * The constructed Notification will be posted on this
2869 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2870 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002871 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002872 public Builder(Context context, String channelId) {
2873 this(context, (Notification) null);
2874 mN.mChannelId = channelId;
2875 }
2876
2877 /**
2878 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2879 * instead. All posted Notifications must specify a NotificationChannel Id.
2880 */
2881 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002882 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002883 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002884 }
2885
Joe Onoratocb109a02011-01-18 17:57:41 -08002886 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002887 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002888 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002889 public Builder(Context context, Notification toAdopt) {
2890 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002891 Resources res = mContext.getResources();
2892 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2893
2894 if (res.getBoolean(R.bool.config_enableNightMode)) {
2895 Configuration currentConfig = res.getConfiguration();
2896 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2897 == Configuration.UI_MODE_NIGHT_YES;
2898 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002899
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002900 if (toAdopt == null) {
2901 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002902 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2903 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2904 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002905 mN.priority = PRIORITY_DEFAULT;
2906 mN.visibility = VISIBILITY_PRIVATE;
2907 } else {
2908 mN = toAdopt;
2909 if (mN.actions != null) {
2910 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002911 }
2912
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002913 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2914 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2915 }
2916
Selim Cinek4ac6f602016-06-13 15:47:03 -07002917 if (mN.getSmallIcon() == null && mN.icon != 0) {
2918 setSmallIcon(mN.icon);
2919 }
2920
2921 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2922 setLargeIcon(mN.largeIcon);
2923 }
2924
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002925 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2926 if (!TextUtils.isEmpty(templateClass)) {
2927 final Class<? extends Style> styleClass
2928 = getNotificationStyleClass(templateClass);
2929 if (styleClass == null) {
2930 Log.d(TAG, "Unknown style class: " + templateClass);
2931 } else {
2932 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002933 final Constructor<? extends Style> ctor =
2934 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002935 ctor.setAccessible(true);
2936 final Style style = ctor.newInstance();
2937 style.restoreFromExtras(mN.extras);
2938
2939 if (style != null) {
2940 setStyle(style);
2941 }
2942 } catch (Throwable t) {
2943 Log.e(TAG, "Could not create Style", t);
2944 }
2945 }
2946 }
2947
2948 }
2949 }
2950
2951 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002952 if (mColorUtil == null) {
2953 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002954 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002955 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002956 }
2957
2958 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002959 * If this notification is duplicative of a Launcher shortcut, sets the
2960 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2961 * the shortcut.
2962 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002963 * This field will be ignored by Launchers that don't support badging, don't show
2964 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002965 *
2966 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2967 * supersedes
2968 */
2969 public Builder setShortcutId(String shortcutId) {
2970 mN.mShortcutId = shortcutId;
2971 return this;
2972 }
2973
2974 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002975 * Sets which icon to display as a badge for this notification.
2976 *
2977 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2978 * {@link #BADGE_ICON_LARGE}.
2979 *
2980 * Note: This value might be ignored, for launchers that don't support badge icons.
2981 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002982 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002983 mN.mBadgeIcon = icon;
2984 return this;
2985 }
2986
2987 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002988 * Sets the group alert behavior for this notification. Use this method to mute this
2989 * notification if alerts for this notification's group should be handled by a different
2990 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04002991 * {@link #setGroup(String) group}. This must be called on all notifications you want to
2992 * mute. For example, if you want only the summary of your group to make noise, all
2993 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002994 *
2995 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2996 */
2997 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2998 mN.mGroupAlertBehavior = groupAlertBehavior;
2999 return this;
3000 }
3001
Jeff Sharkey000ce802017-04-29 13:13:27 -06003002 /** @removed */
3003 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003004 public Builder setChannel(String channelId) {
3005 mN.mChannelId = channelId;
3006 return this;
3007 }
3008
3009 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003010 * Specifies the channel the notification should be delivered on.
3011 */
3012 public Builder setChannelId(String channelId) {
3013 mN.mChannelId = channelId;
3014 return this;
3015 }
3016
Jeff Sharkey000ce802017-04-29 13:13:27 -06003017 /** @removed */
3018 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003019 public Builder setTimeout(long durationMs) {
3020 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003021 return this;
3022 }
3023
3024 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003025 * Specifies a duration in milliseconds after which this notification should be canceled,
3026 * if it is not already canceled.
3027 */
3028 public Builder setTimeoutAfter(long durationMs) {
3029 mN.mTimeout = durationMs;
3030 return this;
3031 }
3032
3033 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003035 *
3036 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3037 * shown anymore by default and must be opted into by using
3038 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003039 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003040 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003041 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003042 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003043 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003044 return this;
3045 }
3046
Joe Onoratocb109a02011-01-18 17:57:41 -08003047 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003048 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003049 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003050 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3051 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003052 */
3053 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003054 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003055 return this;
3056 }
3057
3058 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003059 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003060 *
3061 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003062 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003063 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003064 * Useful when showing an elapsed time (like an ongoing phone call).
3065 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003066 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003067 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003068 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003069 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003070 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003071 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003072 */
3073 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003074 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003075 return this;
3076 }
3077
3078 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003079 * Sets the Chronometer to count down instead of counting up.
3080 *
3081 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3082 * If it isn't set the chronometer will count up.
3083 *
3084 * @see #setUsesChronometer(boolean)
3085 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003086 public Builder setChronometerCountDown(boolean countDown) {
3087 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003088 return this;
3089 }
3090
3091 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003092 * Set the small icon resource, which will be used to represent the notification in the
3093 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003094 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003095
3096 * The platform template for the expanded view will draw this icon in the left, unless a
3097 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3098 * icon will be moved to the right-hand side.
3099 *
3100
3101 * @param icon
3102 * A resource ID in the application's package of the drawable to use.
3103 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003104 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003105 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003106 return setSmallIcon(icon != 0
3107 ? Icon.createWithResource(mContext, icon)
3108 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003109 }
3110
Joe Onoratocb109a02011-01-18 17:57:41 -08003111 /**
3112 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3113 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3114 * LevelListDrawable}.
3115 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003116 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003117 * @param level The level to use for the icon.
3118 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003119 * @see Notification#icon
3120 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003121 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003122 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003123 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003124 return setSmallIcon(icon);
3125 }
3126
3127 /**
3128 * Set the small icon, which will be used to represent the notification in the
3129 * status bar and content view (unless overriden there by a
3130 * {@link #setLargeIcon(Bitmap) large icon}).
3131 *
3132 * @param icon An Icon object to use.
3133 * @see Notification#icon
3134 */
3135 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003136 mN.setSmallIcon(icon);
3137 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3138 mN.icon = icon.getResId();
3139 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003140 return this;
3141 }
3142
Joe Onoratocb109a02011-01-18 17:57:41 -08003143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003144 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003145 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003146 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003147 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003148 return this;
3149 }
3150
Joe Onoratocb109a02011-01-18 17:57:41 -08003151 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003152 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003153 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003155 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003156 return this;
3157 }
3158
Joe Onoratocb109a02011-01-18 17:57:41 -08003159 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003160 * This provides some additional information that is displayed in the notification. No
3161 * guarantees are given where exactly it is displayed.
3162 *
3163 * <p>This information should only be provided if it provides an essential
3164 * benefit to the understanding of the notification. The more text you provide the
3165 * less readable it becomes. For example, an email client should only provide the account
3166 * name here if more than one email account has been added.</p>
3167 *
3168 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3169 * notification header area.
3170 *
3171 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3172 * this will be shown in the third line of text in the platform notification template.
3173 * You should not be using {@link #setProgress(int, int, boolean)} at the
3174 * same time on those versions; they occupy the same place.
3175 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003176 */
3177 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003178 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003179 return this;
3180 }
3181
3182 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003183 * Provides text that will appear as a link to your application's settings.
3184 *
3185 * <p>This text does not appear within notification {@link Style templates} but may
3186 * appear when the user uses an affordance to learn more about the notification.
3187 * Additionally, this text will not appear unless you provide a valid link target by
3188 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3189 *
3190 * <p>This text is meant to be concise description about what the user can customize
3191 * when they click on this link. The recommended maximum length is 40 characters.
3192 * @param text
3193 * @return
3194 */
3195 public Builder setSettingsText(CharSequence text) {
3196 mN.mSettingsText = safeCharSequence(text);
3197 return this;
3198 }
3199
3200 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003201 * Set the remote input history.
3202 *
3203 * This should be set to the most recent inputs that have been sent
3204 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3205 * longer relevant (e.g. for chat notifications once the other party has responded).
3206 *
3207 * The most recent input must be stored at the 0 index, the second most recent at the
3208 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3209 * and how much of each individual input is shown.
3210 *
3211 * <p>Note: The reply text will only be shown on notifications that have least one action
3212 * with a {@code RemoteInput}.</p>
3213 */
3214 public Builder setRemoteInputHistory(CharSequence[] text) {
3215 if (text == null) {
3216 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3217 } else {
3218 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3219 CharSequence[] safe = new CharSequence[N];
3220 for (int i = 0; i < N; i++) {
3221 safe[i] = safeCharSequence(text[i]);
3222 }
3223 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3224 }
3225 return this;
3226 }
3227
3228 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003229 * Sets the number of items this notification represents. May be displayed as a badge count
3230 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003231 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003232 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003233 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003234 return this;
3235 }
3236
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003238 * A small piece of additional information pertaining to this notification.
3239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003240 * The platform template will draw this on the last line of the notification, at the far
3241 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003242 *
3243 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3244 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3245 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003246 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003247 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003248 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003249 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003250 return this;
3251 }
3252
Joe Onoratocb109a02011-01-18 17:57:41 -08003253 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003254 * Set the progress this notification represents.
3255 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003256 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003257 */
3258 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003259 mN.extras.putInt(EXTRA_PROGRESS, progress);
3260 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3261 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003262 return this;
3263 }
3264
3265 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003266 * Supply a custom RemoteViews to use instead of the platform template.
3267 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003268 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003269 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003270 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003271 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003272 return setCustomContentView(views);
3273 }
3274
3275 /**
3276 * Supply custom RemoteViews to use instead of the platform template.
3277 *
3278 * This will override the layout that would otherwise be constructed by this Builder
3279 * object.
3280 */
3281 public Builder setCustomContentView(RemoteViews contentView) {
3282 mN.contentView = contentView;
3283 return this;
3284 }
3285
3286 /**
3287 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3288 *
3289 * This will override the expanded layout that would otherwise be constructed by this
3290 * Builder object.
3291 */
3292 public Builder setCustomBigContentView(RemoteViews contentView) {
3293 mN.bigContentView = contentView;
3294 return this;
3295 }
3296
3297 /**
3298 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3299 *
3300 * This will override the heads-up layout that would otherwise be constructed by this
3301 * Builder object.
3302 */
3303 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3304 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003305 return this;
3306 }
3307
Joe Onoratocb109a02011-01-18 17:57:41 -08003308 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003309 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3310 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003311 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3312 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3313 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003314 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003315 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003316 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003317 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003318 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003319 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003320 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003321 return this;
3322 }
3323
Joe Onoratocb109a02011-01-18 17:57:41 -08003324 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003325 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3326 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003327 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003328 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003329 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003330 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003331 return this;
3332 }
3333
Joe Onoratocb109a02011-01-18 17:57:41 -08003334 /**
3335 * An intent to launch instead of posting the notification to the status bar.
3336 * Only for use with extremely high-priority notifications demanding the user's
3337 * <strong>immediate</strong> attention, such as an incoming phone call or
3338 * alarm clock that the user has explicitly set to a particular time.
3339 * If this facility is used for something else, please give the user an option
3340 * to turn it off and use a normal notification, as this can be extremely
3341 * disruptive.
3342 *
Chris Wren47c20a12014-06-18 17:27:29 -04003343 * <p>
3344 * The system UI may choose to display a heads-up notification, instead of
3345 * launching this intent, while the user is using the device.
3346 * </p>
3347 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003348 * @param intent The pending intent to launch.
3349 * @param highPriority Passing true will cause this notification to be sent
3350 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003351 *
3352 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003353 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003354 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003355 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003356 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3357 return this;
3358 }
3359
Joe Onoratocb109a02011-01-18 17:57:41 -08003360 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003361 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003362 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003363 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003364 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003365 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003366 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003367 return this;
3368 }
3369
Joe Onoratocb109a02011-01-18 17:57:41 -08003370 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003371 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003372 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003373 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003374 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003375 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 setTicker(tickerText);
3377 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003378 return this;
3379 }
3380
Joe Onoratocb109a02011-01-18 17:57:41 -08003381 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003382 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003383 *
3384 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003385 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3386 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003387 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003388 public Builder setLargeIcon(Bitmap b) {
3389 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3390 }
3391
3392 /**
3393 * Add a large icon to the notification content view.
3394 *
3395 * In the platform template, this image will be shown on the left of the notification view
3396 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3397 * badge atop the large icon).
3398 */
3399 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003400 mN.mLargeIcon = icon;
3401 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003402 return this;
3403 }
3404
Joe Onoratocb109a02011-01-18 17:57:41 -08003405 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003406 * Set the sound to play.
3407 *
John Spurlockc0650f022014-07-19 13:22:39 -04003408 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3409 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003410 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003411 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003413 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003414 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 mN.sound = sound;
3416 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003417 return this;
3418 }
3419
Joe Onoratocb109a02011-01-18 17:57:41 -08003420 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003421 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003422 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003423 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3424 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003425 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003427 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003428 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003429 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003430 mN.sound = sound;
3431 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003432 return this;
3433 }
3434
Joe Onoratocb109a02011-01-18 17:57:41 -08003435 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003436 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3437 * use during playback.
3438 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003439 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003440 * @see Notification#sound
3441 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003442 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003443 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 mN.sound = sound;
3445 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003446 return this;
3447 }
3448
3449 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003450 * Set the vibration pattern to use.
3451 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003452 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3453 * <code>pattern</code> parameter.
3454 *
Chris Wren47c20a12014-06-18 17:27:29 -04003455 * <p>
3456 * A notification that vibrates is more likely to be presented as a heads-up notification.
3457 * </p>
3458 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003459 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003460 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003462 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003463 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003464 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003465 return this;
3466 }
3467
Joe Onoratocb109a02011-01-18 17:57:41 -08003468 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 * Set the desired color for the indicator LED on the device, as well as the
3470 * blink duty cycle (specified in milliseconds).
3471 *
3472
3473 * Not all devices will honor all (or even any) of these values.
3474 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003475 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003476 * @see Notification#ledARGB
3477 * @see Notification#ledOnMS
3478 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003479 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003480 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003481 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003482 mN.ledARGB = argb;
3483 mN.ledOnMS = onMs;
3484 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003485 if (onMs != 0 || offMs != 0) {
3486 mN.flags |= FLAG_SHOW_LIGHTS;
3487 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003488 return this;
3489 }
3490
Joe Onoratocb109a02011-01-18 17:57:41 -08003491 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003492 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003493 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003494
3495 * Ongoing notifications cannot be dismissed by the user, so your application or service
3496 * must take care of canceling them.
3497 *
3498
3499 * They are typically used to indicate a background task that the user is actively engaged
3500 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3501 * (e.g., a file download, sync operation, active network connection).
3502 *
3503
3504 * @see Notification#FLAG_ONGOING_EVENT
3505 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003507 public Builder setOngoing(boolean ongoing) {
3508 setFlag(FLAG_ONGOING_EVENT, ongoing);
3509 return this;
3510 }
3511
Joe Onoratocb109a02011-01-18 17:57:41 -08003512 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003513 * Set whether this notification should be colorized. When set, the color set with
3514 * {@link #setColor(int)} will be used as the background color of this notification.
3515 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003516 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3517 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003518 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003519 * For most styles, the coloring will only be applied if the notification is for a
3520 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003521 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003522 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003523 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003524 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003525 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003526 */
3527 public Builder setColorized(boolean colorize) {
3528 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3529 return this;
3530 }
3531
3532 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003533 * Set this flag if you would only like the sound, vibrate
3534 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003535 *
3536 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003537 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003538 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3539 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3540 return this;
3541 }
3542
Joe Onoratocb109a02011-01-18 17:57:41 -08003543 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003544 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003545 *
3546 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003547 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003548 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003549 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003550 return this;
3551 }
3552
Joe Onoratocb109a02011-01-18 17:57:41 -08003553 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003554 * Set whether or not this notification should not bridge to other devices.
3555 *
3556 * <p>Some notifications can be bridged to other devices for remote display.
3557 * This hint can be set to recommend this notification not be bridged.
3558 */
3559 public Builder setLocalOnly(boolean localOnly) {
3560 setFlag(FLAG_LOCAL_ONLY, localOnly);
3561 return this;
3562 }
3563
3564 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003565 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 * <p>
3567 * The value should be one or more of the following fields combined with
3568 * bitwise-or:
3569 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3570 * <p>
3571 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003572 *
3573 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003574 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003575 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003576 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003577 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003578 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003579 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003580 return this;
3581 }
3582
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003583 /**
3584 * Set the priority of this notification.
3585 *
3586 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003587 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003588 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003589 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003590 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003591 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003592 return this;
3593 }
Joe Malin8d40d042012-11-05 11:36:40 -08003594
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003595 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003596 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003597 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003598 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003599 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003600 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 return this;
3603 }
3604
3605 /**
Chris Wrendde75302014-03-26 17:24:15 -04003606 * Add a person that is relevant to this notification.
3607 *
Chris Wrene6c48932014-09-29 17:19:27 -04003608 * <P>
3609 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003610 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3611 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3612 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003613 * </P>
3614 *
3615 * <P>
3616 * The person should be specified by the {@code String} representation of a
3617 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3618 * </P>
3619 *
3620 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3621 * URIs. The path part of these URIs must exist in the contacts database, in the
3622 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3623 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3624 * </P>
3625 *
3626 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003627 * @see Notification#EXTRA_PEOPLE
3628 */
Chris Wrene6c48932014-09-29 17:19:27 -04003629 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003630 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003631 return this;
3632 }
3633
3634 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003635 * Set this notification to be part of a group of notifications sharing the same key.
3636 * Grouped notifications may display in a cluster or stack on devices which
3637 * support such rendering.
3638 *
3639 * <p>To make this notification the summary for its group, also call
3640 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3641 * {@link #setSortKey}.
3642 * @param groupKey The group key of the group.
3643 * @return this object for method chaining
3644 */
3645 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003646 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003647 return this;
3648 }
3649
3650 /**
3651 * Set this notification to be the group summary for a group of notifications.
3652 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003653 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3654 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003655 * @param isGroupSummary Whether this notification should be a group summary.
3656 * @return this object for method chaining
3657 */
3658 public Builder setGroupSummary(boolean isGroupSummary) {
3659 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3660 return this;
3661 }
3662
3663 /**
3664 * Set a sort key that orders this notification among other notifications from the
3665 * same package. This can be useful if an external sort was already applied and an app
3666 * would like to preserve this. Notifications will be sorted lexicographically using this
3667 * value, although providing different priorities in addition to providing sort key may
3668 * cause this value to be ignored.
3669 *
3670 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003671 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003672 *
3673 * @see String#compareTo(String)
3674 */
3675 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003676 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003677 return this;
3678 }
3679
3680 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003681 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003682 *
Griff Hazen720042b2014-02-24 15:46:56 -08003683 * <p>Values within the Bundle will replace existing extras values in this Builder.
3684 *
3685 * @see Notification#extras
3686 */
Griff Hazen959591e2014-05-15 22:26:18 -07003687 public Builder addExtras(Bundle extras) {
3688 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003689 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003690 }
3691 return this;
3692 }
3693
3694 /**
3695 * Set metadata for this notification.
3696 *
3697 * <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 -04003698 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003699 * called.
3700 *
Griff Hazen720042b2014-02-24 15:46:56 -08003701 * <p>Replaces any existing extras values with those from the provided Bundle.
3702 * Use {@link #addExtras} to merge in metadata instead.
3703 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003704 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003705 */
Griff Hazen959591e2014-05-15 22:26:18 -07003706 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003707 if (extras != null) {
3708 mUserExtras = extras;
3709 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003710 return this;
3711 }
3712
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003713 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003714 * Get the current metadata Bundle used by this notification Builder.
3715 *
3716 * <p>The returned Bundle is shared with this Builder.
3717 *
3718 * <p>The current contents of this Bundle are copied into the Notification each time
3719 * {@link #build()} is called.
3720 *
3721 * @see Notification#extras
3722 */
3723 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 return mUserExtras;
3725 }
3726
3727 private Bundle getAllExtras() {
3728 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3729 saveExtras.putAll(mN.extras);
3730 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003731 }
3732
3733 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003734 * Add an action to this notification. Actions are typically displayed by
3735 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003736 * <p>
3737 * Every action must have an icon (32dp square and matching the
3738 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3739 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3740 * <p>
3741 * A notification in its expanded form can display up to 3 actions, from left to right in
3742 * the order they were added. Actions will not be displayed when the notification is
3743 * collapsed, however, so be sure that any essential functions may be accessed by the user
3744 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003745 *
3746 * @param icon Resource ID of a drawable that represents the action.
3747 * @param title Text describing the action.
3748 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003749 *
3750 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003751 */
Dan Sandler86647982015-05-13 23:41:13 -04003752 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003753 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003754 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003755 return this;
3756 }
3757
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003758 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003759 * Add an action to this notification. Actions are typically displayed by
3760 * the system as a button adjacent to the notification content.
3761 * <p>
3762 * Every action must have an icon (32dp square and matching the
3763 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3764 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3765 * <p>
3766 * A notification in its expanded form can display up to 3 actions, from left to right in
3767 * the order they were added. Actions will not be displayed when the notification is
3768 * collapsed, however, so be sure that any essential functions may be accessed by the user
3769 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3770 *
3771 * @param action The action to add.
3772 */
3773 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003774 if (action != null) {
3775 mActions.add(action);
3776 }
Griff Hazen959591e2014-05-15 22:26:18 -07003777 return this;
3778 }
3779
3780 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003781 * Alter the complete list of actions attached to this notification.
3782 * @see #addAction(Action).
3783 *
3784 * @param actions
3785 * @return
3786 */
3787 public Builder setActions(Action... actions) {
3788 mActions.clear();
3789 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003790 if (actions[i] != null) {
3791 mActions.add(actions[i]);
3792 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003793 }
3794 return this;
3795 }
3796
3797 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003798 * Add a rich notification style to be applied at build time.
3799 *
3800 * @param style Object responsible for modifying the notification style.
3801 */
3802 public Builder setStyle(Style style) {
3803 if (mStyle != style) {
3804 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003805 if (mStyle != null) {
3806 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003807 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3808 } else {
3809 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003810 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003811 }
3812 return this;
3813 }
3814
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003815 /**
3816 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003817 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003818 * @return The same Builder.
3819 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003820 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003821 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003822 return this;
3823 }
3824
3825 /**
3826 * Supply a replacement Notification whose contents should be shown in insecure contexts
3827 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3828 * @param n A replacement notification, presumably with some or all info redacted.
3829 * @return The same Builder.
3830 */
3831 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003832 if (n != null) {
3833 mN.publicVersion = new Notification();
3834 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3835 } else {
3836 mN.publicVersion = null;
3837 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003838 return this;
3839 }
3840
Griff Hazenb720abe2014-05-20 13:15:30 -07003841 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003842 * Apply an extender to this notification builder. Extenders may be used to add
3843 * metadata or change options on this builder.
3844 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003845 public Builder extend(Extender extender) {
3846 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003847 return this;
3848 }
3849
Dan Sandler4e787062015-06-17 15:09:48 -04003850 /**
3851 * @hide
3852 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003853 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003854 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003855 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003856 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003857 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003858 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003859 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003860 }
3861
Dan Sandler26e81cf2014-05-06 10:01:27 -04003862 /**
3863 * Sets {@link Notification#color}.
3864 *
3865 * @param argb The accent color to use
3866 *
3867 * @return The same Builder.
3868 */
Tor Norbye80756e32015-03-02 09:39:27 -08003869 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003871 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003872 return this;
3873 }
3874
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003875 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003876 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3877 // This user can never be a badged profile,
3878 // and also includes USER_ALL system notifications.
3879 return null;
3880 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003881 // Note: This assumes that the current user can read the profile badge of the
3882 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003883 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003884 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003885 }
3886
3887 private Bitmap getProfileBadge() {
3888 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003889 if (badge == null) {
3890 return null;
3891 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003892 final int size = mContext.getResources().getDimensionPixelSize(
3893 R.dimen.notification_badge_size);
3894 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003895 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003896 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003897 badge.draw(canvas);
3898 return bitmap;
3899 }
3900
Selim Cinekc848c3a2016-01-13 15:27:30 -08003901 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003902 Bitmap profileBadge = getProfileBadge();
3903
Kenny Guy98193ea2014-07-24 19:54:37 +01003904 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003905 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3906 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003907 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003908 contentView.setDrawableTint(R.id.profile_badge, false,
3909 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003910 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003911 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003912 }
3913
Christoph Studerfe718432014-09-01 18:21:18 +02003914 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003915 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003916 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003917 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003918 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003919 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003920 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003921 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003922 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003923 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003924 }
3925
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003926 /**
3927 * Resets the notification header to its original state
3928 */
3929 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003930 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3931 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003932 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003933 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003934 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003935 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003936 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003937 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003938 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003939 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003940 contentView.setImageViewIcon(R.id.profile_badge, null);
3941 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003942 }
3943
3944 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003945 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3946 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003947 }
3948
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003949 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003950 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003951 }
3952
3953 /**
3954 * @param hasProgress whether the progress bar should be shown and set
3955 */
3956 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003957 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3958 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003959 }
3960
Adrian Roos70d7aa32017-01-11 15:39:06 -08003961 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003962 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003963
Christoph Studerfe718432014-09-01 18:21:18 +02003964 resetStandardTemplate(contentView);
3965
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003966 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003967 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003968 bindNotificationHeader(contentView, p.ambient);
Selim Cinek88188f22017-09-19 16:46:56 -07003969 bindLargeIcon(contentView, p.hideLargeIcon, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003970 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3971 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003972 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07003973 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08003974 if (!p.ambient) {
3975 setTextViewColorPrimary(contentView, R.id.title);
3976 }
Selim Cinek954cc232016-05-20 13:29:23 -07003977 contentView.setViewLayoutWidth(R.id.title, showProgress
3978 ? ViewGroup.LayoutParams.WRAP_CONTENT
3979 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003980 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003981 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003982 int textId = showProgress ? com.android.internal.R.id.text_line_1
3983 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07003984 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08003985 if (!p.ambient) {
3986 setTextViewColorSecondary(contentView, textId);
3987 }
Selim Cinek41598732016-01-11 16:58:37 -08003988 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003989 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003990
Selim Cinek279fa862016-06-14 10:57:25 -07003991 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003992
Selim Cinek29603462015-11-17 19:04:39 -08003993 return contentView;
3994 }
3995
Selim Cinek48f66b72017-08-18 16:17:51 -07003996 private CharSequence processTextSpans(CharSequence text) {
3997 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07003998 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07003999 }
4000 return text;
4001 }
4002
Selim Cinek7b9605b2017-01-19 17:36:00 -08004003 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4004 ensureColors();
4005 contentView.setTextColor(id, mPrimaryTextColor);
4006 }
4007
Selim Cinek48f66b72017-08-18 16:17:51 -07004008 private boolean hasForegroundColor() {
4009 return mForegroundColor != COLOR_INVALID;
4010 }
4011
Selim Cinek389edcd2017-05-11 19:16:44 -07004012 /**
4013 * @return the primary text color
4014 * @hide
4015 */
4016 @VisibleForTesting
4017 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004018 ensureColors();
4019 return mPrimaryTextColor;
4020 }
4021
Selim Cinek389edcd2017-05-11 19:16:44 -07004022 /**
4023 * @return the secondary text color
4024 * @hide
4025 */
4026 @VisibleForTesting
4027 public int getSecondaryTextColor() {
4028 ensureColors();
4029 return mSecondaryTextColor;
4030 }
4031
Selim Cinek7b9605b2017-01-19 17:36:00 -08004032 private int getActionBarColor() {
4033 ensureColors();
4034 return mActionBarColor;
4035 }
4036
Selim Cinek622c64a2017-04-17 17:10:05 -07004037 private int getActionBarColorDeEmphasized() {
4038 int backgroundColor = getBackgroundColor();
4039 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4040 }
4041
Selim Cinek7b9605b2017-01-19 17:36:00 -08004042 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4043 ensureColors();
4044 contentView.setTextColor(id, mSecondaryTextColor);
4045 }
4046
4047 private void ensureColors() {
4048 int backgroundColor = getBackgroundColor();
4049 if (mPrimaryTextColor == COLOR_INVALID
4050 || mSecondaryTextColor == COLOR_INVALID
4051 || mActionBarColor == COLOR_INVALID
4052 || mTextColorsAreForBackground != backgroundColor) {
4053 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004054 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004055 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4056 backgroundColor);
4057 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4058 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004059 if (backgroundColor != COLOR_DEFAULT
4060 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4061 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4062 mPrimaryTextColor, backgroundColor, 4.5);
4063 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4064 mSecondaryTextColor, backgroundColor, 4.5);
4065 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004066 } else {
4067 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4068 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4069 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4070 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004071 // We only respect the given colors if worst case Black or White still has
4072 // contrast
4073 boolean backgroundLight = backLum > textLum
4074 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4075 || backLum <= textLum
4076 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004077 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004078 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004079 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4080 mForegroundColor,
4081 backgroundColor,
4082 true /* findFG */,
4083 4.5f);
4084 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004085 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004086 } else {
4087 mSecondaryTextColor =
4088 NotificationColorUtil.findContrastColorAgainstDark(
4089 mForegroundColor,
4090 backgroundColor,
4091 true /* findFG */,
4092 4.5f);
4093 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004094 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004095 }
4096 } else {
4097 mPrimaryTextColor = mForegroundColor;
4098 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004099 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4100 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004101 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4102 backgroundColor) < 4.5f) {
4103 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004104 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004105 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4106 mSecondaryTextColor,
4107 backgroundColor,
4108 true /* findFG */,
4109 4.5f);
4110 } else {
4111 mSecondaryTextColor
4112 = NotificationColorUtil.findContrastColorAgainstDark(
4113 mSecondaryTextColor,
4114 backgroundColor,
4115 true /* findFG */,
4116 4.5f);
4117 }
4118 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004119 mSecondaryTextColor, backgroundLight
4120 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4121 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004122 }
4123 }
4124 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004125 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4126 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004127 }
4128 }
4129
4130 private void updateBackgroundColor(RemoteViews contentView) {
4131 if (isColorized()) {
4132 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4133 getBackgroundColor());
4134 } else {
4135 // Clear it!
4136 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4137 0);
4138 }
4139 }
4140
Selim Cinek860b6da2015-12-16 19:02:19 -08004141 /**
4142 * @param remoteView the remote view to update the minheight in
4143 * @param hasMinHeight does it have a mimHeight
4144 * @hide
4145 */
4146 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4147 int minHeight = 0;
4148 if (hasMinHeight) {
4149 // we need to set the minHeight of the notification
4150 minHeight = mContext.getResources().getDimensionPixelSize(
4151 com.android.internal.R.dimen.notification_min_content_height);
4152 }
4153 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4154 }
4155
Selim Cinek29603462015-11-17 19:04:39 -08004156 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004157 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4158 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4159 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4160 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004161 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004162 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004163 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004164 contentView.setProgressBackgroundTintList(
4165 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4166 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004167 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004168 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004169 contentView.setProgressTintList(R.id.progress, colorStateList);
4170 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004171 }
Selim Cinek29603462015-11-17 19:04:39 -08004172 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004173 } else {
4174 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004175 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004176 }
Joe Onorato561d3852010-11-20 18:09:34 -08004177 }
4178
Selim Cinek88188f22017-09-19 16:46:56 -07004179 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4180 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004181 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4182 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4183 }
Selim Cinek88188f22017-09-19 16:46:56 -07004184 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4185 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004186 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4187 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4188 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004189 int endMargin = R.dimen.notification_content_picture_margin;
4190 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4191 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4192 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004193 }
4194 // Bind the reply action
4195 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004196
Selim Cinek88188f22017-09-19 16:46:56 -07004197 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4198 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4199 if (actionVisible) {
4200 // We're only showing the icon as big if we're hiding the large icon
4201 int contrastColor = resolveContrastColor();
4202 int iconColor;
4203 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004204 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004205 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004206 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004207 contentView.setOnClickPendingIntent(R.id.right_icon,
4208 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004209 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004210 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4211 ? Color.BLACK : Color.WHITE;
4212 } else {
4213 contentView.setImageViewResource(R.id.right_icon,
4214 R.drawable.ic_reply_notification_large);
4215 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4216 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004217 }
Selim Cinek88188f22017-09-19 16:46:56 -07004218 contentView.setDrawableTint(replyId,
4219 false /* targetBackground */,
4220 iconColor,
4221 PorterDuff.Mode.SRC_ATOP);
4222 contentView.setOnClickPendingIntent(replyId,
4223 action.actionIntent);
4224 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4225 } else {
4226 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004227 }
Selim Cinek88188f22017-09-19 16:46:56 -07004228 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4229 ? View.VISIBLE
4230 : View.GONE);
4231 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004232 ? View.VISIBLE
4233 : View.GONE);
4234 }
4235
4236 private Action findReplyAction() {
4237 ArrayList<Action> actions = mActions;
4238 if (mOriginalActions != null) {
4239 actions = mOriginalActions;
4240 }
4241 int numActions = actions.size();
4242 for (int i = 0; i < numActions; i++) {
4243 Action action = actions.get(i);
4244 if (hasValidRemoteInput(action)) {
4245 return action;
4246 }
4247 }
4248 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004249 }
4250
Adrian Roos487374f2017-01-11 15:48:14 -08004251 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4252 bindSmallIcon(contentView, ambient);
4253 bindHeaderAppName(contentView, ambient);
4254 if (!ambient) {
4255 // Ambient view does not have these
4256 bindHeaderText(contentView);
4257 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004258 bindProfileBadge(contentView);
4259 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004260 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004261 }
4262
4263 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004264 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004265 contentView.setDrawableTint(R.id.expand_button, false, color,
4266 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004267 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004268 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004269 }
4270
Selim Cinek99104832017-01-25 14:47:33 -08004271 /**
4272 * @return the color that is used as the first primary highlight color. This is applied
4273 * in several places like the action buttons or the app name in the header.
4274 */
4275 private int getPrimaryHighlightColor() {
4276 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4277 }
4278
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004279 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4280 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004281 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004282 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004283 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4284 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4285 contentView.setLong(R.id.chronometer, "setBase",
4286 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4287 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004288 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004289 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004290 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004291 } else {
4292 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4293 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004294 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004295 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004296 } else {
4297 // We still want a time to be set but gone, such that we can show and hide it
4298 // on demand in case it's a child notification without anything in the header
4299 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004300 }
4301 }
4302
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004303 private void bindHeaderText(RemoteViews contentView) {
4304 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4305 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004306 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004307 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004308 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004309 if (headerText == null
4310 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4311 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4312 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4313 }
4314 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004315 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004316 contentView.setTextViewText(R.id.header_text, processTextSpans(
4317 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004318 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004319 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4320 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004321 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004322 }
4323 }
4324
Adrian Rooseba05822016-04-22 17:09:27 -07004325 /**
4326 * @hide
4327 */
4328 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004329 CharSequence name = null;
4330 final PackageManager pm = mContext.getPackageManager();
4331 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4332 // only system packages which lump together a bunch of unrelated stuff
4333 // may substitute a different name to make the purpose of the
4334 // notification more clear. the correct package label should always
4335 // be accessible via SystemUI.
4336 final String pkg = mContext.getPackageName();
4337 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4338 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4339 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4340 name = subName;
4341 } else {
4342 Log.w(TAG, "warning: pkg "
4343 + pkg + " attempting to substitute app name '" + subName
4344 + "' without holding perm "
4345 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4346 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004347 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004348 if (TextUtils.isEmpty(name)) {
4349 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4350 }
4351 if (TextUtils.isEmpty(name)) {
4352 // still nothing?
4353 return null;
4354 }
4355
4356 return String.valueOf(name);
4357 }
Adrian Roos487374f2017-01-11 15:48:14 -08004358 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004359 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004360 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004361 setTextViewColorPrimary(contentView, R.id.app_name_text);
4362 } else {
4363 contentView.setTextColor(R.id.app_name_text,
4364 ambient ? resolveAmbientColor() : resolveContrastColor());
4365 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004366 }
4367
Adrian Roos487374f2017-01-11 15:48:14 -08004368 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004369 if (mN.mSmallIcon == null && mN.icon != 0) {
4370 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4371 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004372 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004373 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004374 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004375 }
4376
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004377 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004378 * @return true if the built notification will show the time or the chronometer; false
4379 * otherwise
4380 */
4381 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004382 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004383 }
4384
Christoph Studerfe718432014-09-01 18:21:18 +02004385 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004386 // actions_container is only reset when there are no actions to avoid focus issues with
4387 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004388 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004389 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004390
4391 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4392 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4393
4394 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4395 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4396 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4397 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004398
4399 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004400 }
4401
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004402 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004403 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004404 }
4405
Adrian Roos70d7aa32017-01-11 15:39:06 -08004406 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4407 StandardTemplateParams p) {
4408 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004409
Christoph Studerfe718432014-09-01 18:21:18 +02004410 resetStandardTemplateWithActions(big);
4411
Adrian Roose458aa82015-12-08 16:17:19 -08004412 boolean validRemoteInput = false;
4413
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004414 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004415 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004416 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004417 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004418 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004419 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004420 if (p.ambient) {
4421 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004422 } else if (isColorized()) {
4423 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4424 } else {
4425 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4426 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004427 }
Adrian Roosf852a422016-06-03 13:33:43 -07004428 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4429 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004430 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004431 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004432 Action action = mActions.get(i);
4433 validRemoteInput |= hasValidRemoteInput(action);
4434
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004435 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004436 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004437 big.addView(R.id.actions, button);
4438 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004439 } else {
4440 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004441 }
Adrian Roose458aa82015-12-08 16:17:19 -08004442
4443 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004444 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004445 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4446 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004447 big.setTextViewText(R.id.notification_material_reply_text_1,
4448 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004449 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004450
4451 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4452 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004453 big.setTextViewText(R.id.notification_material_reply_text_2,
4454 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004455 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004456
4457 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4458 big.setViewVisibility(
4459 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004460 big.setTextViewText(R.id.notification_material_reply_text_3,
4461 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004462 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004463 }
4464 }
4465 }
4466
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004467 return big;
4468 }
4469
Adrian Roose458aa82015-12-08 16:17:19 -08004470 private boolean hasValidRemoteInput(Action action) {
4471 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4472 // Weird actions
4473 return false;
4474 }
4475
4476 RemoteInput[] remoteInputs = action.getRemoteInputs();
4477 if (remoteInputs == null) {
4478 return false;
4479 }
4480
4481 for (RemoteInput r : remoteInputs) {
4482 CharSequence[] choices = r.getChoices();
4483 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4484 return true;
4485 }
4486 }
4487 return false;
4488 }
4489
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004490 /**
4491 * Construct a RemoteViews for the final 1U notification layout. In order:
4492 * 1. Custom contentView from the caller
4493 * 2. Style's proposed content view
4494 * 3. Standard template view
4495 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004496 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004497 return createContentView(false /* increasedheight */ );
4498 }
4499
4500 /**
4501 * Construct a RemoteViews for the smaller content view.
4502 *
4503 * @param increasedHeight true if this layout be created with an increased height. Some
4504 * styles may support showing more then just that basic 1U size
4505 * and the system may decide to render important notifications
4506 * slightly bigger even when collapsed.
4507 *
4508 * @hide
4509 */
4510 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004511 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004512 return mN.contentView;
4513 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004514 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004515 if (styleView != null) {
4516 return styleView;
4517 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004518 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004519 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004520 }
4521
Selim Cineka7679b62017-05-10 16:33:25 -07004522 private boolean useExistingRemoteView() {
4523 return mStyle == null || (!mStyle.displayCustomViewInline()
4524 && !mRebuildStyledRemoteViews);
4525 }
4526
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004527 /**
4528 * Construct a RemoteViews for the final big notification layout.
4529 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004530 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004531 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004532 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004533 return mN.bigContentView;
4534 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004535 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004536 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004537 } else if (mActions.size() != 0) {
4538 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004539 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004540 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004541 return result;
4542 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004543
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004544 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004545 * Construct a RemoteViews for the final notification header only. This will not be
4546 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004547 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004548 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004549 * @hide
4550 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004551 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004552 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4553 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004554 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004555 ambient ? R.layout.notification_template_ambient_header
4556 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004557 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004558 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004559 if (colorized != null) {
4560 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4561 } else {
4562 mN.extras.remove(EXTRA_COLORIZED);
4563 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004564 return header;
4565 }
4566
Adrian Roos487374f2017-01-11 15:48:14 -08004567 /**
4568 * Construct a RemoteViews for the ambient version of the notification.
4569 *
4570 * @hide
4571 */
4572 public RemoteViews makeAmbientNotification() {
4573 RemoteViews ambient = applyStandardTemplateWithActions(
4574 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004575 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004576 return ambient;
4577 }
4578
Selim Cinek29603462015-11-17 19:04:39 -08004579 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004580 if (result != null) {
4581 result.setViewVisibility(R.id.text_line_1, View.GONE);
4582 }
Selim Cinek29603462015-11-17 19:04:39 -08004583 }
4584
Selim Cinek6743c0b2017-01-18 18:24:01 -08004585 /**
4586 * Adapt the Notification header if this view is used as an expanded view.
4587 *
4588 * @hide
4589 */
4590 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004591 if (result != null) {
4592 result.setBoolean(R.id.notification_header, "setExpanded", true);
4593 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004594 }
4595
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004596 /**
4597 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004598 *
4599 * @param increasedHeight true if this layout be created with an increased height. Some
4600 * styles may support showing more then just that basic 1U size
4601 * and the system may decide to render important notifications
4602 * slightly bigger even when collapsed.
4603 *
4604 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004605 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004606 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004607 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004608 return mN.headsUpContentView;
4609 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004610 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4611 if (styleView != null) {
4612 return styleView;
4613 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004614 } else if (mActions.size() == 0) {
4615 return null;
4616 }
4617
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004618 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004619 }
4620
Selim Cinek624c02db2015-12-14 21:00:02 -08004621 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004622 * Construct a RemoteViews for the final heads-up notification layout.
4623 */
4624 public RemoteViews createHeadsUpContentView() {
4625 return createHeadsUpContentView(false /* useIncreasedHeight */);
4626 }
4627
4628 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004629 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4630 *
4631 * @hide
4632 */
4633 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004634 return makePublicView(false /* ambient */);
4635 }
4636
4637 /**
4638 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4639 *
4640 * @hide
4641 */
4642 public RemoteViews makePublicAmbientNotification() {
4643 return makePublicView(true /* ambient */);
4644 }
4645
4646 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004647 if (mN.publicVersion != null) {
4648 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004649 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004650 }
4651 Bundle savedBundle = mN.extras;
4652 Style style = mStyle;
4653 mStyle = null;
4654 Icon largeIcon = mN.mLargeIcon;
4655 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004656 Bitmap largeIconLegacy = mN.largeIcon;
4657 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004658 ArrayList<Action> actions = mActions;
4659 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004660 Bundle publicExtras = new Bundle();
4661 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4662 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4663 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4664 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004665 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4666 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004667 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004668 RemoteViews view;
4669 if (ambient) {
4670 publicExtras.putCharSequence(EXTRA_TITLE,
4671 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4672 view = makeAmbientNotification();
4673 } else{
4674 view = makeNotificationHeader(false /* ambient */);
4675 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4676 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004677 mN.extras = savedBundle;
4678 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004679 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004680 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004681 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004682 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004683 }
4684
Selim Cinek6743c0b2017-01-18 18:24:01 -08004685 /**
4686 * Construct a content view for the display when low - priority
4687 *
4688 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4689 * a new subtext is created consisting of the content of the
4690 * notification.
4691 * @hide
4692 */
4693 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4694 int color = mN.color;
4695 mN.color = COLOR_DEFAULT;
4696 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4697 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4698 CharSequence newSummary = createSummaryText();
4699 if (!TextUtils.isEmpty(newSummary)) {
4700 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4701 }
4702 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004703
Adrian Roos6f6e1592017-05-02 16:22:53 -07004704 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004705 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004706 if (summary != null) {
4707 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4708 } else {
4709 mN.extras.remove(EXTRA_SUB_TEXT);
4710 }
4711 mN.color = color;
4712 return header;
4713 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004714
Selim Cinek6743c0b2017-01-18 18:24:01 -08004715 private CharSequence createSummaryText() {
4716 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4717 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4718 return titleText;
4719 }
4720 SpannableStringBuilder summary = new SpannableStringBuilder();
4721 if (titleText == null) {
4722 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4723 }
4724 BidiFormatter bidi = BidiFormatter.getInstance();
4725 if (titleText != null) {
4726 summary.append(bidi.unicodeWrap(titleText));
4727 }
4728 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4729 if (titleText != null && contentText != null) {
4730 summary.append(bidi.unicodeWrap(mContext.getText(
4731 R.string.notification_header_divider_symbol_with_spaces)));
4732 }
4733 if (contentText != null) {
4734 summary.append(bidi.unicodeWrap(contentText));
4735 }
4736 return summary;
4737 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004738
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004739 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004740 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004741 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004742 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004743 emphazisedMode ? getEmphasizedActionLayoutResource()
4744 : tombstone ? getActionTombstoneLayoutResource()
4745 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004746 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004747 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004748 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004749 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004750 if (action.mRemoteInputs != null) {
4751 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4752 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004753 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004754 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004755 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004756 int bgColor;
4757 if (isColorized()) {
4758 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4759 } else {
4760 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4761 : R.color.notification_action_list_dark);
4762 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004763 button.setDrawableTint(R.id.button_holder, true,
4764 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004765 CharSequence title = action.title;
4766 ColorStateList[] outResultColor = null;
4767 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004768 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004769 } else {
4770 outResultColor = new ColorStateList[1];
4771 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4772 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004773 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004774 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004775 if (outResultColor != null && outResultColor[0] != null) {
4776 // We need to set the text color as well since changing a text to uppercase
4777 // clears its spans.
4778 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004779 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004780 button.setTextColor(R.id.action0,resolveContrastColor());
4781 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004782 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004783 button.setTextViewText(R.id.action0, processTextSpans(
4784 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004785 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004786 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004787 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004788 button.setTextColor(R.id.action0,
4789 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004790 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004791 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004792 return button;
4793 }
4794
Joe Onoratocb109a02011-01-18 17:57:41 -08004795 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004796 * Ensures contrast on color spans against a background color. also returns the color of the
4797 * text if a span was found that spans over the whole text.
4798 *
4799 * @param charSequence the charSequence on which the spans are
4800 * @param background the background color to ensure the contrast against
4801 * @param outResultColor an array in which a color will be returned as the first element if
4802 * there exists a full length color span.
4803 * @return the contrasted charSequence
4804 */
4805 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4806 ColorStateList[] outResultColor) {
4807 if (charSequence instanceof Spanned) {
4808 Spanned ss = (Spanned) charSequence;
4809 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4810 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4811 for (Object span : spans) {
4812 Object resultSpan = span;
4813 int spanStart = ss.getSpanStart(span);
4814 int spanEnd = ss.getSpanEnd(span);
4815 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4816 if (resultSpan instanceof CharacterStyle) {
4817 resultSpan = ((CharacterStyle) span).getUnderlying();
4818 }
4819 if (resultSpan instanceof TextAppearanceSpan) {
4820 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4821 ColorStateList textColor = originalSpan.getTextColor();
4822 if (textColor != null) {
4823 int[] colors = textColor.getColors();
4824 int[] newColors = new int[colors.length];
4825 for (int i = 0; i < newColors.length; i++) {
4826 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004827 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004828 }
4829 textColor = new ColorStateList(textColor.getStates().clone(),
4830 newColors);
4831 resultSpan = new TextAppearanceSpan(
4832 originalSpan.getFamily(),
4833 originalSpan.getTextStyle(),
4834 originalSpan.getTextSize(),
4835 textColor,
4836 originalSpan.getLinkTextColor());
4837 if (fullLength) {
4838 outResultColor[0] = new ColorStateList(
4839 textColor.getStates().clone(), newColors);
4840 }
4841 }
4842 } else if (resultSpan instanceof ForegroundColorSpan) {
4843 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4844 int foregroundColor = originalSpan.getForegroundColor();
4845 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004846 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004847 resultSpan = new ForegroundColorSpan(foregroundColor);
4848 if (fullLength) {
4849 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4850 }
4851 } else {
4852 resultSpan = span;
4853 }
4854
4855 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4856 }
4857 return builder;
4858 }
4859 return charSequence;
4860 }
4861
4862 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004863 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004864 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004865 */
4866 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004867 if (!mIsLegacyInitialized) {
4868 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4869 < Build.VERSION_CODES.LOLLIPOP;
4870 mIsLegacyInitialized = true;
4871 }
4872 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004873 }
4874
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004875 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004876 return processLegacyText(charSequence, false /* ambient */);
4877 }
4878
4879 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4880 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4881 boolean wantLightText = ambient;
4882 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004883 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004884 } else {
4885 return charSequence;
4886 }
4887 }
4888
Dan Sandler26e81cf2014-05-06 10:01:27 -04004889 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004890 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004891 */
Adrian Roos487374f2017-01-11 15:48:14 -08004892 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4893 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004894 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004895 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004896 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004897 contentView.setDrawableTint(R.id.icon, false, color,
4898 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004899
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004900 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004901 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004902 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004903 }
4904
Dan Sandler26e81cf2014-05-06 10:01:27 -04004905 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004906 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004907 * if it's grayscale).
4908 */
4909 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004910 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4911 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004912 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004913 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004914 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4915 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004916 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004917 }
4918
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004919 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004920 if (mN.color != COLOR_DEFAULT) {
4921 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004922 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004923 }
4924
Adrian Roos4ff3b122016-02-01 12:26:13 -08004925 int resolveContrastColor() {
4926 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4927 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004928 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004929
Selim Cinekac5f0272017-05-02 16:05:41 -07004930 int color;
4931 int background = mBackgroundColorHint;
4932 if (mBackgroundColorHint == COLOR_INVALID) {
4933 background = mContext.getColor(
4934 com.android.internal.R.color.notification_material_background_color);
4935 }
4936 if (mN.color == COLOR_DEFAULT) {
4937 ensureColors();
4938 color = mSecondaryTextColor;
4939 } else {
4940 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004941 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004942 }
4943 if (Color.alpha(color) < 255) {
4944 // alpha doesn't go well for color filters, so let's blend it manually
4945 color = NotificationColorUtil.compositeColors(color, background);
4946 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004947 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004948 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004949 }
4950
Adrian Roos487374f2017-01-11 15:48:14 -08004951 int resolveAmbientColor() {
4952 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4953 return mCachedAmbientColor;
4954 }
4955 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4956
4957 mCachedAmbientColorIsFor = mN.color;
4958 return mCachedAmbientColor = contrasted;
4959 }
4960
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004961 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004962 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004963 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004964 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004965 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004966 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004967 mN.actions = new Action[mActions.size()];
4968 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004969 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004970 if (!mPersonList.isEmpty()) {
4971 mN.extras.putStringArray(EXTRA_PEOPLE,
4972 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004973 }
Selim Cinek247fa012016-02-18 09:50:48 -08004974 if (mN.bigContentView != null || mN.contentView != null
4975 || mN.headsUpContentView != null) {
4976 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4977 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004978 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004979 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004980
Julia Reynolds3b848122016-02-26 10:45:32 -05004981 /**
4982 * Creates a Builder from an existing notification so further changes can be made.
4983 * @param context The context for your application / activity.
4984 * @param n The notification to create a Builder from.
4985 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004986 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004987 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004988 ApplicationInfo applicationInfo = n.extras.getParcelable(
4989 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004990 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004991 if (applicationInfo != null) {
4992 try {
4993 builderContext = context.createApplicationContext(applicationInfo,
4994 Context.CONTEXT_RESTRICTED);
4995 } catch (NameNotFoundException e) {
4996 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4997 builderContext = context; // try with our context
4998 }
4999 } else {
5000 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005001 }
5002
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005003 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005004 }
5005
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005006 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005007 * @deprecated Use {@link #build()} instead.
5008 */
5009 @Deprecated
5010 public Notification getNotification() {
5011 return build();
5012 }
5013
5014 /**
5015 * Combine all of the options that have been set and return a new {@link Notification}
5016 * object.
5017 */
5018 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005019 // first, add any extras from the calling code
5020 if (mUserExtras != null) {
5021 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005022 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005023
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005024 mN.creationTime = System.currentTimeMillis();
5025
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005026 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005027 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005028
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005029 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005030
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005031 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005032 mStyle.reduceImageSizes(mContext);
5033 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005034 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005035 }
Selim Cinekd0426622017-07-11 13:19:59 +02005036 mN.reduceImageSizes(mContext);
5037
Adrian Roos5081c0d2016-02-26 16:04:19 -08005038 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005039 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005040 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005041 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005042 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5043 mN.contentView.getSequenceNumber());
5044 }
5045 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005046 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005047 if (mN.bigContentView != null) {
5048 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5049 mN.bigContentView.getSequenceNumber());
5050 }
5051 }
5052 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005053 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005054 if (mN.headsUpContentView != null) {
5055 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5056 mN.headsUpContentView.getSequenceNumber());
5057 }
5058 }
5059 }
5060
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005061 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5062 mN.flags |= FLAG_SHOW_LIGHTS;
5063 }
5064
Adrian Roosfb921842017-10-26 14:49:56 +02005065 mN.allPendingIntents = null;
5066
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005067 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005068 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005069
5070 /**
5071 * Apply this Builder to an existing {@link Notification} object.
5072 *
5073 * @hide
5074 */
5075 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005076 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005077 return n;
5078 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005079
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005080 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005081 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005082 * change. Also removes extenders on low ram devices, as
5083 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005084 *
5085 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5086 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005087 * @hide
5088 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005089 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005090 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005091
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005092 // Only strip views for known Styles because we won't know how to
5093 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005094 if (!isLowRam
5095 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005096 && getNotificationStyleClass(templateClass) == null) {
5097 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005098 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005099
5100 // Only strip unmodified BuilderRemoteViews.
5101 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005102 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005103 n.contentView.getSequenceNumber();
5104 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005105 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005106 n.bigContentView.getSequenceNumber();
5107 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005108 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005109 n.headsUpContentView.getSequenceNumber();
5110
5111 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005112 if (!isLowRam
5113 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005114 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005115 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005116
5117 Notification clone = n.clone();
5118 if (stripContentView) {
5119 clone.contentView = null;
5120 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5121 }
5122 if (stripBigContentView) {
5123 clone.bigContentView = null;
5124 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5125 }
5126 if (stripHeadsUpContentView) {
5127 clone.headsUpContentView = null;
5128 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5129 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005130 if (isLowRam) {
5131 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5132 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5133 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5134 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005135 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005136 }
5137
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005138 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005139 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005140 }
5141
5142 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005143 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005144 }
5145
5146 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005147 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005148 }
5149
5150 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005151 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005152 }
5153
5154 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005155 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005156 }
5157
Adrian Roosc1a80b02016-04-05 14:54:55 -07005158 private int getMessagingLayoutResource() {
5159 return R.layout.notification_template_material_messaging;
5160 }
5161
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005162 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005163 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005164 }
5165
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005166 private int getEmphasizedActionLayoutResource() {
5167 return R.layout.notification_material_action_emphasized;
5168 }
5169
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005170 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005171 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005172 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005173
5174 private int getBackgroundColor() {
5175 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005176 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005177 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005178 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5179 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005180 }
5181 }
5182
5183 private boolean isColorized() {
5184 return mN.isColorized();
5185 }
Selim Cinek99104832017-01-25 14:47:33 -08005186
Anthony Chenad4d1582017-04-10 16:07:58 -07005187 private boolean shouldTintActionButtons() {
5188 return mTintActionButtons;
5189 }
5190
Selim Cinek99104832017-01-25 14:47:33 -08005191 private boolean textColorsNeedInversion() {
5192 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5193 return false;
5194 }
5195 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5196 return targetSdkVersion > Build.VERSION_CODES.M
5197 && targetSdkVersion < Build.VERSION_CODES.O;
5198 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005199
5200 /**
5201 * Set a color palette to be used as the background and textColors
5202 *
5203 * @param backgroundColor the color to be used as the background
5204 * @param foregroundColor the color to be used as the foreground
5205 *
5206 * @hide
5207 */
5208 public void setColorPalette(int backgroundColor, int foregroundColor) {
5209 mBackgroundColor = backgroundColor;
5210 mForegroundColor = foregroundColor;
5211 mTextColorsAreForBackground = COLOR_INVALID;
5212 ensureColors();
5213 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005214
5215 /**
5216 * Sets the background color for this notification to be a different one then the default.
5217 * This is mainly used to calculate contrast and won't necessarily be applied to the
5218 * background.
5219 *
5220 * @hide
5221 */
5222 public void setBackgroundColorHint(int backgroundColor) {
5223 mBackgroundColorHint = backgroundColor;
5224 }
Selim Cineka7679b62017-05-10 16:33:25 -07005225
5226
5227 /**
5228 * Forces all styled remoteViews to be built from scratch and not use any cached
5229 * RemoteViews.
5230 * This is needed for legacy apps that are baking in their remoteviews into the
5231 * notification.
5232 *
5233 * @hide
5234 */
5235 public void setRebuildStyledRemoteViews(boolean rebuild) {
5236 mRebuildStyledRemoteViews = rebuild;
5237 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005238 }
5239
5240 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005241 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5242 * remote views.
5243 *
5244 * @hide
5245 */
5246 void reduceImageSizes(Context context) {
5247 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5248 return;
5249 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005250 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005251 if (mLargeIcon != null || largeIcon != null) {
5252 Resources resources = context.getResources();
5253 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005254 int maxWidth = resources.getDimensionPixelSize(isLowRam
5255 ? R.dimen.notification_right_icon_size_low_ram
5256 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005257 int maxHeight = maxWidth;
5258 if (MediaStyle.class.equals(style)
5259 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005260 maxHeight = resources.getDimensionPixelSize(isLowRam
5261 ? R.dimen.notification_media_image_max_height_low_ram
5262 : R.dimen.notification_media_image_max_height);
5263 maxWidth = resources.getDimensionPixelSize(isLowRam
5264 ? R.dimen.notification_media_image_max_width_low_ram
5265 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005266 }
5267 if (mLargeIcon != null) {
5268 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5269 }
5270 if (largeIcon != null) {
5271 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5272 }
5273 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005274 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5275 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5276 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005277 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5278 }
5279
Selim Cineka8cb1262017-08-15 16:53:44 -07005280 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5281 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005282 if (remoteView != null) {
5283 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005284 int maxWidth = resources.getDimensionPixelSize(isLowRam
5285 ? R.dimen.notification_custom_view_max_image_width_low_ram
5286 : R.dimen.notification_custom_view_max_image_width);
5287 int maxHeight = resources.getDimensionPixelSize(isLowRam
5288 ? R.dimen.notification_custom_view_max_image_height_low_ram
5289 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005290 remoteView.reduceImageSizes(maxWidth, maxHeight);
5291 }
5292 }
5293
5294 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005295 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005296 */
Selim Cinek22714f12017-04-13 16:23:53 -07005297 private boolean isForegroundService() {
5298 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005299 }
5300
5301 /**
Selim Cinek99104832017-01-25 14:47:33 -08005302 * @return whether this notification has a media session attached
5303 * @hide
5304 */
5305 public boolean hasMediaSession() {
5306 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5307 }
5308
5309 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005310 * @return the style class of this notification
5311 * @hide
5312 */
5313 public Class<? extends Notification.Style> getNotificationStyle() {
5314 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5315
5316 if (!TextUtils.isEmpty(templateClass)) {
5317 return Notification.getNotificationStyleClass(templateClass);
5318 }
5319 return null;
5320 }
5321
5322 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005323 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005324 *
5325 * @hide
5326 */
5327 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005328 if (isColorizedMedia()) {
5329 return true;
5330 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005331 return extras.getBoolean(EXTRA_COLORIZED)
5332 && (hasColorizedPermission() || isForegroundService());
5333 }
5334
5335 /**
5336 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5337 * permission. The permission is checked when a notification is enqueued.
5338 */
5339 private boolean hasColorizedPermission() {
5340 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005341 }
5342
5343 /**
5344 * @return true if this notification is colorized and it is a media notification
5345 *
5346 * @hide
5347 */
5348 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005349 Class<? extends Style> style = getNotificationStyle();
5350 if (MediaStyle.class.equals(style)) {
5351 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5352 if ((colorized == null || colorized) && hasMediaSession()) {
5353 return true;
5354 }
5355 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5356 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5357 return true;
5358 }
5359 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005360 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005361 }
5362
Selim Cinek0847acd2017-04-24 19:48:29 -07005363
5364 /**
5365 * @return true if this is a media notification
5366 *
5367 * @hide
5368 */
5369 public boolean isMediaNotification() {
5370 Class<? extends Style> style = getNotificationStyle();
5371 if (MediaStyle.class.equals(style)) {
5372 return true;
5373 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5374 return true;
5375 }
5376 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005377 }
5378
Selim Cinek279fa862016-06-14 10:57:25 -07005379 private boolean hasLargeIcon() {
5380 return mLargeIcon != null || largeIcon != null;
5381 }
5382
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005383 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005384 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005385 * @hide
5386 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005387 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005388 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5389 }
5390
5391 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005392 * @return true if the notification will show a chronometer; false otherwise
5393 * @hide
5394 */
5395 public boolean showsChronometer() {
5396 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5397 }
5398
5399 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005400 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005401 */
5402 @SystemApi
5403 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5404 Class<? extends Style>[] classes = new Class[] {
5405 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5406 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5407 MessagingStyle.class };
5408 for (Class<? extends Style> innerClass : classes) {
5409 if (templateClass.equals(innerClass.getName())) {
5410 return innerClass;
5411 }
5412 }
5413 return null;
5414 }
5415
5416 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005417 * An object that can apply a rich notification style to a {@link Notification.Builder}
5418 * object.
5419 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005420 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005421 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005422
5423 /**
5424 * @hide
5425 */
5426 protected CharSequence mSummaryText = null;
5427
5428 /**
5429 * @hide
5430 */
5431 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005432
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005433 protected Builder mBuilder;
5434
Chris Wrend6297db2012-05-03 16:20:13 -04005435 /**
5436 * Overrides ContentTitle in the big form of the template.
5437 * This defaults to the value passed to setContentTitle().
5438 */
5439 protected void internalSetBigContentTitle(CharSequence title) {
5440 mBigContentTitle = title;
5441 }
5442
5443 /**
5444 * Set the first line of text after the detail section in the big form of the template.
5445 */
5446 protected void internalSetSummaryText(CharSequence cs) {
5447 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005448 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005449 }
5450
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005451 public void setBuilder(Builder builder) {
5452 if (mBuilder != builder) {
5453 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005454 if (mBuilder != null) {
5455 mBuilder.setStyle(this);
5456 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005457 }
5458 }
5459
Chris Wrend6297db2012-05-03 16:20:13 -04005460 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005461 if (mBuilder == null) {
5462 throw new IllegalArgumentException("Style requires a valid Builder object");
5463 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005464 }
Chris Wrend6297db2012-05-03 16:20:13 -04005465
5466 protected RemoteViews getStandardView(int layoutId) {
5467 checkBuilder();
5468
Christoph Studer4600f9b2014-07-22 22:44:43 +02005469 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005470 CharSequence oldBuilderContentTitle =
5471 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005472 if (mBigContentTitle != null) {
5473 mBuilder.setContentTitle(mBigContentTitle);
5474 }
5475
Chris Wrend6297db2012-05-03 16:20:13 -04005476 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5477
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005478 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005479
Chris Wrend6297db2012-05-03 16:20:13 -04005480 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5481 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005482 } else {
5483 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005484 }
5485
Chris Wrend6297db2012-05-03 16:20:13 -04005486 return contentView;
5487 }
5488
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005489 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005490 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005491 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005492 *
5493 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005494 * @hide
5495 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005496 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005497 return null;
5498 }
5499
5500 /**
5501 * Construct a Style-specific RemoteViews for the final big notification layout.
5502 * @hide
5503 */
5504 public RemoteViews makeBigContentView() {
5505 return null;
5506 }
5507
5508 /**
5509 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005510 *
5511 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005512 * @hide
5513 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005514 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005515 return null;
5516 }
5517
5518 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005519 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005520 * @hide
5521 */
5522 public void addExtras(Bundle extras) {
5523 if (mSummaryTextSet) {
5524 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5525 }
5526 if (mBigContentTitle != null) {
5527 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5528 }
Chris Wren91ad5632013-06-05 15:05:57 -04005529 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005530 }
5531
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005532 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005533 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005534 * @hide
5535 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005536 protected void restoreFromExtras(Bundle extras) {
5537 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5538 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5539 mSummaryTextSet = true;
5540 }
5541 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5542 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5543 }
5544 }
5545
5546
5547 /**
5548 * @hide
5549 */
5550 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005551 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005552 return wip;
5553 }
5554
Daniel Sandler0ec46202015-06-24 01:27:05 -04005555 /**
5556 * @hide
5557 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005558 public void purgeResources() {}
5559
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005560 /**
5561 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5562 * attached to.
5563 *
5564 * @return the fully constructed Notification.
5565 */
5566 public Notification build() {
5567 checkBuilder();
5568 return mBuilder.build();
5569 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005570
5571 /**
5572 * @hide
5573 * @return true if the style positions the progress bar on the second line; false if the
5574 * style hides the progress bar
5575 */
5576 protected boolean hasProgress() {
5577 return true;
5578 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005579
5580 /**
5581 * @hide
5582 * @return Whether we should put the summary be put into the notification header
5583 */
5584 public boolean hasSummaryInHeader() {
5585 return true;
5586 }
Selim Cinek593610c2016-02-16 18:42:57 -08005587
5588 /**
5589 * @hide
5590 * @return Whether custom content views are displayed inline in the style
5591 */
5592 public boolean displayCustomViewInline() {
5593 return false;
5594 }
Selim Cinekd0426622017-07-11 13:19:59 +02005595
5596 /**
5597 * Reduces the image sizes contained in this style.
5598 *
5599 * @hide
5600 */
5601 public void reduceImageSizes(Context context) {
5602 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005603 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005604
5605 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005606 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005607 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005608 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005609 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005610 * Notification notif = new Notification.Builder(mContext)
5611 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5612 * .setContentText(subject)
5613 * .setSmallIcon(R.drawable.new_post)
5614 * .setLargeIcon(aBitmap)
5615 * .setStyle(new Notification.BigPictureStyle()
5616 * .bigPicture(aBigBitmap))
5617 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005618 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005619 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005620 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005621 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005622 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005623 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005624 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005625 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005626
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005627 public BigPictureStyle() {
5628 }
5629
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005630 /**
5631 * @deprecated use {@code BigPictureStyle()}.
5632 */
5633 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005634 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005635 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005636 }
5637
Chris Wrend6297db2012-05-03 16:20:13 -04005638 /**
5639 * Overrides ContentTitle in the big form of the template.
5640 * This defaults to the value passed to setContentTitle().
5641 */
5642 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005643 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005644 return this;
5645 }
5646
5647 /**
5648 * Set the first line of text after the detail section in the big form of the template.
5649 */
5650 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005651 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005652 return this;
5653 }
5654
Chris Wren0bd664d2012-08-01 13:56:56 -04005655 /**
5656 * Provide the bitmap to be used as the payload for the BigPicture notification.
5657 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005658 public BigPictureStyle bigPicture(Bitmap b) {
5659 mPicture = b;
5660 return this;
5661 }
5662
Chris Wren3745a3d2012-05-22 15:11:52 -04005663 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005664 * Override the large icon when the big notification is shown.
5665 */
5666 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005667 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5668 }
5669
5670 /**
5671 * Override the large icon when the big notification is shown.
5672 */
5673 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005674 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005675 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005676 return this;
5677 }
5678
Riley Andrews0394a0c2015-11-03 23:36:52 -08005679 /** @hide */
5680 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5681
Daniel Sandler0ec46202015-06-24 01:27:05 -04005682 /**
5683 * @hide
5684 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005685 @Override
5686 public void purgeResources() {
5687 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005688 if (mPicture != null &&
5689 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005690 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005691 mPicture = mPicture.createAshmemBitmap();
5692 }
5693 if (mBigLargeIcon != null) {
5694 mBigLargeIcon.convertToAshmem();
5695 }
5696 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005697
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005698 /**
5699 * @hide
5700 */
Selim Cinekd0426622017-07-11 13:19:59 +02005701 @Override
5702 public void reduceImageSizes(Context context) {
5703 super.reduceImageSizes(context);
5704 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005705 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005706 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005707 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5708 ? R.dimen.notification_big_picture_max_height_low_ram
5709 : R.dimen.notification_big_picture_max_height);
5710 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5711 ? R.dimen.notification_big_picture_max_width_low_ram
5712 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005713 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5714 }
5715 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005716 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5717 ? R.dimen.notification_right_icon_size_low_ram
5718 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005719 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5720 }
5721 }
5722
5723 /**
5724 * @hide
5725 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005726 public RemoteViews makeBigContentView() {
5727 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005728 // This covers the following cases:
5729 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005730 // mN.mLargeIcon
5731 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005732 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005733 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005734 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005735 oldLargeIcon = mBuilder.mN.mLargeIcon;
5736 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005737 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5738 // replacement if the other one is null. Because we're restoring these legacy icons
5739 // for old listeners, this is in general non-null.
5740 largeIconLegacy = mBuilder.mN.largeIcon;
5741 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005742 }
5743
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005744 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005745 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005746 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5747 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005748 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005749 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005750 }
Selim Cinek279fa862016-06-14 10:57:25 -07005751 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005752
Christoph Studer5c510ee2014-12-15 16:32:27 +01005753 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005754 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005755 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005756 }
5757
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005758 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005759 return contentView;
5760 }
5761
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005762 /**
5763 * @hide
5764 */
5765 public void addExtras(Bundle extras) {
5766 super.addExtras(extras);
5767
5768 if (mBigLargeIconSet) {
5769 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5770 }
5771 extras.putParcelable(EXTRA_PICTURE, mPicture);
5772 }
5773
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005774 /**
5775 * @hide
5776 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005777 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005778 protected void restoreFromExtras(Bundle extras) {
5779 super.restoreFromExtras(extras);
5780
5781 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005782 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005783 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005784 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005785 mPicture = extras.getParcelable(EXTRA_PICTURE);
5786 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005787
5788 /**
5789 * @hide
5790 */
5791 @Override
5792 public boolean hasSummaryInHeader() {
5793 return false;
5794 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005795 }
5796
5797 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005798 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005799 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005800 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005801 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005802 * Notification notif = new Notification.Builder(mContext)
5803 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5804 * .setContentText(subject)
5805 * .setSmallIcon(R.drawable.new_mail)
5806 * .setLargeIcon(aBitmap)
5807 * .setStyle(new Notification.BigTextStyle()
5808 * .bigText(aVeryLongString))
5809 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005810 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005811 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005812 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005813 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005814 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005815
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005816 private CharSequence mBigText;
5817
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005818 public BigTextStyle() {
5819 }
5820
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005821 /**
5822 * @deprecated use {@code BigTextStyle()}.
5823 */
5824 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005825 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005826 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005827 }
5828
Chris Wrend6297db2012-05-03 16:20:13 -04005829 /**
5830 * Overrides ContentTitle in the big form of the template.
5831 * This defaults to the value passed to setContentTitle().
5832 */
5833 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005834 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005835 return this;
5836 }
5837
5838 /**
5839 * Set the first line of text after the detail section in the big form of the template.
5840 */
5841 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005842 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005843 return this;
5844 }
5845
Chris Wren0bd664d2012-08-01 13:56:56 -04005846 /**
5847 * Provide the longer text to be displayed in the big form of the
5848 * template in place of the content text.
5849 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005850 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005851 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005852 return this;
5853 }
5854
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005855 /**
5856 * @hide
5857 */
5858 public void addExtras(Bundle extras) {
5859 super.addExtras(extras);
5860
Christoph Studer4600f9b2014-07-22 22:44:43 +02005861 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5862 }
5863
5864 /**
5865 * @hide
5866 */
5867 @Override
5868 protected void restoreFromExtras(Bundle extras) {
5869 super.restoreFromExtras(extras);
5870
5871 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005872 }
5873
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005874 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005875 * @param increasedHeight true if this layout be created with an increased height.
5876 *
5877 * @hide
5878 */
5879 @Override
5880 public RemoteViews makeContentView(boolean increasedHeight) {
5881 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005882 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005883 mBuilder.mActions = new ArrayList<>();
5884 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005885 mBuilder.mActions = mBuilder.mOriginalActions;
5886 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005887 return remoteViews;
5888 }
5889 return super.makeContentView(increasedHeight);
5890 }
5891
5892 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005893 * @hide
5894 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005895 @Override
5896 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5897 if (increasedHeight && mBuilder.mActions.size() > 0) {
5898 return makeBigContentView();
5899 }
5900 return super.makeHeadsUpContentView(increasedHeight);
5901 }
5902
5903 /**
5904 * @hide
5905 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005906 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005907
5908 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005909 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005910 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005911
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005912 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005913
Selim Cinek75998782016-04-26 10:39:17 -07005914 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005915
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005916 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005917 if (TextUtils.isEmpty(bigTextText)) {
5918 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5919 // experience
5920 bigTextText = mBuilder.processLegacyText(text);
5921 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005922 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005923
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005924 return contentView;
5925 }
5926
Adrian Roosb1f427c2016-05-26 12:27:15 -07005927 static void applyBigTextContentView(Builder builder,
5928 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005929 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005930 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005931 contentView.setViewVisibility(R.id.big_text,
5932 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005933 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005934 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005935 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005936
5937 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005938 * Helper class for generating large-format notifications that include multiple back-and-forth
5939 * messages of varying types between any number of people.
5940 *
5941 * <br>
5942 * If the platform does not provide large-format notifications, this method has no effect. The
5943 * user will always see the normal notification view.
5944 * <br>
5945 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5946 * so:
5947 * <pre class="prettyprint">
5948 *
5949 * Notification noti = new Notification.Builder()
5950 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5951 * .setContentText(subject)
5952 * .setSmallIcon(R.drawable.new_message)
5953 * .setLargeIcon(aBitmap)
5954 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5955 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5956 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5957 * .build();
5958 * </pre>
5959 */
5960 public static class MessagingStyle extends Style {
5961
5962 /**
5963 * The maximum number of messages that will be retained in the Notification itself (the
5964 * number displayed is up to the platform).
5965 */
5966 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5967
5968 CharSequence mUserDisplayName;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08005969 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005970 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005971 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08005972 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04005973
5974 MessagingStyle() {
5975 }
5976
5977 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005978 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5979 * user before the posting app reposts the notification with those messages after they've
5980 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005981 * {@link #addMessage(Notification.MessagingStyle.Message)}
5982 */
Alex Hillsfd590442016-10-07 09:52:44 -04005983 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005984 mUserDisplayName = userDisplayName;
5985 }
5986
5987 /**
5988 * Returns the name to be displayed for any replies sent by the user
5989 */
5990 public CharSequence getUserDisplayName() {
5991 return mUserDisplayName;
5992 }
5993
5994 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08005995 * Sets the title to be displayed on this conversation. May be set to {@code null}.
5996 *
Kodlee Yin14656422017-12-22 17:00:46 -08005997 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
5998 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
5999 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6000 * {@code true} and passing {@code null} will make it return {@code false}. In
6001 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6002 * to set group conversation status.
6003 *
6004 * @param conversationTitle Title displayed for this conversation
6005 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006006 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006007 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006008 mConversationTitle = conversationTitle;
6009 return this;
6010 }
6011
6012 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006013 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006014 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006015 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006016 public CharSequence getConversationTitle() {
6017 return mConversationTitle;
6018 }
6019
6020 /**
6021 * Adds a message for display by this notification. Convenience call for a simple
6022 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6023 * @param text A {@link CharSequence} to be displayed as the message content
6024 * @param timestamp Time at which the message arrived
6025 * @param sender A {@link CharSequence} to be used for displaying the name of the
6026 * sender. Should be <code>null</code> for messages by the current user, in which case
6027 * the platform will insert {@link #getUserDisplayName()}.
6028 * Should be unique amongst all individuals in the conversation, and should be
6029 * consistent during re-posts of the notification.
6030 *
6031 * @see Message#Message(CharSequence, long, CharSequence)
6032 *
6033 * @return this object for method chaining
6034 */
6035 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006036 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006037 }
6038
6039 /**
6040 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006041 *
6042 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6043 * the newest last.
6044 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006045 * @param message The {@link Message} to be displayed
6046 * @return this object for method chaining
6047 */
6048 public MessagingStyle addMessage(Message message) {
6049 mMessages.add(message);
6050 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6051 mMessages.remove(0);
6052 }
6053 return this;
6054 }
6055
6056 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006057 * Adds a {@link Message} for historic context in this notification.
6058 *
6059 * <p>Messages should be added as historic if they are not the main subject of the
6060 * notification but may give context to a conversation. The system may choose to present
6061 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6062 *
6063 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6064 * the newest last.
6065 *
6066 * @param message The historic {@link Message} to be added
6067 * @return this object for method chaining
6068 */
6069 public MessagingStyle addHistoricMessage(Message message) {
6070 mHistoricMessages.add(message);
6071 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6072 mHistoricMessages.remove(0);
6073 }
6074 return this;
6075 }
6076
6077 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006078 * Gets the list of {@code Message} objects that represent the notification
6079 */
6080 public List<Message> getMessages() {
6081 return mMessages;
6082 }
6083
6084 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006085 * Gets the list of historic {@code Message}s in the notification.
6086 */
6087 public List<Message> getHistoricMessages() {
6088 return mHistoricMessages;
6089 }
6090
6091 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006092 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006093 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006094 * @param isGroupConversation {@code true} if the conversation represents a group,
6095 * {@code false} otherwise.
6096 * @return this object for method chaining
6097 */
6098 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6099 mIsGroupConversation = isGroupConversation;
6100 return this;
6101 }
6102
6103 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006104 * Returns {@code true} if this notification represents a group conversation, otherwise
6105 * {@code false}.
6106 *
6107 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6108 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6109 * not the conversation title is set; returning {@code true} if the conversation title is
6110 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6111 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6112 * named, non-group conversations.
6113 *
6114 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006115 */
6116 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006117 // When target SDK version is < P, a non-null conversation title dictates if this is
6118 // as group conversation.
6119 if (mBuilder != null
6120 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6121 < Build.VERSION_CODES.P) {
6122 return mConversationTitle != null;
6123 }
6124
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006125 return mIsGroupConversation;
6126 }
6127
6128 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006129 * @hide
6130 */
6131 @Override
6132 public void addExtras(Bundle extras) {
6133 super.addExtras(extras);
6134 if (mUserDisplayName != null) {
6135 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6136 }
6137 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006138 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006139 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006140 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6141 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006142 }
Adrian Roos437cd562017-01-18 15:47:03 -08006143 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6144 Message.getBundleArrayForMessages(mHistoricMessages));
6145 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006146
6147 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006148 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006149 }
6150
6151 private void fixTitleAndTextExtras(Bundle extras) {
6152 Message m = findLatestIncomingMessage();
6153 CharSequence text = (m == null) ? null : m.mText;
6154 CharSequence sender = m == null ? null
6155 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6156 CharSequence title;
6157 if (!TextUtils.isEmpty(mConversationTitle)) {
6158 if (!TextUtils.isEmpty(sender)) {
6159 BidiFormatter bidi = BidiFormatter.getInstance();
6160 title = mBuilder.mContext.getString(
6161 com.android.internal.R.string.notification_messaging_title_template,
6162 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6163 } else {
6164 title = mConversationTitle;
6165 }
6166 } else {
6167 title = sender;
6168 }
6169
6170 if (title != null) {
6171 extras.putCharSequence(EXTRA_TITLE, title);
6172 }
6173 if (text != null) {
6174 extras.putCharSequence(EXTRA_TEXT, text);
6175 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006176 }
6177
6178 /**
6179 * @hide
6180 */
6181 @Override
6182 protected void restoreFromExtras(Bundle extras) {
6183 super.restoreFromExtras(extras);
6184
Adrian Roos96b7e202016-05-17 13:50:38 -07006185 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6186 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006187 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006188 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006189 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006190 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006191 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006192 }
6193
6194 /**
6195 * @hide
6196 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006197 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006198 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006199 mBuilder.mOriginalActions = mBuilder.mActions;
6200 mBuilder.mActions = new ArrayList<>();
6201 RemoteViews remoteViews = makeBigContentView();
6202 mBuilder.mActions = mBuilder.mOriginalActions;
6203 mBuilder.mOriginalActions = null;
6204 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006205 }
6206
6207 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006208 return findLatestIncomingMessage(mMessages);
6209 }
6210
6211 /**
6212 * @hide
6213 */
6214 @Nullable
6215 public static Message findLatestIncomingMessage(
6216 List<Message> messages) {
6217 for (int i = messages.size() - 1; i >= 0; i--) {
6218 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006219 // Incoming messages have a non-empty sender.
6220 if (!TextUtils.isEmpty(m.mSender)) {
6221 return m;
6222 }
6223 }
Selim Cinek88188f22017-09-19 16:46:56 -07006224 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006225 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006226 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006227 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006228 return null;
6229 }
6230
6231 /**
6232 * @hide
6233 */
6234 @Override
6235 public RemoteViews makeBigContentView() {
Selim Cinek88188f22017-09-19 16:46:56 -07006236 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006237 ? super.mBigContentTitle
6238 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006239 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
6240 if (isOneToOne) {
6241 // Let's add the conversationTitle in case we didn't have one before and all
6242 // messages are from the same sender
6243 conversationTitle = createConversationTitleFromMessages();
Selim Cinekf7409db2017-10-24 16:17:14 -07006244 } else if (hasOnlyWhiteSpaceSenders()) {
6245 isOneToOne = true;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006246 }
Selim Cinek88188f22017-09-19 16:46:56 -07006247 boolean hasTitle = !TextUtils.isEmpty(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07006248 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006249 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006250 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
6251 .hideLargeIcon(isOneToOne).alwaysShowReply(true));
Selim Cinek88188f22017-09-19 16:46:56 -07006252 addExtras(mBuilder.mN.extras);
6253 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6254 mBuilder.resolveContrastColor());
6255 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6256 mBuilder.mN.mLargeIcon);
6257 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6258 isOneToOne);
6259 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6260 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006261 return contentView;
6262 }
6263
Selim Cinekf7409db2017-10-24 16:17:14 -07006264 private boolean hasOnlyWhiteSpaceSenders() {
6265 for (int i = 0; i < mMessages.size(); i++) {
6266 Message m = mMessages.get(i);
6267 CharSequence sender = m.getSender();
6268 if (!isWhiteSpace(sender)) {
6269 return false;
6270 }
6271 }
6272 return true;
6273 }
6274
6275 private boolean isWhiteSpace(CharSequence sender) {
6276 if (TextUtils.isEmpty(sender)) {
6277 return true;
6278 }
6279 if (sender.toString().matches("^\\s*$")) {
6280 return true;
6281 }
6282 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6283 // For the presentation that we had.
6284 for (int i = 0; i < sender.length(); i++) {
6285 char c = sender.charAt(i);
6286 if (c != '\u200B') {
6287 return false;
6288 }
6289 }
6290 return true;
6291 }
6292
Selim Cinek88188f22017-09-19 16:46:56 -07006293 private CharSequence createConversationTitleFromMessages() {
6294 ArraySet<CharSequence> names = new ArraySet<>();
6295 for (int i = 0; i < mMessages.size(); i++) {
6296 Message m = mMessages.get(i);
6297 CharSequence sender = m.getSender();
6298 if (sender != null) {
6299 names.add(sender);
6300 }
6301 }
6302 SpannableStringBuilder title = new SpannableStringBuilder();
6303 int size = names.size();
6304 for (int i = 0; i < size; i++) {
6305 CharSequence name = names.valueAt(i);
6306 if (!TextUtils.isEmpty(title)) {
6307 title.append(", ");
6308 }
6309 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6310 }
6311 return title;
6312 }
6313
Adrian Roosdedd1df2016-04-26 16:38:47 -07006314 /**
6315 * @hide
6316 */
6317 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006318 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006319 RemoteViews remoteViews = makeBigContentView();
6320 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6321 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006322 }
6323
Adrian Roosc1a80b02016-04-05 14:54:55 -07006324 private static TextAppearanceSpan makeFontColorSpan(int color) {
6325 return new TextAppearanceSpan(null, 0, 0,
6326 ColorStateList.valueOf(color), null);
6327 }
6328
Alex Hillsd9b04d92016-04-11 16:38:16 -04006329 public static final class Message {
6330
6331 static final String KEY_TEXT = "text";
6332 static final String KEY_TIMESTAMP = "time";
6333 static final String KEY_SENDER = "sender";
6334 static final String KEY_DATA_MIME_TYPE = "type";
6335 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006336 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006337
6338 private final CharSequence mText;
6339 private final long mTimestamp;
6340 private final CharSequence mSender;
6341
Shane Brennan5a871862017-03-11 13:14:17 -08006342 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006343 private String mDataMimeType;
6344 private Uri mDataUri;
6345
6346 /**
6347 * Constructor
6348 * @param text A {@link CharSequence} to be displayed as the message content
6349 * @param timestamp Time at which the message arrived
6350 * @param sender A {@link CharSequence} to be used for displaying the name of the
6351 * sender. Should be <code>null</code> for messages by the current user, in which case
6352 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6353 * Should be unique amongst all individuals in the conversation, and should be
6354 * consistent during re-posts of the notification.
6355 */
6356 public Message(CharSequence text, long timestamp, CharSequence sender){
6357 mText = text;
6358 mTimestamp = timestamp;
6359 mSender = sender;
6360 }
6361
6362 /**
6363 * Sets a binary blob of data and an associated MIME type for a message. In the case
6364 * where the platform doesn't support the MIME type, the original text provided in the
6365 * constructor will be used.
6366 * @param dataMimeType The MIME type of the content. See
6367 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6368 * types on Android and Android Wear.
6369 * @param dataUri The uri containing the content whose type is given by the MIME type.
6370 * <p class="note">
6371 * <ol>
6372 * <li>Notification Listeners including the System UI need permission to access the
6373 * data the Uri points to. The recommended ways to do this are:</li>
6374 * <li>Store the data in your own ContentProvider, making sure that other apps have
6375 * the correct permission to access your provider. The preferred mechanism for
6376 * providing access is to use per-URI permissions which are temporary and only
6377 * grant access to the receiving application. An easy way to create a
6378 * ContentProvider like this is to use the FileProvider helper class.</li>
6379 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6380 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6381 * also store non-media types (see MediaStore.Files for more info). Files can be
6382 * inserted into the MediaStore using scanFile() after which a content:// style
6383 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6384 * Note that once added to the system MediaStore the content is accessible to any
6385 * app on the device.</li>
6386 * </ol>
6387 * @return this object for method chaining
6388 */
6389 public Message setData(String dataMimeType, Uri dataUri) {
6390 mDataMimeType = dataMimeType;
6391 mDataUri = dataUri;
6392 return this;
6393 }
6394
Alex Hillsfc737de2016-03-23 17:33:02 -04006395 /**
6396 * Get the text to be used for this message, or the fallback text if a type and content
6397 * Uri have been set
6398 */
6399 public CharSequence getText() {
6400 return mText;
6401 }
6402
6403 /**
6404 * Get the time at which this message arrived
6405 */
6406 public long getTimestamp() {
6407 return mTimestamp;
6408 }
6409
6410 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006411 * Get the extras Bundle for this message.
6412 */
6413 public Bundle getExtras() {
6414 return mExtras;
6415 }
6416
6417 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006418 * Get the text used to display the contact's name in the messaging experience
6419 */
6420 public CharSequence getSender() {
6421 return mSender;
6422 }
6423
6424 /**
6425 * Get the MIME type of the data pointed to by the Uri
6426 */
6427 public String getDataMimeType() {
6428 return mDataMimeType;
6429 }
6430
6431 /**
6432 * Get the the Uri pointing to the content of the message. Can be null, in which case
6433 * {@see #getText()} is used.
6434 */
6435 public Uri getDataUri() {
6436 return mDataUri;
6437 }
6438
Alex Hillsd9b04d92016-04-11 16:38:16 -04006439 private Bundle toBundle() {
6440 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006441 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006442 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006443 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006444 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006445 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006446 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006447 }
6448 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006449 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006450 }
6451 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006452 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006453 }
Shane Brennan5a871862017-03-11 13:14:17 -08006454 if (mExtras != null) {
6455 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6456 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006457 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006458 }
6459
Alex Hillsd9b04d92016-04-11 16:38:16 -04006460 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6461 Bundle[] bundles = new Bundle[messages.size()];
6462 final int N = messages.size();
6463 for (int i = 0; i < N; i++) {
6464 bundles[i] = messages.get(i).toBundle();
6465 }
6466 return bundles;
6467 }
6468
Selim Cinek88188f22017-09-19 16:46:56 -07006469 /**
6470 * @return A list of messages read from the bundles.
6471 *
6472 * @hide
6473 */
6474 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6475 if (bundles == null) {
6476 return new ArrayList<>();
6477 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006478 List<Message> messages = new ArrayList<>(bundles.length);
6479 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006480 if (bundles[i] instanceof Bundle) {
6481 Message message = getMessageFromBundle((Bundle)bundles[i]);
6482 if (message != null) {
6483 messages.add(message);
6484 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006485 }
6486 }
6487 return messages;
6488 }
6489
6490 static Message getMessageFromBundle(Bundle bundle) {
6491 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006492 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006493 return null;
6494 } else {
6495 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6496 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6497 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6498 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006499 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6500 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006501 }
Shane Brennan5a871862017-03-11 13:14:17 -08006502 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6503 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6504 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006505 return message;
6506 }
6507 } catch (ClassCastException e) {
6508 return null;
6509 }
6510 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006511 }
6512 }
6513
6514 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006515 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006516 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006517 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006518 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006519 * Notification notif = new Notification.Builder(mContext)
6520 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6521 * .setContentText(subject)
6522 * .setSmallIcon(R.drawable.new_mail)
6523 * .setLargeIcon(aBitmap)
6524 * .setStyle(new Notification.InboxStyle()
6525 * .addLine(str1)
6526 * .addLine(str2)
6527 * .setContentTitle(&quot;&quot;)
6528 * .setSummaryText(&quot;+3 more&quot;))
6529 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006530 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006531 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006532 * @see Notification#bigContentView
6533 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006534 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006535 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6536
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006537 public InboxStyle() {
6538 }
6539
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006540 /**
6541 * @deprecated use {@code InboxStyle()}.
6542 */
6543 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006544 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006545 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006546 }
6547
Chris Wrend6297db2012-05-03 16:20:13 -04006548 /**
6549 * Overrides ContentTitle in the big form of the template.
6550 * This defaults to the value passed to setContentTitle().
6551 */
6552 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006553 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006554 return this;
6555 }
6556
6557 /**
6558 * Set the first line of text after the detail section in the big form of the template.
6559 */
6560 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006561 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006562 return this;
6563 }
6564
Chris Wren0bd664d2012-08-01 13:56:56 -04006565 /**
6566 * Append a line to the digest section of the Inbox notification.
6567 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006568 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006569 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006570 return this;
6571 }
6572
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006573 /**
6574 * @hide
6575 */
6576 public void addExtras(Bundle extras) {
6577 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006578
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006579 CharSequence[] a = new CharSequence[mTexts.size()];
6580 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6581 }
6582
Christoph Studer4600f9b2014-07-22 22:44:43 +02006583 /**
6584 * @hide
6585 */
6586 @Override
6587 protected void restoreFromExtras(Bundle extras) {
6588 super.restoreFromExtras(extras);
6589
6590 mTexts.clear();
6591 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6592 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6593 }
6594 }
6595
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006596 /**
6597 * @hide
6598 */
6599 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006600 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006601 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006602 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6603 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006604
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006605 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006606
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006607 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006608
Chris Wrend6297db2012-05-03 16:20:13 -04006609 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 -04006610 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006611
Chris Wren4ed80d52012-05-17 09:30:03 -04006612 // Make sure all rows are gone in case we reuse a view.
6613 for (int rowId : rowIds) {
6614 contentView.setViewVisibility(rowId, View.GONE);
6615 }
6616
Daniel Sandler879c5e02012-04-17 16:46:51 -04006617 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006618 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6619 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006620 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006621 int onlyViewId = 0;
6622 int maxRows = rowIds.length;
6623 if (mBuilder.mActions.size() > 0) {
6624 maxRows--;
6625 }
6626 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006627 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006628 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006629 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006630 contentView.setTextViewText(rowIds[i],
6631 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006632 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006633 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006634 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006635 if (first) {
6636 onlyViewId = rowIds[i];
6637 } else {
6638 onlyViewId = 0;
6639 }
Selim Cinek247fa012016-02-18 09:50:48 -08006640 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006641 }
6642 i++;
6643 }
Selim Cinek07c80172016-04-21 16:40:47 -07006644 if (onlyViewId != 0) {
6645 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6646 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6647 R.dimen.notification_text_margin_top);
6648 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6649 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006650
Daniel Sandler879c5e02012-04-17 16:46:51 -04006651 return contentView;
6652 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006653
Selim Cinek247fa012016-02-18 09:50:48 -08006654 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006655 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006656 if (first) {
6657 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6658 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6659 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006660 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006661 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006662 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006663 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006664 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006665 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006666 }
Dan Sandler842dd772014-05-15 09:36:47 -04006667
6668 /**
6669 * Notification style for media playback notifications.
6670 *
6671 * In the expanded form, {@link Notification#bigContentView}, up to 5
6672 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006673 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006674 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6675 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6676 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006677 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006678 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6679 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006680 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006681 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006682 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006683 * Notifications created with MediaStyle will have their category set to
6684 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6685 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006686 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006687 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6688 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006689 * the System UI can identify this as a notification representing an active media session
6690 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6691 *
Selim Cinek99104832017-01-25 14:47:33 -08006692 * <p>
6693 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6694 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6695 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6696 * <p>
6697 *
Dan Sandler842dd772014-05-15 09:36:47 -04006698 * To use this style with your Notification, feed it to
6699 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6700 * <pre class="prettyprint">
6701 * Notification noti = new Notification.Builder()
6702 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006703 * .setContentTitle(&quot;Track title&quot;)
6704 * .setContentText(&quot;Artist - Album&quot;)
6705 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006706 * .setStyle(<b>new Notification.MediaStyle()</b>
6707 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006708 * .build();
6709 * </pre>
6710 *
6711 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006712 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006713 */
6714 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006715 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006716 static final int MAX_MEDIA_BUTTONS = 5;
6717
6718 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006719 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006720
6721 public MediaStyle() {
6722 }
6723
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006724 /**
6725 * @deprecated use {@code MediaStyle()}.
6726 */
6727 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006728 public MediaStyle(Builder builder) {
6729 setBuilder(builder);
6730 }
6731
6732 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006733 * 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 -04006734 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006735 *
6736 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006737 */
6738 public MediaStyle setShowActionsInCompactView(int...actions) {
6739 mActionsToShowInCompact = actions;
6740 return this;
6741 }
6742
6743 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006744 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6745 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006746 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006747 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006748 mToken = token;
6749 return this;
6750 }
6751
Christoph Studer4600f9b2014-07-22 22:44:43 +02006752 /**
6753 * @hide
6754 */
Dan Sandler842dd772014-05-15 09:36:47 -04006755 @Override
6756 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006757 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006758 if (wip.category == null) {
6759 wip.category = Notification.CATEGORY_TRANSPORT;
6760 }
Dan Sandler842dd772014-05-15 09:36:47 -04006761 return wip;
6762 }
6763
Christoph Studer4600f9b2014-07-22 22:44:43 +02006764 /**
6765 * @hide
6766 */
6767 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006768 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006769 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006770 }
6771
6772 /**
6773 * @hide
6774 */
6775 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006776 public RemoteViews makeBigContentView() {
6777 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006778 }
6779
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006780 /**
6781 * @hide
6782 */
6783 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006784 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006785 RemoteViews expanded = makeMediaBigContentView();
6786 return expanded != null ? expanded : makeMediaContentView();
6787 }
6788
Dan Sandler842dd772014-05-15 09:36:47 -04006789 /** @hide */
6790 @Override
6791 public void addExtras(Bundle extras) {
6792 super.addExtras(extras);
6793
6794 if (mToken != null) {
6795 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6796 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006797 if (mActionsToShowInCompact != null) {
6798 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6799 }
Dan Sandler842dd772014-05-15 09:36:47 -04006800 }
6801
Christoph Studer4600f9b2014-07-22 22:44:43 +02006802 /**
6803 * @hide
6804 */
6805 @Override
6806 protected void restoreFromExtras(Bundle extras) {
6807 super.restoreFromExtras(extras);
6808
6809 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6810 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6811 }
6812 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6813 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6814 }
6815 }
6816
Selim Cinek5bf069a2015-11-10 19:14:27 -05006817 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006818 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006819 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006820 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006821 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006822
6823 // If the action buttons should not be tinted, then just use the default
6824 // notification color. Otherwise, just use the passed-in color.
6825 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6826 ? color
6827 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6828 Notification.COLOR_DEFAULT);
6829
Sunny Goyal5b153922017-09-21 21:00:36 -07006830 button.setDrawableTint(R.id.action0, false, tintColor,
6831 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006832 if (!tombstone) {
6833 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6834 }
6835 button.setContentDescription(R.id.action0, action.title);
6836 return button;
6837 }
6838
6839 private RemoteViews makeMediaContentView() {
6840 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006841 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006842
6843 final int numActions = mBuilder.mActions.size();
6844 final int N = mActionsToShowInCompact == null
6845 ? 0
6846 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6847 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006848 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006849 for (int i = 0; i < N; i++) {
6850 if (i >= numActions) {
6851 throw new IllegalArgumentException(String.format(
6852 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6853 i, numActions - 1));
6854 }
6855
6856 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006857 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006858 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006859 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006860 }
6861 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006862 handleImage(view);
6863 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006864 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006865 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006866 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006867 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006868 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006869 return view;
6870 }
6871
Selim Cinek99104832017-01-25 14:47:33 -08006872 private int getPrimaryHighlightColor() {
6873 return mBuilder.getPrimaryHighlightColor();
6874 }
6875
Dan Sandler842dd772014-05-15 09:36:47 -04006876 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006877 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006878 // Dont add an expanded view if there is no more content to be revealed
6879 int actionsInCompact = mActionsToShowInCompact == null
6880 ? 0
6881 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006882 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006883 return null;
6884 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006885 RemoteViews big = mBuilder.applyStandardTemplate(
6886 R.layout.notification_template_material_big_media,
6887 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006888
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006889 if (actionCount > 0) {
6890 big.removeAllViews(com.android.internal.R.id.media_actions);
6891 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006892 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006893 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006894 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006895 }
6896 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006897 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006898 return big;
6899 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006900
Selim Cinek5bf069a2015-11-10 19:14:27 -05006901 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006902 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006903 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6904 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006905 }
6906 }
6907
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006908 /**
6909 * @hide
6910 */
6911 @Override
6912 protected boolean hasProgress() {
6913 return false;
6914 }
Dan Sandler842dd772014-05-15 09:36:47 -04006915 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006916
Selim Cinek593610c2016-02-16 18:42:57 -08006917 /**
6918 * Notification style for custom views that are decorated by the system
6919 *
6920 * <p>Instead of providing a notification that is completely custom, a developer can set this
6921 * style and still obtain system decorations like the notification header with the expand
6922 * affordance and actions.
6923 *
6924 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6925 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6926 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6927 * corresponding custom views to display.
6928 *
6929 * To use this style with your Notification, feed it to
6930 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6931 * <pre class="prettyprint">
6932 * Notification noti = new Notification.Builder()
6933 * .setSmallIcon(R.drawable.ic_stat_player)
6934 * .setLargeIcon(albumArtBitmap))
6935 * .setCustomContentView(contentView);
6936 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6937 * .build();
6938 * </pre>
6939 */
6940 public static class DecoratedCustomViewStyle extends Style {
6941
6942 public DecoratedCustomViewStyle() {
6943 }
6944
Selim Cinek593610c2016-02-16 18:42:57 -08006945 /**
6946 * @hide
6947 */
6948 public boolean displayCustomViewInline() {
6949 return true;
6950 }
6951
6952 /**
6953 * @hide
6954 */
6955 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006956 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006957 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6958 }
6959
6960 /**
6961 * @hide
6962 */
6963 @Override
6964 public RemoteViews makeBigContentView() {
6965 return makeDecoratedBigContentView();
6966 }
6967
6968 /**
6969 * @hide
6970 */
6971 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006972 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006973 return makeDecoratedHeadsUpContentView();
6974 }
6975
Selim Cinek593610c2016-02-16 18:42:57 -08006976 private RemoteViews makeDecoratedHeadsUpContentView() {
6977 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6978 ? mBuilder.mN.contentView
6979 : mBuilder.mN.headsUpContentView;
6980 if (mBuilder.mActions.size() == 0) {
6981 return makeStandardTemplateWithCustomContent(headsUpContentView);
6982 }
6983 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6984 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006985 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006986 return remoteViews;
6987 }
6988
Selim Cinek593610c2016-02-16 18:42:57 -08006989 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6990 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6991 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006992 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006993 return remoteViews;
6994 }
6995
Selim Cinek593610c2016-02-16 18:42:57 -08006996 private RemoteViews makeDecoratedBigContentView() {
6997 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6998 ? mBuilder.mN.contentView
6999 : mBuilder.mN.bigContentView;
7000 if (mBuilder.mActions.size() == 0) {
7001 return makeStandardTemplateWithCustomContent(bigContentView);
7002 }
7003 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7004 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007005 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007006 return remoteViews;
7007 }
Selim Cinek247fa012016-02-18 09:50:48 -08007008
7009 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7010 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007011 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007012 // Need to clone customContent before adding, because otherwise it can no longer be
7013 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007014 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007015 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7016 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007017 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007018 }
Selim Cinek247fa012016-02-18 09:50:48 -08007019 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007020 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007021 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007022 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007023 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007024 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007025 }
Selim Cinek593610c2016-02-16 18:42:57 -08007026 }
7027
Selim Cinek03eb3b72016-02-18 10:39:45 -08007028 /**
7029 * Notification style for media custom views that are decorated by the system
7030 *
7031 * <p>Instead of providing a media notification that is completely custom, a developer can set
7032 * this style and still obtain system decorations like the notification header with the expand
7033 * affordance and actions.
7034 *
7035 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7036 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7037 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7038 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007039 * <p>
7040 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7041 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7042 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007043 * To use this style with your Notification, feed it to
7044 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7045 * <pre class="prettyprint">
7046 * Notification noti = new Notification.Builder()
7047 * .setSmallIcon(R.drawable.ic_stat_player)
7048 * .setLargeIcon(albumArtBitmap))
7049 * .setCustomContentView(contentView);
7050 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7051 * .setMediaSession(mySession))
7052 * .build();
7053 * </pre>
7054 *
7055 * @see android.app.Notification.DecoratedCustomViewStyle
7056 * @see android.app.Notification.MediaStyle
7057 */
7058 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7059
7060 public DecoratedMediaCustomViewStyle() {
7061 }
7062
Selim Cinek03eb3b72016-02-18 10:39:45 -08007063 /**
7064 * @hide
7065 */
7066 public boolean displayCustomViewInline() {
7067 return true;
7068 }
7069
7070 /**
7071 * @hide
7072 */
7073 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007074 public RemoteViews makeContentView(boolean increasedHeight) {
7075 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007076 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7077 mBuilder.mN.contentView);
7078 }
7079
7080 /**
7081 * @hide
7082 */
7083 @Override
7084 public RemoteViews makeBigContentView() {
7085 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7086 ? mBuilder.mN.bigContentView
7087 : mBuilder.mN.contentView;
7088 return makeBigContentViewWithCustomContent(customRemoteView);
7089 }
7090
7091 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7092 RemoteViews remoteViews = super.makeBigContentView();
7093 if (remoteViews != null) {
7094 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7095 customRemoteView);
7096 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007097 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007098 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7099 customRemoteView);
7100 } else {
7101 return null;
7102 }
7103 }
7104
7105 /**
7106 * @hide
7107 */
7108 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007109 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007110 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7111 ? mBuilder.mN.headsUpContentView
7112 : mBuilder.mN.contentView;
7113 return makeBigContentViewWithCustomContent(customRemoteView);
7114 }
7115
7116 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7117 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007118 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007119 // Need to clone customContent before adding, because otherwise it can no longer be
7120 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007121 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007122 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007123 remoteViews.removeAllViews(id);
7124 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007125 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007126 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007127 return remoteViews;
7128 }
7129 }
7130
Christoph Studer4600f9b2014-07-22 22:44:43 +02007131 // When adding a new Style subclass here, don't forget to update
7132 // Builder.getNotificationStyleClass.
7133
Griff Hazen61a9e862014-05-22 16:05:19 -07007134 /**
7135 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7136 * metadata or change options on a notification builder.
7137 */
7138 public interface Extender {
7139 /**
7140 * Apply this extender to a notification builder.
7141 * @param builder the builder to be modified.
7142 * @return the build object for chaining.
7143 */
7144 public Builder extend(Builder builder);
7145 }
7146
7147 /**
7148 * Helper class to add wearable extensions to notifications.
7149 * <p class="note"> See
7150 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7151 * for Android Wear</a> for more information on how to use this class.
7152 * <p>
7153 * To create a notification with wearable extensions:
7154 * <ol>
7155 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7156 * properties.
7157 * <li>Create a {@link android.app.Notification.WearableExtender}.
7158 * <li>Set wearable-specific properties using the
7159 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7160 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7161 * notification.
7162 * <li>Post the notification to the notification system with the
7163 * {@code NotificationManager.notify(...)} methods.
7164 * </ol>
7165 *
7166 * <pre class="prettyprint">
7167 * Notification notif = new Notification.Builder(mContext)
7168 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7169 * .setContentText(subject)
7170 * .setSmallIcon(R.drawable.new_mail)
7171 * .extend(new Notification.WearableExtender()
7172 * .setContentIcon(R.drawable.new_mail))
7173 * .build();
7174 * NotificationManager notificationManger =
7175 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7176 * notificationManger.notify(0, notif);</pre>
7177 *
7178 * <p>Wearable extensions can be accessed on an existing notification by using the
7179 * {@code WearableExtender(Notification)} constructor,
7180 * and then using the {@code get} methods to access values.
7181 *
7182 * <pre class="prettyprint">
7183 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7184 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007185 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007186 */
7187 public static final class WearableExtender implements Extender {
7188 /**
7189 * Sentinel value for an action index that is unset.
7190 */
7191 public static final int UNSET_ACTION_INDEX = -1;
7192
7193 /**
7194 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7195 * default sizing.
7196 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007197 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007198 * on their content.
7199 */
7200 public static final int SIZE_DEFAULT = 0;
7201
7202 /**
7203 * Size value for use with {@link #setCustomSizePreset} to show this notification
7204 * with an extra small size.
7205 * <p>This value is only applicable for custom display notifications created using
7206 * {@link #setDisplayIntent}.
7207 */
7208 public static final int SIZE_XSMALL = 1;
7209
7210 /**
7211 * Size value for use with {@link #setCustomSizePreset} to show this notification
7212 * with a small size.
7213 * <p>This value is only applicable for custom display notifications created using
7214 * {@link #setDisplayIntent}.
7215 */
7216 public static final int SIZE_SMALL = 2;
7217
7218 /**
7219 * Size value for use with {@link #setCustomSizePreset} to show this notification
7220 * with a medium size.
7221 * <p>This value is only applicable for custom display notifications created using
7222 * {@link #setDisplayIntent}.
7223 */
7224 public static final int SIZE_MEDIUM = 3;
7225
7226 /**
7227 * Size value for use with {@link #setCustomSizePreset} to show this notification
7228 * with a large size.
7229 * <p>This value is only applicable for custom display notifications created using
7230 * {@link #setDisplayIntent}.
7231 */
7232 public static final int SIZE_LARGE = 4;
7233
Griff Hazend5f11f92014-05-27 15:40:09 -07007234 /**
7235 * Size value for use with {@link #setCustomSizePreset} to show this notification
7236 * full screen.
7237 * <p>This value is only applicable for custom display notifications created using
7238 * {@link #setDisplayIntent}.
7239 */
7240 public static final int SIZE_FULL_SCREEN = 5;
7241
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007242 /**
7243 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7244 * short amount of time when this notification is displayed on the screen. This
7245 * is the default value.
7246 */
7247 public static final int SCREEN_TIMEOUT_SHORT = 0;
7248
7249 /**
7250 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7251 * for a longer amount of time when this notification is displayed on the screen.
7252 */
7253 public static final int SCREEN_TIMEOUT_LONG = -1;
7254
Griff Hazen61a9e862014-05-22 16:05:19 -07007255 /** Notification extra which contains wearable extensions */
7256 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7257
Pete Gastaf6781d2014-10-07 15:17:05 -04007258 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007259 private static final String KEY_ACTIONS = "actions";
7260 private static final String KEY_FLAGS = "flags";
7261 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7262 private static final String KEY_PAGES = "pages";
7263 private static final String KEY_BACKGROUND = "background";
7264 private static final String KEY_CONTENT_ICON = "contentIcon";
7265 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7266 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7267 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7268 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7269 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007270 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007271 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007272 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007273
7274 // Flags bitwise-ored to mFlags
7275 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7276 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7277 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7278 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007279 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007280 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007281 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007282
7283 // Default value for flags integer
7284 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7285
7286 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7287 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7288
7289 private ArrayList<Action> mActions = new ArrayList<Action>();
7290 private int mFlags = DEFAULT_FLAGS;
7291 private PendingIntent mDisplayIntent;
7292 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7293 private Bitmap mBackground;
7294 private int mContentIcon;
7295 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7296 private int mContentActionIndex = UNSET_ACTION_INDEX;
7297 private int mCustomSizePreset = SIZE_DEFAULT;
7298 private int mCustomContentHeight;
7299 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007300 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007301 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007302 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007303
7304 /**
7305 * Create a {@link android.app.Notification.WearableExtender} with default
7306 * options.
7307 */
7308 public WearableExtender() {
7309 }
7310
7311 public WearableExtender(Notification notif) {
7312 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7313 if (wearableBundle != null) {
7314 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7315 if (actions != null) {
7316 mActions.addAll(actions);
7317 }
7318
7319 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7320 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7321
7322 Notification[] pages = getNotificationArrayFromBundle(
7323 wearableBundle, KEY_PAGES);
7324 if (pages != null) {
7325 Collections.addAll(mPages, pages);
7326 }
7327
7328 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7329 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7330 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7331 DEFAULT_CONTENT_ICON_GRAVITY);
7332 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7333 UNSET_ACTION_INDEX);
7334 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7335 SIZE_DEFAULT);
7336 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7337 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007338 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007339 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007340 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007341 }
7342 }
7343
7344 /**
7345 * Apply wearable extensions to a notification that is being built. This is typically
7346 * called by the {@link android.app.Notification.Builder#extend} method of
7347 * {@link android.app.Notification.Builder}.
7348 */
7349 @Override
7350 public Notification.Builder extend(Notification.Builder builder) {
7351 Bundle wearableBundle = new Bundle();
7352
7353 if (!mActions.isEmpty()) {
7354 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7355 }
7356 if (mFlags != DEFAULT_FLAGS) {
7357 wearableBundle.putInt(KEY_FLAGS, mFlags);
7358 }
7359 if (mDisplayIntent != null) {
7360 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7361 }
7362 if (!mPages.isEmpty()) {
7363 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7364 new Notification[mPages.size()]));
7365 }
7366 if (mBackground != null) {
7367 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7368 }
7369 if (mContentIcon != 0) {
7370 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7371 }
7372 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7373 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7374 }
7375 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7376 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7377 mContentActionIndex);
7378 }
7379 if (mCustomSizePreset != SIZE_DEFAULT) {
7380 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7381 }
7382 if (mCustomContentHeight != 0) {
7383 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7384 }
7385 if (mGravity != DEFAULT_GRAVITY) {
7386 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7387 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007388 if (mHintScreenTimeout != 0) {
7389 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7390 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007391 if (mDismissalId != null) {
7392 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7393 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007394 if (mBridgeTag != null) {
7395 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7396 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007397
7398 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7399 return builder;
7400 }
7401
7402 @Override
7403 public WearableExtender clone() {
7404 WearableExtender that = new WearableExtender();
7405 that.mActions = new ArrayList<Action>(this.mActions);
7406 that.mFlags = this.mFlags;
7407 that.mDisplayIntent = this.mDisplayIntent;
7408 that.mPages = new ArrayList<Notification>(this.mPages);
7409 that.mBackground = this.mBackground;
7410 that.mContentIcon = this.mContentIcon;
7411 that.mContentIconGravity = this.mContentIconGravity;
7412 that.mContentActionIndex = this.mContentActionIndex;
7413 that.mCustomSizePreset = this.mCustomSizePreset;
7414 that.mCustomContentHeight = this.mCustomContentHeight;
7415 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007416 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007417 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007418 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007419 return that;
7420 }
7421
7422 /**
7423 * Add a wearable action to this notification.
7424 *
7425 * <p>When wearable actions are added using this method, the set of actions that
7426 * show on a wearable device splits from devices that only show actions added
7427 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7428 * of which actions display on different devices.
7429 *
7430 * @param action the action to add to this notification
7431 * @return this object for method chaining
7432 * @see android.app.Notification.Action
7433 */
7434 public WearableExtender addAction(Action action) {
7435 mActions.add(action);
7436 return this;
7437 }
7438
7439 /**
7440 * Adds wearable actions to this notification.
7441 *
7442 * <p>When wearable actions are added using this method, the set of actions that
7443 * show on a wearable device splits from devices that only show actions added
7444 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7445 * of which actions display on different devices.
7446 *
7447 * @param actions the actions to add to this notification
7448 * @return this object for method chaining
7449 * @see android.app.Notification.Action
7450 */
7451 public WearableExtender addActions(List<Action> actions) {
7452 mActions.addAll(actions);
7453 return this;
7454 }
7455
7456 /**
7457 * Clear all wearable actions present on this builder.
7458 * @return this object for method chaining.
7459 * @see #addAction
7460 */
7461 public WearableExtender clearActions() {
7462 mActions.clear();
7463 return this;
7464 }
7465
7466 /**
7467 * Get the wearable actions present on this notification.
7468 */
7469 public List<Action> getActions() {
7470 return mActions;
7471 }
7472
7473 /**
7474 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007475 * this notification. The {@link PendingIntent} provided should be for an activity.
7476 *
7477 * <pre class="prettyprint">
7478 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7479 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7480 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7481 * Notification notif = new Notification.Builder(context)
7482 * .extend(new Notification.WearableExtender()
7483 * .setDisplayIntent(displayPendingIntent)
7484 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7485 * .build();</pre>
7486 *
7487 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007488 * should have an empty task affinity. It is also recommended to use the device
7489 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007490 *
7491 * <p>Example AndroidManifest.xml entry:
7492 * <pre class="prettyprint">
7493 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7494 * android:exported=&quot;true&quot;
7495 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007496 * android:taskAffinity=&quot;&quot;
7497 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007498 *
7499 * @param intent the {@link PendingIntent} for an activity
7500 * @return this object for method chaining
7501 * @see android.app.Notification.WearableExtender#getDisplayIntent
7502 */
7503 public WearableExtender setDisplayIntent(PendingIntent intent) {
7504 mDisplayIntent = intent;
7505 return this;
7506 }
7507
7508 /**
7509 * Get the intent to launch inside of an activity view when displaying this
7510 * notification. This {@code PendingIntent} should be for an activity.
7511 */
7512 public PendingIntent getDisplayIntent() {
7513 return mDisplayIntent;
7514 }
7515
7516 /**
7517 * Add an additional page of content to display with this notification. The current
7518 * notification forms the first page, and pages added using this function form
7519 * subsequent pages. This field can be used to separate a notification into multiple
7520 * sections.
7521 *
7522 * @param page the notification to add as another page
7523 * @return this object for method chaining
7524 * @see android.app.Notification.WearableExtender#getPages
7525 */
7526 public WearableExtender addPage(Notification page) {
7527 mPages.add(page);
7528 return this;
7529 }
7530
7531 /**
7532 * Add additional pages of content to display with this notification. The current
7533 * notification forms the first page, and pages added using this function form
7534 * subsequent pages. This field can be used to separate a notification into multiple
7535 * sections.
7536 *
7537 * @param pages a list of notifications
7538 * @return this object for method chaining
7539 * @see android.app.Notification.WearableExtender#getPages
7540 */
7541 public WearableExtender addPages(List<Notification> pages) {
7542 mPages.addAll(pages);
7543 return this;
7544 }
7545
7546 /**
7547 * Clear all additional pages present on this builder.
7548 * @return this object for method chaining.
7549 * @see #addPage
7550 */
7551 public WearableExtender clearPages() {
7552 mPages.clear();
7553 return this;
7554 }
7555
7556 /**
7557 * Get the array of additional pages of content for displaying this notification. The
7558 * current notification forms the first page, and elements within this array form
7559 * subsequent pages. This field can be used to separate a notification into multiple
7560 * sections.
7561 * @return the pages for this notification
7562 */
7563 public List<Notification> getPages() {
7564 return mPages;
7565 }
7566
7567 /**
7568 * Set a background image to be displayed behind the notification content.
7569 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7570 * will work with any notification style.
7571 *
7572 * @param background the background bitmap
7573 * @return this object for method chaining
7574 * @see android.app.Notification.WearableExtender#getBackground
7575 */
7576 public WearableExtender setBackground(Bitmap background) {
7577 mBackground = background;
7578 return this;
7579 }
7580
7581 /**
7582 * Get a background image to be displayed behind the notification content.
7583 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7584 * will work with any notification style.
7585 *
7586 * @return the background image
7587 * @see android.app.Notification.WearableExtender#setBackground
7588 */
7589 public Bitmap getBackground() {
7590 return mBackground;
7591 }
7592
7593 /**
7594 * Set an icon that goes with the content of this notification.
7595 */
7596 public WearableExtender setContentIcon(int icon) {
7597 mContentIcon = icon;
7598 return this;
7599 }
7600
7601 /**
7602 * Get an icon that goes with the content of this notification.
7603 */
7604 public int getContentIcon() {
7605 return mContentIcon;
7606 }
7607
7608 /**
7609 * Set the gravity that the content icon should have within the notification display.
7610 * Supported values include {@link android.view.Gravity#START} and
7611 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7612 * @see #setContentIcon
7613 */
7614 public WearableExtender setContentIconGravity(int contentIconGravity) {
7615 mContentIconGravity = contentIconGravity;
7616 return this;
7617 }
7618
7619 /**
7620 * Get the gravity that the content icon should have within the notification display.
7621 * Supported values include {@link android.view.Gravity#START} and
7622 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7623 * @see #getContentIcon
7624 */
7625 public int getContentIconGravity() {
7626 return mContentIconGravity;
7627 }
7628
7629 /**
7630 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007631 * this notification. This action will no longer display separately from the
7632 * notification's content.
7633 *
Griff Hazenca48d352014-05-28 22:37:13 -07007634 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007635 * set, although the list of available actions comes from the main notification and not
7636 * from the child page's notification.
7637 *
7638 * @param actionIndex The index of the action to hoist onto the current notification page.
7639 * If wearable actions were added to the main notification, this index
7640 * will apply to that list, otherwise it will apply to the regular
7641 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007642 */
7643 public WearableExtender setContentAction(int actionIndex) {
7644 mContentActionIndex = actionIndex;
7645 return this;
7646 }
7647
7648 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007649 * Get the index of the notification action, if any, that was specified as being clickable
7650 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007651 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007652 *
Griff Hazenca48d352014-05-28 22:37:13 -07007653 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007654 * set, although the list of available actions comes from the main notification and not
7655 * from the child page's notification.
7656 *
7657 * <p>If wearable specific actions were added to the main notification, this index will
7658 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007659 *
7660 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007661 */
7662 public int getContentAction() {
7663 return mContentActionIndex;
7664 }
7665
7666 /**
7667 * Set the gravity that this notification should have within the available viewport space.
7668 * Supported values include {@link android.view.Gravity#TOP},
7669 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7670 * The default value is {@link android.view.Gravity#BOTTOM}.
7671 */
7672 public WearableExtender setGravity(int gravity) {
7673 mGravity = gravity;
7674 return this;
7675 }
7676
7677 /**
7678 * Get the gravity that this notification should have within the available viewport space.
7679 * Supported values include {@link android.view.Gravity#TOP},
7680 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7681 * The default value is {@link android.view.Gravity#BOTTOM}.
7682 */
7683 public int getGravity() {
7684 return mGravity;
7685 }
7686
7687 /**
7688 * Set the custom size preset for the display of this notification out of the available
7689 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7690 * {@link #SIZE_LARGE}.
7691 * <p>Some custom size presets are only applicable for custom display notifications created
7692 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7693 * documentation for the preset in question. See also
7694 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7695 */
7696 public WearableExtender setCustomSizePreset(int sizePreset) {
7697 mCustomSizePreset = sizePreset;
7698 return this;
7699 }
7700
7701 /**
7702 * Get the custom size preset for the display of this notification out of the available
7703 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7704 * {@link #SIZE_LARGE}.
7705 * <p>Some custom size presets are only applicable for custom display notifications created
7706 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7707 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7708 */
7709 public int getCustomSizePreset() {
7710 return mCustomSizePreset;
7711 }
7712
7713 /**
7714 * Set the custom height in pixels for the display of this notification's content.
7715 * <p>This option is only available for custom display notifications created
7716 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7717 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7718 * {@link #getCustomContentHeight}.
7719 */
7720 public WearableExtender setCustomContentHeight(int height) {
7721 mCustomContentHeight = height;
7722 return this;
7723 }
7724
7725 /**
7726 * Get the custom height in pixels for the display of this notification's content.
7727 * <p>This option is only available for custom display notifications created
7728 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7729 * {@link #setCustomContentHeight}.
7730 */
7731 public int getCustomContentHeight() {
7732 return mCustomContentHeight;
7733 }
7734
7735 /**
7736 * Set whether the scrolling position for the contents of this notification should start
7737 * at the bottom of the contents instead of the top when the contents are too long to
7738 * display within the screen. Default is false (start scroll at the top).
7739 */
7740 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7741 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7742 return this;
7743 }
7744
7745 /**
7746 * Get whether the scrolling position for the contents of this notification should start
7747 * at the bottom of the contents instead of the top when the contents are too long to
7748 * display within the screen. Default is false (start scroll at the top).
7749 */
7750 public boolean getStartScrollBottom() {
7751 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7752 }
7753
7754 /**
7755 * Set whether the content intent is available when the wearable device is not connected
7756 * to a companion device. The user can still trigger this intent when the wearable device
7757 * is offline, but a visual hint will indicate that the content intent may not be available.
7758 * Defaults to true.
7759 */
7760 public WearableExtender setContentIntentAvailableOffline(
7761 boolean contentIntentAvailableOffline) {
7762 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7763 return this;
7764 }
7765
7766 /**
7767 * Get whether the content intent is available when the wearable device is not connected
7768 * to a companion device. The user can still trigger this intent when the wearable device
7769 * is offline, but a visual hint will indicate that the content intent may not be available.
7770 * Defaults to true.
7771 */
7772 public boolean getContentIntentAvailableOffline() {
7773 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7774 }
7775
7776 /**
7777 * Set a hint that this notification's icon should not be displayed.
7778 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7779 * @return this object for method chaining
7780 */
7781 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7782 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7783 return this;
7784 }
7785
7786 /**
7787 * Get a hint that this notification's icon should not be displayed.
7788 * @return {@code true} if this icon should not be displayed, false otherwise.
7789 * The default value is {@code false} if this was never set.
7790 */
7791 public boolean getHintHideIcon() {
7792 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7793 }
7794
7795 /**
7796 * Set a visual hint that only the background image of this notification should be
7797 * displayed, and other semantic content should be hidden. This hint is only applicable
7798 * to sub-pages added using {@link #addPage}.
7799 */
7800 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7801 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7802 return this;
7803 }
7804
7805 /**
7806 * Get a visual hint that only the background image of this notification should be
7807 * displayed, and other semantic content should be hidden. This hint is only applicable
7808 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7809 */
7810 public boolean getHintShowBackgroundOnly() {
7811 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7812 }
7813
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007814 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007815 * Set a hint that this notification's background should not be clipped if possible,
7816 * and should instead be resized to fully display on the screen, retaining the aspect
7817 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007818 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7819 * @return this object for method chaining
7820 */
7821 public WearableExtender setHintAvoidBackgroundClipping(
7822 boolean hintAvoidBackgroundClipping) {
7823 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7824 return this;
7825 }
7826
7827 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007828 * Get a hint that this notification's background should not be clipped if possible,
7829 * and should instead be resized to fully display on the screen, retaining the aspect
7830 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007831 * @return {@code true} if it's ok if the background is clipped on the screen, false
7832 * otherwise. The default value is {@code false} if this was never set.
7833 */
7834 public boolean getHintAvoidBackgroundClipping() {
7835 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7836 }
7837
7838 /**
7839 * Set a hint that the screen should remain on for at least this duration when
7840 * this notification is displayed on the screen.
7841 * @param timeout The requested screen timeout in milliseconds. Can also be either
7842 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7843 * @return this object for method chaining
7844 */
7845 public WearableExtender setHintScreenTimeout(int timeout) {
7846 mHintScreenTimeout = timeout;
7847 return this;
7848 }
7849
7850 /**
7851 * Get the duration, in milliseconds, that the screen should remain on for
7852 * when this notification is displayed.
7853 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7854 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7855 */
7856 public int getHintScreenTimeout() {
7857 return mHintScreenTimeout;
7858 }
7859
Alex Hills9ab3a232016-04-05 14:54:56 -04007860 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007861 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7862 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7863 * qr codes, as well as other simple black-and-white tickets.
7864 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7865 * @return this object for method chaining
7866 */
7867 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7868 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7869 return this;
7870 }
7871
7872 /**
7873 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7874 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7875 * qr codes, as well as other simple black-and-white tickets.
7876 * @return {@code true} if it should be displayed in ambient, false otherwise
7877 * otherwise. The default value is {@code false} if this was never set.
7878 */
7879 public boolean getHintAmbientBigPicture() {
7880 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7881 }
7882
7883 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007884 * Set a hint that this notification's content intent will launch an {@link Activity}
7885 * directly, telling the platform that it can generate the appropriate transitions.
7886 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7887 * an activity and transitions should be generated, false otherwise.
7888 * @return this object for method chaining
7889 */
7890 public WearableExtender setHintContentIntentLaunchesActivity(
7891 boolean hintContentIntentLaunchesActivity) {
7892 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7893 return this;
7894 }
7895
7896 /**
7897 * Get a hint that this notification's content intent will launch an {@link Activity}
7898 * directly, telling the platform that it can generate the appropriate transitions
7899 * @return {@code true} if the content intent will launch an activity and transitions should
7900 * be generated, false otherwise. The default value is {@code false} if this was never set.
7901 */
7902 public boolean getHintContentIntentLaunchesActivity() {
7903 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7904 }
7905
Nadia Benbernou948627e2016-04-14 14:41:08 -04007906 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007907 * Sets the dismissal id for this notification. If a notification is posted with a
7908 * dismissal id, then when that notification is canceled, notifications on other wearables
7909 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007910 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007911 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007912 * @param dismissalId the dismissal id of the notification.
7913 * @return this object for method chaining
7914 */
7915 public WearableExtender setDismissalId(String dismissalId) {
7916 mDismissalId = dismissalId;
7917 return this;
7918 }
7919
7920 /**
7921 * Returns the dismissal id of the notification.
7922 * @return the dismissal id of the notification or null if it has not been set.
7923 */
7924 public String getDismissalId() {
7925 return mDismissalId;
7926 }
7927
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007928 /**
7929 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7930 * posted from a phone to provide finer-grained control on what notifications are bridged
7931 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7932 * Features to Notifications</a> for more information.
7933 * @param bridgeTag the bridge tag of the notification.
7934 * @return this object for method chaining
7935 */
7936 public WearableExtender setBridgeTag(String bridgeTag) {
7937 mBridgeTag = bridgeTag;
7938 return this;
7939 }
7940
7941 /**
7942 * Returns the bridge tag of the notification.
7943 * @return the bridge tag or null if not present.
7944 */
7945 public String getBridgeTag() {
7946 return mBridgeTag;
7947 }
7948
Griff Hazen61a9e862014-05-22 16:05:19 -07007949 private void setFlag(int mask, boolean value) {
7950 if (value) {
7951 mFlags |= mask;
7952 } else {
7953 mFlags &= ~mask;
7954 }
7955 }
7956 }
7957
7958 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007959 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7960 * with car extensions:
7961 *
7962 * <ol>
7963 * <li>Create an {@link Notification.Builder}, setting any desired
7964 * properties.
7965 * <li>Create a {@link CarExtender}.
7966 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7967 * {@link CarExtender}.
7968 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7969 * to apply the extensions to a notification.
7970 * </ol>
7971 *
7972 * <pre class="prettyprint">
7973 * Notification notification = new Notification.Builder(context)
7974 * ...
7975 * .extend(new CarExtender()
7976 * .set*(...))
7977 * .build();
7978 * </pre>
7979 *
7980 * <p>Car extensions can be accessed on an existing notification by using the
7981 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7982 * to access values.
7983 */
7984 public static final class CarExtender implements Extender {
7985 private static final String TAG = "CarExtender";
7986
7987 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7988 private static final String EXTRA_LARGE_ICON = "large_icon";
7989 private static final String EXTRA_CONVERSATION = "car_conversation";
7990 private static final String EXTRA_COLOR = "app_color";
7991
7992 private Bitmap mLargeIcon;
7993 private UnreadConversation mUnreadConversation;
7994 private int mColor = Notification.COLOR_DEFAULT;
7995
7996 /**
7997 * Create a {@link CarExtender} with default options.
7998 */
7999 public CarExtender() {
8000 }
8001
8002 /**
8003 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8004 *
8005 * @param notif The notification from which to copy options.
8006 */
8007 public CarExtender(Notification notif) {
8008 Bundle carBundle = notif.extras == null ?
8009 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8010 if (carBundle != null) {
8011 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8012 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8013
8014 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8015 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8016 }
8017 }
8018
8019 /**
8020 * Apply car extensions to a notification that is being built. This is typically called by
8021 * the {@link Notification.Builder#extend(Notification.Extender)}
8022 * method of {@link Notification.Builder}.
8023 */
8024 @Override
8025 public Notification.Builder extend(Notification.Builder builder) {
8026 Bundle carExtensions = new Bundle();
8027
8028 if (mLargeIcon != null) {
8029 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8030 }
8031 if (mColor != Notification.COLOR_DEFAULT) {
8032 carExtensions.putInt(EXTRA_COLOR, mColor);
8033 }
8034
8035 if (mUnreadConversation != null) {
8036 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8037 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8038 }
8039
8040 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8041 return builder;
8042 }
8043
8044 /**
8045 * Sets the accent color to use when Android Auto presents the notification.
8046 *
8047 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8048 * to accent the displayed notification. However, not all colors are acceptable in an
8049 * automotive setting. This method can be used to override the color provided in the
8050 * notification in such a situation.
8051 */
Tor Norbye80756e32015-03-02 09:39:27 -08008052 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008053 mColor = color;
8054 return this;
8055 }
8056
8057 /**
8058 * Gets the accent color.
8059 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008060 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008061 */
Tor Norbye80756e32015-03-02 09:39:27 -08008062 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008063 public int getColor() {
8064 return mColor;
8065 }
8066
8067 /**
8068 * Sets the large icon of the car notification.
8069 *
8070 * If no large icon is set in the extender, Android Auto will display the icon
8071 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8072 *
8073 * @param largeIcon The large icon to use in the car notification.
8074 * @return This object for method chaining.
8075 */
8076 public CarExtender setLargeIcon(Bitmap largeIcon) {
8077 mLargeIcon = largeIcon;
8078 return this;
8079 }
8080
8081 /**
8082 * Gets the large icon used in this car notification, or null if no icon has been set.
8083 *
8084 * @return The large icon for the car notification.
8085 * @see CarExtender#setLargeIcon
8086 */
8087 public Bitmap getLargeIcon() {
8088 return mLargeIcon;
8089 }
8090
8091 /**
8092 * Sets the unread conversation in a message notification.
8093 *
8094 * @param unreadConversation The unread part of the conversation this notification conveys.
8095 * @return This object for method chaining.
8096 */
8097 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8098 mUnreadConversation = unreadConversation;
8099 return this;
8100 }
8101
8102 /**
8103 * Returns the unread conversation conveyed by this notification.
8104 * @see #setUnreadConversation(UnreadConversation)
8105 */
8106 public UnreadConversation getUnreadConversation() {
8107 return mUnreadConversation;
8108 }
8109
8110 /**
8111 * A class which holds the unread messages from a conversation.
8112 */
8113 public static class UnreadConversation {
8114 private static final String KEY_AUTHOR = "author";
8115 private static final String KEY_TEXT = "text";
8116 private static final String KEY_MESSAGES = "messages";
8117 private static final String KEY_REMOTE_INPUT = "remote_input";
8118 private static final String KEY_ON_REPLY = "on_reply";
8119 private static final String KEY_ON_READ = "on_read";
8120 private static final String KEY_PARTICIPANTS = "participants";
8121 private static final String KEY_TIMESTAMP = "timestamp";
8122
8123 private final String[] mMessages;
8124 private final RemoteInput mRemoteInput;
8125 private final PendingIntent mReplyPendingIntent;
8126 private final PendingIntent mReadPendingIntent;
8127 private final String[] mParticipants;
8128 private final long mLatestTimestamp;
8129
8130 UnreadConversation(String[] messages, RemoteInput remoteInput,
8131 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8132 String[] participants, long latestTimestamp) {
8133 mMessages = messages;
8134 mRemoteInput = remoteInput;
8135 mReadPendingIntent = readPendingIntent;
8136 mReplyPendingIntent = replyPendingIntent;
8137 mParticipants = participants;
8138 mLatestTimestamp = latestTimestamp;
8139 }
8140
8141 /**
8142 * Gets the list of messages conveyed by this notification.
8143 */
8144 public String[] getMessages() {
8145 return mMessages;
8146 }
8147
8148 /**
8149 * Gets the remote input that will be used to convey the response to a message list, or
8150 * null if no such remote input exists.
8151 */
8152 public RemoteInput getRemoteInput() {
8153 return mRemoteInput;
8154 }
8155
8156 /**
8157 * Gets the pending intent that will be triggered when the user replies to this
8158 * notification.
8159 */
8160 public PendingIntent getReplyPendingIntent() {
8161 return mReplyPendingIntent;
8162 }
8163
8164 /**
8165 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8166 * in this object's message list.
8167 */
8168 public PendingIntent getReadPendingIntent() {
8169 return mReadPendingIntent;
8170 }
8171
8172 /**
8173 * Gets the participants in the conversation.
8174 */
8175 public String[] getParticipants() {
8176 return mParticipants;
8177 }
8178
8179 /**
8180 * Gets the firs participant in the conversation.
8181 */
8182 public String getParticipant() {
8183 return mParticipants.length > 0 ? mParticipants[0] : null;
8184 }
8185
8186 /**
8187 * Gets the timestamp of the conversation.
8188 */
8189 public long getLatestTimestamp() {
8190 return mLatestTimestamp;
8191 }
8192
8193 Bundle getBundleForUnreadConversation() {
8194 Bundle b = new Bundle();
8195 String author = null;
8196 if (mParticipants != null && mParticipants.length > 1) {
8197 author = mParticipants[0];
8198 }
8199 Parcelable[] messages = new Parcelable[mMessages.length];
8200 for (int i = 0; i < messages.length; i++) {
8201 Bundle m = new Bundle();
8202 m.putString(KEY_TEXT, mMessages[i]);
8203 m.putString(KEY_AUTHOR, author);
8204 messages[i] = m;
8205 }
8206 b.putParcelableArray(KEY_MESSAGES, messages);
8207 if (mRemoteInput != null) {
8208 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8209 }
8210 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8211 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8212 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8213 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8214 return b;
8215 }
8216
8217 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8218 if (b == null) {
8219 return null;
8220 }
8221 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8222 String[] messages = null;
8223 if (parcelableMessages != null) {
8224 String[] tmp = new String[parcelableMessages.length];
8225 boolean success = true;
8226 for (int i = 0; i < tmp.length; i++) {
8227 if (!(parcelableMessages[i] instanceof Bundle)) {
8228 success = false;
8229 break;
8230 }
8231 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8232 if (tmp[i] == null) {
8233 success = false;
8234 break;
8235 }
8236 }
8237 if (success) {
8238 messages = tmp;
8239 } else {
8240 return null;
8241 }
8242 }
8243
8244 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8245 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8246
8247 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8248
8249 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8250 if (participants == null || participants.length != 1) {
8251 return null;
8252 }
8253
8254 return new UnreadConversation(messages,
8255 remoteInput,
8256 onReply,
8257 onRead,
8258 participants, b.getLong(KEY_TIMESTAMP));
8259 }
8260 };
8261
8262 /**
8263 * Builder class for {@link CarExtender.UnreadConversation} objects.
8264 */
8265 public static class Builder {
8266 private final List<String> mMessages = new ArrayList<String>();
8267 private final String mParticipant;
8268 private RemoteInput mRemoteInput;
8269 private PendingIntent mReadPendingIntent;
8270 private PendingIntent mReplyPendingIntent;
8271 private long mLatestTimestamp;
8272
8273 /**
8274 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8275 *
8276 * @param name The name of the other participant in the conversation.
8277 */
8278 public Builder(String name) {
8279 mParticipant = name;
8280 }
8281
8282 /**
8283 * Appends a new unread message to the list of messages for this conversation.
8284 *
8285 * The messages should be added from oldest to newest.
8286 *
8287 * @param message The text of the new unread message.
8288 * @return This object for method chaining.
8289 */
8290 public Builder addMessage(String message) {
8291 mMessages.add(message);
8292 return this;
8293 }
8294
8295 /**
8296 * Sets the pending intent and remote input which will convey the reply to this
8297 * notification.
8298 *
8299 * @param pendingIntent The pending intent which will be triggered on a reply.
8300 * @param remoteInput The remote input parcelable which will carry the reply.
8301 * @return This object for method chaining.
8302 *
8303 * @see CarExtender.UnreadConversation#getRemoteInput
8304 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8305 */
8306 public Builder setReplyAction(
8307 PendingIntent pendingIntent, RemoteInput remoteInput) {
8308 mRemoteInput = remoteInput;
8309 mReplyPendingIntent = pendingIntent;
8310
8311 return this;
8312 }
8313
8314 /**
8315 * Sets the pending intent that will be sent once the messages in this notification
8316 * are read.
8317 *
8318 * @param pendingIntent The pending intent to use.
8319 * @return This object for method chaining.
8320 */
8321 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8322 mReadPendingIntent = pendingIntent;
8323 return this;
8324 }
8325
8326 /**
8327 * Sets the timestamp of the most recent message in an unread conversation.
8328 *
8329 * If a messaging notification has been posted by your application and has not
8330 * yet been cancelled, posting a later notification with the same id and tag
8331 * but without a newer timestamp may result in Android Auto not displaying a
8332 * heads up notification for the later notification.
8333 *
8334 * @param timestamp The timestamp of the most recent message in the conversation.
8335 * @return This object for method chaining.
8336 */
8337 public Builder setLatestTimestamp(long timestamp) {
8338 mLatestTimestamp = timestamp;
8339 return this;
8340 }
8341
8342 /**
8343 * Builds a new unread conversation object.
8344 *
8345 * @return The new unread conversation object.
8346 */
8347 public UnreadConversation build() {
8348 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8349 String[] participants = { mParticipant };
8350 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8351 mReadPendingIntent, participants, mLatestTimestamp);
8352 }
8353 }
8354 }
8355
8356 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008357 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8358 * with a TV extension:
8359 *
8360 * <ol>
8361 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8362 * <li>Create a {@link TvExtender}.
8363 * <li>Set TV-specific properties using the {@code set} methods of
8364 * {@link TvExtender}.
8365 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8366 * to apply the extension to a notification.
8367 * </ol>
8368 *
8369 * <pre class="prettyprint">
8370 * Notification notification = new Notification.Builder(context)
8371 * ...
8372 * .extend(new TvExtender()
8373 * .set*(...))
8374 * .build();
8375 * </pre>
8376 *
8377 * <p>TV extensions can be accessed on an existing notification by using the
8378 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8379 * to access values.
8380 *
8381 * @hide
8382 */
8383 @SystemApi
8384 public static final class TvExtender implements Extender {
8385 private static final String TAG = "TvExtender";
8386
8387 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8388 private static final String EXTRA_FLAGS = "flags";
8389 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8390 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008391 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008392
8393 // Flags bitwise-ored to mFlags
8394 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8395
8396 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008397 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008398 private PendingIntent mContentIntent;
8399 private PendingIntent mDeleteIntent;
8400
8401 /**
8402 * Create a {@link TvExtender} with default options.
8403 */
8404 public TvExtender() {
8405 mFlags = FLAG_AVAILABLE_ON_TV;
8406 }
8407
8408 /**
8409 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8410 *
8411 * @param notif The notification from which to copy options.
8412 */
8413 public TvExtender(Notification notif) {
8414 Bundle bundle = notif.extras == null ?
8415 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8416 if (bundle != null) {
8417 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008418 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008419 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8420 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8421 }
8422 }
8423
8424 /**
8425 * Apply a TV extension to a notification that is being built. This is typically called by
8426 * the {@link Notification.Builder#extend(Notification.Extender)}
8427 * method of {@link Notification.Builder}.
8428 */
8429 @Override
8430 public Notification.Builder extend(Notification.Builder builder) {
8431 Bundle bundle = new Bundle();
8432
8433 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008434 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008435 if (mContentIntent != null) {
8436 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8437 }
8438
8439 if (mDeleteIntent != null) {
8440 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8441 }
8442
8443 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8444 return builder;
8445 }
8446
8447 /**
8448 * Returns true if this notification should be shown on TV. This method return true
8449 * if the notification was extended with a TvExtender.
8450 */
8451 public boolean isAvailableOnTv() {
8452 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8453 }
8454
8455 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008456 * Specifies the channel the notification should be delivered on when shown on TV.
8457 * It can be different from the channel that the notification is delivered to when
8458 * posting on a non-TV device.
8459 */
8460 public TvExtender setChannel(String channelId) {
8461 mChannelId = channelId;
8462 return this;
8463 }
8464
8465 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008466 * Specifies the channel the notification should be delivered on when shown on TV.
8467 * It can be different from the channel that the notification is delivered to when
8468 * posting on a non-TV device.
8469 */
8470 public TvExtender setChannelId(String channelId) {
8471 mChannelId = channelId;
8472 return this;
8473 }
8474
Jeff Sharkey000ce802017-04-29 13:13:27 -06008475 /** @removed */
8476 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008477 public String getChannel() {
8478 return mChannelId;
8479 }
8480
8481 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008482 * Returns the id of the channel this notification posts to on TV.
8483 */
8484 public String getChannelId() {
8485 return mChannelId;
8486 }
8487
8488 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008489 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8490 * If provided, it is used instead of the content intent specified
8491 * at the level of Notification.
8492 */
8493 public TvExtender setContentIntent(PendingIntent intent) {
8494 mContentIntent = intent;
8495 return this;
8496 }
8497
8498 /**
8499 * Returns the TV-specific content intent. If this method returns null, the
8500 * main content intent on the notification should be used.
8501 *
8502 * @see {@link Notification#contentIntent}
8503 */
8504 public PendingIntent getContentIntent() {
8505 return mContentIntent;
8506 }
8507
8508 /**
8509 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8510 * by the user on TV. If provided, it is used instead of the delete intent specified
8511 * at the level of Notification.
8512 */
8513 public TvExtender setDeleteIntent(PendingIntent intent) {
8514 mDeleteIntent = intent;
8515 return this;
8516 }
8517
8518 /**
8519 * Returns the TV-specific delete intent. If this method returns null, the
8520 * main delete intent on the notification should be used.
8521 *
8522 * @see {@link Notification#deleteIntent}
8523 */
8524 public PendingIntent getDeleteIntent() {
8525 return mDeleteIntent;
8526 }
8527 }
8528
8529 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008530 * Get an array of Notification objects from a parcelable array bundle field.
8531 * Update the bundle to have a typed array so fetches in the future don't need
8532 * to do an array copy.
8533 */
8534 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8535 Parcelable[] array = bundle.getParcelableArray(key);
8536 if (array instanceof Notification[] || array == null) {
8537 return (Notification[]) array;
8538 }
8539 Notification[] typedArray = Arrays.copyOf(array, array.length,
8540 Notification[].class);
8541 bundle.putParcelableArray(key, typedArray);
8542 return typedArray;
8543 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008544
8545 private static class BuilderRemoteViews extends RemoteViews {
8546 public BuilderRemoteViews(Parcel parcel) {
8547 super(parcel);
8548 }
8549
Kenny Guy77320062014-08-27 21:37:15 +01008550 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8551 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008552 }
8553
8554 @Override
8555 public BuilderRemoteViews clone() {
8556 Parcel p = Parcel.obtain();
8557 writeToParcel(p, 0);
8558 p.setDataPosition(0);
8559 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8560 p.recycle();
8561 return brv;
8562 }
8563 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008564
8565 private static class StandardTemplateParams {
8566 boolean hasProgress = true;
8567 boolean ambient = false;
8568 CharSequence title;
8569 CharSequence text;
Selim Cinek88188f22017-09-19 16:46:56 -07008570 boolean hideLargeIcon;
8571 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008572
8573 final StandardTemplateParams reset() {
8574 hasProgress = true;
8575 ambient = false;
8576 title = null;
8577 text = null;
8578 return this;
8579 }
8580
8581 final StandardTemplateParams hasProgress(boolean hasProgress) {
8582 this.hasProgress = hasProgress;
8583 return this;
8584 }
8585
8586 final StandardTemplateParams title(CharSequence title) {
8587 this.title = title;
8588 return this;
8589 }
8590
8591 final StandardTemplateParams text(CharSequence text) {
8592 this.text = text;
8593 return this;
8594 }
8595
Selim Cinek88188f22017-09-19 16:46:56 -07008596 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
8597 this.alwaysShowReply = alwaysShowReply;
8598 return this;
8599 }
8600
8601 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
8602 this.hideLargeIcon = hideLargeIcon;
8603 return this;
8604 }
8605
Adrian Roos70d7aa32017-01-11 15:39:06 -08008606 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008607 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008608 this.ambient = ambient;
8609 return this;
8610 }
8611
8612 final StandardTemplateParams fillTextsFrom(Builder b) {
8613 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008614 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8615
8616 // Big text notifications should contain their content when viewed in ambient mode.
8617 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8618 if (!ambient || TextUtils.isEmpty(text)) {
8619 text = extras.getCharSequence(EXTRA_TEXT);
8620 }
8621 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008622 return this;
8623 }
8624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625}