blob: fee7d6c8ba2b55a29aa114dbf33e23da3c1de00f [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;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040026import android.annotation.SdkConstant;
27import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040028import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
30import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010031import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040032import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020033import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050034import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020035import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070036import android.content.res.Configuration;
37import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040038import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010039import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070040import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010041import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010042import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040043import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040044import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070045import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080046import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070047import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040049import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020050import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050051import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070052import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Parcel;
54import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040055import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080056import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070057import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070058import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080059import android.text.SpannableStringBuilder;
60import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080062import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080063import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070064import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080065import android.text.style.RelativeSizeSpan;
66import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070067import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040068import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050069import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070070import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080071import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080072import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070073import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070074import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.RemoteViews;
76
Griff Hazen959591e2014-05-15 22:26:18 -070077import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070078import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070079import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070080import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080081import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070082
Tor Norbyed9273d62013-05-30 15:59:53 -070083import java.lang.annotation.Retention;
84import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020085import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050086import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070087import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070088import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050090import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092/**
93 * A class that represents how a persistent notification is to be presented to
94 * the user using the {@link android.app.NotificationManager}.
95 *
Joe Onoratocb109a02011-01-18 17:57:41 -080096 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
97 * easier to construct Notifications.</p>
98 *
Joe Fernandez558459f2011-10-13 16:47:36 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For a guide to creating notifications, read the
102 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
103 * developer guide.</p>
104 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 */
106public class Notification implements Parcelable
107{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400108 private static final String TAG = "Notification";
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400111 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400112 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 */
114 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
115 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
116 = "android.intent.category.NOTIFICATION_PREFERENCES";
117
118 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500119 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
120 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400121 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500122 */
123 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
124
125 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400126 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400127 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
128 * what settings should be shown in the target app.
129 */
130 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
131
132 /**
133 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
135 * that can be used to narrow down what settings should be shown in the target app.
136 */
137 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
138
139 /**
140 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
141 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
142 * that can be used to narrow down what settings should be shown in the target app.
143 */
144 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
145
146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 * Use all default values (where applicable).
148 */
149 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
152 * Use the default notification sound. This will ignore any given
153 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 *
Chris Wren47c20a12014-06-18 17:27:29 -0400155 * <p>
156 * A notification that is noisy is more likely to be presented as a heads-up notification.
157 * </p>
158 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 */
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public static final int DEFAULT_SOUND = 1;
163
164 /**
165 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700167 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 *
Chris Wren47c20a12014-06-18 17:27:29 -0400169 * <p>
170 * A notification that vibrates is more likely to be presented as a heads-up notification.
171 * </p>
172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 */
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 /**
179 * Use the default notification lights. This will ignore the
180 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
181 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 */
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200189 * Maximum length of CharSequences accepted by Builder and friends.
190 *
191 * <p>
192 * Avoids spamming the system with overly large strings such as full e-mails.
193 */
194 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
195
196 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800197 * Maximum entries of reply text that are accepted by Builder and friends.
198 */
199 private static final int MAX_REPLY_HISTORY = 5;
200
201 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500202 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800203 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 * Default value: {@link System#currentTimeMillis() Now}.
205 *
206 * Choose a timestamp that will be most relevant to the user. For most finite events, this
207 * corresponds to the time the event happened (or will happen, in the case of events that have
208 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800209 * timestamped according to when the activity began.
210 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500211 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800212 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * <ul>
214 * <li>Notification of a new chat message should be stamped when the message was received.</li>
215 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
216 * <li>Notification of a completed file download should be stamped when the download finished.</li>
217 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
218 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
219 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800220 * </ul>
221 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700222 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
223 * anymore by default and must be opted into by using
224 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
226 public long when;
227
228 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700229 * The creation time of the notification
230 */
231 private long creationTime;
232
233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400235 *
236 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
Dan Sandler86647982015-05-13 23:41:13 -0400238 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700239 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public int icon;
241
242 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800243 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
244 * leave it at its default value of 0.
245 *
246 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700247 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800248 */
249 public int iconLevel;
250
251 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500252 * The number of events that this notification represents. For example, in a new mail
253 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800254 *
Julia Reynolds30331982017-04-27 10:12:50 -0400255 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500256 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
257 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 */
Julia Reynolds30331982017-04-27 10:12:50 -0400259 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 /**
262 * The intent to execute when the expanded status entry is clicked. If
263 * this is an activity, it must include the
264 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800265 * that you take care of task management as described in the
266 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800267 * Stack</a> document. In particular, make sure to read the notification section
268 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
269 * Notifications</a> for the correct ways to launch an application from a
270 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 */
272 public PendingIntent contentIntent;
273
274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500275 * The intent to execute when the notification is explicitly dismissed by the user, either with
276 * the "Clear All" button or by swiping it away individually.
277 *
278 * This probably shouldn't be launching an activity since several of those will be sent
279 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 */
281 public PendingIntent deleteIntent;
282
283 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700284 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800285 *
Chris Wren47c20a12014-06-18 17:27:29 -0400286 * <p>
287 * The system UI may choose to display a heads-up notification, instead of
288 * launching this intent, while the user is using the device.
289 * </p>
290 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800291 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400292 */
293 public PendingIntent fullScreenIntent;
294
295 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400296 * Text that summarizes this notification for accessibility services.
297 *
298 * As of the L release, this text is no longer shown on screen, but it is still useful to
299 * accessibility services (where it serves as an audible announcement of the notification's
300 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400301 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800302 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 public CharSequence tickerText;
305
306 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400307 * Formerly, a view showing the {@link #tickerText}.
308 *
309 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400310 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400311 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800312 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400313
314 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400315 * The view that will represent this notification in the notification list (which is pulled
316 * down from the status bar).
317 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500318 * As of N, this field may be null. The notification view is determined by the inputs
319 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400320 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400322 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 public RemoteViews contentView;
324
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400325 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400326 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 * opportunity to show more detail. The system UI may choose to show this
328 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400329 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500330 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400331 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
332 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400333 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400335 public RemoteViews bigContentView;
336
Chris Wren8fd39ec2014-02-27 17:43:26 -0500337
338 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400339 * A medium-format version of {@link #contentView}, providing the Notification an
340 * opportunity to add action buttons to contentView. At its discretion, the system UI may
341 * choose to show this as a heads-up notification, which will pop up so the user can see
342 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500344 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
346 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500347 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400348 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349 public RemoteViews headsUpContentView;
350
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400351 /**
Dan Sandler86647982015-05-13 23:41:13 -0400352 * A large bitmap to be shown in the notification content area.
353 *
354 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 */
Dan Sandler86647982015-05-13 23:41:13 -0400356 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800357 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358
359 /**
360 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500361 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400363 * A notification that is noisy is more likely to be presented as a heads-up notification.
364 * </p>
365 *
366 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500367 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500369 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 public Uri sound;
373
374 /**
375 * Use this constant as the value for audioStreamType to request that
376 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700377 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400378 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500379 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700381 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 public static final int STREAM_DEFAULT = -1;
383
384 /**
385 * The audio stream type to use when playing the sound.
386 * Should be one of the STREAM_ constants from
387 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400388 *
389 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700391 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 public int audioStreamType = STREAM_DEFAULT;
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400395 * The default value of {@link #audioAttributes}.
396 */
397 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
398 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
399 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
400 .build();
401
402 /**
403 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500404 *
405 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400406 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400408 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
409
410 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500411 * The pattern with which to vibrate.
412 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * <p>
414 * To vibrate the default pattern, see {@link #defaults}.
415 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500416 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500418 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public long[] vibrate;
422
423 /**
424 * The color of the led. The hardware will do its best approximation.
425 *
426 * @see #FLAG_SHOW_LIGHTS
427 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500428 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 */
Tor Norbye80756e32015-03-02 09:39:27 -0800430 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500431 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public int ledARGB;
433
434 /**
435 * The number of milliseconds for the LED to be on while it's flashing.
436 * The hardware will do its best approximation.
437 *
438 * @see #FLAG_SHOW_LIGHTS
439 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500440 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 public int ledOnMS;
444
445 /**
446 * The number of milliseconds for the LED to be off while it's flashing.
447 * The hardware will do its best approximation.
448 *
449 * @see #FLAG_SHOW_LIGHTS
450 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500451 *
452 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500454 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public int ledOffMS;
456
457 /**
458 * Specifies which values should be taken from the defaults.
459 * <p>
460 * To set, OR the desired from {@link #DEFAULT_SOUND},
461 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
462 * values, use {@link #DEFAULT_ALL}.
463 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500464 *
465 * @deprecated use {@link NotificationChannel#getSound()} and
466 * {@link NotificationChannel#shouldShowLights()} and
467 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public int defaults;
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 /**
473 * Bit to be bitwise-ored into the {@link #flags} field that should be
474 * set if you want the LED on for this notification.
475 * <ul>
476 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
477 * or 0 for both ledOnMS and ledOffMS.</li>
478 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
479 * <li>To flash the LED, pass the number of milliseconds that it should
480 * be on and off to ledOnMS and ledOffMS.</li>
481 * </ul>
482 * <p>
483 * Since hardware varies, you are not guaranteed that any of the values
484 * you pass are honored exactly. Use the system defaults (TODO) if possible
485 * because they will be set to values that work on any given hardware.
486 * <p>
487 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500488 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500489 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
493
494 /**
495 * Bit to be bitwise-ored into the {@link #flags} field that should be
496 * set if this notification is in reference to something that is ongoing,
497 * like a phone call. It should not be set if this notification is in
498 * reference to something that happened at a particular point in time,
499 * like a missed phone call.
500 */
501 public static final int FLAG_ONGOING_EVENT = 0x00000002;
502
503 /**
504 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700505 * the audio will be repeated until the notification is
506 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 */
508 public static final int FLAG_INSISTENT = 0x00000004;
509
510 /**
511 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700512 * set if you would only like the sound, vibrate and ticker to be played
513 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 */
515 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
516
517 /**
518 * Bit to be bitwise-ored into the {@link #flags} field that should be
519 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500520 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 */
522 public static final int FLAG_AUTO_CANCEL = 0x00000010;
523
524 /**
525 * Bit to be bitwise-ored into the {@link #flags} field that should be
526 * set if the notification should not be canceled when the user clicks
527 * the Clear all button.
528 */
529 public static final int FLAG_NO_CLEAR = 0x00000020;
530
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700531 /**
532 * Bit to be bitwise-ored into the {@link #flags} field that should be
533 * set if this notification represents a currently running service. This
534 * will normally be set for you by {@link Service#startForeground}.
535 */
536 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
537
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500539 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800540 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400542 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700543 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400545
Griff Hazendfcb0802014-02-11 12:00:00 -0800546 /**
547 * Bit to be bitswise-ored into the {@link #flags} field that should be
548 * set if this notification is relevant to the current device only
549 * and it is not recommended that it bridge to other devices.
550 */
551 public static final int FLAG_LOCAL_ONLY = 0x00000100;
552
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700553 /**
554 * Bit to be bitswise-ored into the {@link #flags} field that should be
555 * set if this notification is the group summary for a group of notifications.
556 * Grouped notifications may display in a cluster or stack on devices which
557 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
558 */
559 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
560
Julia Reynoldse46bb372016-03-17 11:05:58 -0400561 /**
562 * Bit to be bitswise-ored into the {@link #flags} field that should be
563 * set if this notification is the group summary for an auto-group of notifications.
564 *
565 * @hide
566 */
567 @SystemApi
568 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
569
Julia Reynolds4db59552017-06-30 13:34:01 -0400570 /**
571 * @hide
572 */
573 public static final int FLAG_CAN_COLORIZE = 0x00000800;
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 public int flags;
576
Tor Norbyed9273d62013-05-30 15:59:53 -0700577 /** @hide */
578 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
579 @Retention(RetentionPolicy.SOURCE)
580 public @interface Priority {}
581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 * Default notification {@link #priority}. If your application does not prioritize its own
584 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500585 *
586 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500587 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500588 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 public static final int PRIORITY_DEFAULT = 0;
590
591 /**
592 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
593 * items smaller, or at a different position in the list, compared with your app's
594 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500595 *
596 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500597 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500598 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500599 public static final int PRIORITY_LOW = -1;
600
601 /**
602 * Lowest {@link #priority}; these items might not be shown to the user except under special
603 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500604 *
605 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500606 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500607 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 public static final int PRIORITY_MIN = -2;
609
610 /**
611 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
612 * show these items larger, or at a different position in notification lists, compared with
613 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500614 *
615 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500616 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500617 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500618 public static final int PRIORITY_HIGH = 1;
619
620 /**
621 * Highest {@link #priority}, for your application's most important items that require the
622 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500623 *
624 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500626 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627 public static final int PRIORITY_MAX = 2;
628
629 /**
630 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800631 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632 * Priority is an indication of how much of the user's valuable attention should be consumed by
633 * this notification. Low-priority notifications may be hidden from the user in certain
634 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500635 * system will make a determination about how to interpret this priority when presenting
636 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400637 *
638 * <p>
639 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
640 * as a heads-up notification.
641 * </p>
642 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500643 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700645 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500646 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800648
Dan Sandler26e81cf2014-05-06 10:01:27 -0400649 /**
650 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
651 * to be applied by the standard Style templates when presenting this notification.
652 *
653 * The current template design constructs a colorful header image by overlaying the
654 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
655 * ignored.
656 */
Tor Norbye80756e32015-03-02 09:39:27 -0800657 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400658 public int color = COLOR_DEFAULT;
659
660 /**
661 * Special value of {@link #color} telling the system not to decorate this notification with
662 * any special color but instead use default colors when presenting this notification.
663 */
Tor Norbye80756e32015-03-02 09:39:27 -0800664 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400665 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600666
667 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800668 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800669 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800670 */
671 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800672 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800673
674 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700675 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
676 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600677 * lockscreen).
678 *
679 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
680 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
681 * shown in all situations, but the contents are only available if the device is unlocked for
682 * the appropriate user.
683 *
684 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
685 * can be read even in an "insecure" context (that is, above a secure lockscreen).
686 * To modify the public version of this notification—for example, to redact some portions—see
687 * {@link Builder#setPublicVersion(Notification)}.
688 *
689 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
690 * and ticker until the user has bypassed the lockscreen.
691 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600692 public @Visibility int visibility;
693
694 /** @hide */
695 @IntDef(prefix = { "VISIBILITY_" }, value = {
696 VISIBILITY_PUBLIC,
697 VISIBILITY_PRIVATE,
698 VISIBILITY_SECRET,
699 })
700 @Retention(RetentionPolicy.SOURCE)
701 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600702
Griff Hazenfc3922d2014-08-20 11:56:44 -0700703 /**
704 * Notification visibility: Show this notification in its entirety on all lockscreens.
705 *
706 * {@see #visibility}
707 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600708 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700709
710 /**
711 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
712 * private information on secure lockscreens.
713 *
714 * {@see #visibility}
715 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600716 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700717
718 /**
719 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
720 *
721 * {@see #visibility}
722 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600723 public static final int VISIBILITY_SECRET = -1;
724
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500725 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400726 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500727 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729
730 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400731 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500732 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734
735 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400736 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500737 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739
740 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400741 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500742 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400743 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500744
745 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400748 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500749
750 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400753 public static final String CATEGORY_ALARM = "alarm";
754
755 /**
756 * Notification category: progress of a long-running background operation.
757 */
758 public static final String CATEGORY_PROGRESS = "progress";
759
760 /**
761 * Notification category: social network or sharing update.
762 */
763 public static final String CATEGORY_SOCIAL = "social";
764
765 /**
766 * Notification category: error in background operation or authentication status.
767 */
768 public static final String CATEGORY_ERROR = "err";
769
770 /**
771 * Notification category: media transport control for playback.
772 */
773 public static final String CATEGORY_TRANSPORT = "transport";
774
775 /**
776 * Notification category: system or device status update. Reserved for system use.
777 */
778 public static final String CATEGORY_SYSTEM = "sys";
779
780 /**
781 * Notification category: indication of running background service.
782 */
783 public static final String CATEGORY_SERVICE = "service";
784
785 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400786 * Notification category: a specific, timely recommendation for a single thing.
787 * For example, a news app might want to recommend a news story it believes the user will
788 * want to read next.
789 */
790 public static final String CATEGORY_RECOMMENDATION = "recommendation";
791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: ongoing information about device or contextual status.
794 */
795 public static final String CATEGORY_STATUS = "status";
796
797 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400798 * Notification category: user-scheduled reminder.
799 */
800 public static final String CATEGORY_REMINDER = "reminder";
801
802 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400803 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
804 * that best describes this Notification. May be used by the system for ranking and filtering.
805 */
806 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500807
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700808 private String mGroupKey;
809
810 /**
811 * Get the key used to group this notification into a cluster or stack
812 * with other notifications on devices which support such rendering.
813 */
814 public String getGroup() {
815 return mGroupKey;
816 }
817
818 private String mSortKey;
819
820 /**
821 * Get a sort key that orders this notification among other notifications from the
822 * same package. This can be useful if an external sort was already applied and an app
823 * would like to preserve this. Notifications will be sorted lexicographically using this
824 * value, although providing different priorities in addition to providing sort key may
825 * cause this value to be ignored.
826 *
827 * <p>This sort key can also be used to order members of a notification group. See
828 * {@link Builder#setGroup}.
829 *
830 * @see String#compareTo(String)
831 */
832 public String getSortKey() {
833 return mSortKey;
834 }
835
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500836 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400837 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838 * <p>
839 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
840 * APIs, and are intended to be used by
841 * {@link android.service.notification.NotificationListenerService} implementations to extract
842 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500843 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400844 public Bundle extras = new Bundle();
845
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700847 * All pending intents in the notification as the system needs to be able to access them but
848 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700849 * custom parcelable objects.
850 *
851 * @hide
852 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700853 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700854
855 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700856 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
857 * pending intents inside of it, so only those will get the behavior.
858 *
859 * @hide
860 */
861 static public IBinder whitelistToken;
862
863 /**
864 * Must be set by a process to start associating tokens with Notification objects
865 * coming in to it. This is set by NotificationManagerService.
866 *
867 * @hide
868 */
869 static public IBinder processWhitelistToken;
870
871 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872 * {@link #extras} key: this is the title of the notification,
873 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
874 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500875 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400876
877 /**
878 * {@link #extras} key: this is the title of the notification when shown in expanded form,
879 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
880 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400881 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400882
883 /**
884 * {@link #extras} key: this is the main text payload, as supplied to
885 * {@link Builder#setContentText(CharSequence)}.
886 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500887 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888
889 /**
890 * {@link #extras} key: this is a third line of text, as supplied to
891 * {@link Builder#setSubText(CharSequence)}.
892 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400893 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894
895 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800896 * {@link #extras} key: this is the remote input history, as supplied to
897 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700898 *
899 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
900 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
901 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
902 * notifications once the other party has responded).
903 *
904 * The extra with this key is of type CharSequence[] and contains the most recent entry at
905 * the 0 index, the second most recent at the 1 index, etc.
906 *
907 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800908 */
909 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
910
911 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912 * {@link #extras} key: this is a small piece of additional text as supplied to
913 * {@link Builder#setContentInfo(CharSequence)}.
914 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400915 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400916
917 /**
918 * {@link #extras} key: this is a line of summary information intended to be shown
919 * alongside expanded notifications, as supplied to (e.g.)
920 * {@link BigTextStyle#setSummaryText(CharSequence)}.
921 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400922 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400923
924 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200925 * {@link #extras} key: this is the longer text shown in the big form of a
926 * {@link BigTextStyle} notification, as supplied to
927 * {@link BigTextStyle#bigText(CharSequence)}.
928 */
929 public static final String EXTRA_BIG_TEXT = "android.bigText";
930
931 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400932 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
933 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400934 *
935 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400936 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400937 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500938 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400939
940 /**
941 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
942 * notification payload, as
943 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400944 *
945 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400946 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400947 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400948 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949
950 /**
951 * {@link #extras} key: this is a bitmap to be used instead of the one from
952 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
953 * shown in its expanded form, as supplied to
954 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is the progress value supplied to
960 * {@link Builder#setProgress(int, int, boolean)}.
961 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400962 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: this is the maximum value supplied to
966 * {@link Builder#setProgress(int, int, boolean)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969
970 /**
971 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
972 * {@link Builder#setProgress(int, int, boolean)}.
973 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400974 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400975
976 /**
977 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
978 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
979 * {@link Builder#setUsesChronometer(boolean)}.
980 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400981 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400982
983 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800984 * {@link #extras} key: whether the chronometer set on the notification should count down
985 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700986 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800987 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700988 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800989
990 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400991 * {@link #extras} key: whether {@link #when} should be shown,
992 * as supplied to {@link Builder#setShowWhen(boolean)}.
993 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400994 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995
996 /**
997 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
998 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
999 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001000 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001
1002 /**
1003 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1004 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1005 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001006 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001007
1008 /**
1009 * {@link #extras} key: A string representing the name of the specific
1010 * {@link android.app.Notification.Style} used to create this notification.
1011 */
Chris Wren91ad5632013-06-05 15:05:57 -04001012 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001013
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001015 * {@link #extras} key: A String array containing the people that this notification relates to,
1016 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001017 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001018 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001019
1020 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001021 * Allow certain system-generated notifications to appear before the device is provisioned.
1022 * Only available to notifications coming from the android package.
1023 * @hide
1024 */
Maurice Lam96c10032017-03-29 15:42:38 -07001025 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001026 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001027 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1028
1029 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001030 * {@link #extras} key: A
1031 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1032 * in the background when the notification is selected. The URI must point to an image stream
1033 * suitable for passing into
1034 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1035 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1036 * URI used for this purpose must require no permissions to read the image data.
1037 */
1038 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1039
1040 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001041 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001042 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001043 * {@link android.app.Notification.MediaStyle} notification.
1044 */
1045 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1046
1047 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001048 * {@link #extras} key: the indices of actions to be shown in the compact view,
1049 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1050 */
1051 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1052
Christoph Studer943aa672014-08-03 20:31:16 +02001053 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001054 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1055 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001056 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1057 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001058 */
1059 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1060
1061 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001062 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001063 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001064 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001065 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001066
1067 /**
1068 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1069 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001070 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1071 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001072 */
1073 public static final String EXTRA_MESSAGES = "android.messages";
1074
1075 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001076 * {@link #extras} key: an array of
1077 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1078 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1079 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1080 * array of bundles.
1081 */
1082 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1083
1084 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001085 * {@link #extras} key: whether the notification should be colorized as
1086 * supplied to {@link Builder#setColorized(boolean)}}.
1087 */
1088 public static final String EXTRA_COLORIZED = "android.colorized";
1089
1090 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001091 * @hide
1092 */
1093 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1094
Selim Cinek247fa012016-02-18 09:50:48 -08001095 /**
1096 * @hide
1097 */
1098 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1099
Shane Brennan472a3b32016-12-12 15:28:10 -08001100 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001101 * @hide
1102 */
1103 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1104
1105 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001106 * {@link #extras} key: the audio contents of this notification.
1107 *
1108 * This is for use when rendering the notification on an audio-focused interface;
1109 * the audio contents are a complete sound sample that contains the contents/body of the
1110 * notification. This may be used in substitute of a Text-to-Speech reading of the
1111 * notification. For example if the notification represents a voice message this should point
1112 * to the audio of that message.
1113 *
1114 * The data stored under this key should be a String representation of a Uri that contains the
1115 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1116 *
1117 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1118 * has a field for holding data URI. That field can be used for audio.
1119 * See {@code Message#setData}.
1120 *
1121 * Example usage:
1122 * <pre>
1123 * {@code
1124 * Notification.Builder myBuilder = (build your Notification as normal);
1125 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1126 * }
1127 * </pre>
1128 */
1129 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1130
Dan Sandler80eaa592016-04-14 23:34:54 -04001131 /** @hide */
1132 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001133 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001134 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1135
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001136 /**
1137 * This is set on the notification shown by the activity manager about all apps
1138 * running in the background. It indicates that the notification should be shown
1139 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1140 * notification currently visible to the user. This is a string array of all
1141 * package names of the apps.
1142 * @hide
1143 */
1144 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1145
Dan Sandlerd63f9322015-05-06 15:18:49 -04001146 private Icon mSmallIcon;
1147 private Icon mLargeIcon;
1148
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001149 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001150 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001151
Julia Reynolds13d898c2017-02-02 12:22:05 -05001152 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001153 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001154
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001155 /** @hide */
1156 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1157 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1158 })
1159 @Retention(RetentionPolicy.SOURCE)
1160 public @interface GroupAlertBehavior {}
1161
1162 /**
1163 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1164 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1165 * notification will not be muted when it is in a group.
1166 */
1167 public static final int GROUP_ALERT_ALL = 0;
1168
1169 /**
1170 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1171 * notification in a group should be silenced (no sound or vibration) even if they are posted
1172 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001173 * mute this notification if this notification is a group child. This must be applied to all
1174 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001175 *
1176 * <p> For example, you might want to use this constant if you post a number of children
1177 * notifications at once (say, after a periodic sync), and only need to notify the user
1178 * audibly once.
1179 */
1180 public static final int GROUP_ALERT_SUMMARY = 1;
1181
1182 /**
1183 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1184 * notification in a group should be silenced (no sound or vibration) even if they are
1185 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1186 * to mute this notification if this notification is a group summary.
1187 *
1188 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001189 * in your group have content and the summary is only used to visually group notifications
1190 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001191 */
1192 public static final int GROUP_ALERT_CHILDREN = 2;
1193
Julia Reynolds2f431e22017-06-07 14:12:09 +00001194 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001195
Julia Reynolds13d898c2017-02-02 12:22:05 -05001196 /**
1197 * If this notification is being shown as a badge, always show as a number.
1198 */
1199 public static final int BADGE_ICON_NONE = 0;
1200
1201 /**
1202 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1203 * represent this notification.
1204 */
1205 public static final int BADGE_ICON_SMALL = 1;
1206
1207 /**
1208 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1209 * represent this notification.
1210 */
1211 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001212 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001213
Chris Wren51c75102013-07-16 20:49:17 -04001214 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001215 * Structure to encapsulate a named action that can be shown as part of this notification.
1216 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1217 * selected by the user.
1218 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001219 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1220 * or {@link Notification.Builder#addAction(Notification.Action)}
1221 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001222 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001223 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001224 /**
1225 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1226 * {@link RemoteInput}s.
1227 *
1228 * This is intended for {@link RemoteInput}s that only accept data, meaning
1229 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1230 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1231 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1232 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1233 *
1234 * You can test if a RemoteInput matches these constraints using
1235 * {@link RemoteInput#isDataOnly}.
1236 */
1237 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1238
Griff Hazen959591e2014-05-15 22:26:18 -07001239 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001240 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001241 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001242 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001243
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001244 /**
1245 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001246 *
1247 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001248 */
Dan Sandler86647982015-05-13 23:41:13 -04001249 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001250 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001251
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001252 /**
1253 * Title of the action.
1254 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001255 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001256
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001257 /**
1258 * Intent to send when the user invokes this action. May be null, in which case the action
1259 * may be rendered in a disabled presentation by the system UI.
1260 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001261 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001262
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001264 if (in.readInt() != 0) {
1265 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001266 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1267 icon = mIcon.getResId();
1268 }
Dan Sandler86647982015-05-13 23:41:13 -04001269 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001270 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1271 if (in.readInt() == 1) {
1272 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1273 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001274 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001275 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001276 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001277 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001278
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001279 /**
Dan Sandler86647982015-05-13 23:41:13 -04001280 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001281 */
Dan Sandler86647982015-05-13 23:41:13 -04001282 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001283 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001284 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001285 }
1286
Adrian Roos7af53622016-10-12 13:44:05 -07001287 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001288 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001289 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001290 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001291 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1292 this.icon = icon.getResId();
1293 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001294 this.title = title;
1295 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001296 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001297 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001298 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001299 }
1300
1301 /**
Dan Sandler86647982015-05-13 23:41:13 -04001302 * Return an icon representing the action.
1303 */
1304 public Icon getIcon() {
1305 if (mIcon == null && icon != 0) {
1306 // you snuck an icon in here without using the builder; let's try to keep it
1307 mIcon = Icon.createWithResource("", icon);
1308 }
1309 return mIcon;
1310 }
1311
1312 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001313 * Get additional metadata carried around with this Action.
1314 */
1315 public Bundle getExtras() {
1316 return mExtras;
1317 }
1318
1319 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001320 * Return whether the platform should automatically generate possible replies for this
1321 * {@link Action}
1322 */
1323 public boolean getAllowGeneratedReplies() {
1324 return mAllowGeneratedReplies;
1325 }
1326
1327 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001328 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001329 * May return null if no remote inputs were added. Only returns inputs which accept
1330 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001331 */
1332 public RemoteInput[] getRemoteInputs() {
1333 return mRemoteInputs;
1334 }
1335
1336 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001337 * Get the list of inputs to be collected from the user that ONLY accept data when this
1338 * action is sent. These remote inputs are guaranteed to return true on a call to
1339 * {@link RemoteInput#isDataOnly}.
1340 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001341 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001342 *
1343 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1344 * of non-textual RemoteInputs do not access these remote inputs.
1345 */
1346 public RemoteInput[] getDataOnlyRemoteInputs() {
1347 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1348 }
1349
1350 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001351 * Builder class for {@link Action} objects.
1352 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001353 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001354 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001355 private final CharSequence mTitle;
1356 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001357 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001358 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001359 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001360
1361 /**
1362 * Construct a new builder for {@link Action} object.
1363 * @param icon icon to show for this action
1364 * @param title the title of the action
1365 * @param intent the {@link PendingIntent} to fire when users trigger this action
1366 */
Dan Sandler86647982015-05-13 23:41:13 -04001367 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001368 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001369 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001370 }
1371
1372 /**
1373 * Construct a new builder for {@link Action} object.
1374 * @param icon icon to show for this action
1375 * @param title the title of the action
1376 * @param intent the {@link PendingIntent} to fire when users trigger this action
1377 */
1378 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001379 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001380 }
1381
1382 /**
1383 * Construct a new builder for {@link Action} object using the fields from an
1384 * {@link Action}.
1385 * @param action the action to read fields from.
1386 */
1387 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001388 this(action.getIcon(), action.title, action.actionIntent,
1389 new Bundle(action.mExtras), action.getRemoteInputs(),
1390 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001391 }
1392
Dan Sandler86647982015-05-13 23:41:13 -04001393 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001394 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001395 mIcon = icon;
1396 mTitle = title;
1397 mIntent = intent;
1398 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001399 if (remoteInputs != null) {
1400 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1401 Collections.addAll(mRemoteInputs, remoteInputs);
1402 }
Adrian Roos7af53622016-10-12 13:44:05 -07001403 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001404 }
1405
1406 /**
1407 * Merge additional metadata into this builder.
1408 *
1409 * <p>Values within the Bundle will replace existing extras values in this Builder.
1410 *
1411 * @see Notification.Action#extras
1412 */
1413 public Builder addExtras(Bundle extras) {
1414 if (extras != null) {
1415 mExtras.putAll(extras);
1416 }
1417 return this;
1418 }
1419
1420 /**
1421 * Get the metadata Bundle used by this Builder.
1422 *
1423 * <p>The returned Bundle is shared with this Builder.
1424 */
1425 public Bundle getExtras() {
1426 return mExtras;
1427 }
1428
1429 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001430 * Add an input to be collected from the user when this action is sent.
1431 * Response values can be retrieved from the fired intent by using the
1432 * {@link RemoteInput#getResultsFromIntent} function.
1433 * @param remoteInput a {@link RemoteInput} to add to the action
1434 * @return this object for method chaining
1435 */
1436 public Builder addRemoteInput(RemoteInput remoteInput) {
1437 if (mRemoteInputs == null) {
1438 mRemoteInputs = new ArrayList<RemoteInput>();
1439 }
1440 mRemoteInputs.add(remoteInput);
1441 return this;
1442 }
1443
1444 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001445 * Set whether the platform should automatically generate possible replies to add to
1446 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1447 * {@link RemoteInput}, this has no effect.
1448 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1449 * otherwise
1450 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001451 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001452 */
1453 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1454 mAllowGeneratedReplies = allowGeneratedReplies;
1455 return this;
1456 }
1457
1458 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001459 * Apply an extender to this action builder. Extenders may be used to add
1460 * metadata or change options on this builder.
1461 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001462 public Builder extend(Extender extender) {
1463 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001464 return this;
1465 }
1466
1467 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001468 * Combine all of the options that have been set and return a new {@link Action}
1469 * object.
1470 * @return the built action
1471 */
1472 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001473 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1474 RemoteInput[] previousDataInputs =
1475 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001476 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001477 for (RemoteInput input : previousDataInputs) {
1478 dataOnlyInputs.add(input);
1479 }
1480 }
1481 List<RemoteInput> textInputs = new ArrayList<>();
1482 if (mRemoteInputs != null) {
1483 for (RemoteInput input : mRemoteInputs) {
1484 if (input.isDataOnly()) {
1485 dataOnlyInputs.add(input);
1486 } else {
1487 textInputs.add(input);
1488 }
1489 }
1490 }
1491 if (!dataOnlyInputs.isEmpty()) {
1492 RemoteInput[] dataInputsArr =
1493 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1494 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1495 }
1496 RemoteInput[] textInputsArr = textInputs.isEmpty()
1497 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1498 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001499 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001500 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001501 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001502
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001503 @Override
1504 public Action clone() {
1505 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001506 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001507 title,
1508 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001509 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001510 getRemoteInputs(),
1511 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001512 }
1513 @Override
1514 public int describeContents() {
1515 return 0;
1516 }
1517 @Override
1518 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001519 final Icon ic = getIcon();
1520 if (ic != null) {
1521 out.writeInt(1);
1522 ic.writeToParcel(out, 0);
1523 } else {
1524 out.writeInt(0);
1525 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001526 TextUtils.writeToParcel(title, out, flags);
1527 if (actionIntent != null) {
1528 out.writeInt(1);
1529 actionIntent.writeToParcel(out, flags);
1530 } else {
1531 out.writeInt(0);
1532 }
Griff Hazen959591e2014-05-15 22:26:18 -07001533 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001534 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001535 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001536 }
Griff Hazen959591e2014-05-15 22:26:18 -07001537 public static final Parcelable.Creator<Action> CREATOR =
1538 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001539 public Action createFromParcel(Parcel in) {
1540 return new Action(in);
1541 }
1542 public Action[] newArray(int size) {
1543 return new Action[size];
1544 }
1545 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001546
1547 /**
1548 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1549 * metadata or change options on an action builder.
1550 */
1551 public interface Extender {
1552 /**
1553 * Apply this extender to a notification action builder.
1554 * @param builder the builder to be modified.
1555 * @return the build object for chaining.
1556 */
1557 public Builder extend(Builder builder);
1558 }
1559
1560 /**
1561 * Wearable extender for notification actions. To add extensions to an action,
1562 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1563 * the {@code WearableExtender()} constructor and apply it to a
1564 * {@link android.app.Notification.Action.Builder} using
1565 * {@link android.app.Notification.Action.Builder#extend}.
1566 *
1567 * <pre class="prettyprint">
1568 * Notification.Action action = new Notification.Action.Builder(
1569 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001570 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001571 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001572 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001573 */
1574 public static final class WearableExtender implements Extender {
1575 /** Notification action extra which contains wearable extensions */
1576 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1577
Pete Gastaf6781d2014-10-07 15:17:05 -04001578 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001579 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001580 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1581 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1582 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001583
1584 // Flags bitwise-ored to mFlags
1585 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001586 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001587 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001588
1589 // Default value for flags integer
1590 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1591
1592 private int mFlags = DEFAULT_FLAGS;
1593
Pete Gastaf6781d2014-10-07 15:17:05 -04001594 private CharSequence mInProgressLabel;
1595 private CharSequence mConfirmLabel;
1596 private CharSequence mCancelLabel;
1597
Griff Hazen61a9e862014-05-22 16:05:19 -07001598 /**
1599 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1600 * options.
1601 */
1602 public WearableExtender() {
1603 }
1604
1605 /**
1606 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1607 * wearable options present in an existing notification action.
1608 * @param action the notification action to inspect.
1609 */
1610 public WearableExtender(Action action) {
1611 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1612 if (wearableBundle != null) {
1613 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001614 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1615 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1616 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001617 }
1618 }
1619
1620 /**
1621 * Apply wearable extensions to a notification action that is being built. This is
1622 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1623 * method of {@link android.app.Notification.Action.Builder}.
1624 */
1625 @Override
1626 public Action.Builder extend(Action.Builder builder) {
1627 Bundle wearableBundle = new Bundle();
1628
1629 if (mFlags != DEFAULT_FLAGS) {
1630 wearableBundle.putInt(KEY_FLAGS, mFlags);
1631 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001632 if (mInProgressLabel != null) {
1633 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1634 }
1635 if (mConfirmLabel != null) {
1636 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1637 }
1638 if (mCancelLabel != null) {
1639 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1640 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001641
1642 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1643 return builder;
1644 }
1645
1646 @Override
1647 public WearableExtender clone() {
1648 WearableExtender that = new WearableExtender();
1649 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001650 that.mInProgressLabel = this.mInProgressLabel;
1651 that.mConfirmLabel = this.mConfirmLabel;
1652 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001653 return that;
1654 }
1655
1656 /**
1657 * Set whether this action is available when the wearable device is not connected to
1658 * a companion device. The user can still trigger this action when the wearable device is
1659 * offline, but a visual hint will indicate that the action may not be available.
1660 * Defaults to true.
1661 */
1662 public WearableExtender setAvailableOffline(boolean availableOffline) {
1663 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1664 return this;
1665 }
1666
1667 /**
1668 * Get whether this action is available when the wearable device is not connected to
1669 * a companion device. The user can still trigger this action when the wearable device is
1670 * offline, but a visual hint will indicate that the action may not be available.
1671 * Defaults to true.
1672 */
1673 public boolean isAvailableOffline() {
1674 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1675 }
1676
1677 private void setFlag(int mask, boolean value) {
1678 if (value) {
1679 mFlags |= mask;
1680 } else {
1681 mFlags &= ~mask;
1682 }
1683 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001684
1685 /**
1686 * Set a label to display while the wearable is preparing to automatically execute the
1687 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1688 *
1689 * @param label the label to display while the action is being prepared to execute
1690 * @return this object for method chaining
1691 */
1692 public WearableExtender setInProgressLabel(CharSequence label) {
1693 mInProgressLabel = label;
1694 return this;
1695 }
1696
1697 /**
1698 * Get the label to display while the wearable is preparing to automatically execute
1699 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1700 *
1701 * @return the label to display while the action is being prepared to execute
1702 */
1703 public CharSequence getInProgressLabel() {
1704 return mInProgressLabel;
1705 }
1706
1707 /**
1708 * Set a label to display to confirm that the action should be executed.
1709 * This is usually an imperative verb like "Send".
1710 *
1711 * @param label the label to confirm the action should be executed
1712 * @return this object for method chaining
1713 */
1714 public WearableExtender setConfirmLabel(CharSequence label) {
1715 mConfirmLabel = label;
1716 return this;
1717 }
1718
1719 /**
1720 * Get the label to display to confirm that the action should be executed.
1721 * This is usually an imperative verb like "Send".
1722 *
1723 * @return the label to confirm the action should be executed
1724 */
1725 public CharSequence getConfirmLabel() {
1726 return mConfirmLabel;
1727 }
1728
1729 /**
1730 * Set a label to display to cancel the action.
1731 * This is usually an imperative verb, like "Cancel".
1732 *
1733 * @param label the label to display to cancel the action
1734 * @return this object for method chaining
1735 */
1736 public WearableExtender setCancelLabel(CharSequence label) {
1737 mCancelLabel = label;
1738 return this;
1739 }
1740
1741 /**
1742 * Get the label to display to cancel the action.
1743 * This is usually an imperative verb like "Cancel".
1744 *
1745 * @return the label to display to cancel the action
1746 */
1747 public CharSequence getCancelLabel() {
1748 return mCancelLabel;
1749 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001750
1751 /**
1752 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1753 * platform that it can generate the appropriate transitions.
1754 * @param hintLaunchesActivity {@code true} if the content intent will launch
1755 * an activity and transitions should be generated, false otherwise.
1756 * @return this object for method chaining
1757 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001758 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001759 boolean hintLaunchesActivity) {
1760 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1761 return this;
1762 }
1763
1764 /**
1765 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1766 * platform that it can generate the appropriate transitions
1767 * @return {@code true} if the content intent will launch an activity and transitions
1768 * should be generated, false otherwise. The default value is {@code false} if this was
1769 * never set.
1770 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001771 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001772 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1773 }
Alex Hills9f087612016-06-07 09:08:59 -04001774
1775 /**
1776 * Set a hint that this Action should be displayed inline.
1777 *
1778 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1779 * otherwise
1780 * @return this object for method chaining
1781 */
1782 public WearableExtender setHintDisplayActionInline(
1783 boolean hintDisplayInline) {
1784 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1785 return this;
1786 }
1787
1788 /**
1789 * Get a hint that this Action should be displayed inline.
1790 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001791 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001792 * otherwise. The default value is {@code false} if this was never set.
1793 */
1794 public boolean getHintDisplayActionInline() {
1795 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1796 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001797 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001798 }
1799
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001800 /**
1801 * Array of all {@link Action} structures attached to this notification by
1802 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1803 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1804 * interface for invoking actions.
1805 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001806 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001807
1808 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001809 * Replacement version of this notification whose content will be shown
1810 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1811 * and {@link #VISIBILITY_PUBLIC}.
1812 */
1813 public Notification publicVersion;
1814
1815 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001816 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001817 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 */
1819 public Notification()
1820 {
1821 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001822 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001823 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825
1826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * @hide
1828 */
1829 public Notification(Context context, int icon, CharSequence tickerText, long when,
1830 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1831 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001832 new Builder(context)
1833 .setWhen(when)
1834 .setSmallIcon(icon)
1835 .setTicker(tickerText)
1836 .setContentTitle(contentTitle)
1837 .setContentText(contentText)
1838 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1839 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841
1842 /**
1843 * Constructs a Notification object with the information needed to
1844 * have a status bar icon without the standard expanded view.
1845 *
1846 * @param icon The resource id of the icon to put in the status bar.
1847 * @param tickerText The text that flows by in the status bar when the notification first
1848 * activates.
1849 * @param when The time to show in the time field. In the System.currentTimeMillis
1850 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001851 *
1852 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001854 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 public Notification(int icon, CharSequence tickerText, long when)
1856 {
1857 this.icon = icon;
1858 this.tickerText = tickerText;
1859 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001860 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
1862
1863 /**
1864 * Unflatten the notification from a parcel.
1865 */
Svet Ganovddb94882016-06-23 19:55:24 -07001866 @SuppressWarnings("unchecked")
1867 public Notification(Parcel parcel) {
1868 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1869 // intents in extras are always written as the last entry.
1870 readFromParcelImpl(parcel);
1871 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001872 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001873 }
1874
1875 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 {
1877 int version = parcel.readInt();
1878
Dianne Hackborn98305522017-05-05 17:53:53 -07001879 whitelistToken = parcel.readStrongBinder();
1880 if (whitelistToken == null) {
1881 whitelistToken = processWhitelistToken;
1882 }
1883 // Propagate this token to all pending intents that are unmarshalled from the parcel.
1884 parcel.setClassCookie(PendingIntent.class, whitelistToken);
1885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001887 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001888 if (parcel.readInt() != 0) {
1889 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001890 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1891 icon = mSmallIcon.getResId();
1892 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 number = parcel.readInt();
1895 if (parcel.readInt() != 0) {
1896 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1897 }
1898 if (parcel.readInt() != 0) {
1899 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1900 }
1901 if (parcel.readInt() != 0) {
1902 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1903 }
1904 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001905 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001906 }
1907 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1909 }
Joe Onorato561d3852010-11-20 18:09:34 -08001910 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001911 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 defaults = parcel.readInt();
1914 flags = parcel.readInt();
1915 if (parcel.readInt() != 0) {
1916 sound = Uri.CREATOR.createFromParcel(parcel);
1917 }
1918
1919 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001920 if (parcel.readInt() != 0) {
1921 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 vibrate = parcel.createLongArray();
1924 ledARGB = parcel.readInt();
1925 ledOnMS = parcel.readInt();
1926 ledOffMS = parcel.readInt();
1927 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001928
1929 if (parcel.readInt() != 0) {
1930 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1931 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001932
1933 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001934
John Spurlockfd7f1e02014-03-18 16:41:57 -04001935 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001936
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001937 mGroupKey = parcel.readString();
1938
1939 mSortKey = parcel.readString();
1940
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001941 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001942
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001943 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1944
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001945 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001946 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1947 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001948
Chris Wren8fd39ec2014-02-27 17:43:26 -05001949 if (parcel.readInt() != 0) {
1950 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1951 }
1952
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001953 visibility = parcel.readInt();
1954
1955 if (parcel.readInt() != 0) {
1956 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1957 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001958
1959 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001960
1961 if (parcel.readInt() != 0) {
1962 mChannelId = parcel.readString();
1963 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001964 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001965
1966 if (parcel.readInt() != 0) {
1967 mShortcutId = parcel.readString();
1968 }
1969
1970 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001971
1972 if (parcel.readInt() != 0) {
1973 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1974 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001975
1976 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
1978
Andy Stadler110988c2010-12-03 14:29:16 -08001979 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001980 public Notification clone() {
1981 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001982 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001983 return that;
1984 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001985
Daniel Sandler1a497d32013-04-18 14:52:45 -04001986 /**
1987 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1988 * of this into that.
1989 * @hide
1990 */
1991 public void cloneInto(Notification that, boolean heavy) {
Dianne Hackborn98305522017-05-05 17:53:53 -07001992 that.whitelistToken = this.whitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001993 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001994 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001995 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001996 that.number = this.number;
1997
1998 // PendingIntents are global, so there's no reason (or way) to clone them.
1999 that.contentIntent = this.contentIntent;
2000 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002001 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002002
2003 if (this.tickerText != null) {
2004 that.tickerText = this.tickerText.toString();
2005 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002006 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002007 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002008 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002009 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002010 that.contentView = this.contentView.clone();
2011 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002012 if (heavy && this.mLargeIcon != null) {
2013 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002014 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002015 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002016 that.sound = this.sound; // android.net.Uri is immutable
2017 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002018 if (this.audioAttributes != null) {
2019 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2020 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002021
2022 final long[] vibrate = this.vibrate;
2023 if (vibrate != null) {
2024 final int N = vibrate.length;
2025 final long[] vib = that.vibrate = new long[N];
2026 System.arraycopy(vibrate, 0, vib, 0, N);
2027 }
2028
2029 that.ledARGB = this.ledARGB;
2030 that.ledOnMS = this.ledOnMS;
2031 that.ledOffMS = this.ledOffMS;
2032 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002033
Joe Onorato18e69df2010-05-17 22:26:12 -07002034 that.flags = this.flags;
2035
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002036 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002037
John Spurlockfd7f1e02014-03-18 16:41:57 -04002038 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002039
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002040 that.mGroupKey = this.mGroupKey;
2041
2042 that.mSortKey = this.mSortKey;
2043
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002044 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002045 try {
2046 that.extras = new Bundle(this.extras);
2047 // will unparcel
2048 that.extras.size();
2049 } catch (BadParcelableException e) {
2050 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2051 that.extras = null;
2052 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002053 }
2054
Felipe Lemedd85da62016-06-28 11:29:54 -07002055 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2056 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002057 }
2058
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002059 if (this.actions != null) {
2060 that.actions = new Action[this.actions.length];
2061 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002062 if ( this.actions[i] != null) {
2063 that.actions[i] = this.actions[i].clone();
2064 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002065 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002066 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002067
Daniel Sandler1a497d32013-04-18 14:52:45 -04002068 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002069 that.bigContentView = this.bigContentView.clone();
2070 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002071
Chris Wren8fd39ec2014-02-27 17:43:26 -05002072 if (heavy && this.headsUpContentView != null) {
2073 that.headsUpContentView = this.headsUpContentView.clone();
2074 }
2075
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002076 that.visibility = this.visibility;
2077
2078 if (this.publicVersion != null) {
2079 that.publicVersion = new Notification();
2080 this.publicVersion.cloneInto(that.publicVersion, heavy);
2081 }
2082
Dan Sandler26e81cf2014-05-06 10:01:27 -04002083 that.color = this.color;
2084
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002085 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002086 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002087 that.mShortcutId = this.mShortcutId;
2088 that.mBadgeIcon = this.mBadgeIcon;
2089 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002090 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002091
Daniel Sandler1a497d32013-04-18 14:52:45 -04002092 if (!heavy) {
2093 that.lightenPayload(); // will clean out extras
2094 }
2095 }
2096
2097 /**
2098 * Removes heavyweight parts of the Notification object for archival or for sending to
2099 * listeners when the full contents are not necessary.
2100 * @hide
2101 */
2102 public final void lightenPayload() {
2103 tickerView = null;
2104 contentView = null;
2105 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002106 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002107 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002108 if (extras != null && !extras.isEmpty()) {
2109 final Set<String> keyset = extras.keySet();
2110 final int N = keyset.size();
2111 final String[] keys = keyset.toArray(new String[N]);
2112 for (int i=0; i<N; i++) {
2113 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002114 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2115 continue;
2116 }
Dan Sandler50128532015-12-08 15:42:41 -05002117 final Object obj = extras.get(key);
2118 if (obj != null &&
2119 ( obj instanceof Parcelable
2120 || obj instanceof Parcelable[]
2121 || obj instanceof SparseArray
2122 || obj instanceof ArrayList)) {
2123 extras.remove(key);
2124 }
2125 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002126 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002127 }
2128
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002129 /**
2130 * Make sure this CharSequence is safe to put into a bundle, which basically
2131 * means it had better not be some custom Parcelable implementation.
2132 * @hide
2133 */
2134 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002135 if (cs == null) return cs;
2136 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2137 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2138 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002139 if (cs instanceof Parcelable) {
2140 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2141 + " instance is a custom Parcelable and not allowed in Notification");
2142 return cs.toString();
2143 }
Selim Cinek60a54252016-02-26 17:03:25 -08002144 return removeTextSizeSpans(cs);
2145 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002146
Selim Cinek60a54252016-02-26 17:03:25 -08002147 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2148 if (charSequence instanceof Spanned) {
2149 Spanned ss = (Spanned) charSequence;
2150 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2151 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2152 for (Object span : spans) {
2153 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002154 if (resultSpan instanceof CharacterStyle) {
2155 resultSpan = ((CharacterStyle) span).getUnderlying();
2156 }
2157 if (resultSpan instanceof TextAppearanceSpan) {
2158 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002159 resultSpan = new TextAppearanceSpan(
2160 originalSpan.getFamily(),
2161 originalSpan.getTextStyle(),
2162 -1,
2163 originalSpan.getTextColor(),
2164 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002165 } else if (resultSpan instanceof RelativeSizeSpan
2166 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002167 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002168 } else {
2169 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002170 }
2171 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2172 ss.getSpanFlags(span));
2173 }
2174 return builder;
2175 }
2176 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002177 }
2178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 public int describeContents() {
2180 return 0;
2181 }
2182
2183 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002184 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 */
Svet Ganovddb94882016-06-23 19:55:24 -07002186 public void writeToParcel(Parcel parcel, int flags) {
2187 // We need to mark all pending intents getting into the notification
2188 // system as being put there to later allow the notification ranker
2189 // to launch them and by doing so add the app to the battery saver white
2190 // list for a short period of time. The problem is that the system
2191 // cannot look into the extras as there may be parcelables there that
2192 // the platform does not know how to handle. To go around that we have
2193 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002194 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002195 if (collectPendingIntents) {
2196 PendingIntent.setOnMarshaledListener(
2197 (PendingIntent intent, Parcel out, int outFlags) -> {
2198 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002199 if (allPendingIntents == null) {
2200 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002201 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002202 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002203 }
2204 });
2205 }
2206 try {
2207 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002208 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002209 writeToParcelImpl(parcel, flags);
2210 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002211 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002212 } finally {
2213 if (collectPendingIntents) {
2214 PendingIntent.setOnMarshaledListener(null);
2215 }
2216 }
2217 }
2218
2219 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 parcel.writeInt(1);
2221
Dianne Hackborn98305522017-05-05 17:53:53 -07002222 parcel.writeStrongBinder(whitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002224 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002225 if (mSmallIcon == null && icon != 0) {
2226 // you snuck an icon in here without using the builder; let's try to keep it
2227 mSmallIcon = Icon.createWithResource("", icon);
2228 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002229 if (mSmallIcon != null) {
2230 parcel.writeInt(1);
2231 mSmallIcon.writeToParcel(parcel, 0);
2232 } else {
2233 parcel.writeInt(0);
2234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 parcel.writeInt(number);
2236 if (contentIntent != null) {
2237 parcel.writeInt(1);
2238 contentIntent.writeToParcel(parcel, 0);
2239 } else {
2240 parcel.writeInt(0);
2241 }
2242 if (deleteIntent != null) {
2243 parcel.writeInt(1);
2244 deleteIntent.writeToParcel(parcel, 0);
2245 } else {
2246 parcel.writeInt(0);
2247 }
2248 if (tickerText != null) {
2249 parcel.writeInt(1);
2250 TextUtils.writeToParcel(tickerText, parcel, flags);
2251 } else {
2252 parcel.writeInt(0);
2253 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002254 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002255 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002256 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002257 } else {
2258 parcel.writeInt(0);
2259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (contentView != null) {
2261 parcel.writeInt(1);
2262 contentView.writeToParcel(parcel, 0);
2263 } else {
2264 parcel.writeInt(0);
2265 }
Selim Cinek279fa862016-06-14 10:57:25 -07002266 if (mLargeIcon == null && largeIcon != null) {
2267 // you snuck an icon in here without using the builder; let's try to keep it
2268 mLargeIcon = Icon.createWithBitmap(largeIcon);
2269 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002270 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002271 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002272 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002273 } else {
2274 parcel.writeInt(0);
2275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276
2277 parcel.writeInt(defaults);
2278 parcel.writeInt(this.flags);
2279
2280 if (sound != null) {
2281 parcel.writeInt(1);
2282 sound.writeToParcel(parcel, 0);
2283 } else {
2284 parcel.writeInt(0);
2285 }
2286 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002287
2288 if (audioAttributes != null) {
2289 parcel.writeInt(1);
2290 audioAttributes.writeToParcel(parcel, 0);
2291 } else {
2292 parcel.writeInt(0);
2293 }
2294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 parcel.writeLongArray(vibrate);
2296 parcel.writeInt(ledARGB);
2297 parcel.writeInt(ledOnMS);
2298 parcel.writeInt(ledOffMS);
2299 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002300
2301 if (fullScreenIntent != null) {
2302 parcel.writeInt(1);
2303 fullScreenIntent.writeToParcel(parcel, 0);
2304 } else {
2305 parcel.writeInt(0);
2306 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002307
2308 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002309
John Spurlockfd7f1e02014-03-18 16:41:57 -04002310 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002311
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002312 parcel.writeString(mGroupKey);
2313
2314 parcel.writeString(mSortKey);
2315
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002316 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002317
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002318 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002319
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002320 if (bigContentView != null) {
2321 parcel.writeInt(1);
2322 bigContentView.writeToParcel(parcel, 0);
2323 } else {
2324 parcel.writeInt(0);
2325 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002326
Chris Wren8fd39ec2014-02-27 17:43:26 -05002327 if (headsUpContentView != null) {
2328 parcel.writeInt(1);
2329 headsUpContentView.writeToParcel(parcel, 0);
2330 } else {
2331 parcel.writeInt(0);
2332 }
2333
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002334 parcel.writeInt(visibility);
2335
2336 if (publicVersion != null) {
2337 parcel.writeInt(1);
2338 publicVersion.writeToParcel(parcel, 0);
2339 } else {
2340 parcel.writeInt(0);
2341 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002342
2343 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002344
2345 if (mChannelId != null) {
2346 parcel.writeInt(1);
2347 parcel.writeString(mChannelId);
2348 } else {
2349 parcel.writeInt(0);
2350 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002351 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002352
2353 if (mShortcutId != null) {
2354 parcel.writeInt(1);
2355 parcel.writeString(mShortcutId);
2356 } else {
2357 parcel.writeInt(0);
2358 }
2359
2360 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002361
2362 if (mSettingsText != null) {
2363 parcel.writeInt(1);
2364 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2365 } else {
2366 parcel.writeInt(0);
2367 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002368
2369 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
2371
2372 /**
2373 * Parcelable.Creator that instantiates Notification objects
2374 */
2375 public static final Parcelable.Creator<Notification> CREATOR
2376 = new Parcelable.Creator<Notification>()
2377 {
2378 public Notification createFromParcel(Parcel parcel)
2379 {
2380 return new Notification(parcel);
2381 }
2382
2383 public Notification[] newArray(int size)
2384 {
2385 return new Notification[size];
2386 }
2387 };
2388
2389 /**
2390 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2391 * layout.
2392 *
2393 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2394 * in the view.</p>
2395 * @param context The context for your application / activity.
2396 * @param contentTitle The title that goes in the expanded entry.
2397 * @param contentText The text that goes in the expanded entry.
2398 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2399 * If this is an activity, it must include the
2400 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002401 * that you take care of task management as described in the
2402 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2403 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002404 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002405 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002406 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002408 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 public void setLatestEventInfo(Context context,
2410 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002411 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2412 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2413 new Throwable());
2414 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002415
Selim Cinek4ac6f602016-06-13 15:47:03 -07002416 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2417 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2418 }
2419
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002420 // ensure that any information already set directly is preserved
2421 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002422
2423 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002425 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
2427 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002428 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002430 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002431
2432 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 }
2434
Julia Reynoldsda303542015-11-23 14:00:20 -05002435 /**
2436 * @hide
2437 */
2438 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002439 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002440 }
2441
2442 /**
2443 * @hide
2444 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002445 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002446 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002447 }
2448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 @Override
2450 public String toString() {
2451 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002452 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002453 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002454 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002455 sb.append(priority);
2456 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002457 if (contentView != null) {
2458 sb.append(contentView.getPackage());
2459 sb.append("/0x");
2460 sb.append(Integer.toHexString(contentView.getLayoutId()));
2461 } else {
2462 sb.append("null");
2463 }
2464 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002465 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2466 sb.append("default");
2467 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 int N = this.vibrate.length-1;
2469 sb.append("[");
2470 for (int i=0; i<N; i++) {
2471 sb.append(this.vibrate[i]);
2472 sb.append(',');
2473 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002474 if (N != -1) {
2475 sb.append(this.vibrate[N]);
2476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 } else {
2479 sb.append("null");
2480 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002481 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002482 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002484 } else if (this.sound != null) {
2485 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 } else {
2487 sb.append("null");
2488 }
Chris Wren365b6d32015-07-16 10:39:26 -04002489 if (this.tickerText != null) {
2490 sb.append(" tick");
2491 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002492 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002494 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002495 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002496 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002497 if (this.category != null) {
2498 sb.append(" category=");
2499 sb.append(this.category);
2500 }
2501 if (this.mGroupKey != null) {
2502 sb.append(" groupKey=");
2503 sb.append(this.mGroupKey);
2504 }
2505 if (this.mSortKey != null) {
2506 sb.append(" sortKey=");
2507 sb.append(this.mSortKey);
2508 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002509 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002510 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002511 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002512 }
2513 sb.append(" vis=");
2514 sb.append(visibilityToString(this.visibility));
2515 if (this.publicVersion != null) {
2516 sb.append(" publicVersion=");
2517 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002518 }
2519 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 return sb.toString();
2521 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002522
Dan Sandler1b718782014-07-18 12:43:45 -04002523 /**
2524 * {@hide}
2525 */
2526 public static String visibilityToString(int vis) {
2527 switch (vis) {
2528 case VISIBILITY_PRIVATE:
2529 return "PRIVATE";
2530 case VISIBILITY_PUBLIC:
2531 return "PUBLIC";
2532 case VISIBILITY_SECRET:
2533 return "SECRET";
2534 default:
2535 return "UNKNOWN(" + String.valueOf(vis) + ")";
2536 }
2537 }
2538
Joe Onoratocb109a02011-01-18 17:57:41 -08002539 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002540 * {@hide}
2541 */
2542 public static String priorityToString(@Priority int pri) {
2543 switch (pri) {
2544 case PRIORITY_MIN:
2545 return "MIN";
2546 case PRIORITY_LOW:
2547 return "LOW";
2548 case PRIORITY_DEFAULT:
2549 return "DEFAULT";
2550 case PRIORITY_HIGH:
2551 return "HIGH";
2552 case PRIORITY_MAX:
2553 return "MAX";
2554 default:
2555 return "UNKNOWN(" + String.valueOf(pri) + ")";
2556 }
2557 }
2558
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002559 /**
2560 * @hide
2561 */
2562 public boolean hasCompletedProgress() {
2563 // not a progress notification; can't be complete
2564 if (!extras.containsKey(EXTRA_PROGRESS)
2565 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2566 return false;
2567 }
2568 // many apps use max 0 for 'indeterminate'; not complete
2569 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2570 return false;
2571 }
2572 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2573 }
2574
Jeff Sharkey000ce802017-04-29 13:13:27 -06002575 /** @removed */
2576 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002577 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002578 return mChannelId;
2579 }
2580
2581 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002582 * Returns the id of the channel this notification posts to.
2583 */
2584 public String getChannelId() {
2585 return mChannelId;
2586 }
2587
Jeff Sharkey000ce802017-04-29 13:13:27 -06002588 /** @removed */
2589 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002590 public long getTimeout() {
2591 return mTimeout;
2592 }
2593
2594 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002595 * Returns the duration from posting after which this notification should be canceled by the
2596 * system, if it's not canceled already.
2597 */
2598 public long getTimeoutAfter() {
2599 return mTimeout;
2600 }
2601
2602 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002603 * Returns what icon should be shown for this notification if it is being displayed in a
2604 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2605 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2606 */
2607 public int getBadgeIconType() {
2608 return mBadgeIcon;
2609 }
2610
2611 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002612 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002613 *
2614 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2615 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002616 */
2617 public String getShortcutId() {
2618 return mShortcutId;
2619 }
2620
Julia Reynolds3aedded2017-03-31 14:42:09 -04002621
2622 /**
2623 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2624 */
2625 public CharSequence getSettingsText() {
2626 return mSettingsText;
2627 }
2628
Julia Reynolds13d898c2017-02-02 12:22:05 -05002629 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002630 * Returns which type of notifications in a group are responsible for audibly alerting the
2631 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2632 * {@link #GROUP_ALERT_SUMMARY}.
2633 */
2634 public @GroupAlertBehavior int getGroupAlertBehavior() {
2635 return mGroupAlertBehavior;
2636 }
2637
2638 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002639 * The small icon representing this notification in the status bar and content view.
2640 *
2641 * @return the small icon representing this notification.
2642 *
2643 * @see Builder#getSmallIcon()
2644 * @see Builder#setSmallIcon(Icon)
2645 */
2646 public Icon getSmallIcon() {
2647 return mSmallIcon;
2648 }
2649
2650 /**
2651 * Used when notifying to clean up legacy small icons.
2652 * @hide
2653 */
2654 public void setSmallIcon(Icon icon) {
2655 mSmallIcon = icon;
2656 }
2657
2658 /**
2659 * The large icon shown in this notification's content view.
2660 * @see Builder#getLargeIcon()
2661 * @see Builder#setLargeIcon(Icon)
2662 */
2663 public Icon getLargeIcon() {
2664 return mLargeIcon;
2665 }
2666
2667 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002668 * @hide
2669 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002670 public boolean isGroupSummary() {
2671 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2672 }
2673
2674 /**
2675 * @hide
2676 */
2677 public boolean isGroupChild() {
2678 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2679 }
2680
2681 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002682 * @hide
2683 */
2684 public boolean suppressAlertingDueToGrouping() {
2685 if (isGroupSummary()
2686 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2687 return true;
2688 } else if (isGroupChild()
2689 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2690 return true;
2691 }
2692 return false;
2693 }
2694
2695 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002696 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002697 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002698 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002699 * content views using the platform's notification layout template. If your app supports
2700 * versions of Android as old as API level 4, you can instead use
2701 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2702 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2703 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002704 *
Scott Main183bf112012-08-13 19:12:13 -07002705 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002706 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002708 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002709 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2710 * .setContentText(subject)
2711 * .setSmallIcon(R.drawable.new_mail)
2712 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002713 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002714 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002715 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002716 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002717 /**
2718 * @hide
2719 */
2720 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2721 "android.rebuild.contentViewActionCount";
2722 /**
2723 * @hide
2724 */
2725 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2726 = "android.rebuild.bigViewActionCount";
2727 /**
2728 * @hide
2729 */
2730 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2731 = "android.rebuild.hudViewActionCount";
2732
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002733 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002734
Selim Cinek6743c0b2017-01-18 18:24:01 -08002735 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2736 SystemProperties.getBoolean("notifications.only_title", true);
2737
Selim Cinek389edcd2017-05-11 19:16:44 -07002738 /**
2739 * The lightness difference that has to be added to the primary text color to obtain the
2740 * secondary text color when the background is light.
2741 */
2742 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2743
2744 /**
2745 * The lightness difference that has to be added to the primary text color to obtain the
2746 * secondary text color when the background is dark.
2747 * A bit less then the above value, since it looks better on dark backgrounds.
2748 */
2749 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2750
Joe Onorato46439ce2010-11-19 13:56:21 -08002751 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002752 private Notification mN;
2753 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002754 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002755 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2756 private ArrayList<String> mPersonList = new ArrayList<String>();
2757 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002758 private boolean mIsLegacy;
2759 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002760
2761 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002762 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2763 */
2764 private int mCachedContrastColor = COLOR_INVALID;
2765 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002766 /**
2767 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2768 */
2769 private int mCachedAmbientColor = COLOR_INVALID;
2770 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002771
2772 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002773 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2774 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2775 */
2776 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002777 private int mTextColorsAreForBackground = COLOR_INVALID;
2778 private int mPrimaryTextColor = COLOR_INVALID;
2779 private int mSecondaryTextColor = COLOR_INVALID;
2780 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002781 private int mBackgroundColor = COLOR_INVALID;
2782 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002783 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002784 /**
2785 * A temporary location where actions are stored. If != null the view originally has action
2786 * but doesn't have any for this inflation.
2787 */
2788 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002789 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002790
Anthony Chenad4d1582017-04-10 16:07:58 -07002791 private boolean mTintActionButtons;
2792 private boolean mInNightMode;
2793
Adrian Roos70d7aa32017-01-11 15:39:06 -08002794 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002795 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002796 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002797 * @param context
2798 * A {@link Context} that will be used by the Builder to construct the
2799 * RemoteViews. The Context will not be held past the lifetime of this Builder
2800 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002801 * @param channelId
2802 * The constructed Notification will be posted on this
2803 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2804 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002805 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002806 public Builder(Context context, String channelId) {
2807 this(context, (Notification) null);
2808 mN.mChannelId = channelId;
2809 }
2810
2811 /**
2812 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2813 * instead. All posted Notifications must specify a NotificationChannel Id.
2814 */
2815 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002816 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002817 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 }
2819
Joe Onoratocb109a02011-01-18 17:57:41 -08002820 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002822 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002823 public Builder(Context context, Notification toAdopt) {
2824 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002825 Resources res = mContext.getResources();
2826 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2827
2828 if (res.getBoolean(R.bool.config_enableNightMode)) {
2829 Configuration currentConfig = res.getConfiguration();
2830 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2831 == Configuration.UI_MODE_NIGHT_YES;
2832 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002833
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002834 if (toAdopt == null) {
2835 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002836 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2837 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2838 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002839 mN.priority = PRIORITY_DEFAULT;
2840 mN.visibility = VISIBILITY_PRIVATE;
2841 } else {
2842 mN = toAdopt;
2843 if (mN.actions != null) {
2844 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002845 }
2846
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002847 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2848 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2849 }
2850
Selim Cinek4ac6f602016-06-13 15:47:03 -07002851 if (mN.getSmallIcon() == null && mN.icon != 0) {
2852 setSmallIcon(mN.icon);
2853 }
2854
2855 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2856 setLargeIcon(mN.largeIcon);
2857 }
2858
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002859 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2860 if (!TextUtils.isEmpty(templateClass)) {
2861 final Class<? extends Style> styleClass
2862 = getNotificationStyleClass(templateClass);
2863 if (styleClass == null) {
2864 Log.d(TAG, "Unknown style class: " + templateClass);
2865 } else {
2866 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002867 final Constructor<? extends Style> ctor =
2868 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002869 ctor.setAccessible(true);
2870 final Style style = ctor.newInstance();
2871 style.restoreFromExtras(mN.extras);
2872
2873 if (style != null) {
2874 setStyle(style);
2875 }
2876 } catch (Throwable t) {
2877 Log.e(TAG, "Could not create Style", t);
2878 }
2879 }
2880 }
2881
2882 }
2883 }
2884
2885 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002886 if (mColorUtil == null) {
2887 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002888 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002889 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002890 }
2891
2892 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002893 * If this notification is duplicative of a Launcher shortcut, sets the
2894 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2895 * the shortcut.
2896 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002897 * This field will be ignored by Launchers that don't support badging, don't show
2898 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002899 *
2900 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2901 * supersedes
2902 */
2903 public Builder setShortcutId(String shortcutId) {
2904 mN.mShortcutId = shortcutId;
2905 return this;
2906 }
2907
2908 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002909 * Sets which icon to display as a badge for this notification.
2910 *
2911 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2912 * {@link #BADGE_ICON_LARGE}.
2913 *
2914 * Note: This value might be ignored, for launchers that don't support badge icons.
2915 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002916 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002917 mN.mBadgeIcon = icon;
2918 return this;
2919 }
2920
2921 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002922 * Sets the group alert behavior for this notification. Use this method to mute this
2923 * notification if alerts for this notification's group should be handled by a different
2924 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04002925 * {@link #setGroup(String) group}. This must be called on all notifications you want to
2926 * mute. For example, if you want only the summary of your group to make noise, all
2927 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002928 *
2929 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2930 */
2931 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2932 mN.mGroupAlertBehavior = groupAlertBehavior;
2933 return this;
2934 }
2935
Jeff Sharkey000ce802017-04-29 13:13:27 -06002936 /** @removed */
2937 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002938 public Builder setChannel(String channelId) {
2939 mN.mChannelId = channelId;
2940 return this;
2941 }
2942
2943 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002944 * Specifies the channel the notification should be delivered on.
2945 */
2946 public Builder setChannelId(String channelId) {
2947 mN.mChannelId = channelId;
2948 return this;
2949 }
2950
Jeff Sharkey000ce802017-04-29 13:13:27 -06002951 /** @removed */
2952 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002953 public Builder setTimeout(long durationMs) {
2954 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002955 return this;
2956 }
2957
2958 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002959 * Specifies a duration in milliseconds after which this notification should be canceled,
2960 * if it is not already canceled.
2961 */
2962 public Builder setTimeoutAfter(long durationMs) {
2963 mN.mTimeout = durationMs;
2964 return this;
2965 }
2966
2967 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002968 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002969 *
2970 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2971 * shown anymore by default and must be opted into by using
2972 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002973 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002974 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002975 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002976 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002977 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002978 return this;
2979 }
2980
Joe Onoratocb109a02011-01-18 17:57:41 -08002981 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002982 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002983 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002984 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2985 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002986 */
2987 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002988 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002989 return this;
2990 }
2991
2992 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002993 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002994 *
2995 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002996 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002997 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002998 * Useful when showing an elapsed time (like an ongoing phone call).
2999 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003000 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003001 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003002 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003003 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003004 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003005 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003006 */
3007 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003008 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003009 return this;
3010 }
3011
3012 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003013 * Sets the Chronometer to count down instead of counting up.
3014 *
3015 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3016 * If it isn't set the chronometer will count up.
3017 *
3018 * @see #setUsesChronometer(boolean)
3019 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003020 public Builder setChronometerCountDown(boolean countDown) {
3021 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003022 return this;
3023 }
3024
3025 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003026 * Set the small icon resource, which will be used to represent the notification in the
3027 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003028 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003029
3030 * The platform template for the expanded view will draw this icon in the left, unless a
3031 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3032 * icon will be moved to the right-hand side.
3033 *
3034
3035 * @param icon
3036 * A resource ID in the application's package of the drawable to use.
3037 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003038 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003039 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003040 return setSmallIcon(icon != 0
3041 ? Icon.createWithResource(mContext, icon)
3042 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003043 }
3044
Joe Onoratocb109a02011-01-18 17:57:41 -08003045 /**
3046 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3047 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3048 * LevelListDrawable}.
3049 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003051 * @param level The level to use for the icon.
3052 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003053 * @see Notification#icon
3054 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003055 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003056 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003057 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003058 return setSmallIcon(icon);
3059 }
3060
3061 /**
3062 * Set the small icon, which will be used to represent the notification in the
3063 * status bar and content view (unless overriden there by a
3064 * {@link #setLargeIcon(Bitmap) large icon}).
3065 *
3066 * @param icon An Icon object to use.
3067 * @see Notification#icon
3068 */
3069 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003070 mN.setSmallIcon(icon);
3071 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3072 mN.icon = icon.getResId();
3073 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003074 return this;
3075 }
3076
Joe Onoratocb109a02011-01-18 17:57:41 -08003077 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003078 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003079 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003080 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003081 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003082 return this;
3083 }
3084
Joe Onoratocb109a02011-01-18 17:57:41 -08003085 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003086 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003087 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003088 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003089 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003090 return this;
3091 }
3092
Joe Onoratocb109a02011-01-18 17:57:41 -08003093 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003094 * This provides some additional information that is displayed in the notification. No
3095 * guarantees are given where exactly it is displayed.
3096 *
3097 * <p>This information should only be provided if it provides an essential
3098 * benefit to the understanding of the notification. The more text you provide the
3099 * less readable it becomes. For example, an email client should only provide the account
3100 * name here if more than one email account has been added.</p>
3101 *
3102 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3103 * notification header area.
3104 *
3105 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3106 * this will be shown in the third line of text in the platform notification template.
3107 * You should not be using {@link #setProgress(int, int, boolean)} at the
3108 * same time on those versions; they occupy the same place.
3109 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003110 */
3111 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003112 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003113 return this;
3114 }
3115
3116 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003117 * Provides text that will appear as a link to your application's settings.
3118 *
3119 * <p>This text does not appear within notification {@link Style templates} but may
3120 * appear when the user uses an affordance to learn more about the notification.
3121 * Additionally, this text will not appear unless you provide a valid link target by
3122 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3123 *
3124 * <p>This text is meant to be concise description about what the user can customize
3125 * when they click on this link. The recommended maximum length is 40 characters.
3126 * @param text
3127 * @return
3128 */
3129 public Builder setSettingsText(CharSequence text) {
3130 mN.mSettingsText = safeCharSequence(text);
3131 return this;
3132 }
3133
3134 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003135 * Set the remote input history.
3136 *
3137 * This should be set to the most recent inputs that have been sent
3138 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3139 * longer relevant (e.g. for chat notifications once the other party has responded).
3140 *
3141 * The most recent input must be stored at the 0 index, the second most recent at the
3142 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3143 * and how much of each individual input is shown.
3144 *
3145 * <p>Note: The reply text will only be shown on notifications that have least one action
3146 * with a {@code RemoteInput}.</p>
3147 */
3148 public Builder setRemoteInputHistory(CharSequence[] text) {
3149 if (text == null) {
3150 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3151 } else {
3152 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3153 CharSequence[] safe = new CharSequence[N];
3154 for (int i = 0; i < N; i++) {
3155 safe[i] = safeCharSequence(text[i]);
3156 }
3157 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3158 }
3159 return this;
3160 }
3161
3162 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003163 * Sets the number of items this notification represents. May be displayed as a badge count
3164 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003165 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003166 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003167 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003168 return this;
3169 }
3170
Joe Onoratocb109a02011-01-18 17:57:41 -08003171 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003172 * A small piece of additional information pertaining to this notification.
3173 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003174 * The platform template will draw this on the last line of the notification, at the far
3175 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003176 *
3177 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3178 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3179 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003180 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003181 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003182 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003183 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003184 return this;
3185 }
3186
Joe Onoratocb109a02011-01-18 17:57:41 -08003187 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003188 * Set the progress this notification represents.
3189 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003190 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003191 */
3192 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003193 mN.extras.putInt(EXTRA_PROGRESS, progress);
3194 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3195 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003196 return this;
3197 }
3198
3199 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003200 * Supply a custom RemoteViews to use instead of the platform template.
3201 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003202 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003203 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003204 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003205 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003206 return setCustomContentView(views);
3207 }
3208
3209 /**
3210 * Supply custom RemoteViews to use instead of the platform template.
3211 *
3212 * This will override the layout that would otherwise be constructed by this Builder
3213 * object.
3214 */
3215 public Builder setCustomContentView(RemoteViews contentView) {
3216 mN.contentView = contentView;
3217 return this;
3218 }
3219
3220 /**
3221 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3222 *
3223 * This will override the expanded layout that would otherwise be constructed by this
3224 * Builder object.
3225 */
3226 public Builder setCustomBigContentView(RemoteViews contentView) {
3227 mN.bigContentView = contentView;
3228 return this;
3229 }
3230
3231 /**
3232 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3233 *
3234 * This will override the heads-up layout that would otherwise be constructed by this
3235 * Builder object.
3236 */
3237 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3238 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003239 return this;
3240 }
3241
Joe Onoratocb109a02011-01-18 17:57:41 -08003242 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003243 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3244 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003245 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3246 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3247 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003248 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003249 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003250 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003251 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003252 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003253 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003254 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003255 return this;
3256 }
3257
Joe Onoratocb109a02011-01-18 17:57:41 -08003258 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003259 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3260 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003261 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003262 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003263 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003264 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003265 return this;
3266 }
3267
Joe Onoratocb109a02011-01-18 17:57:41 -08003268 /**
3269 * An intent to launch instead of posting the notification to the status bar.
3270 * Only for use with extremely high-priority notifications demanding the user's
3271 * <strong>immediate</strong> attention, such as an incoming phone call or
3272 * alarm clock that the user has explicitly set to a particular time.
3273 * If this facility is used for something else, please give the user an option
3274 * to turn it off and use a normal notification, as this can be extremely
3275 * disruptive.
3276 *
Chris Wren47c20a12014-06-18 17:27:29 -04003277 * <p>
3278 * The system UI may choose to display a heads-up notification, instead of
3279 * launching this intent, while the user is using the device.
3280 * </p>
3281 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003282 * @param intent The pending intent to launch.
3283 * @param highPriority Passing true will cause this notification to be sent
3284 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003285 *
3286 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003288 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003289 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003290 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3291 return this;
3292 }
3293
Joe Onoratocb109a02011-01-18 17:57:41 -08003294 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003295 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003296 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003297 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003299 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003300 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003301 return this;
3302 }
3303
Joe Onoratocb109a02011-01-18 17:57:41 -08003304 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003305 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003306 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003307 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003308 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003309 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003310 setTicker(tickerText);
3311 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003312 return this;
3313 }
3314
Joe Onoratocb109a02011-01-18 17:57:41 -08003315 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003316 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003317 *
3318 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003319 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3320 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003321 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003322 public Builder setLargeIcon(Bitmap b) {
3323 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3324 }
3325
3326 /**
3327 * Add a large icon to the notification content view.
3328 *
3329 * In the platform template, this image will be shown on the left of the notification view
3330 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3331 * badge atop the large icon).
3332 */
3333 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003334 mN.mLargeIcon = icon;
3335 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003336 return this;
3337 }
3338
Joe Onoratocb109a02011-01-18 17:57:41 -08003339 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003340 * Set the sound to play.
3341 *
John Spurlockc0650f022014-07-19 13:22:39 -04003342 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3343 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003344 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003345 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003346 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003347 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003348 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003349 mN.sound = sound;
3350 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003351 return this;
3352 }
3353
Joe Onoratocb109a02011-01-18 17:57:41 -08003354 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003355 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003356 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003357 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3358 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003359 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003360 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003361 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003362 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003363 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003364 mN.sound = sound;
3365 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003366 return this;
3367 }
3368
Joe Onoratocb109a02011-01-18 17:57:41 -08003369 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003370 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3371 * use during playback.
3372 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003373 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003374 * @see Notification#sound
3375 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003376 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003377 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003378 mN.sound = sound;
3379 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003380 return this;
3381 }
3382
3383 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003384 * Set the vibration pattern to use.
3385 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003386 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3387 * <code>pattern</code> parameter.
3388 *
Chris Wren47c20a12014-06-18 17:27:29 -04003389 * <p>
3390 * A notification that vibrates is more likely to be presented as a heads-up notification.
3391 * </p>
3392 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003393 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003394 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003395 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003396 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003397 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003398 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003399 return this;
3400 }
3401
Joe Onoratocb109a02011-01-18 17:57:41 -08003402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003403 * Set the desired color for the indicator LED on the device, as well as the
3404 * blink duty cycle (specified in milliseconds).
3405 *
3406
3407 * Not all devices will honor all (or even any) of these values.
3408 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003409 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003410 * @see Notification#ledARGB
3411 * @see Notification#ledOnMS
3412 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003413 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003414 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003415 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003416 mN.ledARGB = argb;
3417 mN.ledOnMS = onMs;
3418 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003419 if (onMs != 0 || offMs != 0) {
3420 mN.flags |= FLAG_SHOW_LIGHTS;
3421 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003422 return this;
3423 }
3424
Joe Onoratocb109a02011-01-18 17:57:41 -08003425 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003426 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003427 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003428
3429 * Ongoing notifications cannot be dismissed by the user, so your application or service
3430 * must take care of canceling them.
3431 *
3432
3433 * They are typically used to indicate a background task that the user is actively engaged
3434 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3435 * (e.g., a file download, sync operation, active network connection).
3436 *
3437
3438 * @see Notification#FLAG_ONGOING_EVENT
3439 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003440 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 public Builder setOngoing(boolean ongoing) {
3442 setFlag(FLAG_ONGOING_EVENT, ongoing);
3443 return this;
3444 }
3445
Joe Onoratocb109a02011-01-18 17:57:41 -08003446 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003447 * Set whether this notification should be colorized. When set, the color set with
3448 * {@link #setColor(int)} will be used as the background color of this notification.
3449 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003450 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3451 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003452 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003453 * For most styles, the coloring will only be applied if the notification is for a
3454 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003455 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003456 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003457 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003458 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003459 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003460 */
3461 public Builder setColorized(boolean colorize) {
3462 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3463 return this;
3464 }
3465
3466 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 * Set this flag if you would only like the sound, vibrate
3468 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 *
3470 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3473 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003478 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003479 *
3480 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003481 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003482 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003483 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003484 return this;
3485 }
3486
Joe Onoratocb109a02011-01-18 17:57:41 -08003487 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003488 * Set whether or not this notification should not bridge to other devices.
3489 *
3490 * <p>Some notifications can be bridged to other devices for remote display.
3491 * This hint can be set to recommend this notification not be bridged.
3492 */
3493 public Builder setLocalOnly(boolean localOnly) {
3494 setFlag(FLAG_LOCAL_ONLY, localOnly);
3495 return this;
3496 }
3497
3498 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003499 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 * <p>
3501 * The value should be one or more of the following fields combined with
3502 * bitwise-or:
3503 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3504 * <p>
3505 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003506 *
3507 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003508 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003509 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003510 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003511 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003512 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003513 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003514 return this;
3515 }
3516
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003517 /**
3518 * Set the priority of this notification.
3519 *
3520 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003521 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003522 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003523 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003524 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003525 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 return this;
3527 }
Joe Malin8d40d042012-11-05 11:36:40 -08003528
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003529 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003530 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003531 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003532 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003533 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003534 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003535 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003536 return this;
3537 }
3538
3539 /**
Chris Wrendde75302014-03-26 17:24:15 -04003540 * Add a person that is relevant to this notification.
3541 *
Chris Wrene6c48932014-09-29 17:19:27 -04003542 * <P>
3543 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003544 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3545 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3546 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003547 * </P>
3548 *
3549 * <P>
3550 * The person should be specified by the {@code String} representation of a
3551 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3552 * </P>
3553 *
3554 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3555 * URIs. The path part of these URIs must exist in the contacts database, in the
3556 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3557 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3558 * </P>
3559 *
3560 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003561 * @see Notification#EXTRA_PEOPLE
3562 */
Chris Wrene6c48932014-09-29 17:19:27 -04003563 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003564 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003565 return this;
3566 }
3567
3568 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003569 * Set this notification to be part of a group of notifications sharing the same key.
3570 * Grouped notifications may display in a cluster or stack on devices which
3571 * support such rendering.
3572 *
3573 * <p>To make this notification the summary for its group, also call
3574 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3575 * {@link #setSortKey}.
3576 * @param groupKey The group key of the group.
3577 * @return this object for method chaining
3578 */
3579 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003580 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003581 return this;
3582 }
3583
3584 /**
3585 * Set this notification to be the group summary for a group of notifications.
3586 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003587 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3588 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003589 * @param isGroupSummary Whether this notification should be a group summary.
3590 * @return this object for method chaining
3591 */
3592 public Builder setGroupSummary(boolean isGroupSummary) {
3593 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3594 return this;
3595 }
3596
3597 /**
3598 * Set a sort key that orders this notification among other notifications from the
3599 * same package. This can be useful if an external sort was already applied and an app
3600 * would like to preserve this. Notifications will be sorted lexicographically using this
3601 * value, although providing different priorities in addition to providing sort key may
3602 * cause this value to be ignored.
3603 *
3604 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003605 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003606 *
3607 * @see String#compareTo(String)
3608 */
3609 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003610 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003611 return this;
3612 }
3613
3614 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003615 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 *
Griff Hazen720042b2014-02-24 15:46:56 -08003617 * <p>Values within the Bundle will replace existing extras values in this Builder.
3618 *
3619 * @see Notification#extras
3620 */
Griff Hazen959591e2014-05-15 22:26:18 -07003621 public Builder addExtras(Bundle extras) {
3622 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003624 }
3625 return this;
3626 }
3627
3628 /**
3629 * Set metadata for this notification.
3630 *
3631 * <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 -04003632 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003633 * called.
3634 *
Griff Hazen720042b2014-02-24 15:46:56 -08003635 * <p>Replaces any existing extras values with those from the provided Bundle.
3636 * Use {@link #addExtras} to merge in metadata instead.
3637 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003638 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003639 */
Griff Hazen959591e2014-05-15 22:26:18 -07003640 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003641 if (extras != null) {
3642 mUserExtras = extras;
3643 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003644 return this;
3645 }
3646
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003647 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003648 * Get the current metadata Bundle used by this notification Builder.
3649 *
3650 * <p>The returned Bundle is shared with this Builder.
3651 *
3652 * <p>The current contents of this Bundle are copied into the Notification each time
3653 * {@link #build()} is called.
3654 *
3655 * @see Notification#extras
3656 */
3657 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003658 return mUserExtras;
3659 }
3660
3661 private Bundle getAllExtras() {
3662 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3663 saveExtras.putAll(mN.extras);
3664 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003665 }
3666
3667 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003668 * Add an action to this notification. Actions are typically displayed by
3669 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003670 * <p>
3671 * Every action must have an icon (32dp square and matching the
3672 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3673 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3674 * <p>
3675 * A notification in its expanded form can display up to 3 actions, from left to right in
3676 * the order they were added. Actions will not be displayed when the notification is
3677 * collapsed, however, so be sure that any essential functions may be accessed by the user
3678 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003679 *
3680 * @param icon Resource ID of a drawable that represents the action.
3681 * @param title Text describing the action.
3682 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003683 *
3684 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003685 */
Dan Sandler86647982015-05-13 23:41:13 -04003686 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003687 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003688 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003689 return this;
3690 }
3691
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003692 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003693 * Add an action to this notification. Actions are typically displayed by
3694 * the system as a button adjacent to the notification content.
3695 * <p>
3696 * Every action must have an icon (32dp square and matching the
3697 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3698 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3699 * <p>
3700 * A notification in its expanded form can display up to 3 actions, from left to right in
3701 * the order they were added. Actions will not be displayed when the notification is
3702 * collapsed, however, so be sure that any essential functions may be accessed by the user
3703 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3704 *
3705 * @param action The action to add.
3706 */
3707 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003708 if (action != null) {
3709 mActions.add(action);
3710 }
Griff Hazen959591e2014-05-15 22:26:18 -07003711 return this;
3712 }
3713
3714 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003715 * Alter the complete list of actions attached to this notification.
3716 * @see #addAction(Action).
3717 *
3718 * @param actions
3719 * @return
3720 */
3721 public Builder setActions(Action... actions) {
3722 mActions.clear();
3723 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003724 if (actions[i] != null) {
3725 mActions.add(actions[i]);
3726 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003727 }
3728 return this;
3729 }
3730
3731 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003732 * Add a rich notification style to be applied at build time.
3733 *
3734 * @param style Object responsible for modifying the notification style.
3735 */
3736 public Builder setStyle(Style style) {
3737 if (mStyle != style) {
3738 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003739 if (mStyle != null) {
3740 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3742 } else {
3743 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003744 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003745 }
3746 return this;
3747 }
3748
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003749 /**
3750 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003751 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003752 * @return The same Builder.
3753 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003754 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003756 return this;
3757 }
3758
3759 /**
3760 * Supply a replacement Notification whose contents should be shown in insecure contexts
3761 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3762 * @param n A replacement notification, presumably with some or all info redacted.
3763 * @return The same Builder.
3764 */
3765 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003766 if (n != null) {
3767 mN.publicVersion = new Notification();
3768 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3769 } else {
3770 mN.publicVersion = null;
3771 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003772 return this;
3773 }
3774
Griff Hazenb720abe2014-05-20 13:15:30 -07003775 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003776 * Apply an extender to this notification builder. Extenders may be used to add
3777 * metadata or change options on this builder.
3778 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003779 public Builder extend(Extender extender) {
3780 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003781 return this;
3782 }
3783
Dan Sandler4e787062015-06-17 15:09:48 -04003784 /**
3785 * @hide
3786 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003787 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003788 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003789 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003790 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003791 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003792 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003793 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003794 }
3795
Dan Sandler26e81cf2014-05-06 10:01:27 -04003796 /**
3797 * Sets {@link Notification#color}.
3798 *
3799 * @param argb The accent color to use
3800 *
3801 * @return The same Builder.
3802 */
Tor Norbye80756e32015-03-02 09:39:27 -08003803 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003804 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003805 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003806 return this;
3807 }
3808
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003809 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003810 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3811 // This user can never be a badged profile,
3812 // and also includes USER_ALL system notifications.
3813 return null;
3814 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003815 // Note: This assumes that the current user can read the profile badge of the
3816 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003817 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003818 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003819 }
3820
3821 private Bitmap getProfileBadge() {
3822 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003823 if (badge == null) {
3824 return null;
3825 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003826 final int size = mContext.getResources().getDimensionPixelSize(
3827 R.dimen.notification_badge_size);
3828 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003829 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003830 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003831 badge.draw(canvas);
3832 return bitmap;
3833 }
3834
Selim Cinekc848c3a2016-01-13 15:27:30 -08003835 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003836 Bitmap profileBadge = getProfileBadge();
3837
Kenny Guy98193ea2014-07-24 19:54:37 +01003838 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003839 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3840 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003841 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003842 contentView.setDrawableTint(R.id.profile_badge, false,
3843 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003844 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003845 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003846 }
3847
Christoph Studerfe718432014-09-01 18:21:18 +02003848 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003849 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003850 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003851 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003852 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003853 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003854 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003855 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003856 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003857 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003858 }
3859
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003860 /**
3861 * Resets the notification header to its original state
3862 */
3863 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003864 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3865 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003866 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003867 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003868 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003869 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003870 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003871 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003872 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003873 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003874 contentView.setImageViewIcon(R.id.profile_badge, null);
3875 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003876 }
3877
3878 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003879 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3880 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003881 }
3882
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003883 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003884 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003885 }
3886
3887 /**
3888 * @param hasProgress whether the progress bar should be shown and set
3889 */
3890 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003891 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3892 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003893 }
3894
Adrian Roos70d7aa32017-01-11 15:39:06 -08003895 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003896 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003897
Christoph Studerfe718432014-09-01 18:21:18 +02003898 resetStandardTemplate(contentView);
3899
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003900 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003901 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003902 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003903 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003904 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3905 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003906 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07003907 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08003908 if (!p.ambient) {
3909 setTextViewColorPrimary(contentView, R.id.title);
3910 }
Selim Cinek954cc232016-05-20 13:29:23 -07003911 contentView.setViewLayoutWidth(R.id.title, showProgress
3912 ? ViewGroup.LayoutParams.WRAP_CONTENT
3913 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003914 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003915 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003916 int textId = showProgress ? com.android.internal.R.id.text_line_1
3917 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07003918 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08003919 if (!p.ambient) {
3920 setTextViewColorSecondary(contentView, textId);
3921 }
Selim Cinek41598732016-01-11 16:58:37 -08003922 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003923 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003924
Selim Cinek279fa862016-06-14 10:57:25 -07003925 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003926
Selim Cinek29603462015-11-17 19:04:39 -08003927 return contentView;
3928 }
3929
Selim Cinek48f66b72017-08-18 16:17:51 -07003930 private CharSequence processTextSpans(CharSequence text) {
3931 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07003932 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07003933 }
3934 return text;
3935 }
3936
Selim Cinek7b9605b2017-01-19 17:36:00 -08003937 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3938 ensureColors();
3939 contentView.setTextColor(id, mPrimaryTextColor);
3940 }
3941
Selim Cinek48f66b72017-08-18 16:17:51 -07003942 private boolean hasForegroundColor() {
3943 return mForegroundColor != COLOR_INVALID;
3944 }
3945
Selim Cinek389edcd2017-05-11 19:16:44 -07003946 /**
3947 * @return the primary text color
3948 * @hide
3949 */
3950 @VisibleForTesting
3951 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003952 ensureColors();
3953 return mPrimaryTextColor;
3954 }
3955
Selim Cinek389edcd2017-05-11 19:16:44 -07003956 /**
3957 * @return the secondary text color
3958 * @hide
3959 */
3960 @VisibleForTesting
3961 public int getSecondaryTextColor() {
3962 ensureColors();
3963 return mSecondaryTextColor;
3964 }
3965
Selim Cinek7b9605b2017-01-19 17:36:00 -08003966 private int getActionBarColor() {
3967 ensureColors();
3968 return mActionBarColor;
3969 }
3970
Selim Cinek622c64a2017-04-17 17:10:05 -07003971 private int getActionBarColorDeEmphasized() {
3972 int backgroundColor = getBackgroundColor();
3973 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3974 }
3975
Selim Cinek7b9605b2017-01-19 17:36:00 -08003976 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3977 ensureColors();
3978 contentView.setTextColor(id, mSecondaryTextColor);
3979 }
3980
3981 private void ensureColors() {
3982 int backgroundColor = getBackgroundColor();
3983 if (mPrimaryTextColor == COLOR_INVALID
3984 || mSecondaryTextColor == COLOR_INVALID
3985 || mActionBarColor == COLOR_INVALID
3986 || mTextColorsAreForBackground != backgroundColor) {
3987 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07003988 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003989 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3990 backgroundColor);
3991 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3992 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003993 if (backgroundColor != COLOR_DEFAULT
3994 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3995 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3996 mPrimaryTextColor, backgroundColor, 4.5);
3997 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3998 mSecondaryTextColor, backgroundColor, 4.5);
3999 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004000 } else {
4001 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4002 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4003 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4004 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004005 // We only respect the given colors if worst case Black or White still has
4006 // contrast
4007 boolean backgroundLight = backLum > textLum
4008 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4009 || backLum <= textLum
4010 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004011 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004012 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004013 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4014 mForegroundColor,
4015 backgroundColor,
4016 true /* findFG */,
4017 4.5f);
4018 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004019 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004020 } else {
4021 mSecondaryTextColor =
4022 NotificationColorUtil.findContrastColorAgainstDark(
4023 mForegroundColor,
4024 backgroundColor,
4025 true /* findFG */,
4026 4.5f);
4027 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004028 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004029 }
4030 } else {
4031 mPrimaryTextColor = mForegroundColor;
4032 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004033 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4034 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004035 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4036 backgroundColor) < 4.5f) {
4037 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004038 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004039 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4040 mSecondaryTextColor,
4041 backgroundColor,
4042 true /* findFG */,
4043 4.5f);
4044 } else {
4045 mSecondaryTextColor
4046 = NotificationColorUtil.findContrastColorAgainstDark(
4047 mSecondaryTextColor,
4048 backgroundColor,
4049 true /* findFG */,
4050 4.5f);
4051 }
4052 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004053 mSecondaryTextColor, backgroundLight
4054 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4055 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004056 }
4057 }
4058 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004059 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4060 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004061 }
4062 }
4063
4064 private void updateBackgroundColor(RemoteViews contentView) {
4065 if (isColorized()) {
4066 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4067 getBackgroundColor());
4068 } else {
4069 // Clear it!
4070 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4071 0);
4072 }
4073 }
4074
Selim Cinek860b6da2015-12-16 19:02:19 -08004075 /**
4076 * @param remoteView the remote view to update the minheight in
4077 * @param hasMinHeight does it have a mimHeight
4078 * @hide
4079 */
4080 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4081 int minHeight = 0;
4082 if (hasMinHeight) {
4083 // we need to set the minHeight of the notification
4084 minHeight = mContext.getResources().getDimensionPixelSize(
4085 com.android.internal.R.dimen.notification_min_content_height);
4086 }
4087 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4088 }
4089
Selim Cinek29603462015-11-17 19:04:39 -08004090 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004091 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4092 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4093 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4094 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004095 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004096 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004097 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004098 contentView.setProgressBackgroundTintList(
4099 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4100 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004101 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004102 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004103 contentView.setProgressTintList(R.id.progress, colorStateList);
4104 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004105 }
Selim Cinek29603462015-11-17 19:04:39 -08004106 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004107 } else {
4108 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004109 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004110 }
Joe Onorato561d3852010-11-20 18:09:34 -08004111 }
4112
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004113 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07004114 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4115 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4116 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004117 if (mN.mLargeIcon != null) {
4118 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4119 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4120 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004121 int endMargin = R.dimen.notification_content_picture_margin;
4122 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4123 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4124 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004125 // Bind the reply action
4126 Action action = findReplyAction();
4127 contentView.setViewVisibility(R.id.reply_icon_action, action != null
4128 ? View.VISIBLE
4129 : View.GONE);
4130
4131 if (action != null) {
4132 int contrastColor = resolveContrastColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004133 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004134 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004135 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004136 int iconColor = NotificationColorUtil.isColorLight(contrastColor)
4137 ? Color.BLACK : Color.WHITE;
Sunny Goyal5b153922017-09-21 21:00:36 -07004138 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004139 false /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004140 iconColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004141 contentView.setOnClickPendingIntent(R.id.right_icon,
4142 action.actionIntent);
4143 contentView.setOnClickPendingIntent(R.id.reply_icon_action,
4144 action.actionIntent);
4145 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
4146 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4147
4148 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004149 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004150 contentView.setViewVisibility(R.id.right_icon_container, mN.mLargeIcon != null
4151 ? View.VISIBLE
4152 : View.GONE);
4153 }
4154
4155 private Action findReplyAction() {
4156 ArrayList<Action> actions = mActions;
4157 if (mOriginalActions != null) {
4158 actions = mOriginalActions;
4159 }
4160 int numActions = actions.size();
4161 for (int i = 0; i < numActions; i++) {
4162 Action action = actions.get(i);
4163 if (hasValidRemoteInput(action)) {
4164 return action;
4165 }
4166 }
4167 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004168 }
4169
Adrian Roos487374f2017-01-11 15:48:14 -08004170 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4171 bindSmallIcon(contentView, ambient);
4172 bindHeaderAppName(contentView, ambient);
4173 if (!ambient) {
4174 // Ambient view does not have these
4175 bindHeaderText(contentView);
4176 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004177 bindProfileBadge(contentView);
4178 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004179 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004180 }
4181
4182 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004183 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004184 contentView.setDrawableTint(R.id.expand_button, false, color,
4185 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004186 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004187 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004188 }
4189
Selim Cinek99104832017-01-25 14:47:33 -08004190 /**
4191 * @return the color that is used as the first primary highlight color. This is applied
4192 * in several places like the action buttons or the app name in the header.
4193 */
4194 private int getPrimaryHighlightColor() {
4195 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4196 }
4197
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004198 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4199 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004200 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004201 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004202 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4203 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4204 contentView.setLong(R.id.chronometer, "setBase",
4205 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4206 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004207 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004208 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004209 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004210 } else {
4211 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4212 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004213 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004214 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004215 } else {
4216 // We still want a time to be set but gone, such that we can show and hide it
4217 // on demand in case it's a child notification without anything in the header
4218 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004219 }
4220 }
4221
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004222 private void bindHeaderText(RemoteViews contentView) {
4223 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4224 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004225 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004226 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004227 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004228 if (headerText == null
4229 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4230 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4231 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4232 }
4233 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004234 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004235 contentView.setTextViewText(R.id.header_text, processTextSpans(
4236 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004237 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004238 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4239 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004240 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004241 }
4242 }
4243
Adrian Rooseba05822016-04-22 17:09:27 -07004244 /**
4245 * @hide
4246 */
4247 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004248 CharSequence name = null;
4249 final PackageManager pm = mContext.getPackageManager();
4250 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4251 // only system packages which lump together a bunch of unrelated stuff
4252 // may substitute a different name to make the purpose of the
4253 // notification more clear. the correct package label should always
4254 // be accessible via SystemUI.
4255 final String pkg = mContext.getPackageName();
4256 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4257 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4258 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4259 name = subName;
4260 } else {
4261 Log.w(TAG, "warning: pkg "
4262 + pkg + " attempting to substitute app name '" + subName
4263 + "' without holding perm "
4264 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4265 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004266 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004267 if (TextUtils.isEmpty(name)) {
4268 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4269 }
4270 if (TextUtils.isEmpty(name)) {
4271 // still nothing?
4272 return null;
4273 }
4274
4275 return String.valueOf(name);
4276 }
Adrian Roos487374f2017-01-11 15:48:14 -08004277 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004278 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004279 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004280 setTextViewColorPrimary(contentView, R.id.app_name_text);
4281 } else {
4282 contentView.setTextColor(R.id.app_name_text,
4283 ambient ? resolveAmbientColor() : resolveContrastColor());
4284 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004285 }
4286
Adrian Roos487374f2017-01-11 15:48:14 -08004287 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004288 if (mN.mSmallIcon == null && mN.icon != 0) {
4289 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4290 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004291 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004292 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004293 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004294 }
4295
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004296 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004297 * @return true if the built notification will show the time or the chronometer; false
4298 * otherwise
4299 */
4300 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004301 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004302 }
4303
Christoph Studerfe718432014-09-01 18:21:18 +02004304 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004305 // actions_container is only reset when there are no actions to avoid focus issues with
4306 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004307 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004308 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004309
4310 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4311 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4312
4313 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4314 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4315 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4316 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004317
4318 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004319 }
4320
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004321 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004322 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004323 }
4324
Adrian Roos70d7aa32017-01-11 15:39:06 -08004325 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4326 StandardTemplateParams p) {
4327 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004328
Christoph Studerfe718432014-09-01 18:21:18 +02004329 resetStandardTemplateWithActions(big);
4330
Adrian Roose458aa82015-12-08 16:17:19 -08004331 boolean validRemoteInput = false;
4332
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004333 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004334 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004335 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004336 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004337 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004338 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004339 if (p.ambient) {
4340 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004341 } else if (isColorized()) {
4342 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4343 } else {
4344 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4345 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004346 }
Adrian Roosf852a422016-06-03 13:33:43 -07004347 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4348 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004349 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004350 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004351 Action action = mActions.get(i);
4352 validRemoteInput |= hasValidRemoteInput(action);
4353
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004354 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004355 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004356 big.addView(R.id.actions, button);
4357 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004358 } else {
4359 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004360 }
Adrian Roose458aa82015-12-08 16:17:19 -08004361
4362 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004363 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004364 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4365 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004366 big.setTextViewText(R.id.notification_material_reply_text_1,
4367 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004368 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004369
4370 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4371 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004372 big.setTextViewText(R.id.notification_material_reply_text_2,
4373 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004374 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004375
4376 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4377 big.setViewVisibility(
4378 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004379 big.setTextViewText(R.id.notification_material_reply_text_3,
4380 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004381 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004382 }
4383 }
4384 }
4385
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004386 return big;
4387 }
4388
Adrian Roose458aa82015-12-08 16:17:19 -08004389 private boolean hasValidRemoteInput(Action action) {
4390 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4391 // Weird actions
4392 return false;
4393 }
4394
4395 RemoteInput[] remoteInputs = action.getRemoteInputs();
4396 if (remoteInputs == null) {
4397 return false;
4398 }
4399
4400 for (RemoteInput r : remoteInputs) {
4401 CharSequence[] choices = r.getChoices();
4402 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4403 return true;
4404 }
4405 }
4406 return false;
4407 }
4408
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004409 /**
4410 * Construct a RemoteViews for the final 1U notification layout. In order:
4411 * 1. Custom contentView from the caller
4412 * 2. Style's proposed content view
4413 * 3. Standard template view
4414 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004415 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004416 return createContentView(false /* increasedheight */ );
4417 }
4418
4419 /**
4420 * Construct a RemoteViews for the smaller content view.
4421 *
4422 * @param increasedHeight true if this layout be created with an increased height. Some
4423 * styles may support showing more then just that basic 1U size
4424 * and the system may decide to render important notifications
4425 * slightly bigger even when collapsed.
4426 *
4427 * @hide
4428 */
4429 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004430 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004431 return mN.contentView;
4432 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004433 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004434 if (styleView != null) {
4435 return styleView;
4436 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004437 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004438 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004439 }
4440
Selim Cineka7679b62017-05-10 16:33:25 -07004441 private boolean useExistingRemoteView() {
4442 return mStyle == null || (!mStyle.displayCustomViewInline()
4443 && !mRebuildStyledRemoteViews);
4444 }
4445
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004446 /**
4447 * Construct a RemoteViews for the final big notification layout.
4448 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004449 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004450 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004451 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004452 return mN.bigContentView;
4453 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004454 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004455 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004456 } else if (mActions.size() != 0) {
4457 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004458 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004459 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004460 return result;
4461 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004462
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004463 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004464 * Construct a RemoteViews for the final notification header only. This will not be
4465 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004466 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004467 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004468 * @hide
4469 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004470 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004471 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4472 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004473 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004474 ambient ? R.layout.notification_template_ambient_header
4475 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004476 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004477 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004478 if (colorized != null) {
4479 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4480 } else {
4481 mN.extras.remove(EXTRA_COLORIZED);
4482 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004483 return header;
4484 }
4485
Adrian Roos487374f2017-01-11 15:48:14 -08004486 /**
4487 * Construct a RemoteViews for the ambient version of the notification.
4488 *
4489 * @hide
4490 */
4491 public RemoteViews makeAmbientNotification() {
4492 RemoteViews ambient = applyStandardTemplateWithActions(
4493 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004494 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004495 return ambient;
4496 }
4497
Selim Cinek29603462015-11-17 19:04:39 -08004498 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004499 if (result != null) {
4500 result.setViewVisibility(R.id.text_line_1, View.GONE);
4501 }
Selim Cinek29603462015-11-17 19:04:39 -08004502 }
4503
Selim Cinek6743c0b2017-01-18 18:24:01 -08004504 /**
4505 * Adapt the Notification header if this view is used as an expanded view.
4506 *
4507 * @hide
4508 */
4509 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004510 if (result != null) {
4511 result.setBoolean(R.id.notification_header, "setExpanded", true);
4512 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004513 }
4514
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004515 /**
4516 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004517 *
4518 * @param increasedHeight true if this layout be created with an increased height. Some
4519 * styles may support showing more then just that basic 1U size
4520 * and the system may decide to render important notifications
4521 * slightly bigger even when collapsed.
4522 *
4523 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004524 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004525 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004526 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004527 return mN.headsUpContentView;
4528 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004529 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4530 if (styleView != null) {
4531 return styleView;
4532 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004533 } else if (mActions.size() == 0) {
4534 return null;
4535 }
4536
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004537 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004538 }
4539
Selim Cinek624c02db2015-12-14 21:00:02 -08004540 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004541 * Construct a RemoteViews for the final heads-up notification layout.
4542 */
4543 public RemoteViews createHeadsUpContentView() {
4544 return createHeadsUpContentView(false /* useIncreasedHeight */);
4545 }
4546
4547 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004548 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4549 *
4550 * @hide
4551 */
4552 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004553 return makePublicView(false /* ambient */);
4554 }
4555
4556 /**
4557 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4558 *
4559 * @hide
4560 */
4561 public RemoteViews makePublicAmbientNotification() {
4562 return makePublicView(true /* ambient */);
4563 }
4564
4565 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004566 if (mN.publicVersion != null) {
4567 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004568 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004569 }
4570 Bundle savedBundle = mN.extras;
4571 Style style = mStyle;
4572 mStyle = null;
4573 Icon largeIcon = mN.mLargeIcon;
4574 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004575 Bitmap largeIconLegacy = mN.largeIcon;
4576 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004577 ArrayList<Action> actions = mActions;
4578 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004579 Bundle publicExtras = new Bundle();
4580 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4581 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4582 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4583 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004584 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4585 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004586 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004587 RemoteViews view;
4588 if (ambient) {
4589 publicExtras.putCharSequence(EXTRA_TITLE,
4590 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4591 view = makeAmbientNotification();
4592 } else{
4593 view = makeNotificationHeader(false /* ambient */);
4594 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4595 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004596 mN.extras = savedBundle;
4597 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004598 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004599 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004600 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004601 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004602 }
4603
Selim Cinek6743c0b2017-01-18 18:24:01 -08004604 /**
4605 * Construct a content view for the display when low - priority
4606 *
4607 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4608 * a new subtext is created consisting of the content of the
4609 * notification.
4610 * @hide
4611 */
4612 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4613 int color = mN.color;
4614 mN.color = COLOR_DEFAULT;
4615 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4616 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4617 CharSequence newSummary = createSummaryText();
4618 if (!TextUtils.isEmpty(newSummary)) {
4619 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4620 }
4621 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004622
Adrian Roos6f6e1592017-05-02 16:22:53 -07004623 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004624 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004625 if (summary != null) {
4626 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4627 } else {
4628 mN.extras.remove(EXTRA_SUB_TEXT);
4629 }
4630 mN.color = color;
4631 return header;
4632 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004633
Selim Cinek6743c0b2017-01-18 18:24:01 -08004634 private CharSequence createSummaryText() {
4635 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4636 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4637 return titleText;
4638 }
4639 SpannableStringBuilder summary = new SpannableStringBuilder();
4640 if (titleText == null) {
4641 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4642 }
4643 BidiFormatter bidi = BidiFormatter.getInstance();
4644 if (titleText != null) {
4645 summary.append(bidi.unicodeWrap(titleText));
4646 }
4647 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4648 if (titleText != null && contentText != null) {
4649 summary.append(bidi.unicodeWrap(mContext.getText(
4650 R.string.notification_header_divider_symbol_with_spaces)));
4651 }
4652 if (contentText != null) {
4653 summary.append(bidi.unicodeWrap(contentText));
4654 }
4655 return summary;
4656 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004657
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004658 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004659 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004660 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004661 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004662 emphazisedMode ? getEmphasizedActionLayoutResource()
4663 : tombstone ? getActionTombstoneLayoutResource()
4664 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004665 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004666 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004667 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004668 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004669 if (action.mRemoteInputs != null) {
4670 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4671 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004672 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004673 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004674 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004675 int bgColor;
4676 if (isColorized()) {
4677 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4678 } else {
4679 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4680 : R.color.notification_action_list_dark);
4681 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004682 button.setDrawableTint(R.id.button_holder, true,
4683 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004684 CharSequence title = action.title;
4685 ColorStateList[] outResultColor = null;
4686 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004687 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004688 } else {
4689 outResultColor = new ColorStateList[1];
4690 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4691 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004692 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004693 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004694 if (outResultColor != null && outResultColor[0] != null) {
4695 // We need to set the text color as well since changing a text to uppercase
4696 // clears its spans.
4697 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004698 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004699 button.setTextColor(R.id.action0,resolveContrastColor());
4700 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004701 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004702 button.setTextViewText(R.id.action0, processTextSpans(
4703 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004704 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004705 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004706 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004707 button.setTextColor(R.id.action0,
4708 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004709 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004710 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004711 return button;
4712 }
4713
Joe Onoratocb109a02011-01-18 17:57:41 -08004714 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004715 * Ensures contrast on color spans against a background color. also returns the color of the
4716 * text if a span was found that spans over the whole text.
4717 *
4718 * @param charSequence the charSequence on which the spans are
4719 * @param background the background color to ensure the contrast against
4720 * @param outResultColor an array in which a color will be returned as the first element if
4721 * there exists a full length color span.
4722 * @return the contrasted charSequence
4723 */
4724 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4725 ColorStateList[] outResultColor) {
4726 if (charSequence instanceof Spanned) {
4727 Spanned ss = (Spanned) charSequence;
4728 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4729 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4730 for (Object span : spans) {
4731 Object resultSpan = span;
4732 int spanStart = ss.getSpanStart(span);
4733 int spanEnd = ss.getSpanEnd(span);
4734 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4735 if (resultSpan instanceof CharacterStyle) {
4736 resultSpan = ((CharacterStyle) span).getUnderlying();
4737 }
4738 if (resultSpan instanceof TextAppearanceSpan) {
4739 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4740 ColorStateList textColor = originalSpan.getTextColor();
4741 if (textColor != null) {
4742 int[] colors = textColor.getColors();
4743 int[] newColors = new int[colors.length];
4744 for (int i = 0; i < newColors.length; i++) {
4745 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004746 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004747 }
4748 textColor = new ColorStateList(textColor.getStates().clone(),
4749 newColors);
4750 resultSpan = new TextAppearanceSpan(
4751 originalSpan.getFamily(),
4752 originalSpan.getTextStyle(),
4753 originalSpan.getTextSize(),
4754 textColor,
4755 originalSpan.getLinkTextColor());
4756 if (fullLength) {
4757 outResultColor[0] = new ColorStateList(
4758 textColor.getStates().clone(), newColors);
4759 }
4760 }
4761 } else if (resultSpan instanceof ForegroundColorSpan) {
4762 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4763 int foregroundColor = originalSpan.getForegroundColor();
4764 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004765 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004766 resultSpan = new ForegroundColorSpan(foregroundColor);
4767 if (fullLength) {
4768 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4769 }
4770 } else {
4771 resultSpan = span;
4772 }
4773
4774 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4775 }
4776 return builder;
4777 }
4778 return charSequence;
4779 }
4780
4781 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004782 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004783 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004784 */
4785 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004786 if (!mIsLegacyInitialized) {
4787 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4788 < Build.VERSION_CODES.LOLLIPOP;
4789 mIsLegacyInitialized = true;
4790 }
4791 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004792 }
4793
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004794 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004795 return processLegacyText(charSequence, false /* ambient */);
4796 }
4797
4798 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4799 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4800 boolean wantLightText = ambient;
4801 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004802 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004803 } else {
4804 return charSequence;
4805 }
4806 }
4807
Dan Sandler26e81cf2014-05-06 10:01:27 -04004808 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004809 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004810 */
Adrian Roos487374f2017-01-11 15:48:14 -08004811 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4812 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004813 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004814 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004815 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004816 contentView.setDrawableTint(R.id.icon, false, color,
4817 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004818
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004819 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004820 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004821 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004822 }
4823
Dan Sandler26e81cf2014-05-06 10:01:27 -04004824 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004825 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004826 * if it's grayscale).
4827 */
4828 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004829 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4830 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004831 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004832 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004833 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4834 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004835 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004836 }
4837
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004838 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004839 if (mN.color != COLOR_DEFAULT) {
4840 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004841 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004842 }
4843
Adrian Roos4ff3b122016-02-01 12:26:13 -08004844 int resolveContrastColor() {
4845 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4846 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004847 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004848
Selim Cinekac5f0272017-05-02 16:05:41 -07004849 int color;
4850 int background = mBackgroundColorHint;
4851 if (mBackgroundColorHint == COLOR_INVALID) {
4852 background = mContext.getColor(
4853 com.android.internal.R.color.notification_material_background_color);
4854 }
4855 if (mN.color == COLOR_DEFAULT) {
4856 ensureColors();
4857 color = mSecondaryTextColor;
4858 } else {
4859 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004860 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004861 }
4862 if (Color.alpha(color) < 255) {
4863 // alpha doesn't go well for color filters, so let's blend it manually
4864 color = NotificationColorUtil.compositeColors(color, background);
4865 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004866 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004867 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004868 }
4869
Adrian Roos487374f2017-01-11 15:48:14 -08004870 int resolveAmbientColor() {
4871 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4872 return mCachedAmbientColor;
4873 }
4874 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4875
4876 mCachedAmbientColorIsFor = mN.color;
4877 return mCachedAmbientColor = contrasted;
4878 }
4879
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004880 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004881 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004882 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004883 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004884 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004885 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004886 mN.actions = new Action[mActions.size()];
4887 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004888 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004889 if (!mPersonList.isEmpty()) {
4890 mN.extras.putStringArray(EXTRA_PEOPLE,
4891 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004892 }
Selim Cinek247fa012016-02-18 09:50:48 -08004893 if (mN.bigContentView != null || mN.contentView != null
4894 || mN.headsUpContentView != null) {
4895 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4896 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004897 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004898 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004899
Julia Reynolds3b848122016-02-26 10:45:32 -05004900 /**
4901 * Creates a Builder from an existing notification so further changes can be made.
4902 * @param context The context for your application / activity.
4903 * @param n The notification to create a Builder from.
4904 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004905 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004906 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004907 ApplicationInfo applicationInfo = n.extras.getParcelable(
4908 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004909 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004910 if (applicationInfo != null) {
4911 try {
4912 builderContext = context.createApplicationContext(applicationInfo,
4913 Context.CONTEXT_RESTRICTED);
4914 } catch (NameNotFoundException e) {
4915 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4916 builderContext = context; // try with our context
4917 }
4918 } else {
4919 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004920 }
4921
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004922 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004923 }
4924
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004925 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004926 * @deprecated Use {@link #build()} instead.
4927 */
4928 @Deprecated
4929 public Notification getNotification() {
4930 return build();
4931 }
4932
4933 /**
4934 * Combine all of the options that have been set and return a new {@link Notification}
4935 * object.
4936 */
4937 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004938 // first, add any extras from the calling code
4939 if (mUserExtras != null) {
4940 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004941 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004942
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004943 mN.creationTime = System.currentTimeMillis();
4944
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004945 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004946 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004947
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004948 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004949
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004950 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02004951 mStyle.reduceImageSizes(mContext);
4952 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004953 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004954 }
Selim Cinekd0426622017-07-11 13:19:59 +02004955 mN.reduceImageSizes(mContext);
4956
Adrian Roos5081c0d2016-02-26 16:04:19 -08004957 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004958 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004959 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004960 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004961 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4962 mN.contentView.getSequenceNumber());
4963 }
4964 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004965 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004966 if (mN.bigContentView != null) {
4967 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4968 mN.bigContentView.getSequenceNumber());
4969 }
4970 }
4971 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004972 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004973 if (mN.headsUpContentView != null) {
4974 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4975 mN.headsUpContentView.getSequenceNumber());
4976 }
4977 }
4978 }
4979
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004980 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4981 mN.flags |= FLAG_SHOW_LIGHTS;
4982 }
4983
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004984 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004985 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004986
4987 /**
4988 * Apply this Builder to an existing {@link Notification} object.
4989 *
4990 * @hide
4991 */
4992 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004993 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004994 return n;
4995 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004996
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004997 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004998 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04004999 * change. Also removes extenders on low ram devices, as
5000 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005001 *
5002 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5003 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005004 * @hide
5005 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005006 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005007 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005008
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005009 // Only strip views for known Styles because we won't know how to
5010 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005011 if (!isLowRam
5012 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005013 && getNotificationStyleClass(templateClass) == null) {
5014 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005015 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005016
5017 // Only strip unmodified BuilderRemoteViews.
5018 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005019 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005020 n.contentView.getSequenceNumber();
5021 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005022 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005023 n.bigContentView.getSequenceNumber();
5024 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005025 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005026 n.headsUpContentView.getSequenceNumber();
5027
5028 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005029 if (!isLowRam
5030 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005031 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005032 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005033
5034 Notification clone = n.clone();
5035 if (stripContentView) {
5036 clone.contentView = null;
5037 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5038 }
5039 if (stripBigContentView) {
5040 clone.bigContentView = null;
5041 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5042 }
5043 if (stripHeadsUpContentView) {
5044 clone.headsUpContentView = null;
5045 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5046 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005047 if (isLowRam) {
5048 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5049 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5050 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5051 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005052 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005053 }
5054
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005055 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005056 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005057 }
5058
5059 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005060 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005061 }
5062
5063 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005064 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005065 }
5066
5067 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005068 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005069 }
5070
5071 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005072 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005073 }
5074
Adrian Roosc1a80b02016-04-05 14:54:55 -07005075 private int getMessagingLayoutResource() {
5076 return R.layout.notification_template_material_messaging;
5077 }
5078
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005079 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005080 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005081 }
5082
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005083 private int getEmphasizedActionLayoutResource() {
5084 return R.layout.notification_material_action_emphasized;
5085 }
5086
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005087 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005088 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005089 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005090
5091 private int getBackgroundColor() {
5092 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005093 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005094 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005095 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5096 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005097 }
5098 }
5099
5100 private boolean isColorized() {
5101 return mN.isColorized();
5102 }
Selim Cinek99104832017-01-25 14:47:33 -08005103
Anthony Chenad4d1582017-04-10 16:07:58 -07005104 private boolean shouldTintActionButtons() {
5105 return mTintActionButtons;
5106 }
5107
Selim Cinek99104832017-01-25 14:47:33 -08005108 private boolean textColorsNeedInversion() {
5109 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5110 return false;
5111 }
5112 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5113 return targetSdkVersion > Build.VERSION_CODES.M
5114 && targetSdkVersion < Build.VERSION_CODES.O;
5115 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005116
5117 /**
5118 * Set a color palette to be used as the background and textColors
5119 *
5120 * @param backgroundColor the color to be used as the background
5121 * @param foregroundColor the color to be used as the foreground
5122 *
5123 * @hide
5124 */
5125 public void setColorPalette(int backgroundColor, int foregroundColor) {
5126 mBackgroundColor = backgroundColor;
5127 mForegroundColor = foregroundColor;
5128 mTextColorsAreForBackground = COLOR_INVALID;
5129 ensureColors();
5130 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005131
5132 /**
5133 * Sets the background color for this notification to be a different one then the default.
5134 * This is mainly used to calculate contrast and won't necessarily be applied to the
5135 * background.
5136 *
5137 * @hide
5138 */
5139 public void setBackgroundColorHint(int backgroundColor) {
5140 mBackgroundColorHint = backgroundColor;
5141 }
Selim Cineka7679b62017-05-10 16:33:25 -07005142
5143
5144 /**
5145 * Forces all styled remoteViews to be built from scratch and not use any cached
5146 * RemoteViews.
5147 * This is needed for legacy apps that are baking in their remoteviews into the
5148 * notification.
5149 *
5150 * @hide
5151 */
5152 public void setRebuildStyledRemoteViews(boolean rebuild) {
5153 mRebuildStyledRemoteViews = rebuild;
5154 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005155 }
5156
5157 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005158 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5159 * remote views.
5160 *
5161 * @hide
5162 */
5163 void reduceImageSizes(Context context) {
5164 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5165 return;
5166 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005167 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005168 if (mLargeIcon != null || largeIcon != null) {
5169 Resources resources = context.getResources();
5170 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005171 int maxWidth = resources.getDimensionPixelSize(isLowRam
5172 ? R.dimen.notification_right_icon_size_low_ram
5173 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005174 int maxHeight = maxWidth;
5175 if (MediaStyle.class.equals(style)
5176 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005177 maxHeight = resources.getDimensionPixelSize(isLowRam
5178 ? R.dimen.notification_media_image_max_height_low_ram
5179 : R.dimen.notification_media_image_max_height);
5180 maxWidth = resources.getDimensionPixelSize(isLowRam
5181 ? R.dimen.notification_media_image_max_width_low_ram
5182 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005183 }
5184 if (mLargeIcon != null) {
5185 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5186 }
5187 if (largeIcon != null) {
5188 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5189 }
5190 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005191 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5192 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5193 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005194 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5195 }
5196
Selim Cineka8cb1262017-08-15 16:53:44 -07005197 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5198 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005199 if (remoteView != null) {
5200 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005201 int maxWidth = resources.getDimensionPixelSize(isLowRam
5202 ? R.dimen.notification_custom_view_max_image_width_low_ram
5203 : R.dimen.notification_custom_view_max_image_width);
5204 int maxHeight = resources.getDimensionPixelSize(isLowRam
5205 ? R.dimen.notification_custom_view_max_image_height_low_ram
5206 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005207 remoteView.reduceImageSizes(maxWidth, maxHeight);
5208 }
5209 }
5210
5211 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005212 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005213 */
Selim Cinek22714f12017-04-13 16:23:53 -07005214 private boolean isForegroundService() {
5215 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005216 }
5217
5218 /**
Selim Cinek99104832017-01-25 14:47:33 -08005219 * @return whether this notification has a media session attached
5220 * @hide
5221 */
5222 public boolean hasMediaSession() {
5223 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5224 }
5225
5226 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005227 * @return the style class of this notification
5228 * @hide
5229 */
5230 public Class<? extends Notification.Style> getNotificationStyle() {
5231 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5232
5233 if (!TextUtils.isEmpty(templateClass)) {
5234 return Notification.getNotificationStyleClass(templateClass);
5235 }
5236 return null;
5237 }
5238
5239 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005240 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005241 *
5242 * @hide
5243 */
5244 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005245 if (isColorizedMedia()) {
5246 return true;
5247 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005248 return extras.getBoolean(EXTRA_COLORIZED)
5249 && (hasColorizedPermission() || isForegroundService());
5250 }
5251
5252 /**
5253 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5254 * permission. The permission is checked when a notification is enqueued.
5255 */
5256 private boolean hasColorizedPermission() {
5257 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005258 }
5259
5260 /**
5261 * @return true if this notification is colorized and it is a media notification
5262 *
5263 * @hide
5264 */
5265 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005266 Class<? extends Style> style = getNotificationStyle();
5267 if (MediaStyle.class.equals(style)) {
5268 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5269 if ((colorized == null || colorized) && hasMediaSession()) {
5270 return true;
5271 }
5272 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5273 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5274 return true;
5275 }
5276 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005277 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005278 }
5279
Selim Cinek0847acd2017-04-24 19:48:29 -07005280
5281 /**
5282 * @return true if this is a media notification
5283 *
5284 * @hide
5285 */
5286 public boolean isMediaNotification() {
5287 Class<? extends Style> style = getNotificationStyle();
5288 if (MediaStyle.class.equals(style)) {
5289 return true;
5290 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5291 return true;
5292 }
5293 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005294 }
5295
Selim Cinek279fa862016-06-14 10:57:25 -07005296 private boolean hasLargeIcon() {
5297 return mLargeIcon != null || largeIcon != null;
5298 }
5299
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005300 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005301 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005302 * @hide
5303 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005304 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005305 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5306 }
5307
5308 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005309 * @return true if the notification will show a chronometer; false otherwise
5310 * @hide
5311 */
5312 public boolean showsChronometer() {
5313 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5314 }
5315
5316 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005317 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005318 */
5319 @SystemApi
5320 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5321 Class<? extends Style>[] classes = new Class[] {
5322 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5323 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5324 MessagingStyle.class };
5325 for (Class<? extends Style> innerClass : classes) {
5326 if (templateClass.equals(innerClass.getName())) {
5327 return innerClass;
5328 }
5329 }
5330 return null;
5331 }
5332
5333 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005334 * An object that can apply a rich notification style to a {@link Notification.Builder}
5335 * object.
5336 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005337 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005338 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005339
5340 /**
5341 * @hide
5342 */
5343 protected CharSequence mSummaryText = null;
5344
5345 /**
5346 * @hide
5347 */
5348 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005349
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005350 protected Builder mBuilder;
5351
Chris Wrend6297db2012-05-03 16:20:13 -04005352 /**
5353 * Overrides ContentTitle in the big form of the template.
5354 * This defaults to the value passed to setContentTitle().
5355 */
5356 protected void internalSetBigContentTitle(CharSequence title) {
5357 mBigContentTitle = title;
5358 }
5359
5360 /**
5361 * Set the first line of text after the detail section in the big form of the template.
5362 */
5363 protected void internalSetSummaryText(CharSequence cs) {
5364 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005365 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005366 }
5367
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005368 public void setBuilder(Builder builder) {
5369 if (mBuilder != builder) {
5370 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005371 if (mBuilder != null) {
5372 mBuilder.setStyle(this);
5373 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005374 }
5375 }
5376
Chris Wrend6297db2012-05-03 16:20:13 -04005377 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005378 if (mBuilder == null) {
5379 throw new IllegalArgumentException("Style requires a valid Builder object");
5380 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005381 }
Chris Wrend6297db2012-05-03 16:20:13 -04005382
5383 protected RemoteViews getStandardView(int layoutId) {
5384 checkBuilder();
5385
Christoph Studer4600f9b2014-07-22 22:44:43 +02005386 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005387 CharSequence oldBuilderContentTitle =
5388 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005389 if (mBigContentTitle != null) {
5390 mBuilder.setContentTitle(mBigContentTitle);
5391 }
5392
Chris Wrend6297db2012-05-03 16:20:13 -04005393 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5394
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005395 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005396
Chris Wrend6297db2012-05-03 16:20:13 -04005397 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5398 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005399 } else {
5400 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005401 }
5402
Chris Wrend6297db2012-05-03 16:20:13 -04005403 return contentView;
5404 }
5405
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005406 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005407 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005408 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005409 *
5410 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005411 * @hide
5412 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005413 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005414 return null;
5415 }
5416
5417 /**
5418 * Construct a Style-specific RemoteViews for the final big notification layout.
5419 * @hide
5420 */
5421 public RemoteViews makeBigContentView() {
5422 return null;
5423 }
5424
5425 /**
5426 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005427 *
5428 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005429 * @hide
5430 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005431 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005432 return null;
5433 }
5434
5435 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005436 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005437 * @hide
5438 */
5439 public void addExtras(Bundle extras) {
5440 if (mSummaryTextSet) {
5441 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5442 }
5443 if (mBigContentTitle != null) {
5444 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5445 }
Chris Wren91ad5632013-06-05 15:05:57 -04005446 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005447 }
5448
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005449 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005450 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005451 * @hide
5452 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005453 protected void restoreFromExtras(Bundle extras) {
5454 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5455 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5456 mSummaryTextSet = true;
5457 }
5458 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5459 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5460 }
5461 }
5462
5463
5464 /**
5465 * @hide
5466 */
5467 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005468 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005469 return wip;
5470 }
5471
Daniel Sandler0ec46202015-06-24 01:27:05 -04005472 /**
5473 * @hide
5474 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005475 public void purgeResources() {}
5476
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005477 /**
5478 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5479 * attached to.
5480 *
5481 * @return the fully constructed Notification.
5482 */
5483 public Notification build() {
5484 checkBuilder();
5485 return mBuilder.build();
5486 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005487
5488 /**
5489 * @hide
5490 * @return true if the style positions the progress bar on the second line; false if the
5491 * style hides the progress bar
5492 */
5493 protected boolean hasProgress() {
5494 return true;
5495 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005496
5497 /**
5498 * @hide
5499 * @return Whether we should put the summary be put into the notification header
5500 */
5501 public boolean hasSummaryInHeader() {
5502 return true;
5503 }
Selim Cinek593610c2016-02-16 18:42:57 -08005504
5505 /**
5506 * @hide
5507 * @return Whether custom content views are displayed inline in the style
5508 */
5509 public boolean displayCustomViewInline() {
5510 return false;
5511 }
Selim Cinekd0426622017-07-11 13:19:59 +02005512
5513 /**
5514 * Reduces the image sizes contained in this style.
5515 *
5516 * @hide
5517 */
5518 public void reduceImageSizes(Context context) {
5519 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005520 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005521
5522 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005523 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005524 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005525 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005526 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005527 * Notification notif = new Notification.Builder(mContext)
5528 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5529 * .setContentText(subject)
5530 * .setSmallIcon(R.drawable.new_post)
5531 * .setLargeIcon(aBitmap)
5532 * .setStyle(new Notification.BigPictureStyle()
5533 * .bigPicture(aBigBitmap))
5534 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005535 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005536 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005537 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005538 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005539 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005540 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005541 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005542 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005543
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005544 public BigPictureStyle() {
5545 }
5546
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005547 /**
5548 * @deprecated use {@code BigPictureStyle()}.
5549 */
5550 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005551 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005552 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005553 }
5554
Chris Wrend6297db2012-05-03 16:20:13 -04005555 /**
5556 * Overrides ContentTitle in the big form of the template.
5557 * This defaults to the value passed to setContentTitle().
5558 */
5559 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005560 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005561 return this;
5562 }
5563
5564 /**
5565 * Set the first line of text after the detail section in the big form of the template.
5566 */
5567 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005568 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005569 return this;
5570 }
5571
Chris Wren0bd664d2012-08-01 13:56:56 -04005572 /**
5573 * Provide the bitmap to be used as the payload for the BigPicture notification.
5574 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005575 public BigPictureStyle bigPicture(Bitmap b) {
5576 mPicture = b;
5577 return this;
5578 }
5579
Chris Wren3745a3d2012-05-22 15:11:52 -04005580 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005581 * Override the large icon when the big notification is shown.
5582 */
5583 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005584 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5585 }
5586
5587 /**
5588 * Override the large icon when the big notification is shown.
5589 */
5590 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005591 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005592 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005593 return this;
5594 }
5595
Riley Andrews0394a0c2015-11-03 23:36:52 -08005596 /** @hide */
5597 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5598
Daniel Sandler0ec46202015-06-24 01:27:05 -04005599 /**
5600 * @hide
5601 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005602 @Override
5603 public void purgeResources() {
5604 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005605 if (mPicture != null &&
5606 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005607 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005608 mPicture = mPicture.createAshmemBitmap();
5609 }
5610 if (mBigLargeIcon != null) {
5611 mBigLargeIcon.convertToAshmem();
5612 }
5613 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005614
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005615 /**
5616 * @hide
5617 */
Selim Cinekd0426622017-07-11 13:19:59 +02005618 @Override
5619 public void reduceImageSizes(Context context) {
5620 super.reduceImageSizes(context);
5621 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005622 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005623 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005624 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5625 ? R.dimen.notification_big_picture_max_height_low_ram
5626 : R.dimen.notification_big_picture_max_height);
5627 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5628 ? R.dimen.notification_big_picture_max_width_low_ram
5629 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005630 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5631 }
5632 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005633 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5634 ? R.dimen.notification_right_icon_size_low_ram
5635 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005636 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5637 }
5638 }
5639
5640 /**
5641 * @hide
5642 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005643 public RemoteViews makeBigContentView() {
5644 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005645 // This covers the following cases:
5646 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005647 // mN.mLargeIcon
5648 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005649 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005650 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005651 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005652 oldLargeIcon = mBuilder.mN.mLargeIcon;
5653 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005654 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5655 // replacement if the other one is null. Because we're restoring these legacy icons
5656 // for old listeners, this is in general non-null.
5657 largeIconLegacy = mBuilder.mN.largeIcon;
5658 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005659 }
5660
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005661 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005662 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005663 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5664 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005665 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005666 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005667 }
Selim Cinek279fa862016-06-14 10:57:25 -07005668 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005669
Christoph Studer5c510ee2014-12-15 16:32:27 +01005670 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005671 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005672 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005673 }
5674
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005675 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005676 return contentView;
5677 }
5678
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005679 /**
5680 * @hide
5681 */
5682 public void addExtras(Bundle extras) {
5683 super.addExtras(extras);
5684
5685 if (mBigLargeIconSet) {
5686 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5687 }
5688 extras.putParcelable(EXTRA_PICTURE, mPicture);
5689 }
5690
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005691 /**
5692 * @hide
5693 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005694 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005695 protected void restoreFromExtras(Bundle extras) {
5696 super.restoreFromExtras(extras);
5697
5698 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005699 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005700 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005701 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005702 mPicture = extras.getParcelable(EXTRA_PICTURE);
5703 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005704
5705 /**
5706 * @hide
5707 */
5708 @Override
5709 public boolean hasSummaryInHeader() {
5710 return false;
5711 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005712 }
5713
5714 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005715 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005716 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005717 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005718 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005719 * Notification notif = new Notification.Builder(mContext)
5720 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5721 * .setContentText(subject)
5722 * .setSmallIcon(R.drawable.new_mail)
5723 * .setLargeIcon(aBitmap)
5724 * .setStyle(new Notification.BigTextStyle()
5725 * .bigText(aVeryLongString))
5726 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005727 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005728 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005729 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005730 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005731 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005732
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005733 private CharSequence mBigText;
5734
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005735 public BigTextStyle() {
5736 }
5737
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005738 /**
5739 * @deprecated use {@code BigTextStyle()}.
5740 */
5741 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005742 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005743 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005744 }
5745
Chris Wrend6297db2012-05-03 16:20:13 -04005746 /**
5747 * Overrides ContentTitle in the big form of the template.
5748 * This defaults to the value passed to setContentTitle().
5749 */
5750 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005751 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005752 return this;
5753 }
5754
5755 /**
5756 * Set the first line of text after the detail section in the big form of the template.
5757 */
5758 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005759 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005760 return this;
5761 }
5762
Chris Wren0bd664d2012-08-01 13:56:56 -04005763 /**
5764 * Provide the longer text to be displayed in the big form of the
5765 * template in place of the content text.
5766 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005767 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005768 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005769 return this;
5770 }
5771
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005772 /**
5773 * @hide
5774 */
5775 public void addExtras(Bundle extras) {
5776 super.addExtras(extras);
5777
Christoph Studer4600f9b2014-07-22 22:44:43 +02005778 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5779 }
5780
5781 /**
5782 * @hide
5783 */
5784 @Override
5785 protected void restoreFromExtras(Bundle extras) {
5786 super.restoreFromExtras(extras);
5787
5788 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005789 }
5790
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005791 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005792 * @param increasedHeight true if this layout be created with an increased height.
5793 *
5794 * @hide
5795 */
5796 @Override
5797 public RemoteViews makeContentView(boolean increasedHeight) {
5798 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005799 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005800 mBuilder.mActions = new ArrayList<>();
5801 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005802 mBuilder.mActions = mBuilder.mOriginalActions;
5803 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005804 return remoteViews;
5805 }
5806 return super.makeContentView(increasedHeight);
5807 }
5808
5809 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005810 * @hide
5811 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005812 @Override
5813 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5814 if (increasedHeight && mBuilder.mActions.size() > 0) {
5815 return makeBigContentView();
5816 }
5817 return super.makeHeadsUpContentView(increasedHeight);
5818 }
5819
5820 /**
5821 * @hide
5822 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005823 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005824
5825 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005826 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005827 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005828
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005829 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005830
Selim Cinek75998782016-04-26 10:39:17 -07005831 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005832
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005833 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005834 if (TextUtils.isEmpty(bigTextText)) {
5835 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5836 // experience
5837 bigTextText = mBuilder.processLegacyText(text);
5838 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005839 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005840
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005841 return contentView;
5842 }
5843
Adrian Roosb1f427c2016-05-26 12:27:15 -07005844 static void applyBigTextContentView(Builder builder,
5845 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005846 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005847 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005848 contentView.setViewVisibility(R.id.big_text,
5849 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005850 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005851 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005852 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005853
5854 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005855 * Helper class for generating large-format notifications that include multiple back-and-forth
5856 * messages of varying types between any number of people.
5857 *
5858 * <br>
5859 * If the platform does not provide large-format notifications, this method has no effect. The
5860 * user will always see the normal notification view.
5861 * <br>
5862 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5863 * so:
5864 * <pre class="prettyprint">
5865 *
5866 * Notification noti = new Notification.Builder()
5867 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5868 * .setContentText(subject)
5869 * .setSmallIcon(R.drawable.new_message)
5870 * .setLargeIcon(aBitmap)
5871 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5872 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5873 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5874 * .build();
5875 * </pre>
5876 */
5877 public static class MessagingStyle extends Style {
5878
5879 /**
5880 * The maximum number of messages that will be retained in the Notification itself (the
5881 * number displayed is up to the platform).
5882 */
5883 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5884
5885 CharSequence mUserDisplayName;
5886 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005887 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005888 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005889
5890 MessagingStyle() {
5891 }
5892
5893 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005894 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5895 * user before the posting app reposts the notification with those messages after they've
5896 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005897 * {@link #addMessage(Notification.MessagingStyle.Message)}
5898 */
Alex Hillsfd590442016-10-07 09:52:44 -04005899 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005900 mUserDisplayName = userDisplayName;
5901 }
5902
5903 /**
5904 * Returns the name to be displayed for any replies sent by the user
5905 */
5906 public CharSequence getUserDisplayName() {
5907 return mUserDisplayName;
5908 }
5909
5910 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005911 * Sets the title to be displayed on this conversation. This should only be used for
5912 * group messaging and left unset for one-on-one conversations.
5913 * @param conversationTitle
5914 * @return this object for method chaining.
5915 */
5916 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5917 mConversationTitle = conversationTitle;
5918 return this;
5919 }
5920
5921 /**
5922 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5923 * should be for one-on-one conversations
5924 */
5925 public CharSequence getConversationTitle() {
5926 return mConversationTitle;
5927 }
5928
5929 /**
5930 * Adds a message for display by this notification. Convenience call for a simple
5931 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5932 * @param text A {@link CharSequence} to be displayed as the message content
5933 * @param timestamp Time at which the message arrived
5934 * @param sender A {@link CharSequence} to be used for displaying the name of the
5935 * sender. Should be <code>null</code> for messages by the current user, in which case
5936 * the platform will insert {@link #getUserDisplayName()}.
5937 * Should be unique amongst all individuals in the conversation, and should be
5938 * consistent during re-posts of the notification.
5939 *
5940 * @see Message#Message(CharSequence, long, CharSequence)
5941 *
5942 * @return this object for method chaining
5943 */
5944 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005945 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005946 }
5947
5948 /**
5949 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005950 *
5951 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5952 * the newest last.
5953 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005954 * @param message The {@link Message} to be displayed
5955 * @return this object for method chaining
5956 */
5957 public MessagingStyle addMessage(Message message) {
5958 mMessages.add(message);
5959 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5960 mMessages.remove(0);
5961 }
5962 return this;
5963 }
5964
5965 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005966 * Adds a {@link Message} for historic context in this notification.
5967 *
5968 * <p>Messages should be added as historic if they are not the main subject of the
5969 * notification but may give context to a conversation. The system may choose to present
5970 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5971 *
5972 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5973 * the newest last.
5974 *
5975 * @param message The historic {@link Message} to be added
5976 * @return this object for method chaining
5977 */
5978 public MessagingStyle addHistoricMessage(Message message) {
5979 mHistoricMessages.add(message);
5980 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5981 mHistoricMessages.remove(0);
5982 }
5983 return this;
5984 }
5985
5986 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005987 * Gets the list of {@code Message} objects that represent the notification
5988 */
5989 public List<Message> getMessages() {
5990 return mMessages;
5991 }
5992
5993 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005994 * Gets the list of historic {@code Message}s in the notification.
5995 */
5996 public List<Message> getHistoricMessages() {
5997 return mHistoricMessages;
5998 }
5999
6000 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006001 * @hide
6002 */
6003 @Override
6004 public void addExtras(Bundle extras) {
6005 super.addExtras(extras);
6006 if (mUserDisplayName != null) {
6007 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6008 }
6009 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006010 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006011 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006012 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6013 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006014 }
Adrian Roos437cd562017-01-18 15:47:03 -08006015 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6016 Message.getBundleArrayForMessages(mHistoricMessages));
6017 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006018
6019 fixTitleAndTextExtras(extras);
6020 }
6021
6022 private void fixTitleAndTextExtras(Bundle extras) {
6023 Message m = findLatestIncomingMessage();
6024 CharSequence text = (m == null) ? null : m.mText;
6025 CharSequence sender = m == null ? null
6026 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6027 CharSequence title;
6028 if (!TextUtils.isEmpty(mConversationTitle)) {
6029 if (!TextUtils.isEmpty(sender)) {
6030 BidiFormatter bidi = BidiFormatter.getInstance();
6031 title = mBuilder.mContext.getString(
6032 com.android.internal.R.string.notification_messaging_title_template,
6033 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6034 } else {
6035 title = mConversationTitle;
6036 }
6037 } else {
6038 title = sender;
6039 }
6040
6041 if (title != null) {
6042 extras.putCharSequence(EXTRA_TITLE, title);
6043 }
6044 if (text != null) {
6045 extras.putCharSequence(EXTRA_TEXT, text);
6046 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006047 }
6048
6049 /**
6050 * @hide
6051 */
6052 @Override
6053 protected void restoreFromExtras(Bundle extras) {
6054 super.restoreFromExtras(extras);
6055
6056 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08006057 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07006058 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6059 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006060 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
6061 if (messages != null && messages instanceof Parcelable[]) {
6062 mMessages = Message.getMessagesFromBundleArray(messages);
6063 }
6064 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
6065 if (histMessages != null && histMessages instanceof Parcelable[]) {
6066 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04006067 }
6068 }
6069
6070 /**
6071 * @hide
6072 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006073 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006074 public RemoteViews makeContentView(boolean increasedHeight) {
6075 if (!increasedHeight) {
6076 Message m = findLatestIncomingMessage();
6077 CharSequence title = mConversationTitle != null
6078 ? mConversationTitle
6079 : (m == null) ? null : m.mSender;
6080 CharSequence text = (m == null)
6081 ? null
6082 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04006083
Selim Cinek7d1009b2017-01-25 15:28:28 -08006084 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
6085 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
6086 } else {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006087 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006088 mBuilder.mActions = new ArrayList<>();
6089 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006090 mBuilder.mActions = mBuilder.mOriginalActions;
6091 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006092 return remoteViews;
6093 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006094 }
6095
6096 private Message findLatestIncomingMessage() {
6097 for (int i = mMessages.size() - 1; i >= 0; i--) {
6098 Message m = mMessages.get(i);
6099 // Incoming messages have a non-empty sender.
6100 if (!TextUtils.isEmpty(m.mSender)) {
6101 return m;
6102 }
6103 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006104 if (!mMessages.isEmpty()) {
6105 // No incoming messages, fall back to outgoing message
6106 return mMessages.get(mMessages.size() - 1);
6107 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006108 return null;
6109 }
6110
6111 /**
6112 * @hide
6113 */
6114 @Override
6115 public RemoteViews makeBigContentView() {
6116 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
6117 ? super.mBigContentTitle
6118 : mConversationTitle;
6119 boolean hasTitle = !TextUtils.isEmpty(title);
6120
Adrian Roosfeafa052016-06-01 17:09:45 -07006121 if (mMessages.size() == 1) {
6122 // Special case for a single message: Use the big text style
6123 // so the collapsed and expanded versions match nicely.
6124 CharSequence bigTitle;
6125 CharSequence text;
6126 if (hasTitle) {
6127 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006128 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07006129 } else {
6130 bigTitle = mMessages.get(0).mSender;
6131 text = mMessages.get(0).mText;
6132 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006133 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
6134 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006135 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07006136 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
6137 return contentView;
6138 }
6139
Adrian Roos48d746a2016-04-12 14:57:28 -07006140 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006141 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006142 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006143
6144 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
6145 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
6146
6147 // Make sure all rows are gone in case we reuse a view.
6148 for (int rowId : rowIds) {
6149 contentView.setViewVisibility(rowId, View.GONE);
6150 }
6151
6152 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006153 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
6154 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006155 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07006156 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006157
Adrian Roosfeafa052016-06-01 17:09:45 -07006158 int contractedChildId = View.NO_ID;
6159 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08006160 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
6161 - (rowIds.length - mMessages.size()));
6162 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
6163 Message m = mHistoricMessages.get(firstHistoricMessage + i);
6164 int rowId = rowIds[i];
6165
Selim Cinek7b9605b2017-01-19 17:36:00 -08006166 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08006167
6168 if (contractedMessage == m) {
6169 contractedChildId = rowId;
6170 }
6171
6172 i++;
6173 }
6174
Adrian Roosc1a80b02016-04-05 14:54:55 -07006175 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
6176 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
6177 Message m = mMessages.get(firstMessage + i);
6178 int rowId = rowIds[i];
6179
6180 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006181 contentView.setTextViewText(rowId, mBuilder.processTextSpans(
6182 makeMessageLine(m, mBuilder)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006183 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006184
Adrian Roosfeafa052016-06-01 17:09:45 -07006185 if (contractedMessage == m) {
6186 contractedChildId = rowId;
6187 }
6188
Adrian Roosc1a80b02016-04-05 14:54:55 -07006189 i++;
6190 }
Adrian Roos437cd562017-01-18 15:47:03 -08006191 // Clear the remaining views for reapply. Ensures that historic message views can
6192 // reliably be identified as being GONE and having non-null text.
6193 while (i < rowIds.length) {
6194 int rowId = rowIds[i];
6195 contentView.setTextViewText(rowId, null);
6196 i++;
6197 }
6198
Adrian Roosfeafa052016-06-01 17:09:45 -07006199 // Record this here to allow transformation between the contracted and expanded views.
6200 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
6201 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04006202 return contentView;
6203 }
6204
Selim Cinek7b9605b2017-01-19 17:36:00 -08006205 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006206 BidiFormatter bidi = BidiFormatter.getInstance();
6207 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08006208 boolean colorize = builder.isColorized();
Anthony Chenad4d1582017-04-10 16:07:58 -07006209 TextAppearanceSpan colorSpan;
6210 CharSequence messageName;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006211 if (TextUtils.isEmpty(m.mSender)) {
6212 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
6213 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006214 makeFontColorSpan(colorize
6215 ? builder.getPrimaryTextColor()
6216 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006217 0 /* flags */);
6218 } else {
6219 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006220 makeFontColorSpan(colorize
6221 ? builder.getPrimaryTextColor()
6222 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006223 0 /* flags */);
6224 }
6225 CharSequence text = m.mText == null ? "" : m.mText;
6226 sb.append(" ").append(bidi.unicodeWrap(text));
6227 return sb;
6228 }
6229
Adrian Roosdedd1df2016-04-26 16:38:47 -07006230 /**
6231 * @hide
6232 */
6233 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006234 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6235 if (increasedHeight) {
6236 return makeBigContentView();
6237 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07006238 Message m = findLatestIncomingMessage();
6239 CharSequence title = mConversationTitle != null
6240 ? mConversationTitle
6241 : (m == null) ? null : m.mSender;
6242 CharSequence text = (m == null)
6243 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08006244 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006245
6246 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006247 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07006248 }
6249
Adrian Roosc1a80b02016-04-05 14:54:55 -07006250 private static TextAppearanceSpan makeFontColorSpan(int color) {
6251 return new TextAppearanceSpan(null, 0, 0,
6252 ColorStateList.valueOf(color), null);
6253 }
6254
Alex Hillsd9b04d92016-04-11 16:38:16 -04006255 public static final class Message {
6256
6257 static final String KEY_TEXT = "text";
6258 static final String KEY_TIMESTAMP = "time";
6259 static final String KEY_SENDER = "sender";
6260 static final String KEY_DATA_MIME_TYPE = "type";
6261 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006262 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006263
6264 private final CharSequence mText;
6265 private final long mTimestamp;
6266 private final CharSequence mSender;
6267
Shane Brennan5a871862017-03-11 13:14:17 -08006268 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006269 private String mDataMimeType;
6270 private Uri mDataUri;
6271
6272 /**
6273 * Constructor
6274 * @param text A {@link CharSequence} to be displayed as the message content
6275 * @param timestamp Time at which the message arrived
6276 * @param sender A {@link CharSequence} to be used for displaying the name of the
6277 * sender. Should be <code>null</code> for messages by the current user, in which case
6278 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6279 * Should be unique amongst all individuals in the conversation, and should be
6280 * consistent during re-posts of the notification.
6281 */
6282 public Message(CharSequence text, long timestamp, CharSequence sender){
6283 mText = text;
6284 mTimestamp = timestamp;
6285 mSender = sender;
6286 }
6287
6288 /**
6289 * Sets a binary blob of data and an associated MIME type for a message. In the case
6290 * where the platform doesn't support the MIME type, the original text provided in the
6291 * constructor will be used.
6292 * @param dataMimeType The MIME type of the content. See
6293 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6294 * types on Android and Android Wear.
6295 * @param dataUri The uri containing the content whose type is given by the MIME type.
6296 * <p class="note">
6297 * <ol>
6298 * <li>Notification Listeners including the System UI need permission to access the
6299 * data the Uri points to. The recommended ways to do this are:</li>
6300 * <li>Store the data in your own ContentProvider, making sure that other apps have
6301 * the correct permission to access your provider. The preferred mechanism for
6302 * providing access is to use per-URI permissions which are temporary and only
6303 * grant access to the receiving application. An easy way to create a
6304 * ContentProvider like this is to use the FileProvider helper class.</li>
6305 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6306 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6307 * also store non-media types (see MediaStore.Files for more info). Files can be
6308 * inserted into the MediaStore using scanFile() after which a content:// style
6309 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6310 * Note that once added to the system MediaStore the content is accessible to any
6311 * app on the device.</li>
6312 * </ol>
6313 * @return this object for method chaining
6314 */
6315 public Message setData(String dataMimeType, Uri dataUri) {
6316 mDataMimeType = dataMimeType;
6317 mDataUri = dataUri;
6318 return this;
6319 }
6320
Alex Hillsfc737de2016-03-23 17:33:02 -04006321 /**
6322 * Get the text to be used for this message, or the fallback text if a type and content
6323 * Uri have been set
6324 */
6325 public CharSequence getText() {
6326 return mText;
6327 }
6328
6329 /**
6330 * Get the time at which this message arrived
6331 */
6332 public long getTimestamp() {
6333 return mTimestamp;
6334 }
6335
6336 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006337 * Get the extras Bundle for this message.
6338 */
6339 public Bundle getExtras() {
6340 return mExtras;
6341 }
6342
6343 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006344 * Get the text used to display the contact's name in the messaging experience
6345 */
6346 public CharSequence getSender() {
6347 return mSender;
6348 }
6349
6350 /**
6351 * Get the MIME type of the data pointed to by the Uri
6352 */
6353 public String getDataMimeType() {
6354 return mDataMimeType;
6355 }
6356
6357 /**
6358 * Get the the Uri pointing to the content of the message. Can be null, in which case
6359 * {@see #getText()} is used.
6360 */
6361 public Uri getDataUri() {
6362 return mDataUri;
6363 }
6364
Alex Hillsd9b04d92016-04-11 16:38:16 -04006365 private Bundle toBundle() {
6366 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006367 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006368 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006369 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006370 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006371 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006372 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006373 }
6374 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006375 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006376 }
6377 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006378 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006379 }
Shane Brennan5a871862017-03-11 13:14:17 -08006380 if (mExtras != null) {
6381 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6382 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006383 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006384 }
6385
Alex Hillsd9b04d92016-04-11 16:38:16 -04006386 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6387 Bundle[] bundles = new Bundle[messages.size()];
6388 final int N = messages.size();
6389 for (int i = 0; i < N; i++) {
6390 bundles[i] = messages.get(i).toBundle();
6391 }
6392 return bundles;
6393 }
6394
Adrian Roosdedd1df2016-04-26 16:38:47 -07006395 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006396 List<Message> messages = new ArrayList<>(bundles.length);
6397 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006398 if (bundles[i] instanceof Bundle) {
6399 Message message = getMessageFromBundle((Bundle)bundles[i]);
6400 if (message != null) {
6401 messages.add(message);
6402 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006403 }
6404 }
6405 return messages;
6406 }
6407
6408 static Message getMessageFromBundle(Bundle bundle) {
6409 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006410 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006411 return null;
6412 } else {
6413 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6414 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6415 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6416 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006417 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6418 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006419 }
Shane Brennan5a871862017-03-11 13:14:17 -08006420 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6421 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6422 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006423 return message;
6424 }
6425 } catch (ClassCastException e) {
6426 return null;
6427 }
6428 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006429 }
6430 }
6431
6432 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006433 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006434 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006435 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006436 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006437 * Notification notif = new Notification.Builder(mContext)
6438 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6439 * .setContentText(subject)
6440 * .setSmallIcon(R.drawable.new_mail)
6441 * .setLargeIcon(aBitmap)
6442 * .setStyle(new Notification.InboxStyle()
6443 * .addLine(str1)
6444 * .addLine(str2)
6445 * .setContentTitle(&quot;&quot;)
6446 * .setSummaryText(&quot;+3 more&quot;))
6447 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006448 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006449 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006450 * @see Notification#bigContentView
6451 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006452 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006453 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6454
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006455 public InboxStyle() {
6456 }
6457
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006458 /**
6459 * @deprecated use {@code InboxStyle()}.
6460 */
6461 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006462 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006463 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006464 }
6465
Chris Wrend6297db2012-05-03 16:20:13 -04006466 /**
6467 * Overrides ContentTitle in the big form of the template.
6468 * This defaults to the value passed to setContentTitle().
6469 */
6470 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006471 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006472 return this;
6473 }
6474
6475 /**
6476 * Set the first line of text after the detail section in the big form of the template.
6477 */
6478 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006479 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006480 return this;
6481 }
6482
Chris Wren0bd664d2012-08-01 13:56:56 -04006483 /**
6484 * Append a line to the digest section of the Inbox notification.
6485 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006486 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006487 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006488 return this;
6489 }
6490
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006491 /**
6492 * @hide
6493 */
6494 public void addExtras(Bundle extras) {
6495 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006496
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006497 CharSequence[] a = new CharSequence[mTexts.size()];
6498 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6499 }
6500
Christoph Studer4600f9b2014-07-22 22:44:43 +02006501 /**
6502 * @hide
6503 */
6504 @Override
6505 protected void restoreFromExtras(Bundle extras) {
6506 super.restoreFromExtras(extras);
6507
6508 mTexts.clear();
6509 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6510 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6511 }
6512 }
6513
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006514 /**
6515 * @hide
6516 */
6517 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006518 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006519 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006520 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6521 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006522
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006523 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006524
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006525 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006526
Chris Wrend6297db2012-05-03 16:20:13 -04006527 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 -04006528 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006529
Chris Wren4ed80d52012-05-17 09:30:03 -04006530 // Make sure all rows are gone in case we reuse a view.
6531 for (int rowId : rowIds) {
6532 contentView.setViewVisibility(rowId, View.GONE);
6533 }
6534
Daniel Sandler879c5e02012-04-17 16:46:51 -04006535 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006536 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6537 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006538 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006539 int onlyViewId = 0;
6540 int maxRows = rowIds.length;
6541 if (mBuilder.mActions.size() > 0) {
6542 maxRows--;
6543 }
6544 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006545 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006546 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006547 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006548 contentView.setTextViewText(rowIds[i],
6549 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006550 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006551 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006552 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006553 if (first) {
6554 onlyViewId = rowIds[i];
6555 } else {
6556 onlyViewId = 0;
6557 }
Selim Cinek247fa012016-02-18 09:50:48 -08006558 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006559 }
6560 i++;
6561 }
Selim Cinek07c80172016-04-21 16:40:47 -07006562 if (onlyViewId != 0) {
6563 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6564 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6565 R.dimen.notification_text_margin_top);
6566 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6567 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006568
Daniel Sandler879c5e02012-04-17 16:46:51 -04006569 return contentView;
6570 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006571
Selim Cinek247fa012016-02-18 09:50:48 -08006572 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006573 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006574 if (first) {
6575 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6576 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6577 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006578 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006579 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006580 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006581 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006582 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006583 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006584 }
Dan Sandler842dd772014-05-15 09:36:47 -04006585
6586 /**
6587 * Notification style for media playback notifications.
6588 *
6589 * In the expanded form, {@link Notification#bigContentView}, up to 5
6590 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006591 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006592 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6593 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6594 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006595 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006596 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6597 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006598 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006599 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006600 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006601 * Notifications created with MediaStyle will have their category set to
6602 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6603 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006604 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006605 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6606 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006607 * the System UI can identify this as a notification representing an active media session
6608 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6609 *
Selim Cinek99104832017-01-25 14:47:33 -08006610 * <p>
6611 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6612 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6613 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6614 * <p>
6615 *
Dan Sandler842dd772014-05-15 09:36:47 -04006616 * To use this style with your Notification, feed it to
6617 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6618 * <pre class="prettyprint">
6619 * Notification noti = new Notification.Builder()
6620 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006621 * .setContentTitle(&quot;Track title&quot;)
6622 * .setContentText(&quot;Artist - Album&quot;)
6623 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006624 * .setStyle(<b>new Notification.MediaStyle()</b>
6625 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006626 * .build();
6627 * </pre>
6628 *
6629 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006630 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006631 */
6632 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006633 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006634 static final int MAX_MEDIA_BUTTONS = 5;
6635
6636 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006637 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006638
6639 public MediaStyle() {
6640 }
6641
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006642 /**
6643 * @deprecated use {@code MediaStyle()}.
6644 */
6645 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006646 public MediaStyle(Builder builder) {
6647 setBuilder(builder);
6648 }
6649
6650 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006651 * 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 -04006652 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006653 *
6654 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006655 */
6656 public MediaStyle setShowActionsInCompactView(int...actions) {
6657 mActionsToShowInCompact = actions;
6658 return this;
6659 }
6660
6661 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006662 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6663 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006664 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006665 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006666 mToken = token;
6667 return this;
6668 }
6669
Christoph Studer4600f9b2014-07-22 22:44:43 +02006670 /**
6671 * @hide
6672 */
Dan Sandler842dd772014-05-15 09:36:47 -04006673 @Override
6674 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006675 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006676 if (wip.category == null) {
6677 wip.category = Notification.CATEGORY_TRANSPORT;
6678 }
Dan Sandler842dd772014-05-15 09:36:47 -04006679 return wip;
6680 }
6681
Christoph Studer4600f9b2014-07-22 22:44:43 +02006682 /**
6683 * @hide
6684 */
6685 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006686 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006687 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006688 }
6689
6690 /**
6691 * @hide
6692 */
6693 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006694 public RemoteViews makeBigContentView() {
6695 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006696 }
6697
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006698 /**
6699 * @hide
6700 */
6701 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006702 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006703 RemoteViews expanded = makeMediaBigContentView();
6704 return expanded != null ? expanded : makeMediaContentView();
6705 }
6706
Dan Sandler842dd772014-05-15 09:36:47 -04006707 /** @hide */
6708 @Override
6709 public void addExtras(Bundle extras) {
6710 super.addExtras(extras);
6711
6712 if (mToken != null) {
6713 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6714 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006715 if (mActionsToShowInCompact != null) {
6716 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6717 }
Dan Sandler842dd772014-05-15 09:36:47 -04006718 }
6719
Christoph Studer4600f9b2014-07-22 22:44:43 +02006720 /**
6721 * @hide
6722 */
6723 @Override
6724 protected void restoreFromExtras(Bundle extras) {
6725 super.restoreFromExtras(extras);
6726
6727 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6728 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6729 }
6730 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6731 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6732 }
6733 }
6734
Selim Cinek5bf069a2015-11-10 19:14:27 -05006735 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006736 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006737 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006738 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006739 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006740
6741 // If the action buttons should not be tinted, then just use the default
6742 // notification color. Otherwise, just use the passed-in color.
6743 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6744 ? color
6745 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6746 Notification.COLOR_DEFAULT);
6747
Sunny Goyal5b153922017-09-21 21:00:36 -07006748 button.setDrawableTint(R.id.action0, false, tintColor,
6749 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006750 if (!tombstone) {
6751 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6752 }
6753 button.setContentDescription(R.id.action0, action.title);
6754 return button;
6755 }
6756
6757 private RemoteViews makeMediaContentView() {
6758 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006759 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006760
6761 final int numActions = mBuilder.mActions.size();
6762 final int N = mActionsToShowInCompact == null
6763 ? 0
6764 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6765 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006766 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006767 for (int i = 0; i < N; i++) {
6768 if (i >= numActions) {
6769 throw new IllegalArgumentException(String.format(
6770 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6771 i, numActions - 1));
6772 }
6773
6774 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006775 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006776 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006777 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006778 }
6779 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006780 handleImage(view);
6781 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006782 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006783 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006784 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006785 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006786 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006787 return view;
6788 }
6789
Selim Cinek99104832017-01-25 14:47:33 -08006790 private int getPrimaryHighlightColor() {
6791 return mBuilder.getPrimaryHighlightColor();
6792 }
6793
Dan Sandler842dd772014-05-15 09:36:47 -04006794 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006795 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006796 // Dont add an expanded view if there is no more content to be revealed
6797 int actionsInCompact = mActionsToShowInCompact == null
6798 ? 0
6799 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006800 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006801 return null;
6802 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006803 RemoteViews big = mBuilder.applyStandardTemplate(
6804 R.layout.notification_template_material_big_media,
6805 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006806
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006807 if (actionCount > 0) {
6808 big.removeAllViews(com.android.internal.R.id.media_actions);
6809 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006810 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006811 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006812 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006813 }
6814 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006815 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006816 return big;
6817 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006818
Selim Cinek5bf069a2015-11-10 19:14:27 -05006819 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006820 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006821 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6822 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006823 }
6824 }
6825
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006826 /**
6827 * @hide
6828 */
6829 @Override
6830 protected boolean hasProgress() {
6831 return false;
6832 }
Dan Sandler842dd772014-05-15 09:36:47 -04006833 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006834
Selim Cinek593610c2016-02-16 18:42:57 -08006835 /**
6836 * Notification style for custom views that are decorated by the system
6837 *
6838 * <p>Instead of providing a notification that is completely custom, a developer can set this
6839 * style and still obtain system decorations like the notification header with the expand
6840 * affordance and actions.
6841 *
6842 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6843 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6844 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6845 * corresponding custom views to display.
6846 *
6847 * To use this style with your Notification, feed it to
6848 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6849 * <pre class="prettyprint">
6850 * Notification noti = new Notification.Builder()
6851 * .setSmallIcon(R.drawable.ic_stat_player)
6852 * .setLargeIcon(albumArtBitmap))
6853 * .setCustomContentView(contentView);
6854 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6855 * .build();
6856 * </pre>
6857 */
6858 public static class DecoratedCustomViewStyle extends Style {
6859
6860 public DecoratedCustomViewStyle() {
6861 }
6862
Selim Cinek593610c2016-02-16 18:42:57 -08006863 /**
6864 * @hide
6865 */
6866 public boolean displayCustomViewInline() {
6867 return true;
6868 }
6869
6870 /**
6871 * @hide
6872 */
6873 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006874 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006875 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6876 }
6877
6878 /**
6879 * @hide
6880 */
6881 @Override
6882 public RemoteViews makeBigContentView() {
6883 return makeDecoratedBigContentView();
6884 }
6885
6886 /**
6887 * @hide
6888 */
6889 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006890 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006891 return makeDecoratedHeadsUpContentView();
6892 }
6893
Selim Cinek593610c2016-02-16 18:42:57 -08006894 private RemoteViews makeDecoratedHeadsUpContentView() {
6895 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6896 ? mBuilder.mN.contentView
6897 : mBuilder.mN.headsUpContentView;
6898 if (mBuilder.mActions.size() == 0) {
6899 return makeStandardTemplateWithCustomContent(headsUpContentView);
6900 }
6901 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6902 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006903 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006904 return remoteViews;
6905 }
6906
Selim Cinek593610c2016-02-16 18:42:57 -08006907 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6908 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6909 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006910 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006911 return remoteViews;
6912 }
6913
Selim Cinek593610c2016-02-16 18:42:57 -08006914 private RemoteViews makeDecoratedBigContentView() {
6915 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6916 ? mBuilder.mN.contentView
6917 : mBuilder.mN.bigContentView;
6918 if (mBuilder.mActions.size() == 0) {
6919 return makeStandardTemplateWithCustomContent(bigContentView);
6920 }
6921 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6922 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006923 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006924 return remoteViews;
6925 }
Selim Cinek247fa012016-02-18 09:50:48 -08006926
6927 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6928 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006929 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006930 // Need to clone customContent before adding, because otherwise it can no longer be
6931 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006932 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006933 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6934 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07006935 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08006936 }
Selim Cinek247fa012016-02-18 09:50:48 -08006937 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006938 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006939 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006940 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006941 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006942 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006943 }
Selim Cinek593610c2016-02-16 18:42:57 -08006944 }
6945
Selim Cinek03eb3b72016-02-18 10:39:45 -08006946 /**
6947 * Notification style for media custom views that are decorated by the system
6948 *
6949 * <p>Instead of providing a media notification that is completely custom, a developer can set
6950 * this style and still obtain system decorations like the notification header with the expand
6951 * affordance and actions.
6952 *
6953 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6954 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6955 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6956 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006957 * <p>
6958 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6959 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6960 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006961 * To use this style with your Notification, feed it to
6962 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6963 * <pre class="prettyprint">
6964 * Notification noti = new Notification.Builder()
6965 * .setSmallIcon(R.drawable.ic_stat_player)
6966 * .setLargeIcon(albumArtBitmap))
6967 * .setCustomContentView(contentView);
6968 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6969 * .setMediaSession(mySession))
6970 * .build();
6971 * </pre>
6972 *
6973 * @see android.app.Notification.DecoratedCustomViewStyle
6974 * @see android.app.Notification.MediaStyle
6975 */
6976 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6977
6978 public DecoratedMediaCustomViewStyle() {
6979 }
6980
Selim Cinek03eb3b72016-02-18 10:39:45 -08006981 /**
6982 * @hide
6983 */
6984 public boolean displayCustomViewInline() {
6985 return true;
6986 }
6987
6988 /**
6989 * @hide
6990 */
6991 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006992 public RemoteViews makeContentView(boolean increasedHeight) {
6993 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006994 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6995 mBuilder.mN.contentView);
6996 }
6997
6998 /**
6999 * @hide
7000 */
7001 @Override
7002 public RemoteViews makeBigContentView() {
7003 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7004 ? mBuilder.mN.bigContentView
7005 : mBuilder.mN.contentView;
7006 return makeBigContentViewWithCustomContent(customRemoteView);
7007 }
7008
7009 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7010 RemoteViews remoteViews = super.makeBigContentView();
7011 if (remoteViews != null) {
7012 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7013 customRemoteView);
7014 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007015 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007016 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7017 customRemoteView);
7018 } else {
7019 return null;
7020 }
7021 }
7022
7023 /**
7024 * @hide
7025 */
7026 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007027 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007028 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7029 ? mBuilder.mN.headsUpContentView
7030 : mBuilder.mN.contentView;
7031 return makeBigContentViewWithCustomContent(customRemoteView);
7032 }
7033
7034 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7035 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007036 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007037 // Need to clone customContent before adding, because otherwise it can no longer be
7038 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007039 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007040 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007041 remoteViews.removeAllViews(id);
7042 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007043 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007044 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007045 return remoteViews;
7046 }
7047 }
7048
Christoph Studer4600f9b2014-07-22 22:44:43 +02007049 // When adding a new Style subclass here, don't forget to update
7050 // Builder.getNotificationStyleClass.
7051
Griff Hazen61a9e862014-05-22 16:05:19 -07007052 /**
7053 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7054 * metadata or change options on a notification builder.
7055 */
7056 public interface Extender {
7057 /**
7058 * Apply this extender to a notification builder.
7059 * @param builder the builder to be modified.
7060 * @return the build object for chaining.
7061 */
7062 public Builder extend(Builder builder);
7063 }
7064
7065 /**
7066 * Helper class to add wearable extensions to notifications.
7067 * <p class="note"> See
7068 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7069 * for Android Wear</a> for more information on how to use this class.
7070 * <p>
7071 * To create a notification with wearable extensions:
7072 * <ol>
7073 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7074 * properties.
7075 * <li>Create a {@link android.app.Notification.WearableExtender}.
7076 * <li>Set wearable-specific properties using the
7077 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7078 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7079 * notification.
7080 * <li>Post the notification to the notification system with the
7081 * {@code NotificationManager.notify(...)} methods.
7082 * </ol>
7083 *
7084 * <pre class="prettyprint">
7085 * Notification notif = new Notification.Builder(mContext)
7086 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7087 * .setContentText(subject)
7088 * .setSmallIcon(R.drawable.new_mail)
7089 * .extend(new Notification.WearableExtender()
7090 * .setContentIcon(R.drawable.new_mail))
7091 * .build();
7092 * NotificationManager notificationManger =
7093 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7094 * notificationManger.notify(0, notif);</pre>
7095 *
7096 * <p>Wearable extensions can be accessed on an existing notification by using the
7097 * {@code WearableExtender(Notification)} constructor,
7098 * and then using the {@code get} methods to access values.
7099 *
7100 * <pre class="prettyprint">
7101 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7102 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007103 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007104 */
7105 public static final class WearableExtender implements Extender {
7106 /**
7107 * Sentinel value for an action index that is unset.
7108 */
7109 public static final int UNSET_ACTION_INDEX = -1;
7110
7111 /**
7112 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7113 * default sizing.
7114 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007115 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007116 * on their content.
7117 */
7118 public static final int SIZE_DEFAULT = 0;
7119
7120 /**
7121 * Size value for use with {@link #setCustomSizePreset} to show this notification
7122 * with an extra small size.
7123 * <p>This value is only applicable for custom display notifications created using
7124 * {@link #setDisplayIntent}.
7125 */
7126 public static final int SIZE_XSMALL = 1;
7127
7128 /**
7129 * Size value for use with {@link #setCustomSizePreset} to show this notification
7130 * with a small size.
7131 * <p>This value is only applicable for custom display notifications created using
7132 * {@link #setDisplayIntent}.
7133 */
7134 public static final int SIZE_SMALL = 2;
7135
7136 /**
7137 * Size value for use with {@link #setCustomSizePreset} to show this notification
7138 * with a medium size.
7139 * <p>This value is only applicable for custom display notifications created using
7140 * {@link #setDisplayIntent}.
7141 */
7142 public static final int SIZE_MEDIUM = 3;
7143
7144 /**
7145 * Size value for use with {@link #setCustomSizePreset} to show this notification
7146 * with a large size.
7147 * <p>This value is only applicable for custom display notifications created using
7148 * {@link #setDisplayIntent}.
7149 */
7150 public static final int SIZE_LARGE = 4;
7151
Griff Hazend5f11f92014-05-27 15:40:09 -07007152 /**
7153 * Size value for use with {@link #setCustomSizePreset} to show this notification
7154 * full screen.
7155 * <p>This value is only applicable for custom display notifications created using
7156 * {@link #setDisplayIntent}.
7157 */
7158 public static final int SIZE_FULL_SCREEN = 5;
7159
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007160 /**
7161 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7162 * short amount of time when this notification is displayed on the screen. This
7163 * is the default value.
7164 */
7165 public static final int SCREEN_TIMEOUT_SHORT = 0;
7166
7167 /**
7168 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7169 * for a longer amount of time when this notification is displayed on the screen.
7170 */
7171 public static final int SCREEN_TIMEOUT_LONG = -1;
7172
Griff Hazen61a9e862014-05-22 16:05:19 -07007173 /** Notification extra which contains wearable extensions */
7174 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7175
Pete Gastaf6781d2014-10-07 15:17:05 -04007176 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007177 private static final String KEY_ACTIONS = "actions";
7178 private static final String KEY_FLAGS = "flags";
7179 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7180 private static final String KEY_PAGES = "pages";
7181 private static final String KEY_BACKGROUND = "background";
7182 private static final String KEY_CONTENT_ICON = "contentIcon";
7183 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7184 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7185 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7186 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7187 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007188 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007189 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007190 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007191
7192 // Flags bitwise-ored to mFlags
7193 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7194 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7195 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7196 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007197 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007198 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007199 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007200
7201 // Default value for flags integer
7202 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7203
7204 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7205 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7206
7207 private ArrayList<Action> mActions = new ArrayList<Action>();
7208 private int mFlags = DEFAULT_FLAGS;
7209 private PendingIntent mDisplayIntent;
7210 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7211 private Bitmap mBackground;
7212 private int mContentIcon;
7213 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7214 private int mContentActionIndex = UNSET_ACTION_INDEX;
7215 private int mCustomSizePreset = SIZE_DEFAULT;
7216 private int mCustomContentHeight;
7217 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007218 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007219 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007220 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007221
7222 /**
7223 * Create a {@link android.app.Notification.WearableExtender} with default
7224 * options.
7225 */
7226 public WearableExtender() {
7227 }
7228
7229 public WearableExtender(Notification notif) {
7230 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7231 if (wearableBundle != null) {
7232 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7233 if (actions != null) {
7234 mActions.addAll(actions);
7235 }
7236
7237 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7238 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7239
7240 Notification[] pages = getNotificationArrayFromBundle(
7241 wearableBundle, KEY_PAGES);
7242 if (pages != null) {
7243 Collections.addAll(mPages, pages);
7244 }
7245
7246 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7247 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7248 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7249 DEFAULT_CONTENT_ICON_GRAVITY);
7250 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7251 UNSET_ACTION_INDEX);
7252 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7253 SIZE_DEFAULT);
7254 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7255 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007256 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007257 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007258 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007259 }
7260 }
7261
7262 /**
7263 * Apply wearable extensions to a notification that is being built. This is typically
7264 * called by the {@link android.app.Notification.Builder#extend} method of
7265 * {@link android.app.Notification.Builder}.
7266 */
7267 @Override
7268 public Notification.Builder extend(Notification.Builder builder) {
7269 Bundle wearableBundle = new Bundle();
7270
7271 if (!mActions.isEmpty()) {
7272 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7273 }
7274 if (mFlags != DEFAULT_FLAGS) {
7275 wearableBundle.putInt(KEY_FLAGS, mFlags);
7276 }
7277 if (mDisplayIntent != null) {
7278 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7279 }
7280 if (!mPages.isEmpty()) {
7281 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7282 new Notification[mPages.size()]));
7283 }
7284 if (mBackground != null) {
7285 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7286 }
7287 if (mContentIcon != 0) {
7288 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7289 }
7290 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7291 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7292 }
7293 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7294 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7295 mContentActionIndex);
7296 }
7297 if (mCustomSizePreset != SIZE_DEFAULT) {
7298 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7299 }
7300 if (mCustomContentHeight != 0) {
7301 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7302 }
7303 if (mGravity != DEFAULT_GRAVITY) {
7304 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7305 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007306 if (mHintScreenTimeout != 0) {
7307 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7308 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007309 if (mDismissalId != null) {
7310 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7311 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007312 if (mBridgeTag != null) {
7313 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7314 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007315
7316 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7317 return builder;
7318 }
7319
7320 @Override
7321 public WearableExtender clone() {
7322 WearableExtender that = new WearableExtender();
7323 that.mActions = new ArrayList<Action>(this.mActions);
7324 that.mFlags = this.mFlags;
7325 that.mDisplayIntent = this.mDisplayIntent;
7326 that.mPages = new ArrayList<Notification>(this.mPages);
7327 that.mBackground = this.mBackground;
7328 that.mContentIcon = this.mContentIcon;
7329 that.mContentIconGravity = this.mContentIconGravity;
7330 that.mContentActionIndex = this.mContentActionIndex;
7331 that.mCustomSizePreset = this.mCustomSizePreset;
7332 that.mCustomContentHeight = this.mCustomContentHeight;
7333 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007334 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007335 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007336 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007337 return that;
7338 }
7339
7340 /**
7341 * Add a wearable action to this notification.
7342 *
7343 * <p>When wearable actions are added using this method, the set of actions that
7344 * show on a wearable device splits from devices that only show actions added
7345 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7346 * of which actions display on different devices.
7347 *
7348 * @param action the action to add to this notification
7349 * @return this object for method chaining
7350 * @see android.app.Notification.Action
7351 */
7352 public WearableExtender addAction(Action action) {
7353 mActions.add(action);
7354 return this;
7355 }
7356
7357 /**
7358 * Adds wearable actions to this notification.
7359 *
7360 * <p>When wearable actions are added using this method, the set of actions that
7361 * show on a wearable device splits from devices that only show actions added
7362 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7363 * of which actions display on different devices.
7364 *
7365 * @param actions the actions to add to this notification
7366 * @return this object for method chaining
7367 * @see android.app.Notification.Action
7368 */
7369 public WearableExtender addActions(List<Action> actions) {
7370 mActions.addAll(actions);
7371 return this;
7372 }
7373
7374 /**
7375 * Clear all wearable actions present on this builder.
7376 * @return this object for method chaining.
7377 * @see #addAction
7378 */
7379 public WearableExtender clearActions() {
7380 mActions.clear();
7381 return this;
7382 }
7383
7384 /**
7385 * Get the wearable actions present on this notification.
7386 */
7387 public List<Action> getActions() {
7388 return mActions;
7389 }
7390
7391 /**
7392 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007393 * this notification. The {@link PendingIntent} provided should be for an activity.
7394 *
7395 * <pre class="prettyprint">
7396 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7397 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7398 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7399 * Notification notif = new Notification.Builder(context)
7400 * .extend(new Notification.WearableExtender()
7401 * .setDisplayIntent(displayPendingIntent)
7402 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7403 * .build();</pre>
7404 *
7405 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007406 * should have an empty task affinity. It is also recommended to use the device
7407 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007408 *
7409 * <p>Example AndroidManifest.xml entry:
7410 * <pre class="prettyprint">
7411 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7412 * android:exported=&quot;true&quot;
7413 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007414 * android:taskAffinity=&quot;&quot;
7415 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007416 *
7417 * @param intent the {@link PendingIntent} for an activity
7418 * @return this object for method chaining
7419 * @see android.app.Notification.WearableExtender#getDisplayIntent
7420 */
7421 public WearableExtender setDisplayIntent(PendingIntent intent) {
7422 mDisplayIntent = intent;
7423 return this;
7424 }
7425
7426 /**
7427 * Get the intent to launch inside of an activity view when displaying this
7428 * notification. This {@code PendingIntent} should be for an activity.
7429 */
7430 public PendingIntent getDisplayIntent() {
7431 return mDisplayIntent;
7432 }
7433
7434 /**
7435 * Add an additional page of content to display with this notification. The current
7436 * notification forms the first page, and pages added using this function form
7437 * subsequent pages. This field can be used to separate a notification into multiple
7438 * sections.
7439 *
7440 * @param page the notification to add as another page
7441 * @return this object for method chaining
7442 * @see android.app.Notification.WearableExtender#getPages
7443 */
7444 public WearableExtender addPage(Notification page) {
7445 mPages.add(page);
7446 return this;
7447 }
7448
7449 /**
7450 * Add additional pages of content to display with this notification. The current
7451 * notification forms the first page, and pages added using this function form
7452 * subsequent pages. This field can be used to separate a notification into multiple
7453 * sections.
7454 *
7455 * @param pages a list of notifications
7456 * @return this object for method chaining
7457 * @see android.app.Notification.WearableExtender#getPages
7458 */
7459 public WearableExtender addPages(List<Notification> pages) {
7460 mPages.addAll(pages);
7461 return this;
7462 }
7463
7464 /**
7465 * Clear all additional pages present on this builder.
7466 * @return this object for method chaining.
7467 * @see #addPage
7468 */
7469 public WearableExtender clearPages() {
7470 mPages.clear();
7471 return this;
7472 }
7473
7474 /**
7475 * Get the array of additional pages of content for displaying this notification. The
7476 * current notification forms the first page, and elements within this array form
7477 * subsequent pages. This field can be used to separate a notification into multiple
7478 * sections.
7479 * @return the pages for this notification
7480 */
7481 public List<Notification> getPages() {
7482 return mPages;
7483 }
7484
7485 /**
7486 * Set a background image to be displayed behind the notification content.
7487 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7488 * will work with any notification style.
7489 *
7490 * @param background the background bitmap
7491 * @return this object for method chaining
7492 * @see android.app.Notification.WearableExtender#getBackground
7493 */
7494 public WearableExtender setBackground(Bitmap background) {
7495 mBackground = background;
7496 return this;
7497 }
7498
7499 /**
7500 * Get a background image to be displayed behind the notification content.
7501 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7502 * will work with any notification style.
7503 *
7504 * @return the background image
7505 * @see android.app.Notification.WearableExtender#setBackground
7506 */
7507 public Bitmap getBackground() {
7508 return mBackground;
7509 }
7510
7511 /**
7512 * Set an icon that goes with the content of this notification.
7513 */
7514 public WearableExtender setContentIcon(int icon) {
7515 mContentIcon = icon;
7516 return this;
7517 }
7518
7519 /**
7520 * Get an icon that goes with the content of this notification.
7521 */
7522 public int getContentIcon() {
7523 return mContentIcon;
7524 }
7525
7526 /**
7527 * Set the gravity that the content icon should have within the notification display.
7528 * Supported values include {@link android.view.Gravity#START} and
7529 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7530 * @see #setContentIcon
7531 */
7532 public WearableExtender setContentIconGravity(int contentIconGravity) {
7533 mContentIconGravity = contentIconGravity;
7534 return this;
7535 }
7536
7537 /**
7538 * Get the gravity that the content icon should have within the notification display.
7539 * Supported values include {@link android.view.Gravity#START} and
7540 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7541 * @see #getContentIcon
7542 */
7543 public int getContentIconGravity() {
7544 return mContentIconGravity;
7545 }
7546
7547 /**
7548 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007549 * this notification. This action will no longer display separately from the
7550 * notification's content.
7551 *
Griff Hazenca48d352014-05-28 22:37:13 -07007552 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007553 * set, although the list of available actions comes from the main notification and not
7554 * from the child page's notification.
7555 *
7556 * @param actionIndex The index of the action to hoist onto the current notification page.
7557 * If wearable actions were added to the main notification, this index
7558 * will apply to that list, otherwise it will apply to the regular
7559 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007560 */
7561 public WearableExtender setContentAction(int actionIndex) {
7562 mContentActionIndex = actionIndex;
7563 return this;
7564 }
7565
7566 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007567 * Get the index of the notification action, if any, that was specified as being clickable
7568 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007569 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007570 *
Griff Hazenca48d352014-05-28 22:37:13 -07007571 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007572 * set, although the list of available actions comes from the main notification and not
7573 * from the child page's notification.
7574 *
7575 * <p>If wearable specific actions were added to the main notification, this index will
7576 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007577 *
7578 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007579 */
7580 public int getContentAction() {
7581 return mContentActionIndex;
7582 }
7583
7584 /**
7585 * Set the gravity that this notification should have within the available viewport space.
7586 * Supported values include {@link android.view.Gravity#TOP},
7587 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7588 * The default value is {@link android.view.Gravity#BOTTOM}.
7589 */
7590 public WearableExtender setGravity(int gravity) {
7591 mGravity = gravity;
7592 return this;
7593 }
7594
7595 /**
7596 * Get the gravity that this notification should have within the available viewport space.
7597 * Supported values include {@link android.view.Gravity#TOP},
7598 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7599 * The default value is {@link android.view.Gravity#BOTTOM}.
7600 */
7601 public int getGravity() {
7602 return mGravity;
7603 }
7604
7605 /**
7606 * Set the custom size preset for the display of this notification out of the available
7607 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7608 * {@link #SIZE_LARGE}.
7609 * <p>Some custom size presets are only applicable for custom display notifications created
7610 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7611 * documentation for the preset in question. See also
7612 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7613 */
7614 public WearableExtender setCustomSizePreset(int sizePreset) {
7615 mCustomSizePreset = sizePreset;
7616 return this;
7617 }
7618
7619 /**
7620 * Get the custom size preset for the display of this notification out of the available
7621 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7622 * {@link #SIZE_LARGE}.
7623 * <p>Some custom size presets are only applicable for custom display notifications created
7624 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7625 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7626 */
7627 public int getCustomSizePreset() {
7628 return mCustomSizePreset;
7629 }
7630
7631 /**
7632 * Set the custom height in pixels for the display of this notification's content.
7633 * <p>This option is only available for custom display notifications created
7634 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7635 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7636 * {@link #getCustomContentHeight}.
7637 */
7638 public WearableExtender setCustomContentHeight(int height) {
7639 mCustomContentHeight = height;
7640 return this;
7641 }
7642
7643 /**
7644 * Get the custom height in pixels for the display of this notification's content.
7645 * <p>This option is only available for custom display notifications created
7646 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7647 * {@link #setCustomContentHeight}.
7648 */
7649 public int getCustomContentHeight() {
7650 return mCustomContentHeight;
7651 }
7652
7653 /**
7654 * Set whether the scrolling position for the contents of this notification should start
7655 * at the bottom of the contents instead of the top when the contents are too long to
7656 * display within the screen. Default is false (start scroll at the top).
7657 */
7658 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7659 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7660 return this;
7661 }
7662
7663 /**
7664 * Get whether the scrolling position for the contents of this notification should start
7665 * at the bottom of the contents instead of the top when the contents are too long to
7666 * display within the screen. Default is false (start scroll at the top).
7667 */
7668 public boolean getStartScrollBottom() {
7669 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7670 }
7671
7672 /**
7673 * Set whether the content intent is available when the wearable device is not connected
7674 * to a companion device. The user can still trigger this intent when the wearable device
7675 * is offline, but a visual hint will indicate that the content intent may not be available.
7676 * Defaults to true.
7677 */
7678 public WearableExtender setContentIntentAvailableOffline(
7679 boolean contentIntentAvailableOffline) {
7680 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7681 return this;
7682 }
7683
7684 /**
7685 * Get whether the content intent is available when the wearable device is not connected
7686 * to a companion device. The user can still trigger this intent when the wearable device
7687 * is offline, but a visual hint will indicate that the content intent may not be available.
7688 * Defaults to true.
7689 */
7690 public boolean getContentIntentAvailableOffline() {
7691 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7692 }
7693
7694 /**
7695 * Set a hint that this notification's icon should not be displayed.
7696 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7697 * @return this object for method chaining
7698 */
7699 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7700 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7701 return this;
7702 }
7703
7704 /**
7705 * Get a hint that this notification's icon should not be displayed.
7706 * @return {@code true} if this icon should not be displayed, false otherwise.
7707 * The default value is {@code false} if this was never set.
7708 */
7709 public boolean getHintHideIcon() {
7710 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7711 }
7712
7713 /**
7714 * Set a visual hint that only the background image of this notification should be
7715 * displayed, and other semantic content should be hidden. This hint is only applicable
7716 * to sub-pages added using {@link #addPage}.
7717 */
7718 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7719 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7720 return this;
7721 }
7722
7723 /**
7724 * Get a visual hint that only the background image of this notification should be
7725 * displayed, and other semantic content should be hidden. This hint is only applicable
7726 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7727 */
7728 public boolean getHintShowBackgroundOnly() {
7729 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7730 }
7731
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007732 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007733 * Set a hint that this notification's background should not be clipped if possible,
7734 * and should instead be resized to fully display on the screen, retaining the aspect
7735 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007736 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7737 * @return this object for method chaining
7738 */
7739 public WearableExtender setHintAvoidBackgroundClipping(
7740 boolean hintAvoidBackgroundClipping) {
7741 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7742 return this;
7743 }
7744
7745 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007746 * Get a hint that this notification's background should not be clipped if possible,
7747 * and should instead be resized to fully display on the screen, retaining the aspect
7748 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007749 * @return {@code true} if it's ok if the background is clipped on the screen, false
7750 * otherwise. The default value is {@code false} if this was never set.
7751 */
7752 public boolean getHintAvoidBackgroundClipping() {
7753 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7754 }
7755
7756 /**
7757 * Set a hint that the screen should remain on for at least this duration when
7758 * this notification is displayed on the screen.
7759 * @param timeout The requested screen timeout in milliseconds. Can also be either
7760 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7761 * @return this object for method chaining
7762 */
7763 public WearableExtender setHintScreenTimeout(int timeout) {
7764 mHintScreenTimeout = timeout;
7765 return this;
7766 }
7767
7768 /**
7769 * Get the duration, in milliseconds, that the screen should remain on for
7770 * when this notification is displayed.
7771 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7772 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7773 */
7774 public int getHintScreenTimeout() {
7775 return mHintScreenTimeout;
7776 }
7777
Alex Hills9ab3a232016-04-05 14:54:56 -04007778 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007779 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7780 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7781 * qr codes, as well as other simple black-and-white tickets.
7782 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7783 * @return this object for method chaining
7784 */
7785 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7786 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7787 return this;
7788 }
7789
7790 /**
7791 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7792 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7793 * qr codes, as well as other simple black-and-white tickets.
7794 * @return {@code true} if it should be displayed in ambient, false otherwise
7795 * otherwise. The default value is {@code false} if this was never set.
7796 */
7797 public boolean getHintAmbientBigPicture() {
7798 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7799 }
7800
7801 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007802 * Set a hint that this notification's content intent will launch an {@link Activity}
7803 * directly, telling the platform that it can generate the appropriate transitions.
7804 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7805 * an activity and transitions should be generated, false otherwise.
7806 * @return this object for method chaining
7807 */
7808 public WearableExtender setHintContentIntentLaunchesActivity(
7809 boolean hintContentIntentLaunchesActivity) {
7810 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7811 return this;
7812 }
7813
7814 /**
7815 * Get a hint that this notification's content intent will launch an {@link Activity}
7816 * directly, telling the platform that it can generate the appropriate transitions
7817 * @return {@code true} if the content intent will launch an activity and transitions should
7818 * be generated, false otherwise. The default value is {@code false} if this was never set.
7819 */
7820 public boolean getHintContentIntentLaunchesActivity() {
7821 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7822 }
7823
Nadia Benbernou948627e2016-04-14 14:41:08 -04007824 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007825 * Sets the dismissal id for this notification. If a notification is posted with a
7826 * dismissal id, then when that notification is canceled, notifications on other wearables
7827 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007828 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007829 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007830 * @param dismissalId the dismissal id of the notification.
7831 * @return this object for method chaining
7832 */
7833 public WearableExtender setDismissalId(String dismissalId) {
7834 mDismissalId = dismissalId;
7835 return this;
7836 }
7837
7838 /**
7839 * Returns the dismissal id of the notification.
7840 * @return the dismissal id of the notification or null if it has not been set.
7841 */
7842 public String getDismissalId() {
7843 return mDismissalId;
7844 }
7845
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007846 /**
7847 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7848 * posted from a phone to provide finer-grained control on what notifications are bridged
7849 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7850 * Features to Notifications</a> for more information.
7851 * @param bridgeTag the bridge tag of the notification.
7852 * @return this object for method chaining
7853 */
7854 public WearableExtender setBridgeTag(String bridgeTag) {
7855 mBridgeTag = bridgeTag;
7856 return this;
7857 }
7858
7859 /**
7860 * Returns the bridge tag of the notification.
7861 * @return the bridge tag or null if not present.
7862 */
7863 public String getBridgeTag() {
7864 return mBridgeTag;
7865 }
7866
Griff Hazen61a9e862014-05-22 16:05:19 -07007867 private void setFlag(int mask, boolean value) {
7868 if (value) {
7869 mFlags |= mask;
7870 } else {
7871 mFlags &= ~mask;
7872 }
7873 }
7874 }
7875
7876 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007877 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7878 * with car extensions:
7879 *
7880 * <ol>
7881 * <li>Create an {@link Notification.Builder}, setting any desired
7882 * properties.
7883 * <li>Create a {@link CarExtender}.
7884 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7885 * {@link CarExtender}.
7886 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7887 * to apply the extensions to a notification.
7888 * </ol>
7889 *
7890 * <pre class="prettyprint">
7891 * Notification notification = new Notification.Builder(context)
7892 * ...
7893 * .extend(new CarExtender()
7894 * .set*(...))
7895 * .build();
7896 * </pre>
7897 *
7898 * <p>Car extensions can be accessed on an existing notification by using the
7899 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7900 * to access values.
7901 */
7902 public static final class CarExtender implements Extender {
7903 private static final String TAG = "CarExtender";
7904
7905 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7906 private static final String EXTRA_LARGE_ICON = "large_icon";
7907 private static final String EXTRA_CONVERSATION = "car_conversation";
7908 private static final String EXTRA_COLOR = "app_color";
7909
7910 private Bitmap mLargeIcon;
7911 private UnreadConversation mUnreadConversation;
7912 private int mColor = Notification.COLOR_DEFAULT;
7913
7914 /**
7915 * Create a {@link CarExtender} with default options.
7916 */
7917 public CarExtender() {
7918 }
7919
7920 /**
7921 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7922 *
7923 * @param notif The notification from which to copy options.
7924 */
7925 public CarExtender(Notification notif) {
7926 Bundle carBundle = notif.extras == null ?
7927 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7928 if (carBundle != null) {
7929 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7930 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7931
7932 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7933 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7934 }
7935 }
7936
7937 /**
7938 * Apply car extensions to a notification that is being built. This is typically called by
7939 * the {@link Notification.Builder#extend(Notification.Extender)}
7940 * method of {@link Notification.Builder}.
7941 */
7942 @Override
7943 public Notification.Builder extend(Notification.Builder builder) {
7944 Bundle carExtensions = new Bundle();
7945
7946 if (mLargeIcon != null) {
7947 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7948 }
7949 if (mColor != Notification.COLOR_DEFAULT) {
7950 carExtensions.putInt(EXTRA_COLOR, mColor);
7951 }
7952
7953 if (mUnreadConversation != null) {
7954 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7955 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7956 }
7957
7958 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7959 return builder;
7960 }
7961
7962 /**
7963 * Sets the accent color to use when Android Auto presents the notification.
7964 *
7965 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7966 * to accent the displayed notification. However, not all colors are acceptable in an
7967 * automotive setting. This method can be used to override the color provided in the
7968 * notification in such a situation.
7969 */
Tor Norbye80756e32015-03-02 09:39:27 -08007970 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007971 mColor = color;
7972 return this;
7973 }
7974
7975 /**
7976 * Gets the accent color.
7977 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007978 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007979 */
Tor Norbye80756e32015-03-02 09:39:27 -08007980 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007981 public int getColor() {
7982 return mColor;
7983 }
7984
7985 /**
7986 * Sets the large icon of the car notification.
7987 *
7988 * If no large icon is set in the extender, Android Auto will display the icon
7989 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7990 *
7991 * @param largeIcon The large icon to use in the car notification.
7992 * @return This object for method chaining.
7993 */
7994 public CarExtender setLargeIcon(Bitmap largeIcon) {
7995 mLargeIcon = largeIcon;
7996 return this;
7997 }
7998
7999 /**
8000 * Gets the large icon used in this car notification, or null if no icon has been set.
8001 *
8002 * @return The large icon for the car notification.
8003 * @see CarExtender#setLargeIcon
8004 */
8005 public Bitmap getLargeIcon() {
8006 return mLargeIcon;
8007 }
8008
8009 /**
8010 * Sets the unread conversation in a message notification.
8011 *
8012 * @param unreadConversation The unread part of the conversation this notification conveys.
8013 * @return This object for method chaining.
8014 */
8015 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8016 mUnreadConversation = unreadConversation;
8017 return this;
8018 }
8019
8020 /**
8021 * Returns the unread conversation conveyed by this notification.
8022 * @see #setUnreadConversation(UnreadConversation)
8023 */
8024 public UnreadConversation getUnreadConversation() {
8025 return mUnreadConversation;
8026 }
8027
8028 /**
8029 * A class which holds the unread messages from a conversation.
8030 */
8031 public static class UnreadConversation {
8032 private static final String KEY_AUTHOR = "author";
8033 private static final String KEY_TEXT = "text";
8034 private static final String KEY_MESSAGES = "messages";
8035 private static final String KEY_REMOTE_INPUT = "remote_input";
8036 private static final String KEY_ON_REPLY = "on_reply";
8037 private static final String KEY_ON_READ = "on_read";
8038 private static final String KEY_PARTICIPANTS = "participants";
8039 private static final String KEY_TIMESTAMP = "timestamp";
8040
8041 private final String[] mMessages;
8042 private final RemoteInput mRemoteInput;
8043 private final PendingIntent mReplyPendingIntent;
8044 private final PendingIntent mReadPendingIntent;
8045 private final String[] mParticipants;
8046 private final long mLatestTimestamp;
8047
8048 UnreadConversation(String[] messages, RemoteInput remoteInput,
8049 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8050 String[] participants, long latestTimestamp) {
8051 mMessages = messages;
8052 mRemoteInput = remoteInput;
8053 mReadPendingIntent = readPendingIntent;
8054 mReplyPendingIntent = replyPendingIntent;
8055 mParticipants = participants;
8056 mLatestTimestamp = latestTimestamp;
8057 }
8058
8059 /**
8060 * Gets the list of messages conveyed by this notification.
8061 */
8062 public String[] getMessages() {
8063 return mMessages;
8064 }
8065
8066 /**
8067 * Gets the remote input that will be used to convey the response to a message list, or
8068 * null if no such remote input exists.
8069 */
8070 public RemoteInput getRemoteInput() {
8071 return mRemoteInput;
8072 }
8073
8074 /**
8075 * Gets the pending intent that will be triggered when the user replies to this
8076 * notification.
8077 */
8078 public PendingIntent getReplyPendingIntent() {
8079 return mReplyPendingIntent;
8080 }
8081
8082 /**
8083 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8084 * in this object's message list.
8085 */
8086 public PendingIntent getReadPendingIntent() {
8087 return mReadPendingIntent;
8088 }
8089
8090 /**
8091 * Gets the participants in the conversation.
8092 */
8093 public String[] getParticipants() {
8094 return mParticipants;
8095 }
8096
8097 /**
8098 * Gets the firs participant in the conversation.
8099 */
8100 public String getParticipant() {
8101 return mParticipants.length > 0 ? mParticipants[0] : null;
8102 }
8103
8104 /**
8105 * Gets the timestamp of the conversation.
8106 */
8107 public long getLatestTimestamp() {
8108 return mLatestTimestamp;
8109 }
8110
8111 Bundle getBundleForUnreadConversation() {
8112 Bundle b = new Bundle();
8113 String author = null;
8114 if (mParticipants != null && mParticipants.length > 1) {
8115 author = mParticipants[0];
8116 }
8117 Parcelable[] messages = new Parcelable[mMessages.length];
8118 for (int i = 0; i < messages.length; i++) {
8119 Bundle m = new Bundle();
8120 m.putString(KEY_TEXT, mMessages[i]);
8121 m.putString(KEY_AUTHOR, author);
8122 messages[i] = m;
8123 }
8124 b.putParcelableArray(KEY_MESSAGES, messages);
8125 if (mRemoteInput != null) {
8126 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8127 }
8128 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8129 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8130 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8131 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8132 return b;
8133 }
8134
8135 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8136 if (b == null) {
8137 return null;
8138 }
8139 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8140 String[] messages = null;
8141 if (parcelableMessages != null) {
8142 String[] tmp = new String[parcelableMessages.length];
8143 boolean success = true;
8144 for (int i = 0; i < tmp.length; i++) {
8145 if (!(parcelableMessages[i] instanceof Bundle)) {
8146 success = false;
8147 break;
8148 }
8149 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8150 if (tmp[i] == null) {
8151 success = false;
8152 break;
8153 }
8154 }
8155 if (success) {
8156 messages = tmp;
8157 } else {
8158 return null;
8159 }
8160 }
8161
8162 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8163 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8164
8165 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8166
8167 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8168 if (participants == null || participants.length != 1) {
8169 return null;
8170 }
8171
8172 return new UnreadConversation(messages,
8173 remoteInput,
8174 onReply,
8175 onRead,
8176 participants, b.getLong(KEY_TIMESTAMP));
8177 }
8178 };
8179
8180 /**
8181 * Builder class for {@link CarExtender.UnreadConversation} objects.
8182 */
8183 public static class Builder {
8184 private final List<String> mMessages = new ArrayList<String>();
8185 private final String mParticipant;
8186 private RemoteInput mRemoteInput;
8187 private PendingIntent mReadPendingIntent;
8188 private PendingIntent mReplyPendingIntent;
8189 private long mLatestTimestamp;
8190
8191 /**
8192 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8193 *
8194 * @param name The name of the other participant in the conversation.
8195 */
8196 public Builder(String name) {
8197 mParticipant = name;
8198 }
8199
8200 /**
8201 * Appends a new unread message to the list of messages for this conversation.
8202 *
8203 * The messages should be added from oldest to newest.
8204 *
8205 * @param message The text of the new unread message.
8206 * @return This object for method chaining.
8207 */
8208 public Builder addMessage(String message) {
8209 mMessages.add(message);
8210 return this;
8211 }
8212
8213 /**
8214 * Sets the pending intent and remote input which will convey the reply to this
8215 * notification.
8216 *
8217 * @param pendingIntent The pending intent which will be triggered on a reply.
8218 * @param remoteInput The remote input parcelable which will carry the reply.
8219 * @return This object for method chaining.
8220 *
8221 * @see CarExtender.UnreadConversation#getRemoteInput
8222 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8223 */
8224 public Builder setReplyAction(
8225 PendingIntent pendingIntent, RemoteInput remoteInput) {
8226 mRemoteInput = remoteInput;
8227 mReplyPendingIntent = pendingIntent;
8228
8229 return this;
8230 }
8231
8232 /**
8233 * Sets the pending intent that will be sent once the messages in this notification
8234 * are read.
8235 *
8236 * @param pendingIntent The pending intent to use.
8237 * @return This object for method chaining.
8238 */
8239 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8240 mReadPendingIntent = pendingIntent;
8241 return this;
8242 }
8243
8244 /**
8245 * Sets the timestamp of the most recent message in an unread conversation.
8246 *
8247 * If a messaging notification has been posted by your application and has not
8248 * yet been cancelled, posting a later notification with the same id and tag
8249 * but without a newer timestamp may result in Android Auto not displaying a
8250 * heads up notification for the later notification.
8251 *
8252 * @param timestamp The timestamp of the most recent message in the conversation.
8253 * @return This object for method chaining.
8254 */
8255 public Builder setLatestTimestamp(long timestamp) {
8256 mLatestTimestamp = timestamp;
8257 return this;
8258 }
8259
8260 /**
8261 * Builds a new unread conversation object.
8262 *
8263 * @return The new unread conversation object.
8264 */
8265 public UnreadConversation build() {
8266 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8267 String[] participants = { mParticipant };
8268 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8269 mReadPendingIntent, participants, mLatestTimestamp);
8270 }
8271 }
8272 }
8273
8274 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008275 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8276 * with a TV extension:
8277 *
8278 * <ol>
8279 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8280 * <li>Create a {@link TvExtender}.
8281 * <li>Set TV-specific properties using the {@code set} methods of
8282 * {@link TvExtender}.
8283 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8284 * to apply the extension to a notification.
8285 * </ol>
8286 *
8287 * <pre class="prettyprint">
8288 * Notification notification = new Notification.Builder(context)
8289 * ...
8290 * .extend(new TvExtender()
8291 * .set*(...))
8292 * .build();
8293 * </pre>
8294 *
8295 * <p>TV extensions can be accessed on an existing notification by using the
8296 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8297 * to access values.
8298 *
8299 * @hide
8300 */
8301 @SystemApi
8302 public static final class TvExtender implements Extender {
8303 private static final String TAG = "TvExtender";
8304
8305 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8306 private static final String EXTRA_FLAGS = "flags";
8307 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8308 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008309 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008310
8311 // Flags bitwise-ored to mFlags
8312 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8313
8314 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008315 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008316 private PendingIntent mContentIntent;
8317 private PendingIntent mDeleteIntent;
8318
8319 /**
8320 * Create a {@link TvExtender} with default options.
8321 */
8322 public TvExtender() {
8323 mFlags = FLAG_AVAILABLE_ON_TV;
8324 }
8325
8326 /**
8327 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8328 *
8329 * @param notif The notification from which to copy options.
8330 */
8331 public TvExtender(Notification notif) {
8332 Bundle bundle = notif.extras == null ?
8333 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8334 if (bundle != null) {
8335 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008336 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008337 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8338 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8339 }
8340 }
8341
8342 /**
8343 * Apply a TV extension to a notification that is being built. This is typically called by
8344 * the {@link Notification.Builder#extend(Notification.Extender)}
8345 * method of {@link Notification.Builder}.
8346 */
8347 @Override
8348 public Notification.Builder extend(Notification.Builder builder) {
8349 Bundle bundle = new Bundle();
8350
8351 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008352 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008353 if (mContentIntent != null) {
8354 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8355 }
8356
8357 if (mDeleteIntent != null) {
8358 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8359 }
8360
8361 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8362 return builder;
8363 }
8364
8365 /**
8366 * Returns true if this notification should be shown on TV. This method return true
8367 * if the notification was extended with a TvExtender.
8368 */
8369 public boolean isAvailableOnTv() {
8370 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8371 }
8372
8373 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008374 * Specifies the channel the notification should be delivered on when shown on TV.
8375 * It can be different from the channel that the notification is delivered to when
8376 * posting on a non-TV device.
8377 */
8378 public TvExtender setChannel(String channelId) {
8379 mChannelId = channelId;
8380 return this;
8381 }
8382
8383 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008384 * Specifies the channel the notification should be delivered on when shown on TV.
8385 * It can be different from the channel that the notification is delivered to when
8386 * posting on a non-TV device.
8387 */
8388 public TvExtender setChannelId(String channelId) {
8389 mChannelId = channelId;
8390 return this;
8391 }
8392
Jeff Sharkey000ce802017-04-29 13:13:27 -06008393 /** @removed */
8394 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008395 public String getChannel() {
8396 return mChannelId;
8397 }
8398
8399 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008400 * Returns the id of the channel this notification posts to on TV.
8401 */
8402 public String getChannelId() {
8403 return mChannelId;
8404 }
8405
8406 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008407 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8408 * If provided, it is used instead of the content intent specified
8409 * at the level of Notification.
8410 */
8411 public TvExtender setContentIntent(PendingIntent intent) {
8412 mContentIntent = intent;
8413 return this;
8414 }
8415
8416 /**
8417 * Returns the TV-specific content intent. If this method returns null, the
8418 * main content intent on the notification should be used.
8419 *
8420 * @see {@link Notification#contentIntent}
8421 */
8422 public PendingIntent getContentIntent() {
8423 return mContentIntent;
8424 }
8425
8426 /**
8427 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8428 * by the user on TV. If provided, it is used instead of the delete intent specified
8429 * at the level of Notification.
8430 */
8431 public TvExtender setDeleteIntent(PendingIntent intent) {
8432 mDeleteIntent = intent;
8433 return this;
8434 }
8435
8436 /**
8437 * Returns the TV-specific delete intent. If this method returns null, the
8438 * main delete intent on the notification should be used.
8439 *
8440 * @see {@link Notification#deleteIntent}
8441 */
8442 public PendingIntent getDeleteIntent() {
8443 return mDeleteIntent;
8444 }
8445 }
8446
8447 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008448 * Get an array of Notification objects from a parcelable array bundle field.
8449 * Update the bundle to have a typed array so fetches in the future don't need
8450 * to do an array copy.
8451 */
8452 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8453 Parcelable[] array = bundle.getParcelableArray(key);
8454 if (array instanceof Notification[] || array == null) {
8455 return (Notification[]) array;
8456 }
8457 Notification[] typedArray = Arrays.copyOf(array, array.length,
8458 Notification[].class);
8459 bundle.putParcelableArray(key, typedArray);
8460 return typedArray;
8461 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008462
8463 private static class BuilderRemoteViews extends RemoteViews {
8464 public BuilderRemoteViews(Parcel parcel) {
8465 super(parcel);
8466 }
8467
Kenny Guy77320062014-08-27 21:37:15 +01008468 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8469 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008470 }
8471
8472 @Override
8473 public BuilderRemoteViews clone() {
8474 Parcel p = Parcel.obtain();
8475 writeToParcel(p, 0);
8476 p.setDataPosition(0);
8477 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8478 p.recycle();
8479 return brv;
8480 }
8481 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008482
8483 private static class StandardTemplateParams {
8484 boolean hasProgress = true;
8485 boolean ambient = false;
8486 CharSequence title;
8487 CharSequence text;
8488
8489 final StandardTemplateParams reset() {
8490 hasProgress = true;
8491 ambient = false;
8492 title = null;
8493 text = null;
8494 return this;
8495 }
8496
8497 final StandardTemplateParams hasProgress(boolean hasProgress) {
8498 this.hasProgress = hasProgress;
8499 return this;
8500 }
8501
8502 final StandardTemplateParams title(CharSequence title) {
8503 this.title = title;
8504 return this;
8505 }
8506
8507 final StandardTemplateParams text(CharSequence text) {
8508 this.text = text;
8509 return this;
8510 }
8511
8512 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008513 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008514 this.ambient = ambient;
8515 return this;
8516 }
8517
8518 final StandardTemplateParams fillTextsFrom(Builder b) {
8519 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008520 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8521
8522 // Big text notifications should contain their content when viewed in ambient mode.
8523 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8524 if (!ambient || TextUtils.isEmpty(text)) {
8525 text = extras.getCharSequence(EXTRA_TEXT);
8526 }
8527 this.text = b.processLegacyText(text, ambient);
8528
Adrian Roos70d7aa32017-01-11 15:39:06 -08008529 return this;
8530 }
8531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008532}