blob: e31e73f145af9ad9f000a6ed2f61acdbfa671dfa [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;
Joe Onoratoef1e7762010-09-17 18:38:38 -040036import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010037import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070038import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010039import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040041import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040042import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070043import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080044import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070045import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040047import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020048import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050049import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070050import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Parcel;
52import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040053import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080054import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070055import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070056import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080057import android.text.SpannableStringBuilder;
58import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070061import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080062import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070063import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080064import android.text.style.RelativeSizeSpan;
65import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070066import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040067import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050068import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070069import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080070import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080071import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070072import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070073import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.RemoteViews;
75
Griff Hazen959591e2014-05-15 22:26:18 -070076import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070077import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070078import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070079import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080080import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070081
Tor Norbyed9273d62013-05-30 15:59:53 -070082import java.lang.annotation.Retention;
83import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020084import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070086import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070087import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070088import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050089import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091/**
92 * A class that represents how a persistent notification is to be presented to
93 * the user using the {@link android.app.NotificationManager}.
94 *
Joe Onoratocb109a02011-01-18 17:57:41 -080095 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
96 * easier to construct Notifications.</p>
97 *
Joe Fernandez558459f2011-10-13 16:47:36 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For a guide to creating notifications, read the
101 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
102 * developer guide.</p>
103 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 */
105public class Notification implements Parcelable
106{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400107 private static final String TAG = "Notification";
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400110 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400111 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400112 */
113 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
114 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
115 = "android.intent.category.NOTIFICATION_PREFERENCES";
116
117 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500118 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
119 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 */
122 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
123
124 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
126 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
127 * that can be used to narrow down what settings should be shown in the target app.
128 */
129 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
130
131 /**
132 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
133 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
134 * that can be used to narrow down what settings should be shown in the target app.
135 */
136 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
137
138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * Use all default values (where applicable).
140 */
141 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Use the default notification sound. This will ignore any given
145 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500146 *
Chris Wren47c20a12014-06-18 17:27:29 -0400147 * <p>
148 * A notification that is noisy is more likely to be presented as a heads-up notification.
149 * </p>
150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152 */
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public static final int DEFAULT_SOUND = 1;
155
156 /**
157 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700159 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 *
Chris Wren47c20a12014-06-18 17:27:29 -0400161 * <p>
162 * A notification that vibrates is more likely to be presented as a heads-up notification.
163 * </p>
164 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500166 */
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 /**
171 * Use the default notification lights. This will ignore the
172 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
173 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200181 * Maximum length of CharSequences accepted by Builder and friends.
182 *
183 * <p>
184 * Avoids spamming the system with overly large strings such as full e-mails.
185 */
186 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
187
188 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800189 * Maximum entries of reply text that are accepted by Builder and friends.
190 */
191 private static final int MAX_REPLY_HISTORY = 5;
192
193 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500194 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800195 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 * Default value: {@link System#currentTimeMillis() Now}.
197 *
198 * Choose a timestamp that will be most relevant to the user. For most finite events, this
199 * corresponds to the time the event happened (or will happen, in the case of events that have
200 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800201 * timestamped according to when the activity began.
202 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500203 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * <ul>
206 * <li>Notification of a new chat message should be stamped when the message was received.</li>
207 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
208 * <li>Notification of a completed file download should be stamped when the download finished.</li>
209 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
210 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
211 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800212 * </ul>
213 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700214 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
215 * anymore by default and must be opted into by using
216 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 */
218 public long when;
219
220 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700221 * The creation time of the notification
222 */
223 private long creationTime;
224
225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400227 *
228 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Dan Sandler86647982015-05-13 23:41:13 -0400230 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700231 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public int icon;
233
234 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
236 * leave it at its default value of 0.
237 *
238 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700239 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800240 */
241 public int iconLevel;
242
243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500244 * The number of events that this notification represents. For example, in a new mail
245 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800246 *
Julia Reynolds30331982017-04-27 10:12:50 -0400247 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500248 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
249 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Julia Reynolds30331982017-04-27 10:12:50 -0400251 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 /**
254 * The intent to execute when the expanded status entry is clicked. If
255 * this is an activity, it must include the
256 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800257 * that you take care of task management as described in the
258 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800259 * Stack</a> document. In particular, make sure to read the notification section
260 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
261 * Notifications</a> for the correct ways to launch an application from a
262 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
264 public PendingIntent contentIntent;
265
266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500267 * The intent to execute when the notification is explicitly dismissed by the user, either with
268 * the "Clear All" button or by swiping it away individually.
269 *
270 * This probably shouldn't be launching an activity since several of those will be sent
271 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public PendingIntent deleteIntent;
274
275 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700276 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800277 *
Chris Wren47c20a12014-06-18 17:27:29 -0400278 * <p>
279 * The system UI may choose to display a heads-up notification, instead of
280 * launching this intent, while the user is using the device.
281 * </p>
282 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800283 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400284 */
285 public PendingIntent fullScreenIntent;
286
287 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400288 * Text that summarizes this notification for accessibility services.
289 *
290 * As of the L release, this text is no longer shown on screen, but it is still useful to
291 * accessibility services (where it serves as an audible announcement of the notification's
292 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400293 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800294 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public CharSequence tickerText;
297
298 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400299 * Formerly, a view showing the {@link #tickerText}.
300 *
301 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400302 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400303 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400305
306 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 * The view that will represent this notification in the notification list (which is pulled
308 * down from the status bar).
309 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500310 * As of N, this field may be null. The notification view is determined by the inputs
311 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 public RemoteViews contentView;
316
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400317 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400318 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 * opportunity to show more detail. The system UI may choose to show this
320 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500322 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
324 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400325 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400326 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 public RemoteViews bigContentView;
328
Chris Wren8fd39ec2014-02-27 17:43:26 -0500329
330 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A medium-format version of {@link #contentView}, providing the Notification an
332 * opportunity to add action buttons to contentView. At its discretion, the system UI may
333 * choose to show this as a heads-up notification, which will pop up so the user can see
334 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500336 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
338 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400340 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500341 public RemoteViews headsUpContentView;
342
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400343 /**
Dan Sandler86647982015-05-13 23:41:13 -0400344 * A large bitmap to be shown in the notification content area.
345 *
346 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Dan Sandler86647982015-05-13 23:41:13 -0400348 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800349 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
352 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400355 * A notification that is noisy is more likely to be presented as a heads-up notification.
356 * </p>
357 *
358 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 public Uri sound;
365
366 /**
367 * Use this constant as the value for audioStreamType to request that
368 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700369 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400370 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public static final int STREAM_DEFAULT = -1;
375
376 /**
377 * The audio stream type to use when playing the sound.
378 * Should be one of the STREAM_ constants from
379 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
381 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int audioStreamType = STREAM_DEFAULT;
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400387 * The default value of {@link #audioAttributes}.
388 */
389 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
390 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
391 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
392 .build();
393
394 /**
395 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500396 *
397 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400400 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
401
402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 * The pattern with which to vibrate.
404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * <p>
406 * To vibrate the default pattern, see {@link #defaults}.
407 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public long[] vibrate;
414
415 /**
416 * The color of the led. The hardware will do its best approximation.
417 *
418 * @see #FLAG_SHOW_LIGHTS
419 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Tor Norbye80756e32015-03-02 09:39:27 -0800422 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public int ledARGB;
425
426 /**
427 * The number of milliseconds for the LED to be on while it's flashing.
428 * The hardware will do its best approximation.
429 *
430 * @see #FLAG_SHOW_LIGHTS
431 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public int ledOnMS;
436
437 /**
438 * The number of milliseconds for the LED to be off while it's flashing.
439 * The hardware will do its best approximation.
440 *
441 * @see #FLAG_SHOW_LIGHTS
442 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public int ledOffMS;
448
449 /**
450 * Specifies which values should be taken from the defaults.
451 * <p>
452 * To set, OR the desired from {@link #DEFAULT_SOUND},
453 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
454 * values, use {@link #DEFAULT_ALL}.
455 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 *
457 * @deprecated use {@link NotificationChannel#getSound()} and
458 * {@link NotificationChannel#shouldShowLights()} and
459 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public int defaults;
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if you want the LED on for this notification.
467 * <ul>
468 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
469 * or 0 for both ledOnMS and ledOffMS.</li>
470 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
471 * <li>To flash the LED, pass the number of milliseconds that it should
472 * be on and off to ledOnMS and ledOffMS.</li>
473 * </ul>
474 * <p>
475 * Since hardware varies, you are not guaranteed that any of the values
476 * you pass are honored exactly. Use the system defaults (TODO) if possible
477 * because they will be set to values that work on any given hardware.
478 * <p>
479 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500480 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
485
486 /**
487 * Bit to be bitwise-ored into the {@link #flags} field that should be
488 * set if this notification is in reference to something that is ongoing,
489 * like a phone call. It should not be set if this notification is in
490 * reference to something that happened at a particular point in time,
491 * like a missed phone call.
492 */
493 public static final int FLAG_ONGOING_EVENT = 0x00000002;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700497 * the audio will be repeated until the notification is
498 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
500 public static final int FLAG_INSISTENT = 0x00000004;
501
502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700504 * set if you would only like the sound, vibrate and ticker to be played
505 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
508
509 /**
510 * Bit to be bitwise-ored into the {@link #flags} field that should be
511 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500512 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
514 public static final int FLAG_AUTO_CANCEL = 0x00000010;
515
516 /**
517 * Bit to be bitwise-ored into the {@link #flags} field that should be
518 * set if the notification should not be canceled when the user clicks
519 * the Clear all button.
520 */
521 public static final int FLAG_NO_CLEAR = 0x00000020;
522
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700523 /**
524 * Bit to be bitwise-ored into the {@link #flags} field that should be
525 * set if this notification represents a currently running service. This
526 * will normally be set for you by {@link Service#startForeground}.
527 */
528 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
529
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400534 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700535 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400537
Griff Hazendfcb0802014-02-11 12:00:00 -0800538 /**
539 * Bit to be bitswise-ored into the {@link #flags} field that should be
540 * set if this notification is relevant to the current device only
541 * and it is not recommended that it bridge to other devices.
542 */
543 public static final int FLAG_LOCAL_ONLY = 0x00000100;
544
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700545 /**
546 * Bit to be bitswise-ored into the {@link #flags} field that should be
547 * set if this notification is the group summary for a group of notifications.
548 * Grouped notifications may display in a cluster or stack on devices which
549 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
550 */
551 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
552
Julia Reynoldse46bb372016-03-17 11:05:58 -0400553 /**
554 * Bit to be bitswise-ored into the {@link #flags} field that should be
555 * set if this notification is the group summary for an auto-group of notifications.
556 *
557 * @hide
558 */
559 @SystemApi
560 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
561
Julia Reynoldsd71c5a92017-06-30 13:34:01 -0400562 /**
563 * @hide
564 */
565 public static final int FLAG_CAN_COLORIZE = 0x00000800;
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public int flags;
568
Tor Norbyed9273d62013-05-30 15:59:53 -0700569 /** @hide */
570 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
571 @Retention(RetentionPolicy.SOURCE)
572 public @interface Priority {}
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 * Default notification {@link #priority}. If your application does not prioritize its own
576 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500577 *
578 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500580 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 public static final int PRIORITY_DEFAULT = 0;
582
583 /**
584 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
585 * items smaller, or at a different position in the list, compared with your app's
586 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500587 *
588 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500590 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 public static final int PRIORITY_LOW = -1;
592
593 /**
594 * Lowest {@link #priority}; these items might not be shown to the user except under special
595 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 *
597 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500599 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 public static final int PRIORITY_MIN = -2;
601
602 /**
603 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
604 * show these items larger, or at a different position in notification lists, compared with
605 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 *
607 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500609 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 public static final int PRIORITY_HIGH = 1;
611
612 /**
613 * Highest {@link #priority}, for your application's most important items that require the
614 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 *
616 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500618 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 public static final int PRIORITY_MAX = 2;
620
621 /**
622 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800623 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 * Priority is an indication of how much of the user's valuable attention should be consumed by
625 * this notification. Low-priority notifications may be hidden from the user in certain
626 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500627 * system will make a determination about how to interpret this priority when presenting
628 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400629 *
630 * <p>
631 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
632 * as a heads-up notification.
633 * </p>
634 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700637 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500638 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800640
Dan Sandler26e81cf2014-05-06 10:01:27 -0400641 /**
642 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
643 * to be applied by the standard Style templates when presenting this notification.
644 *
645 * The current template design constructs a colorful header image by overlaying the
646 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
647 * ignored.
648 */
Tor Norbye80756e32015-03-02 09:39:27 -0800649 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400650 public int color = COLOR_DEFAULT;
651
652 /**
653 * Special value of {@link #color} telling the system not to decorate this notification with
654 * any special color but instead use default colors when presenting this notification.
655 */
Tor Norbye80756e32015-03-02 09:39:27 -0800656 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400657 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600658
659 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800660 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800661 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800662 */
663 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800664 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800665
666 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700667 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
668 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600669 * lockscreen).
670 *
671 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
672 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
673 * shown in all situations, but the contents are only available if the device is unlocked for
674 * the appropriate user.
675 *
676 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
677 * can be read even in an "insecure" context (that is, above a secure lockscreen).
678 * To modify the public version of this notification—for example, to redact some portions—see
679 * {@link Builder#setPublicVersion(Notification)}.
680 *
681 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
682 * and ticker until the user has bypassed the lockscreen.
683 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600684 public @Visibility int visibility;
685
686 /** @hide */
687 @IntDef(prefix = { "VISIBILITY_" }, value = {
688 VISIBILITY_PUBLIC,
689 VISIBILITY_PRIVATE,
690 VISIBILITY_SECRET,
691 })
692 @Retention(RetentionPolicy.SOURCE)
693 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600694
Griff Hazenfc3922d2014-08-20 11:56:44 -0700695 /**
696 * Notification visibility: Show this notification in its entirety on all lockscreens.
697 *
698 * {@see #visibility}
699 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600700 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700701
702 /**
703 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
704 * private information on secure lockscreens.
705 *
706 * {@see #visibility}
707 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600708 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700709
710 /**
711 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
712 *
713 * {@see #visibility}
714 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600715 public static final int VISIBILITY_SECRET = -1;
716
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500717 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400718 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
722 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400723 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500724 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
727 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400728 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500729 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731
732 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400733 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500734 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736
737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400738 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500739 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500741
742 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400743 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500744 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400745 public static final String CATEGORY_ALARM = "alarm";
746
747 /**
748 * Notification category: progress of a long-running background operation.
749 */
750 public static final String CATEGORY_PROGRESS = "progress";
751
752 /**
753 * Notification category: social network or sharing update.
754 */
755 public static final String CATEGORY_SOCIAL = "social";
756
757 /**
758 * Notification category: error in background operation or authentication status.
759 */
760 public static final String CATEGORY_ERROR = "err";
761
762 /**
763 * Notification category: media transport control for playback.
764 */
765 public static final String CATEGORY_TRANSPORT = "transport";
766
767 /**
768 * Notification category: system or device status update. Reserved for system use.
769 */
770 public static final String CATEGORY_SYSTEM = "sys";
771
772 /**
773 * Notification category: indication of running background service.
774 */
775 public static final String CATEGORY_SERVICE = "service";
776
777 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400778 * Notification category: a specific, timely recommendation for a single thing.
779 * For example, a news app might want to recommend a news story it believes the user will
780 * want to read next.
781 */
782 public static final String CATEGORY_RECOMMENDATION = "recommendation";
783
784 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 * Notification category: ongoing information about device or contextual status.
786 */
787 public static final String CATEGORY_STATUS = "status";
788
789 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400790 * Notification category: user-scheduled reminder.
791 */
792 public static final String CATEGORY_REMINDER = "reminder";
793
794 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
796 * that best describes this Notification. May be used by the system for ranking and filtering.
797 */
798 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700800 private String mGroupKey;
801
802 /**
803 * Get the key used to group this notification into a cluster or stack
804 * with other notifications on devices which support such rendering.
805 */
806 public String getGroup() {
807 return mGroupKey;
808 }
809
810 private String mSortKey;
811
812 /**
813 * Get a sort key that orders this notification among other notifications from the
814 * same package. This can be useful if an external sort was already applied and an app
815 * would like to preserve this. Notifications will be sorted lexicographically using this
816 * value, although providing different priorities in addition to providing sort key may
817 * cause this value to be ignored.
818 *
819 * <p>This sort key can also be used to order members of a notification group. See
820 * {@link Builder#setGroup}.
821 *
822 * @see String#compareTo(String)
823 */
824 public String getSortKey() {
825 return mSortKey;
826 }
827
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500828 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830 * <p>
831 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
832 * APIs, and are intended to be used by
833 * {@link android.service.notification.NotificationListenerService} implementations to extract
834 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500835 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400836 public Bundle extras = new Bundle();
837
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700839 * All pending intents in the notification as the system needs to be able to access them but
840 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700841 * custom parcelable objects.
842 *
843 * @hide
844 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700845 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700846
847 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700848 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
849 * pending intents inside of it, so only those will get the behavior.
850 *
851 * @hide
852 */
853 static public IBinder whitelistToken;
854
855 /**
856 * Must be set by a process to start associating tokens with Notification objects
857 * coming in to it. This is set by NotificationManagerService.
858 *
859 * @hide
860 */
861 static public IBinder processWhitelistToken;
862
863 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 * {@link #extras} key: this is the title of the notification,
865 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
866 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500867 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400868
869 /**
870 * {@link #extras} key: this is the title of the notification when shown in expanded form,
871 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
872 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400873 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874
875 /**
876 * {@link #extras} key: this is the main text payload, as supplied to
877 * {@link Builder#setContentText(CharSequence)}.
878 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500879 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880
881 /**
882 * {@link #extras} key: this is a third line of text, as supplied to
883 * {@link Builder#setSubText(CharSequence)}.
884 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400885 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400886
887 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800888 * {@link #extras} key: this is the remote input history, as supplied to
889 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700890 *
891 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
892 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
893 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
894 * notifications once the other party has responded).
895 *
896 * The extra with this key is of type CharSequence[] and contains the most recent entry at
897 * the 0 index, the second most recent at the 1 index, etc.
898 *
899 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800900 */
901 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
902
903 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400904 * {@link #extras} key: this is a small piece of additional text as supplied to
905 * {@link Builder#setContentInfo(CharSequence)}.
906 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400907 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400908
909 /**
910 * {@link #extras} key: this is a line of summary information intended to be shown
911 * alongside expanded notifications, as supplied to (e.g.)
912 * {@link BigTextStyle#setSummaryText(CharSequence)}.
913 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915
916 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200917 * {@link #extras} key: this is the longer text shown in the big form of a
918 * {@link BigTextStyle} notification, as supplied to
919 * {@link BigTextStyle#bigText(CharSequence)}.
920 */
921 public static final String EXTRA_BIG_TEXT = "android.bigText";
922
923 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
925 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400926 *
927 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400929 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500930 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
933 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
934 * notification payload, as
935 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400936 *
937 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400938 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400939 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
943 * {@link #extras} key: this is a bitmap to be used instead of the one from
944 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
945 * shown in its expanded form, as supplied to
946 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
947 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400948 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949
950 /**
951 * {@link #extras} key: this is the progress value supplied to
952 * {@link Builder#setProgress(int, int, boolean)}.
953 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400954 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400955
956 /**
957 * {@link #extras} key: this is the maximum value supplied to
958 * {@link Builder#setProgress(int, int, boolean)}.
959 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400960 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400961
962 /**
963 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
964 * {@link Builder#setProgress(int, int, boolean)}.
965 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
969 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
970 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
971 * {@link Builder#setUsesChronometer(boolean)}.
972 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400973 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974
975 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800976 * {@link #extras} key: whether the chronometer set on the notification should count down
977 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700978 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800979 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700980 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800981
982 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983 * {@link #extras} key: whether {@link #when} should be shown,
984 * as supplied to {@link Builder#setShowWhen(boolean)}.
985 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400986 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400987
988 /**
989 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
990 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
991 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400992 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993
994 /**
995 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
996 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
997 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400998 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400999
1000 /**
1001 * {@link #extras} key: A string representing the name of the specific
1002 * {@link android.app.Notification.Style} used to create this notification.
1003 */
Chris Wren91ad5632013-06-05 15:05:57 -04001004 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001005
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001006 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001007 * {@link #extras} key: A String array containing the people that this notification relates to,
1008 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001010 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001011
1012 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001013 * Allow certain system-generated notifications to appear before the device is provisioned.
1014 * Only available to notifications coming from the android package.
1015 * @hide
1016 */
Maurice Lam96c10032017-03-29 15:42:38 -07001017 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001018 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001019 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1020
1021 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001022 * {@link #extras} key: A
1023 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1024 * in the background when the notification is selected. The URI must point to an image stream
1025 * suitable for passing into
1026 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1027 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1028 * URI used for this purpose must require no permissions to read the image data.
1029 */
1030 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1031
1032 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001033 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001034 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001035 * {@link android.app.Notification.MediaStyle} notification.
1036 */
1037 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1038
1039 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001040 * {@link #extras} key: the indices of actions to be shown in the compact view,
1041 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1042 */
1043 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1044
Christoph Studer943aa672014-08-03 20:31:16 +02001045 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001046 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1047 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001048 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1049 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001050 */
1051 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1052
1053 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001054 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001055 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001056 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001057 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001058
1059 /**
1060 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1061 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001062 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1063 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001064 */
1065 public static final String EXTRA_MESSAGES = "android.messages";
1066
1067 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001068 * {@link #extras} key: an array of
1069 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1070 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1071 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1072 * array of bundles.
1073 */
1074 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1075
1076 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001077 * {@link #extras} key: whether the notification should be colorized as
1078 * supplied to {@link Builder#setColorized(boolean)}}.
1079 */
1080 public static final String EXTRA_COLORIZED = "android.colorized";
1081
1082 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001083 * @hide
1084 */
1085 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1086
Selim Cinek247fa012016-02-18 09:50:48 -08001087 /**
1088 * @hide
1089 */
1090 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1091
Shane Brennan472a3b32016-12-12 15:28:10 -08001092 /**
1093 * {@link #extras} key: the audio contents of this notification.
1094 *
1095 * This is for use when rendering the notification on an audio-focused interface;
1096 * the audio contents are a complete sound sample that contains the contents/body of the
1097 * notification. This may be used in substitute of a Text-to-Speech reading of the
1098 * notification. For example if the notification represents a voice message this should point
1099 * to the audio of that message.
1100 *
1101 * The data stored under this key should be a String representation of a Uri that contains the
1102 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1103 *
1104 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1105 * has a field for holding data URI. That field can be used for audio.
1106 * See {@code Message#setData}.
1107 *
1108 * Example usage:
1109 * <pre>
1110 * {@code
1111 * Notification.Builder myBuilder = (build your Notification as normal);
1112 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1113 * }
1114 * </pre>
1115 */
1116 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1117
Dan Sandler80eaa592016-04-14 23:34:54 -04001118 /** @hide */
1119 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001120 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001121 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1122
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001123 /**
1124 * This is set on the notification shown by the activity manager about all apps
1125 * running in the background. It indicates that the notification should be shown
1126 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1127 * notification currently visible to the user. This is a string array of all
1128 * package names of the apps.
1129 * @hide
1130 */
1131 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1132
Dan Sandlerd63f9322015-05-06 15:18:49 -04001133 private Icon mSmallIcon;
1134 private Icon mLargeIcon;
1135
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001136 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001137 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001138
Julia Reynolds13d898c2017-02-02 12:22:05 -05001139 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001140 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001141
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001142 /** @hide */
1143 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1144 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1145 })
1146 @Retention(RetentionPolicy.SOURCE)
1147 public @interface GroupAlertBehavior {}
1148
1149 /**
1150 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1151 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1152 * notification will not be muted when it is in a group.
1153 */
1154 public static final int GROUP_ALERT_ALL = 0;
1155
1156 /**
1157 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1158 * notification in a group should be silenced (no sound or vibration) even if they are posted
1159 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1160 * mute this notification if this notification is a group child.
1161 *
1162 * <p> For example, you might want to use this constant if you post a number of children
1163 * notifications at once (say, after a periodic sync), and only need to notify the user
1164 * audibly once.
1165 */
1166 public static final int GROUP_ALERT_SUMMARY = 1;
1167
1168 /**
1169 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1170 * notification in a group should be silenced (no sound or vibration) even if they are
1171 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1172 * to mute this notification if this notification is a group summary.
1173 *
1174 * <p>For example, you might want to use this constant if only the children notifications
1175 * in your group have content and the summary is only used to visually group notifications.
1176 */
1177 public static final int GROUP_ALERT_CHILDREN = 2;
1178
Julia Reynolds2f431e22017-06-07 14:12:09 +00001179 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001180
Julia Reynolds13d898c2017-02-02 12:22:05 -05001181 /**
1182 * If this notification is being shown as a badge, always show as a number.
1183 */
1184 public static final int BADGE_ICON_NONE = 0;
1185
1186 /**
1187 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1188 * represent this notification.
1189 */
1190 public static final int BADGE_ICON_SMALL = 1;
1191
1192 /**
1193 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1194 * represent this notification.
1195 */
1196 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001197 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001198
Chris Wren51c75102013-07-16 20:49:17 -04001199 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001200 * Structure to encapsulate a named action that can be shown as part of this notification.
1201 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1202 * selected by the user.
1203 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001204 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1205 * or {@link Notification.Builder#addAction(Notification.Action)}
1206 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001207 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001208 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001209 /**
1210 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1211 * {@link RemoteInput}s.
1212 *
1213 * This is intended for {@link RemoteInput}s that only accept data, meaning
1214 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1215 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1216 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1217 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1218 *
1219 * You can test if a RemoteInput matches these constraints using
1220 * {@link RemoteInput#isDataOnly}.
1221 */
1222 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1223
Griff Hazen959591e2014-05-15 22:26:18 -07001224 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001225 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001226 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001227 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001228
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001229 /**
1230 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001231 *
1232 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001233 */
Dan Sandler86647982015-05-13 23:41:13 -04001234 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001235 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001236
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001237 /**
1238 * Title of the action.
1239 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001240 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001241
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001242 /**
1243 * Intent to send when the user invokes this action. May be null, in which case the action
1244 * may be rendered in a disabled presentation by the system UI.
1245 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001246 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001247
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001248 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001249 if (in.readInt() != 0) {
1250 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001251 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1252 icon = mIcon.getResId();
1253 }
Dan Sandler86647982015-05-13 23:41:13 -04001254 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001255 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1256 if (in.readInt() == 1) {
1257 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1258 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001259 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001260 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001261 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001263
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001264 /**
Dan Sandler86647982015-05-13 23:41:13 -04001265 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001266 */
Dan Sandler86647982015-05-13 23:41:13 -04001267 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001268 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001269 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001270 }
1271
Adrian Roos7af53622016-10-12 13:44:05 -07001272 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001273 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001274 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001275 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001276 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1277 this.icon = icon.getResId();
1278 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001279 this.title = title;
1280 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001281 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001282 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001283 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001284 }
1285
1286 /**
Dan Sandler86647982015-05-13 23:41:13 -04001287 * Return an icon representing the action.
1288 */
1289 public Icon getIcon() {
1290 if (mIcon == null && icon != 0) {
1291 // you snuck an icon in here without using the builder; let's try to keep it
1292 mIcon = Icon.createWithResource("", icon);
1293 }
1294 return mIcon;
1295 }
1296
1297 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001298 * Get additional metadata carried around with this Action.
1299 */
1300 public Bundle getExtras() {
1301 return mExtras;
1302 }
1303
1304 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001305 * Return whether the platform should automatically generate possible replies for this
1306 * {@link Action}
1307 */
1308 public boolean getAllowGeneratedReplies() {
1309 return mAllowGeneratedReplies;
1310 }
1311
1312 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001313 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001314 * May return null if no remote inputs were added. Only returns inputs which accept
1315 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001316 */
1317 public RemoteInput[] getRemoteInputs() {
1318 return mRemoteInputs;
1319 }
1320
1321 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001322 * Get the list of inputs to be collected from the user that ONLY accept data when this
1323 * action is sent. These remote inputs are guaranteed to return true on a call to
1324 * {@link RemoteInput#isDataOnly}.
1325 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001326 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001327 *
1328 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1329 * of non-textual RemoteInputs do not access these remote inputs.
1330 */
1331 public RemoteInput[] getDataOnlyRemoteInputs() {
1332 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1333 }
1334
1335 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001336 * Builder class for {@link Action} objects.
1337 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001338 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001339 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001340 private final CharSequence mTitle;
1341 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001342 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001343 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001344 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001345
1346 /**
1347 * Construct a new builder for {@link Action} object.
1348 * @param icon icon to show for this action
1349 * @param title the title of the action
1350 * @param intent the {@link PendingIntent} to fire when users trigger this action
1351 */
Dan Sandler86647982015-05-13 23:41:13 -04001352 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001353 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001354 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001355 }
1356
1357 /**
1358 * Construct a new builder for {@link Action} object.
1359 * @param icon icon to show for this action
1360 * @param title the title of the action
1361 * @param intent the {@link PendingIntent} to fire when users trigger this action
1362 */
1363 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001364 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001365 }
1366
1367 /**
1368 * Construct a new builder for {@link Action} object using the fields from an
1369 * {@link Action}.
1370 * @param action the action to read fields from.
1371 */
1372 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001373 this(action.getIcon(), action.title, action.actionIntent,
1374 new Bundle(action.mExtras), action.getRemoteInputs(),
1375 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001376 }
1377
Dan Sandler86647982015-05-13 23:41:13 -04001378 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001379 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001380 mIcon = icon;
1381 mTitle = title;
1382 mIntent = intent;
1383 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001384 if (remoteInputs != null) {
1385 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1386 Collections.addAll(mRemoteInputs, remoteInputs);
1387 }
Adrian Roos7af53622016-10-12 13:44:05 -07001388 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001389 }
1390
1391 /**
1392 * Merge additional metadata into this builder.
1393 *
1394 * <p>Values within the Bundle will replace existing extras values in this Builder.
1395 *
1396 * @see Notification.Action#extras
1397 */
1398 public Builder addExtras(Bundle extras) {
1399 if (extras != null) {
1400 mExtras.putAll(extras);
1401 }
1402 return this;
1403 }
1404
1405 /**
1406 * Get the metadata Bundle used by this Builder.
1407 *
1408 * <p>The returned Bundle is shared with this Builder.
1409 */
1410 public Bundle getExtras() {
1411 return mExtras;
1412 }
1413
1414 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001415 * Add an input to be collected from the user when this action is sent.
1416 * Response values can be retrieved from the fired intent by using the
1417 * {@link RemoteInput#getResultsFromIntent} function.
1418 * @param remoteInput a {@link RemoteInput} to add to the action
1419 * @return this object for method chaining
1420 */
1421 public Builder addRemoteInput(RemoteInput remoteInput) {
1422 if (mRemoteInputs == null) {
1423 mRemoteInputs = new ArrayList<RemoteInput>();
1424 }
1425 mRemoteInputs.add(remoteInput);
1426 return this;
1427 }
1428
1429 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001430 * Set whether the platform should automatically generate possible replies to add to
1431 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1432 * {@link RemoteInput}, this has no effect.
1433 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1434 * otherwise
1435 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001436 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001437 */
1438 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1439 mAllowGeneratedReplies = allowGeneratedReplies;
1440 return this;
1441 }
1442
1443 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001444 * Apply an extender to this action builder. Extenders may be used to add
1445 * metadata or change options on this builder.
1446 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001447 public Builder extend(Extender extender) {
1448 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001449 return this;
1450 }
1451
1452 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001453 * Combine all of the options that have been set and return a new {@link Action}
1454 * object.
1455 * @return the built action
1456 */
1457 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001458 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1459 RemoteInput[] previousDataInputs =
1460 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001461 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001462 for (RemoteInput input : previousDataInputs) {
1463 dataOnlyInputs.add(input);
1464 }
1465 }
1466 List<RemoteInput> textInputs = new ArrayList<>();
1467 if (mRemoteInputs != null) {
1468 for (RemoteInput input : mRemoteInputs) {
1469 if (input.isDataOnly()) {
1470 dataOnlyInputs.add(input);
1471 } else {
1472 textInputs.add(input);
1473 }
1474 }
1475 }
1476 if (!dataOnlyInputs.isEmpty()) {
1477 RemoteInput[] dataInputsArr =
1478 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1479 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1480 }
1481 RemoteInput[] textInputsArr = textInputs.isEmpty()
1482 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1483 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001484 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001485 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001486 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001487
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 @Override
1489 public Action clone() {
1490 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001491 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001492 title,
1493 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001494 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001495 getRemoteInputs(),
1496 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001497 }
1498 @Override
1499 public int describeContents() {
1500 return 0;
1501 }
1502 @Override
1503 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001504 final Icon ic = getIcon();
1505 if (ic != null) {
1506 out.writeInt(1);
1507 ic.writeToParcel(out, 0);
1508 } else {
1509 out.writeInt(0);
1510 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001511 TextUtils.writeToParcel(title, out, flags);
1512 if (actionIntent != null) {
1513 out.writeInt(1);
1514 actionIntent.writeToParcel(out, flags);
1515 } else {
1516 out.writeInt(0);
1517 }
Griff Hazen959591e2014-05-15 22:26:18 -07001518 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001519 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001520 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001521 }
Griff Hazen959591e2014-05-15 22:26:18 -07001522 public static final Parcelable.Creator<Action> CREATOR =
1523 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001524 public Action createFromParcel(Parcel in) {
1525 return new Action(in);
1526 }
1527 public Action[] newArray(int size) {
1528 return new Action[size];
1529 }
1530 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001531
1532 /**
1533 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1534 * metadata or change options on an action builder.
1535 */
1536 public interface Extender {
1537 /**
1538 * Apply this extender to a notification action builder.
1539 * @param builder the builder to be modified.
1540 * @return the build object for chaining.
1541 */
1542 public Builder extend(Builder builder);
1543 }
1544
1545 /**
1546 * Wearable extender for notification actions. To add extensions to an action,
1547 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1548 * the {@code WearableExtender()} constructor and apply it to a
1549 * {@link android.app.Notification.Action.Builder} using
1550 * {@link android.app.Notification.Action.Builder#extend}.
1551 *
1552 * <pre class="prettyprint">
1553 * Notification.Action action = new Notification.Action.Builder(
1554 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001555 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001556 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001557 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001558 */
1559 public static final class WearableExtender implements Extender {
1560 /** Notification action extra which contains wearable extensions */
1561 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1562
Pete Gastaf6781d2014-10-07 15:17:05 -04001563 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001564 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001565 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1566 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1567 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001568
1569 // Flags bitwise-ored to mFlags
1570 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001571 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001572 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001573
1574 // Default value for flags integer
1575 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1576
1577 private int mFlags = DEFAULT_FLAGS;
1578
Pete Gastaf6781d2014-10-07 15:17:05 -04001579 private CharSequence mInProgressLabel;
1580 private CharSequence mConfirmLabel;
1581 private CharSequence mCancelLabel;
1582
Griff Hazen61a9e862014-05-22 16:05:19 -07001583 /**
1584 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1585 * options.
1586 */
1587 public WearableExtender() {
1588 }
1589
1590 /**
1591 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1592 * wearable options present in an existing notification action.
1593 * @param action the notification action to inspect.
1594 */
1595 public WearableExtender(Action action) {
1596 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1597 if (wearableBundle != null) {
1598 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001599 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1600 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1601 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001602 }
1603 }
1604
1605 /**
1606 * Apply wearable extensions to a notification action that is being built. This is
1607 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1608 * method of {@link android.app.Notification.Action.Builder}.
1609 */
1610 @Override
1611 public Action.Builder extend(Action.Builder builder) {
1612 Bundle wearableBundle = new Bundle();
1613
1614 if (mFlags != DEFAULT_FLAGS) {
1615 wearableBundle.putInt(KEY_FLAGS, mFlags);
1616 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001617 if (mInProgressLabel != null) {
1618 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1619 }
1620 if (mConfirmLabel != null) {
1621 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1622 }
1623 if (mCancelLabel != null) {
1624 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1625 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001626
1627 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1628 return builder;
1629 }
1630
1631 @Override
1632 public WearableExtender clone() {
1633 WearableExtender that = new WearableExtender();
1634 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001635 that.mInProgressLabel = this.mInProgressLabel;
1636 that.mConfirmLabel = this.mConfirmLabel;
1637 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001638 return that;
1639 }
1640
1641 /**
1642 * Set whether this action is available when the wearable device is not connected to
1643 * a companion device. The user can still trigger this action when the wearable device is
1644 * offline, but a visual hint will indicate that the action may not be available.
1645 * Defaults to true.
1646 */
1647 public WearableExtender setAvailableOffline(boolean availableOffline) {
1648 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1649 return this;
1650 }
1651
1652 /**
1653 * Get whether this action is available when the wearable device is not connected to
1654 * a companion device. The user can still trigger this action when the wearable device is
1655 * offline, but a visual hint will indicate that the action may not be available.
1656 * Defaults to true.
1657 */
1658 public boolean isAvailableOffline() {
1659 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1660 }
1661
1662 private void setFlag(int mask, boolean value) {
1663 if (value) {
1664 mFlags |= mask;
1665 } else {
1666 mFlags &= ~mask;
1667 }
1668 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001669
1670 /**
1671 * Set a label to display while the wearable is preparing to automatically execute the
1672 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1673 *
1674 * @param label the label to display while the action is being prepared to execute
1675 * @return this object for method chaining
1676 */
1677 public WearableExtender setInProgressLabel(CharSequence label) {
1678 mInProgressLabel = label;
1679 return this;
1680 }
1681
1682 /**
1683 * Get the label to display while the wearable is preparing to automatically execute
1684 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1685 *
1686 * @return the label to display while the action is being prepared to execute
1687 */
1688 public CharSequence getInProgressLabel() {
1689 return mInProgressLabel;
1690 }
1691
1692 /**
1693 * Set a label to display to confirm that the action should be executed.
1694 * This is usually an imperative verb like "Send".
1695 *
1696 * @param label the label to confirm the action should be executed
1697 * @return this object for method chaining
1698 */
1699 public WearableExtender setConfirmLabel(CharSequence label) {
1700 mConfirmLabel = label;
1701 return this;
1702 }
1703
1704 /**
1705 * Get the label to display to confirm that the action should be executed.
1706 * This is usually an imperative verb like "Send".
1707 *
1708 * @return the label to confirm the action should be executed
1709 */
1710 public CharSequence getConfirmLabel() {
1711 return mConfirmLabel;
1712 }
1713
1714 /**
1715 * Set a label to display to cancel the action.
1716 * This is usually an imperative verb, like "Cancel".
1717 *
1718 * @param label the label to display to cancel the action
1719 * @return this object for method chaining
1720 */
1721 public WearableExtender setCancelLabel(CharSequence label) {
1722 mCancelLabel = label;
1723 return this;
1724 }
1725
1726 /**
1727 * Get the label to display to cancel the action.
1728 * This is usually an imperative verb like "Cancel".
1729 *
1730 * @return the label to display to cancel the action
1731 */
1732 public CharSequence getCancelLabel() {
1733 return mCancelLabel;
1734 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001735
1736 /**
1737 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1738 * platform that it can generate the appropriate transitions.
1739 * @param hintLaunchesActivity {@code true} if the content intent will launch
1740 * an activity and transitions should be generated, false otherwise.
1741 * @return this object for method chaining
1742 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001743 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001744 boolean hintLaunchesActivity) {
1745 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1746 return this;
1747 }
1748
1749 /**
1750 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1751 * platform that it can generate the appropriate transitions
1752 * @return {@code true} if the content intent will launch an activity and transitions
1753 * should be generated, false otherwise. The default value is {@code false} if this was
1754 * never set.
1755 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001756 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001757 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1758 }
Alex Hills9f087612016-06-07 09:08:59 -04001759
1760 /**
1761 * Set a hint that this Action should be displayed inline.
1762 *
1763 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1764 * otherwise
1765 * @return this object for method chaining
1766 */
1767 public WearableExtender setHintDisplayActionInline(
1768 boolean hintDisplayInline) {
1769 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1770 return this;
1771 }
1772
1773 /**
1774 * Get a hint that this Action should be displayed inline.
1775 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001776 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001777 * otherwise. The default value is {@code false} if this was never set.
1778 */
1779 public boolean getHintDisplayActionInline() {
1780 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1781 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001782 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001783 }
1784
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001785 /**
1786 * Array of all {@link Action} structures attached to this notification by
1787 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1788 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1789 * interface for invoking actions.
1790 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001791 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001792
1793 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001794 * Replacement version of this notification whose content will be shown
1795 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1796 * and {@link #VISIBILITY_PUBLIC}.
1797 */
1798 public Notification publicVersion;
1799
1800 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001801 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001802 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 */
1804 public Notification()
1805 {
1806 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001807 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001808 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810
1811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 * @hide
1813 */
1814 public Notification(Context context, int icon, CharSequence tickerText, long when,
1815 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1816 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001817 new Builder(context)
1818 .setWhen(when)
1819 .setSmallIcon(icon)
1820 .setTicker(tickerText)
1821 .setContentTitle(contentTitle)
1822 .setContentText(contentText)
1823 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1824 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826
1827 /**
1828 * Constructs a Notification object with the information needed to
1829 * have a status bar icon without the standard expanded view.
1830 *
1831 * @param icon The resource id of the icon to put in the status bar.
1832 * @param tickerText The text that flows by in the status bar when the notification first
1833 * activates.
1834 * @param when The time to show in the time field. In the System.currentTimeMillis
1835 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001836 *
1837 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001839 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 public Notification(int icon, CharSequence tickerText, long when)
1841 {
1842 this.icon = icon;
1843 this.tickerText = tickerText;
1844 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001845 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847
1848 /**
1849 * Unflatten the notification from a parcel.
1850 */
Svet Ganovddb94882016-06-23 19:55:24 -07001851 @SuppressWarnings("unchecked")
1852 public Notification(Parcel parcel) {
1853 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1854 // intents in extras are always written as the last entry.
1855 readFromParcelImpl(parcel);
1856 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001857 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001858 }
1859
1860 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 {
1862 int version = parcel.readInt();
1863
Dianne Hackborn98305522017-05-05 17:53:53 -07001864 whitelistToken = parcel.readStrongBinder();
1865 if (whitelistToken == null) {
1866 whitelistToken = processWhitelistToken;
1867 }
1868 // Propagate this token to all pending intents that are unmarshalled from the parcel.
1869 parcel.setClassCookie(PendingIntent.class, whitelistToken);
1870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001872 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001873 if (parcel.readInt() != 0) {
1874 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001875 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1876 icon = mSmallIcon.getResId();
1877 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 number = parcel.readInt();
1880 if (parcel.readInt() != 0) {
1881 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1882 }
1883 if (parcel.readInt() != 0) {
1884 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1885 }
1886 if (parcel.readInt() != 0) {
1887 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1888 }
1889 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001890 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001891 }
1892 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1894 }
Joe Onorato561d3852010-11-20 18:09:34 -08001895 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001896 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 defaults = parcel.readInt();
1899 flags = parcel.readInt();
1900 if (parcel.readInt() != 0) {
1901 sound = Uri.CREATOR.createFromParcel(parcel);
1902 }
1903
1904 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001905 if (parcel.readInt() != 0) {
1906 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 vibrate = parcel.createLongArray();
1909 ledARGB = parcel.readInt();
1910 ledOnMS = parcel.readInt();
1911 ledOffMS = parcel.readInt();
1912 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001913
1914 if (parcel.readInt() != 0) {
1915 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1916 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001917
1918 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001919
John Spurlockfd7f1e02014-03-18 16:41:57 -04001920 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001921
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001922 mGroupKey = parcel.readString();
1923
1924 mSortKey = parcel.readString();
1925
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001926 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001927
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1929
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001930 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001931 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1932 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001933
Chris Wren8fd39ec2014-02-27 17:43:26 -05001934 if (parcel.readInt() != 0) {
1935 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1936 }
1937
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001938 visibility = parcel.readInt();
1939
1940 if (parcel.readInt() != 0) {
1941 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1942 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001943
1944 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001945
1946 if (parcel.readInt() != 0) {
1947 mChannelId = parcel.readString();
1948 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001949 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001950
1951 if (parcel.readInt() != 0) {
1952 mShortcutId = parcel.readString();
1953 }
1954
1955 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001956
1957 if (parcel.readInt() != 0) {
1958 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1959 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001960
1961 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963
Andy Stadler110988c2010-12-03 14:29:16 -08001964 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001965 public Notification clone() {
1966 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001967 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001968 return that;
1969 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001970
Daniel Sandler1a497d32013-04-18 14:52:45 -04001971 /**
1972 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1973 * of this into that.
1974 * @hide
1975 */
1976 public void cloneInto(Notification that, boolean heavy) {
Dianne Hackborn98305522017-05-05 17:53:53 -07001977 that.whitelistToken = this.whitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001978 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001979 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001980 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001981 that.number = this.number;
1982
1983 // PendingIntents are global, so there's no reason (or way) to clone them.
1984 that.contentIntent = this.contentIntent;
1985 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001986 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001987
1988 if (this.tickerText != null) {
1989 that.tickerText = this.tickerText.toString();
1990 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001991 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001992 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001993 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001994 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001995 that.contentView = this.contentView.clone();
1996 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001997 if (heavy && this.mLargeIcon != null) {
1998 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001999 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002000 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002001 that.sound = this.sound; // android.net.Uri is immutable
2002 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002003 if (this.audioAttributes != null) {
2004 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2005 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002006
2007 final long[] vibrate = this.vibrate;
2008 if (vibrate != null) {
2009 final int N = vibrate.length;
2010 final long[] vib = that.vibrate = new long[N];
2011 System.arraycopy(vibrate, 0, vib, 0, N);
2012 }
2013
2014 that.ledARGB = this.ledARGB;
2015 that.ledOnMS = this.ledOnMS;
2016 that.ledOffMS = this.ledOffMS;
2017 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002018
Joe Onorato18e69df2010-05-17 22:26:12 -07002019 that.flags = this.flags;
2020
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002021 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002022
John Spurlockfd7f1e02014-03-18 16:41:57 -04002023 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002024
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002025 that.mGroupKey = this.mGroupKey;
2026
2027 that.mSortKey = this.mSortKey;
2028
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002029 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002030 try {
2031 that.extras = new Bundle(this.extras);
2032 // will unparcel
2033 that.extras.size();
2034 } catch (BadParcelableException e) {
2035 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2036 that.extras = null;
2037 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002038 }
2039
Felipe Lemedd85da62016-06-28 11:29:54 -07002040 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2041 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002042 }
2043
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002044 if (this.actions != null) {
2045 that.actions = new Action[this.actions.length];
2046 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002047 if ( this.actions[i] != null) {
2048 that.actions[i] = this.actions[i].clone();
2049 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002050 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002051 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002052
Daniel Sandler1a497d32013-04-18 14:52:45 -04002053 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002054 that.bigContentView = this.bigContentView.clone();
2055 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002056
Chris Wren8fd39ec2014-02-27 17:43:26 -05002057 if (heavy && this.headsUpContentView != null) {
2058 that.headsUpContentView = this.headsUpContentView.clone();
2059 }
2060
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002061 that.visibility = this.visibility;
2062
2063 if (this.publicVersion != null) {
2064 that.publicVersion = new Notification();
2065 this.publicVersion.cloneInto(that.publicVersion, heavy);
2066 }
2067
Dan Sandler26e81cf2014-05-06 10:01:27 -04002068 that.color = this.color;
2069
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002070 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002071 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002072 that.mShortcutId = this.mShortcutId;
2073 that.mBadgeIcon = this.mBadgeIcon;
2074 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002075 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002076
Daniel Sandler1a497d32013-04-18 14:52:45 -04002077 if (!heavy) {
2078 that.lightenPayload(); // will clean out extras
2079 }
2080 }
2081
2082 /**
2083 * Removes heavyweight parts of the Notification object for archival or for sending to
2084 * listeners when the full contents are not necessary.
2085 * @hide
2086 */
2087 public final void lightenPayload() {
2088 tickerView = null;
2089 contentView = null;
2090 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002091 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002092 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002093 if (extras != null && !extras.isEmpty()) {
2094 final Set<String> keyset = extras.keySet();
2095 final int N = keyset.size();
2096 final String[] keys = keyset.toArray(new String[N]);
2097 for (int i=0; i<N; i++) {
2098 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002099 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2100 continue;
2101 }
Dan Sandler50128532015-12-08 15:42:41 -05002102 final Object obj = extras.get(key);
2103 if (obj != null &&
2104 ( obj instanceof Parcelable
2105 || obj instanceof Parcelable[]
2106 || obj instanceof SparseArray
2107 || obj instanceof ArrayList)) {
2108 extras.remove(key);
2109 }
2110 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002111 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002112 }
2113
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002114 /**
2115 * Make sure this CharSequence is safe to put into a bundle, which basically
2116 * means it had better not be some custom Parcelable implementation.
2117 * @hide
2118 */
2119 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002120 if (cs == null) return cs;
2121 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2122 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2123 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002124 if (cs instanceof Parcelable) {
2125 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2126 + " instance is a custom Parcelable and not allowed in Notification");
2127 return cs.toString();
2128 }
Selim Cinek60a54252016-02-26 17:03:25 -08002129 return removeTextSizeSpans(cs);
2130 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002131
Selim Cinek60a54252016-02-26 17:03:25 -08002132 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2133 if (charSequence instanceof Spanned) {
2134 Spanned ss = (Spanned) charSequence;
2135 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2136 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2137 for (Object span : spans) {
2138 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002139 if (resultSpan instanceof CharacterStyle) {
2140 resultSpan = ((CharacterStyle) span).getUnderlying();
2141 }
2142 if (resultSpan instanceof TextAppearanceSpan) {
2143 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002144 resultSpan = new TextAppearanceSpan(
2145 originalSpan.getFamily(),
2146 originalSpan.getTextStyle(),
2147 -1,
2148 originalSpan.getTextColor(),
2149 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002150 } else if (resultSpan instanceof RelativeSizeSpan
2151 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002152 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002153 } else {
2154 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002155 }
2156 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2157 ss.getSpanFlags(span));
2158 }
2159 return builder;
2160 }
2161 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002162 }
2163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 public int describeContents() {
2165 return 0;
2166 }
2167
2168 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002169 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 */
Svet Ganovddb94882016-06-23 19:55:24 -07002171 public void writeToParcel(Parcel parcel, int flags) {
2172 // We need to mark all pending intents getting into the notification
2173 // system as being put there to later allow the notification ranker
2174 // to launch them and by doing so add the app to the battery saver white
2175 // list for a short period of time. The problem is that the system
2176 // cannot look into the extras as there may be parcelables there that
2177 // the platform does not know how to handle. To go around that we have
2178 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002179 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002180 if (collectPendingIntents) {
2181 PendingIntent.setOnMarshaledListener(
2182 (PendingIntent intent, Parcel out, int outFlags) -> {
2183 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002184 if (allPendingIntents == null) {
2185 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002186 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002187 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002188 }
2189 });
2190 }
2191 try {
2192 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002193 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002194 writeToParcelImpl(parcel, flags);
2195 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002196 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002197 } finally {
2198 if (collectPendingIntents) {
2199 PendingIntent.setOnMarshaledListener(null);
2200 }
2201 }
2202 }
2203
2204 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 parcel.writeInt(1);
2206
Dianne Hackborn98305522017-05-05 17:53:53 -07002207 parcel.writeStrongBinder(whitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002209 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002210 if (mSmallIcon == null && icon != 0) {
2211 // you snuck an icon in here without using the builder; let's try to keep it
2212 mSmallIcon = Icon.createWithResource("", icon);
2213 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002214 if (mSmallIcon != null) {
2215 parcel.writeInt(1);
2216 mSmallIcon.writeToParcel(parcel, 0);
2217 } else {
2218 parcel.writeInt(0);
2219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 parcel.writeInt(number);
2221 if (contentIntent != null) {
2222 parcel.writeInt(1);
2223 contentIntent.writeToParcel(parcel, 0);
2224 } else {
2225 parcel.writeInt(0);
2226 }
2227 if (deleteIntent != null) {
2228 parcel.writeInt(1);
2229 deleteIntent.writeToParcel(parcel, 0);
2230 } else {
2231 parcel.writeInt(0);
2232 }
2233 if (tickerText != null) {
2234 parcel.writeInt(1);
2235 TextUtils.writeToParcel(tickerText, parcel, flags);
2236 } else {
2237 parcel.writeInt(0);
2238 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002239 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002240 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002241 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002242 } else {
2243 parcel.writeInt(0);
2244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 if (contentView != null) {
2246 parcel.writeInt(1);
2247 contentView.writeToParcel(parcel, 0);
2248 } else {
2249 parcel.writeInt(0);
2250 }
Selim Cinek279fa862016-06-14 10:57:25 -07002251 if (mLargeIcon == null && largeIcon != null) {
2252 // you snuck an icon in here without using the builder; let's try to keep it
2253 mLargeIcon = Icon.createWithBitmap(largeIcon);
2254 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002255 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002256 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002257 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002258 } else {
2259 parcel.writeInt(0);
2260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261
2262 parcel.writeInt(defaults);
2263 parcel.writeInt(this.flags);
2264
2265 if (sound != null) {
2266 parcel.writeInt(1);
2267 sound.writeToParcel(parcel, 0);
2268 } else {
2269 parcel.writeInt(0);
2270 }
2271 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002272
2273 if (audioAttributes != null) {
2274 parcel.writeInt(1);
2275 audioAttributes.writeToParcel(parcel, 0);
2276 } else {
2277 parcel.writeInt(0);
2278 }
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 parcel.writeLongArray(vibrate);
2281 parcel.writeInt(ledARGB);
2282 parcel.writeInt(ledOnMS);
2283 parcel.writeInt(ledOffMS);
2284 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002285
2286 if (fullScreenIntent != null) {
2287 parcel.writeInt(1);
2288 fullScreenIntent.writeToParcel(parcel, 0);
2289 } else {
2290 parcel.writeInt(0);
2291 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002292
2293 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002294
John Spurlockfd7f1e02014-03-18 16:41:57 -04002295 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002296
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002297 parcel.writeString(mGroupKey);
2298
2299 parcel.writeString(mSortKey);
2300
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002301 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002302
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002303 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002304
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002305 if (bigContentView != null) {
2306 parcel.writeInt(1);
2307 bigContentView.writeToParcel(parcel, 0);
2308 } else {
2309 parcel.writeInt(0);
2310 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002311
Chris Wren8fd39ec2014-02-27 17:43:26 -05002312 if (headsUpContentView != null) {
2313 parcel.writeInt(1);
2314 headsUpContentView.writeToParcel(parcel, 0);
2315 } else {
2316 parcel.writeInt(0);
2317 }
2318
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002319 parcel.writeInt(visibility);
2320
2321 if (publicVersion != null) {
2322 parcel.writeInt(1);
2323 publicVersion.writeToParcel(parcel, 0);
2324 } else {
2325 parcel.writeInt(0);
2326 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002327
2328 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002329
2330 if (mChannelId != null) {
2331 parcel.writeInt(1);
2332 parcel.writeString(mChannelId);
2333 } else {
2334 parcel.writeInt(0);
2335 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002336 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002337
2338 if (mShortcutId != null) {
2339 parcel.writeInt(1);
2340 parcel.writeString(mShortcutId);
2341 } else {
2342 parcel.writeInt(0);
2343 }
2344
2345 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002346
2347 if (mSettingsText != null) {
2348 parcel.writeInt(1);
2349 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2350 } else {
2351 parcel.writeInt(0);
2352 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002353
2354 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356
2357 /**
2358 * Parcelable.Creator that instantiates Notification objects
2359 */
2360 public static final Parcelable.Creator<Notification> CREATOR
2361 = new Parcelable.Creator<Notification>()
2362 {
2363 public Notification createFromParcel(Parcel parcel)
2364 {
2365 return new Notification(parcel);
2366 }
2367
2368 public Notification[] newArray(int size)
2369 {
2370 return new Notification[size];
2371 }
2372 };
2373
2374 /**
2375 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2376 * layout.
2377 *
2378 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2379 * in the view.</p>
2380 * @param context The context for your application / activity.
2381 * @param contentTitle The title that goes in the expanded entry.
2382 * @param contentText The text that goes in the expanded entry.
2383 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2384 * If this is an activity, it must include the
2385 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002386 * that you take care of task management as described in the
2387 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2388 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002390 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002391 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 public void setLatestEventInfo(Context context,
2395 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002396 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2397 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2398 new Throwable());
2399 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002400
Selim Cinek4ac6f602016-06-13 15:47:03 -07002401 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2402 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2403 }
2404
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002405 // ensure that any information already set directly is preserved
2406 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002407
2408 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002410 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002413 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002415 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002416
2417 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419
Julia Reynoldsda303542015-11-23 14:00:20 -05002420 /**
2421 * @hide
2422 */
2423 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002424 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002425 }
2426
2427 /**
2428 * @hide
2429 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002430 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002431 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002432 }
2433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 @Override
2435 public String toString() {
2436 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002437 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002438 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002439 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002440 sb.append(priority);
2441 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002442 if (contentView != null) {
2443 sb.append(contentView.getPackage());
2444 sb.append("/0x");
2445 sb.append(Integer.toHexString(contentView.getLayoutId()));
2446 } else {
2447 sb.append("null");
2448 }
2449 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002450 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2451 sb.append("default");
2452 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 int N = this.vibrate.length-1;
2454 sb.append("[");
2455 for (int i=0; i<N; i++) {
2456 sb.append(this.vibrate[i]);
2457 sb.append(',');
2458 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002459 if (N != -1) {
2460 sb.append(this.vibrate[N]);
2461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 } else {
2464 sb.append("null");
2465 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002467 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002469 } else if (this.sound != null) {
2470 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 } else {
2472 sb.append("null");
2473 }
Chris Wren365b6d32015-07-16 10:39:26 -04002474 if (this.tickerText != null) {
2475 sb.append(" tick");
2476 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002477 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002479 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002480 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002481 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002482 if (this.category != null) {
2483 sb.append(" category=");
2484 sb.append(this.category);
2485 }
2486 if (this.mGroupKey != null) {
2487 sb.append(" groupKey=");
2488 sb.append(this.mGroupKey);
2489 }
2490 if (this.mSortKey != null) {
2491 sb.append(" sortKey=");
2492 sb.append(this.mSortKey);
2493 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002494 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002495 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002496 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002497 }
2498 sb.append(" vis=");
2499 sb.append(visibilityToString(this.visibility));
2500 if (this.publicVersion != null) {
2501 sb.append(" publicVersion=");
2502 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002503 }
2504 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 return sb.toString();
2506 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002507
Dan Sandler1b718782014-07-18 12:43:45 -04002508 /**
2509 * {@hide}
2510 */
2511 public static String visibilityToString(int vis) {
2512 switch (vis) {
2513 case VISIBILITY_PRIVATE:
2514 return "PRIVATE";
2515 case VISIBILITY_PUBLIC:
2516 return "PUBLIC";
2517 case VISIBILITY_SECRET:
2518 return "SECRET";
2519 default:
2520 return "UNKNOWN(" + String.valueOf(vis) + ")";
2521 }
2522 }
2523
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002525 * {@hide}
2526 */
2527 public static String priorityToString(@Priority int pri) {
2528 switch (pri) {
2529 case PRIORITY_MIN:
2530 return "MIN";
2531 case PRIORITY_LOW:
2532 return "LOW";
2533 case PRIORITY_DEFAULT:
2534 return "DEFAULT";
2535 case PRIORITY_HIGH:
2536 return "HIGH";
2537 case PRIORITY_MAX:
2538 return "MAX";
2539 default:
2540 return "UNKNOWN(" + String.valueOf(pri) + ")";
2541 }
2542 }
2543
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002544 /**
2545 * @hide
2546 */
2547 public boolean hasCompletedProgress() {
2548 // not a progress notification; can't be complete
2549 if (!extras.containsKey(EXTRA_PROGRESS)
2550 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2551 return false;
2552 }
2553 // many apps use max 0 for 'indeterminate'; not complete
2554 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2555 return false;
2556 }
2557 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2558 }
2559
Jeff Sharkey000ce802017-04-29 13:13:27 -06002560 /** @removed */
2561 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002562 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002563 return mChannelId;
2564 }
2565
2566 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002567 * Returns the id of the channel this notification posts to.
2568 */
2569 public String getChannelId() {
2570 return mChannelId;
2571 }
2572
Jeff Sharkey000ce802017-04-29 13:13:27 -06002573 /** @removed */
2574 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002575 public long getTimeout() {
2576 return mTimeout;
2577 }
2578
2579 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002580 * Returns the duration from posting after which this notification should be canceled by the
2581 * system, if it's not canceled already.
2582 */
2583 public long getTimeoutAfter() {
2584 return mTimeout;
2585 }
2586
2587 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002588 * Returns what icon should be shown for this notification if it is being displayed in a
2589 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2590 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2591 */
2592 public int getBadgeIconType() {
2593 return mBadgeIcon;
2594 }
2595
2596 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002597 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002598 *
2599 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2600 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002601 */
2602 public String getShortcutId() {
2603 return mShortcutId;
2604 }
2605
Julia Reynolds3aedded2017-03-31 14:42:09 -04002606
2607 /**
2608 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2609 */
2610 public CharSequence getSettingsText() {
2611 return mSettingsText;
2612 }
2613
Julia Reynolds13d898c2017-02-02 12:22:05 -05002614 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002615 * Returns which type of notifications in a group are responsible for audibly alerting the
2616 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2617 * {@link #GROUP_ALERT_SUMMARY}.
2618 */
2619 public @GroupAlertBehavior int getGroupAlertBehavior() {
2620 return mGroupAlertBehavior;
2621 }
2622
2623 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002624 * The small icon representing this notification in the status bar and content view.
2625 *
2626 * @return the small icon representing this notification.
2627 *
2628 * @see Builder#getSmallIcon()
2629 * @see Builder#setSmallIcon(Icon)
2630 */
2631 public Icon getSmallIcon() {
2632 return mSmallIcon;
2633 }
2634
2635 /**
2636 * Used when notifying to clean up legacy small icons.
2637 * @hide
2638 */
2639 public void setSmallIcon(Icon icon) {
2640 mSmallIcon = icon;
2641 }
2642
2643 /**
2644 * The large icon shown in this notification's content view.
2645 * @see Builder#getLargeIcon()
2646 * @see Builder#setLargeIcon(Icon)
2647 */
2648 public Icon getLargeIcon() {
2649 return mLargeIcon;
2650 }
2651
2652 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002653 * @hide
2654 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002655 public boolean isGroupSummary() {
2656 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2657 }
2658
2659 /**
2660 * @hide
2661 */
2662 public boolean isGroupChild() {
2663 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2664 }
2665
2666 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002667 * @hide
2668 */
2669 public boolean suppressAlertingDueToGrouping() {
2670 if (isGroupSummary()
2671 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2672 return true;
2673 } else if (isGroupChild()
2674 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2675 return true;
2676 }
2677 return false;
2678 }
2679
2680 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002681 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002682 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002684 * content views using the platform's notification layout template. If your app supports
2685 * versions of Android as old as API level 4, you can instead use
2686 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2687 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2688 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002689 *
Scott Main183bf112012-08-13 19:12:13 -07002690 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002691 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002692 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002693 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002694 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2695 * .setContentText(subject)
2696 * .setSmallIcon(R.drawable.new_mail)
2697 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002698 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002700 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002701 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002702 /**
2703 * @hide
2704 */
2705 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2706 "android.rebuild.contentViewActionCount";
2707 /**
2708 * @hide
2709 */
2710 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2711 = "android.rebuild.bigViewActionCount";
2712 /**
2713 * @hide
2714 */
2715 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2716 = "android.rebuild.hudViewActionCount";
2717
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002718 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002719
Selim Cinek6743c0b2017-01-18 18:24:01 -08002720 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2721 SystemProperties.getBoolean("notifications.only_title", true);
2722
Selim Cinek389edcd2017-05-11 19:16:44 -07002723 /**
2724 * The lightness difference that has to be added to the primary text color to obtain the
2725 * secondary text color when the background is light.
2726 */
2727 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2728
2729 /**
2730 * The lightness difference that has to be added to the primary text color to obtain the
2731 * secondary text color when the background is dark.
2732 * A bit less then the above value, since it looks better on dark backgrounds.
2733 */
2734 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2735
Joe Onorato46439ce2010-11-19 13:56:21 -08002736 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002737 private Notification mN;
2738 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002739 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002740 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2741 private ArrayList<String> mPersonList = new ArrayList<String>();
2742 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002743 private boolean mIsLegacy;
2744 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002745
2746 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002747 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2748 */
2749 private int mCachedContrastColor = COLOR_INVALID;
2750 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002751 /**
2752 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2753 */
2754 private int mCachedAmbientColor = COLOR_INVALID;
2755 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002756
2757 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002758 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2759 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2760 */
2761 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002762 private int mTextColorsAreForBackground = COLOR_INVALID;
2763 private int mPrimaryTextColor = COLOR_INVALID;
2764 private int mSecondaryTextColor = COLOR_INVALID;
2765 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002766 private int mBackgroundColor = COLOR_INVALID;
2767 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002768 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002769 /**
2770 * A temporary location where actions are stored. If != null the view originally has action
2771 * but doesn't have any for this inflation.
2772 */
2773 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002774 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002775
2776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002777 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002778 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002779 * @param context
2780 * A {@link Context} that will be used by the Builder to construct the
2781 * RemoteViews. The Context will not be held past the lifetime of this Builder
2782 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002783 * @param channelId
2784 * The constructed Notification will be posted on this
2785 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2786 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002787 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002788 public Builder(Context context, String channelId) {
2789 this(context, (Notification) null);
2790 mN.mChannelId = channelId;
2791 }
2792
2793 /**
2794 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2795 * instead. All posted Notifications must specify a NotificationChannel Id.
2796 */
2797 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002798 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002799 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002800 }
2801
Joe Onoratocb109a02011-01-18 17:57:41 -08002802 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002803 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002804 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002805 public Builder(Context context, Notification toAdopt) {
2806 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002807
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002808 if (toAdopt == null) {
2809 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002810 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2811 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2812 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002813 mN.priority = PRIORITY_DEFAULT;
2814 mN.visibility = VISIBILITY_PRIVATE;
2815 } else {
2816 mN = toAdopt;
2817 if (mN.actions != null) {
2818 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002819 }
2820
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2822 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2823 }
2824
Selim Cinek4ac6f602016-06-13 15:47:03 -07002825 if (mN.getSmallIcon() == null && mN.icon != 0) {
2826 setSmallIcon(mN.icon);
2827 }
2828
2829 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2830 setLargeIcon(mN.largeIcon);
2831 }
2832
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2834 if (!TextUtils.isEmpty(templateClass)) {
2835 final Class<? extends Style> styleClass
2836 = getNotificationStyleClass(templateClass);
2837 if (styleClass == null) {
2838 Log.d(TAG, "Unknown style class: " + templateClass);
2839 } else {
2840 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002841 final Constructor<? extends Style> ctor =
2842 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002843 ctor.setAccessible(true);
2844 final Style style = ctor.newInstance();
2845 style.restoreFromExtras(mN.extras);
2846
2847 if (style != null) {
2848 setStyle(style);
2849 }
2850 } catch (Throwable t) {
2851 Log.e(TAG, "Could not create Style", t);
2852 }
2853 }
2854 }
2855
2856 }
2857 }
2858
2859 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002860 if (mColorUtil == null) {
2861 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002862 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002863 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002864 }
2865
2866 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002867 * If this notification is duplicative of a Launcher shortcut, sets the
2868 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2869 * the shortcut.
2870 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002871 * This field will be ignored by Launchers that don't support badging, don't show
2872 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002873 *
2874 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2875 * supersedes
2876 */
2877 public Builder setShortcutId(String shortcutId) {
2878 mN.mShortcutId = shortcutId;
2879 return this;
2880 }
2881
2882 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002883 * Sets which icon to display as a badge for this notification.
2884 *
2885 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2886 * {@link #BADGE_ICON_LARGE}.
2887 *
2888 * Note: This value might be ignored, for launchers that don't support badge icons.
2889 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002890 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002891 mN.mBadgeIcon = icon;
2892 return this;
2893 }
2894
2895 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002896 * Sets the group alert behavior for this notification. Use this method to mute this
2897 * notification if alerts for this notification's group should be handled by a different
2898 * notification. This is only applicable for notifications that belong to a
2899 * {@link #setGroup(String) group}.
2900 *
2901 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2902 */
2903 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2904 mN.mGroupAlertBehavior = groupAlertBehavior;
2905 return this;
2906 }
2907
Jeff Sharkey000ce802017-04-29 13:13:27 -06002908 /** @removed */
2909 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002910 public Builder setChannel(String channelId) {
2911 mN.mChannelId = channelId;
2912 return this;
2913 }
2914
2915 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002916 * Specifies the channel the notification should be delivered on.
2917 */
2918 public Builder setChannelId(String channelId) {
2919 mN.mChannelId = channelId;
2920 return this;
2921 }
2922
Jeff Sharkey000ce802017-04-29 13:13:27 -06002923 /** @removed */
2924 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002925 public Builder setTimeout(long durationMs) {
2926 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002927 return this;
2928 }
2929
2930 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002931 * Specifies a duration in milliseconds after which this notification should be canceled,
2932 * if it is not already canceled.
2933 */
2934 public Builder setTimeoutAfter(long durationMs) {
2935 mN.mTimeout = durationMs;
2936 return this;
2937 }
2938
2939 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002940 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002941 *
2942 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2943 * shown anymore by default and must be opted into by using
2944 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002945 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002946 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002947 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002948 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002949 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002950 return this;
2951 }
2952
Joe Onoratocb109a02011-01-18 17:57:41 -08002953 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002954 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002955 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002956 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2957 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002958 */
2959 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002960 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002961 return this;
2962 }
2963
2964 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002965 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002966 *
2967 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002968 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002969 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002970 * Useful when showing an elapsed time (like an ongoing phone call).
2971 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002972 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002973 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002974 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002975 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002976 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002977 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002978 */
2979 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002980 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002981 return this;
2982 }
2983
2984 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002985 * Sets the Chronometer to count down instead of counting up.
2986 *
2987 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2988 * If it isn't set the chronometer will count up.
2989 *
2990 * @see #setUsesChronometer(boolean)
2991 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002992 public Builder setChronometerCountDown(boolean countDown) {
2993 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002994 return this;
2995 }
2996
2997 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002998 * Set the small icon resource, which will be used to represent the notification in the
2999 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003000 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003001
3002 * The platform template for the expanded view will draw this icon in the left, unless a
3003 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3004 * icon will be moved to the right-hand side.
3005 *
3006
3007 * @param icon
3008 * A resource ID in the application's package of the drawable to use.
3009 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003010 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003011 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003012 return setSmallIcon(icon != 0
3013 ? Icon.createWithResource(mContext, icon)
3014 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003015 }
3016
Joe Onoratocb109a02011-01-18 17:57:41 -08003017 /**
3018 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3019 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3020 * LevelListDrawable}.
3021 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003022 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003023 * @param level The level to use for the icon.
3024 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003025 * @see Notification#icon
3026 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003027 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003028 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003029 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003030 return setSmallIcon(icon);
3031 }
3032
3033 /**
3034 * Set the small icon, which will be used to represent the notification in the
3035 * status bar and content view (unless overriden there by a
3036 * {@link #setLargeIcon(Bitmap) large icon}).
3037 *
3038 * @param icon An Icon object to use.
3039 * @see Notification#icon
3040 */
3041 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003042 mN.setSmallIcon(icon);
3043 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3044 mN.icon = icon.getResId();
3045 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003046 return this;
3047 }
3048
Joe Onoratocb109a02011-01-18 17:57:41 -08003049 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003051 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003052 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003053 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003054 return this;
3055 }
3056
Joe Onoratocb109a02011-01-18 17:57:41 -08003057 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003058 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003059 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003060 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003061 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003062 return this;
3063 }
3064
Joe Onoratocb109a02011-01-18 17:57:41 -08003065 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003066 * This provides some additional information that is displayed in the notification. No
3067 * guarantees are given where exactly it is displayed.
3068 *
3069 * <p>This information should only be provided if it provides an essential
3070 * benefit to the understanding of the notification. The more text you provide the
3071 * less readable it becomes. For example, an email client should only provide the account
3072 * name here if more than one email account has been added.</p>
3073 *
3074 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3075 * notification header area.
3076 *
3077 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3078 * this will be shown in the third line of text in the platform notification template.
3079 * You should not be using {@link #setProgress(int, int, boolean)} at the
3080 * same time on those versions; they occupy the same place.
3081 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003082 */
3083 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003084 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003085 return this;
3086 }
3087
3088 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003089 * Provides text that will appear as a link to your application's settings.
3090 *
3091 * <p>This text does not appear within notification {@link Style templates} but may
3092 * appear when the user uses an affordance to learn more about the notification.
3093 * Additionally, this text will not appear unless you provide a valid link target by
3094 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3095 *
3096 * <p>This text is meant to be concise description about what the user can customize
3097 * when they click on this link. The recommended maximum length is 40 characters.
3098 * @param text
3099 * @return
3100 */
3101 public Builder setSettingsText(CharSequence text) {
3102 mN.mSettingsText = safeCharSequence(text);
3103 return this;
3104 }
3105
3106 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003107 * Set the remote input history.
3108 *
3109 * This should be set to the most recent inputs that have been sent
3110 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3111 * longer relevant (e.g. for chat notifications once the other party has responded).
3112 *
3113 * The most recent input must be stored at the 0 index, the second most recent at the
3114 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3115 * and how much of each individual input is shown.
3116 *
3117 * <p>Note: The reply text will only be shown on notifications that have least one action
3118 * with a {@code RemoteInput}.</p>
3119 */
3120 public Builder setRemoteInputHistory(CharSequence[] text) {
3121 if (text == null) {
3122 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3123 } else {
3124 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3125 CharSequence[] safe = new CharSequence[N];
3126 for (int i = 0; i < N; i++) {
3127 safe[i] = safeCharSequence(text[i]);
3128 }
3129 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3130 }
3131 return this;
3132 }
3133
3134 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003135 * Sets the number of items this notification represents. May be displayed as a badge count
3136 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003137 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003138 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003139 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003140 return this;
3141 }
3142
Joe Onoratocb109a02011-01-18 17:57:41 -08003143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003144 * A small piece of additional information pertaining to this notification.
3145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003146 * The platform template will draw this on the last line of the notification, at the far
3147 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003148 *
3149 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3150 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3151 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003152 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003153 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003155 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003156 return this;
3157 }
3158
Joe Onoratocb109a02011-01-18 17:57:41 -08003159 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003160 * Set the progress this notification represents.
3161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003162 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003163 */
3164 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 mN.extras.putInt(EXTRA_PROGRESS, progress);
3166 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3167 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003168 return this;
3169 }
3170
3171 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003172 * Supply a custom RemoteViews to use instead of the platform template.
3173 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003174 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003175 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003176 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003178 return setCustomContentView(views);
3179 }
3180
3181 /**
3182 * Supply custom RemoteViews to use instead of the platform template.
3183 *
3184 * This will override the layout that would otherwise be constructed by this Builder
3185 * object.
3186 */
3187 public Builder setCustomContentView(RemoteViews contentView) {
3188 mN.contentView = contentView;
3189 return this;
3190 }
3191
3192 /**
3193 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3194 *
3195 * This will override the expanded layout that would otherwise be constructed by this
3196 * Builder object.
3197 */
3198 public Builder setCustomBigContentView(RemoteViews contentView) {
3199 mN.bigContentView = contentView;
3200 return this;
3201 }
3202
3203 /**
3204 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3205 *
3206 * This will override the heads-up layout that would otherwise be constructed by this
3207 * Builder object.
3208 */
3209 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3210 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003211 return this;
3212 }
3213
Joe Onoratocb109a02011-01-18 17:57:41 -08003214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003215 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003217 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3218 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3219 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003220 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003221 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003223 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003224 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003225 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003226 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003227 return this;
3228 }
3229
Joe Onoratocb109a02011-01-18 17:57:41 -08003230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003231 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003233 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003234 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003235 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003236 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003237 return this;
3238 }
3239
Joe Onoratocb109a02011-01-18 17:57:41 -08003240 /**
3241 * An intent to launch instead of posting the notification to the status bar.
3242 * Only for use with extremely high-priority notifications demanding the user's
3243 * <strong>immediate</strong> attention, such as an incoming phone call or
3244 * alarm clock that the user has explicitly set to a particular time.
3245 * If this facility is used for something else, please give the user an option
3246 * to turn it off and use a normal notification, as this can be extremely
3247 * disruptive.
3248 *
Chris Wren47c20a12014-06-18 17:27:29 -04003249 * <p>
3250 * The system UI may choose to display a heads-up notification, instead of
3251 * launching this intent, while the user is using the device.
3252 * </p>
3253 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003254 * @param intent The pending intent to launch.
3255 * @param highPriority Passing true will cause this notification to be sent
3256 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003257 *
3258 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003259 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003260 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003261 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003262 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3263 return this;
3264 }
3265
Joe Onoratocb109a02011-01-18 17:57:41 -08003266 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003267 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003269 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003270 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003271 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003272 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003273 return this;
3274 }
3275
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003277 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003278 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003279 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003280 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003282 setTicker(tickerText);
3283 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003284 return this;
3285 }
3286
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003288 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003289 *
3290 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003291 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3292 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003293 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003294 public Builder setLargeIcon(Bitmap b) {
3295 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3296 }
3297
3298 /**
3299 * Add a large icon to the notification content view.
3300 *
3301 * In the platform template, this image will be shown on the left of the notification view
3302 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3303 * badge atop the large icon).
3304 */
3305 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 mN.mLargeIcon = icon;
3307 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003308 return this;
3309 }
3310
Joe Onoratocb109a02011-01-18 17:57:41 -08003311 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003312 * Set the sound to play.
3313 *
John Spurlockc0650f022014-07-19 13:22:39 -04003314 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3315 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003316 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003317 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003318 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003319 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003320 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003321 mN.sound = sound;
3322 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003323 return this;
3324 }
3325
Joe Onoratocb109a02011-01-18 17:57:41 -08003326 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003327 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003328 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003329 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3330 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003331 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003332 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003333 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003334 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003335 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 mN.sound = sound;
3337 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 return this;
3339 }
3340
Joe Onoratocb109a02011-01-18 17:57:41 -08003341 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003342 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3343 * use during playback.
3344 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003345 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003346 * @see Notification#sound
3347 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003348 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003349 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003350 mN.sound = sound;
3351 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003352 return this;
3353 }
3354
3355 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003356 * Set the vibration pattern to use.
3357 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003358 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3359 * <code>pattern</code> parameter.
3360 *
Chris Wren47c20a12014-06-18 17:27:29 -04003361 * <p>
3362 * A notification that vibrates is more likely to be presented as a heads-up notification.
3363 * </p>
3364 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003365 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003366 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003367 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003368 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003369 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003370 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003371 return this;
3372 }
3373
Joe Onoratocb109a02011-01-18 17:57:41 -08003374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003375 * Set the desired color for the indicator LED on the device, as well as the
3376 * blink duty cycle (specified in milliseconds).
3377 *
3378
3379 * Not all devices will honor all (or even any) of these values.
3380 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003381 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003382 * @see Notification#ledARGB
3383 * @see Notification#ledOnMS
3384 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003385 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003386 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003387 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003388 mN.ledARGB = argb;
3389 mN.ledOnMS = onMs;
3390 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003391 if (onMs != 0 || offMs != 0) {
3392 mN.flags |= FLAG_SHOW_LIGHTS;
3393 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003394 return this;
3395 }
3396
Joe Onoratocb109a02011-01-18 17:57:41 -08003397 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003398 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003399 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003400
3401 * Ongoing notifications cannot be dismissed by the user, so your application or service
3402 * must take care of canceling them.
3403 *
3404
3405 * They are typically used to indicate a background task that the user is actively engaged
3406 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3407 * (e.g., a file download, sync operation, active network connection).
3408 *
3409
3410 * @see Notification#FLAG_ONGOING_EVENT
3411 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003413 public Builder setOngoing(boolean ongoing) {
3414 setFlag(FLAG_ONGOING_EVENT, ongoing);
3415 return this;
3416 }
3417
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003419 * Set whether this notification should be colorized. When set, the color set with
3420 * {@link #setColor(int)} will be used as the background color of this notification.
3421 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003422 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3423 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003424 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003425 * For most styles, the coloring will only be applied if the notification is for a
3426 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003427 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003428 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003429 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003430 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003431 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003432 */
3433 public Builder setColorized(boolean colorize) {
3434 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3435 return this;
3436 }
3437
3438 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003439 * Set this flag if you would only like the sound, vibrate
3440 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003441 *
3442 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003443 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003444 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3445 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3446 return this;
3447 }
3448
Joe Onoratocb109a02011-01-18 17:57:41 -08003449 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003450 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003451 *
3452 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003453 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003454 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003455 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003456 return this;
3457 }
3458
Joe Onoratocb109a02011-01-18 17:57:41 -08003459 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003460 * Set whether or not this notification should not bridge to other devices.
3461 *
3462 * <p>Some notifications can be bridged to other devices for remote display.
3463 * This hint can be set to recommend this notification not be bridged.
3464 */
3465 public Builder setLocalOnly(boolean localOnly) {
3466 setFlag(FLAG_LOCAL_ONLY, localOnly);
3467 return this;
3468 }
3469
3470 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003471 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 * <p>
3473 * The value should be one or more of the following fields combined with
3474 * bitwise-or:
3475 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3476 * <p>
3477 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003478 *
3479 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003480 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003481 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003483 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003484 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003485 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003486 return this;
3487 }
3488
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003489 /**
3490 * Set the priority of this notification.
3491 *
3492 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003493 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003494 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003495 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003496 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003498 return this;
3499 }
Joe Malin8d40d042012-11-05 11:36:40 -08003500
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003501 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003502 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003503 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003504 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003506 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003508 return this;
3509 }
3510
3511 /**
Chris Wrendde75302014-03-26 17:24:15 -04003512 * Add a person that is relevant to this notification.
3513 *
Chris Wrene6c48932014-09-29 17:19:27 -04003514 * <P>
3515 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003516 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3517 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3518 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003519 * </P>
3520 *
3521 * <P>
3522 * The person should be specified by the {@code String} representation of a
3523 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3524 * </P>
3525 *
3526 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3527 * URIs. The path part of these URIs must exist in the contacts database, in the
3528 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3529 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3530 * </P>
3531 *
3532 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003533 * @see Notification#EXTRA_PEOPLE
3534 */
Chris Wrene6c48932014-09-29 17:19:27 -04003535 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003537 return this;
3538 }
3539
3540 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003541 * Set this notification to be part of a group of notifications sharing the same key.
3542 * Grouped notifications may display in a cluster or stack on devices which
3543 * support such rendering.
3544 *
3545 * <p>To make this notification the summary for its group, also call
3546 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3547 * {@link #setSortKey}.
3548 * @param groupKey The group key of the group.
3549 * @return this object for method chaining
3550 */
3551 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003553 return this;
3554 }
3555
3556 /**
3557 * Set this notification to be the group summary for a group of notifications.
3558 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003559 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3560 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003561 * @param isGroupSummary Whether this notification should be a group summary.
3562 * @return this object for method chaining
3563 */
3564 public Builder setGroupSummary(boolean isGroupSummary) {
3565 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3566 return this;
3567 }
3568
3569 /**
3570 * Set a sort key that orders this notification among other notifications from the
3571 * same package. This can be useful if an external sort was already applied and an app
3572 * would like to preserve this. Notifications will be sorted lexicographically using this
3573 * value, although providing different priorities in addition to providing sort key may
3574 * cause this value to be ignored.
3575 *
3576 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003577 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003578 *
3579 * @see String#compareTo(String)
3580 */
3581 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003582 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003583 return this;
3584 }
3585
3586 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003587 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003588 *
Griff Hazen720042b2014-02-24 15:46:56 -08003589 * <p>Values within the Bundle will replace existing extras values in this Builder.
3590 *
3591 * @see Notification#extras
3592 */
Griff Hazen959591e2014-05-15 22:26:18 -07003593 public Builder addExtras(Bundle extras) {
3594 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003595 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003596 }
3597 return this;
3598 }
3599
3600 /**
3601 * Set metadata for this notification.
3602 *
3603 * <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 -04003604 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003605 * called.
3606 *
Griff Hazen720042b2014-02-24 15:46:56 -08003607 * <p>Replaces any existing extras values with those from the provided Bundle.
3608 * Use {@link #addExtras} to merge in metadata instead.
3609 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003610 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 */
Griff Hazen959591e2014-05-15 22:26:18 -07003612 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003613 if (extras != null) {
3614 mUserExtras = extras;
3615 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 return this;
3617 }
3618
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003619 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003620 * Get the current metadata Bundle used by this notification Builder.
3621 *
3622 * <p>The returned Bundle is shared with this Builder.
3623 *
3624 * <p>The current contents of this Bundle are copied into the Notification each time
3625 * {@link #build()} is called.
3626 *
3627 * @see Notification#extras
3628 */
3629 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003630 return mUserExtras;
3631 }
3632
3633 private Bundle getAllExtras() {
3634 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3635 saveExtras.putAll(mN.extras);
3636 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003637 }
3638
3639 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003640 * Add an action to this notification. Actions are typically displayed by
3641 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003642 * <p>
3643 * Every action must have an icon (32dp square and matching the
3644 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3645 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3646 * <p>
3647 * A notification in its expanded form can display up to 3 actions, from left to right in
3648 * the order they were added. Actions will not be displayed when the notification is
3649 * collapsed, however, so be sure that any essential functions may be accessed by the user
3650 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003651 *
3652 * @param icon Resource ID of a drawable that represents the action.
3653 * @param title Text describing the action.
3654 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003655 *
3656 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003657 */
Dan Sandler86647982015-05-13 23:41:13 -04003658 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003659 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003660 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003661 return this;
3662 }
3663
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003664 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003665 * Add an action to this notification. Actions are typically displayed by
3666 * the system as a button adjacent to the notification content.
3667 * <p>
3668 * Every action must have an icon (32dp square and matching the
3669 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3670 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3671 * <p>
3672 * A notification in its expanded form can display up to 3 actions, from left to right in
3673 * the order they were added. Actions will not be displayed when the notification is
3674 * collapsed, however, so be sure that any essential functions may be accessed by the user
3675 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3676 *
3677 * @param action The action to add.
3678 */
3679 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003680 if (action != null) {
3681 mActions.add(action);
3682 }
Griff Hazen959591e2014-05-15 22:26:18 -07003683 return this;
3684 }
3685
3686 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003687 * Alter the complete list of actions attached to this notification.
3688 * @see #addAction(Action).
3689 *
3690 * @param actions
3691 * @return
3692 */
3693 public Builder setActions(Action... actions) {
3694 mActions.clear();
3695 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003696 if (actions[i] != null) {
3697 mActions.add(actions[i]);
3698 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 }
3700 return this;
3701 }
3702
3703 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003704 * Add a rich notification style to be applied at build time.
3705 *
3706 * @param style Object responsible for modifying the notification style.
3707 */
3708 public Builder setStyle(Style style) {
3709 if (mStyle != style) {
3710 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003711 if (mStyle != null) {
3712 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003713 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3714 } else {
3715 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003716 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003717 }
3718 return this;
3719 }
3720
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003721 /**
3722 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003723 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003724 * @return The same Builder.
3725 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003726 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003727 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003728 return this;
3729 }
3730
3731 /**
3732 * Supply a replacement Notification whose contents should be shown in insecure contexts
3733 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3734 * @param n A replacement notification, presumably with some or all info redacted.
3735 * @return The same Builder.
3736 */
3737 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 if (n != null) {
3739 mN.publicVersion = new Notification();
3740 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3741 } else {
3742 mN.publicVersion = null;
3743 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003744 return this;
3745 }
3746
Griff Hazenb720abe2014-05-20 13:15:30 -07003747 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003748 * Apply an extender to this notification builder. Extenders may be used to add
3749 * metadata or change options on this builder.
3750 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003751 public Builder extend(Extender extender) {
3752 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003753 return this;
3754 }
3755
Dan Sandler4e787062015-06-17 15:09:48 -04003756 /**
3757 * @hide
3758 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003759 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003760 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003762 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003764 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003765 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003766 }
3767
Dan Sandler26e81cf2014-05-06 10:01:27 -04003768 /**
3769 * Sets {@link Notification#color}.
3770 *
3771 * @param argb The accent color to use
3772 *
3773 * @return The same Builder.
3774 */
Tor Norbye80756e32015-03-02 09:39:27 -08003775 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003776 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003777 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003778 return this;
3779 }
3780
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003781 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003782 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3783 // This user can never be a badged profile,
3784 // and also includes USER_ALL system notifications.
3785 return null;
3786 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003787 // Note: This assumes that the current user can read the profile badge of the
3788 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003789 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003790 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003791 }
3792
3793 private Bitmap getProfileBadge() {
3794 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003795 if (badge == null) {
3796 return null;
3797 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003798 final int size = mContext.getResources().getDimensionPixelSize(
3799 R.dimen.notification_badge_size);
3800 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003801 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003802 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003803 badge.draw(canvas);
3804 return bitmap;
3805 }
3806
Selim Cinekc848c3a2016-01-13 15:27:30 -08003807 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003808 Bitmap profileBadge = getProfileBadge();
3809
Kenny Guy98193ea2014-07-24 19:54:37 +01003810 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003811 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3812 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003813 if (isColorized()) {
3814 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3815 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3816 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003817 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003818 }
3819
Christoph Studerfe718432014-09-01 18:21:18 +02003820 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003821 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003822 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003823 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003824 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003825 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003826 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003827 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003828 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003829 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003830 }
3831
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003832 /**
3833 * Resets the notification header to its original state
3834 */
3835 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003836 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3837 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003838 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003839 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003840 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003841 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003842 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003843 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003844 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003845 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003846 contentView.setImageViewIcon(R.id.profile_badge, null);
3847 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003848 }
3849
3850 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003851 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3852 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003853 }
3854
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003855 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003856 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003857 }
3858
3859 /**
3860 * @param hasProgress whether the progress bar should be shown and set
3861 */
3862 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003863 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3864 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003865 }
3866
Adrian Roos70d7aa32017-01-11 15:39:06 -08003867 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003868 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003869
Christoph Studerfe718432014-09-01 18:21:18 +02003870 resetStandardTemplate(contentView);
3871
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003872 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003873 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003874 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003875 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003876 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3877 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003878 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003879 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003880 if (!p.ambient) {
3881 setTextViewColorPrimary(contentView, R.id.title);
3882 }
Selim Cinek954cc232016-05-20 13:29:23 -07003883 contentView.setViewLayoutWidth(R.id.title, showProgress
3884 ? ViewGroup.LayoutParams.WRAP_CONTENT
3885 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003886 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003887 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003888 int textId = showProgress ? com.android.internal.R.id.text_line_1
3889 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003890 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003891 if (!p.ambient) {
3892 setTextViewColorSecondary(contentView, textId);
3893 }
Selim Cinek41598732016-01-11 16:58:37 -08003894 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003895 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003896
Selim Cinek279fa862016-06-14 10:57:25 -07003897 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003898
Selim Cinek29603462015-11-17 19:04:39 -08003899 return contentView;
3900 }
3901
Selim Cinek7b9605b2017-01-19 17:36:00 -08003902 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3903 ensureColors();
3904 contentView.setTextColor(id, mPrimaryTextColor);
3905 }
3906
Selim Cinek389edcd2017-05-11 19:16:44 -07003907 /**
3908 * @return the primary text color
3909 * @hide
3910 */
3911 @VisibleForTesting
3912 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003913 ensureColors();
3914 return mPrimaryTextColor;
3915 }
3916
Selim Cinek389edcd2017-05-11 19:16:44 -07003917 /**
3918 * @return the secondary text color
3919 * @hide
3920 */
3921 @VisibleForTesting
3922 public int getSecondaryTextColor() {
3923 ensureColors();
3924 return mSecondaryTextColor;
3925 }
3926
Selim Cinek7b9605b2017-01-19 17:36:00 -08003927 private int getActionBarColor() {
3928 ensureColors();
3929 return mActionBarColor;
3930 }
3931
Selim Cinek622c64a2017-04-17 17:10:05 -07003932 private int getActionBarColorDeEmphasized() {
3933 int backgroundColor = getBackgroundColor();
3934 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3935 }
3936
Selim Cinek7b9605b2017-01-19 17:36:00 -08003937 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3938 ensureColors();
3939 contentView.setTextColor(id, mSecondaryTextColor);
3940 }
3941
3942 private void ensureColors() {
3943 int backgroundColor = getBackgroundColor();
3944 if (mPrimaryTextColor == COLOR_INVALID
3945 || mSecondaryTextColor == COLOR_INVALID
3946 || mActionBarColor == COLOR_INVALID
3947 || mTextColorsAreForBackground != backgroundColor) {
3948 mTextColorsAreForBackground = backgroundColor;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003949 if (mForegroundColor == COLOR_INVALID || !isColorized()) {
3950 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3951 backgroundColor);
3952 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3953 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003954 if (backgroundColor != COLOR_DEFAULT
3955 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3956 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3957 mPrimaryTextColor, backgroundColor, 4.5);
3958 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3959 mSecondaryTextColor, backgroundColor, 4.5);
3960 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07003961 } else {
3962 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
3963 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
3964 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
3965 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07003966 // We only respect the given colors if worst case Black or White still has
3967 // contrast
3968 boolean backgroundLight = backLum > textLum
3969 && satisfiesTextContrast(backgroundColor, Color.BLACK)
3970 || backLum <= textLum
3971 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003972 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07003973 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003974 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3975 mForegroundColor,
3976 backgroundColor,
3977 true /* findFG */,
3978 4.5f);
3979 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003980 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003981 } else {
3982 mSecondaryTextColor =
3983 NotificationColorUtil.findContrastColorAgainstDark(
3984 mForegroundColor,
3985 backgroundColor,
3986 true /* findFG */,
3987 4.5f);
3988 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003989 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003990 }
3991 } else {
3992 mPrimaryTextColor = mForegroundColor;
3993 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003994 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
3995 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003996 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
3997 backgroundColor) < 4.5f) {
3998 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07003999 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004000 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4001 mSecondaryTextColor,
4002 backgroundColor,
4003 true /* findFG */,
4004 4.5f);
4005 } else {
4006 mSecondaryTextColor
4007 = NotificationColorUtil.findContrastColorAgainstDark(
4008 mSecondaryTextColor,
4009 backgroundColor,
4010 true /* findFG */,
4011 4.5f);
4012 }
4013 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004014 mSecondaryTextColor, backgroundLight
4015 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4016 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004017 }
4018 }
4019 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004020 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4021 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004022 }
4023 }
4024
4025 private void updateBackgroundColor(RemoteViews contentView) {
4026 if (isColorized()) {
4027 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4028 getBackgroundColor());
4029 } else {
4030 // Clear it!
4031 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4032 0);
4033 }
4034 }
4035
Selim Cinek860b6da2015-12-16 19:02:19 -08004036 /**
4037 * @param remoteView the remote view to update the minheight in
4038 * @param hasMinHeight does it have a mimHeight
4039 * @hide
4040 */
4041 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4042 int minHeight = 0;
4043 if (hasMinHeight) {
4044 // we need to set the minHeight of the notification
4045 minHeight = mContext.getResources().getDimensionPixelSize(
4046 com.android.internal.R.dimen.notification_min_content_height);
4047 }
4048 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4049 }
4050
Selim Cinek29603462015-11-17 19:04:39 -08004051 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004052 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4053 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4054 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4055 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004056 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004057 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004058 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004059 contentView.setProgressBackgroundTintList(
4060 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4061 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004062 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004063 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004064 contentView.setProgressTintList(R.id.progress, colorStateList);
4065 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004066 }
Selim Cinek29603462015-11-17 19:04:39 -08004067 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004068 } else {
4069 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004070 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004071 }
Joe Onorato561d3852010-11-20 18:09:34 -08004072 }
4073
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004074 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07004075 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4076 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4077 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004078 if (mN.mLargeIcon != null) {
4079 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4080 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4081 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004082 int endMargin = R.dimen.notification_content_picture_margin;
4083 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4084 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4085 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004086 // Bind the reply action
4087 Action action = findReplyAction();
4088 contentView.setViewVisibility(R.id.reply_icon_action, action != null
4089 ? View.VISIBLE
4090 : View.GONE);
4091
4092 if (action != null) {
4093 int contrastColor = resolveContrastColor();
4094 contentView.setDrawableParameters(R.id.reply_icon_action,
4095 true /* targetBackground */,
4096 -1,
4097 contrastColor,
4098 PorterDuff.Mode.SRC_ATOP, -1);
4099 int iconColor = NotificationColorUtil.isColorLight(contrastColor)
4100 ? Color.BLACK : Color.WHITE;
4101 contentView.setDrawableParameters(R.id.reply_icon_action,
4102 false /* targetBackground */,
4103 -1,
4104 iconColor,
4105 PorterDuff.Mode.SRC_ATOP, -1);
4106 contentView.setOnClickPendingIntent(R.id.right_icon,
4107 action.actionIntent);
4108 contentView.setOnClickPendingIntent(R.id.reply_icon_action,
4109 action.actionIntent);
4110 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
4111 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4112
4113 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004114 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004115 contentView.setViewVisibility(R.id.right_icon_container, mN.mLargeIcon != null
4116 ? View.VISIBLE
4117 : View.GONE);
4118 }
4119
4120 private Action findReplyAction() {
4121 ArrayList<Action> actions = mActions;
4122 if (mOriginalActions != null) {
4123 actions = mOriginalActions;
4124 }
4125 int numActions = actions.size();
4126 for (int i = 0; i < numActions; i++) {
4127 Action action = actions.get(i);
4128 if (hasValidRemoteInput(action)) {
4129 return action;
4130 }
4131 }
4132 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004133 }
4134
Adrian Roos487374f2017-01-11 15:48:14 -08004135 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4136 bindSmallIcon(contentView, ambient);
4137 bindHeaderAppName(contentView, ambient);
4138 if (!ambient) {
4139 // Ambient view does not have these
4140 bindHeaderText(contentView);
4141 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004142 bindProfileBadge(contentView);
4143 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004144 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004145 }
4146
4147 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004148 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08004149 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004150 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004151 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004152 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004153 }
4154
Selim Cinek99104832017-01-25 14:47:33 -08004155 /**
4156 * @return the color that is used as the first primary highlight color. This is applied
4157 * in several places like the action buttons or the app name in the header.
4158 */
4159 private int getPrimaryHighlightColor() {
4160 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4161 }
4162
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004163 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4164 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004165 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004166 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004167 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4168 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4169 contentView.setLong(R.id.chronometer, "setBase",
4170 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4171 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004172 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004173 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004174 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004175 } else {
4176 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4177 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004178 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004179 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004180 } else {
4181 // We still want a time to be set but gone, such that we can show and hide it
4182 // on demand in case it's a child notification without anything in the header
4183 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004184 }
4185 }
4186
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004187 private void bindHeaderText(RemoteViews contentView) {
4188 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4189 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004190 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004191 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004192 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004193 if (headerText == null
4194 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4195 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4196 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4197 }
4198 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004199 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004200 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004201 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004202 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4203 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004204 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004205 }
4206 }
4207
Adrian Rooseba05822016-04-22 17:09:27 -07004208 /**
4209 * @hide
4210 */
4211 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004212 CharSequence name = null;
4213 final PackageManager pm = mContext.getPackageManager();
4214 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4215 // only system packages which lump together a bunch of unrelated stuff
4216 // may substitute a different name to make the purpose of the
4217 // notification more clear. the correct package label should always
4218 // be accessible via SystemUI.
4219 final String pkg = mContext.getPackageName();
4220 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4221 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4222 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4223 name = subName;
4224 } else {
4225 Log.w(TAG, "warning: pkg "
4226 + pkg + " attempting to substitute app name '" + subName
4227 + "' without holding perm "
4228 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4229 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004230 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004231 if (TextUtils.isEmpty(name)) {
4232 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4233 }
4234 if (TextUtils.isEmpty(name)) {
4235 // still nothing?
4236 return null;
4237 }
4238
4239 return String.valueOf(name);
4240 }
Adrian Roos487374f2017-01-11 15:48:14 -08004241 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004242 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004243 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004244 setTextViewColorPrimary(contentView, R.id.app_name_text);
4245 } else {
4246 contentView.setTextColor(R.id.app_name_text,
4247 ambient ? resolveAmbientColor() : resolveContrastColor());
4248 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004249 }
4250
Adrian Roos487374f2017-01-11 15:48:14 -08004251 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004252 if (mN.mSmallIcon == null && mN.icon != 0) {
4253 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4254 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004255 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004256 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4257 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004258 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004259 }
4260
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004261 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004262 * @return true if the built notification will show the time or the chronometer; false
4263 * otherwise
4264 */
4265 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004266 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004267 }
4268
Christoph Studerfe718432014-09-01 18:21:18 +02004269 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004270 // actions_container is only reset when there are no actions to avoid focus issues with
4271 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004272 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004273 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004274
4275 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4276 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4277
4278 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4279 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4280 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4281 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004282
4283 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004284 }
4285
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004286 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004287 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004288 }
4289
Adrian Roos70d7aa32017-01-11 15:39:06 -08004290 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4291 StandardTemplateParams p) {
4292 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004293
Christoph Studerfe718432014-09-01 18:21:18 +02004294 resetStandardTemplateWithActions(big);
4295
Adrian Roose458aa82015-12-08 16:17:19 -08004296 boolean validRemoteInput = false;
4297
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004298 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004299 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004300 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004301 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004302 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004303 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004304 if (p.ambient) {
4305 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004306 } else if (isColorized()) {
4307 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4308 } else {
4309 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4310 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004311 }
Adrian Roosf852a422016-06-03 13:33:43 -07004312 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4313 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004314 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004315 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004316 Action action = mActions.get(i);
4317 validRemoteInput |= hasValidRemoteInput(action);
4318
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004319 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004320 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004321 big.addView(R.id.actions, button);
4322 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004323 } else {
4324 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004325 }
Adrian Roose458aa82015-12-08 16:17:19 -08004326
4327 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004328 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004329 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4330 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4331 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004332 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004333
4334 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4335 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4336 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004337 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004338
4339 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4340 big.setViewVisibility(
4341 R.id.notification_material_reply_text_3, View.VISIBLE);
4342 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004343 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004344 }
4345 }
4346 }
4347
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004348 return big;
4349 }
4350
Adrian Roose458aa82015-12-08 16:17:19 -08004351 private boolean hasValidRemoteInput(Action action) {
4352 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4353 // Weird actions
4354 return false;
4355 }
4356
4357 RemoteInput[] remoteInputs = action.getRemoteInputs();
4358 if (remoteInputs == null) {
4359 return false;
4360 }
4361
4362 for (RemoteInput r : remoteInputs) {
4363 CharSequence[] choices = r.getChoices();
4364 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4365 return true;
4366 }
4367 }
4368 return false;
4369 }
4370
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004371 /**
4372 * Construct a RemoteViews for the final 1U notification layout. In order:
4373 * 1. Custom contentView from the caller
4374 * 2. Style's proposed content view
4375 * 3. Standard template view
4376 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004377 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004378 return createContentView(false /* increasedheight */ );
4379 }
4380
4381 /**
4382 * Construct a RemoteViews for the smaller content view.
4383 *
4384 * @param increasedHeight true if this layout be created with an increased height. Some
4385 * styles may support showing more then just that basic 1U size
4386 * and the system may decide to render important notifications
4387 * slightly bigger even when collapsed.
4388 *
4389 * @hide
4390 */
4391 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004392 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004393 return mN.contentView;
4394 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004395 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004396 if (styleView != null) {
4397 return styleView;
4398 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004399 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004400 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004401 }
4402
Selim Cineka7679b62017-05-10 16:33:25 -07004403 private boolean useExistingRemoteView() {
4404 return mStyle == null || (!mStyle.displayCustomViewInline()
4405 && !mRebuildStyledRemoteViews);
4406 }
4407
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 /**
4409 * Construct a RemoteViews for the final big notification layout.
4410 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004411 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004412 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004413 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004414 return mN.bigContentView;
4415 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004416 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004417 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004418 } else if (mActions.size() != 0) {
4419 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004420 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004421 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004422 return result;
4423 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004424
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004425 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004426 * Construct a RemoteViews for the final notification header only. This will not be
4427 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004428 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004429 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004430 * @hide
4431 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004432 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004433 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4434 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004435 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004436 ambient ? R.layout.notification_template_ambient_header
4437 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004438 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004439 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004440 if (colorized != null) {
4441 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4442 } else {
4443 mN.extras.remove(EXTRA_COLORIZED);
4444 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004445 return header;
4446 }
4447
Adrian Roos487374f2017-01-11 15:48:14 -08004448 /**
4449 * Construct a RemoteViews for the ambient version of the notification.
4450 *
4451 * @hide
4452 */
4453 public RemoteViews makeAmbientNotification() {
4454 RemoteViews ambient = applyStandardTemplateWithActions(
4455 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004456 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004457 return ambient;
4458 }
4459
Selim Cinek29603462015-11-17 19:04:39 -08004460 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004461 if (result != null) {
4462 result.setViewVisibility(R.id.text_line_1, View.GONE);
4463 }
Selim Cinek29603462015-11-17 19:04:39 -08004464 }
4465
Selim Cinek6743c0b2017-01-18 18:24:01 -08004466 /**
4467 * Adapt the Notification header if this view is used as an expanded view.
4468 *
4469 * @hide
4470 */
4471 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004472 if (result != null) {
4473 result.setBoolean(R.id.notification_header, "setExpanded", true);
4474 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004475 }
4476
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004477 /**
4478 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004479 *
4480 * @param increasedHeight true if this layout be created with an increased height. Some
4481 * styles may support showing more then just that basic 1U size
4482 * and the system may decide to render important notifications
4483 * slightly bigger even when collapsed.
4484 *
4485 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004486 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004487 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004488 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004489 return mN.headsUpContentView;
4490 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004491 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4492 if (styleView != null) {
4493 return styleView;
4494 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004495 } else if (mActions.size() == 0) {
4496 return null;
4497 }
4498
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004499 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004500 }
4501
Selim Cinek624c02db2015-12-14 21:00:02 -08004502 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004503 * Construct a RemoteViews for the final heads-up notification layout.
4504 */
4505 public RemoteViews createHeadsUpContentView() {
4506 return createHeadsUpContentView(false /* useIncreasedHeight */);
4507 }
4508
4509 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004510 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4511 *
4512 * @hide
4513 */
4514 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004515 return makePublicView(false /* ambient */);
4516 }
4517
4518 /**
4519 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4520 *
4521 * @hide
4522 */
4523 public RemoteViews makePublicAmbientNotification() {
4524 return makePublicView(true /* ambient */);
4525 }
4526
4527 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004528 if (mN.publicVersion != null) {
4529 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004530 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004531 }
4532 Bundle savedBundle = mN.extras;
4533 Style style = mStyle;
4534 mStyle = null;
4535 Icon largeIcon = mN.mLargeIcon;
4536 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004537 Bitmap largeIconLegacy = mN.largeIcon;
4538 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004539 ArrayList<Action> actions = mActions;
4540 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004541 Bundle publicExtras = new Bundle();
4542 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4543 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4544 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4545 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004546 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4547 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004548 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004549 RemoteViews view;
4550 if (ambient) {
4551 publicExtras.putCharSequence(EXTRA_TITLE,
4552 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4553 view = makeAmbientNotification();
4554 } else{
4555 view = makeNotificationHeader(false /* ambient */);
4556 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4557 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004558 mN.extras = savedBundle;
4559 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004560 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004561 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004562 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004563 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004564 }
4565
Selim Cinek6743c0b2017-01-18 18:24:01 -08004566 /**
4567 * Construct a content view for the display when low - priority
4568 *
4569 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4570 * a new subtext is created consisting of the content of the
4571 * notification.
4572 * @hide
4573 */
4574 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4575 int color = mN.color;
4576 mN.color = COLOR_DEFAULT;
4577 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4578 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4579 CharSequence newSummary = createSummaryText();
4580 if (!TextUtils.isEmpty(newSummary)) {
4581 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4582 }
4583 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004584
Adrian Roos6f6e1592017-05-02 16:22:53 -07004585 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004586 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004587 if (summary != null) {
4588 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4589 } else {
4590 mN.extras.remove(EXTRA_SUB_TEXT);
4591 }
4592 mN.color = color;
4593 return header;
4594 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004595
Selim Cinek6743c0b2017-01-18 18:24:01 -08004596 private CharSequence createSummaryText() {
4597 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4598 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4599 return titleText;
4600 }
4601 SpannableStringBuilder summary = new SpannableStringBuilder();
4602 if (titleText == null) {
4603 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4604 }
4605 BidiFormatter bidi = BidiFormatter.getInstance();
4606 if (titleText != null) {
4607 summary.append(bidi.unicodeWrap(titleText));
4608 }
4609 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4610 if (titleText != null && contentText != null) {
4611 summary.append(bidi.unicodeWrap(mContext.getText(
4612 R.string.notification_header_divider_symbol_with_spaces)));
4613 }
4614 if (contentText != null) {
4615 summary.append(bidi.unicodeWrap(contentText));
4616 }
4617 return summary;
4618 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004619
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004620 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004621 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004622 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004623 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004624 emphazisedMode ? getEmphasizedActionLayoutResource()
4625 : tombstone ? getActionTombstoneLayoutResource()
4626 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004627 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004628 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004629 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004630 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004631 if (action.mRemoteInputs != null) {
4632 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4633 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004634 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004635 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004636 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004637 int bgColor;
4638 if (isColorized()) {
4639 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4640 } else {
4641 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4642 : R.color.notification_action_list_dark);
4643 }
Selim Cinek981962e2016-07-20 20:41:58 -07004644 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004645 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004646 CharSequence title = action.title;
4647 ColorStateList[] outResultColor = null;
4648 if (isLegacy()) {
4649 title = clearColorSpans(title);
4650 } else {
4651 outResultColor = new ColorStateList[1];
4652 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4653 }
4654 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004655 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004656 if (outResultColor != null && outResultColor[0] != null) {
4657 // We need to set the text color as well since changing a text to uppercase
4658 // clears its spans.
4659 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004660 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004661 button.setTextColor(R.id.action0,resolveContrastColor());
4662 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004663 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004664 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004665 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004666 setTextViewColorPrimary(button, R.id.action0);
4667 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004668 button.setTextColor(R.id.action0,
4669 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004670 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004671 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004672 return button;
4673 }
4674
Joe Onoratocb109a02011-01-18 17:57:41 -08004675 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004676 * Clears all color spans of a text
4677 * @param charSequence the input text
4678 * @return the same text but without color spans
4679 */
4680 private CharSequence clearColorSpans(CharSequence charSequence) {
4681 if (charSequence instanceof Spanned) {
4682 Spanned ss = (Spanned) charSequence;
4683 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4684 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4685 for (Object span : spans) {
4686 Object resultSpan = span;
4687 if (resultSpan instanceof CharacterStyle) {
4688 resultSpan = ((CharacterStyle) span).getUnderlying();
4689 }
4690 if (resultSpan instanceof TextAppearanceSpan) {
4691 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4692 if (originalSpan.getTextColor() != null) {
4693 resultSpan = new TextAppearanceSpan(
4694 originalSpan.getFamily(),
4695 originalSpan.getTextStyle(),
4696 originalSpan.getTextSize(),
4697 null,
4698 originalSpan.getLinkTextColor());
4699 }
4700 } else if (resultSpan instanceof ForegroundColorSpan
4701 || (resultSpan instanceof BackgroundColorSpan)) {
4702 continue;
4703 } else {
4704 resultSpan = span;
4705 }
4706 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4707 ss.getSpanFlags(span));
4708 }
4709 return builder;
4710 }
4711 return charSequence;
4712 }
4713
4714 /**
4715 * 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(
4746 colors[i], background);
4747 }
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(
4765 foregroundColor, background);
4766 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) {
Adrian Roos487374f2017-01-11 15:48:14 -08004816 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004817 PorterDuff.Mode.SRC_ATOP, -1);
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
Adrian Roos4ff3b122016-02-01 12:26:13 -08004833 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004834 PorterDuff.Mode.SRC_ATOP, -1);
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,
4860 background);
4861 }
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) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004951 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004952 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004953
Adrian Roos5081c0d2016-02-26 16:04:19 -08004954 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004955 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004956 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004957 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004958 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4959 mN.contentView.getSequenceNumber());
4960 }
4961 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004962 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004963 if (mN.bigContentView != null) {
4964 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4965 mN.bigContentView.getSequenceNumber());
4966 }
4967 }
4968 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004969 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004970 if (mN.headsUpContentView != null) {
4971 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4972 mN.headsUpContentView.getSequenceNumber());
4973 }
4974 }
4975 }
4976
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004977 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4978 mN.flags |= FLAG_SHOW_LIGHTS;
4979 }
4980
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004981 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004982 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004983
4984 /**
4985 * Apply this Builder to an existing {@link Notification} object.
4986 *
4987 * @hide
4988 */
4989 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004990 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004991 return n;
4992 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004993
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004994 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004995 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4996 * change.
4997 *
4998 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4999 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005000 * @hide
5001 */
Adrian Roos184bfe022016-03-03 13:41:44 -08005002 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005003 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005004
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005005 // Only strip views for known Styles because we won't know how to
5006 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08005007 if (!TextUtils.isEmpty(templateClass)
5008 && getNotificationStyleClass(templateClass) == null) {
5009 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005010 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005011
5012 // Only strip unmodified BuilderRemoteViews.
5013 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005014 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005015 n.contentView.getSequenceNumber();
5016 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005017 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005018 n.bigContentView.getSequenceNumber();
5019 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005020 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005021 n.headsUpContentView.getSequenceNumber();
5022
5023 // Nothing to do here, no need to clone.
5024 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
5025 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005026 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005027
5028 Notification clone = n.clone();
5029 if (stripContentView) {
5030 clone.contentView = null;
5031 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5032 }
5033 if (stripBigContentView) {
5034 clone.bigContentView = null;
5035 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5036 }
5037 if (stripHeadsUpContentView) {
5038 clone.headsUpContentView = null;
5039 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5040 }
5041 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005042 }
5043
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005044 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005045 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005046 }
5047
5048 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005049 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005050 }
5051
5052 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005053 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005054 }
5055
5056 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005057 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005058 }
5059
5060 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005061 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005062 }
5063
Adrian Roosc1a80b02016-04-05 14:54:55 -07005064 private int getMessagingLayoutResource() {
5065 return R.layout.notification_template_material_messaging;
5066 }
5067
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005068 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005069 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005070 }
5071
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005072 private int getEmphasizedActionLayoutResource() {
5073 return R.layout.notification_material_action_emphasized;
5074 }
5075
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005076 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005077 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005078 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005079
5080 private int getBackgroundColor() {
5081 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005082 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005083 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005084 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5085 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005086 }
5087 }
5088
5089 private boolean isColorized() {
5090 return mN.isColorized();
5091 }
Selim Cinek99104832017-01-25 14:47:33 -08005092
5093 private boolean textColorsNeedInversion() {
5094 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5095 return false;
5096 }
5097 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5098 return targetSdkVersion > Build.VERSION_CODES.M
5099 && targetSdkVersion < Build.VERSION_CODES.O;
5100 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005101
5102 /**
5103 * Set a color palette to be used as the background and textColors
5104 *
5105 * @param backgroundColor the color to be used as the background
5106 * @param foregroundColor the color to be used as the foreground
5107 *
5108 * @hide
5109 */
5110 public void setColorPalette(int backgroundColor, int foregroundColor) {
5111 mBackgroundColor = backgroundColor;
5112 mForegroundColor = foregroundColor;
5113 mTextColorsAreForBackground = COLOR_INVALID;
5114 ensureColors();
5115 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005116
5117 /**
5118 * Sets the background color for this notification to be a different one then the default.
5119 * This is mainly used to calculate contrast and won't necessarily be applied to the
5120 * background.
5121 *
5122 * @hide
5123 */
5124 public void setBackgroundColorHint(int backgroundColor) {
5125 mBackgroundColorHint = backgroundColor;
5126 }
Selim Cineka7679b62017-05-10 16:33:25 -07005127
5128
5129 /**
5130 * Forces all styled remoteViews to be built from scratch and not use any cached
5131 * RemoteViews.
5132 * This is needed for legacy apps that are baking in their remoteviews into the
5133 * notification.
5134 *
5135 * @hide
5136 */
5137 public void setRebuildStyledRemoteViews(boolean rebuild) {
5138 mRebuildStyledRemoteViews = rebuild;
5139 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005140 }
5141
5142 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005143 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005144 */
Selim Cinek22714f12017-04-13 16:23:53 -07005145 private boolean isForegroundService() {
5146 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005147 }
5148
5149 /**
Selim Cinek99104832017-01-25 14:47:33 -08005150 * @return whether this notification has a media session attached
5151 * @hide
5152 */
5153 public boolean hasMediaSession() {
5154 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5155 }
5156
5157 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005158 * @return the style class of this notification
5159 * @hide
5160 */
5161 public Class<? extends Notification.Style> getNotificationStyle() {
5162 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5163
5164 if (!TextUtils.isEmpty(templateClass)) {
5165 return Notification.getNotificationStyleClass(templateClass);
5166 }
5167 return null;
5168 }
5169
5170 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005171 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005172 *
5173 * @hide
5174 */
5175 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005176 if (isColorizedMedia()) {
5177 return true;
5178 }
Julia Reynoldsd71c5a92017-06-30 13:34:01 -04005179 return extras.getBoolean(EXTRA_COLORIZED)
5180 && (hasColorizedPermission() || isForegroundService());
5181 }
5182
5183 /**
5184 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5185 * permission. The permission is checked when a notification is enqueued.
5186 */
5187 private boolean hasColorizedPermission() {
5188 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005189 }
5190
5191 /**
5192 * @return true if this notification is colorized and it is a media notification
5193 *
5194 * @hide
5195 */
5196 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005197 Class<? extends Style> style = getNotificationStyle();
5198 if (MediaStyle.class.equals(style)) {
5199 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5200 if ((colorized == null || colorized) && hasMediaSession()) {
5201 return true;
5202 }
5203 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5204 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5205 return true;
5206 }
5207 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005208 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005209 }
5210
Selim Cinek0847acd2017-04-24 19:48:29 -07005211
5212 /**
5213 * @return true if this is a media notification
5214 *
5215 * @hide
5216 */
5217 public boolean isMediaNotification() {
5218 Class<? extends Style> style = getNotificationStyle();
5219 if (MediaStyle.class.equals(style)) {
5220 return true;
5221 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5222 return true;
5223 }
5224 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005225 }
5226
Selim Cinek279fa862016-06-14 10:57:25 -07005227 private boolean hasLargeIcon() {
5228 return mLargeIcon != null || largeIcon != null;
5229 }
5230
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005231 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005232 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005233 * @hide
5234 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005235 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005236 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5237 }
5238
5239 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005240 * @return true if the notification will show a chronometer; false otherwise
5241 * @hide
5242 */
5243 public boolean showsChronometer() {
5244 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5245 }
5246
5247 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005248 * @hide
5249 */
5250 @SystemApi
5251 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5252 Class<? extends Style>[] classes = new Class[] {
5253 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5254 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5255 MessagingStyle.class };
5256 for (Class<? extends Style> innerClass : classes) {
5257 if (templateClass.equals(innerClass.getName())) {
5258 return innerClass;
5259 }
5260 }
5261 return null;
5262 }
5263
5264 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005265 * An object that can apply a rich notification style to a {@link Notification.Builder}
5266 * object.
5267 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005268 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005269 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005270
5271 /**
5272 * @hide
5273 */
5274 protected CharSequence mSummaryText = null;
5275
5276 /**
5277 * @hide
5278 */
5279 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005280
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005281 protected Builder mBuilder;
5282
Chris Wrend6297db2012-05-03 16:20:13 -04005283 /**
5284 * Overrides ContentTitle in the big form of the template.
5285 * This defaults to the value passed to setContentTitle().
5286 */
5287 protected void internalSetBigContentTitle(CharSequence title) {
5288 mBigContentTitle = title;
5289 }
5290
5291 /**
5292 * Set the first line of text after the detail section in the big form of the template.
5293 */
5294 protected void internalSetSummaryText(CharSequence cs) {
5295 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005296 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005297 }
5298
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005299 public void setBuilder(Builder builder) {
5300 if (mBuilder != builder) {
5301 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005302 if (mBuilder != null) {
5303 mBuilder.setStyle(this);
5304 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005305 }
5306 }
5307
Chris Wrend6297db2012-05-03 16:20:13 -04005308 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005309 if (mBuilder == null) {
5310 throw new IllegalArgumentException("Style requires a valid Builder object");
5311 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005312 }
Chris Wrend6297db2012-05-03 16:20:13 -04005313
5314 protected RemoteViews getStandardView(int layoutId) {
5315 checkBuilder();
5316
Christoph Studer4600f9b2014-07-22 22:44:43 +02005317 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005318 CharSequence oldBuilderContentTitle =
5319 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005320 if (mBigContentTitle != null) {
5321 mBuilder.setContentTitle(mBigContentTitle);
5322 }
5323
Chris Wrend6297db2012-05-03 16:20:13 -04005324 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5325
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005326 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005327
Chris Wrend6297db2012-05-03 16:20:13 -04005328 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5329 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005330 } else {
5331 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005332 }
5333
Chris Wrend6297db2012-05-03 16:20:13 -04005334 return contentView;
5335 }
5336
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005337 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005338 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005339 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005340 *
5341 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005342 * @hide
5343 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005344 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005345 return null;
5346 }
5347
5348 /**
5349 * Construct a Style-specific RemoteViews for the final big notification layout.
5350 * @hide
5351 */
5352 public RemoteViews makeBigContentView() {
5353 return null;
5354 }
5355
5356 /**
5357 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005358 *
5359 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005360 * @hide
5361 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005362 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005363 return null;
5364 }
5365
5366 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005367 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005368 * @hide
5369 */
5370 public void addExtras(Bundle extras) {
5371 if (mSummaryTextSet) {
5372 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5373 }
5374 if (mBigContentTitle != null) {
5375 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5376 }
Chris Wren91ad5632013-06-05 15:05:57 -04005377 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005378 }
5379
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005380 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005381 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005382 * @hide
5383 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005384 protected void restoreFromExtras(Bundle extras) {
5385 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5386 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5387 mSummaryTextSet = true;
5388 }
5389 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5390 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5391 }
5392 }
5393
5394
5395 /**
5396 * @hide
5397 */
5398 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005399 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005400 return wip;
5401 }
5402
Daniel Sandler0ec46202015-06-24 01:27:05 -04005403 /**
5404 * @hide
5405 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005406 public void purgeResources() {}
5407
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005408 /**
5409 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5410 * attached to.
5411 *
5412 * @return the fully constructed Notification.
5413 */
5414 public Notification build() {
5415 checkBuilder();
5416 return mBuilder.build();
5417 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005418
5419 /**
5420 * @hide
5421 * @return true if the style positions the progress bar on the second line; false if the
5422 * style hides the progress bar
5423 */
5424 protected boolean hasProgress() {
5425 return true;
5426 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005427
5428 /**
5429 * @hide
5430 * @return Whether we should put the summary be put into the notification header
5431 */
5432 public boolean hasSummaryInHeader() {
5433 return true;
5434 }
Selim Cinek593610c2016-02-16 18:42:57 -08005435
5436 /**
5437 * @hide
5438 * @return Whether custom content views are displayed inline in the style
5439 */
5440 public boolean displayCustomViewInline() {
5441 return false;
5442 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005443 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005444
5445 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005446 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005447 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005448 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005449 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005450 * Notification notif = new Notification.Builder(mContext)
5451 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5452 * .setContentText(subject)
5453 * .setSmallIcon(R.drawable.new_post)
5454 * .setLargeIcon(aBitmap)
5455 * .setStyle(new Notification.BigPictureStyle()
5456 * .bigPicture(aBigBitmap))
5457 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005458 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005459 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005460 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005461 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005462 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005463 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005464 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005465 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005466
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005467 public BigPictureStyle() {
5468 }
5469
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005470 /**
5471 * @deprecated use {@code BigPictureStyle()}.
5472 */
5473 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005474 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005475 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005476 }
5477
Chris Wrend6297db2012-05-03 16:20:13 -04005478 /**
5479 * Overrides ContentTitle in the big form of the template.
5480 * This defaults to the value passed to setContentTitle().
5481 */
5482 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005483 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005484 return this;
5485 }
5486
5487 /**
5488 * Set the first line of text after the detail section in the big form of the template.
5489 */
5490 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005491 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005492 return this;
5493 }
5494
Chris Wren0bd664d2012-08-01 13:56:56 -04005495 /**
5496 * Provide the bitmap to be used as the payload for the BigPicture notification.
5497 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005498 public BigPictureStyle bigPicture(Bitmap b) {
5499 mPicture = b;
5500 return this;
5501 }
5502
Chris Wren3745a3d2012-05-22 15:11:52 -04005503 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005504 * Override the large icon when the big notification is shown.
5505 */
5506 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005507 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5508 }
5509
5510 /**
5511 * Override the large icon when the big notification is shown.
5512 */
5513 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005514 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005515 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005516 return this;
5517 }
5518
Riley Andrews0394a0c2015-11-03 23:36:52 -08005519 /** @hide */
5520 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5521
Daniel Sandler0ec46202015-06-24 01:27:05 -04005522 /**
5523 * @hide
5524 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005525 @Override
5526 public void purgeResources() {
5527 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005528 if (mPicture != null &&
5529 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005530 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005531 mPicture = mPicture.createAshmemBitmap();
5532 }
5533 if (mBigLargeIcon != null) {
5534 mBigLargeIcon.convertToAshmem();
5535 }
5536 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005537
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005538 /**
5539 * @hide
5540 */
5541 public RemoteViews makeBigContentView() {
5542 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005543 // This covers the following cases:
5544 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005545 // mN.mLargeIcon
5546 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005547 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005548 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005549 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005550 oldLargeIcon = mBuilder.mN.mLargeIcon;
5551 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005552 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5553 // replacement if the other one is null. Because we're restoring these legacy icons
5554 // for old listeners, this is in general non-null.
5555 largeIconLegacy = mBuilder.mN.largeIcon;
5556 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005557 }
5558
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005559 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005560 if (mSummaryTextSet) {
5561 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005562 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005563 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005564 }
Selim Cinek279fa862016-06-14 10:57:25 -07005565 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005566
Christoph Studer5c510ee2014-12-15 16:32:27 +01005567 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005568 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005569 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005570 }
5571
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005572 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005573 return contentView;
5574 }
5575
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005576 /**
5577 * @hide
5578 */
5579 public void addExtras(Bundle extras) {
5580 super.addExtras(extras);
5581
5582 if (mBigLargeIconSet) {
5583 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5584 }
5585 extras.putParcelable(EXTRA_PICTURE, mPicture);
5586 }
5587
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005588 /**
5589 * @hide
5590 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005591 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005592 protected void restoreFromExtras(Bundle extras) {
5593 super.restoreFromExtras(extras);
5594
5595 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005596 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005597 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005598 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005599 mPicture = extras.getParcelable(EXTRA_PICTURE);
5600 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005601
5602 /**
5603 * @hide
5604 */
5605 @Override
5606 public boolean hasSummaryInHeader() {
5607 return false;
5608 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005609 }
5610
5611 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005612 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005613 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005614 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005615 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005616 * Notification notif = new Notification.Builder(mContext)
5617 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5618 * .setContentText(subject)
5619 * .setSmallIcon(R.drawable.new_mail)
5620 * .setLargeIcon(aBitmap)
5621 * .setStyle(new Notification.BigTextStyle()
5622 * .bigText(aVeryLongString))
5623 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005624 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005625 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005626 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005627 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005628 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005629
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005630 private CharSequence mBigText;
5631
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005632 public BigTextStyle() {
5633 }
5634
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005635 /**
5636 * @deprecated use {@code BigTextStyle()}.
5637 */
5638 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005639 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005640 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005641 }
5642
Chris Wrend6297db2012-05-03 16:20:13 -04005643 /**
5644 * Overrides ContentTitle in the big form of the template.
5645 * This defaults to the value passed to setContentTitle().
5646 */
5647 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005648 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005649 return this;
5650 }
5651
5652 /**
5653 * Set the first line of text after the detail section in the big form of the template.
5654 */
5655 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005656 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005657 return this;
5658 }
5659
Chris Wren0bd664d2012-08-01 13:56:56 -04005660 /**
5661 * Provide the longer text to be displayed in the big form of the
5662 * template in place of the content text.
5663 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005664 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005665 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005666 return this;
5667 }
5668
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005669 /**
5670 * @hide
5671 */
5672 public void addExtras(Bundle extras) {
5673 super.addExtras(extras);
5674
Christoph Studer4600f9b2014-07-22 22:44:43 +02005675 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5676 }
5677
5678 /**
5679 * @hide
5680 */
5681 @Override
5682 protected void restoreFromExtras(Bundle extras) {
5683 super.restoreFromExtras(extras);
5684
5685 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005686 }
5687
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005688 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005689 * @param increasedHeight true if this layout be created with an increased height.
5690 *
5691 * @hide
5692 */
5693 @Override
5694 public RemoteViews makeContentView(boolean increasedHeight) {
5695 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005696 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005697 mBuilder.mActions = new ArrayList<>();
5698 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005699 mBuilder.mActions = mBuilder.mOriginalActions;
5700 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005701 return remoteViews;
5702 }
5703 return super.makeContentView(increasedHeight);
5704 }
5705
5706 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005707 * @hide
5708 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005709 @Override
5710 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5711 if (increasedHeight && mBuilder.mActions.size() > 0) {
5712 return makeBigContentView();
5713 }
5714 return super.makeHeadsUpContentView(increasedHeight);
5715 }
5716
5717 /**
5718 * @hide
5719 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005720 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005721
5722 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005723 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005724 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005725
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005726 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005727
Selim Cinek75998782016-04-26 10:39:17 -07005728 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005729
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005730 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005731 if (TextUtils.isEmpty(bigTextText)) {
5732 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5733 // experience
5734 bigTextText = mBuilder.processLegacyText(text);
5735 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005736 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005737
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005738 return contentView;
5739 }
5740
Adrian Roosb1f427c2016-05-26 12:27:15 -07005741 static void applyBigTextContentView(Builder builder,
5742 RemoteViews contentView, CharSequence bigTextText) {
5743 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005744 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005745 contentView.setViewVisibility(R.id.big_text,
5746 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005747 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005748 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005749 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005750
5751 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005752 * Helper class for generating large-format notifications that include multiple back-and-forth
5753 * messages of varying types between any number of people.
5754 *
5755 * <br>
5756 * If the platform does not provide large-format notifications, this method has no effect. The
5757 * user will always see the normal notification view.
5758 * <br>
5759 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5760 * so:
5761 * <pre class="prettyprint">
5762 *
5763 * Notification noti = new Notification.Builder()
5764 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5765 * .setContentText(subject)
5766 * .setSmallIcon(R.drawable.new_message)
5767 * .setLargeIcon(aBitmap)
5768 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5769 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5770 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5771 * .build();
5772 * </pre>
5773 */
5774 public static class MessagingStyle extends Style {
5775
5776 /**
5777 * The maximum number of messages that will be retained in the Notification itself (the
5778 * number displayed is up to the platform).
5779 */
5780 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5781
5782 CharSequence mUserDisplayName;
5783 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005784 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005785 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005786
5787 MessagingStyle() {
5788 }
5789
5790 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005791 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5792 * user before the posting app reposts the notification with those messages after they've
5793 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005794 * {@link #addMessage(Notification.MessagingStyle.Message)}
5795 */
Alex Hillsfd590442016-10-07 09:52:44 -04005796 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005797 mUserDisplayName = userDisplayName;
5798 }
5799
5800 /**
5801 * Returns the name to be displayed for any replies sent by the user
5802 */
5803 public CharSequence getUserDisplayName() {
5804 return mUserDisplayName;
5805 }
5806
5807 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005808 * Sets the title to be displayed on this conversation. This should only be used for
5809 * group messaging and left unset for one-on-one conversations.
5810 * @param conversationTitle
5811 * @return this object for method chaining.
5812 */
5813 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5814 mConversationTitle = conversationTitle;
5815 return this;
5816 }
5817
5818 /**
5819 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5820 * should be for one-on-one conversations
5821 */
5822 public CharSequence getConversationTitle() {
5823 return mConversationTitle;
5824 }
5825
5826 /**
5827 * Adds a message for display by this notification. Convenience call for a simple
5828 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5829 * @param text A {@link CharSequence} to be displayed as the message content
5830 * @param timestamp Time at which the message arrived
5831 * @param sender A {@link CharSequence} to be used for displaying the name of the
5832 * sender. Should be <code>null</code> for messages by the current user, in which case
5833 * the platform will insert {@link #getUserDisplayName()}.
5834 * Should be unique amongst all individuals in the conversation, and should be
5835 * consistent during re-posts of the notification.
5836 *
5837 * @see Message#Message(CharSequence, long, CharSequence)
5838 *
5839 * @return this object for method chaining
5840 */
5841 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005842 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005843 }
5844
5845 /**
5846 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005847 *
5848 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5849 * the newest last.
5850 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005851 * @param message The {@link Message} to be displayed
5852 * @return this object for method chaining
5853 */
5854 public MessagingStyle addMessage(Message message) {
5855 mMessages.add(message);
5856 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5857 mMessages.remove(0);
5858 }
5859 return this;
5860 }
5861
5862 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005863 * Adds a {@link Message} for historic context in this notification.
5864 *
5865 * <p>Messages should be added as historic if they are not the main subject of the
5866 * notification but may give context to a conversation. The system may choose to present
5867 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5868 *
5869 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5870 * the newest last.
5871 *
5872 * @param message The historic {@link Message} to be added
5873 * @return this object for method chaining
5874 */
5875 public MessagingStyle addHistoricMessage(Message message) {
5876 mHistoricMessages.add(message);
5877 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5878 mHistoricMessages.remove(0);
5879 }
5880 return this;
5881 }
5882
5883 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005884 * Gets the list of {@code Message} objects that represent the notification
5885 */
5886 public List<Message> getMessages() {
5887 return mMessages;
5888 }
5889
5890 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005891 * Gets the list of historic {@code Message}s in the notification.
5892 */
5893 public List<Message> getHistoricMessages() {
5894 return mHistoricMessages;
5895 }
5896
5897 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005898 * @hide
5899 */
5900 @Override
5901 public void addExtras(Bundle extras) {
5902 super.addExtras(extras);
5903 if (mUserDisplayName != null) {
5904 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5905 }
5906 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005907 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005908 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005909 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5910 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005911 }
Adrian Roos437cd562017-01-18 15:47:03 -08005912 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5913 Message.getBundleArrayForMessages(mHistoricMessages));
5914 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005915
5916 fixTitleAndTextExtras(extras);
5917 }
5918
5919 private void fixTitleAndTextExtras(Bundle extras) {
5920 Message m = findLatestIncomingMessage();
5921 CharSequence text = (m == null) ? null : m.mText;
5922 CharSequence sender = m == null ? null
5923 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5924 CharSequence title;
5925 if (!TextUtils.isEmpty(mConversationTitle)) {
5926 if (!TextUtils.isEmpty(sender)) {
5927 BidiFormatter bidi = BidiFormatter.getInstance();
5928 title = mBuilder.mContext.getString(
5929 com.android.internal.R.string.notification_messaging_title_template,
5930 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5931 } else {
5932 title = mConversationTitle;
5933 }
5934 } else {
5935 title = sender;
5936 }
5937
5938 if (title != null) {
5939 extras.putCharSequence(EXTRA_TITLE, title);
5940 }
5941 if (text != null) {
5942 extras.putCharSequence(EXTRA_TEXT, text);
5943 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005944 }
5945
5946 /**
5947 * @hide
5948 */
5949 @Override
5950 protected void restoreFromExtras(Bundle extras) {
5951 super.restoreFromExtras(extras);
5952
5953 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005954 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005955 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5956 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005957 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5958 if (messages != null && messages instanceof Parcelable[]) {
5959 mMessages = Message.getMessagesFromBundleArray(messages);
5960 }
5961 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5962 if (histMessages != null && histMessages instanceof Parcelable[]) {
5963 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005964 }
5965 }
5966
5967 /**
5968 * @hide
5969 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005970 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005971 public RemoteViews makeContentView(boolean increasedHeight) {
5972 if (!increasedHeight) {
5973 Message m = findLatestIncomingMessage();
5974 CharSequence title = mConversationTitle != null
5975 ? mConversationTitle
5976 : (m == null) ? null : m.mSender;
5977 CharSequence text = (m == null)
5978 ? null
5979 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005980
Selim Cinek7d1009b2017-01-25 15:28:28 -08005981 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5982 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5983 } else {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005984 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005985 mBuilder.mActions = new ArrayList<>();
5986 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005987 mBuilder.mActions = mBuilder.mOriginalActions;
5988 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005989 return remoteViews;
5990 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005991 }
5992
5993 private Message findLatestIncomingMessage() {
5994 for (int i = mMessages.size() - 1; i >= 0; i--) {
5995 Message m = mMessages.get(i);
5996 // Incoming messages have a non-empty sender.
5997 if (!TextUtils.isEmpty(m.mSender)) {
5998 return m;
5999 }
6000 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006001 if (!mMessages.isEmpty()) {
6002 // No incoming messages, fall back to outgoing message
6003 return mMessages.get(mMessages.size() - 1);
6004 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006005 return null;
6006 }
6007
6008 /**
6009 * @hide
6010 */
6011 @Override
6012 public RemoteViews makeBigContentView() {
6013 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
6014 ? super.mBigContentTitle
6015 : mConversationTitle;
6016 boolean hasTitle = !TextUtils.isEmpty(title);
6017
Adrian Roosfeafa052016-06-01 17:09:45 -07006018 if (mMessages.size() == 1) {
6019 // Special case for a single message: Use the big text style
6020 // so the collapsed and expanded versions match nicely.
6021 CharSequence bigTitle;
6022 CharSequence text;
6023 if (hasTitle) {
6024 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006025 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07006026 } else {
6027 bigTitle = mMessages.get(0).mSender;
6028 text = mMessages.get(0).mText;
6029 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006030 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
6031 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006032 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07006033 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
6034 return contentView;
6035 }
6036
Adrian Roos48d746a2016-04-12 14:57:28 -07006037 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006038 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006039 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006040
6041 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
6042 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
6043
6044 // Make sure all rows are gone in case we reuse a view.
6045 for (int rowId : rowIds) {
6046 contentView.setViewVisibility(rowId, View.GONE);
6047 }
6048
6049 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006050 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
6051 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006052 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07006053 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006054
Adrian Roosfeafa052016-06-01 17:09:45 -07006055 int contractedChildId = View.NO_ID;
6056 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08006057 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
6058 - (rowIds.length - mMessages.size()));
6059 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
6060 Message m = mHistoricMessages.get(firstHistoricMessage + i);
6061 int rowId = rowIds[i];
6062
Selim Cinek7b9605b2017-01-19 17:36:00 -08006063 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08006064
6065 if (contractedMessage == m) {
6066 contractedChildId = rowId;
6067 }
6068
6069 i++;
6070 }
6071
Adrian Roosc1a80b02016-04-05 14:54:55 -07006072 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
6073 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
6074 Message m = mMessages.get(firstMessage + i);
6075 int rowId = rowIds[i];
6076
6077 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006078 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
6079 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006080
Adrian Roosfeafa052016-06-01 17:09:45 -07006081 if (contractedMessage == m) {
6082 contractedChildId = rowId;
6083 }
6084
Adrian Roosc1a80b02016-04-05 14:54:55 -07006085 i++;
6086 }
Adrian Roos437cd562017-01-18 15:47:03 -08006087 // Clear the remaining views for reapply. Ensures that historic message views can
6088 // reliably be identified as being GONE and having non-null text.
6089 while (i < rowIds.length) {
6090 int rowId = rowIds[i];
6091 contentView.setTextViewText(rowId, null);
6092 i++;
6093 }
6094
Adrian Roosfeafa052016-06-01 17:09:45 -07006095 // Record this here to allow transformation between the contracted and expanded views.
6096 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
6097 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04006098 return contentView;
6099 }
6100
Selim Cinek7b9605b2017-01-19 17:36:00 -08006101 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006102 BidiFormatter bidi = BidiFormatter.getInstance();
6103 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08006104 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07006105 if (TextUtils.isEmpty(m.mSender)) {
6106 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
6107 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006108 makeFontColorSpan(colorize
6109 ? builder.getPrimaryTextColor()
6110 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006111 0 /* flags */);
6112 } else {
6113 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006114 makeFontColorSpan(colorize
6115 ? builder.getPrimaryTextColor()
6116 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006117 0 /* flags */);
6118 }
6119 CharSequence text = m.mText == null ? "" : m.mText;
6120 sb.append(" ").append(bidi.unicodeWrap(text));
6121 return sb;
6122 }
6123
Adrian Roosdedd1df2016-04-26 16:38:47 -07006124 /**
6125 * @hide
6126 */
6127 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006128 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6129 if (increasedHeight) {
6130 return makeBigContentView();
6131 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07006132 Message m = findLatestIncomingMessage();
6133 CharSequence title = mConversationTitle != null
6134 ? mConversationTitle
6135 : (m == null) ? null : m.mSender;
6136 CharSequence text = (m == null)
6137 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08006138 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006139
6140 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006141 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07006142 }
6143
Adrian Roosc1a80b02016-04-05 14:54:55 -07006144 private static TextAppearanceSpan makeFontColorSpan(int color) {
6145 return new TextAppearanceSpan(null, 0, 0,
6146 ColorStateList.valueOf(color), null);
6147 }
6148
Alex Hillsd9b04d92016-04-11 16:38:16 -04006149 public static final class Message {
6150
6151 static final String KEY_TEXT = "text";
6152 static final String KEY_TIMESTAMP = "time";
6153 static final String KEY_SENDER = "sender";
6154 static final String KEY_DATA_MIME_TYPE = "type";
6155 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006156 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006157
6158 private final CharSequence mText;
6159 private final long mTimestamp;
6160 private final CharSequence mSender;
6161
Shane Brennan5a871862017-03-11 13:14:17 -08006162 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006163 private String mDataMimeType;
6164 private Uri mDataUri;
6165
6166 /**
6167 * Constructor
6168 * @param text A {@link CharSequence} to be displayed as the message content
6169 * @param timestamp Time at which the message arrived
6170 * @param sender A {@link CharSequence} to be used for displaying the name of the
6171 * sender. Should be <code>null</code> for messages by the current user, in which case
6172 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6173 * Should be unique amongst all individuals in the conversation, and should be
6174 * consistent during re-posts of the notification.
6175 */
6176 public Message(CharSequence text, long timestamp, CharSequence sender){
6177 mText = text;
6178 mTimestamp = timestamp;
6179 mSender = sender;
6180 }
6181
6182 /**
6183 * Sets a binary blob of data and an associated MIME type for a message. In the case
6184 * where the platform doesn't support the MIME type, the original text provided in the
6185 * constructor will be used.
6186 * @param dataMimeType The MIME type of the content. See
6187 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6188 * types on Android and Android Wear.
6189 * @param dataUri The uri containing the content whose type is given by the MIME type.
6190 * <p class="note">
6191 * <ol>
6192 * <li>Notification Listeners including the System UI need permission to access the
6193 * data the Uri points to. The recommended ways to do this are:</li>
6194 * <li>Store the data in your own ContentProvider, making sure that other apps have
6195 * the correct permission to access your provider. The preferred mechanism for
6196 * providing access is to use per-URI permissions which are temporary and only
6197 * grant access to the receiving application. An easy way to create a
6198 * ContentProvider like this is to use the FileProvider helper class.</li>
6199 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6200 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6201 * also store non-media types (see MediaStore.Files for more info). Files can be
6202 * inserted into the MediaStore using scanFile() after which a content:// style
6203 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6204 * Note that once added to the system MediaStore the content is accessible to any
6205 * app on the device.</li>
6206 * </ol>
6207 * @return this object for method chaining
6208 */
6209 public Message setData(String dataMimeType, Uri dataUri) {
6210 mDataMimeType = dataMimeType;
6211 mDataUri = dataUri;
6212 return this;
6213 }
6214
Alex Hillsfc737de2016-03-23 17:33:02 -04006215 /**
6216 * Get the text to be used for this message, or the fallback text if a type and content
6217 * Uri have been set
6218 */
6219 public CharSequence getText() {
6220 return mText;
6221 }
6222
6223 /**
6224 * Get the time at which this message arrived
6225 */
6226 public long getTimestamp() {
6227 return mTimestamp;
6228 }
6229
6230 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006231 * Get the extras Bundle for this message.
6232 */
6233 public Bundle getExtras() {
6234 return mExtras;
6235 }
6236
6237 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006238 * Get the text used to display the contact's name in the messaging experience
6239 */
6240 public CharSequence getSender() {
6241 return mSender;
6242 }
6243
6244 /**
6245 * Get the MIME type of the data pointed to by the Uri
6246 */
6247 public String getDataMimeType() {
6248 return mDataMimeType;
6249 }
6250
6251 /**
6252 * Get the the Uri pointing to the content of the message. Can be null, in which case
6253 * {@see #getText()} is used.
6254 */
6255 public Uri getDataUri() {
6256 return mDataUri;
6257 }
6258
Alex Hillsd9b04d92016-04-11 16:38:16 -04006259 private Bundle toBundle() {
6260 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006261 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006262 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006263 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006264 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006265 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006266 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006267 }
6268 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006269 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006270 }
6271 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006272 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006273 }
Shane Brennan5a871862017-03-11 13:14:17 -08006274 if (mExtras != null) {
6275 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6276 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006277 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006278 }
6279
Alex Hillsd9b04d92016-04-11 16:38:16 -04006280 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6281 Bundle[] bundles = new Bundle[messages.size()];
6282 final int N = messages.size();
6283 for (int i = 0; i < N; i++) {
6284 bundles[i] = messages.get(i).toBundle();
6285 }
6286 return bundles;
6287 }
6288
Adrian Roosdedd1df2016-04-26 16:38:47 -07006289 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006290 List<Message> messages = new ArrayList<>(bundles.length);
6291 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006292 if (bundles[i] instanceof Bundle) {
6293 Message message = getMessageFromBundle((Bundle)bundles[i]);
6294 if (message != null) {
6295 messages.add(message);
6296 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006297 }
6298 }
6299 return messages;
6300 }
6301
6302 static Message getMessageFromBundle(Bundle bundle) {
6303 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006304 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006305 return null;
6306 } else {
6307 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6308 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6309 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6310 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006311 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6312 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006313 }
Shane Brennan5a871862017-03-11 13:14:17 -08006314 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6315 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6316 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006317 return message;
6318 }
6319 } catch (ClassCastException e) {
6320 return null;
6321 }
6322 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006323 }
6324 }
6325
6326 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006327 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006328 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006329 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006330 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006331 * Notification notif = new Notification.Builder(mContext)
6332 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6333 * .setContentText(subject)
6334 * .setSmallIcon(R.drawable.new_mail)
6335 * .setLargeIcon(aBitmap)
6336 * .setStyle(new Notification.InboxStyle()
6337 * .addLine(str1)
6338 * .addLine(str2)
6339 * .setContentTitle(&quot;&quot;)
6340 * .setSummaryText(&quot;+3 more&quot;))
6341 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006342 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006343 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006344 * @see Notification#bigContentView
6345 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006346 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006347 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6348
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006349 public InboxStyle() {
6350 }
6351
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006352 /**
6353 * @deprecated use {@code InboxStyle()}.
6354 */
6355 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006356 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006357 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006358 }
6359
Chris Wrend6297db2012-05-03 16:20:13 -04006360 /**
6361 * Overrides ContentTitle in the big form of the template.
6362 * This defaults to the value passed to setContentTitle().
6363 */
6364 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006365 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006366 return this;
6367 }
6368
6369 /**
6370 * Set the first line of text after the detail section in the big form of the template.
6371 */
6372 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006373 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006374 return this;
6375 }
6376
Chris Wren0bd664d2012-08-01 13:56:56 -04006377 /**
6378 * Append a line to the digest section of the Inbox notification.
6379 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006380 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006381 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006382 return this;
6383 }
6384
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006385 /**
6386 * @hide
6387 */
6388 public void addExtras(Bundle extras) {
6389 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006390
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006391 CharSequence[] a = new CharSequence[mTexts.size()];
6392 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6393 }
6394
Christoph Studer4600f9b2014-07-22 22:44:43 +02006395 /**
6396 * @hide
6397 */
6398 @Override
6399 protected void restoreFromExtras(Bundle extras) {
6400 super.restoreFromExtras(extras);
6401
6402 mTexts.clear();
6403 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6404 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6405 }
6406 }
6407
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006408 /**
6409 * @hide
6410 */
6411 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006412 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006413 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006414 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6415 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006416
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006417 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006418
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006419 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006420
Chris Wrend6297db2012-05-03 16:20:13 -04006421 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 -04006422 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006423
Chris Wren4ed80d52012-05-17 09:30:03 -04006424 // Make sure all rows are gone in case we reuse a view.
6425 for (int rowId : rowIds) {
6426 contentView.setViewVisibility(rowId, View.GONE);
6427 }
6428
Daniel Sandler879c5e02012-04-17 16:46:51 -04006429 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006430 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6431 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006432 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006433 int onlyViewId = 0;
6434 int maxRows = rowIds.length;
6435 if (mBuilder.mActions.size() > 0) {
6436 maxRows--;
6437 }
6438 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006439 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006440 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006441 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006442 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006443 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006444 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006445 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006446 if (first) {
6447 onlyViewId = rowIds[i];
6448 } else {
6449 onlyViewId = 0;
6450 }
Selim Cinek247fa012016-02-18 09:50:48 -08006451 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006452 }
6453 i++;
6454 }
Selim Cinek07c80172016-04-21 16:40:47 -07006455 if (onlyViewId != 0) {
6456 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6457 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6458 R.dimen.notification_text_margin_top);
6459 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6460 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006461
Daniel Sandler879c5e02012-04-17 16:46:51 -04006462 return contentView;
6463 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006464
Selim Cinek247fa012016-02-18 09:50:48 -08006465 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006466 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006467 if (first) {
6468 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6469 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6470 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006471 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006472 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006473 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006474 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006475 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006476 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006477 }
Dan Sandler842dd772014-05-15 09:36:47 -04006478
6479 /**
6480 * Notification style for media playback notifications.
6481 *
6482 * In the expanded form, {@link Notification#bigContentView}, up to 5
6483 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006484 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006485 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6486 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6487 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006488 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006489 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6490 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006491 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006492 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006493 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006494 * Notifications created with MediaStyle will have their category set to
6495 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6496 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006497 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006498 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6499 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006500 * the System UI can identify this as a notification representing an active media session
6501 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6502 *
Selim Cinek99104832017-01-25 14:47:33 -08006503 * <p>
6504 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6505 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6506 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6507 * <p>
6508 *
Dan Sandler842dd772014-05-15 09:36:47 -04006509 * To use this style with your Notification, feed it to
6510 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6511 * <pre class="prettyprint">
6512 * Notification noti = new Notification.Builder()
6513 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006514 * .setContentTitle(&quot;Track title&quot;)
6515 * .setContentText(&quot;Artist - Album&quot;)
6516 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006517 * .setStyle(<b>new Notification.MediaStyle()</b>
6518 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006519 * .build();
6520 * </pre>
6521 *
6522 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006523 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006524 */
6525 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006526 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006527 static final int MAX_MEDIA_BUTTONS = 5;
6528
6529 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006530 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006531
6532 public MediaStyle() {
6533 }
6534
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006535 /**
6536 * @deprecated use {@code MediaStyle()}.
6537 */
6538 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006539 public MediaStyle(Builder builder) {
6540 setBuilder(builder);
6541 }
6542
6543 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006544 * 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 -04006545 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006546 *
6547 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006548 */
6549 public MediaStyle setShowActionsInCompactView(int...actions) {
6550 mActionsToShowInCompact = actions;
6551 return this;
6552 }
6553
6554 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006555 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6556 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006557 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006558 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006559 mToken = token;
6560 return this;
6561 }
6562
Christoph Studer4600f9b2014-07-22 22:44:43 +02006563 /**
6564 * @hide
6565 */
Dan Sandler842dd772014-05-15 09:36:47 -04006566 @Override
6567 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006568 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006569 if (wip.category == null) {
6570 wip.category = Notification.CATEGORY_TRANSPORT;
6571 }
Dan Sandler842dd772014-05-15 09:36:47 -04006572 return wip;
6573 }
6574
Christoph Studer4600f9b2014-07-22 22:44:43 +02006575 /**
6576 * @hide
6577 */
6578 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006579 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006580 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006581 }
6582
6583 /**
6584 * @hide
6585 */
6586 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006587 public RemoteViews makeBigContentView() {
6588 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006589 }
6590
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006591 /**
6592 * @hide
6593 */
6594 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006595 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006596 RemoteViews expanded = makeMediaBigContentView();
6597 return expanded != null ? expanded : makeMediaContentView();
6598 }
6599
Dan Sandler842dd772014-05-15 09:36:47 -04006600 /** @hide */
6601 @Override
6602 public void addExtras(Bundle extras) {
6603 super.addExtras(extras);
6604
6605 if (mToken != null) {
6606 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6607 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006608 if (mActionsToShowInCompact != null) {
6609 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6610 }
Dan Sandler842dd772014-05-15 09:36:47 -04006611 }
6612
Christoph Studer4600f9b2014-07-22 22:44:43 +02006613 /**
6614 * @hide
6615 */
6616 @Override
6617 protected void restoreFromExtras(Bundle extras) {
6618 super.restoreFromExtras(extras);
6619
6620 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6621 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6622 }
6623 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6624 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6625 }
6626 }
6627
Selim Cinek5bf069a2015-11-10 19:14:27 -05006628 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006629 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006630 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006631 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006632 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006633 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6634 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006635 if (!tombstone) {
6636 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6637 }
6638 button.setContentDescription(R.id.action0, action.title);
6639 return button;
6640 }
6641
6642 private RemoteViews makeMediaContentView() {
6643 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006644 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006645
6646 final int numActions = mBuilder.mActions.size();
6647 final int N = mActionsToShowInCompact == null
6648 ? 0
6649 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6650 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006651 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006652 for (int i = 0; i < N; i++) {
6653 if (i >= numActions) {
6654 throw new IllegalArgumentException(String.format(
6655 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6656 i, numActions - 1));
6657 }
6658
6659 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006660 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006661 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006662 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006663 }
6664 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006665 handleImage(view);
6666 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006667 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006668 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006669 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006670 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006671 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006672 return view;
6673 }
6674
Selim Cinek99104832017-01-25 14:47:33 -08006675 private int getPrimaryHighlightColor() {
6676 return mBuilder.getPrimaryHighlightColor();
6677 }
6678
Dan Sandler842dd772014-05-15 09:36:47 -04006679 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006680 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006681 // Dont add an expanded view if there is no more content to be revealed
6682 int actionsInCompact = mActionsToShowInCompact == null
6683 ? 0
6684 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006685 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006686 return null;
6687 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006688 RemoteViews big = mBuilder.applyStandardTemplate(
6689 R.layout.notification_template_material_big_media,
6690 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006691
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006692 if (actionCount > 0) {
6693 big.removeAllViews(com.android.internal.R.id.media_actions);
6694 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006695 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006696 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006697 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006698 }
6699 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006700 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006701 return big;
6702 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006703
Selim Cinek5bf069a2015-11-10 19:14:27 -05006704 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006705 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006706 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6707 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006708 }
6709 }
6710
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006711 /**
6712 * @hide
6713 */
6714 @Override
6715 protected boolean hasProgress() {
6716 return false;
6717 }
Dan Sandler842dd772014-05-15 09:36:47 -04006718 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006719
Selim Cinek593610c2016-02-16 18:42:57 -08006720 /**
6721 * Notification style for custom views that are decorated by the system
6722 *
6723 * <p>Instead of providing a notification that is completely custom, a developer can set this
6724 * style and still obtain system decorations like the notification header with the expand
6725 * affordance and actions.
6726 *
6727 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6728 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6729 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6730 * corresponding custom views to display.
6731 *
6732 * To use this style with your Notification, feed it to
6733 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6734 * <pre class="prettyprint">
6735 * Notification noti = new Notification.Builder()
6736 * .setSmallIcon(R.drawable.ic_stat_player)
6737 * .setLargeIcon(albumArtBitmap))
6738 * .setCustomContentView(contentView);
6739 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6740 * .build();
6741 * </pre>
6742 */
6743 public static class DecoratedCustomViewStyle extends Style {
6744
6745 public DecoratedCustomViewStyle() {
6746 }
6747
Selim Cinek593610c2016-02-16 18:42:57 -08006748 /**
6749 * @hide
6750 */
6751 public boolean displayCustomViewInline() {
6752 return true;
6753 }
6754
6755 /**
6756 * @hide
6757 */
6758 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006759 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006760 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6761 }
6762
6763 /**
6764 * @hide
6765 */
6766 @Override
6767 public RemoteViews makeBigContentView() {
6768 return makeDecoratedBigContentView();
6769 }
6770
6771 /**
6772 * @hide
6773 */
6774 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006775 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006776 return makeDecoratedHeadsUpContentView();
6777 }
6778
Selim Cinek593610c2016-02-16 18:42:57 -08006779 private RemoteViews makeDecoratedHeadsUpContentView() {
6780 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6781 ? mBuilder.mN.contentView
6782 : mBuilder.mN.headsUpContentView;
6783 if (mBuilder.mActions.size() == 0) {
6784 return makeStandardTemplateWithCustomContent(headsUpContentView);
6785 }
6786 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6787 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006788 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006789 return remoteViews;
6790 }
6791
Selim Cinek593610c2016-02-16 18:42:57 -08006792 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6793 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6794 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006795 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006796 return remoteViews;
6797 }
6798
Selim Cinek593610c2016-02-16 18:42:57 -08006799 private RemoteViews makeDecoratedBigContentView() {
6800 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6801 ? mBuilder.mN.contentView
6802 : mBuilder.mN.bigContentView;
6803 if (mBuilder.mActions.size() == 0) {
6804 return makeStandardTemplateWithCustomContent(bigContentView);
6805 }
6806 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6807 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006808 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006809 return remoteViews;
6810 }
Selim Cinek247fa012016-02-18 09:50:48 -08006811
6812 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6813 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006814 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006815 // Need to clone customContent before adding, because otherwise it can no longer be
6816 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006817 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006818 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6819 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006820 }
Selim Cinek247fa012016-02-18 09:50:48 -08006821 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006822 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006823 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006824 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006825 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006826 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006827 }
Selim Cinek593610c2016-02-16 18:42:57 -08006828 }
6829
Selim Cinek03eb3b72016-02-18 10:39:45 -08006830 /**
6831 * Notification style for media custom views that are decorated by the system
6832 *
6833 * <p>Instead of providing a media notification that is completely custom, a developer can set
6834 * this style and still obtain system decorations like the notification header with the expand
6835 * affordance and actions.
6836 *
6837 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6838 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6839 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6840 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006841 * <p>
6842 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6843 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6844 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006845 * To use this style with your Notification, feed it to
6846 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6847 * <pre class="prettyprint">
6848 * Notification noti = new Notification.Builder()
6849 * .setSmallIcon(R.drawable.ic_stat_player)
6850 * .setLargeIcon(albumArtBitmap))
6851 * .setCustomContentView(contentView);
6852 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6853 * .setMediaSession(mySession))
6854 * .build();
6855 * </pre>
6856 *
6857 * @see android.app.Notification.DecoratedCustomViewStyle
6858 * @see android.app.Notification.MediaStyle
6859 */
6860 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6861
6862 public DecoratedMediaCustomViewStyle() {
6863 }
6864
Selim Cinek03eb3b72016-02-18 10:39:45 -08006865 /**
6866 * @hide
6867 */
6868 public boolean displayCustomViewInline() {
6869 return true;
6870 }
6871
6872 /**
6873 * @hide
6874 */
6875 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006876 public RemoteViews makeContentView(boolean increasedHeight) {
6877 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006878 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6879 mBuilder.mN.contentView);
6880 }
6881
6882 /**
6883 * @hide
6884 */
6885 @Override
6886 public RemoteViews makeBigContentView() {
6887 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6888 ? mBuilder.mN.bigContentView
6889 : mBuilder.mN.contentView;
6890 return makeBigContentViewWithCustomContent(customRemoteView);
6891 }
6892
6893 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6894 RemoteViews remoteViews = super.makeBigContentView();
6895 if (remoteViews != null) {
6896 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6897 customRemoteView);
6898 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006899 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006900 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6901 customRemoteView);
6902 } else {
6903 return null;
6904 }
6905 }
6906
6907 /**
6908 * @hide
6909 */
6910 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006911 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006912 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6913 ? mBuilder.mN.headsUpContentView
6914 : mBuilder.mN.contentView;
6915 return makeBigContentViewWithCustomContent(customRemoteView);
6916 }
6917
6918 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6919 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006920 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006921 // Need to clone customContent before adding, because otherwise it can no longer be
6922 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006923 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006924 remoteViews.removeAllViews(id);
6925 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006926 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006927 return remoteViews;
6928 }
6929 }
6930
Christoph Studer4600f9b2014-07-22 22:44:43 +02006931 // When adding a new Style subclass here, don't forget to update
6932 // Builder.getNotificationStyleClass.
6933
Griff Hazen61a9e862014-05-22 16:05:19 -07006934 /**
6935 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6936 * metadata or change options on a notification builder.
6937 */
6938 public interface Extender {
6939 /**
6940 * Apply this extender to a notification builder.
6941 * @param builder the builder to be modified.
6942 * @return the build object for chaining.
6943 */
6944 public Builder extend(Builder builder);
6945 }
6946
6947 /**
6948 * Helper class to add wearable extensions to notifications.
6949 * <p class="note"> See
6950 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6951 * for Android Wear</a> for more information on how to use this class.
6952 * <p>
6953 * To create a notification with wearable extensions:
6954 * <ol>
6955 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6956 * properties.
6957 * <li>Create a {@link android.app.Notification.WearableExtender}.
6958 * <li>Set wearable-specific properties using the
6959 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6960 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6961 * notification.
6962 * <li>Post the notification to the notification system with the
6963 * {@code NotificationManager.notify(...)} methods.
6964 * </ol>
6965 *
6966 * <pre class="prettyprint">
6967 * Notification notif = new Notification.Builder(mContext)
6968 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6969 * .setContentText(subject)
6970 * .setSmallIcon(R.drawable.new_mail)
6971 * .extend(new Notification.WearableExtender()
6972 * .setContentIcon(R.drawable.new_mail))
6973 * .build();
6974 * NotificationManager notificationManger =
6975 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6976 * notificationManger.notify(0, notif);</pre>
6977 *
6978 * <p>Wearable extensions can be accessed on an existing notification by using the
6979 * {@code WearableExtender(Notification)} constructor,
6980 * and then using the {@code get} methods to access values.
6981 *
6982 * <pre class="prettyprint">
6983 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6984 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006985 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006986 */
6987 public static final class WearableExtender implements Extender {
6988 /**
6989 * Sentinel value for an action index that is unset.
6990 */
6991 public static final int UNSET_ACTION_INDEX = -1;
6992
6993 /**
6994 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6995 * default sizing.
6996 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006997 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006998 * on their content.
6999 */
7000 public static final int SIZE_DEFAULT = 0;
7001
7002 /**
7003 * Size value for use with {@link #setCustomSizePreset} to show this notification
7004 * with an extra small size.
7005 * <p>This value is only applicable for custom display notifications created using
7006 * {@link #setDisplayIntent}.
7007 */
7008 public static final int SIZE_XSMALL = 1;
7009
7010 /**
7011 * Size value for use with {@link #setCustomSizePreset} to show this notification
7012 * with a small size.
7013 * <p>This value is only applicable for custom display notifications created using
7014 * {@link #setDisplayIntent}.
7015 */
7016 public static final int SIZE_SMALL = 2;
7017
7018 /**
7019 * Size value for use with {@link #setCustomSizePreset} to show this notification
7020 * with a medium size.
7021 * <p>This value is only applicable for custom display notifications created using
7022 * {@link #setDisplayIntent}.
7023 */
7024 public static final int SIZE_MEDIUM = 3;
7025
7026 /**
7027 * Size value for use with {@link #setCustomSizePreset} to show this notification
7028 * with a large size.
7029 * <p>This value is only applicable for custom display notifications created using
7030 * {@link #setDisplayIntent}.
7031 */
7032 public static final int SIZE_LARGE = 4;
7033
Griff Hazend5f11f92014-05-27 15:40:09 -07007034 /**
7035 * Size value for use with {@link #setCustomSizePreset} to show this notification
7036 * full screen.
7037 * <p>This value is only applicable for custom display notifications created using
7038 * {@link #setDisplayIntent}.
7039 */
7040 public static final int SIZE_FULL_SCREEN = 5;
7041
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007042 /**
7043 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7044 * short amount of time when this notification is displayed on the screen. This
7045 * is the default value.
7046 */
7047 public static final int SCREEN_TIMEOUT_SHORT = 0;
7048
7049 /**
7050 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7051 * for a longer amount of time when this notification is displayed on the screen.
7052 */
7053 public static final int SCREEN_TIMEOUT_LONG = -1;
7054
Griff Hazen61a9e862014-05-22 16:05:19 -07007055 /** Notification extra which contains wearable extensions */
7056 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7057
Pete Gastaf6781d2014-10-07 15:17:05 -04007058 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007059 private static final String KEY_ACTIONS = "actions";
7060 private static final String KEY_FLAGS = "flags";
7061 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7062 private static final String KEY_PAGES = "pages";
7063 private static final String KEY_BACKGROUND = "background";
7064 private static final String KEY_CONTENT_ICON = "contentIcon";
7065 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7066 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7067 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7068 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7069 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007070 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007071 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007072 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007073
7074 // Flags bitwise-ored to mFlags
7075 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7076 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7077 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7078 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007079 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007080 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007081 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007082
7083 // Default value for flags integer
7084 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7085
7086 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7087 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7088
7089 private ArrayList<Action> mActions = new ArrayList<Action>();
7090 private int mFlags = DEFAULT_FLAGS;
7091 private PendingIntent mDisplayIntent;
7092 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7093 private Bitmap mBackground;
7094 private int mContentIcon;
7095 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7096 private int mContentActionIndex = UNSET_ACTION_INDEX;
7097 private int mCustomSizePreset = SIZE_DEFAULT;
7098 private int mCustomContentHeight;
7099 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007100 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007101 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007102 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007103
7104 /**
7105 * Create a {@link android.app.Notification.WearableExtender} with default
7106 * options.
7107 */
7108 public WearableExtender() {
7109 }
7110
7111 public WearableExtender(Notification notif) {
7112 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7113 if (wearableBundle != null) {
7114 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7115 if (actions != null) {
7116 mActions.addAll(actions);
7117 }
7118
7119 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7120 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7121
7122 Notification[] pages = getNotificationArrayFromBundle(
7123 wearableBundle, KEY_PAGES);
7124 if (pages != null) {
7125 Collections.addAll(mPages, pages);
7126 }
7127
7128 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7129 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7130 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7131 DEFAULT_CONTENT_ICON_GRAVITY);
7132 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7133 UNSET_ACTION_INDEX);
7134 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7135 SIZE_DEFAULT);
7136 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7137 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007138 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007139 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007140 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007141 }
7142 }
7143
7144 /**
7145 * Apply wearable extensions to a notification that is being built. This is typically
7146 * called by the {@link android.app.Notification.Builder#extend} method of
7147 * {@link android.app.Notification.Builder}.
7148 */
7149 @Override
7150 public Notification.Builder extend(Notification.Builder builder) {
7151 Bundle wearableBundle = new Bundle();
7152
7153 if (!mActions.isEmpty()) {
7154 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7155 }
7156 if (mFlags != DEFAULT_FLAGS) {
7157 wearableBundle.putInt(KEY_FLAGS, mFlags);
7158 }
7159 if (mDisplayIntent != null) {
7160 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7161 }
7162 if (!mPages.isEmpty()) {
7163 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7164 new Notification[mPages.size()]));
7165 }
7166 if (mBackground != null) {
7167 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7168 }
7169 if (mContentIcon != 0) {
7170 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7171 }
7172 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7173 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7174 }
7175 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7176 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7177 mContentActionIndex);
7178 }
7179 if (mCustomSizePreset != SIZE_DEFAULT) {
7180 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7181 }
7182 if (mCustomContentHeight != 0) {
7183 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7184 }
7185 if (mGravity != DEFAULT_GRAVITY) {
7186 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7187 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007188 if (mHintScreenTimeout != 0) {
7189 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7190 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007191 if (mDismissalId != null) {
7192 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7193 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007194 if (mBridgeTag != null) {
7195 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7196 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007197
7198 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7199 return builder;
7200 }
7201
7202 @Override
7203 public WearableExtender clone() {
7204 WearableExtender that = new WearableExtender();
7205 that.mActions = new ArrayList<Action>(this.mActions);
7206 that.mFlags = this.mFlags;
7207 that.mDisplayIntent = this.mDisplayIntent;
7208 that.mPages = new ArrayList<Notification>(this.mPages);
7209 that.mBackground = this.mBackground;
7210 that.mContentIcon = this.mContentIcon;
7211 that.mContentIconGravity = this.mContentIconGravity;
7212 that.mContentActionIndex = this.mContentActionIndex;
7213 that.mCustomSizePreset = this.mCustomSizePreset;
7214 that.mCustomContentHeight = this.mCustomContentHeight;
7215 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007216 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007217 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007218 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007219 return that;
7220 }
7221
7222 /**
7223 * Add a wearable action to this notification.
7224 *
7225 * <p>When wearable actions are added using this method, the set of actions that
7226 * show on a wearable device splits from devices that only show actions added
7227 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7228 * of which actions display on different devices.
7229 *
7230 * @param action the action to add to this notification
7231 * @return this object for method chaining
7232 * @see android.app.Notification.Action
7233 */
7234 public WearableExtender addAction(Action action) {
7235 mActions.add(action);
7236 return this;
7237 }
7238
7239 /**
7240 * Adds wearable actions to this notification.
7241 *
7242 * <p>When wearable actions are added using this method, the set of actions that
7243 * show on a wearable device splits from devices that only show actions added
7244 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7245 * of which actions display on different devices.
7246 *
7247 * @param actions the actions to add to this notification
7248 * @return this object for method chaining
7249 * @see android.app.Notification.Action
7250 */
7251 public WearableExtender addActions(List<Action> actions) {
7252 mActions.addAll(actions);
7253 return this;
7254 }
7255
7256 /**
7257 * Clear all wearable actions present on this builder.
7258 * @return this object for method chaining.
7259 * @see #addAction
7260 */
7261 public WearableExtender clearActions() {
7262 mActions.clear();
7263 return this;
7264 }
7265
7266 /**
7267 * Get the wearable actions present on this notification.
7268 */
7269 public List<Action> getActions() {
7270 return mActions;
7271 }
7272
7273 /**
7274 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007275 * this notification. The {@link PendingIntent} provided should be for an activity.
7276 *
7277 * <pre class="prettyprint">
7278 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7279 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7280 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7281 * Notification notif = new Notification.Builder(context)
7282 * .extend(new Notification.WearableExtender()
7283 * .setDisplayIntent(displayPendingIntent)
7284 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7285 * .build();</pre>
7286 *
7287 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007288 * should have an empty task affinity. It is also recommended to use the device
7289 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007290 *
7291 * <p>Example AndroidManifest.xml entry:
7292 * <pre class="prettyprint">
7293 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7294 * android:exported=&quot;true&quot;
7295 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007296 * android:taskAffinity=&quot;&quot;
7297 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007298 *
7299 * @param intent the {@link PendingIntent} for an activity
7300 * @return this object for method chaining
7301 * @see android.app.Notification.WearableExtender#getDisplayIntent
7302 */
7303 public WearableExtender setDisplayIntent(PendingIntent intent) {
7304 mDisplayIntent = intent;
7305 return this;
7306 }
7307
7308 /**
7309 * Get the intent to launch inside of an activity view when displaying this
7310 * notification. This {@code PendingIntent} should be for an activity.
7311 */
7312 public PendingIntent getDisplayIntent() {
7313 return mDisplayIntent;
7314 }
7315
7316 /**
7317 * Add an additional page of content to display with this notification. The current
7318 * notification forms the first page, and pages added using this function form
7319 * subsequent pages. This field can be used to separate a notification into multiple
7320 * sections.
7321 *
7322 * @param page the notification to add as another page
7323 * @return this object for method chaining
7324 * @see android.app.Notification.WearableExtender#getPages
7325 */
7326 public WearableExtender addPage(Notification page) {
7327 mPages.add(page);
7328 return this;
7329 }
7330
7331 /**
7332 * Add additional pages of content to display with this notification. The current
7333 * notification forms the first page, and pages added using this function form
7334 * subsequent pages. This field can be used to separate a notification into multiple
7335 * sections.
7336 *
7337 * @param pages a list of notifications
7338 * @return this object for method chaining
7339 * @see android.app.Notification.WearableExtender#getPages
7340 */
7341 public WearableExtender addPages(List<Notification> pages) {
7342 mPages.addAll(pages);
7343 return this;
7344 }
7345
7346 /**
7347 * Clear all additional pages present on this builder.
7348 * @return this object for method chaining.
7349 * @see #addPage
7350 */
7351 public WearableExtender clearPages() {
7352 mPages.clear();
7353 return this;
7354 }
7355
7356 /**
7357 * Get the array of additional pages of content for displaying this notification. The
7358 * current notification forms the first page, and elements within this array form
7359 * subsequent pages. This field can be used to separate a notification into multiple
7360 * sections.
7361 * @return the pages for this notification
7362 */
7363 public List<Notification> getPages() {
7364 return mPages;
7365 }
7366
7367 /**
7368 * Set a background image to be displayed behind the notification content.
7369 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7370 * will work with any notification style.
7371 *
7372 * @param background the background bitmap
7373 * @return this object for method chaining
7374 * @see android.app.Notification.WearableExtender#getBackground
7375 */
7376 public WearableExtender setBackground(Bitmap background) {
7377 mBackground = background;
7378 return this;
7379 }
7380
7381 /**
7382 * Get a background image to be displayed behind the notification content.
7383 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7384 * will work with any notification style.
7385 *
7386 * @return the background image
7387 * @see android.app.Notification.WearableExtender#setBackground
7388 */
7389 public Bitmap getBackground() {
7390 return mBackground;
7391 }
7392
7393 /**
7394 * Set an icon that goes with the content of this notification.
7395 */
7396 public WearableExtender setContentIcon(int icon) {
7397 mContentIcon = icon;
7398 return this;
7399 }
7400
7401 /**
7402 * Get an icon that goes with the content of this notification.
7403 */
7404 public int getContentIcon() {
7405 return mContentIcon;
7406 }
7407
7408 /**
7409 * Set the gravity that the content icon should have within the notification display.
7410 * Supported values include {@link android.view.Gravity#START} and
7411 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7412 * @see #setContentIcon
7413 */
7414 public WearableExtender setContentIconGravity(int contentIconGravity) {
7415 mContentIconGravity = contentIconGravity;
7416 return this;
7417 }
7418
7419 /**
7420 * Get the gravity that the content icon should have within the notification display.
7421 * Supported values include {@link android.view.Gravity#START} and
7422 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7423 * @see #getContentIcon
7424 */
7425 public int getContentIconGravity() {
7426 return mContentIconGravity;
7427 }
7428
7429 /**
7430 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007431 * this notification. This action will no longer display separately from the
7432 * notification's content.
7433 *
Griff Hazenca48d352014-05-28 22:37:13 -07007434 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007435 * set, although the list of available actions comes from the main notification and not
7436 * from the child page's notification.
7437 *
7438 * @param actionIndex The index of the action to hoist onto the current notification page.
7439 * If wearable actions were added to the main notification, this index
7440 * will apply to that list, otherwise it will apply to the regular
7441 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007442 */
7443 public WearableExtender setContentAction(int actionIndex) {
7444 mContentActionIndex = actionIndex;
7445 return this;
7446 }
7447
7448 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007449 * Get the index of the notification action, if any, that was specified as being clickable
7450 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007451 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007452 *
Griff Hazenca48d352014-05-28 22:37:13 -07007453 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007454 * set, although the list of available actions comes from the main notification and not
7455 * from the child page's notification.
7456 *
7457 * <p>If wearable specific actions were added to the main notification, this index will
7458 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007459 *
7460 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007461 */
7462 public int getContentAction() {
7463 return mContentActionIndex;
7464 }
7465
7466 /**
7467 * Set the gravity that this notification should have within the available viewport space.
7468 * Supported values include {@link android.view.Gravity#TOP},
7469 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7470 * The default value is {@link android.view.Gravity#BOTTOM}.
7471 */
7472 public WearableExtender setGravity(int gravity) {
7473 mGravity = gravity;
7474 return this;
7475 }
7476
7477 /**
7478 * Get the gravity that this notification should have within the available viewport space.
7479 * Supported values include {@link android.view.Gravity#TOP},
7480 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7481 * The default value is {@link android.view.Gravity#BOTTOM}.
7482 */
7483 public int getGravity() {
7484 return mGravity;
7485 }
7486
7487 /**
7488 * Set the custom size preset for the display of this notification out of the available
7489 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7490 * {@link #SIZE_LARGE}.
7491 * <p>Some custom size presets are only applicable for custom display notifications created
7492 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7493 * documentation for the preset in question. See also
7494 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7495 */
7496 public WearableExtender setCustomSizePreset(int sizePreset) {
7497 mCustomSizePreset = sizePreset;
7498 return this;
7499 }
7500
7501 /**
7502 * Get the custom size preset for the display of this notification out of the available
7503 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7504 * {@link #SIZE_LARGE}.
7505 * <p>Some custom size presets are only applicable for custom display notifications created
7506 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7507 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7508 */
7509 public int getCustomSizePreset() {
7510 return mCustomSizePreset;
7511 }
7512
7513 /**
7514 * Set the custom height in pixels for the display of this notification's content.
7515 * <p>This option is only available for custom display notifications created
7516 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7517 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7518 * {@link #getCustomContentHeight}.
7519 */
7520 public WearableExtender setCustomContentHeight(int height) {
7521 mCustomContentHeight = height;
7522 return this;
7523 }
7524
7525 /**
7526 * Get the custom height in pixels for the display of this notification's content.
7527 * <p>This option is only available for custom display notifications created
7528 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7529 * {@link #setCustomContentHeight}.
7530 */
7531 public int getCustomContentHeight() {
7532 return mCustomContentHeight;
7533 }
7534
7535 /**
7536 * Set whether the scrolling position for the contents of this notification should start
7537 * at the bottom of the contents instead of the top when the contents are too long to
7538 * display within the screen. Default is false (start scroll at the top).
7539 */
7540 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7541 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7542 return this;
7543 }
7544
7545 /**
7546 * Get whether the scrolling position for the contents of this notification should start
7547 * at the bottom of the contents instead of the top when the contents are too long to
7548 * display within the screen. Default is false (start scroll at the top).
7549 */
7550 public boolean getStartScrollBottom() {
7551 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7552 }
7553
7554 /**
7555 * Set whether the content intent is available when the wearable device is not connected
7556 * to a companion device. The user can still trigger this intent when the wearable device
7557 * is offline, but a visual hint will indicate that the content intent may not be available.
7558 * Defaults to true.
7559 */
7560 public WearableExtender setContentIntentAvailableOffline(
7561 boolean contentIntentAvailableOffline) {
7562 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7563 return this;
7564 }
7565
7566 /**
7567 * Get whether the content intent is available when the wearable device is not connected
7568 * to a companion device. The user can still trigger this intent when the wearable device
7569 * is offline, but a visual hint will indicate that the content intent may not be available.
7570 * Defaults to true.
7571 */
7572 public boolean getContentIntentAvailableOffline() {
7573 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7574 }
7575
7576 /**
7577 * Set a hint that this notification's icon should not be displayed.
7578 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7579 * @return this object for method chaining
7580 */
7581 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7582 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7583 return this;
7584 }
7585
7586 /**
7587 * Get a hint that this notification's icon should not be displayed.
7588 * @return {@code true} if this icon should not be displayed, false otherwise.
7589 * The default value is {@code false} if this was never set.
7590 */
7591 public boolean getHintHideIcon() {
7592 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7593 }
7594
7595 /**
7596 * Set a visual hint that only the background image of this notification should be
7597 * displayed, and other semantic content should be hidden. This hint is only applicable
7598 * to sub-pages added using {@link #addPage}.
7599 */
7600 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7601 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7602 return this;
7603 }
7604
7605 /**
7606 * Get a visual hint that only the background image of this notification should be
7607 * displayed, and other semantic content should be hidden. This hint is only applicable
7608 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7609 */
7610 public boolean getHintShowBackgroundOnly() {
7611 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7612 }
7613
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007614 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007615 * Set a hint that this notification's background should not be clipped if possible,
7616 * and should instead be resized to fully display on the screen, retaining the aspect
7617 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007618 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7619 * @return this object for method chaining
7620 */
7621 public WearableExtender setHintAvoidBackgroundClipping(
7622 boolean hintAvoidBackgroundClipping) {
7623 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7624 return this;
7625 }
7626
7627 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007628 * Get a hint that this notification's background should not be clipped if possible,
7629 * and should instead be resized to fully display on the screen, retaining the aspect
7630 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007631 * @return {@code true} if it's ok if the background is clipped on the screen, false
7632 * otherwise. The default value is {@code false} if this was never set.
7633 */
7634 public boolean getHintAvoidBackgroundClipping() {
7635 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7636 }
7637
7638 /**
7639 * Set a hint that the screen should remain on for at least this duration when
7640 * this notification is displayed on the screen.
7641 * @param timeout The requested screen timeout in milliseconds. Can also be either
7642 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7643 * @return this object for method chaining
7644 */
7645 public WearableExtender setHintScreenTimeout(int timeout) {
7646 mHintScreenTimeout = timeout;
7647 return this;
7648 }
7649
7650 /**
7651 * Get the duration, in milliseconds, that the screen should remain on for
7652 * when this notification is displayed.
7653 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7654 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7655 */
7656 public int getHintScreenTimeout() {
7657 return mHintScreenTimeout;
7658 }
7659
Alex Hills9ab3a232016-04-05 14:54:56 -04007660 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007661 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7662 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7663 * qr codes, as well as other simple black-and-white tickets.
7664 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7665 * @return this object for method chaining
7666 */
7667 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7668 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7669 return this;
7670 }
7671
7672 /**
7673 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7674 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7675 * qr codes, as well as other simple black-and-white tickets.
7676 * @return {@code true} if it should be displayed in ambient, false otherwise
7677 * otherwise. The default value is {@code false} if this was never set.
7678 */
7679 public boolean getHintAmbientBigPicture() {
7680 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7681 }
7682
7683 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007684 * Set a hint that this notification's content intent will launch an {@link Activity}
7685 * directly, telling the platform that it can generate the appropriate transitions.
7686 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7687 * an activity and transitions should be generated, false otherwise.
7688 * @return this object for method chaining
7689 */
7690 public WearableExtender setHintContentIntentLaunchesActivity(
7691 boolean hintContentIntentLaunchesActivity) {
7692 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7693 return this;
7694 }
7695
7696 /**
7697 * Get a hint that this notification's content intent will launch an {@link Activity}
7698 * directly, telling the platform that it can generate the appropriate transitions
7699 * @return {@code true} if the content intent will launch an activity and transitions should
7700 * be generated, false otherwise. The default value is {@code false} if this was never set.
7701 */
7702 public boolean getHintContentIntentLaunchesActivity() {
7703 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7704 }
7705
Nadia Benbernou948627e2016-04-14 14:41:08 -04007706 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007707 * Sets the dismissal id for this notification. If a notification is posted with a
7708 * dismissal id, then when that notification is canceled, notifications on other wearables
7709 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007710 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007711 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007712 * @param dismissalId the dismissal id of the notification.
7713 * @return this object for method chaining
7714 */
7715 public WearableExtender setDismissalId(String dismissalId) {
7716 mDismissalId = dismissalId;
7717 return this;
7718 }
7719
7720 /**
7721 * Returns the dismissal id of the notification.
7722 * @return the dismissal id of the notification or null if it has not been set.
7723 */
7724 public String getDismissalId() {
7725 return mDismissalId;
7726 }
7727
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007728 /**
7729 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7730 * posted from a phone to provide finer-grained control on what notifications are bridged
7731 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7732 * Features to Notifications</a> for more information.
7733 * @param bridgeTag the bridge tag of the notification.
7734 * @return this object for method chaining
7735 */
7736 public WearableExtender setBridgeTag(String bridgeTag) {
7737 mBridgeTag = bridgeTag;
7738 return this;
7739 }
7740
7741 /**
7742 * Returns the bridge tag of the notification.
7743 * @return the bridge tag or null if not present.
7744 */
7745 public String getBridgeTag() {
7746 return mBridgeTag;
7747 }
7748
Griff Hazen61a9e862014-05-22 16:05:19 -07007749 private void setFlag(int mask, boolean value) {
7750 if (value) {
7751 mFlags |= mask;
7752 } else {
7753 mFlags &= ~mask;
7754 }
7755 }
7756 }
7757
7758 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007759 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7760 * with car extensions:
7761 *
7762 * <ol>
7763 * <li>Create an {@link Notification.Builder}, setting any desired
7764 * properties.
7765 * <li>Create a {@link CarExtender}.
7766 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7767 * {@link CarExtender}.
7768 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7769 * to apply the extensions to a notification.
7770 * </ol>
7771 *
7772 * <pre class="prettyprint">
7773 * Notification notification = new Notification.Builder(context)
7774 * ...
7775 * .extend(new CarExtender()
7776 * .set*(...))
7777 * .build();
7778 * </pre>
7779 *
7780 * <p>Car extensions can be accessed on an existing notification by using the
7781 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7782 * to access values.
7783 */
7784 public static final class CarExtender implements Extender {
7785 private static final String TAG = "CarExtender";
7786
7787 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7788 private static final String EXTRA_LARGE_ICON = "large_icon";
7789 private static final String EXTRA_CONVERSATION = "car_conversation";
7790 private static final String EXTRA_COLOR = "app_color";
7791
7792 private Bitmap mLargeIcon;
7793 private UnreadConversation mUnreadConversation;
7794 private int mColor = Notification.COLOR_DEFAULT;
7795
7796 /**
7797 * Create a {@link CarExtender} with default options.
7798 */
7799 public CarExtender() {
7800 }
7801
7802 /**
7803 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7804 *
7805 * @param notif The notification from which to copy options.
7806 */
7807 public CarExtender(Notification notif) {
7808 Bundle carBundle = notif.extras == null ?
7809 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7810 if (carBundle != null) {
7811 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7812 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7813
7814 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7815 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7816 }
7817 }
7818
7819 /**
7820 * Apply car extensions to a notification that is being built. This is typically called by
7821 * the {@link Notification.Builder#extend(Notification.Extender)}
7822 * method of {@link Notification.Builder}.
7823 */
7824 @Override
7825 public Notification.Builder extend(Notification.Builder builder) {
7826 Bundle carExtensions = new Bundle();
7827
7828 if (mLargeIcon != null) {
7829 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7830 }
7831 if (mColor != Notification.COLOR_DEFAULT) {
7832 carExtensions.putInt(EXTRA_COLOR, mColor);
7833 }
7834
7835 if (mUnreadConversation != null) {
7836 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7837 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7838 }
7839
7840 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7841 return builder;
7842 }
7843
7844 /**
7845 * Sets the accent color to use when Android Auto presents the notification.
7846 *
7847 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7848 * to accent the displayed notification. However, not all colors are acceptable in an
7849 * automotive setting. This method can be used to override the color provided in the
7850 * notification in such a situation.
7851 */
Tor Norbye80756e32015-03-02 09:39:27 -08007852 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007853 mColor = color;
7854 return this;
7855 }
7856
7857 /**
7858 * Gets the accent color.
7859 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007860 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007861 */
Tor Norbye80756e32015-03-02 09:39:27 -08007862 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007863 public int getColor() {
7864 return mColor;
7865 }
7866
7867 /**
7868 * Sets the large icon of the car notification.
7869 *
7870 * If no large icon is set in the extender, Android Auto will display the icon
7871 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7872 *
7873 * @param largeIcon The large icon to use in the car notification.
7874 * @return This object for method chaining.
7875 */
7876 public CarExtender setLargeIcon(Bitmap largeIcon) {
7877 mLargeIcon = largeIcon;
7878 return this;
7879 }
7880
7881 /**
7882 * Gets the large icon used in this car notification, or null if no icon has been set.
7883 *
7884 * @return The large icon for the car notification.
7885 * @see CarExtender#setLargeIcon
7886 */
7887 public Bitmap getLargeIcon() {
7888 return mLargeIcon;
7889 }
7890
7891 /**
7892 * Sets the unread conversation in a message notification.
7893 *
7894 * @param unreadConversation The unread part of the conversation this notification conveys.
7895 * @return This object for method chaining.
7896 */
7897 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7898 mUnreadConversation = unreadConversation;
7899 return this;
7900 }
7901
7902 /**
7903 * Returns the unread conversation conveyed by this notification.
7904 * @see #setUnreadConversation(UnreadConversation)
7905 */
7906 public UnreadConversation getUnreadConversation() {
7907 return mUnreadConversation;
7908 }
7909
7910 /**
7911 * A class which holds the unread messages from a conversation.
7912 */
7913 public static class UnreadConversation {
7914 private static final String KEY_AUTHOR = "author";
7915 private static final String KEY_TEXT = "text";
7916 private static final String KEY_MESSAGES = "messages";
7917 private static final String KEY_REMOTE_INPUT = "remote_input";
7918 private static final String KEY_ON_REPLY = "on_reply";
7919 private static final String KEY_ON_READ = "on_read";
7920 private static final String KEY_PARTICIPANTS = "participants";
7921 private static final String KEY_TIMESTAMP = "timestamp";
7922
7923 private final String[] mMessages;
7924 private final RemoteInput mRemoteInput;
7925 private final PendingIntent mReplyPendingIntent;
7926 private final PendingIntent mReadPendingIntent;
7927 private final String[] mParticipants;
7928 private final long mLatestTimestamp;
7929
7930 UnreadConversation(String[] messages, RemoteInput remoteInput,
7931 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7932 String[] participants, long latestTimestamp) {
7933 mMessages = messages;
7934 mRemoteInput = remoteInput;
7935 mReadPendingIntent = readPendingIntent;
7936 mReplyPendingIntent = replyPendingIntent;
7937 mParticipants = participants;
7938 mLatestTimestamp = latestTimestamp;
7939 }
7940
7941 /**
7942 * Gets the list of messages conveyed by this notification.
7943 */
7944 public String[] getMessages() {
7945 return mMessages;
7946 }
7947
7948 /**
7949 * Gets the remote input that will be used to convey the response to a message list, or
7950 * null if no such remote input exists.
7951 */
7952 public RemoteInput getRemoteInput() {
7953 return mRemoteInput;
7954 }
7955
7956 /**
7957 * Gets the pending intent that will be triggered when the user replies to this
7958 * notification.
7959 */
7960 public PendingIntent getReplyPendingIntent() {
7961 return mReplyPendingIntent;
7962 }
7963
7964 /**
7965 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7966 * in this object's message list.
7967 */
7968 public PendingIntent getReadPendingIntent() {
7969 return mReadPendingIntent;
7970 }
7971
7972 /**
7973 * Gets the participants in the conversation.
7974 */
7975 public String[] getParticipants() {
7976 return mParticipants;
7977 }
7978
7979 /**
7980 * Gets the firs participant in the conversation.
7981 */
7982 public String getParticipant() {
7983 return mParticipants.length > 0 ? mParticipants[0] : null;
7984 }
7985
7986 /**
7987 * Gets the timestamp of the conversation.
7988 */
7989 public long getLatestTimestamp() {
7990 return mLatestTimestamp;
7991 }
7992
7993 Bundle getBundleForUnreadConversation() {
7994 Bundle b = new Bundle();
7995 String author = null;
7996 if (mParticipants != null && mParticipants.length > 1) {
7997 author = mParticipants[0];
7998 }
7999 Parcelable[] messages = new Parcelable[mMessages.length];
8000 for (int i = 0; i < messages.length; i++) {
8001 Bundle m = new Bundle();
8002 m.putString(KEY_TEXT, mMessages[i]);
8003 m.putString(KEY_AUTHOR, author);
8004 messages[i] = m;
8005 }
8006 b.putParcelableArray(KEY_MESSAGES, messages);
8007 if (mRemoteInput != null) {
8008 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8009 }
8010 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8011 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8012 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8013 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8014 return b;
8015 }
8016
8017 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8018 if (b == null) {
8019 return null;
8020 }
8021 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8022 String[] messages = null;
8023 if (parcelableMessages != null) {
8024 String[] tmp = new String[parcelableMessages.length];
8025 boolean success = true;
8026 for (int i = 0; i < tmp.length; i++) {
8027 if (!(parcelableMessages[i] instanceof Bundle)) {
8028 success = false;
8029 break;
8030 }
8031 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8032 if (tmp[i] == null) {
8033 success = false;
8034 break;
8035 }
8036 }
8037 if (success) {
8038 messages = tmp;
8039 } else {
8040 return null;
8041 }
8042 }
8043
8044 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8045 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8046
8047 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8048
8049 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8050 if (participants == null || participants.length != 1) {
8051 return null;
8052 }
8053
8054 return new UnreadConversation(messages,
8055 remoteInput,
8056 onReply,
8057 onRead,
8058 participants, b.getLong(KEY_TIMESTAMP));
8059 }
8060 };
8061
8062 /**
8063 * Builder class for {@link CarExtender.UnreadConversation} objects.
8064 */
8065 public static class Builder {
8066 private final List<String> mMessages = new ArrayList<String>();
8067 private final String mParticipant;
8068 private RemoteInput mRemoteInput;
8069 private PendingIntent mReadPendingIntent;
8070 private PendingIntent mReplyPendingIntent;
8071 private long mLatestTimestamp;
8072
8073 /**
8074 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8075 *
8076 * @param name The name of the other participant in the conversation.
8077 */
8078 public Builder(String name) {
8079 mParticipant = name;
8080 }
8081
8082 /**
8083 * Appends a new unread message to the list of messages for this conversation.
8084 *
8085 * The messages should be added from oldest to newest.
8086 *
8087 * @param message The text of the new unread message.
8088 * @return This object for method chaining.
8089 */
8090 public Builder addMessage(String message) {
8091 mMessages.add(message);
8092 return this;
8093 }
8094
8095 /**
8096 * Sets the pending intent and remote input which will convey the reply to this
8097 * notification.
8098 *
8099 * @param pendingIntent The pending intent which will be triggered on a reply.
8100 * @param remoteInput The remote input parcelable which will carry the reply.
8101 * @return This object for method chaining.
8102 *
8103 * @see CarExtender.UnreadConversation#getRemoteInput
8104 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8105 */
8106 public Builder setReplyAction(
8107 PendingIntent pendingIntent, RemoteInput remoteInput) {
8108 mRemoteInput = remoteInput;
8109 mReplyPendingIntent = pendingIntent;
8110
8111 return this;
8112 }
8113
8114 /**
8115 * Sets the pending intent that will be sent once the messages in this notification
8116 * are read.
8117 *
8118 * @param pendingIntent The pending intent to use.
8119 * @return This object for method chaining.
8120 */
8121 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8122 mReadPendingIntent = pendingIntent;
8123 return this;
8124 }
8125
8126 /**
8127 * Sets the timestamp of the most recent message in an unread conversation.
8128 *
8129 * If a messaging notification has been posted by your application and has not
8130 * yet been cancelled, posting a later notification with the same id and tag
8131 * but without a newer timestamp may result in Android Auto not displaying a
8132 * heads up notification for the later notification.
8133 *
8134 * @param timestamp The timestamp of the most recent message in the conversation.
8135 * @return This object for method chaining.
8136 */
8137 public Builder setLatestTimestamp(long timestamp) {
8138 mLatestTimestamp = timestamp;
8139 return this;
8140 }
8141
8142 /**
8143 * Builds a new unread conversation object.
8144 *
8145 * @return The new unread conversation object.
8146 */
8147 public UnreadConversation build() {
8148 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8149 String[] participants = { mParticipant };
8150 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8151 mReadPendingIntent, participants, mLatestTimestamp);
8152 }
8153 }
8154 }
8155
8156 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008157 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8158 * with a TV extension:
8159 *
8160 * <ol>
8161 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8162 * <li>Create a {@link TvExtender}.
8163 * <li>Set TV-specific properties using the {@code set} methods of
8164 * {@link TvExtender}.
8165 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8166 * to apply the extension to a notification.
8167 * </ol>
8168 *
8169 * <pre class="prettyprint">
8170 * Notification notification = new Notification.Builder(context)
8171 * ...
8172 * .extend(new TvExtender()
8173 * .set*(...))
8174 * .build();
8175 * </pre>
8176 *
8177 * <p>TV extensions can be accessed on an existing notification by using the
8178 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8179 * to access values.
8180 *
8181 * @hide
8182 */
8183 @SystemApi
8184 public static final class TvExtender implements Extender {
8185 private static final String TAG = "TvExtender";
8186
8187 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8188 private static final String EXTRA_FLAGS = "flags";
8189 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8190 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008191 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008192
8193 // Flags bitwise-ored to mFlags
8194 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8195
8196 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008197 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008198 private PendingIntent mContentIntent;
8199 private PendingIntent mDeleteIntent;
8200
8201 /**
8202 * Create a {@link TvExtender} with default options.
8203 */
8204 public TvExtender() {
8205 mFlags = FLAG_AVAILABLE_ON_TV;
8206 }
8207
8208 /**
8209 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8210 *
8211 * @param notif The notification from which to copy options.
8212 */
8213 public TvExtender(Notification notif) {
8214 Bundle bundle = notif.extras == null ?
8215 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8216 if (bundle != null) {
8217 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008218 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008219 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8220 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8221 }
8222 }
8223
8224 /**
8225 * Apply a TV extension to a notification that is being built. This is typically called by
8226 * the {@link Notification.Builder#extend(Notification.Extender)}
8227 * method of {@link Notification.Builder}.
8228 */
8229 @Override
8230 public Notification.Builder extend(Notification.Builder builder) {
8231 Bundle bundle = new Bundle();
8232
8233 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008234 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008235 if (mContentIntent != null) {
8236 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8237 }
8238
8239 if (mDeleteIntent != null) {
8240 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8241 }
8242
8243 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8244 return builder;
8245 }
8246
8247 /**
8248 * Returns true if this notification should be shown on TV. This method return true
8249 * if the notification was extended with a TvExtender.
8250 */
8251 public boolean isAvailableOnTv() {
8252 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8253 }
8254
8255 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008256 * Specifies the channel the notification should be delivered on when shown on TV.
8257 * It can be different from the channel that the notification is delivered to when
8258 * posting on a non-TV device.
8259 */
8260 public TvExtender setChannel(String channelId) {
8261 mChannelId = channelId;
8262 return this;
8263 }
8264
8265 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008266 * Specifies the channel the notification should be delivered on when shown on TV.
8267 * It can be different from the channel that the notification is delivered to when
8268 * posting on a non-TV device.
8269 */
8270 public TvExtender setChannelId(String channelId) {
8271 mChannelId = channelId;
8272 return this;
8273 }
8274
Jeff Sharkey000ce802017-04-29 13:13:27 -06008275 /** @removed */
8276 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008277 public String getChannel() {
8278 return mChannelId;
8279 }
8280
8281 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008282 * Returns the id of the channel this notification posts to on TV.
8283 */
8284 public String getChannelId() {
8285 return mChannelId;
8286 }
8287
8288 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008289 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8290 * If provided, it is used instead of the content intent specified
8291 * at the level of Notification.
8292 */
8293 public TvExtender setContentIntent(PendingIntent intent) {
8294 mContentIntent = intent;
8295 return this;
8296 }
8297
8298 /**
8299 * Returns the TV-specific content intent. If this method returns null, the
8300 * main content intent on the notification should be used.
8301 *
8302 * @see {@link Notification#contentIntent}
8303 */
8304 public PendingIntent getContentIntent() {
8305 return mContentIntent;
8306 }
8307
8308 /**
8309 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8310 * by the user on TV. If provided, it is used instead of the delete intent specified
8311 * at the level of Notification.
8312 */
8313 public TvExtender setDeleteIntent(PendingIntent intent) {
8314 mDeleteIntent = intent;
8315 return this;
8316 }
8317
8318 /**
8319 * Returns the TV-specific delete intent. If this method returns null, the
8320 * main delete intent on the notification should be used.
8321 *
8322 * @see {@link Notification#deleteIntent}
8323 */
8324 public PendingIntent getDeleteIntent() {
8325 return mDeleteIntent;
8326 }
8327 }
8328
8329 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008330 * Get an array of Notification objects from a parcelable array bundle field.
8331 * Update the bundle to have a typed array so fetches in the future don't need
8332 * to do an array copy.
8333 */
8334 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8335 Parcelable[] array = bundle.getParcelableArray(key);
8336 if (array instanceof Notification[] || array == null) {
8337 return (Notification[]) array;
8338 }
8339 Notification[] typedArray = Arrays.copyOf(array, array.length,
8340 Notification[].class);
8341 bundle.putParcelableArray(key, typedArray);
8342 return typedArray;
8343 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008344
8345 private static class BuilderRemoteViews extends RemoteViews {
8346 public BuilderRemoteViews(Parcel parcel) {
8347 super(parcel);
8348 }
8349
Kenny Guy77320062014-08-27 21:37:15 +01008350 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8351 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008352 }
8353
8354 @Override
8355 public BuilderRemoteViews clone() {
8356 Parcel p = Parcel.obtain();
8357 writeToParcel(p, 0);
8358 p.setDataPosition(0);
8359 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8360 p.recycle();
8361 return brv;
8362 }
8363 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008364
8365 private static class StandardTemplateParams {
8366 boolean hasProgress = true;
8367 boolean ambient = false;
8368 CharSequence title;
8369 CharSequence text;
8370
8371 final StandardTemplateParams reset() {
8372 hasProgress = true;
8373 ambient = false;
8374 title = null;
8375 text = null;
8376 return this;
8377 }
8378
8379 final StandardTemplateParams hasProgress(boolean hasProgress) {
8380 this.hasProgress = hasProgress;
8381 return this;
8382 }
8383
8384 final StandardTemplateParams title(CharSequence title) {
8385 this.title = title;
8386 return this;
8387 }
8388
8389 final StandardTemplateParams text(CharSequence text) {
8390 this.text = text;
8391 return this;
8392 }
8393
8394 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008395 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008396 this.ambient = ambient;
8397 return this;
8398 }
8399
8400 final StandardTemplateParams fillTextsFrom(Builder b) {
8401 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008402 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8403 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008404 return this;
8405 }
8406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407}