blob: 71b159ea0f66fb146606bf84915110eaf8a2e9b7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040026import android.annotation.SdkConstant;
27import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040028import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Context;
30import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010031import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040032import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020033import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050034import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020035import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070036import android.content.res.Configuration;
37import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040038import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010039import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070040import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010041import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010042import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040043import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040044import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070045import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080046import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070047import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040049import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020050import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050051import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070052import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Parcel;
54import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040055import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080056import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070057import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070058import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080059import android.text.SpannableStringBuilder;
60import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080062import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070063import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080072import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080073import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070074import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070075import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.widget.RemoteViews;
77
Griff Hazen959591e2014-05-15 22:26:18 -070078import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070079import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070080import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070081import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080082import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070083
Tor Norbyed9273d62013-05-30 15:59:53 -070084import java.lang.annotation.Retention;
85import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020086import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050087import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070088import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070089import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070090import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050091import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093/**
94 * A class that represents how a persistent notification is to be presented to
95 * the user using the {@link android.app.NotificationManager}.
96 *
Joe Onoratocb109a02011-01-18 17:57:41 -080097 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
98 * easier to construct Notifications.</p>
99 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700100 * <div class="special reference">
101 * <h3>Developer Guides</h3>
102 * <p>For a guide to creating notifications, read the
103 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
104 * developer guide.</p>
105 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
107public class Notification implements Parcelable
108{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400109 private static final String TAG = "Notification";
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400112 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400113 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400114 */
115 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
116 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
117 = "android.intent.category.NOTIFICATION_PREFERENCES";
118
119 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500120 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
121 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400122 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500123 */
124 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
125
126 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
129 * that can be used to narrow down what settings should be shown in the target app.
130 */
131 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
132
133 /**
134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
135 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
136 * that can be used to narrow down what settings should be shown in the target app.
137 */
138 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
139
140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * Use all default values (where applicable).
142 */
143 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
146 * Use the default notification sound. This will ignore any given
147 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500148 *
Chris Wren47c20a12014-06-18 17:27:29 -0400149 * <p>
150 * A notification that is noisy is more likely to be presented as a heads-up notification.
151 * </p>
152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 */
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 public static final int DEFAULT_SOUND = 1;
157
158 /**
159 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700161 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 *
Chris Wren47c20a12014-06-18 17:27:29 -0400163 * <p>
164 * A notification that vibrates is more likely to be presented as a heads-up notification.
165 * </p>
166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 */
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 /**
173 * Use the default notification lights. This will ignore the
174 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
175 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 */
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200183 * Maximum length of CharSequences accepted by Builder and friends.
184 *
185 * <p>
186 * Avoids spamming the system with overly large strings such as full e-mails.
187 */
188 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
189
190 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800191 * Maximum entries of reply text that are accepted by Builder and friends.
192 */
193 private static final int MAX_REPLY_HISTORY = 5;
194
195 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * Default value: {@link System#currentTimeMillis() Now}.
199 *
200 * Choose a timestamp that will be most relevant to the user. For most finite events, this
201 * corresponds to the time the event happened (or will happen, in the case of events that have
202 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800203 * timestamped according to when the activity began.
204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800206 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500207 * <ul>
208 * <li>Notification of a new chat message should be stamped when the message was received.</li>
209 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
210 * <li>Notification of a completed file download should be stamped when the download finished.</li>
211 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
212 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
213 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800214 * </ul>
215 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700216 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
217 * anymore by default and must be opted into by using
218 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public long when;
221
222 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700223 * The creation time of the notification
224 */
225 private long creationTime;
226
227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400229 *
230 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 */
Dan Sandler86647982015-05-13 23:41:13 -0400232 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700233 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public int icon;
235
236 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800237 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
238 * leave it at its default value of 0.
239 *
240 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700241 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800242 */
243 public int iconLevel;
244
245 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500246 * The number of events that this notification represents. For example, in a new mail
247 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800248 *
Julia Reynolds30331982017-04-27 10:12:50 -0400249 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500250 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
251 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
Julia Reynolds30331982017-04-27 10:12:50 -0400253 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 /**
256 * The intent to execute when the expanded status entry is clicked. If
257 * this is an activity, it must include the
258 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800259 * that you take care of task management as described in the
260 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800261 * Stack</a> document. In particular, make sure to read the notification section
262 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
263 * Notifications</a> for the correct ways to launch an application from a
264 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 */
266 public PendingIntent contentIntent;
267
268 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500269 * The intent to execute when the notification is explicitly dismissed by the user, either with
270 * the "Clear All" button or by swiping it away individually.
271 *
272 * This probably shouldn't be launching an activity since several of those will be sent
273 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 */
275 public PendingIntent deleteIntent;
276
277 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700278 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800279 *
Chris Wren47c20a12014-06-18 17:27:29 -0400280 * <p>
281 * The system UI may choose to display a heads-up notification, instead of
282 * launching this intent, while the user is using the device.
283 * </p>
284 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800285 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400286 */
287 public PendingIntent fullScreenIntent;
288
289 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400290 * Text that summarizes this notification for accessibility services.
291 *
292 * As of the L release, this text is no longer shown on screen, but it is still useful to
293 * accessibility services (where it serves as an audible announcement of the notification's
294 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400295 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800296 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
298 public CharSequence tickerText;
299
300 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400301 * Formerly, a view showing the {@link #tickerText}.
302 *
303 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400304 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400305 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800306 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400307
308 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400309 * The view that will represent this notification in the notification list (which is pulled
310 * down from the status bar).
311 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500312 * As of N, this field may be null. The notification view is determined by the inputs
313 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 public RemoteViews contentView;
318
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400319 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400320 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400321 * opportunity to show more detail. The system UI may choose to show this
322 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500324 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400325 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
326 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400328 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400329 public RemoteViews bigContentView;
330
Chris Wren8fd39ec2014-02-27 17:43:26 -0500331
332 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400333 * A medium-format version of {@link #contentView}, providing the Notification an
334 * opportunity to add action buttons to contentView. At its discretion, the system UI may
335 * choose to show this as a heads-up notification, which will pop up so the user can see
336 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500338 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400339 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
340 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500341 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400342 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500343 public RemoteViews headsUpContentView;
344
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400345 /**
Dan Sandler86647982015-05-13 23:41:13 -0400346 * A large bitmap to be shown in the notification content area.
347 *
348 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Dan Sandler86647982015-05-13 23:41:13 -0400350 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800351 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352
353 /**
354 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400357 * A notification that is noisy is more likely to be presented as a heads-up notification.
358 * </p>
359 *
360 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500361 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500365 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 public Uri sound;
367
368 /**
369 * Use this constant as the value for audioStreamType to request that
370 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700371 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400372 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500373 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700375 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 public static final int STREAM_DEFAULT = -1;
377
378 /**
379 * The audio stream type to use when playing the sound.
380 * Should be one of the STREAM_ constants from
381 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400382 *
383 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700385 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 public int audioStreamType = STREAM_DEFAULT;
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400389 * The default value of {@link #audioAttributes}.
390 */
391 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
392 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
393 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
394 .build();
395
396 /**
397 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500398 *
399 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400400 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500401 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400402 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
403
404 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500405 * The pattern with which to vibrate.
406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * <p>
408 * To vibrate the default pattern, see {@link #defaults}.
409 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500414 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 public long[] vibrate;
416
417 /**
418 * The color of the led. The hardware will do its best approximation.
419 *
420 * @see #FLAG_SHOW_LIGHTS
421 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500422 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 */
Tor Norbye80756e32015-03-02 09:39:27 -0800424 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500425 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 public int ledARGB;
427
428 /**
429 * The number of milliseconds for the LED to be on while it's flashing.
430 * The hardware will do its best approximation.
431 *
432 * @see #FLAG_SHOW_LIGHTS
433 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500436 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 public int ledOnMS;
438
439 /**
440 * The number of milliseconds for the LED to be off while it's flashing.
441 * The hardware will do its best approximation.
442 *
443 * @see #FLAG_SHOW_LIGHTS
444 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500445 *
446 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 public int ledOffMS;
450
451 /**
452 * Specifies which values should be taken from the defaults.
453 * <p>
454 * To set, OR the desired from {@link #DEFAULT_SOUND},
455 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
456 * values, use {@link #DEFAULT_ALL}.
457 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 *
459 * @deprecated use {@link NotificationChannel#getSound()} and
460 * {@link NotificationChannel#shouldShowLights()} and
461 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 public int defaults;
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * Bit to be bitwise-ored into the {@link #flags} field that should be
468 * set if you want the LED on for this notification.
469 * <ul>
470 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
471 * or 0 for both ledOnMS and ledOffMS.</li>
472 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
473 * <li>To flash the LED, pass the number of milliseconds that it should
474 * be on and off to ledOnMS and ledOffMS.</li>
475 * </ul>
476 * <p>
477 * Since hardware varies, you are not guaranteed that any of the values
478 * you pass are honored exactly. Use the system defaults (TODO) if possible
479 * because they will be set to values that work on any given hardware.
480 * <p>
481 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500482 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500485 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
487
488 /**
489 * Bit to be bitwise-ored into the {@link #flags} field that should be
490 * set if this notification is in reference to something that is ongoing,
491 * like a phone call. It should not be set if this notification is in
492 * reference to something that happened at a particular point in time,
493 * like a missed phone call.
494 */
495 public static final int FLAG_ONGOING_EVENT = 0x00000002;
496
497 /**
498 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700499 * the audio will be repeated until the notification is
500 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 */
502 public static final int FLAG_INSISTENT = 0x00000004;
503
504 /**
505 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700506 * set if you would only like the sound, vibrate and ticker to be played
507 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
509 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
510
511 /**
512 * Bit to be bitwise-ored into the {@link #flags} field that should be
513 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500514 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 */
516 public static final int FLAG_AUTO_CANCEL = 0x00000010;
517
518 /**
519 * Bit to be bitwise-ored into the {@link #flags} field that should be
520 * set if the notification should not be canceled when the user clicks
521 * the Clear all button.
522 */
523 public static final int FLAG_NO_CLEAR = 0x00000020;
524
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700525 /**
526 * Bit to be bitwise-ored into the {@link #flags} field that should be
527 * set if this notification represents a currently running service. This
528 * will normally be set for you by {@link Service#startForeground}.
529 */
530 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
531
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400532 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800534 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500535 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400536 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700537 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500538 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400539
Griff Hazendfcb0802014-02-11 12:00:00 -0800540 /**
541 * Bit to be bitswise-ored into the {@link #flags} field that should be
542 * set if this notification is relevant to the current device only
543 * and it is not recommended that it bridge to other devices.
544 */
545 public static final int FLAG_LOCAL_ONLY = 0x00000100;
546
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700547 /**
548 * Bit to be bitswise-ored into the {@link #flags} field that should be
549 * set if this notification is the group summary for a group of notifications.
550 * Grouped notifications may display in a cluster or stack on devices which
551 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
552 */
553 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
554
Julia Reynoldse46bb372016-03-17 11:05:58 -0400555 /**
556 * Bit to be bitswise-ored into the {@link #flags} field that should be
557 * set if this notification is the group summary for an auto-group of notifications.
558 *
559 * @hide
560 */
561 @SystemApi
562 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
563
Julia Reynolds4db59552017-06-30 13:34:01 -0400564 /**
565 * @hide
566 */
567 public static final int FLAG_CAN_COLORIZE = 0x00000800;
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 public int flags;
570
Tor Norbyed9273d62013-05-30 15:59:53 -0700571 /** @hide */
572 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
573 @Retention(RetentionPolicy.SOURCE)
574 public @interface Priority {}
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 * Default notification {@link #priority}. If your application does not prioritize its own
578 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500579 *
580 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500582 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 public static final int PRIORITY_DEFAULT = 0;
584
585 /**
586 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
587 * items smaller, or at a different position in the list, compared with your app's
588 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500589 *
590 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500592 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500593 public static final int PRIORITY_LOW = -1;
594
595 /**
596 * Lowest {@link #priority}; these items might not be shown to the user except under special
597 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500598 *
599 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500601 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500602 public static final int PRIORITY_MIN = -2;
603
604 /**
605 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
606 * show these items larger, or at a different position in notification lists, compared with
607 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500608 *
609 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500611 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500612 public static final int PRIORITY_HIGH = 1;
613
614 /**
615 * Highest {@link #priority}, for your application's most important items that require the
616 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500617 *
618 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500620 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621 public static final int PRIORITY_MAX = 2;
622
623 /**
624 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800625 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 * Priority is an indication of how much of the user's valuable attention should be consumed by
627 * this notification. Low-priority notifications may be hidden from the user in certain
628 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500629 * system will make a determination about how to interpret this priority when presenting
630 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400631 *
632 * <p>
633 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
634 * as a heads-up notification.
635 * </p>
636 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500637 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500638 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700639 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500640 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800642
Dan Sandler26e81cf2014-05-06 10:01:27 -0400643 /**
644 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
645 * to be applied by the standard Style templates when presenting this notification.
646 *
647 * The current template design constructs a colorful header image by overlaying the
648 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
649 * ignored.
650 */
Tor Norbye80756e32015-03-02 09:39:27 -0800651 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400652 public int color = COLOR_DEFAULT;
653
654 /**
655 * Special value of {@link #color} telling the system not to decorate this notification with
656 * any special color but instead use default colors when presenting this notification.
657 */
Tor Norbye80756e32015-03-02 09:39:27 -0800658 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400659 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600660
661 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800662 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800663 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800664 */
665 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800666 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800667
668 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700669 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
670 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600671 * lockscreen).
672 *
673 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
674 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
675 * shown in all situations, but the contents are only available if the device is unlocked for
676 * the appropriate user.
677 *
678 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
679 * can be read even in an "insecure" context (that is, above a secure lockscreen).
680 * To modify the public version of this notification—for example, to redact some portions—see
681 * {@link Builder#setPublicVersion(Notification)}.
682 *
683 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
684 * and ticker until the user has bypassed the lockscreen.
685 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600686 public @Visibility int visibility;
687
688 /** @hide */
689 @IntDef(prefix = { "VISIBILITY_" }, value = {
690 VISIBILITY_PUBLIC,
691 VISIBILITY_PRIVATE,
692 VISIBILITY_SECRET,
693 })
694 @Retention(RetentionPolicy.SOURCE)
695 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600696
Griff Hazenfc3922d2014-08-20 11:56:44 -0700697 /**
698 * Notification visibility: Show this notification in its entirety on all lockscreens.
699 *
700 * {@see #visibility}
701 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600702 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700703
704 /**
705 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
706 * private information on secure lockscreens.
707 *
708 * {@see #visibility}
709 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700711
712 /**
713 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
714 *
715 * {@see #visibility}
716 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600717 public static final int VISIBILITY_SECRET = -1;
718
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400722 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500723
724 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400725 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400727 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500728
729 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400730 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500731 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400732 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733
734 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400735 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500736 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400737 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500738
739 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400740 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500741 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400742 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743
744 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400745 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500746 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400747 public static final String CATEGORY_ALARM = "alarm";
748
749 /**
750 * Notification category: progress of a long-running background operation.
751 */
752 public static final String CATEGORY_PROGRESS = "progress";
753
754 /**
755 * Notification category: social network or sharing update.
756 */
757 public static final String CATEGORY_SOCIAL = "social";
758
759 /**
760 * Notification category: error in background operation or authentication status.
761 */
762 public static final String CATEGORY_ERROR = "err";
763
764 /**
765 * Notification category: media transport control for playback.
766 */
767 public static final String CATEGORY_TRANSPORT = "transport";
768
769 /**
770 * Notification category: system or device status update. Reserved for system use.
771 */
772 public static final String CATEGORY_SYSTEM = "sys";
773
774 /**
775 * Notification category: indication of running background service.
776 */
777 public static final String CATEGORY_SERVICE = "service";
778
779 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400780 * Notification category: a specific, timely recommendation for a single thing.
781 * For example, a news app might want to recommend a news story it believes the user will
782 * want to read next.
783 */
784 public static final String CATEGORY_RECOMMENDATION = "recommendation";
785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: ongoing information about device or contextual status.
788 */
789 public static final String CATEGORY_STATUS = "status";
790
791 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400792 * Notification category: user-scheduled reminder.
793 */
794 public static final String CATEGORY_REMINDER = "reminder";
795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
798 * that best describes this Notification. May be used by the system for ranking and filtering.
799 */
800 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500801
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700802 private String mGroupKey;
803
804 /**
805 * Get the key used to group this notification into a cluster or stack
806 * with other notifications on devices which support such rendering.
807 */
808 public String getGroup() {
809 return mGroupKey;
810 }
811
812 private String mSortKey;
813
814 /**
815 * Get a sort key that orders this notification among other notifications from the
816 * same package. This can be useful if an external sort was already applied and an app
817 * would like to preserve this. Notifications will be sorted lexicographically using this
818 * value, although providing different priorities in addition to providing sort key may
819 * cause this value to be ignored.
820 *
821 * <p>This sort key can also be used to order members of a notification group. See
822 * {@link Builder#setGroup}.
823 *
824 * @see String#compareTo(String)
825 */
826 public String getSortKey() {
827 return mSortKey;
828 }
829
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500830 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400831 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400832 * <p>
833 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
834 * APIs, and are intended to be used by
835 * {@link android.service.notification.NotificationListenerService} implementations to extract
836 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500837 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 public Bundle extras = new Bundle();
839
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400840 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700841 * All pending intents in the notification as the system needs to be able to access them but
842 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700843 * custom parcelable objects.
844 *
845 * @hide
846 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700847 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700848
849 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700850 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
851 * pending intents inside of it, so only those will get the behavior.
852 *
853 * @hide
854 */
855 static public IBinder whitelistToken;
856
857 /**
858 * Must be set by a process to start associating tokens with Notification objects
859 * coming in to it. This is set by NotificationManagerService.
860 *
861 * @hide
862 */
863 static public IBinder processWhitelistToken;
864
865 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866 * {@link #extras} key: this is the title of the notification,
867 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
868 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500869 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400870
871 /**
872 * {@link #extras} key: this is the title of the notification when shown in expanded form,
873 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
874 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400875 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400876
877 /**
878 * {@link #extras} key: this is the main text payload, as supplied to
879 * {@link Builder#setContentText(CharSequence)}.
880 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500881 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400882
883 /**
884 * {@link #extras} key: this is a third line of text, as supplied to
885 * {@link Builder#setSubText(CharSequence)}.
886 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400887 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888
889 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800890 * {@link #extras} key: this is the remote input history, as supplied to
891 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700892 *
893 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
894 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
895 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
896 * notifications once the other party has responded).
897 *
898 * The extra with this key is of type CharSequence[] and contains the most recent entry at
899 * the 0 index, the second most recent at the 1 index, etc.
900 *
901 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800902 */
903 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
904
905 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400906 * {@link #extras} key: this is a small piece of additional text as supplied to
907 * {@link Builder#setContentInfo(CharSequence)}.
908 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400909 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400910
911 /**
912 * {@link #extras} key: this is a line of summary information intended to be shown
913 * alongside expanded notifications, as supplied to (e.g.)
914 * {@link BigTextStyle#setSummaryText(CharSequence)}.
915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917
918 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200919 * {@link #extras} key: this is the longer text shown in the big form of a
920 * {@link BigTextStyle} notification, as supplied to
921 * {@link BigTextStyle#bigText(CharSequence)}.
922 */
923 public static final String EXTRA_BIG_TEXT = "android.bigText";
924
925 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400926 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
927 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400928 *
929 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400930 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400931 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500932 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933
934 /**
935 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
936 * notification payload, as
937 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400938 *
939 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400941 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400942 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400943
944 /**
945 * {@link #extras} key: this is a bitmap to be used instead of the one from
946 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
947 * shown in its expanded form, as supplied to
948 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
949 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400950 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is the progress value supplied to
954 * {@link Builder#setProgress(int, int, boolean)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is the maximum value supplied to
960 * {@link Builder#setProgress(int, int, boolean)}.
961 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400962 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
966 * {@link Builder#setProgress(int, int, boolean)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400969
970 /**
971 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
972 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
973 * {@link Builder#setUsesChronometer(boolean)}.
974 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400975 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976
977 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800978 * {@link #extras} key: whether the chronometer set on the notification should count down
979 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700980 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800981 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700982 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800983
984 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985 * {@link #extras} key: whether {@link #when} should be shown,
986 * as supplied to {@link Builder#setShowWhen(boolean)}.
987 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400988 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400989
990 /**
991 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
992 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
993 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400994 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995
996 /**
997 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
998 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
999 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001000 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001001
1002 /**
1003 * {@link #extras} key: A string representing the name of the specific
1004 * {@link android.app.Notification.Style} used to create this notification.
1005 */
Chris Wren91ad5632013-06-05 15:05:57 -04001006 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001007
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001009 * {@link #extras} key: A String array containing the people that this notification relates to,
1010 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001013
1014 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001015 * Allow certain system-generated notifications to appear before the device is provisioned.
1016 * Only available to notifications coming from the android package.
1017 * @hide
1018 */
Maurice Lam96c10032017-03-29 15:42:38 -07001019 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001020 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001021 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1022
1023 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001024 * {@link #extras} key: A
1025 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1026 * in the background when the notification is selected. The URI must point to an image stream
1027 * suitable for passing into
1028 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1029 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1030 * URI used for this purpose must require no permissions to read the image data.
1031 */
1032 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1033
1034 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001035 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001036 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001037 * {@link android.app.Notification.MediaStyle} notification.
1038 */
1039 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1040
1041 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001042 * {@link #extras} key: the indices of actions to be shown in the compact view,
1043 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1044 */
1045 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1046
Christoph Studer943aa672014-08-03 20:31:16 +02001047 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001048 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1049 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001050 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1051 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001052 */
1053 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1054
1055 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001056 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001057 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001058 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001059 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001060
1061 /**
1062 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1063 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001064 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1065 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001066 */
1067 public static final String EXTRA_MESSAGES = "android.messages";
1068
1069 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001070 * {@link #extras} key: an array of
1071 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1072 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1073 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1074 * array of bundles.
1075 */
1076 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1077
1078 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001079 * {@link #extras} key: whether the notification should be colorized as
1080 * supplied to {@link Builder#setColorized(boolean)}}.
1081 */
1082 public static final String EXTRA_COLORIZED = "android.colorized";
1083
1084 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001085 * @hide
1086 */
1087 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1088
Selim Cinek247fa012016-02-18 09:50:48 -08001089 /**
1090 * @hide
1091 */
1092 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1093
Shane Brennan472a3b32016-12-12 15:28:10 -08001094 /**
1095 * {@link #extras} key: the audio contents of this notification.
1096 *
1097 * This is for use when rendering the notification on an audio-focused interface;
1098 * the audio contents are a complete sound sample that contains the contents/body of the
1099 * notification. This may be used in substitute of a Text-to-Speech reading of the
1100 * notification. For example if the notification represents a voice message this should point
1101 * to the audio of that message.
1102 *
1103 * The data stored under this key should be a String representation of a Uri that contains the
1104 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1105 *
1106 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1107 * has a field for holding data URI. That field can be used for audio.
1108 * See {@code Message#setData}.
1109 *
1110 * Example usage:
1111 * <pre>
1112 * {@code
1113 * Notification.Builder myBuilder = (build your Notification as normal);
1114 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1115 * }
1116 * </pre>
1117 */
1118 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1119
Dan Sandler80eaa592016-04-14 23:34:54 -04001120 /** @hide */
1121 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001122 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001123 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1124
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001125 /**
1126 * This is set on the notification shown by the activity manager about all apps
1127 * running in the background. It indicates that the notification should be shown
1128 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1129 * notification currently visible to the user. This is a string array of all
1130 * package names of the apps.
1131 * @hide
1132 */
1133 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1134
Dan Sandlerd63f9322015-05-06 15:18:49 -04001135 private Icon mSmallIcon;
1136 private Icon mLargeIcon;
1137
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001138 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001139 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001140
Julia Reynolds13d898c2017-02-02 12:22:05 -05001141 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001142 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001143
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001144 /** @hide */
1145 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1146 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1147 })
1148 @Retention(RetentionPolicy.SOURCE)
1149 public @interface GroupAlertBehavior {}
1150
1151 /**
1152 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1153 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1154 * notification will not be muted when it is in a group.
1155 */
1156 public static final int GROUP_ALERT_ALL = 0;
1157
1158 /**
1159 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1160 * notification in a group should be silenced (no sound or vibration) even if they are posted
1161 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1162 * mute this notification if this notification is a group child.
1163 *
1164 * <p> For example, you might want to use this constant if you post a number of children
1165 * notifications at once (say, after a periodic sync), and only need to notify the user
1166 * audibly once.
1167 */
1168 public static final int GROUP_ALERT_SUMMARY = 1;
1169
1170 /**
1171 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1172 * notification in a group should be silenced (no sound or vibration) even if they are
1173 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1174 * to mute this notification if this notification is a group summary.
1175 *
1176 * <p>For example, you might want to use this constant if only the children notifications
1177 * in your group have content and the summary is only used to visually group notifications.
1178 */
1179 public static final int GROUP_ALERT_CHILDREN = 2;
1180
Julia Reynolds2f431e22017-06-07 14:12:09 +00001181 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001182
Julia Reynolds13d898c2017-02-02 12:22:05 -05001183 /**
1184 * If this notification is being shown as a badge, always show as a number.
1185 */
1186 public static final int BADGE_ICON_NONE = 0;
1187
1188 /**
1189 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1190 * represent this notification.
1191 */
1192 public static final int BADGE_ICON_SMALL = 1;
1193
1194 /**
1195 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1196 * represent this notification.
1197 */
1198 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001199 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001200
Chris Wren51c75102013-07-16 20:49:17 -04001201 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001202 * Structure to encapsulate a named action that can be shown as part of this notification.
1203 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1204 * selected by the user.
1205 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001206 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1207 * or {@link Notification.Builder#addAction(Notification.Action)}
1208 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001209 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001210 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001211 /**
1212 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1213 * {@link RemoteInput}s.
1214 *
1215 * This is intended for {@link RemoteInput}s that only accept data, meaning
1216 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1217 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1218 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1219 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1220 *
1221 * You can test if a RemoteInput matches these constraints using
1222 * {@link RemoteInput#isDataOnly}.
1223 */
1224 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1225
Griff Hazen959591e2014-05-15 22:26:18 -07001226 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001227 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001228 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001229 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001230
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001231 /**
1232 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001233 *
1234 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001235 */
Dan Sandler86647982015-05-13 23:41:13 -04001236 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001237 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001238
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001239 /**
1240 * Title of the action.
1241 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001242 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001243
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001244 /**
1245 * Intent to send when the user invokes this action. May be null, in which case the action
1246 * may be rendered in a disabled presentation by the system UI.
1247 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001248 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001249
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001250 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001251 if (in.readInt() != 0) {
1252 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001253 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1254 icon = mIcon.getResId();
1255 }
Dan Sandler86647982015-05-13 23:41:13 -04001256 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001257 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1258 if (in.readInt() == 1) {
1259 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1260 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001261 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001262 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001263 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001265
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001266 /**
Dan Sandler86647982015-05-13 23:41:13 -04001267 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001268 */
Dan Sandler86647982015-05-13 23:41:13 -04001269 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001270 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001271 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001272 }
1273
Adrian Roos7af53622016-10-12 13:44:05 -07001274 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001275 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001276 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001277 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001278 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1279 this.icon = icon.getResId();
1280 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001281 this.title = title;
1282 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001283 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001284 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001285 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001286 }
1287
1288 /**
Dan Sandler86647982015-05-13 23:41:13 -04001289 * Return an icon representing the action.
1290 */
1291 public Icon getIcon() {
1292 if (mIcon == null && icon != 0) {
1293 // you snuck an icon in here without using the builder; let's try to keep it
1294 mIcon = Icon.createWithResource("", icon);
1295 }
1296 return mIcon;
1297 }
1298
1299 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001300 * Get additional metadata carried around with this Action.
1301 */
1302 public Bundle getExtras() {
1303 return mExtras;
1304 }
1305
1306 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001307 * Return whether the platform should automatically generate possible replies for this
1308 * {@link Action}
1309 */
1310 public boolean getAllowGeneratedReplies() {
1311 return mAllowGeneratedReplies;
1312 }
1313
1314 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001315 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001316 * May return null if no remote inputs were added. Only returns inputs which accept
1317 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001318 */
1319 public RemoteInput[] getRemoteInputs() {
1320 return mRemoteInputs;
1321 }
1322
1323 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001324 * Get the list of inputs to be collected from the user that ONLY accept data when this
1325 * action is sent. These remote inputs are guaranteed to return true on a call to
1326 * {@link RemoteInput#isDataOnly}.
1327 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001328 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001329 *
1330 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1331 * of non-textual RemoteInputs do not access these remote inputs.
1332 */
1333 public RemoteInput[] getDataOnlyRemoteInputs() {
1334 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1335 }
1336
1337 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001338 * Builder class for {@link Action} objects.
1339 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001340 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001341 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001342 private final CharSequence mTitle;
1343 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001344 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001345 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001346 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001347
1348 /**
1349 * Construct a new builder for {@link Action} object.
1350 * @param icon icon to show for this action
1351 * @param title the title of the action
1352 * @param intent the {@link PendingIntent} to fire when users trigger this action
1353 */
Dan Sandler86647982015-05-13 23:41:13 -04001354 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001355 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001356 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001357 }
1358
1359 /**
1360 * Construct a new builder for {@link Action} object.
1361 * @param icon icon to show for this action
1362 * @param title the title of the action
1363 * @param intent the {@link PendingIntent} to fire when users trigger this action
1364 */
1365 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001366 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001367 }
1368
1369 /**
1370 * Construct a new builder for {@link Action} object using the fields from an
1371 * {@link Action}.
1372 * @param action the action to read fields from.
1373 */
1374 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001375 this(action.getIcon(), action.title, action.actionIntent,
1376 new Bundle(action.mExtras), action.getRemoteInputs(),
1377 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001378 }
1379
Dan Sandler86647982015-05-13 23:41:13 -04001380 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001381 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001382 mIcon = icon;
1383 mTitle = title;
1384 mIntent = intent;
1385 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001386 if (remoteInputs != null) {
1387 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1388 Collections.addAll(mRemoteInputs, remoteInputs);
1389 }
Adrian Roos7af53622016-10-12 13:44:05 -07001390 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001391 }
1392
1393 /**
1394 * Merge additional metadata into this builder.
1395 *
1396 * <p>Values within the Bundle will replace existing extras values in this Builder.
1397 *
1398 * @see Notification.Action#extras
1399 */
1400 public Builder addExtras(Bundle extras) {
1401 if (extras != null) {
1402 mExtras.putAll(extras);
1403 }
1404 return this;
1405 }
1406
1407 /**
1408 * Get the metadata Bundle used by this Builder.
1409 *
1410 * <p>The returned Bundle is shared with this Builder.
1411 */
1412 public Bundle getExtras() {
1413 return mExtras;
1414 }
1415
1416 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001417 * Add an input to be collected from the user when this action is sent.
1418 * Response values can be retrieved from the fired intent by using the
1419 * {@link RemoteInput#getResultsFromIntent} function.
1420 * @param remoteInput a {@link RemoteInput} to add to the action
1421 * @return this object for method chaining
1422 */
1423 public Builder addRemoteInput(RemoteInput remoteInput) {
1424 if (mRemoteInputs == null) {
1425 mRemoteInputs = new ArrayList<RemoteInput>();
1426 }
1427 mRemoteInputs.add(remoteInput);
1428 return this;
1429 }
1430
1431 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001432 * Set whether the platform should automatically generate possible replies to add to
1433 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1434 * {@link RemoteInput}, this has no effect.
1435 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1436 * otherwise
1437 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001438 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001439 */
1440 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1441 mAllowGeneratedReplies = allowGeneratedReplies;
1442 return this;
1443 }
1444
1445 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001446 * Apply an extender to this action builder. Extenders may be used to add
1447 * metadata or change options on this builder.
1448 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001449 public Builder extend(Extender extender) {
1450 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001451 return this;
1452 }
1453
1454 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001455 * Combine all of the options that have been set and return a new {@link Action}
1456 * object.
1457 * @return the built action
1458 */
1459 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001460 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1461 RemoteInput[] previousDataInputs =
1462 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001463 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001464 for (RemoteInput input : previousDataInputs) {
1465 dataOnlyInputs.add(input);
1466 }
1467 }
1468 List<RemoteInput> textInputs = new ArrayList<>();
1469 if (mRemoteInputs != null) {
1470 for (RemoteInput input : mRemoteInputs) {
1471 if (input.isDataOnly()) {
1472 dataOnlyInputs.add(input);
1473 } else {
1474 textInputs.add(input);
1475 }
1476 }
1477 }
1478 if (!dataOnlyInputs.isEmpty()) {
1479 RemoteInput[] dataInputsArr =
1480 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1481 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1482 }
1483 RemoteInput[] textInputsArr = textInputs.isEmpty()
1484 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1485 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001486 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001487 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001489
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001490 @Override
1491 public Action clone() {
1492 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001493 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001494 title,
1495 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001496 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001497 getRemoteInputs(),
1498 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001499 }
1500 @Override
1501 public int describeContents() {
1502 return 0;
1503 }
1504 @Override
1505 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001506 final Icon ic = getIcon();
1507 if (ic != null) {
1508 out.writeInt(1);
1509 ic.writeToParcel(out, 0);
1510 } else {
1511 out.writeInt(0);
1512 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001513 TextUtils.writeToParcel(title, out, flags);
1514 if (actionIntent != null) {
1515 out.writeInt(1);
1516 actionIntent.writeToParcel(out, flags);
1517 } else {
1518 out.writeInt(0);
1519 }
Griff Hazen959591e2014-05-15 22:26:18 -07001520 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001521 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001522 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001523 }
Griff Hazen959591e2014-05-15 22:26:18 -07001524 public static final Parcelable.Creator<Action> CREATOR =
1525 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001526 public Action createFromParcel(Parcel in) {
1527 return new Action(in);
1528 }
1529 public Action[] newArray(int size) {
1530 return new Action[size];
1531 }
1532 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001533
1534 /**
1535 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1536 * metadata or change options on an action builder.
1537 */
1538 public interface Extender {
1539 /**
1540 * Apply this extender to a notification action builder.
1541 * @param builder the builder to be modified.
1542 * @return the build object for chaining.
1543 */
1544 public Builder extend(Builder builder);
1545 }
1546
1547 /**
1548 * Wearable extender for notification actions. To add extensions to an action,
1549 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1550 * the {@code WearableExtender()} constructor and apply it to a
1551 * {@link android.app.Notification.Action.Builder} using
1552 * {@link android.app.Notification.Action.Builder#extend}.
1553 *
1554 * <pre class="prettyprint">
1555 * Notification.Action action = new Notification.Action.Builder(
1556 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001557 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001558 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001559 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001560 */
1561 public static final class WearableExtender implements Extender {
1562 /** Notification action extra which contains wearable extensions */
1563 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1564
Pete Gastaf6781d2014-10-07 15:17:05 -04001565 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001566 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001567 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1568 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1569 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001570
1571 // Flags bitwise-ored to mFlags
1572 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001573 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001574 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001575
1576 // Default value for flags integer
1577 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1578
1579 private int mFlags = DEFAULT_FLAGS;
1580
Pete Gastaf6781d2014-10-07 15:17:05 -04001581 private CharSequence mInProgressLabel;
1582 private CharSequence mConfirmLabel;
1583 private CharSequence mCancelLabel;
1584
Griff Hazen61a9e862014-05-22 16:05:19 -07001585 /**
1586 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1587 * options.
1588 */
1589 public WearableExtender() {
1590 }
1591
1592 /**
1593 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1594 * wearable options present in an existing notification action.
1595 * @param action the notification action to inspect.
1596 */
1597 public WearableExtender(Action action) {
1598 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1599 if (wearableBundle != null) {
1600 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001601 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1602 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1603 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001604 }
1605 }
1606
1607 /**
1608 * Apply wearable extensions to a notification action that is being built. This is
1609 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1610 * method of {@link android.app.Notification.Action.Builder}.
1611 */
1612 @Override
1613 public Action.Builder extend(Action.Builder builder) {
1614 Bundle wearableBundle = new Bundle();
1615
1616 if (mFlags != DEFAULT_FLAGS) {
1617 wearableBundle.putInt(KEY_FLAGS, mFlags);
1618 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001619 if (mInProgressLabel != null) {
1620 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1621 }
1622 if (mConfirmLabel != null) {
1623 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1624 }
1625 if (mCancelLabel != null) {
1626 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1627 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001628
1629 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1630 return builder;
1631 }
1632
1633 @Override
1634 public WearableExtender clone() {
1635 WearableExtender that = new WearableExtender();
1636 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001637 that.mInProgressLabel = this.mInProgressLabel;
1638 that.mConfirmLabel = this.mConfirmLabel;
1639 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001640 return that;
1641 }
1642
1643 /**
1644 * Set whether this action is available when the wearable device is not connected to
1645 * a companion device. The user can still trigger this action when the wearable device is
1646 * offline, but a visual hint will indicate that the action may not be available.
1647 * Defaults to true.
1648 */
1649 public WearableExtender setAvailableOffline(boolean availableOffline) {
1650 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1651 return this;
1652 }
1653
1654 /**
1655 * Get whether this action is available when the wearable device is not connected to
1656 * a companion device. The user can still trigger this action when the wearable device is
1657 * offline, but a visual hint will indicate that the action may not be available.
1658 * Defaults to true.
1659 */
1660 public boolean isAvailableOffline() {
1661 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1662 }
1663
1664 private void setFlag(int mask, boolean value) {
1665 if (value) {
1666 mFlags |= mask;
1667 } else {
1668 mFlags &= ~mask;
1669 }
1670 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001671
1672 /**
1673 * Set a label to display while the wearable is preparing to automatically execute the
1674 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1675 *
1676 * @param label the label to display while the action is being prepared to execute
1677 * @return this object for method chaining
1678 */
1679 public WearableExtender setInProgressLabel(CharSequence label) {
1680 mInProgressLabel = label;
1681 return this;
1682 }
1683
1684 /**
1685 * Get the label to display while the wearable is preparing to automatically execute
1686 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1687 *
1688 * @return the label to display while the action is being prepared to execute
1689 */
1690 public CharSequence getInProgressLabel() {
1691 return mInProgressLabel;
1692 }
1693
1694 /**
1695 * Set a label to display to confirm that the action should be executed.
1696 * This is usually an imperative verb like "Send".
1697 *
1698 * @param label the label to confirm the action should be executed
1699 * @return this object for method chaining
1700 */
1701 public WearableExtender setConfirmLabel(CharSequence label) {
1702 mConfirmLabel = label;
1703 return this;
1704 }
1705
1706 /**
1707 * Get the label to display to confirm that the action should be executed.
1708 * This is usually an imperative verb like "Send".
1709 *
1710 * @return the label to confirm the action should be executed
1711 */
1712 public CharSequence getConfirmLabel() {
1713 return mConfirmLabel;
1714 }
1715
1716 /**
1717 * Set a label to display to cancel the action.
1718 * This is usually an imperative verb, like "Cancel".
1719 *
1720 * @param label the label to display to cancel the action
1721 * @return this object for method chaining
1722 */
1723 public WearableExtender setCancelLabel(CharSequence label) {
1724 mCancelLabel = label;
1725 return this;
1726 }
1727
1728 /**
1729 * Get the label to display to cancel the action.
1730 * This is usually an imperative verb like "Cancel".
1731 *
1732 * @return the label to display to cancel the action
1733 */
1734 public CharSequence getCancelLabel() {
1735 return mCancelLabel;
1736 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001737
1738 /**
1739 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1740 * platform that it can generate the appropriate transitions.
1741 * @param hintLaunchesActivity {@code true} if the content intent will launch
1742 * an activity and transitions should be generated, false otherwise.
1743 * @return this object for method chaining
1744 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001745 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001746 boolean hintLaunchesActivity) {
1747 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1748 return this;
1749 }
1750
1751 /**
1752 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1753 * platform that it can generate the appropriate transitions
1754 * @return {@code true} if the content intent will launch an activity and transitions
1755 * should be generated, false otherwise. The default value is {@code false} if this was
1756 * never set.
1757 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001758 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001759 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1760 }
Alex Hills9f087612016-06-07 09:08:59 -04001761
1762 /**
1763 * Set a hint that this Action should be displayed inline.
1764 *
1765 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1766 * otherwise
1767 * @return this object for method chaining
1768 */
1769 public WearableExtender setHintDisplayActionInline(
1770 boolean hintDisplayInline) {
1771 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1772 return this;
1773 }
1774
1775 /**
1776 * Get a hint that this Action should be displayed inline.
1777 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001778 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001779 * otherwise. The default value is {@code false} if this was never set.
1780 */
1781 public boolean getHintDisplayActionInline() {
1782 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1783 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001784 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001785 }
1786
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001787 /**
1788 * Array of all {@link Action} structures attached to this notification by
1789 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1790 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1791 * interface for invoking actions.
1792 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001793 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001794
1795 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001796 * Replacement version of this notification whose content will be shown
1797 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1798 * and {@link #VISIBILITY_PUBLIC}.
1799 */
1800 public Notification publicVersion;
1801
1802 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001803 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001804 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 */
1806 public Notification()
1807 {
1808 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001809 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001810 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812
1813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 * @hide
1815 */
1816 public Notification(Context context, int icon, CharSequence tickerText, long when,
1817 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1818 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001819 new Builder(context)
1820 .setWhen(when)
1821 .setSmallIcon(icon)
1822 .setTicker(tickerText)
1823 .setContentTitle(contentTitle)
1824 .setContentText(contentText)
1825 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1826 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
1828
1829 /**
1830 * Constructs a Notification object with the information needed to
1831 * have a status bar icon without the standard expanded view.
1832 *
1833 * @param icon The resource id of the icon to put in the status bar.
1834 * @param tickerText The text that flows by in the status bar when the notification first
1835 * activates.
1836 * @param when The time to show in the time field. In the System.currentTimeMillis
1837 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001838 *
1839 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001841 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 public Notification(int icon, CharSequence tickerText, long when)
1843 {
1844 this.icon = icon;
1845 this.tickerText = tickerText;
1846 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001847 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849
1850 /**
1851 * Unflatten the notification from a parcel.
1852 */
Svet Ganovddb94882016-06-23 19:55:24 -07001853 @SuppressWarnings("unchecked")
1854 public Notification(Parcel parcel) {
1855 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1856 // intents in extras are always written as the last entry.
1857 readFromParcelImpl(parcel);
1858 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001859 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001860 }
1861
1862 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 {
1864 int version = parcel.readInt();
1865
Dianne Hackborn98305522017-05-05 17:53:53 -07001866 whitelistToken = parcel.readStrongBinder();
1867 if (whitelistToken == null) {
1868 whitelistToken = processWhitelistToken;
1869 }
1870 // Propagate this token to all pending intents that are unmarshalled from the parcel.
1871 parcel.setClassCookie(PendingIntent.class, whitelistToken);
1872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001874 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001875 if (parcel.readInt() != 0) {
1876 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001877 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1878 icon = mSmallIcon.getResId();
1879 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 number = parcel.readInt();
1882 if (parcel.readInt() != 0) {
1883 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1884 }
1885 if (parcel.readInt() != 0) {
1886 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1887 }
1888 if (parcel.readInt() != 0) {
1889 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1890 }
1891 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001892 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001893 }
1894 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1896 }
Joe Onorato561d3852010-11-20 18:09:34 -08001897 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001898 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 defaults = parcel.readInt();
1901 flags = parcel.readInt();
1902 if (parcel.readInt() != 0) {
1903 sound = Uri.CREATOR.createFromParcel(parcel);
1904 }
1905
1906 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001907 if (parcel.readInt() != 0) {
1908 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 vibrate = parcel.createLongArray();
1911 ledARGB = parcel.readInt();
1912 ledOnMS = parcel.readInt();
1913 ledOffMS = parcel.readInt();
1914 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001915
1916 if (parcel.readInt() != 0) {
1917 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1918 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001919
1920 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001921
John Spurlockfd7f1e02014-03-18 16:41:57 -04001922 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001923
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001924 mGroupKey = parcel.readString();
1925
1926 mSortKey = parcel.readString();
1927
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001928 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001929
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001930 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1931
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001932 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001933 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1934 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001935
Chris Wren8fd39ec2014-02-27 17:43:26 -05001936 if (parcel.readInt() != 0) {
1937 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1938 }
1939
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001940 visibility = parcel.readInt();
1941
1942 if (parcel.readInt() != 0) {
1943 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1944 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001945
1946 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001947
1948 if (parcel.readInt() != 0) {
1949 mChannelId = parcel.readString();
1950 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001951 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001952
1953 if (parcel.readInt() != 0) {
1954 mShortcutId = parcel.readString();
1955 }
1956
1957 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001958
1959 if (parcel.readInt() != 0) {
1960 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1961 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001962
1963 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965
Andy Stadler110988c2010-12-03 14:29:16 -08001966 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001967 public Notification clone() {
1968 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001969 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001970 return that;
1971 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001972
Daniel Sandler1a497d32013-04-18 14:52:45 -04001973 /**
1974 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1975 * of this into that.
1976 * @hide
1977 */
1978 public void cloneInto(Notification that, boolean heavy) {
Dianne Hackborn98305522017-05-05 17:53:53 -07001979 that.whitelistToken = this.whitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001980 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001981 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001982 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001983 that.number = this.number;
1984
1985 // PendingIntents are global, so there's no reason (or way) to clone them.
1986 that.contentIntent = this.contentIntent;
1987 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001988 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001989
1990 if (this.tickerText != null) {
1991 that.tickerText = this.tickerText.toString();
1992 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001993 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001994 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001995 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001996 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001997 that.contentView = this.contentView.clone();
1998 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001999 if (heavy && this.mLargeIcon != null) {
2000 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002001 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002002 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002003 that.sound = this.sound; // android.net.Uri is immutable
2004 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002005 if (this.audioAttributes != null) {
2006 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2007 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002008
2009 final long[] vibrate = this.vibrate;
2010 if (vibrate != null) {
2011 final int N = vibrate.length;
2012 final long[] vib = that.vibrate = new long[N];
2013 System.arraycopy(vibrate, 0, vib, 0, N);
2014 }
2015
2016 that.ledARGB = this.ledARGB;
2017 that.ledOnMS = this.ledOnMS;
2018 that.ledOffMS = this.ledOffMS;
2019 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002020
Joe Onorato18e69df2010-05-17 22:26:12 -07002021 that.flags = this.flags;
2022
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002023 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002024
John Spurlockfd7f1e02014-03-18 16:41:57 -04002025 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002026
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002027 that.mGroupKey = this.mGroupKey;
2028
2029 that.mSortKey = this.mSortKey;
2030
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002032 try {
2033 that.extras = new Bundle(this.extras);
2034 // will unparcel
2035 that.extras.size();
2036 } catch (BadParcelableException e) {
2037 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2038 that.extras = null;
2039 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040 }
2041
Felipe Lemedd85da62016-06-28 11:29:54 -07002042 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2043 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002044 }
2045
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002046 if (this.actions != null) {
2047 that.actions = new Action[this.actions.length];
2048 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002049 if ( this.actions[i] != null) {
2050 that.actions[i] = this.actions[i].clone();
2051 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002052 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002053 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002054
Daniel Sandler1a497d32013-04-18 14:52:45 -04002055 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002056 that.bigContentView = this.bigContentView.clone();
2057 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002058
Chris Wren8fd39ec2014-02-27 17:43:26 -05002059 if (heavy && this.headsUpContentView != null) {
2060 that.headsUpContentView = this.headsUpContentView.clone();
2061 }
2062
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002063 that.visibility = this.visibility;
2064
2065 if (this.publicVersion != null) {
2066 that.publicVersion = new Notification();
2067 this.publicVersion.cloneInto(that.publicVersion, heavy);
2068 }
2069
Dan Sandler26e81cf2014-05-06 10:01:27 -04002070 that.color = this.color;
2071
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002072 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002073 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002074 that.mShortcutId = this.mShortcutId;
2075 that.mBadgeIcon = this.mBadgeIcon;
2076 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002077 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002078
Daniel Sandler1a497d32013-04-18 14:52:45 -04002079 if (!heavy) {
2080 that.lightenPayload(); // will clean out extras
2081 }
2082 }
2083
2084 /**
2085 * Removes heavyweight parts of the Notification object for archival or for sending to
2086 * listeners when the full contents are not necessary.
2087 * @hide
2088 */
2089 public final void lightenPayload() {
2090 tickerView = null;
2091 contentView = null;
2092 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002093 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002094 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002095 if (extras != null && !extras.isEmpty()) {
2096 final Set<String> keyset = extras.keySet();
2097 final int N = keyset.size();
2098 final String[] keys = keyset.toArray(new String[N]);
2099 for (int i=0; i<N; i++) {
2100 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002101 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2102 continue;
2103 }
Dan Sandler50128532015-12-08 15:42:41 -05002104 final Object obj = extras.get(key);
2105 if (obj != null &&
2106 ( obj instanceof Parcelable
2107 || obj instanceof Parcelable[]
2108 || obj instanceof SparseArray
2109 || obj instanceof ArrayList)) {
2110 extras.remove(key);
2111 }
2112 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002113 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002114 }
2115
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002116 /**
2117 * Make sure this CharSequence is safe to put into a bundle, which basically
2118 * means it had better not be some custom Parcelable implementation.
2119 * @hide
2120 */
2121 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002122 if (cs == null) return cs;
2123 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2124 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2125 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002126 if (cs instanceof Parcelable) {
2127 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2128 + " instance is a custom Parcelable and not allowed in Notification");
2129 return cs.toString();
2130 }
Selim Cinek60a54252016-02-26 17:03:25 -08002131 return removeTextSizeSpans(cs);
2132 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002133
Selim Cinek60a54252016-02-26 17:03:25 -08002134 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2135 if (charSequence instanceof Spanned) {
2136 Spanned ss = (Spanned) charSequence;
2137 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2138 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2139 for (Object span : spans) {
2140 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002141 if (resultSpan instanceof CharacterStyle) {
2142 resultSpan = ((CharacterStyle) span).getUnderlying();
2143 }
2144 if (resultSpan instanceof TextAppearanceSpan) {
2145 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002146 resultSpan = new TextAppearanceSpan(
2147 originalSpan.getFamily(),
2148 originalSpan.getTextStyle(),
2149 -1,
2150 originalSpan.getTextColor(),
2151 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002152 } else if (resultSpan instanceof RelativeSizeSpan
2153 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002154 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002155 } else {
2156 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002157 }
2158 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2159 ss.getSpanFlags(span));
2160 }
2161 return builder;
2162 }
2163 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002164 }
2165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 public int describeContents() {
2167 return 0;
2168 }
2169
2170 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002171 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 */
Svet Ganovddb94882016-06-23 19:55:24 -07002173 public void writeToParcel(Parcel parcel, int flags) {
2174 // We need to mark all pending intents getting into the notification
2175 // system as being put there to later allow the notification ranker
2176 // to launch them and by doing so add the app to the battery saver white
2177 // list for a short period of time. The problem is that the system
2178 // cannot look into the extras as there may be parcelables there that
2179 // the platform does not know how to handle. To go around that we have
2180 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002181 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002182 if (collectPendingIntents) {
2183 PendingIntent.setOnMarshaledListener(
2184 (PendingIntent intent, Parcel out, int outFlags) -> {
2185 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002186 if (allPendingIntents == null) {
2187 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002188 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002189 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002190 }
2191 });
2192 }
2193 try {
2194 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002195 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002196 writeToParcelImpl(parcel, flags);
2197 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002198 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002199 } finally {
2200 if (collectPendingIntents) {
2201 PendingIntent.setOnMarshaledListener(null);
2202 }
2203 }
2204 }
2205
2206 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 parcel.writeInt(1);
2208
Dianne Hackborn98305522017-05-05 17:53:53 -07002209 parcel.writeStrongBinder(whitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002211 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002212 if (mSmallIcon == null && icon != 0) {
2213 // you snuck an icon in here without using the builder; let's try to keep it
2214 mSmallIcon = Icon.createWithResource("", icon);
2215 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002216 if (mSmallIcon != null) {
2217 parcel.writeInt(1);
2218 mSmallIcon.writeToParcel(parcel, 0);
2219 } else {
2220 parcel.writeInt(0);
2221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 parcel.writeInt(number);
2223 if (contentIntent != null) {
2224 parcel.writeInt(1);
2225 contentIntent.writeToParcel(parcel, 0);
2226 } else {
2227 parcel.writeInt(0);
2228 }
2229 if (deleteIntent != null) {
2230 parcel.writeInt(1);
2231 deleteIntent.writeToParcel(parcel, 0);
2232 } else {
2233 parcel.writeInt(0);
2234 }
2235 if (tickerText != null) {
2236 parcel.writeInt(1);
2237 TextUtils.writeToParcel(tickerText, parcel, flags);
2238 } else {
2239 parcel.writeInt(0);
2240 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002241 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002242 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002243 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002244 } else {
2245 parcel.writeInt(0);
2246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 if (contentView != null) {
2248 parcel.writeInt(1);
2249 contentView.writeToParcel(parcel, 0);
2250 } else {
2251 parcel.writeInt(0);
2252 }
Selim Cinek279fa862016-06-14 10:57:25 -07002253 if (mLargeIcon == null && largeIcon != null) {
2254 // you snuck an icon in here without using the builder; let's try to keep it
2255 mLargeIcon = Icon.createWithBitmap(largeIcon);
2256 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002257 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002258 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002259 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002260 } else {
2261 parcel.writeInt(0);
2262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263
2264 parcel.writeInt(defaults);
2265 parcel.writeInt(this.flags);
2266
2267 if (sound != null) {
2268 parcel.writeInt(1);
2269 sound.writeToParcel(parcel, 0);
2270 } else {
2271 parcel.writeInt(0);
2272 }
2273 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002274
2275 if (audioAttributes != null) {
2276 parcel.writeInt(1);
2277 audioAttributes.writeToParcel(parcel, 0);
2278 } else {
2279 parcel.writeInt(0);
2280 }
2281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 parcel.writeLongArray(vibrate);
2283 parcel.writeInt(ledARGB);
2284 parcel.writeInt(ledOnMS);
2285 parcel.writeInt(ledOffMS);
2286 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002287
2288 if (fullScreenIntent != null) {
2289 parcel.writeInt(1);
2290 fullScreenIntent.writeToParcel(parcel, 0);
2291 } else {
2292 parcel.writeInt(0);
2293 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002294
2295 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002296
John Spurlockfd7f1e02014-03-18 16:41:57 -04002297 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002298
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002299 parcel.writeString(mGroupKey);
2300
2301 parcel.writeString(mSortKey);
2302
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002303 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002304
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002305 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002306
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002307 if (bigContentView != null) {
2308 parcel.writeInt(1);
2309 bigContentView.writeToParcel(parcel, 0);
2310 } else {
2311 parcel.writeInt(0);
2312 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002313
Chris Wren8fd39ec2014-02-27 17:43:26 -05002314 if (headsUpContentView != null) {
2315 parcel.writeInt(1);
2316 headsUpContentView.writeToParcel(parcel, 0);
2317 } else {
2318 parcel.writeInt(0);
2319 }
2320
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002321 parcel.writeInt(visibility);
2322
2323 if (publicVersion != null) {
2324 parcel.writeInt(1);
2325 publicVersion.writeToParcel(parcel, 0);
2326 } else {
2327 parcel.writeInt(0);
2328 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002329
2330 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002331
2332 if (mChannelId != null) {
2333 parcel.writeInt(1);
2334 parcel.writeString(mChannelId);
2335 } else {
2336 parcel.writeInt(0);
2337 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002338 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002339
2340 if (mShortcutId != null) {
2341 parcel.writeInt(1);
2342 parcel.writeString(mShortcutId);
2343 } else {
2344 parcel.writeInt(0);
2345 }
2346
2347 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002348
2349 if (mSettingsText != null) {
2350 parcel.writeInt(1);
2351 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2352 } else {
2353 parcel.writeInt(0);
2354 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002355
2356 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
2358
2359 /**
2360 * Parcelable.Creator that instantiates Notification objects
2361 */
2362 public static final Parcelable.Creator<Notification> CREATOR
2363 = new Parcelable.Creator<Notification>()
2364 {
2365 public Notification createFromParcel(Parcel parcel)
2366 {
2367 return new Notification(parcel);
2368 }
2369
2370 public Notification[] newArray(int size)
2371 {
2372 return new Notification[size];
2373 }
2374 };
2375
2376 /**
2377 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2378 * layout.
2379 *
2380 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2381 * in the view.</p>
2382 * @param context The context for your application / activity.
2383 * @param contentTitle The title that goes in the expanded entry.
2384 * @param contentText The text that goes in the expanded entry.
2385 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2386 * If this is an activity, it must include the
2387 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002388 * that you take care of task management as described in the
2389 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2390 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002391 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002392 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002393 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002395 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 public void setLatestEventInfo(Context context,
2397 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002398 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2399 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2400 new Throwable());
2401 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002402
Selim Cinek4ac6f602016-06-13 15:47:03 -07002403 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2404 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2405 }
2406
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002407 // ensure that any information already set directly is preserved
2408 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002409
2410 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002412 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
2414 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002415 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002417 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002418
2419 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421
Julia Reynoldsda303542015-11-23 14:00:20 -05002422 /**
2423 * @hide
2424 */
2425 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002426 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002427 }
2428
2429 /**
2430 * @hide
2431 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002432 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002433 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002434 }
2435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 @Override
2437 public String toString() {
2438 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002439 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002440 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002441 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002442 sb.append(priority);
2443 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002444 if (contentView != null) {
2445 sb.append(contentView.getPackage());
2446 sb.append("/0x");
2447 sb.append(Integer.toHexString(contentView.getLayoutId()));
2448 } else {
2449 sb.append("null");
2450 }
2451 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002452 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2453 sb.append("default");
2454 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 int N = this.vibrate.length-1;
2456 sb.append("[");
2457 for (int i=0; i<N; i++) {
2458 sb.append(this.vibrate[i]);
2459 sb.append(',');
2460 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002461 if (N != -1) {
2462 sb.append(this.vibrate[N]);
2463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 } else {
2466 sb.append("null");
2467 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002469 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002471 } else if (this.sound != null) {
2472 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 } else {
2474 sb.append("null");
2475 }
Chris Wren365b6d32015-07-16 10:39:26 -04002476 if (this.tickerText != null) {
2477 sb.append(" tick");
2478 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002479 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002481 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002482 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002483 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002484 if (this.category != null) {
2485 sb.append(" category=");
2486 sb.append(this.category);
2487 }
2488 if (this.mGroupKey != null) {
2489 sb.append(" groupKey=");
2490 sb.append(this.mGroupKey);
2491 }
2492 if (this.mSortKey != null) {
2493 sb.append(" sortKey=");
2494 sb.append(this.mSortKey);
2495 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002496 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002497 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002498 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002499 }
2500 sb.append(" vis=");
2501 sb.append(visibilityToString(this.visibility));
2502 if (this.publicVersion != null) {
2503 sb.append(" publicVersion=");
2504 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002505 }
2506 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 return sb.toString();
2508 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002509
Dan Sandler1b718782014-07-18 12:43:45 -04002510 /**
2511 * {@hide}
2512 */
2513 public static String visibilityToString(int vis) {
2514 switch (vis) {
2515 case VISIBILITY_PRIVATE:
2516 return "PRIVATE";
2517 case VISIBILITY_PUBLIC:
2518 return "PUBLIC";
2519 case VISIBILITY_SECRET:
2520 return "SECRET";
2521 default:
2522 return "UNKNOWN(" + String.valueOf(vis) + ")";
2523 }
2524 }
2525
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002527 * {@hide}
2528 */
2529 public static String priorityToString(@Priority int pri) {
2530 switch (pri) {
2531 case PRIORITY_MIN:
2532 return "MIN";
2533 case PRIORITY_LOW:
2534 return "LOW";
2535 case PRIORITY_DEFAULT:
2536 return "DEFAULT";
2537 case PRIORITY_HIGH:
2538 return "HIGH";
2539 case PRIORITY_MAX:
2540 return "MAX";
2541 default:
2542 return "UNKNOWN(" + String.valueOf(pri) + ")";
2543 }
2544 }
2545
Jeff Sharkey000ce802017-04-29 13:13:27 -06002546 /** @removed */
2547 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002548 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002549 return mChannelId;
2550 }
2551
2552 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002553 * Returns the id of the channel this notification posts to.
2554 */
2555 public String getChannelId() {
2556 return mChannelId;
2557 }
2558
Jeff Sharkey000ce802017-04-29 13:13:27 -06002559 /** @removed */
2560 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002561 public long getTimeout() {
2562 return mTimeout;
2563 }
2564
2565 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002566 * Returns the duration from posting after which this notification should be canceled by the
2567 * system, if it's not canceled already.
2568 */
2569 public long getTimeoutAfter() {
2570 return mTimeout;
2571 }
2572
2573 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002574 * Returns what icon should be shown for this notification if it is being displayed in a
2575 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2576 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2577 */
2578 public int getBadgeIconType() {
2579 return mBadgeIcon;
2580 }
2581
2582 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002583 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002584 *
2585 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2586 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002587 */
2588 public String getShortcutId() {
2589 return mShortcutId;
2590 }
2591
Julia Reynolds3aedded2017-03-31 14:42:09 -04002592
2593 /**
2594 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2595 */
2596 public CharSequence getSettingsText() {
2597 return mSettingsText;
2598 }
2599
Julia Reynolds13d898c2017-02-02 12:22:05 -05002600 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002601 * Returns which type of notifications in a group are responsible for audibly alerting the
2602 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2603 * {@link #GROUP_ALERT_SUMMARY}.
2604 */
2605 public @GroupAlertBehavior int getGroupAlertBehavior() {
2606 return mGroupAlertBehavior;
2607 }
2608
2609 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002610 * The small icon representing this notification in the status bar and content view.
2611 *
2612 * @return the small icon representing this notification.
2613 *
2614 * @see Builder#getSmallIcon()
2615 * @see Builder#setSmallIcon(Icon)
2616 */
2617 public Icon getSmallIcon() {
2618 return mSmallIcon;
2619 }
2620
2621 /**
2622 * Used when notifying to clean up legacy small icons.
2623 * @hide
2624 */
2625 public void setSmallIcon(Icon icon) {
2626 mSmallIcon = icon;
2627 }
2628
2629 /**
2630 * The large icon shown in this notification's content view.
2631 * @see Builder#getLargeIcon()
2632 * @see Builder#setLargeIcon(Icon)
2633 */
2634 public Icon getLargeIcon() {
2635 return mLargeIcon;
2636 }
2637
2638 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002639 * @hide
2640 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002641 public boolean isGroupSummary() {
2642 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2643 }
2644
2645 /**
2646 * @hide
2647 */
2648 public boolean isGroupChild() {
2649 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2650 }
2651
2652 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002653 * @hide
2654 */
2655 public boolean suppressAlertingDueToGrouping() {
2656 if (isGroupSummary()
2657 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2658 return true;
2659 } else if (isGroupChild()
2660 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2661 return true;
2662 }
2663 return false;
2664 }
2665
2666 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002667 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002668 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002670 * content views using the platform's notification layout template. If your app supports
2671 * versions of Android as old as API level 4, you can instead use
2672 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2673 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2674 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002675 *
Scott Main183bf112012-08-13 19:12:13 -07002676 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002677 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002678 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002679 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002680 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2681 * .setContentText(subject)
2682 * .setSmallIcon(R.drawable.new_mail)
2683 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002684 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002685 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002686 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002687 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002688 /**
2689 * @hide
2690 */
2691 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2692 "android.rebuild.contentViewActionCount";
2693 /**
2694 * @hide
2695 */
2696 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2697 = "android.rebuild.bigViewActionCount";
2698 /**
2699 * @hide
2700 */
2701 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2702 = "android.rebuild.hudViewActionCount";
2703
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002704 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002705
Selim Cinek6743c0b2017-01-18 18:24:01 -08002706 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2707 SystemProperties.getBoolean("notifications.only_title", true);
2708
Selim Cinek389edcd2017-05-11 19:16:44 -07002709 /**
2710 * The lightness difference that has to be added to the primary text color to obtain the
2711 * secondary text color when the background is light.
2712 */
2713 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2714
2715 /**
2716 * The lightness difference that has to be added to the primary text color to obtain the
2717 * secondary text color when the background is dark.
2718 * A bit less then the above value, since it looks better on dark backgrounds.
2719 */
2720 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2721
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002723 private Notification mN;
2724 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002725 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002726 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2727 private ArrayList<String> mPersonList = new ArrayList<String>();
2728 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002729 private boolean mIsLegacy;
2730 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002731
2732 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002733 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2734 */
2735 private int mCachedContrastColor = COLOR_INVALID;
2736 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002737 /**
2738 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2739 */
2740 private int mCachedAmbientColor = COLOR_INVALID;
2741 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002742
2743 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002744 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2745 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2746 */
2747 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002748 private int mTextColorsAreForBackground = COLOR_INVALID;
2749 private int mPrimaryTextColor = COLOR_INVALID;
2750 private int mSecondaryTextColor = COLOR_INVALID;
2751 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002752 private int mBackgroundColor = COLOR_INVALID;
2753 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002754 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002755 /**
2756 * A temporary location where actions are stored. If != null the view originally has action
2757 * but doesn't have any for this inflation.
2758 */
2759 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002760 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002761
Anthony Chenad4d1582017-04-10 16:07:58 -07002762 private boolean mTintActionButtons;
2763 private boolean mInNightMode;
2764
Adrian Roos70d7aa32017-01-11 15:39:06 -08002765 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002766 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002767 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002768 * @param context
2769 * A {@link Context} that will be used by the Builder to construct the
2770 * RemoteViews. The Context will not be held past the lifetime of this Builder
2771 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002772 * @param channelId
2773 * The constructed Notification will be posted on this
2774 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2775 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002776 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002777 public Builder(Context context, String channelId) {
2778 this(context, (Notification) null);
2779 mN.mChannelId = channelId;
2780 }
2781
2782 /**
2783 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2784 * instead. All posted Notifications must specify a NotificationChannel Id.
2785 */
2786 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002787 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002788 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002789 }
2790
Joe Onoratocb109a02011-01-18 17:57:41 -08002791 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002792 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002793 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002794 public Builder(Context context, Notification toAdopt) {
2795 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002796 Resources res = mContext.getResources();
2797 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2798
2799 if (res.getBoolean(R.bool.config_enableNightMode)) {
2800 Configuration currentConfig = res.getConfiguration();
2801 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2802 == Configuration.UI_MODE_NIGHT_YES;
2803 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002804
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002805 if (toAdopt == null) {
2806 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002807 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2808 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2809 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002810 mN.priority = PRIORITY_DEFAULT;
2811 mN.visibility = VISIBILITY_PRIVATE;
2812 } else {
2813 mN = toAdopt;
2814 if (mN.actions != null) {
2815 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002816 }
2817
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002818 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2819 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2820 }
2821
Selim Cinek4ac6f602016-06-13 15:47:03 -07002822 if (mN.getSmallIcon() == null && mN.icon != 0) {
2823 setSmallIcon(mN.icon);
2824 }
2825
2826 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2827 setLargeIcon(mN.largeIcon);
2828 }
2829
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002830 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2831 if (!TextUtils.isEmpty(templateClass)) {
2832 final Class<? extends Style> styleClass
2833 = getNotificationStyleClass(templateClass);
2834 if (styleClass == null) {
2835 Log.d(TAG, "Unknown style class: " + templateClass);
2836 } else {
2837 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002838 final Constructor<? extends Style> ctor =
2839 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002840 ctor.setAccessible(true);
2841 final Style style = ctor.newInstance();
2842 style.restoreFromExtras(mN.extras);
2843
2844 if (style != null) {
2845 setStyle(style);
2846 }
2847 } catch (Throwable t) {
2848 Log.e(TAG, "Could not create Style", t);
2849 }
2850 }
2851 }
2852
2853 }
2854 }
2855
2856 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002857 if (mColorUtil == null) {
2858 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002859 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002860 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002861 }
2862
2863 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002864 * If this notification is duplicative of a Launcher shortcut, sets the
2865 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2866 * the shortcut.
2867 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002868 * This field will be ignored by Launchers that don't support badging, don't show
2869 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002870 *
2871 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2872 * supersedes
2873 */
2874 public Builder setShortcutId(String shortcutId) {
2875 mN.mShortcutId = shortcutId;
2876 return this;
2877 }
2878
2879 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002880 * Sets which icon to display as a badge for this notification.
2881 *
2882 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2883 * {@link #BADGE_ICON_LARGE}.
2884 *
2885 * Note: This value might be ignored, for launchers that don't support badge icons.
2886 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002887 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002888 mN.mBadgeIcon = icon;
2889 return this;
2890 }
2891
2892 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002893 * Sets the group alert behavior for this notification. Use this method to mute this
2894 * notification if alerts for this notification's group should be handled by a different
2895 * notification. This is only applicable for notifications that belong to a
2896 * {@link #setGroup(String) group}.
2897 *
2898 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2899 */
2900 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2901 mN.mGroupAlertBehavior = groupAlertBehavior;
2902 return this;
2903 }
2904
Jeff Sharkey000ce802017-04-29 13:13:27 -06002905 /** @removed */
2906 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002907 public Builder setChannel(String channelId) {
2908 mN.mChannelId = channelId;
2909 return this;
2910 }
2911
2912 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002913 * Specifies the channel the notification should be delivered on.
2914 */
2915 public Builder setChannelId(String channelId) {
2916 mN.mChannelId = channelId;
2917 return this;
2918 }
2919
Jeff Sharkey000ce802017-04-29 13:13:27 -06002920 /** @removed */
2921 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002922 public Builder setTimeout(long durationMs) {
2923 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002924 return this;
2925 }
2926
2927 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002928 * Specifies a duration in milliseconds after which this notification should be canceled,
2929 * if it is not already canceled.
2930 */
2931 public Builder setTimeoutAfter(long durationMs) {
2932 mN.mTimeout = durationMs;
2933 return this;
2934 }
2935
2936 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002937 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002938 *
2939 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2940 * shown anymore by default and must be opted into by using
2941 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002942 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002943 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002944 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002945 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002946 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002947 return this;
2948 }
2949
Joe Onoratocb109a02011-01-18 17:57:41 -08002950 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002951 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002952 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002953 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2954 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002955 */
2956 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002957 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002958 return this;
2959 }
2960
2961 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002962 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002963 *
2964 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002965 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002966 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002967 * Useful when showing an elapsed time (like an ongoing phone call).
2968 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002969 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002970 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002971 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002972 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002973 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002974 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002975 */
2976 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002977 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002978 return this;
2979 }
2980
2981 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002982 * Sets the Chronometer to count down instead of counting up.
2983 *
2984 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2985 * If it isn't set the chronometer will count up.
2986 *
2987 * @see #setUsesChronometer(boolean)
2988 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002989 public Builder setChronometerCountDown(boolean countDown) {
2990 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002991 return this;
2992 }
2993
2994 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002995 * Set the small icon resource, which will be used to represent the notification in the
2996 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002997 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002998
2999 * The platform template for the expanded view will draw this icon in the left, unless a
3000 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3001 * icon will be moved to the right-hand side.
3002 *
3003
3004 * @param icon
3005 * A resource ID in the application's package of the drawable to use.
3006 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003007 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003008 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003009 return setSmallIcon(icon != 0
3010 ? Icon.createWithResource(mContext, icon)
3011 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003012 }
3013
Joe Onoratocb109a02011-01-18 17:57:41 -08003014 /**
3015 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3016 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3017 * LevelListDrawable}.
3018 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003019 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003020 * @param level The level to use for the icon.
3021 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003022 * @see Notification#icon
3023 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003024 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003025 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003026 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003027 return setSmallIcon(icon);
3028 }
3029
3030 /**
3031 * Set the small icon, which will be used to represent the notification in the
3032 * status bar and content view (unless overriden there by a
3033 * {@link #setLargeIcon(Bitmap) large icon}).
3034 *
3035 * @param icon An Icon object to use.
3036 * @see Notification#icon
3037 */
3038 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003039 mN.setSmallIcon(icon);
3040 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3041 mN.icon = icon.getResId();
3042 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003043 return this;
3044 }
3045
Joe Onoratocb109a02011-01-18 17:57:41 -08003046 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003047 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003048 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003049 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003050 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003051 return this;
3052 }
3053
Joe Onoratocb109a02011-01-18 17:57:41 -08003054 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003055 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003056 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003057 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003059 return this;
3060 }
3061
Joe Onoratocb109a02011-01-18 17:57:41 -08003062 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003063 * This provides some additional information that is displayed in the notification. No
3064 * guarantees are given where exactly it is displayed.
3065 *
3066 * <p>This information should only be provided if it provides an essential
3067 * benefit to the understanding of the notification. The more text you provide the
3068 * less readable it becomes. For example, an email client should only provide the account
3069 * name here if more than one email account has been added.</p>
3070 *
3071 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3072 * notification header area.
3073 *
3074 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3075 * this will be shown in the third line of text in the platform notification template.
3076 * You should not be using {@link #setProgress(int, int, boolean)} at the
3077 * same time on those versions; they occupy the same place.
3078 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003079 */
3080 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003081 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003082 return this;
3083 }
3084
3085 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003086 * Provides text that will appear as a link to your application's settings.
3087 *
3088 * <p>This text does not appear within notification {@link Style templates} but may
3089 * appear when the user uses an affordance to learn more about the notification.
3090 * Additionally, this text will not appear unless you provide a valid link target by
3091 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3092 *
3093 * <p>This text is meant to be concise description about what the user can customize
3094 * when they click on this link. The recommended maximum length is 40 characters.
3095 * @param text
3096 * @return
3097 */
3098 public Builder setSettingsText(CharSequence text) {
3099 mN.mSettingsText = safeCharSequence(text);
3100 return this;
3101 }
3102
3103 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003104 * Set the remote input history.
3105 *
3106 * This should be set to the most recent inputs that have been sent
3107 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3108 * longer relevant (e.g. for chat notifications once the other party has responded).
3109 *
3110 * The most recent input must be stored at the 0 index, the second most recent at the
3111 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3112 * and how much of each individual input is shown.
3113 *
3114 * <p>Note: The reply text will only be shown on notifications that have least one action
3115 * with a {@code RemoteInput}.</p>
3116 */
3117 public Builder setRemoteInputHistory(CharSequence[] text) {
3118 if (text == null) {
3119 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3120 } else {
3121 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3122 CharSequence[] safe = new CharSequence[N];
3123 for (int i = 0; i < N; i++) {
3124 safe[i] = safeCharSequence(text[i]);
3125 }
3126 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3127 }
3128 return this;
3129 }
3130
3131 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003132 * Sets the number of items this notification represents. May be displayed as a badge count
3133 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003134 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003135 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003136 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003137 return this;
3138 }
3139
Joe Onoratocb109a02011-01-18 17:57:41 -08003140 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003141 * A small piece of additional information pertaining to this notification.
3142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003143 * The platform template will draw this on the last line of the notification, at the far
3144 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003145 *
3146 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3147 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3148 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003149 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003150 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003151 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003152 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003153 return this;
3154 }
3155
Joe Onoratocb109a02011-01-18 17:57:41 -08003156 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003157 * Set the progress this notification represents.
3158 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003159 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003160 */
3161 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003162 mN.extras.putInt(EXTRA_PROGRESS, progress);
3163 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3164 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003165 return this;
3166 }
3167
3168 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003169 * Supply a custom RemoteViews to use instead of the platform template.
3170 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003171 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003172 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003173 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003174 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003175 return setCustomContentView(views);
3176 }
3177
3178 /**
3179 * Supply custom RemoteViews to use instead of the platform template.
3180 *
3181 * This will override the layout that would otherwise be constructed by this Builder
3182 * object.
3183 */
3184 public Builder setCustomContentView(RemoteViews contentView) {
3185 mN.contentView = contentView;
3186 return this;
3187 }
3188
3189 /**
3190 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3191 *
3192 * This will override the expanded layout that would otherwise be constructed by this
3193 * Builder object.
3194 */
3195 public Builder setCustomBigContentView(RemoteViews contentView) {
3196 mN.bigContentView = contentView;
3197 return this;
3198 }
3199
3200 /**
3201 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3202 *
3203 * This will override the heads-up layout that would otherwise be constructed by this
3204 * Builder object.
3205 */
3206 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3207 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 return this;
3209 }
3210
Joe Onoratocb109a02011-01-18 17:57:41 -08003211 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003212 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3213 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003214 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3215 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3216 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003217 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003218 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003219 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003220 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003221 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003222 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003223 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003224 return this;
3225 }
3226
Joe Onoratocb109a02011-01-18 17:57:41 -08003227 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003228 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3229 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003230 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003231 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003232 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003233 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003234 return this;
3235 }
3236
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 /**
3238 * An intent to launch instead of posting the notification to the status bar.
3239 * Only for use with extremely high-priority notifications demanding the user's
3240 * <strong>immediate</strong> attention, such as an incoming phone call or
3241 * alarm clock that the user has explicitly set to a particular time.
3242 * If this facility is used for something else, please give the user an option
3243 * to turn it off and use a normal notification, as this can be extremely
3244 * disruptive.
3245 *
Chris Wren47c20a12014-06-18 17:27:29 -04003246 * <p>
3247 * The system UI may choose to display a heads-up notification, instead of
3248 * launching this intent, while the user is using the device.
3249 * </p>
3250 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003251 * @param intent The pending intent to launch.
3252 * @param highPriority Passing true will cause this notification to be sent
3253 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003254 *
3255 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003256 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003257 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003258 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003259 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3260 return this;
3261 }
3262
Joe Onoratocb109a02011-01-18 17:57:41 -08003263 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003264 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003265 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003266 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003267 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003268 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003269 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003270 return this;
3271 }
3272
Joe Onoratocb109a02011-01-18 17:57:41 -08003273 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003274 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003275 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003277 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003278 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003279 setTicker(tickerText);
3280 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 return this;
3282 }
3283
Joe Onoratocb109a02011-01-18 17:57:41 -08003284 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003285 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 *
3287 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003288 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3289 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003290 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003291 public Builder setLargeIcon(Bitmap b) {
3292 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3293 }
3294
3295 /**
3296 * Add a large icon to the notification content view.
3297 *
3298 * In the platform template, this image will be shown on the left of the notification view
3299 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3300 * badge atop the large icon).
3301 */
3302 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003303 mN.mLargeIcon = icon;
3304 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003305 return this;
3306 }
3307
Joe Onoratocb109a02011-01-18 17:57:41 -08003308 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003309 * Set the sound to play.
3310 *
John Spurlockc0650f022014-07-19 13:22:39 -04003311 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3312 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003313 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003314 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003315 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003316 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003317 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003318 mN.sound = sound;
3319 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003320 return this;
3321 }
3322
Joe Onoratocb109a02011-01-18 17:57:41 -08003323 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003324 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003325 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003326 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3327 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003328 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003329 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003330 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003331 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003332 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003333 mN.sound = sound;
3334 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003335 return this;
3336 }
3337
Joe Onoratocb109a02011-01-18 17:57:41 -08003338 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003339 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3340 * use during playback.
3341 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003342 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003343 * @see Notification#sound
3344 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003345 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003346 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003347 mN.sound = sound;
3348 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003349 return this;
3350 }
3351
3352 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003353 * Set the vibration pattern to use.
3354 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003355 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3356 * <code>pattern</code> parameter.
3357 *
Chris Wren47c20a12014-06-18 17:27:29 -04003358 * <p>
3359 * A notification that vibrates is more likely to be presented as a heads-up notification.
3360 * </p>
3361 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003362 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003363 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003364 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003365 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003366 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003367 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003368 return this;
3369 }
3370
Joe Onoratocb109a02011-01-18 17:57:41 -08003371 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003372 * Set the desired color for the indicator LED on the device, as well as the
3373 * blink duty cycle (specified in milliseconds).
3374 *
3375
3376 * Not all devices will honor all (or even any) of these values.
3377 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003378 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003379 * @see Notification#ledARGB
3380 * @see Notification#ledOnMS
3381 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003382 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003383 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003384 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003385 mN.ledARGB = argb;
3386 mN.ledOnMS = onMs;
3387 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003388 if (onMs != 0 || offMs != 0) {
3389 mN.flags |= FLAG_SHOW_LIGHTS;
3390 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003391 return this;
3392 }
3393
Joe Onoratocb109a02011-01-18 17:57:41 -08003394 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003395 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003396 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003397
3398 * Ongoing notifications cannot be dismissed by the user, so your application or service
3399 * must take care of canceling them.
3400 *
3401
3402 * They are typically used to indicate a background task that the user is actively engaged
3403 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3404 * (e.g., a file download, sync operation, active network connection).
3405 *
3406
3407 * @see Notification#FLAG_ONGOING_EVENT
3408 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003409 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003410 public Builder setOngoing(boolean ongoing) {
3411 setFlag(FLAG_ONGOING_EVENT, ongoing);
3412 return this;
3413 }
3414
Joe Onoratocb109a02011-01-18 17:57:41 -08003415 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003416 * Set whether this notification should be colorized. When set, the color set with
3417 * {@link #setColor(int)} will be used as the background color of this notification.
3418 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003419 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3420 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003421 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003422 * For most styles, the coloring will only be applied if the notification is for a
3423 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003424 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003425 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003426 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003427 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003428 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003429 */
3430 public Builder setColorized(boolean colorize) {
3431 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3432 return this;
3433 }
3434
3435 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003436 * Set this flag if you would only like the sound, vibrate
3437 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003438 *
3439 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003440 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3442 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3443 return this;
3444 }
3445
Joe Onoratocb109a02011-01-18 17:57:41 -08003446 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003447 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003448 *
3449 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003450 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003451 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003452 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003453 return this;
3454 }
3455
Joe Onoratocb109a02011-01-18 17:57:41 -08003456 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003457 * Set whether or not this notification should not bridge to other devices.
3458 *
3459 * <p>Some notifications can be bridged to other devices for remote display.
3460 * This hint can be set to recommend this notification not be bridged.
3461 */
3462 public Builder setLocalOnly(boolean localOnly) {
3463 setFlag(FLAG_LOCAL_ONLY, localOnly);
3464 return this;
3465 }
3466
3467 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003468 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003469 * <p>
3470 * The value should be one or more of the following fields combined with
3471 * bitwise-or:
3472 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3473 * <p>
3474 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003475 *
3476 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003477 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003478 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003479 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003480 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003481 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003482 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003483 return this;
3484 }
3485
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003486 /**
3487 * Set the priority of this notification.
3488 *
3489 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003490 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003491 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003492 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003493 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003494 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003495 return this;
3496 }
Joe Malin8d40d042012-11-05 11:36:40 -08003497
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003498 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003499 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003500 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003501 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003502 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003503 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003504 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 return this;
3506 }
3507
3508 /**
Chris Wrendde75302014-03-26 17:24:15 -04003509 * Add a person that is relevant to this notification.
3510 *
Chris Wrene6c48932014-09-29 17:19:27 -04003511 * <P>
3512 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003513 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3514 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3515 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003516 * </P>
3517 *
3518 * <P>
3519 * The person should be specified by the {@code String} representation of a
3520 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3521 * </P>
3522 *
3523 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3524 * URIs. The path part of these URIs must exist in the contacts database, in the
3525 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3526 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3527 * </P>
3528 *
3529 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003530 * @see Notification#EXTRA_PEOPLE
3531 */
Chris Wrene6c48932014-09-29 17:19:27 -04003532 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003533 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003534 return this;
3535 }
3536
3537 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003538 * Set this notification to be part of a group of notifications sharing the same key.
3539 * Grouped notifications may display in a cluster or stack on devices which
3540 * support such rendering.
3541 *
3542 * <p>To make this notification the summary for its group, also call
3543 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3544 * {@link #setSortKey}.
3545 * @param groupKey The group key of the group.
3546 * @return this object for method chaining
3547 */
3548 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003549 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003550 return this;
3551 }
3552
3553 /**
3554 * Set this notification to be the group summary for a group of notifications.
3555 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003556 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3557 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003558 * @param isGroupSummary Whether this notification should be a group summary.
3559 * @return this object for method chaining
3560 */
3561 public Builder setGroupSummary(boolean isGroupSummary) {
3562 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3563 return this;
3564 }
3565
3566 /**
3567 * Set a sort key that orders this notification among other notifications from the
3568 * same package. This can be useful if an external sort was already applied and an app
3569 * would like to preserve this. Notifications will be sorted lexicographically using this
3570 * value, although providing different priorities in addition to providing sort key may
3571 * cause this value to be ignored.
3572 *
3573 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003574 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003575 *
3576 * @see String#compareTo(String)
3577 */
3578 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003579 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003580 return this;
3581 }
3582
3583 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003584 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003585 *
Griff Hazen720042b2014-02-24 15:46:56 -08003586 * <p>Values within the Bundle will replace existing extras values in this Builder.
3587 *
3588 * @see Notification#extras
3589 */
Griff Hazen959591e2014-05-15 22:26:18 -07003590 public Builder addExtras(Bundle extras) {
3591 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003592 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003593 }
3594 return this;
3595 }
3596
3597 /**
3598 * Set metadata for this notification.
3599 *
3600 * <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 -04003601 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 * called.
3603 *
Griff Hazen720042b2014-02-24 15:46:56 -08003604 * <p>Replaces any existing extras values with those from the provided Bundle.
3605 * Use {@link #addExtras} to merge in metadata instead.
3606 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003607 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003608 */
Griff Hazen959591e2014-05-15 22:26:18 -07003609 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003610 if (extras != null) {
3611 mUserExtras = extras;
3612 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003613 return this;
3614 }
3615
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003616 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003617 * Get the current metadata Bundle used by this notification Builder.
3618 *
3619 * <p>The returned Bundle is shared with this Builder.
3620 *
3621 * <p>The current contents of this Bundle are copied into the Notification each time
3622 * {@link #build()} is called.
3623 *
3624 * @see Notification#extras
3625 */
3626 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003627 return mUserExtras;
3628 }
3629
3630 private Bundle getAllExtras() {
3631 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3632 saveExtras.putAll(mN.extras);
3633 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003634 }
3635
3636 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003637 * Add an action to this notification. Actions are typically displayed by
3638 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003639 * <p>
3640 * Every action must have an icon (32dp square and matching the
3641 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3642 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3643 * <p>
3644 * A notification in its expanded form can display up to 3 actions, from left to right in
3645 * the order they were added. Actions will not be displayed when the notification is
3646 * collapsed, however, so be sure that any essential functions may be accessed by the user
3647 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003648 *
3649 * @param icon Resource ID of a drawable that represents the action.
3650 * @param title Text describing the action.
3651 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003652 *
3653 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003654 */
Dan Sandler86647982015-05-13 23:41:13 -04003655 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003656 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003657 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003658 return this;
3659 }
3660
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003661 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003662 * Add an action to this notification. Actions are typically displayed by
3663 * the system as a button adjacent to the notification content.
3664 * <p>
3665 * Every action must have an icon (32dp square and matching the
3666 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3667 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3668 * <p>
3669 * A notification in its expanded form can display up to 3 actions, from left to right in
3670 * the order they were added. Actions will not be displayed when the notification is
3671 * collapsed, however, so be sure that any essential functions may be accessed by the user
3672 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3673 *
3674 * @param action The action to add.
3675 */
3676 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003677 if (action != null) {
3678 mActions.add(action);
3679 }
Griff Hazen959591e2014-05-15 22:26:18 -07003680 return this;
3681 }
3682
3683 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003684 * Alter the complete list of actions attached to this notification.
3685 * @see #addAction(Action).
3686 *
3687 * @param actions
3688 * @return
3689 */
3690 public Builder setActions(Action... actions) {
3691 mActions.clear();
3692 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003693 if (actions[i] != null) {
3694 mActions.add(actions[i]);
3695 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696 }
3697 return this;
3698 }
3699
3700 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003701 * Add a rich notification style to be applied at build time.
3702 *
3703 * @param style Object responsible for modifying the notification style.
3704 */
3705 public Builder setStyle(Style style) {
3706 if (mStyle != style) {
3707 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003708 if (mStyle != null) {
3709 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003710 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3711 } else {
3712 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003713 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003714 }
3715 return this;
3716 }
3717
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003718 /**
3719 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003720 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003721 * @return The same Builder.
3722 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003723 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003725 return this;
3726 }
3727
3728 /**
3729 * Supply a replacement Notification whose contents should be shown in insecure contexts
3730 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3731 * @param n A replacement notification, presumably with some or all info redacted.
3732 * @return The same Builder.
3733 */
3734 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003735 if (n != null) {
3736 mN.publicVersion = new Notification();
3737 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3738 } else {
3739 mN.publicVersion = null;
3740 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003741 return this;
3742 }
3743
Griff Hazenb720abe2014-05-20 13:15:30 -07003744 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003745 * Apply an extender to this notification builder. Extenders may be used to add
3746 * metadata or change options on this builder.
3747 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003748 public Builder extend(Extender extender) {
3749 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003750 return this;
3751 }
3752
Dan Sandler4e787062015-06-17 15:09:48 -04003753 /**
3754 * @hide
3755 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003756 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003757 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003758 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003759 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003760 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003761 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003762 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003763 }
3764
Dan Sandler26e81cf2014-05-06 10:01:27 -04003765 /**
3766 * Sets {@link Notification#color}.
3767 *
3768 * @param argb The accent color to use
3769 *
3770 * @return The same Builder.
3771 */
Tor Norbye80756e32015-03-02 09:39:27 -08003772 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003773 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003774 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003775 return this;
3776 }
3777
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003778 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003779 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3780 // This user can never be a badged profile,
3781 // and also includes USER_ALL system notifications.
3782 return null;
3783 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003784 // Note: This assumes that the current user can read the profile badge of the
3785 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003786 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003787 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003788 }
3789
3790 private Bitmap getProfileBadge() {
3791 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003792 if (badge == null) {
3793 return null;
3794 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003795 final int size = mContext.getResources().getDimensionPixelSize(
3796 R.dimen.notification_badge_size);
3797 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003798 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003799 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003800 badge.draw(canvas);
3801 return bitmap;
3802 }
3803
Selim Cinekc848c3a2016-01-13 15:27:30 -08003804 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003805 Bitmap profileBadge = getProfileBadge();
3806
Kenny Guy98193ea2014-07-24 19:54:37 +01003807 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003808 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3809 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003810 if (isColorized()) {
3811 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3812 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3813 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003814 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003815 }
3816
Christoph Studerfe718432014-09-01 18:21:18 +02003817 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003818 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003819 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003820 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003821 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003822 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003823 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003824 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003825 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003826 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003827 }
3828
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003829 /**
3830 * Resets the notification header to its original state
3831 */
3832 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003833 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3834 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003835 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003836 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003837 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003838 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003839 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003840 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003841 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003842 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003843 contentView.setImageViewIcon(R.id.profile_badge, null);
3844 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003845 }
3846
3847 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003848 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3849 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003850 }
3851
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003852 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003853 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003854 }
3855
3856 /**
3857 * @param hasProgress whether the progress bar should be shown and set
3858 */
3859 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003860 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3861 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003862 }
3863
Adrian Roos70d7aa32017-01-11 15:39:06 -08003864 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003865 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003866
Christoph Studerfe718432014-09-01 18:21:18 +02003867 resetStandardTemplate(contentView);
3868
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003870 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003871 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003872 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003873 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3874 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003875 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003876 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003877 if (!p.ambient) {
3878 setTextViewColorPrimary(contentView, R.id.title);
3879 }
Selim Cinek954cc232016-05-20 13:29:23 -07003880 contentView.setViewLayoutWidth(R.id.title, showProgress
3881 ? ViewGroup.LayoutParams.WRAP_CONTENT
3882 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003883 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003884 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003885 int textId = showProgress ? com.android.internal.R.id.text_line_1
3886 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003887 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003888 if (!p.ambient) {
3889 setTextViewColorSecondary(contentView, textId);
3890 }
Selim Cinek41598732016-01-11 16:58:37 -08003891 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003892 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003893
Selim Cinek279fa862016-06-14 10:57:25 -07003894 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003895
Selim Cinek29603462015-11-17 19:04:39 -08003896 return contentView;
3897 }
3898
Selim Cinek7b9605b2017-01-19 17:36:00 -08003899 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3900 ensureColors();
3901 contentView.setTextColor(id, mPrimaryTextColor);
3902 }
3903
Selim Cinek389edcd2017-05-11 19:16:44 -07003904 /**
3905 * @return the primary text color
3906 * @hide
3907 */
3908 @VisibleForTesting
3909 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003910 ensureColors();
3911 return mPrimaryTextColor;
3912 }
3913
Selim Cinek389edcd2017-05-11 19:16:44 -07003914 /**
3915 * @return the secondary text color
3916 * @hide
3917 */
3918 @VisibleForTesting
3919 public int getSecondaryTextColor() {
3920 ensureColors();
3921 return mSecondaryTextColor;
3922 }
3923
Selim Cinek7b9605b2017-01-19 17:36:00 -08003924 private int getActionBarColor() {
3925 ensureColors();
3926 return mActionBarColor;
3927 }
3928
Selim Cinek622c64a2017-04-17 17:10:05 -07003929 private int getActionBarColorDeEmphasized() {
3930 int backgroundColor = getBackgroundColor();
3931 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3932 }
3933
Selim Cinek7b9605b2017-01-19 17:36:00 -08003934 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3935 ensureColors();
3936 contentView.setTextColor(id, mSecondaryTextColor);
3937 }
3938
3939 private void ensureColors() {
3940 int backgroundColor = getBackgroundColor();
3941 if (mPrimaryTextColor == COLOR_INVALID
3942 || mSecondaryTextColor == COLOR_INVALID
3943 || mActionBarColor == COLOR_INVALID
3944 || mTextColorsAreForBackground != backgroundColor) {
3945 mTextColorsAreForBackground = backgroundColor;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003946 if (mForegroundColor == COLOR_INVALID || !isColorized()) {
3947 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3948 backgroundColor);
3949 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3950 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003951 if (backgroundColor != COLOR_DEFAULT
3952 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3953 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3954 mPrimaryTextColor, backgroundColor, 4.5);
3955 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3956 mSecondaryTextColor, backgroundColor, 4.5);
3957 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07003958 } else {
3959 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
3960 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
3961 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
3962 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07003963 // We only respect the given colors if worst case Black or White still has
3964 // contrast
3965 boolean backgroundLight = backLum > textLum
3966 && satisfiesTextContrast(backgroundColor, Color.BLACK)
3967 || backLum <= textLum
3968 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003969 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07003970 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003971 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3972 mForegroundColor,
3973 backgroundColor,
3974 true /* findFG */,
3975 4.5f);
3976 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003977 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003978 } else {
3979 mSecondaryTextColor =
3980 NotificationColorUtil.findContrastColorAgainstDark(
3981 mForegroundColor,
3982 backgroundColor,
3983 true /* findFG */,
3984 4.5f);
3985 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003986 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003987 }
3988 } else {
3989 mPrimaryTextColor = mForegroundColor;
3990 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003991 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
3992 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003993 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
3994 backgroundColor) < 4.5f) {
3995 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07003996 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003997 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3998 mSecondaryTextColor,
3999 backgroundColor,
4000 true /* findFG */,
4001 4.5f);
4002 } else {
4003 mSecondaryTextColor
4004 = NotificationColorUtil.findContrastColorAgainstDark(
4005 mSecondaryTextColor,
4006 backgroundColor,
4007 true /* findFG */,
4008 4.5f);
4009 }
4010 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004011 mSecondaryTextColor, backgroundLight
4012 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4013 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004014 }
4015 }
4016 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004017 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4018 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004019 }
4020 }
4021
4022 private void updateBackgroundColor(RemoteViews contentView) {
4023 if (isColorized()) {
4024 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4025 getBackgroundColor());
4026 } else {
4027 // Clear it!
4028 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4029 0);
4030 }
4031 }
4032
Selim Cinek860b6da2015-12-16 19:02:19 -08004033 /**
4034 * @param remoteView the remote view to update the minheight in
4035 * @param hasMinHeight does it have a mimHeight
4036 * @hide
4037 */
4038 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4039 int minHeight = 0;
4040 if (hasMinHeight) {
4041 // we need to set the minHeight of the notification
4042 minHeight = mContext.getResources().getDimensionPixelSize(
4043 com.android.internal.R.dimen.notification_min_content_height);
4044 }
4045 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4046 }
4047
Selim Cinek29603462015-11-17 19:04:39 -08004048 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004049 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4050 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4051 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4052 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004053 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004054 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004055 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004056 contentView.setProgressBackgroundTintList(
4057 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4058 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004059 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004060 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004061 contentView.setProgressTintList(R.id.progress, colorStateList);
4062 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004063 }
Selim Cinek29603462015-11-17 19:04:39 -08004064 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004065 } else {
4066 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004067 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004068 }
Joe Onorato561d3852010-11-20 18:09:34 -08004069 }
4070
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004071 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07004072 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4073 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4074 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004075 if (mN.mLargeIcon != null) {
4076 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4077 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4078 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004079 int endMargin = R.dimen.notification_content_picture_margin;
4080 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4081 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4082 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004083 // Bind the reply action
4084 Action action = findReplyAction();
4085 contentView.setViewVisibility(R.id.reply_icon_action, action != null
4086 ? View.VISIBLE
4087 : View.GONE);
4088
4089 if (action != null) {
4090 int contrastColor = resolveContrastColor();
4091 contentView.setDrawableParameters(R.id.reply_icon_action,
4092 true /* targetBackground */,
4093 -1,
4094 contrastColor,
4095 PorterDuff.Mode.SRC_ATOP, -1);
4096 int iconColor = NotificationColorUtil.isColorLight(contrastColor)
4097 ? Color.BLACK : Color.WHITE;
4098 contentView.setDrawableParameters(R.id.reply_icon_action,
4099 false /* targetBackground */,
4100 -1,
4101 iconColor,
4102 PorterDuff.Mode.SRC_ATOP, -1);
4103 contentView.setOnClickPendingIntent(R.id.right_icon,
4104 action.actionIntent);
4105 contentView.setOnClickPendingIntent(R.id.reply_icon_action,
4106 action.actionIntent);
4107 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
4108 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4109
4110 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004111 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004112 contentView.setViewVisibility(R.id.right_icon_container, mN.mLargeIcon != null
4113 ? View.VISIBLE
4114 : View.GONE);
4115 }
4116
4117 private Action findReplyAction() {
4118 ArrayList<Action> actions = mActions;
4119 if (mOriginalActions != null) {
4120 actions = mOriginalActions;
4121 }
4122 int numActions = actions.size();
4123 for (int i = 0; i < numActions; i++) {
4124 Action action = actions.get(i);
4125 if (hasValidRemoteInput(action)) {
4126 return action;
4127 }
4128 }
4129 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004130 }
4131
Adrian Roos487374f2017-01-11 15:48:14 -08004132 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4133 bindSmallIcon(contentView, ambient);
4134 bindHeaderAppName(contentView, ambient);
4135 if (!ambient) {
4136 // Ambient view does not have these
4137 bindHeaderText(contentView);
4138 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004139 bindProfileBadge(contentView);
4140 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004141 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004142 }
4143
4144 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004145 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08004146 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004147 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004148 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004149 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004150 }
4151
Selim Cinek99104832017-01-25 14:47:33 -08004152 /**
4153 * @return the color that is used as the first primary highlight color. This is applied
4154 * in several places like the action buttons or the app name in the header.
4155 */
4156 private int getPrimaryHighlightColor() {
4157 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4158 }
4159
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004160 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4161 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004162 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004163 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004164 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4165 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4166 contentView.setLong(R.id.chronometer, "setBase",
4167 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4168 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004169 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004170 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004171 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004172 } else {
4173 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4174 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004175 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004176 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004177 } else {
4178 // We still want a time to be set but gone, such that we can show and hide it
4179 // on demand in case it's a child notification without anything in the header
4180 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004181 }
4182 }
4183
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004184 private void bindHeaderText(RemoteViews contentView) {
4185 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4186 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004187 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004188 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004189 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004190 if (headerText == null
4191 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4192 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4193 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4194 }
4195 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004196 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004197 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004198 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004199 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4200 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004201 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004202 }
4203 }
4204
Adrian Rooseba05822016-04-22 17:09:27 -07004205 /**
4206 * @hide
4207 */
4208 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004209 CharSequence name = null;
4210 final PackageManager pm = mContext.getPackageManager();
4211 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4212 // only system packages which lump together a bunch of unrelated stuff
4213 // may substitute a different name to make the purpose of the
4214 // notification more clear. the correct package label should always
4215 // be accessible via SystemUI.
4216 final String pkg = mContext.getPackageName();
4217 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4218 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4219 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4220 name = subName;
4221 } else {
4222 Log.w(TAG, "warning: pkg "
4223 + pkg + " attempting to substitute app name '" + subName
4224 + "' without holding perm "
4225 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4226 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004227 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004228 if (TextUtils.isEmpty(name)) {
4229 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4230 }
4231 if (TextUtils.isEmpty(name)) {
4232 // still nothing?
4233 return null;
4234 }
4235
4236 return String.valueOf(name);
4237 }
Adrian Roos487374f2017-01-11 15:48:14 -08004238 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004239 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004240 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004241 setTextViewColorPrimary(contentView, R.id.app_name_text);
4242 } else {
4243 contentView.setTextColor(R.id.app_name_text,
4244 ambient ? resolveAmbientColor() : resolveContrastColor());
4245 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004246 }
4247
Adrian Roos487374f2017-01-11 15:48:14 -08004248 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004249 if (mN.mSmallIcon == null && mN.icon != 0) {
4250 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4251 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004252 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004253 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4254 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004255 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004256 }
4257
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004258 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004259 * @return true if the built notification will show the time or the chronometer; false
4260 * otherwise
4261 */
4262 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004263 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004264 }
4265
Christoph Studerfe718432014-09-01 18:21:18 +02004266 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004267 // actions_container is only reset when there are no actions to avoid focus issues with
4268 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004269 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004270 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004271
4272 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4273 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4274
4275 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4276 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4277 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4278 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004279
4280 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004281 }
4282
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004283 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004284 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004285 }
4286
Adrian Roos70d7aa32017-01-11 15:39:06 -08004287 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4288 StandardTemplateParams p) {
4289 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004290
Christoph Studerfe718432014-09-01 18:21:18 +02004291 resetStandardTemplateWithActions(big);
4292
Adrian Roose458aa82015-12-08 16:17:19 -08004293 boolean validRemoteInput = false;
4294
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004295 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004296 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004297 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004298 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004299 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004300 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004301 if (p.ambient) {
4302 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004303 } else if (isColorized()) {
4304 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4305 } else {
4306 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4307 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004308 }
Adrian Roosf852a422016-06-03 13:33:43 -07004309 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4310 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004311 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004312 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004313 Action action = mActions.get(i);
4314 validRemoteInput |= hasValidRemoteInput(action);
4315
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004316 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004317 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004318 big.addView(R.id.actions, button);
4319 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004320 } else {
4321 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004322 }
Adrian Roose458aa82015-12-08 16:17:19 -08004323
4324 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004325 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004326 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4327 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4328 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004329 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004330
4331 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4332 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4333 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004334 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004335
4336 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4337 big.setViewVisibility(
4338 R.id.notification_material_reply_text_3, View.VISIBLE);
4339 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004340 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004341 }
4342 }
4343 }
4344
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004345 return big;
4346 }
4347
Adrian Roose458aa82015-12-08 16:17:19 -08004348 private boolean hasValidRemoteInput(Action action) {
4349 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4350 // Weird actions
4351 return false;
4352 }
4353
4354 RemoteInput[] remoteInputs = action.getRemoteInputs();
4355 if (remoteInputs == null) {
4356 return false;
4357 }
4358
4359 for (RemoteInput r : remoteInputs) {
4360 CharSequence[] choices = r.getChoices();
4361 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4362 return true;
4363 }
4364 }
4365 return false;
4366 }
4367
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004368 /**
4369 * Construct a RemoteViews for the final 1U notification layout. In order:
4370 * 1. Custom contentView from the caller
4371 * 2. Style's proposed content view
4372 * 3. Standard template view
4373 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004374 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004375 return createContentView(false /* increasedheight */ );
4376 }
4377
4378 /**
4379 * Construct a RemoteViews for the smaller content view.
4380 *
4381 * @param increasedHeight true if this layout be created with an increased height. Some
4382 * styles may support showing more then just that basic 1U size
4383 * and the system may decide to render important notifications
4384 * slightly bigger even when collapsed.
4385 *
4386 * @hide
4387 */
4388 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004389 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004390 return mN.contentView;
4391 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004392 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004393 if (styleView != null) {
4394 return styleView;
4395 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004396 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004397 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004398 }
4399
Selim Cineka7679b62017-05-10 16:33:25 -07004400 private boolean useExistingRemoteView() {
4401 return mStyle == null || (!mStyle.displayCustomViewInline()
4402 && !mRebuildStyledRemoteViews);
4403 }
4404
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004405 /**
4406 * Construct a RemoteViews for the final big notification layout.
4407 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004408 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004409 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004410 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004411 return mN.bigContentView;
4412 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004413 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004414 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004415 } else if (mActions.size() != 0) {
4416 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004417 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004418 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004419 return result;
4420 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004421
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004422 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004423 * Construct a RemoteViews for the final notification header only. This will not be
4424 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004425 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004426 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004427 * @hide
4428 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004429 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004430 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4431 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004432 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004433 ambient ? R.layout.notification_template_ambient_header
4434 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004435 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004436 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004437 if (colorized != null) {
4438 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4439 } else {
4440 mN.extras.remove(EXTRA_COLORIZED);
4441 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004442 return header;
4443 }
4444
Adrian Roos487374f2017-01-11 15:48:14 -08004445 /**
4446 * Construct a RemoteViews for the ambient version of the notification.
4447 *
4448 * @hide
4449 */
4450 public RemoteViews makeAmbientNotification() {
4451 RemoteViews ambient = applyStandardTemplateWithActions(
4452 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004453 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004454 return ambient;
4455 }
4456
Selim Cinek29603462015-11-17 19:04:39 -08004457 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004458 if (result != null) {
4459 result.setViewVisibility(R.id.text_line_1, View.GONE);
4460 }
Selim Cinek29603462015-11-17 19:04:39 -08004461 }
4462
Selim Cinek6743c0b2017-01-18 18:24:01 -08004463 /**
4464 * Adapt the Notification header if this view is used as an expanded view.
4465 *
4466 * @hide
4467 */
4468 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004469 if (result != null) {
4470 result.setBoolean(R.id.notification_header, "setExpanded", true);
4471 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004472 }
4473
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004474 /**
4475 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004476 *
4477 * @param increasedHeight true if this layout be created with an increased height. Some
4478 * styles may support showing more then just that basic 1U size
4479 * and the system may decide to render important notifications
4480 * slightly bigger even when collapsed.
4481 *
4482 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004483 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004484 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004485 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004486 return mN.headsUpContentView;
4487 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004488 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4489 if (styleView != null) {
4490 return styleView;
4491 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004492 } else if (mActions.size() == 0) {
4493 return null;
4494 }
4495
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004496 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004497 }
4498
Selim Cinek624c02db2015-12-14 21:00:02 -08004499 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004500 * Construct a RemoteViews for the final heads-up notification layout.
4501 */
4502 public RemoteViews createHeadsUpContentView() {
4503 return createHeadsUpContentView(false /* useIncreasedHeight */);
4504 }
4505
4506 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004507 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4508 *
4509 * @hide
4510 */
4511 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004512 return makePublicView(false /* ambient */);
4513 }
4514
4515 /**
4516 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4517 *
4518 * @hide
4519 */
4520 public RemoteViews makePublicAmbientNotification() {
4521 return makePublicView(true /* ambient */);
4522 }
4523
4524 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004525 if (mN.publicVersion != null) {
4526 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004527 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004528 }
4529 Bundle savedBundle = mN.extras;
4530 Style style = mStyle;
4531 mStyle = null;
4532 Icon largeIcon = mN.mLargeIcon;
4533 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004534 Bitmap largeIconLegacy = mN.largeIcon;
4535 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004536 ArrayList<Action> actions = mActions;
4537 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004538 Bundle publicExtras = new Bundle();
4539 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4540 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4541 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4542 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004543 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4544 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004545 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004546 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004547 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004548 final RemoteViews view = ambient ? makeAmbientNotification()
4549 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004550 mN.extras = savedBundle;
4551 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004552 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004553 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004554 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004555 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004556 }
4557
Selim Cinek6743c0b2017-01-18 18:24:01 -08004558 /**
4559 * Construct a content view for the display when low - priority
4560 *
4561 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4562 * a new subtext is created consisting of the content of the
4563 * notification.
4564 * @hide
4565 */
4566 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4567 int color = mN.color;
4568 mN.color = COLOR_DEFAULT;
4569 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4570 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4571 CharSequence newSummary = createSummaryText();
4572 if (!TextUtils.isEmpty(newSummary)) {
4573 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4574 }
4575 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004576
Adrian Roos6f6e1592017-05-02 16:22:53 -07004577 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004578 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004579 if (summary != null) {
4580 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4581 } else {
4582 mN.extras.remove(EXTRA_SUB_TEXT);
4583 }
4584 mN.color = color;
4585 return header;
4586 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004587
Selim Cinek6743c0b2017-01-18 18:24:01 -08004588 private CharSequence createSummaryText() {
4589 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4590 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4591 return titleText;
4592 }
4593 SpannableStringBuilder summary = new SpannableStringBuilder();
4594 if (titleText == null) {
4595 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4596 }
4597 BidiFormatter bidi = BidiFormatter.getInstance();
4598 if (titleText != null) {
4599 summary.append(bidi.unicodeWrap(titleText));
4600 }
4601 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4602 if (titleText != null && contentText != null) {
4603 summary.append(bidi.unicodeWrap(mContext.getText(
4604 R.string.notification_header_divider_symbol_with_spaces)));
4605 }
4606 if (contentText != null) {
4607 summary.append(bidi.unicodeWrap(contentText));
4608 }
4609 return summary;
4610 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004611
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004612 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004613 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004614 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004615 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004616 emphazisedMode ? getEmphasizedActionLayoutResource()
4617 : tombstone ? getActionTombstoneLayoutResource()
4618 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004619 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004620 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004621 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004622 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004623 if (action.mRemoteInputs != null) {
4624 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4625 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004626 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004627 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004628 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004629 int bgColor;
4630 if (isColorized()) {
4631 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4632 } else {
4633 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4634 : R.color.notification_action_list_dark);
4635 }
Selim Cinek981962e2016-07-20 20:41:58 -07004636 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004637 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004638 CharSequence title = action.title;
4639 ColorStateList[] outResultColor = null;
4640 if (isLegacy()) {
4641 title = clearColorSpans(title);
4642 } else {
4643 outResultColor = new ColorStateList[1];
4644 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4645 }
4646 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004647 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004648 if (outResultColor != null && outResultColor[0] != null) {
4649 // We need to set the text color as well since changing a text to uppercase
4650 // clears its spans.
4651 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004652 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004653 button.setTextColor(R.id.action0,resolveContrastColor());
4654 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004655 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004656 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004657 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004658 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004659 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004660 button.setTextColor(R.id.action0,
4661 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004662 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004663 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004664 return button;
4665 }
4666
Joe Onoratocb109a02011-01-18 17:57:41 -08004667 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004668 * Clears all color spans of a text
4669 * @param charSequence the input text
4670 * @return the same text but without color spans
4671 */
4672 private CharSequence clearColorSpans(CharSequence charSequence) {
4673 if (charSequence instanceof Spanned) {
4674 Spanned ss = (Spanned) charSequence;
4675 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4676 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4677 for (Object span : spans) {
4678 Object resultSpan = span;
4679 if (resultSpan instanceof CharacterStyle) {
4680 resultSpan = ((CharacterStyle) span).getUnderlying();
4681 }
4682 if (resultSpan instanceof TextAppearanceSpan) {
4683 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4684 if (originalSpan.getTextColor() != null) {
4685 resultSpan = new TextAppearanceSpan(
4686 originalSpan.getFamily(),
4687 originalSpan.getTextStyle(),
4688 originalSpan.getTextSize(),
4689 null,
4690 originalSpan.getLinkTextColor());
4691 }
4692 } else if (resultSpan instanceof ForegroundColorSpan
4693 || (resultSpan instanceof BackgroundColorSpan)) {
4694 continue;
4695 } else {
4696 resultSpan = span;
4697 }
4698 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4699 ss.getSpanFlags(span));
4700 }
4701 return builder;
4702 }
4703 return charSequence;
4704 }
4705
4706 /**
4707 * Ensures contrast on color spans against a background color. also returns the color of the
4708 * text if a span was found that spans over the whole text.
4709 *
4710 * @param charSequence the charSequence on which the spans are
4711 * @param background the background color to ensure the contrast against
4712 * @param outResultColor an array in which a color will be returned as the first element if
4713 * there exists a full length color span.
4714 * @return the contrasted charSequence
4715 */
4716 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4717 ColorStateList[] outResultColor) {
4718 if (charSequence instanceof Spanned) {
4719 Spanned ss = (Spanned) charSequence;
4720 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4721 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4722 for (Object span : spans) {
4723 Object resultSpan = span;
4724 int spanStart = ss.getSpanStart(span);
4725 int spanEnd = ss.getSpanEnd(span);
4726 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4727 if (resultSpan instanceof CharacterStyle) {
4728 resultSpan = ((CharacterStyle) span).getUnderlying();
4729 }
4730 if (resultSpan instanceof TextAppearanceSpan) {
4731 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4732 ColorStateList textColor = originalSpan.getTextColor();
4733 if (textColor != null) {
4734 int[] colors = textColor.getColors();
4735 int[] newColors = new int[colors.length];
4736 for (int i = 0; i < newColors.length; i++) {
4737 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004738 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004739 }
4740 textColor = new ColorStateList(textColor.getStates().clone(),
4741 newColors);
4742 resultSpan = new TextAppearanceSpan(
4743 originalSpan.getFamily(),
4744 originalSpan.getTextStyle(),
4745 originalSpan.getTextSize(),
4746 textColor,
4747 originalSpan.getLinkTextColor());
4748 if (fullLength) {
4749 outResultColor[0] = new ColorStateList(
4750 textColor.getStates().clone(), newColors);
4751 }
4752 }
4753 } else if (resultSpan instanceof ForegroundColorSpan) {
4754 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4755 int foregroundColor = originalSpan.getForegroundColor();
4756 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004757 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004758 resultSpan = new ForegroundColorSpan(foregroundColor);
4759 if (fullLength) {
4760 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4761 }
4762 } else {
4763 resultSpan = span;
4764 }
4765
4766 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4767 }
4768 return builder;
4769 }
4770 return charSequence;
4771 }
4772
4773 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004774 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004775 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004776 */
4777 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004778 if (!mIsLegacyInitialized) {
4779 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4780 < Build.VERSION_CODES.LOLLIPOP;
4781 mIsLegacyInitialized = true;
4782 }
4783 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004784 }
4785
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004786 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004787 return processLegacyText(charSequence, false /* ambient */);
4788 }
4789
4790 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4791 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4792 boolean wantLightText = ambient;
4793 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004794 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004795 } else {
4796 return charSequence;
4797 }
4798 }
4799
Dan Sandler26e81cf2014-05-06 10:01:27 -04004800 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004801 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004802 */
Adrian Roos487374f2017-01-11 15:48:14 -08004803 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4804 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004805 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004806 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004807 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004808 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004809 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004810
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004811 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004812 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004813 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004814 }
4815
Dan Sandler26e81cf2014-05-06 10:01:27 -04004816 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004817 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004818 * if it's grayscale).
4819 */
4820 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004821 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4822 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004823 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004824 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004825 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004826 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004827 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004828 }
4829
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004830 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004831 if (mN.color != COLOR_DEFAULT) {
4832 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004833 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004834 }
4835
Adrian Roos4ff3b122016-02-01 12:26:13 -08004836 int resolveContrastColor() {
4837 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4838 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004839 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004840
Selim Cinekac5f0272017-05-02 16:05:41 -07004841 int color;
4842 int background = mBackgroundColorHint;
4843 if (mBackgroundColorHint == COLOR_INVALID) {
4844 background = mContext.getColor(
4845 com.android.internal.R.color.notification_material_background_color);
4846 }
4847 if (mN.color == COLOR_DEFAULT) {
4848 ensureColors();
4849 color = mSecondaryTextColor;
4850 } else {
4851 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004852 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004853 }
4854 if (Color.alpha(color) < 255) {
4855 // alpha doesn't go well for color filters, so let's blend it manually
4856 color = NotificationColorUtil.compositeColors(color, background);
4857 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004858 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004859 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004860 }
4861
Adrian Roos487374f2017-01-11 15:48:14 -08004862 int resolveAmbientColor() {
4863 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4864 return mCachedAmbientColor;
4865 }
4866 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4867
4868 mCachedAmbientColorIsFor = mN.color;
4869 return mCachedAmbientColor = contrasted;
4870 }
4871
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004872 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004873 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004874 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004875 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004876 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004877 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004878 mN.actions = new Action[mActions.size()];
4879 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004880 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004881 if (!mPersonList.isEmpty()) {
4882 mN.extras.putStringArray(EXTRA_PEOPLE,
4883 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004884 }
Selim Cinek247fa012016-02-18 09:50:48 -08004885 if (mN.bigContentView != null || mN.contentView != null
4886 || mN.headsUpContentView != null) {
4887 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4888 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004889 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004890 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004891
Julia Reynolds3b848122016-02-26 10:45:32 -05004892 /**
4893 * Creates a Builder from an existing notification so further changes can be made.
4894 * @param context The context for your application / activity.
4895 * @param n The notification to create a Builder from.
4896 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004897 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004898 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004899 ApplicationInfo applicationInfo = n.extras.getParcelable(
4900 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004901 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004902 if (applicationInfo != null) {
4903 try {
4904 builderContext = context.createApplicationContext(applicationInfo,
4905 Context.CONTEXT_RESTRICTED);
4906 } catch (NameNotFoundException e) {
4907 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4908 builderContext = context; // try with our context
4909 }
4910 } else {
4911 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004912 }
4913
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004914 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004915 }
4916
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004917 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004918 * @deprecated Use {@link #build()} instead.
4919 */
4920 @Deprecated
4921 public Notification getNotification() {
4922 return build();
4923 }
4924
4925 /**
4926 * Combine all of the options that have been set and return a new {@link Notification}
4927 * object.
4928 */
4929 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004930 // first, add any extras from the calling code
4931 if (mUserExtras != null) {
4932 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004933 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004934
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004935 mN.creationTime = System.currentTimeMillis();
4936
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004937 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004938 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004939
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004940 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004941
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004942 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004943 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004944 }
Adrian Roos5081c0d2016-02-26 16:04:19 -08004945 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004946 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004947 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004948 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004949 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4950 mN.contentView.getSequenceNumber());
4951 }
4952 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004953 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004954 if (mN.bigContentView != null) {
4955 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4956 mN.bigContentView.getSequenceNumber());
4957 }
4958 }
4959 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004960 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004961 if (mN.headsUpContentView != null) {
4962 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4963 mN.headsUpContentView.getSequenceNumber());
4964 }
4965 }
4966 }
4967
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004968 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4969 mN.flags |= FLAG_SHOW_LIGHTS;
4970 }
4971
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004972 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004973 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004974
4975 /**
4976 * Apply this Builder to an existing {@link Notification} object.
4977 *
4978 * @hide
4979 */
4980 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004981 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004982 return n;
4983 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004984
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004985 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004986 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04004987 * change. Also removes extenders on low ram devices, as
4988 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08004989 *
4990 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4991 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004992 * @hide
4993 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04004994 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004995 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004996
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004997 // Only strip views for known Styles because we won't know how to
4998 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04004999 if (!isLowRam
5000 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005001 && getNotificationStyleClass(templateClass) == null) {
5002 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005003 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005004
5005 // Only strip unmodified BuilderRemoteViews.
5006 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005007 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005008 n.contentView.getSequenceNumber();
5009 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005010 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005011 n.bigContentView.getSequenceNumber();
5012 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005013 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005014 n.headsUpContentView.getSequenceNumber();
5015
5016 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005017 if (!isLowRam
5018 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005019 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005020 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005021
5022 Notification clone = n.clone();
5023 if (stripContentView) {
5024 clone.contentView = null;
5025 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5026 }
5027 if (stripBigContentView) {
5028 clone.bigContentView = null;
5029 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5030 }
5031 if (stripHeadsUpContentView) {
5032 clone.headsUpContentView = null;
5033 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5034 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005035 if (isLowRam) {
5036 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5037 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5038 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5039 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005040 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005041 }
5042
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005043 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005044 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005045 }
5046
5047 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005048 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005049 }
5050
5051 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005052 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005053 }
5054
5055 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005056 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005057 }
5058
5059 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005060 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005061 }
5062
Adrian Roosc1a80b02016-04-05 14:54:55 -07005063 private int getMessagingLayoutResource() {
5064 return R.layout.notification_template_material_messaging;
5065 }
5066
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005067 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005068 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005069 }
5070
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005071 private int getEmphasizedActionLayoutResource() {
5072 return R.layout.notification_material_action_emphasized;
5073 }
5074
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005075 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005076 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005077 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005078
5079 private int getBackgroundColor() {
5080 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005081 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005082 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005083 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5084 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005085 }
5086 }
5087
5088 private boolean isColorized() {
5089 return mN.isColorized();
5090 }
Selim Cinek99104832017-01-25 14:47:33 -08005091
Anthony Chenad4d1582017-04-10 16:07:58 -07005092 private boolean shouldTintActionButtons() {
5093 return mTintActionButtons;
5094 }
5095
Selim Cinek99104832017-01-25 14:47:33 -08005096 private boolean textColorsNeedInversion() {
5097 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5098 return false;
5099 }
5100 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5101 return targetSdkVersion > Build.VERSION_CODES.M
5102 && targetSdkVersion < Build.VERSION_CODES.O;
5103 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005104
5105 /**
5106 * Set a color palette to be used as the background and textColors
5107 *
5108 * @param backgroundColor the color to be used as the background
5109 * @param foregroundColor the color to be used as the foreground
5110 *
5111 * @hide
5112 */
5113 public void setColorPalette(int backgroundColor, int foregroundColor) {
5114 mBackgroundColor = backgroundColor;
5115 mForegroundColor = foregroundColor;
5116 mTextColorsAreForBackground = COLOR_INVALID;
5117 ensureColors();
5118 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005119
5120 /**
5121 * Sets the background color for this notification to be a different one then the default.
5122 * This is mainly used to calculate contrast and won't necessarily be applied to the
5123 * background.
5124 *
5125 * @hide
5126 */
5127 public void setBackgroundColorHint(int backgroundColor) {
5128 mBackgroundColorHint = backgroundColor;
5129 }
Selim Cineka7679b62017-05-10 16:33:25 -07005130
5131
5132 /**
5133 * Forces all styled remoteViews to be built from scratch and not use any cached
5134 * RemoteViews.
5135 * This is needed for legacy apps that are baking in their remoteviews into the
5136 * notification.
5137 *
5138 * @hide
5139 */
5140 public void setRebuildStyledRemoteViews(boolean rebuild) {
5141 mRebuildStyledRemoteViews = rebuild;
5142 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005143 }
5144
5145 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005146 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005147 */
Selim Cinek22714f12017-04-13 16:23:53 -07005148 private boolean isForegroundService() {
5149 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005150 }
5151
5152 /**
Selim Cinek99104832017-01-25 14:47:33 -08005153 * @return whether this notification has a media session attached
5154 * @hide
5155 */
5156 public boolean hasMediaSession() {
5157 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5158 }
5159
5160 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005161 * @return the style class of this notification
5162 * @hide
5163 */
5164 public Class<? extends Notification.Style> getNotificationStyle() {
5165 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5166
5167 if (!TextUtils.isEmpty(templateClass)) {
5168 return Notification.getNotificationStyleClass(templateClass);
5169 }
5170 return null;
5171 }
5172
5173 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005174 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005175 *
5176 * @hide
5177 */
5178 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005179 if (isColorizedMedia()) {
5180 return true;
5181 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005182 return extras.getBoolean(EXTRA_COLORIZED)
5183 && (hasColorizedPermission() || isForegroundService());
5184 }
5185
5186 /**
5187 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5188 * permission. The permission is checked when a notification is enqueued.
5189 */
5190 private boolean hasColorizedPermission() {
5191 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005192 }
5193
5194 /**
5195 * @return true if this notification is colorized and it is a media notification
5196 *
5197 * @hide
5198 */
5199 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005200 Class<? extends Style> style = getNotificationStyle();
5201 if (MediaStyle.class.equals(style)) {
5202 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5203 if ((colorized == null || colorized) && hasMediaSession()) {
5204 return true;
5205 }
5206 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5207 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5208 return true;
5209 }
5210 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005211 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005212 }
5213
Selim Cinek0847acd2017-04-24 19:48:29 -07005214
5215 /**
5216 * @return true if this is a media notification
5217 *
5218 * @hide
5219 */
5220 public boolean isMediaNotification() {
5221 Class<? extends Style> style = getNotificationStyle();
5222 if (MediaStyle.class.equals(style)) {
5223 return true;
5224 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5225 return true;
5226 }
5227 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005228 }
5229
Selim Cinek279fa862016-06-14 10:57:25 -07005230 private boolean hasLargeIcon() {
5231 return mLargeIcon != null || largeIcon != null;
5232 }
5233
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005234 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005235 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005236 * @hide
5237 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005238 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005239 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5240 }
5241
5242 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005243 * @return true if the notification will show a chronometer; false otherwise
5244 * @hide
5245 */
5246 public boolean showsChronometer() {
5247 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5248 }
5249
5250 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005251 * @hide
5252 */
5253 @SystemApi
5254 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5255 Class<? extends Style>[] classes = new Class[] {
5256 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5257 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5258 MessagingStyle.class };
5259 for (Class<? extends Style> innerClass : classes) {
5260 if (templateClass.equals(innerClass.getName())) {
5261 return innerClass;
5262 }
5263 }
5264 return null;
5265 }
5266
5267 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005268 * An object that can apply a rich notification style to a {@link Notification.Builder}
5269 * object.
5270 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005271 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005272 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005273
5274 /**
5275 * @hide
5276 */
5277 protected CharSequence mSummaryText = null;
5278
5279 /**
5280 * @hide
5281 */
5282 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005283
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005284 protected Builder mBuilder;
5285
Chris Wrend6297db2012-05-03 16:20:13 -04005286 /**
5287 * Overrides ContentTitle in the big form of the template.
5288 * This defaults to the value passed to setContentTitle().
5289 */
5290 protected void internalSetBigContentTitle(CharSequence title) {
5291 mBigContentTitle = title;
5292 }
5293
5294 /**
5295 * Set the first line of text after the detail section in the big form of the template.
5296 */
5297 protected void internalSetSummaryText(CharSequence cs) {
5298 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005299 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005300 }
5301
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005302 public void setBuilder(Builder builder) {
5303 if (mBuilder != builder) {
5304 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005305 if (mBuilder != null) {
5306 mBuilder.setStyle(this);
5307 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005308 }
5309 }
5310
Chris Wrend6297db2012-05-03 16:20:13 -04005311 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005312 if (mBuilder == null) {
5313 throw new IllegalArgumentException("Style requires a valid Builder object");
5314 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005315 }
Chris Wrend6297db2012-05-03 16:20:13 -04005316
5317 protected RemoteViews getStandardView(int layoutId) {
5318 checkBuilder();
5319
Christoph Studer4600f9b2014-07-22 22:44:43 +02005320 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005321 CharSequence oldBuilderContentTitle =
5322 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005323 if (mBigContentTitle != null) {
5324 mBuilder.setContentTitle(mBigContentTitle);
5325 }
5326
Chris Wrend6297db2012-05-03 16:20:13 -04005327 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5328
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005329 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005330
Chris Wrend6297db2012-05-03 16:20:13 -04005331 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5332 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005333 } else {
5334 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005335 }
5336
Chris Wrend6297db2012-05-03 16:20:13 -04005337 return contentView;
5338 }
5339
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005340 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005341 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005342 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005343 *
5344 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005345 * @hide
5346 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005347 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005348 return null;
5349 }
5350
5351 /**
5352 * Construct a Style-specific RemoteViews for the final big notification layout.
5353 * @hide
5354 */
5355 public RemoteViews makeBigContentView() {
5356 return null;
5357 }
5358
5359 /**
5360 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005361 *
5362 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005363 * @hide
5364 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005365 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005366 return null;
5367 }
5368
5369 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005370 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005371 * @hide
5372 */
5373 public void addExtras(Bundle extras) {
5374 if (mSummaryTextSet) {
5375 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5376 }
5377 if (mBigContentTitle != null) {
5378 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5379 }
Chris Wren91ad5632013-06-05 15:05:57 -04005380 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005381 }
5382
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005383 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005384 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005385 * @hide
5386 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005387 protected void restoreFromExtras(Bundle extras) {
5388 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5389 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5390 mSummaryTextSet = true;
5391 }
5392 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5393 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5394 }
5395 }
5396
5397
5398 /**
5399 * @hide
5400 */
5401 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005402 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005403 return wip;
5404 }
5405
Daniel Sandler0ec46202015-06-24 01:27:05 -04005406 /**
5407 * @hide
5408 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005409 public void purgeResources() {}
5410
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005411 /**
5412 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5413 * attached to.
5414 *
5415 * @return the fully constructed Notification.
5416 */
5417 public Notification build() {
5418 checkBuilder();
5419 return mBuilder.build();
5420 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005421
5422 /**
5423 * @hide
5424 * @return true if the style positions the progress bar on the second line; false if the
5425 * style hides the progress bar
5426 */
5427 protected boolean hasProgress() {
5428 return true;
5429 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005430
5431 /**
5432 * @hide
5433 * @return Whether we should put the summary be put into the notification header
5434 */
5435 public boolean hasSummaryInHeader() {
5436 return true;
5437 }
Selim Cinek593610c2016-02-16 18:42:57 -08005438
5439 /**
5440 * @hide
5441 * @return Whether custom content views are displayed inline in the style
5442 */
5443 public boolean displayCustomViewInline() {
5444 return false;
5445 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005446 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005447
5448 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005449 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005450 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005451 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005452 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005453 * Notification notif = new Notification.Builder(mContext)
5454 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5455 * .setContentText(subject)
5456 * .setSmallIcon(R.drawable.new_post)
5457 * .setLargeIcon(aBitmap)
5458 * .setStyle(new Notification.BigPictureStyle()
5459 * .bigPicture(aBigBitmap))
5460 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005461 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005462 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005463 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005464 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005465 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005466 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005467 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005468 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005469
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005470 public BigPictureStyle() {
5471 }
5472
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005473 /**
5474 * @deprecated use {@code BigPictureStyle()}.
5475 */
5476 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005477 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005478 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005479 }
5480
Chris Wrend6297db2012-05-03 16:20:13 -04005481 /**
5482 * Overrides ContentTitle in the big form of the template.
5483 * This defaults to the value passed to setContentTitle().
5484 */
5485 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005486 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005487 return this;
5488 }
5489
5490 /**
5491 * Set the first line of text after the detail section in the big form of the template.
5492 */
5493 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005494 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005495 return this;
5496 }
5497
Chris Wren0bd664d2012-08-01 13:56:56 -04005498 /**
5499 * Provide the bitmap to be used as the payload for the BigPicture notification.
5500 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005501 public BigPictureStyle bigPicture(Bitmap b) {
5502 mPicture = b;
5503 return this;
5504 }
5505
Chris Wren3745a3d2012-05-22 15:11:52 -04005506 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005507 * Override the large icon when the big notification is shown.
5508 */
5509 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005510 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5511 }
5512
5513 /**
5514 * Override the large icon when the big notification is shown.
5515 */
5516 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005517 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005518 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005519 return this;
5520 }
5521
Riley Andrews0394a0c2015-11-03 23:36:52 -08005522 /** @hide */
5523 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5524
Daniel Sandler0ec46202015-06-24 01:27:05 -04005525 /**
5526 * @hide
5527 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005528 @Override
5529 public void purgeResources() {
5530 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005531 if (mPicture != null &&
5532 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005533 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005534 mPicture = mPicture.createAshmemBitmap();
5535 }
5536 if (mBigLargeIcon != null) {
5537 mBigLargeIcon.convertToAshmem();
5538 }
5539 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005540
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005541 /**
5542 * @hide
5543 */
5544 public RemoteViews makeBigContentView() {
5545 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005546 // This covers the following cases:
5547 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005548 // mN.mLargeIcon
5549 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005550 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005551 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005552 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005553 oldLargeIcon = mBuilder.mN.mLargeIcon;
5554 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005555 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5556 // replacement if the other one is null. Because we're restoring these legacy icons
5557 // for old listeners, this is in general non-null.
5558 largeIconLegacy = mBuilder.mN.largeIcon;
5559 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005560 }
5561
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005562 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005563 if (mSummaryTextSet) {
5564 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005565 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005566 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005567 }
Selim Cinek279fa862016-06-14 10:57:25 -07005568 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005569
Christoph Studer5c510ee2014-12-15 16:32:27 +01005570 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005571 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005572 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005573 }
5574
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005575 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005576 return contentView;
5577 }
5578
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005579 /**
5580 * @hide
5581 */
5582 public void addExtras(Bundle extras) {
5583 super.addExtras(extras);
5584
5585 if (mBigLargeIconSet) {
5586 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5587 }
5588 extras.putParcelable(EXTRA_PICTURE, mPicture);
5589 }
5590
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005591 /**
5592 * @hide
5593 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005594 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005595 protected void restoreFromExtras(Bundle extras) {
5596 super.restoreFromExtras(extras);
5597
5598 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005599 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005600 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005601 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005602 mPicture = extras.getParcelable(EXTRA_PICTURE);
5603 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005604
5605 /**
5606 * @hide
5607 */
5608 @Override
5609 public boolean hasSummaryInHeader() {
5610 return false;
5611 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005612 }
5613
5614 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005615 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005616 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005617 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005618 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005619 * Notification notif = new Notification.Builder(mContext)
5620 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5621 * .setContentText(subject)
5622 * .setSmallIcon(R.drawable.new_mail)
5623 * .setLargeIcon(aBitmap)
5624 * .setStyle(new Notification.BigTextStyle()
5625 * .bigText(aVeryLongString))
5626 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005627 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005628 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005629 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005630 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005631 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005632
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005633 private CharSequence mBigText;
5634
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005635 public BigTextStyle() {
5636 }
5637
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005638 /**
5639 * @deprecated use {@code BigTextStyle()}.
5640 */
5641 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005642 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005643 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005644 }
5645
Chris Wrend6297db2012-05-03 16:20:13 -04005646 /**
5647 * Overrides ContentTitle in the big form of the template.
5648 * This defaults to the value passed to setContentTitle().
5649 */
5650 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005651 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005652 return this;
5653 }
5654
5655 /**
5656 * Set the first line of text after the detail section in the big form of the template.
5657 */
5658 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005659 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005660 return this;
5661 }
5662
Chris Wren0bd664d2012-08-01 13:56:56 -04005663 /**
5664 * Provide the longer text to be displayed in the big form of the
5665 * template in place of the content text.
5666 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005667 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005668 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005669 return this;
5670 }
5671
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005672 /**
5673 * @hide
5674 */
5675 public void addExtras(Bundle extras) {
5676 super.addExtras(extras);
5677
Christoph Studer4600f9b2014-07-22 22:44:43 +02005678 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5679 }
5680
5681 /**
5682 * @hide
5683 */
5684 @Override
5685 protected void restoreFromExtras(Bundle extras) {
5686 super.restoreFromExtras(extras);
5687
5688 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005689 }
5690
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005691 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005692 * @param increasedHeight true if this layout be created with an increased height.
5693 *
5694 * @hide
5695 */
5696 @Override
5697 public RemoteViews makeContentView(boolean increasedHeight) {
5698 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005699 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005700 mBuilder.mActions = new ArrayList<>();
5701 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005702 mBuilder.mActions = mBuilder.mOriginalActions;
5703 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005704 return remoteViews;
5705 }
5706 return super.makeContentView(increasedHeight);
5707 }
5708
5709 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005710 * @hide
5711 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005712 @Override
5713 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5714 if (increasedHeight && mBuilder.mActions.size() > 0) {
5715 return makeBigContentView();
5716 }
5717 return super.makeHeadsUpContentView(increasedHeight);
5718 }
5719
5720 /**
5721 * @hide
5722 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005723 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005724
5725 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005726 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005727 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005728
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005729 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005730
Selim Cinek75998782016-04-26 10:39:17 -07005731 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005732
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005733 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005734 if (TextUtils.isEmpty(bigTextText)) {
5735 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5736 // experience
5737 bigTextText = mBuilder.processLegacyText(text);
5738 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005739 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005740
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005741 return contentView;
5742 }
5743
Adrian Roosb1f427c2016-05-26 12:27:15 -07005744 static void applyBigTextContentView(Builder builder,
5745 RemoteViews contentView, CharSequence bigTextText) {
5746 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005747 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005748 contentView.setViewVisibility(R.id.big_text,
5749 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005750 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005751 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005752 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005753
5754 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005755 * Helper class for generating large-format notifications that include multiple back-and-forth
5756 * messages of varying types between any number of people.
5757 *
5758 * <br>
5759 * If the platform does not provide large-format notifications, this method has no effect. The
5760 * user will always see the normal notification view.
5761 * <br>
5762 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5763 * so:
5764 * <pre class="prettyprint">
5765 *
5766 * Notification noti = new Notification.Builder()
5767 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5768 * .setContentText(subject)
5769 * .setSmallIcon(R.drawable.new_message)
5770 * .setLargeIcon(aBitmap)
5771 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5772 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5773 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5774 * .build();
5775 * </pre>
5776 */
5777 public static class MessagingStyle extends Style {
5778
5779 /**
5780 * The maximum number of messages that will be retained in the Notification itself (the
5781 * number displayed is up to the platform).
5782 */
5783 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5784
5785 CharSequence mUserDisplayName;
5786 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005787 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005788 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005789
5790 MessagingStyle() {
5791 }
5792
5793 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005794 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5795 * user before the posting app reposts the notification with those messages after they've
5796 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005797 * {@link #addMessage(Notification.MessagingStyle.Message)}
5798 */
Alex Hillsfd590442016-10-07 09:52:44 -04005799 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005800 mUserDisplayName = userDisplayName;
5801 }
5802
5803 /**
5804 * Returns the name to be displayed for any replies sent by the user
5805 */
5806 public CharSequence getUserDisplayName() {
5807 return mUserDisplayName;
5808 }
5809
5810 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005811 * Sets the title to be displayed on this conversation. This should only be used for
5812 * group messaging and left unset for one-on-one conversations.
5813 * @param conversationTitle
5814 * @return this object for method chaining.
5815 */
5816 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5817 mConversationTitle = conversationTitle;
5818 return this;
5819 }
5820
5821 /**
5822 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5823 * should be for one-on-one conversations
5824 */
5825 public CharSequence getConversationTitle() {
5826 return mConversationTitle;
5827 }
5828
5829 /**
5830 * Adds a message for display by this notification. Convenience call for a simple
5831 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5832 * @param text A {@link CharSequence} to be displayed as the message content
5833 * @param timestamp Time at which the message arrived
5834 * @param sender A {@link CharSequence} to be used for displaying the name of the
5835 * sender. Should be <code>null</code> for messages by the current user, in which case
5836 * the platform will insert {@link #getUserDisplayName()}.
5837 * Should be unique amongst all individuals in the conversation, and should be
5838 * consistent during re-posts of the notification.
5839 *
5840 * @see Message#Message(CharSequence, long, CharSequence)
5841 *
5842 * @return this object for method chaining
5843 */
5844 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005845 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005846 }
5847
5848 /**
5849 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005850 *
5851 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5852 * the newest last.
5853 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005854 * @param message The {@link Message} to be displayed
5855 * @return this object for method chaining
5856 */
5857 public MessagingStyle addMessage(Message message) {
5858 mMessages.add(message);
5859 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5860 mMessages.remove(0);
5861 }
5862 return this;
5863 }
5864
5865 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005866 * Adds a {@link Message} for historic context in this notification.
5867 *
5868 * <p>Messages should be added as historic if they are not the main subject of the
5869 * notification but may give context to a conversation. The system may choose to present
5870 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5871 *
5872 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5873 * the newest last.
5874 *
5875 * @param message The historic {@link Message} to be added
5876 * @return this object for method chaining
5877 */
5878 public MessagingStyle addHistoricMessage(Message message) {
5879 mHistoricMessages.add(message);
5880 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5881 mHistoricMessages.remove(0);
5882 }
5883 return this;
5884 }
5885
5886 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005887 * Gets the list of {@code Message} objects that represent the notification
5888 */
5889 public List<Message> getMessages() {
5890 return mMessages;
5891 }
5892
5893 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005894 * Gets the list of historic {@code Message}s in the notification.
5895 */
5896 public List<Message> getHistoricMessages() {
5897 return mHistoricMessages;
5898 }
5899
5900 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005901 * @hide
5902 */
5903 @Override
5904 public void addExtras(Bundle extras) {
5905 super.addExtras(extras);
5906 if (mUserDisplayName != null) {
5907 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5908 }
5909 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005910 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005911 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005912 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5913 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005914 }
Adrian Roos437cd562017-01-18 15:47:03 -08005915 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5916 Message.getBundleArrayForMessages(mHistoricMessages));
5917 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005918
5919 fixTitleAndTextExtras(extras);
5920 }
5921
5922 private void fixTitleAndTextExtras(Bundle extras) {
5923 Message m = findLatestIncomingMessage();
5924 CharSequence text = (m == null) ? null : m.mText;
5925 CharSequence sender = m == null ? null
5926 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5927 CharSequence title;
5928 if (!TextUtils.isEmpty(mConversationTitle)) {
5929 if (!TextUtils.isEmpty(sender)) {
5930 BidiFormatter bidi = BidiFormatter.getInstance();
5931 title = mBuilder.mContext.getString(
5932 com.android.internal.R.string.notification_messaging_title_template,
5933 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5934 } else {
5935 title = mConversationTitle;
5936 }
5937 } else {
5938 title = sender;
5939 }
5940
5941 if (title != null) {
5942 extras.putCharSequence(EXTRA_TITLE, title);
5943 }
5944 if (text != null) {
5945 extras.putCharSequence(EXTRA_TEXT, text);
5946 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005947 }
5948
5949 /**
5950 * @hide
5951 */
5952 @Override
5953 protected void restoreFromExtras(Bundle extras) {
5954 super.restoreFromExtras(extras);
5955
5956 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005957 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005958 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5959 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005960 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5961 if (messages != null && messages instanceof Parcelable[]) {
5962 mMessages = Message.getMessagesFromBundleArray(messages);
5963 }
5964 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5965 if (histMessages != null && histMessages instanceof Parcelable[]) {
5966 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005967 }
5968 }
5969
5970 /**
5971 * @hide
5972 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005973 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005974 public RemoteViews makeContentView(boolean increasedHeight) {
5975 if (!increasedHeight) {
5976 Message m = findLatestIncomingMessage();
5977 CharSequence title = mConversationTitle != null
5978 ? mConversationTitle
5979 : (m == null) ? null : m.mSender;
5980 CharSequence text = (m == null)
5981 ? null
5982 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005983
Selim Cinek7d1009b2017-01-25 15:28:28 -08005984 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5985 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5986 } else {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005987 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005988 mBuilder.mActions = new ArrayList<>();
5989 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005990 mBuilder.mActions = mBuilder.mOriginalActions;
5991 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005992 return remoteViews;
5993 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005994 }
5995
5996 private Message findLatestIncomingMessage() {
5997 for (int i = mMessages.size() - 1; i >= 0; i--) {
5998 Message m = mMessages.get(i);
5999 // Incoming messages have a non-empty sender.
6000 if (!TextUtils.isEmpty(m.mSender)) {
6001 return m;
6002 }
6003 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006004 if (!mMessages.isEmpty()) {
6005 // No incoming messages, fall back to outgoing message
6006 return mMessages.get(mMessages.size() - 1);
6007 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006008 return null;
6009 }
6010
6011 /**
6012 * @hide
6013 */
6014 @Override
6015 public RemoteViews makeBigContentView() {
6016 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
6017 ? super.mBigContentTitle
6018 : mConversationTitle;
6019 boolean hasTitle = !TextUtils.isEmpty(title);
6020
Adrian Roosfeafa052016-06-01 17:09:45 -07006021 if (mMessages.size() == 1) {
6022 // Special case for a single message: Use the big text style
6023 // so the collapsed and expanded versions match nicely.
6024 CharSequence bigTitle;
6025 CharSequence text;
6026 if (hasTitle) {
6027 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006028 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07006029 } else {
6030 bigTitle = mMessages.get(0).mSender;
6031 text = mMessages.get(0).mText;
6032 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006033 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
6034 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006035 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07006036 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
6037 return contentView;
6038 }
6039
Adrian Roos48d746a2016-04-12 14:57:28 -07006040 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006041 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006042 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006043
6044 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
6045 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
6046
6047 // Make sure all rows are gone in case we reuse a view.
6048 for (int rowId : rowIds) {
6049 contentView.setViewVisibility(rowId, View.GONE);
6050 }
6051
6052 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006053 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
6054 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006055 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07006056 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006057
Adrian Roosfeafa052016-06-01 17:09:45 -07006058 int contractedChildId = View.NO_ID;
6059 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08006060 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
6061 - (rowIds.length - mMessages.size()));
6062 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
6063 Message m = mHistoricMessages.get(firstHistoricMessage + i);
6064 int rowId = rowIds[i];
6065
Selim Cinek7b9605b2017-01-19 17:36:00 -08006066 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08006067
6068 if (contractedMessage == m) {
6069 contractedChildId = rowId;
6070 }
6071
6072 i++;
6073 }
6074
Adrian Roosc1a80b02016-04-05 14:54:55 -07006075 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
6076 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
6077 Message m = mMessages.get(firstMessage + i);
6078 int rowId = rowIds[i];
6079
6080 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006081 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
6082 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006083
Adrian Roosfeafa052016-06-01 17:09:45 -07006084 if (contractedMessage == m) {
6085 contractedChildId = rowId;
6086 }
6087
Adrian Roosc1a80b02016-04-05 14:54:55 -07006088 i++;
6089 }
Adrian Roos437cd562017-01-18 15:47:03 -08006090 // Clear the remaining views for reapply. Ensures that historic message views can
6091 // reliably be identified as being GONE and having non-null text.
6092 while (i < rowIds.length) {
6093 int rowId = rowIds[i];
6094 contentView.setTextViewText(rowId, null);
6095 i++;
6096 }
6097
Adrian Roosfeafa052016-06-01 17:09:45 -07006098 // Record this here to allow transformation between the contracted and expanded views.
6099 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
6100 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04006101 return contentView;
6102 }
6103
Selim Cinek7b9605b2017-01-19 17:36:00 -08006104 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006105 BidiFormatter bidi = BidiFormatter.getInstance();
6106 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08006107 boolean colorize = builder.isColorized();
Anthony Chenad4d1582017-04-10 16:07:58 -07006108 TextAppearanceSpan colorSpan;
6109 CharSequence messageName;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006110 if (TextUtils.isEmpty(m.mSender)) {
6111 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
6112 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006113 makeFontColorSpan(colorize
6114 ? builder.getPrimaryTextColor()
6115 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006116 0 /* flags */);
6117 } else {
6118 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006119 makeFontColorSpan(colorize
6120 ? builder.getPrimaryTextColor()
6121 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006122 0 /* flags */);
6123 }
6124 CharSequence text = m.mText == null ? "" : m.mText;
6125 sb.append(" ").append(bidi.unicodeWrap(text));
6126 return sb;
6127 }
6128
Adrian Roosdedd1df2016-04-26 16:38:47 -07006129 /**
6130 * @hide
6131 */
6132 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006133 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6134 if (increasedHeight) {
6135 return makeBigContentView();
6136 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07006137 Message m = findLatestIncomingMessage();
6138 CharSequence title = mConversationTitle != null
6139 ? mConversationTitle
6140 : (m == null) ? null : m.mSender;
6141 CharSequence text = (m == null)
6142 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08006143 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006144
6145 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006146 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07006147 }
6148
Adrian Roosc1a80b02016-04-05 14:54:55 -07006149 private static TextAppearanceSpan makeFontColorSpan(int color) {
6150 return new TextAppearanceSpan(null, 0, 0,
6151 ColorStateList.valueOf(color), null);
6152 }
6153
Alex Hillsd9b04d92016-04-11 16:38:16 -04006154 public static final class Message {
6155
6156 static final String KEY_TEXT = "text";
6157 static final String KEY_TIMESTAMP = "time";
6158 static final String KEY_SENDER = "sender";
6159 static final String KEY_DATA_MIME_TYPE = "type";
6160 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006161 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006162
6163 private final CharSequence mText;
6164 private final long mTimestamp;
6165 private final CharSequence mSender;
6166
Shane Brennan5a871862017-03-11 13:14:17 -08006167 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006168 private String mDataMimeType;
6169 private Uri mDataUri;
6170
6171 /**
6172 * Constructor
6173 * @param text A {@link CharSequence} to be displayed as the message content
6174 * @param timestamp Time at which the message arrived
6175 * @param sender A {@link CharSequence} to be used for displaying the name of the
6176 * sender. Should be <code>null</code> for messages by the current user, in which case
6177 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6178 * Should be unique amongst all individuals in the conversation, and should be
6179 * consistent during re-posts of the notification.
6180 */
6181 public Message(CharSequence text, long timestamp, CharSequence sender){
6182 mText = text;
6183 mTimestamp = timestamp;
6184 mSender = sender;
6185 }
6186
6187 /**
6188 * Sets a binary blob of data and an associated MIME type for a message. In the case
6189 * where the platform doesn't support the MIME type, the original text provided in the
6190 * constructor will be used.
6191 * @param dataMimeType The MIME type of the content. See
6192 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6193 * types on Android and Android Wear.
6194 * @param dataUri The uri containing the content whose type is given by the MIME type.
6195 * <p class="note">
6196 * <ol>
6197 * <li>Notification Listeners including the System UI need permission to access the
6198 * data the Uri points to. The recommended ways to do this are:</li>
6199 * <li>Store the data in your own ContentProvider, making sure that other apps have
6200 * the correct permission to access your provider. The preferred mechanism for
6201 * providing access is to use per-URI permissions which are temporary and only
6202 * grant access to the receiving application. An easy way to create a
6203 * ContentProvider like this is to use the FileProvider helper class.</li>
6204 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6205 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6206 * also store non-media types (see MediaStore.Files for more info). Files can be
6207 * inserted into the MediaStore using scanFile() after which a content:// style
6208 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6209 * Note that once added to the system MediaStore the content is accessible to any
6210 * app on the device.</li>
6211 * </ol>
6212 * @return this object for method chaining
6213 */
6214 public Message setData(String dataMimeType, Uri dataUri) {
6215 mDataMimeType = dataMimeType;
6216 mDataUri = dataUri;
6217 return this;
6218 }
6219
Alex Hillsfc737de2016-03-23 17:33:02 -04006220 /**
6221 * Get the text to be used for this message, or the fallback text if a type and content
6222 * Uri have been set
6223 */
6224 public CharSequence getText() {
6225 return mText;
6226 }
6227
6228 /**
6229 * Get the time at which this message arrived
6230 */
6231 public long getTimestamp() {
6232 return mTimestamp;
6233 }
6234
6235 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006236 * Get the extras Bundle for this message.
6237 */
6238 public Bundle getExtras() {
6239 return mExtras;
6240 }
6241
6242 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006243 * Get the text used to display the contact's name in the messaging experience
6244 */
6245 public CharSequence getSender() {
6246 return mSender;
6247 }
6248
6249 /**
6250 * Get the MIME type of the data pointed to by the Uri
6251 */
6252 public String getDataMimeType() {
6253 return mDataMimeType;
6254 }
6255
6256 /**
6257 * Get the the Uri pointing to the content of the message. Can be null, in which case
6258 * {@see #getText()} is used.
6259 */
6260 public Uri getDataUri() {
6261 return mDataUri;
6262 }
6263
Alex Hillsd9b04d92016-04-11 16:38:16 -04006264 private Bundle toBundle() {
6265 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006266 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006267 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006268 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006269 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006270 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006271 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006272 }
6273 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006274 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006275 }
6276 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006277 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006278 }
Shane Brennan5a871862017-03-11 13:14:17 -08006279 if (mExtras != null) {
6280 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6281 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006282 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006283 }
6284
Alex Hillsd9b04d92016-04-11 16:38:16 -04006285 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6286 Bundle[] bundles = new Bundle[messages.size()];
6287 final int N = messages.size();
6288 for (int i = 0; i < N; i++) {
6289 bundles[i] = messages.get(i).toBundle();
6290 }
6291 return bundles;
6292 }
6293
Adrian Roosdedd1df2016-04-26 16:38:47 -07006294 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006295 List<Message> messages = new ArrayList<>(bundles.length);
6296 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006297 if (bundles[i] instanceof Bundle) {
6298 Message message = getMessageFromBundle((Bundle)bundles[i]);
6299 if (message != null) {
6300 messages.add(message);
6301 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006302 }
6303 }
6304 return messages;
6305 }
6306
6307 static Message getMessageFromBundle(Bundle bundle) {
6308 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006309 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006310 return null;
6311 } else {
6312 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6313 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6314 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6315 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006316 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6317 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006318 }
Shane Brennan5a871862017-03-11 13:14:17 -08006319 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6320 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6321 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006322 return message;
6323 }
6324 } catch (ClassCastException e) {
6325 return null;
6326 }
6327 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006328 }
6329 }
6330
6331 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006332 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006333 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006334 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006335 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006336 * Notification notif = new Notification.Builder(mContext)
6337 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6338 * .setContentText(subject)
6339 * .setSmallIcon(R.drawable.new_mail)
6340 * .setLargeIcon(aBitmap)
6341 * .setStyle(new Notification.InboxStyle()
6342 * .addLine(str1)
6343 * .addLine(str2)
6344 * .setContentTitle(&quot;&quot;)
6345 * .setSummaryText(&quot;+3 more&quot;))
6346 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006347 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006348 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006349 * @see Notification#bigContentView
6350 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006351 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006352 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6353
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006354 public InboxStyle() {
6355 }
6356
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006357 /**
6358 * @deprecated use {@code InboxStyle()}.
6359 */
6360 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006361 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006362 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006363 }
6364
Chris Wrend6297db2012-05-03 16:20:13 -04006365 /**
6366 * Overrides ContentTitle in the big form of the template.
6367 * This defaults to the value passed to setContentTitle().
6368 */
6369 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006370 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006371 return this;
6372 }
6373
6374 /**
6375 * Set the first line of text after the detail section in the big form of the template.
6376 */
6377 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006378 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006379 return this;
6380 }
6381
Chris Wren0bd664d2012-08-01 13:56:56 -04006382 /**
6383 * Append a line to the digest section of the Inbox notification.
6384 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006385 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006386 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006387 return this;
6388 }
6389
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006390 /**
6391 * @hide
6392 */
6393 public void addExtras(Bundle extras) {
6394 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006395
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006396 CharSequence[] a = new CharSequence[mTexts.size()];
6397 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6398 }
6399
Christoph Studer4600f9b2014-07-22 22:44:43 +02006400 /**
6401 * @hide
6402 */
6403 @Override
6404 protected void restoreFromExtras(Bundle extras) {
6405 super.restoreFromExtras(extras);
6406
6407 mTexts.clear();
6408 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6409 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6410 }
6411 }
6412
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006413 /**
6414 * @hide
6415 */
6416 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006417 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006418 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006419 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6420 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006421
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006422 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006423
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006424 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006425
Chris Wrend6297db2012-05-03 16:20:13 -04006426 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 -04006427 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006428
Chris Wren4ed80d52012-05-17 09:30:03 -04006429 // Make sure all rows are gone in case we reuse a view.
6430 for (int rowId : rowIds) {
6431 contentView.setViewVisibility(rowId, View.GONE);
6432 }
6433
Daniel Sandler879c5e02012-04-17 16:46:51 -04006434 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006435 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6436 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006437 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006438 int onlyViewId = 0;
6439 int maxRows = rowIds.length;
6440 if (mBuilder.mActions.size() > 0) {
6441 maxRows--;
6442 }
6443 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006444 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006445 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006446 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006447 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006448 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006449 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006450 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006451 if (first) {
6452 onlyViewId = rowIds[i];
6453 } else {
6454 onlyViewId = 0;
6455 }
Selim Cinek247fa012016-02-18 09:50:48 -08006456 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006457 }
6458 i++;
6459 }
Selim Cinek07c80172016-04-21 16:40:47 -07006460 if (onlyViewId != 0) {
6461 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6462 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6463 R.dimen.notification_text_margin_top);
6464 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6465 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006466
Daniel Sandler879c5e02012-04-17 16:46:51 -04006467 return contentView;
6468 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006469
Selim Cinek247fa012016-02-18 09:50:48 -08006470 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006471 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006472 if (first) {
6473 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6474 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6475 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006476 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006477 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006478 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006479 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006480 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006481 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006482 }
Dan Sandler842dd772014-05-15 09:36:47 -04006483
6484 /**
6485 * Notification style for media playback notifications.
6486 *
6487 * In the expanded form, {@link Notification#bigContentView}, up to 5
6488 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006489 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006490 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6491 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6492 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006493 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006494 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6495 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006496 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006497 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006498 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006499 * Notifications created with MediaStyle will have their category set to
6500 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6501 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006502 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006503 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6504 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006505 * the System UI can identify this as a notification representing an active media session
6506 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6507 *
Selim Cinek99104832017-01-25 14:47:33 -08006508 * <p>
6509 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6510 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6511 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6512 * <p>
6513 *
Dan Sandler842dd772014-05-15 09:36:47 -04006514 * To use this style with your Notification, feed it to
6515 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6516 * <pre class="prettyprint">
6517 * Notification noti = new Notification.Builder()
6518 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006519 * .setContentTitle(&quot;Track title&quot;)
6520 * .setContentText(&quot;Artist - Album&quot;)
6521 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006522 * .setStyle(<b>new Notification.MediaStyle()</b>
6523 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006524 * .build();
6525 * </pre>
6526 *
6527 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006528 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006529 */
6530 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006531 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006532 static final int MAX_MEDIA_BUTTONS = 5;
6533
6534 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006535 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006536
6537 public MediaStyle() {
6538 }
6539
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006540 /**
6541 * @deprecated use {@code MediaStyle()}.
6542 */
6543 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006544 public MediaStyle(Builder builder) {
6545 setBuilder(builder);
6546 }
6547
6548 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006549 * 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 -04006550 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006551 *
6552 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006553 */
6554 public MediaStyle setShowActionsInCompactView(int...actions) {
6555 mActionsToShowInCompact = actions;
6556 return this;
6557 }
6558
6559 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006560 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6561 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006562 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006563 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006564 mToken = token;
6565 return this;
6566 }
6567
Christoph Studer4600f9b2014-07-22 22:44:43 +02006568 /**
6569 * @hide
6570 */
Dan Sandler842dd772014-05-15 09:36:47 -04006571 @Override
6572 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006573 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006574 if (wip.category == null) {
6575 wip.category = Notification.CATEGORY_TRANSPORT;
6576 }
Dan Sandler842dd772014-05-15 09:36:47 -04006577 return wip;
6578 }
6579
Christoph Studer4600f9b2014-07-22 22:44:43 +02006580 /**
6581 * @hide
6582 */
6583 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006584 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006585 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006586 }
6587
6588 /**
6589 * @hide
6590 */
6591 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006592 public RemoteViews makeBigContentView() {
6593 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006594 }
6595
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006596 /**
6597 * @hide
6598 */
6599 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006600 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006601 RemoteViews expanded = makeMediaBigContentView();
6602 return expanded != null ? expanded : makeMediaContentView();
6603 }
6604
Dan Sandler842dd772014-05-15 09:36:47 -04006605 /** @hide */
6606 @Override
6607 public void addExtras(Bundle extras) {
6608 super.addExtras(extras);
6609
6610 if (mToken != null) {
6611 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6612 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006613 if (mActionsToShowInCompact != null) {
6614 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6615 }
Dan Sandler842dd772014-05-15 09:36:47 -04006616 }
6617
Christoph Studer4600f9b2014-07-22 22:44:43 +02006618 /**
6619 * @hide
6620 */
6621 @Override
6622 protected void restoreFromExtras(Bundle extras) {
6623 super.restoreFromExtras(extras);
6624
6625 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6626 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6627 }
6628 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6629 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6630 }
6631 }
6632
Selim Cinek5bf069a2015-11-10 19:14:27 -05006633 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006634 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006635 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006636 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006637 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006638
6639 // If the action buttons should not be tinted, then just use the default
6640 // notification color. Otherwise, just use the passed-in color.
6641 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6642 ? color
6643 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6644 Notification.COLOR_DEFAULT);
6645
6646 button.setDrawableParameters(R.id.action0, false, -1, tintColor,
6647 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006648 if (!tombstone) {
6649 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6650 }
6651 button.setContentDescription(R.id.action0, action.title);
6652 return button;
6653 }
6654
6655 private RemoteViews makeMediaContentView() {
6656 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006657 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006658
6659 final int numActions = mBuilder.mActions.size();
6660 final int N = mActionsToShowInCompact == null
6661 ? 0
6662 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6663 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006664 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006665 for (int i = 0; i < N; i++) {
6666 if (i >= numActions) {
6667 throw new IllegalArgumentException(String.format(
6668 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6669 i, numActions - 1));
6670 }
6671
6672 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006673 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006674 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006675 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006676 }
6677 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006678 handleImage(view);
6679 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006680 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006681 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006682 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006683 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006684 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006685 return view;
6686 }
6687
Selim Cinek99104832017-01-25 14:47:33 -08006688 private int getPrimaryHighlightColor() {
6689 return mBuilder.getPrimaryHighlightColor();
6690 }
6691
Dan Sandler842dd772014-05-15 09:36:47 -04006692 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006693 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006694 // Dont add an expanded view if there is no more content to be revealed
6695 int actionsInCompact = mActionsToShowInCompact == null
6696 ? 0
6697 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006698 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006699 return null;
6700 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006701 RemoteViews big = mBuilder.applyStandardTemplate(
6702 R.layout.notification_template_material_big_media,
6703 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006704
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006705 if (actionCount > 0) {
6706 big.removeAllViews(com.android.internal.R.id.media_actions);
6707 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006708 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006709 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006710 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006711 }
6712 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006713 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006714 return big;
6715 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006716
Selim Cinek5bf069a2015-11-10 19:14:27 -05006717 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006718 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006719 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6720 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006721 }
6722 }
6723
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006724 /**
6725 * @hide
6726 */
6727 @Override
6728 protected boolean hasProgress() {
6729 return false;
6730 }
Dan Sandler842dd772014-05-15 09:36:47 -04006731 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006732
Selim Cinek593610c2016-02-16 18:42:57 -08006733 /**
6734 * Notification style for custom views that are decorated by the system
6735 *
6736 * <p>Instead of providing a notification that is completely custom, a developer can set this
6737 * style and still obtain system decorations like the notification header with the expand
6738 * affordance and actions.
6739 *
6740 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6741 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6742 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6743 * corresponding custom views to display.
6744 *
6745 * To use this style with your Notification, feed it to
6746 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6747 * <pre class="prettyprint">
6748 * Notification noti = new Notification.Builder()
6749 * .setSmallIcon(R.drawable.ic_stat_player)
6750 * .setLargeIcon(albumArtBitmap))
6751 * .setCustomContentView(contentView);
6752 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6753 * .build();
6754 * </pre>
6755 */
6756 public static class DecoratedCustomViewStyle extends Style {
6757
6758 public DecoratedCustomViewStyle() {
6759 }
6760
Selim Cinek593610c2016-02-16 18:42:57 -08006761 /**
6762 * @hide
6763 */
6764 public boolean displayCustomViewInline() {
6765 return true;
6766 }
6767
6768 /**
6769 * @hide
6770 */
6771 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006772 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006773 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6774 }
6775
6776 /**
6777 * @hide
6778 */
6779 @Override
6780 public RemoteViews makeBigContentView() {
6781 return makeDecoratedBigContentView();
6782 }
6783
6784 /**
6785 * @hide
6786 */
6787 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006788 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006789 return makeDecoratedHeadsUpContentView();
6790 }
6791
Selim Cinek593610c2016-02-16 18:42:57 -08006792 private RemoteViews makeDecoratedHeadsUpContentView() {
6793 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6794 ? mBuilder.mN.contentView
6795 : mBuilder.mN.headsUpContentView;
6796 if (mBuilder.mActions.size() == 0) {
6797 return makeStandardTemplateWithCustomContent(headsUpContentView);
6798 }
6799 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6800 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006801 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006802 return remoteViews;
6803 }
6804
Selim Cinek593610c2016-02-16 18:42:57 -08006805 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6806 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6807 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006808 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006809 return remoteViews;
6810 }
6811
Selim Cinek593610c2016-02-16 18:42:57 -08006812 private RemoteViews makeDecoratedBigContentView() {
6813 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6814 ? mBuilder.mN.contentView
6815 : mBuilder.mN.bigContentView;
6816 if (mBuilder.mActions.size() == 0) {
6817 return makeStandardTemplateWithCustomContent(bigContentView);
6818 }
6819 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6820 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006821 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006822 return remoteViews;
6823 }
Selim Cinek247fa012016-02-18 09:50:48 -08006824
6825 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6826 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006827 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006828 // Need to clone customContent before adding, because otherwise it can no longer be
6829 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006830 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006831 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6832 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006833 }
Selim Cinek247fa012016-02-18 09:50:48 -08006834 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006835 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006836 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006837 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006838 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006839 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006840 }
Selim Cinek593610c2016-02-16 18:42:57 -08006841 }
6842
Selim Cinek03eb3b72016-02-18 10:39:45 -08006843 /**
6844 * Notification style for media custom views that are decorated by the system
6845 *
6846 * <p>Instead of providing a media notification that is completely custom, a developer can set
6847 * this style and still obtain system decorations like the notification header with the expand
6848 * affordance and actions.
6849 *
6850 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6851 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6852 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6853 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006854 * <p>
6855 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6856 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6857 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006858 * To use this style with your Notification, feed it to
6859 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6860 * <pre class="prettyprint">
6861 * Notification noti = new Notification.Builder()
6862 * .setSmallIcon(R.drawable.ic_stat_player)
6863 * .setLargeIcon(albumArtBitmap))
6864 * .setCustomContentView(contentView);
6865 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6866 * .setMediaSession(mySession))
6867 * .build();
6868 * </pre>
6869 *
6870 * @see android.app.Notification.DecoratedCustomViewStyle
6871 * @see android.app.Notification.MediaStyle
6872 */
6873 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6874
6875 public DecoratedMediaCustomViewStyle() {
6876 }
6877
Selim Cinek03eb3b72016-02-18 10:39:45 -08006878 /**
6879 * @hide
6880 */
6881 public boolean displayCustomViewInline() {
6882 return true;
6883 }
6884
6885 /**
6886 * @hide
6887 */
6888 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006889 public RemoteViews makeContentView(boolean increasedHeight) {
6890 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006891 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6892 mBuilder.mN.contentView);
6893 }
6894
6895 /**
6896 * @hide
6897 */
6898 @Override
6899 public RemoteViews makeBigContentView() {
6900 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6901 ? mBuilder.mN.bigContentView
6902 : mBuilder.mN.contentView;
6903 return makeBigContentViewWithCustomContent(customRemoteView);
6904 }
6905
6906 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6907 RemoteViews remoteViews = super.makeBigContentView();
6908 if (remoteViews != null) {
6909 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6910 customRemoteView);
6911 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006912 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006913 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6914 customRemoteView);
6915 } else {
6916 return null;
6917 }
6918 }
6919
6920 /**
6921 * @hide
6922 */
6923 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006924 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006925 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6926 ? mBuilder.mN.headsUpContentView
6927 : mBuilder.mN.contentView;
6928 return makeBigContentViewWithCustomContent(customRemoteView);
6929 }
6930
6931 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6932 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006933 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006934 // Need to clone customContent before adding, because otherwise it can no longer be
6935 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006936 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006937 remoteViews.removeAllViews(id);
6938 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006939 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006940 return remoteViews;
6941 }
6942 }
6943
Christoph Studer4600f9b2014-07-22 22:44:43 +02006944 // When adding a new Style subclass here, don't forget to update
6945 // Builder.getNotificationStyleClass.
6946
Griff Hazen61a9e862014-05-22 16:05:19 -07006947 /**
6948 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6949 * metadata or change options on a notification builder.
6950 */
6951 public interface Extender {
6952 /**
6953 * Apply this extender to a notification builder.
6954 * @param builder the builder to be modified.
6955 * @return the build object for chaining.
6956 */
6957 public Builder extend(Builder builder);
6958 }
6959
6960 /**
6961 * Helper class to add wearable extensions to notifications.
6962 * <p class="note"> See
6963 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6964 * for Android Wear</a> for more information on how to use this class.
6965 * <p>
6966 * To create a notification with wearable extensions:
6967 * <ol>
6968 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6969 * properties.
6970 * <li>Create a {@link android.app.Notification.WearableExtender}.
6971 * <li>Set wearable-specific properties using the
6972 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6973 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6974 * notification.
6975 * <li>Post the notification to the notification system with the
6976 * {@code NotificationManager.notify(...)} methods.
6977 * </ol>
6978 *
6979 * <pre class="prettyprint">
6980 * Notification notif = new Notification.Builder(mContext)
6981 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6982 * .setContentText(subject)
6983 * .setSmallIcon(R.drawable.new_mail)
6984 * .extend(new Notification.WearableExtender()
6985 * .setContentIcon(R.drawable.new_mail))
6986 * .build();
6987 * NotificationManager notificationManger =
6988 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6989 * notificationManger.notify(0, notif);</pre>
6990 *
6991 * <p>Wearable extensions can be accessed on an existing notification by using the
6992 * {@code WearableExtender(Notification)} constructor,
6993 * and then using the {@code get} methods to access values.
6994 *
6995 * <pre class="prettyprint">
6996 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6997 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006998 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006999 */
7000 public static final class WearableExtender implements Extender {
7001 /**
7002 * Sentinel value for an action index that is unset.
7003 */
7004 public static final int UNSET_ACTION_INDEX = -1;
7005
7006 /**
7007 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7008 * default sizing.
7009 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007010 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007011 * on their content.
7012 */
7013 public static final int SIZE_DEFAULT = 0;
7014
7015 /**
7016 * Size value for use with {@link #setCustomSizePreset} to show this notification
7017 * with an extra small size.
7018 * <p>This value is only applicable for custom display notifications created using
7019 * {@link #setDisplayIntent}.
7020 */
7021 public static final int SIZE_XSMALL = 1;
7022
7023 /**
7024 * Size value for use with {@link #setCustomSizePreset} to show this notification
7025 * with a small size.
7026 * <p>This value is only applicable for custom display notifications created using
7027 * {@link #setDisplayIntent}.
7028 */
7029 public static final int SIZE_SMALL = 2;
7030
7031 /**
7032 * Size value for use with {@link #setCustomSizePreset} to show this notification
7033 * with a medium size.
7034 * <p>This value is only applicable for custom display notifications created using
7035 * {@link #setDisplayIntent}.
7036 */
7037 public static final int SIZE_MEDIUM = 3;
7038
7039 /**
7040 * Size value for use with {@link #setCustomSizePreset} to show this notification
7041 * with a large size.
7042 * <p>This value is only applicable for custom display notifications created using
7043 * {@link #setDisplayIntent}.
7044 */
7045 public static final int SIZE_LARGE = 4;
7046
Griff Hazend5f11f92014-05-27 15:40:09 -07007047 /**
7048 * Size value for use with {@link #setCustomSizePreset} to show this notification
7049 * full screen.
7050 * <p>This value is only applicable for custom display notifications created using
7051 * {@link #setDisplayIntent}.
7052 */
7053 public static final int SIZE_FULL_SCREEN = 5;
7054
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007055 /**
7056 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7057 * short amount of time when this notification is displayed on the screen. This
7058 * is the default value.
7059 */
7060 public static final int SCREEN_TIMEOUT_SHORT = 0;
7061
7062 /**
7063 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7064 * for a longer amount of time when this notification is displayed on the screen.
7065 */
7066 public static final int SCREEN_TIMEOUT_LONG = -1;
7067
Griff Hazen61a9e862014-05-22 16:05:19 -07007068 /** Notification extra which contains wearable extensions */
7069 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7070
Pete Gastaf6781d2014-10-07 15:17:05 -04007071 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007072 private static final String KEY_ACTIONS = "actions";
7073 private static final String KEY_FLAGS = "flags";
7074 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7075 private static final String KEY_PAGES = "pages";
7076 private static final String KEY_BACKGROUND = "background";
7077 private static final String KEY_CONTENT_ICON = "contentIcon";
7078 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7079 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7080 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7081 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7082 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007083 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007084 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007085 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007086
7087 // Flags bitwise-ored to mFlags
7088 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7089 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7090 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7091 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007092 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007093 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007094 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007095
7096 // Default value for flags integer
7097 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7098
7099 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7100 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7101
7102 private ArrayList<Action> mActions = new ArrayList<Action>();
7103 private int mFlags = DEFAULT_FLAGS;
7104 private PendingIntent mDisplayIntent;
7105 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7106 private Bitmap mBackground;
7107 private int mContentIcon;
7108 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7109 private int mContentActionIndex = UNSET_ACTION_INDEX;
7110 private int mCustomSizePreset = SIZE_DEFAULT;
7111 private int mCustomContentHeight;
7112 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007113 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007114 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007115 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007116
7117 /**
7118 * Create a {@link android.app.Notification.WearableExtender} with default
7119 * options.
7120 */
7121 public WearableExtender() {
7122 }
7123
7124 public WearableExtender(Notification notif) {
7125 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7126 if (wearableBundle != null) {
7127 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7128 if (actions != null) {
7129 mActions.addAll(actions);
7130 }
7131
7132 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7133 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7134
7135 Notification[] pages = getNotificationArrayFromBundle(
7136 wearableBundle, KEY_PAGES);
7137 if (pages != null) {
7138 Collections.addAll(mPages, pages);
7139 }
7140
7141 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7142 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7143 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7144 DEFAULT_CONTENT_ICON_GRAVITY);
7145 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7146 UNSET_ACTION_INDEX);
7147 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7148 SIZE_DEFAULT);
7149 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7150 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007151 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007152 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007153 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007154 }
7155 }
7156
7157 /**
7158 * Apply wearable extensions to a notification that is being built. This is typically
7159 * called by the {@link android.app.Notification.Builder#extend} method of
7160 * {@link android.app.Notification.Builder}.
7161 */
7162 @Override
7163 public Notification.Builder extend(Notification.Builder builder) {
7164 Bundle wearableBundle = new Bundle();
7165
7166 if (!mActions.isEmpty()) {
7167 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7168 }
7169 if (mFlags != DEFAULT_FLAGS) {
7170 wearableBundle.putInt(KEY_FLAGS, mFlags);
7171 }
7172 if (mDisplayIntent != null) {
7173 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7174 }
7175 if (!mPages.isEmpty()) {
7176 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7177 new Notification[mPages.size()]));
7178 }
7179 if (mBackground != null) {
7180 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7181 }
7182 if (mContentIcon != 0) {
7183 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7184 }
7185 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7186 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7187 }
7188 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7189 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7190 mContentActionIndex);
7191 }
7192 if (mCustomSizePreset != SIZE_DEFAULT) {
7193 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7194 }
7195 if (mCustomContentHeight != 0) {
7196 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7197 }
7198 if (mGravity != DEFAULT_GRAVITY) {
7199 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7200 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007201 if (mHintScreenTimeout != 0) {
7202 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7203 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007204 if (mDismissalId != null) {
7205 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7206 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007207 if (mBridgeTag != null) {
7208 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7209 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007210
7211 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7212 return builder;
7213 }
7214
7215 @Override
7216 public WearableExtender clone() {
7217 WearableExtender that = new WearableExtender();
7218 that.mActions = new ArrayList<Action>(this.mActions);
7219 that.mFlags = this.mFlags;
7220 that.mDisplayIntent = this.mDisplayIntent;
7221 that.mPages = new ArrayList<Notification>(this.mPages);
7222 that.mBackground = this.mBackground;
7223 that.mContentIcon = this.mContentIcon;
7224 that.mContentIconGravity = this.mContentIconGravity;
7225 that.mContentActionIndex = this.mContentActionIndex;
7226 that.mCustomSizePreset = this.mCustomSizePreset;
7227 that.mCustomContentHeight = this.mCustomContentHeight;
7228 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007229 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007230 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007231 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007232 return that;
7233 }
7234
7235 /**
7236 * Add a wearable action to this notification.
7237 *
7238 * <p>When wearable actions are added using this method, the set of actions that
7239 * show on a wearable device splits from devices that only show actions added
7240 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7241 * of which actions display on different devices.
7242 *
7243 * @param action the action to add to this notification
7244 * @return this object for method chaining
7245 * @see android.app.Notification.Action
7246 */
7247 public WearableExtender addAction(Action action) {
7248 mActions.add(action);
7249 return this;
7250 }
7251
7252 /**
7253 * Adds wearable actions to this notification.
7254 *
7255 * <p>When wearable actions are added using this method, the set of actions that
7256 * show on a wearable device splits from devices that only show actions added
7257 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7258 * of which actions display on different devices.
7259 *
7260 * @param actions the actions to add to this notification
7261 * @return this object for method chaining
7262 * @see android.app.Notification.Action
7263 */
7264 public WearableExtender addActions(List<Action> actions) {
7265 mActions.addAll(actions);
7266 return this;
7267 }
7268
7269 /**
7270 * Clear all wearable actions present on this builder.
7271 * @return this object for method chaining.
7272 * @see #addAction
7273 */
7274 public WearableExtender clearActions() {
7275 mActions.clear();
7276 return this;
7277 }
7278
7279 /**
7280 * Get the wearable actions present on this notification.
7281 */
7282 public List<Action> getActions() {
7283 return mActions;
7284 }
7285
7286 /**
7287 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007288 * this notification. The {@link PendingIntent} provided should be for an activity.
7289 *
7290 * <pre class="prettyprint">
7291 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7292 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7293 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7294 * Notification notif = new Notification.Builder(context)
7295 * .extend(new Notification.WearableExtender()
7296 * .setDisplayIntent(displayPendingIntent)
7297 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7298 * .build();</pre>
7299 *
7300 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007301 * should have an empty task affinity. It is also recommended to use the device
7302 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007303 *
7304 * <p>Example AndroidManifest.xml entry:
7305 * <pre class="prettyprint">
7306 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7307 * android:exported=&quot;true&quot;
7308 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007309 * android:taskAffinity=&quot;&quot;
7310 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007311 *
7312 * @param intent the {@link PendingIntent} for an activity
7313 * @return this object for method chaining
7314 * @see android.app.Notification.WearableExtender#getDisplayIntent
7315 */
7316 public WearableExtender setDisplayIntent(PendingIntent intent) {
7317 mDisplayIntent = intent;
7318 return this;
7319 }
7320
7321 /**
7322 * Get the intent to launch inside of an activity view when displaying this
7323 * notification. This {@code PendingIntent} should be for an activity.
7324 */
7325 public PendingIntent getDisplayIntent() {
7326 return mDisplayIntent;
7327 }
7328
7329 /**
7330 * Add an additional page of content to display with this notification. The current
7331 * notification forms the first page, and pages added using this function form
7332 * subsequent pages. This field can be used to separate a notification into multiple
7333 * sections.
7334 *
7335 * @param page the notification to add as another page
7336 * @return this object for method chaining
7337 * @see android.app.Notification.WearableExtender#getPages
7338 */
7339 public WearableExtender addPage(Notification page) {
7340 mPages.add(page);
7341 return this;
7342 }
7343
7344 /**
7345 * Add additional pages of content to display with this notification. The current
7346 * notification forms the first page, and pages added using this function form
7347 * subsequent pages. This field can be used to separate a notification into multiple
7348 * sections.
7349 *
7350 * @param pages a list of notifications
7351 * @return this object for method chaining
7352 * @see android.app.Notification.WearableExtender#getPages
7353 */
7354 public WearableExtender addPages(List<Notification> pages) {
7355 mPages.addAll(pages);
7356 return this;
7357 }
7358
7359 /**
7360 * Clear all additional pages present on this builder.
7361 * @return this object for method chaining.
7362 * @see #addPage
7363 */
7364 public WearableExtender clearPages() {
7365 mPages.clear();
7366 return this;
7367 }
7368
7369 /**
7370 * Get the array of additional pages of content for displaying this notification. The
7371 * current notification forms the first page, and elements within this array form
7372 * subsequent pages. This field can be used to separate a notification into multiple
7373 * sections.
7374 * @return the pages for this notification
7375 */
7376 public List<Notification> getPages() {
7377 return mPages;
7378 }
7379
7380 /**
7381 * Set a background image to be displayed behind the notification content.
7382 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7383 * will work with any notification style.
7384 *
7385 * @param background the background bitmap
7386 * @return this object for method chaining
7387 * @see android.app.Notification.WearableExtender#getBackground
7388 */
7389 public WearableExtender setBackground(Bitmap background) {
7390 mBackground = background;
7391 return this;
7392 }
7393
7394 /**
7395 * Get a background image to be displayed behind the notification content.
7396 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7397 * will work with any notification style.
7398 *
7399 * @return the background image
7400 * @see android.app.Notification.WearableExtender#setBackground
7401 */
7402 public Bitmap getBackground() {
7403 return mBackground;
7404 }
7405
7406 /**
7407 * Set an icon that goes with the content of this notification.
7408 */
7409 public WearableExtender setContentIcon(int icon) {
7410 mContentIcon = icon;
7411 return this;
7412 }
7413
7414 /**
7415 * Get an icon that goes with the content of this notification.
7416 */
7417 public int getContentIcon() {
7418 return mContentIcon;
7419 }
7420
7421 /**
7422 * Set the gravity that the content icon should have within the notification display.
7423 * Supported values include {@link android.view.Gravity#START} and
7424 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7425 * @see #setContentIcon
7426 */
7427 public WearableExtender setContentIconGravity(int contentIconGravity) {
7428 mContentIconGravity = contentIconGravity;
7429 return this;
7430 }
7431
7432 /**
7433 * Get the gravity that the content icon should have within the notification display.
7434 * Supported values include {@link android.view.Gravity#START} and
7435 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7436 * @see #getContentIcon
7437 */
7438 public int getContentIconGravity() {
7439 return mContentIconGravity;
7440 }
7441
7442 /**
7443 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007444 * this notification. This action will no longer display separately from the
7445 * notification's content.
7446 *
Griff Hazenca48d352014-05-28 22:37:13 -07007447 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007448 * set, although the list of available actions comes from the main notification and not
7449 * from the child page's notification.
7450 *
7451 * @param actionIndex The index of the action to hoist onto the current notification page.
7452 * If wearable actions were added to the main notification, this index
7453 * will apply to that list, otherwise it will apply to the regular
7454 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007455 */
7456 public WearableExtender setContentAction(int actionIndex) {
7457 mContentActionIndex = actionIndex;
7458 return this;
7459 }
7460
7461 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007462 * Get the index of the notification action, if any, that was specified as being clickable
7463 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007464 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007465 *
Griff Hazenca48d352014-05-28 22:37:13 -07007466 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007467 * set, although the list of available actions comes from the main notification and not
7468 * from the child page's notification.
7469 *
7470 * <p>If wearable specific actions were added to the main notification, this index will
7471 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007472 *
7473 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007474 */
7475 public int getContentAction() {
7476 return mContentActionIndex;
7477 }
7478
7479 /**
7480 * Set the gravity that this notification should have within the available viewport space.
7481 * Supported values include {@link android.view.Gravity#TOP},
7482 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7483 * The default value is {@link android.view.Gravity#BOTTOM}.
7484 */
7485 public WearableExtender setGravity(int gravity) {
7486 mGravity = gravity;
7487 return this;
7488 }
7489
7490 /**
7491 * Get the gravity that this notification should have within the available viewport space.
7492 * Supported values include {@link android.view.Gravity#TOP},
7493 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7494 * The default value is {@link android.view.Gravity#BOTTOM}.
7495 */
7496 public int getGravity() {
7497 return mGravity;
7498 }
7499
7500 /**
7501 * Set the custom size preset for the display of this notification out of the available
7502 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7503 * {@link #SIZE_LARGE}.
7504 * <p>Some custom size presets are only applicable for custom display notifications created
7505 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7506 * documentation for the preset in question. See also
7507 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7508 */
7509 public WearableExtender setCustomSizePreset(int sizePreset) {
7510 mCustomSizePreset = sizePreset;
7511 return this;
7512 }
7513
7514 /**
7515 * Get the custom size preset for the display of this notification out of the available
7516 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7517 * {@link #SIZE_LARGE}.
7518 * <p>Some custom size presets are only applicable for custom display notifications created
7519 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7520 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7521 */
7522 public int getCustomSizePreset() {
7523 return mCustomSizePreset;
7524 }
7525
7526 /**
7527 * Set the custom height in pixels for the display of this notification's content.
7528 * <p>This option is only available for custom display notifications created
7529 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7530 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7531 * {@link #getCustomContentHeight}.
7532 */
7533 public WearableExtender setCustomContentHeight(int height) {
7534 mCustomContentHeight = height;
7535 return this;
7536 }
7537
7538 /**
7539 * Get the custom height in pixels for the display of this notification's content.
7540 * <p>This option is only available for custom display notifications created
7541 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7542 * {@link #setCustomContentHeight}.
7543 */
7544 public int getCustomContentHeight() {
7545 return mCustomContentHeight;
7546 }
7547
7548 /**
7549 * Set whether the scrolling position for the contents of this notification should start
7550 * at the bottom of the contents instead of the top when the contents are too long to
7551 * display within the screen. Default is false (start scroll at the top).
7552 */
7553 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7554 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7555 return this;
7556 }
7557
7558 /**
7559 * Get whether the scrolling position for the contents of this notification should start
7560 * at the bottom of the contents instead of the top when the contents are too long to
7561 * display within the screen. Default is false (start scroll at the top).
7562 */
7563 public boolean getStartScrollBottom() {
7564 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7565 }
7566
7567 /**
7568 * Set whether the content intent is available when the wearable device is not connected
7569 * to a companion device. The user can still trigger this intent when the wearable device
7570 * is offline, but a visual hint will indicate that the content intent may not be available.
7571 * Defaults to true.
7572 */
7573 public WearableExtender setContentIntentAvailableOffline(
7574 boolean contentIntentAvailableOffline) {
7575 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7576 return this;
7577 }
7578
7579 /**
7580 * Get whether the content intent is available when the wearable device is not connected
7581 * to a companion device. The user can still trigger this intent when the wearable device
7582 * is offline, but a visual hint will indicate that the content intent may not be available.
7583 * Defaults to true.
7584 */
7585 public boolean getContentIntentAvailableOffline() {
7586 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7587 }
7588
7589 /**
7590 * Set a hint that this notification's icon should not be displayed.
7591 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7592 * @return this object for method chaining
7593 */
7594 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7595 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7596 return this;
7597 }
7598
7599 /**
7600 * Get a hint that this notification's icon should not be displayed.
7601 * @return {@code true} if this icon should not be displayed, false otherwise.
7602 * The default value is {@code false} if this was never set.
7603 */
7604 public boolean getHintHideIcon() {
7605 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7606 }
7607
7608 /**
7609 * Set a visual hint that only the background image of this notification should be
7610 * displayed, and other semantic content should be hidden. This hint is only applicable
7611 * to sub-pages added using {@link #addPage}.
7612 */
7613 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7614 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7615 return this;
7616 }
7617
7618 /**
7619 * Get a visual hint that only the background image of this notification should be
7620 * displayed, and other semantic content should be hidden. This hint is only applicable
7621 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7622 */
7623 public boolean getHintShowBackgroundOnly() {
7624 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7625 }
7626
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007627 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007628 * Set 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 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7632 * @return this object for method chaining
7633 */
7634 public WearableExtender setHintAvoidBackgroundClipping(
7635 boolean hintAvoidBackgroundClipping) {
7636 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7637 return this;
7638 }
7639
7640 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007641 * Get a hint that this notification's background should not be clipped if possible,
7642 * and should instead be resized to fully display on the screen, retaining the aspect
7643 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007644 * @return {@code true} if it's ok if the background is clipped on the screen, false
7645 * otherwise. The default value is {@code false} if this was never set.
7646 */
7647 public boolean getHintAvoidBackgroundClipping() {
7648 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7649 }
7650
7651 /**
7652 * Set a hint that the screen should remain on for at least this duration when
7653 * this notification is displayed on the screen.
7654 * @param timeout The requested screen timeout in milliseconds. Can also be either
7655 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7656 * @return this object for method chaining
7657 */
7658 public WearableExtender setHintScreenTimeout(int timeout) {
7659 mHintScreenTimeout = timeout;
7660 return this;
7661 }
7662
7663 /**
7664 * Get the duration, in milliseconds, that the screen should remain on for
7665 * when this notification is displayed.
7666 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7667 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7668 */
7669 public int getHintScreenTimeout() {
7670 return mHintScreenTimeout;
7671 }
7672
Alex Hills9ab3a232016-04-05 14:54:56 -04007673 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007674 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7675 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7676 * qr codes, as well as other simple black-and-white tickets.
7677 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7678 * @return this object for method chaining
7679 */
7680 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7681 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7682 return this;
7683 }
7684
7685 /**
7686 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7687 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7688 * qr codes, as well as other simple black-and-white tickets.
7689 * @return {@code true} if it should be displayed in ambient, false otherwise
7690 * otherwise. The default value is {@code false} if this was never set.
7691 */
7692 public boolean getHintAmbientBigPicture() {
7693 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7694 }
7695
7696 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007697 * Set 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 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7700 * an activity and transitions should be generated, false otherwise.
7701 * @return this object for method chaining
7702 */
7703 public WearableExtender setHintContentIntentLaunchesActivity(
7704 boolean hintContentIntentLaunchesActivity) {
7705 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7706 return this;
7707 }
7708
7709 /**
7710 * Get a hint that this notification's content intent will launch an {@link Activity}
7711 * directly, telling the platform that it can generate the appropriate transitions
7712 * @return {@code true} if the content intent will launch an activity and transitions should
7713 * be generated, false otherwise. The default value is {@code false} if this was never set.
7714 */
7715 public boolean getHintContentIntentLaunchesActivity() {
7716 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7717 }
7718
Nadia Benbernou948627e2016-04-14 14:41:08 -04007719 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007720 * Sets the dismissal id for this notification. If a notification is posted with a
7721 * dismissal id, then when that notification is canceled, notifications on other wearables
7722 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007723 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007724 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007725 * @param dismissalId the dismissal id of the notification.
7726 * @return this object for method chaining
7727 */
7728 public WearableExtender setDismissalId(String dismissalId) {
7729 mDismissalId = dismissalId;
7730 return this;
7731 }
7732
7733 /**
7734 * Returns the dismissal id of the notification.
7735 * @return the dismissal id of the notification or null if it has not been set.
7736 */
7737 public String getDismissalId() {
7738 return mDismissalId;
7739 }
7740
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007741 /**
7742 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7743 * posted from a phone to provide finer-grained control on what notifications are bridged
7744 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7745 * Features to Notifications</a> for more information.
7746 * @param bridgeTag the bridge tag of the notification.
7747 * @return this object for method chaining
7748 */
7749 public WearableExtender setBridgeTag(String bridgeTag) {
7750 mBridgeTag = bridgeTag;
7751 return this;
7752 }
7753
7754 /**
7755 * Returns the bridge tag of the notification.
7756 * @return the bridge tag or null if not present.
7757 */
7758 public String getBridgeTag() {
7759 return mBridgeTag;
7760 }
7761
Griff Hazen61a9e862014-05-22 16:05:19 -07007762 private void setFlag(int mask, boolean value) {
7763 if (value) {
7764 mFlags |= mask;
7765 } else {
7766 mFlags &= ~mask;
7767 }
7768 }
7769 }
7770
7771 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007772 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7773 * with car extensions:
7774 *
7775 * <ol>
7776 * <li>Create an {@link Notification.Builder}, setting any desired
7777 * properties.
7778 * <li>Create a {@link CarExtender}.
7779 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7780 * {@link CarExtender}.
7781 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7782 * to apply the extensions to a notification.
7783 * </ol>
7784 *
7785 * <pre class="prettyprint">
7786 * Notification notification = new Notification.Builder(context)
7787 * ...
7788 * .extend(new CarExtender()
7789 * .set*(...))
7790 * .build();
7791 * </pre>
7792 *
7793 * <p>Car extensions can be accessed on an existing notification by using the
7794 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7795 * to access values.
7796 */
7797 public static final class CarExtender implements Extender {
7798 private static final String TAG = "CarExtender";
7799
7800 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7801 private static final String EXTRA_LARGE_ICON = "large_icon";
7802 private static final String EXTRA_CONVERSATION = "car_conversation";
7803 private static final String EXTRA_COLOR = "app_color";
7804
7805 private Bitmap mLargeIcon;
7806 private UnreadConversation mUnreadConversation;
7807 private int mColor = Notification.COLOR_DEFAULT;
7808
7809 /**
7810 * Create a {@link CarExtender} with default options.
7811 */
7812 public CarExtender() {
7813 }
7814
7815 /**
7816 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7817 *
7818 * @param notif The notification from which to copy options.
7819 */
7820 public CarExtender(Notification notif) {
7821 Bundle carBundle = notif.extras == null ?
7822 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7823 if (carBundle != null) {
7824 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7825 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7826
7827 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7828 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7829 }
7830 }
7831
7832 /**
7833 * Apply car extensions to a notification that is being built. This is typically called by
7834 * the {@link Notification.Builder#extend(Notification.Extender)}
7835 * method of {@link Notification.Builder}.
7836 */
7837 @Override
7838 public Notification.Builder extend(Notification.Builder builder) {
7839 Bundle carExtensions = new Bundle();
7840
7841 if (mLargeIcon != null) {
7842 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7843 }
7844 if (mColor != Notification.COLOR_DEFAULT) {
7845 carExtensions.putInt(EXTRA_COLOR, mColor);
7846 }
7847
7848 if (mUnreadConversation != null) {
7849 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7850 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7851 }
7852
7853 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7854 return builder;
7855 }
7856
7857 /**
7858 * Sets the accent color to use when Android Auto presents the notification.
7859 *
7860 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7861 * to accent the displayed notification. However, not all colors are acceptable in an
7862 * automotive setting. This method can be used to override the color provided in the
7863 * notification in such a situation.
7864 */
Tor Norbye80756e32015-03-02 09:39:27 -08007865 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007866 mColor = color;
7867 return this;
7868 }
7869
7870 /**
7871 * Gets the accent color.
7872 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007873 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007874 */
Tor Norbye80756e32015-03-02 09:39:27 -08007875 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007876 public int getColor() {
7877 return mColor;
7878 }
7879
7880 /**
7881 * Sets the large icon of the car notification.
7882 *
7883 * If no large icon is set in the extender, Android Auto will display the icon
7884 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7885 *
7886 * @param largeIcon The large icon to use in the car notification.
7887 * @return This object for method chaining.
7888 */
7889 public CarExtender setLargeIcon(Bitmap largeIcon) {
7890 mLargeIcon = largeIcon;
7891 return this;
7892 }
7893
7894 /**
7895 * Gets the large icon used in this car notification, or null if no icon has been set.
7896 *
7897 * @return The large icon for the car notification.
7898 * @see CarExtender#setLargeIcon
7899 */
7900 public Bitmap getLargeIcon() {
7901 return mLargeIcon;
7902 }
7903
7904 /**
7905 * Sets the unread conversation in a message notification.
7906 *
7907 * @param unreadConversation The unread part of the conversation this notification conveys.
7908 * @return This object for method chaining.
7909 */
7910 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7911 mUnreadConversation = unreadConversation;
7912 return this;
7913 }
7914
7915 /**
7916 * Returns the unread conversation conveyed by this notification.
7917 * @see #setUnreadConversation(UnreadConversation)
7918 */
7919 public UnreadConversation getUnreadConversation() {
7920 return mUnreadConversation;
7921 }
7922
7923 /**
7924 * A class which holds the unread messages from a conversation.
7925 */
7926 public static class UnreadConversation {
7927 private static final String KEY_AUTHOR = "author";
7928 private static final String KEY_TEXT = "text";
7929 private static final String KEY_MESSAGES = "messages";
7930 private static final String KEY_REMOTE_INPUT = "remote_input";
7931 private static final String KEY_ON_REPLY = "on_reply";
7932 private static final String KEY_ON_READ = "on_read";
7933 private static final String KEY_PARTICIPANTS = "participants";
7934 private static final String KEY_TIMESTAMP = "timestamp";
7935
7936 private final String[] mMessages;
7937 private final RemoteInput mRemoteInput;
7938 private final PendingIntent mReplyPendingIntent;
7939 private final PendingIntent mReadPendingIntent;
7940 private final String[] mParticipants;
7941 private final long mLatestTimestamp;
7942
7943 UnreadConversation(String[] messages, RemoteInput remoteInput,
7944 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7945 String[] participants, long latestTimestamp) {
7946 mMessages = messages;
7947 mRemoteInput = remoteInput;
7948 mReadPendingIntent = readPendingIntent;
7949 mReplyPendingIntent = replyPendingIntent;
7950 mParticipants = participants;
7951 mLatestTimestamp = latestTimestamp;
7952 }
7953
7954 /**
7955 * Gets the list of messages conveyed by this notification.
7956 */
7957 public String[] getMessages() {
7958 return mMessages;
7959 }
7960
7961 /**
7962 * Gets the remote input that will be used to convey the response to a message list, or
7963 * null if no such remote input exists.
7964 */
7965 public RemoteInput getRemoteInput() {
7966 return mRemoteInput;
7967 }
7968
7969 /**
7970 * Gets the pending intent that will be triggered when the user replies to this
7971 * notification.
7972 */
7973 public PendingIntent getReplyPendingIntent() {
7974 return mReplyPendingIntent;
7975 }
7976
7977 /**
7978 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7979 * in this object's message list.
7980 */
7981 public PendingIntent getReadPendingIntent() {
7982 return mReadPendingIntent;
7983 }
7984
7985 /**
7986 * Gets the participants in the conversation.
7987 */
7988 public String[] getParticipants() {
7989 return mParticipants;
7990 }
7991
7992 /**
7993 * Gets the firs participant in the conversation.
7994 */
7995 public String getParticipant() {
7996 return mParticipants.length > 0 ? mParticipants[0] : null;
7997 }
7998
7999 /**
8000 * Gets the timestamp of the conversation.
8001 */
8002 public long getLatestTimestamp() {
8003 return mLatestTimestamp;
8004 }
8005
8006 Bundle getBundleForUnreadConversation() {
8007 Bundle b = new Bundle();
8008 String author = null;
8009 if (mParticipants != null && mParticipants.length > 1) {
8010 author = mParticipants[0];
8011 }
8012 Parcelable[] messages = new Parcelable[mMessages.length];
8013 for (int i = 0; i < messages.length; i++) {
8014 Bundle m = new Bundle();
8015 m.putString(KEY_TEXT, mMessages[i]);
8016 m.putString(KEY_AUTHOR, author);
8017 messages[i] = m;
8018 }
8019 b.putParcelableArray(KEY_MESSAGES, messages);
8020 if (mRemoteInput != null) {
8021 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8022 }
8023 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8024 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8025 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8026 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8027 return b;
8028 }
8029
8030 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8031 if (b == null) {
8032 return null;
8033 }
8034 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8035 String[] messages = null;
8036 if (parcelableMessages != null) {
8037 String[] tmp = new String[parcelableMessages.length];
8038 boolean success = true;
8039 for (int i = 0; i < tmp.length; i++) {
8040 if (!(parcelableMessages[i] instanceof Bundle)) {
8041 success = false;
8042 break;
8043 }
8044 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8045 if (tmp[i] == null) {
8046 success = false;
8047 break;
8048 }
8049 }
8050 if (success) {
8051 messages = tmp;
8052 } else {
8053 return null;
8054 }
8055 }
8056
8057 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8058 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8059
8060 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8061
8062 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8063 if (participants == null || participants.length != 1) {
8064 return null;
8065 }
8066
8067 return new UnreadConversation(messages,
8068 remoteInput,
8069 onReply,
8070 onRead,
8071 participants, b.getLong(KEY_TIMESTAMP));
8072 }
8073 };
8074
8075 /**
8076 * Builder class for {@link CarExtender.UnreadConversation} objects.
8077 */
8078 public static class Builder {
8079 private final List<String> mMessages = new ArrayList<String>();
8080 private final String mParticipant;
8081 private RemoteInput mRemoteInput;
8082 private PendingIntent mReadPendingIntent;
8083 private PendingIntent mReplyPendingIntent;
8084 private long mLatestTimestamp;
8085
8086 /**
8087 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8088 *
8089 * @param name The name of the other participant in the conversation.
8090 */
8091 public Builder(String name) {
8092 mParticipant = name;
8093 }
8094
8095 /**
8096 * Appends a new unread message to the list of messages for this conversation.
8097 *
8098 * The messages should be added from oldest to newest.
8099 *
8100 * @param message The text of the new unread message.
8101 * @return This object for method chaining.
8102 */
8103 public Builder addMessage(String message) {
8104 mMessages.add(message);
8105 return this;
8106 }
8107
8108 /**
8109 * Sets the pending intent and remote input which will convey the reply to this
8110 * notification.
8111 *
8112 * @param pendingIntent The pending intent which will be triggered on a reply.
8113 * @param remoteInput The remote input parcelable which will carry the reply.
8114 * @return This object for method chaining.
8115 *
8116 * @see CarExtender.UnreadConversation#getRemoteInput
8117 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8118 */
8119 public Builder setReplyAction(
8120 PendingIntent pendingIntent, RemoteInput remoteInput) {
8121 mRemoteInput = remoteInput;
8122 mReplyPendingIntent = pendingIntent;
8123
8124 return this;
8125 }
8126
8127 /**
8128 * Sets the pending intent that will be sent once the messages in this notification
8129 * are read.
8130 *
8131 * @param pendingIntent The pending intent to use.
8132 * @return This object for method chaining.
8133 */
8134 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8135 mReadPendingIntent = pendingIntent;
8136 return this;
8137 }
8138
8139 /**
8140 * Sets the timestamp of the most recent message in an unread conversation.
8141 *
8142 * If a messaging notification has been posted by your application and has not
8143 * yet been cancelled, posting a later notification with the same id and tag
8144 * but without a newer timestamp may result in Android Auto not displaying a
8145 * heads up notification for the later notification.
8146 *
8147 * @param timestamp The timestamp of the most recent message in the conversation.
8148 * @return This object for method chaining.
8149 */
8150 public Builder setLatestTimestamp(long timestamp) {
8151 mLatestTimestamp = timestamp;
8152 return this;
8153 }
8154
8155 /**
8156 * Builds a new unread conversation object.
8157 *
8158 * @return The new unread conversation object.
8159 */
8160 public UnreadConversation build() {
8161 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8162 String[] participants = { mParticipant };
8163 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8164 mReadPendingIntent, participants, mLatestTimestamp);
8165 }
8166 }
8167 }
8168
8169 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008170 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8171 * with a TV extension:
8172 *
8173 * <ol>
8174 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8175 * <li>Create a {@link TvExtender}.
8176 * <li>Set TV-specific properties using the {@code set} methods of
8177 * {@link TvExtender}.
8178 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8179 * to apply the extension to a notification.
8180 * </ol>
8181 *
8182 * <pre class="prettyprint">
8183 * Notification notification = new Notification.Builder(context)
8184 * ...
8185 * .extend(new TvExtender()
8186 * .set*(...))
8187 * .build();
8188 * </pre>
8189 *
8190 * <p>TV extensions can be accessed on an existing notification by using the
8191 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8192 * to access values.
8193 *
8194 * @hide
8195 */
8196 @SystemApi
8197 public static final class TvExtender implements Extender {
8198 private static final String TAG = "TvExtender";
8199
8200 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8201 private static final String EXTRA_FLAGS = "flags";
8202 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8203 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008204 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008205
8206 // Flags bitwise-ored to mFlags
8207 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8208
8209 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008210 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008211 private PendingIntent mContentIntent;
8212 private PendingIntent mDeleteIntent;
8213
8214 /**
8215 * Create a {@link TvExtender} with default options.
8216 */
8217 public TvExtender() {
8218 mFlags = FLAG_AVAILABLE_ON_TV;
8219 }
8220
8221 /**
8222 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8223 *
8224 * @param notif The notification from which to copy options.
8225 */
8226 public TvExtender(Notification notif) {
8227 Bundle bundle = notif.extras == null ?
8228 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8229 if (bundle != null) {
8230 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008231 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008232 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8233 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8234 }
8235 }
8236
8237 /**
8238 * Apply a TV extension to a notification that is being built. This is typically called by
8239 * the {@link Notification.Builder#extend(Notification.Extender)}
8240 * method of {@link Notification.Builder}.
8241 */
8242 @Override
8243 public Notification.Builder extend(Notification.Builder builder) {
8244 Bundle bundle = new Bundle();
8245
8246 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008247 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008248 if (mContentIntent != null) {
8249 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8250 }
8251
8252 if (mDeleteIntent != null) {
8253 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8254 }
8255
8256 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8257 return builder;
8258 }
8259
8260 /**
8261 * Returns true if this notification should be shown on TV. This method return true
8262 * if the notification was extended with a TvExtender.
8263 */
8264 public boolean isAvailableOnTv() {
8265 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8266 }
8267
8268 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008269 * Specifies the channel the notification should be delivered on when shown on TV.
8270 * It can be different from the channel that the notification is delivered to when
8271 * posting on a non-TV device.
8272 */
8273 public TvExtender setChannel(String channelId) {
8274 mChannelId = channelId;
8275 return this;
8276 }
8277
8278 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008279 * Specifies the channel the notification should be delivered on when shown on TV.
8280 * It can be different from the channel that the notification is delivered to when
8281 * posting on a non-TV device.
8282 */
8283 public TvExtender setChannelId(String channelId) {
8284 mChannelId = channelId;
8285 return this;
8286 }
8287
Jeff Sharkey000ce802017-04-29 13:13:27 -06008288 /** @removed */
8289 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008290 public String getChannel() {
8291 return mChannelId;
8292 }
8293
8294 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008295 * Returns the id of the channel this notification posts to on TV.
8296 */
8297 public String getChannelId() {
8298 return mChannelId;
8299 }
8300
8301 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008302 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8303 * If provided, it is used instead of the content intent specified
8304 * at the level of Notification.
8305 */
8306 public TvExtender setContentIntent(PendingIntent intent) {
8307 mContentIntent = intent;
8308 return this;
8309 }
8310
8311 /**
8312 * Returns the TV-specific content intent. If this method returns null, the
8313 * main content intent on the notification should be used.
8314 *
8315 * @see {@link Notification#contentIntent}
8316 */
8317 public PendingIntent getContentIntent() {
8318 return mContentIntent;
8319 }
8320
8321 /**
8322 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8323 * by the user on TV. If provided, it is used instead of the delete intent specified
8324 * at the level of Notification.
8325 */
8326 public TvExtender setDeleteIntent(PendingIntent intent) {
8327 mDeleteIntent = intent;
8328 return this;
8329 }
8330
8331 /**
8332 * Returns the TV-specific delete intent. If this method returns null, the
8333 * main delete intent on the notification should be used.
8334 *
8335 * @see {@link Notification#deleteIntent}
8336 */
8337 public PendingIntent getDeleteIntent() {
8338 return mDeleteIntent;
8339 }
8340 }
8341
8342 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008343 * Get an array of Notification objects from a parcelable array bundle field.
8344 * Update the bundle to have a typed array so fetches in the future don't need
8345 * to do an array copy.
8346 */
8347 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8348 Parcelable[] array = bundle.getParcelableArray(key);
8349 if (array instanceof Notification[] || array == null) {
8350 return (Notification[]) array;
8351 }
8352 Notification[] typedArray = Arrays.copyOf(array, array.length,
8353 Notification[].class);
8354 bundle.putParcelableArray(key, typedArray);
8355 return typedArray;
8356 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008357
8358 private static class BuilderRemoteViews extends RemoteViews {
8359 public BuilderRemoteViews(Parcel parcel) {
8360 super(parcel);
8361 }
8362
Kenny Guy77320062014-08-27 21:37:15 +01008363 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8364 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008365 }
8366
8367 @Override
8368 public BuilderRemoteViews clone() {
8369 Parcel p = Parcel.obtain();
8370 writeToParcel(p, 0);
8371 p.setDataPosition(0);
8372 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8373 p.recycle();
8374 return brv;
8375 }
8376 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008377
8378 private static class StandardTemplateParams {
8379 boolean hasProgress = true;
8380 boolean ambient = false;
8381 CharSequence title;
8382 CharSequence text;
8383
8384 final StandardTemplateParams reset() {
8385 hasProgress = true;
8386 ambient = false;
8387 title = null;
8388 text = null;
8389 return this;
8390 }
8391
8392 final StandardTemplateParams hasProgress(boolean hasProgress) {
8393 this.hasProgress = hasProgress;
8394 return this;
8395 }
8396
8397 final StandardTemplateParams title(CharSequence title) {
8398 this.title = title;
8399 return this;
8400 }
8401
8402 final StandardTemplateParams text(CharSequence text) {
8403 this.text = text;
8404 return this;
8405 }
8406
8407 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008408 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008409 this.ambient = ambient;
8410 return this;
8411 }
8412
8413 final StandardTemplateParams fillTextsFrom(Builder b) {
8414 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008415 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8416 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008417 return this;
8418 }
8419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420}