blob: f4277ce3917e54dc262b5dea2fa1db28d81d7e53 [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 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001095 * @hide
1096 */
1097 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1098
1099 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001100 * {@link #extras} key: the audio contents of this notification.
1101 *
1102 * This is for use when rendering the notification on an audio-focused interface;
1103 * the audio contents are a complete sound sample that contains the contents/body of the
1104 * notification. This may be used in substitute of a Text-to-Speech reading of the
1105 * notification. For example if the notification represents a voice message this should point
1106 * to the audio of that message.
1107 *
1108 * The data stored under this key should be a String representation of a Uri that contains the
1109 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1110 *
1111 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1112 * has a field for holding data URI. That field can be used for audio.
1113 * See {@code Message#setData}.
1114 *
1115 * Example usage:
1116 * <pre>
1117 * {@code
1118 * Notification.Builder myBuilder = (build your Notification as normal);
1119 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1120 * }
1121 * </pre>
1122 */
1123 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1124
Dan Sandler80eaa592016-04-14 23:34:54 -04001125 /** @hide */
1126 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001127 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001128 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1129
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001130 /**
1131 * This is set on the notification shown by the activity manager about all apps
1132 * running in the background. It indicates that the notification should be shown
1133 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1134 * notification currently visible to the user. This is a string array of all
1135 * package names of the apps.
1136 * @hide
1137 */
1138 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1139
Dan Sandlerd63f9322015-05-06 15:18:49 -04001140 private Icon mSmallIcon;
1141 private Icon mLargeIcon;
1142
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001143 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001144 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001145
Julia Reynolds13d898c2017-02-02 12:22:05 -05001146 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001147 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001148
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001149 /** @hide */
1150 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1151 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1152 })
1153 @Retention(RetentionPolicy.SOURCE)
1154 public @interface GroupAlertBehavior {}
1155
1156 /**
1157 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1158 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1159 * notification will not be muted when it is in a group.
1160 */
1161 public static final int GROUP_ALERT_ALL = 0;
1162
1163 /**
1164 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1165 * notification in a group should be silenced (no sound or vibration) even if they are posted
1166 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
1167 * mute this notification if this notification is a group child.
1168 *
1169 * <p> For example, you might want to use this constant if you post a number of children
1170 * notifications at once (say, after a periodic sync), and only need to notify the user
1171 * audibly once.
1172 */
1173 public static final int GROUP_ALERT_SUMMARY = 1;
1174
1175 /**
1176 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1177 * notification in a group should be silenced (no sound or vibration) even if they are
1178 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1179 * to mute this notification if this notification is a group summary.
1180 *
1181 * <p>For example, you might want to use this constant if only the children notifications
1182 * in your group have content and the summary is only used to visually group notifications.
1183 */
1184 public static final int GROUP_ALERT_CHILDREN = 2;
1185
Julia Reynolds2f431e22017-06-07 14:12:09 +00001186 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001187
Julia Reynolds13d898c2017-02-02 12:22:05 -05001188 /**
1189 * If this notification is being shown as a badge, always show as a number.
1190 */
1191 public static final int BADGE_ICON_NONE = 0;
1192
1193 /**
1194 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1195 * represent this notification.
1196 */
1197 public static final int BADGE_ICON_SMALL = 1;
1198
1199 /**
1200 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1201 * represent this notification.
1202 */
1203 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001204 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001205
Chris Wren51c75102013-07-16 20:49:17 -04001206 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001207 * Structure to encapsulate a named action that can be shown as part of this notification.
1208 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1209 * selected by the user.
1210 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001211 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1212 * or {@link Notification.Builder#addAction(Notification.Action)}
1213 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001214 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001215 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001216 /**
1217 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1218 * {@link RemoteInput}s.
1219 *
1220 * This is intended for {@link RemoteInput}s that only accept data, meaning
1221 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1222 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1223 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1224 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1225 *
1226 * You can test if a RemoteInput matches these constraints using
1227 * {@link RemoteInput#isDataOnly}.
1228 */
1229 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1230
Griff Hazen959591e2014-05-15 22:26:18 -07001231 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001232 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001233 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001234 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001235
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001236 /**
1237 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001238 *
1239 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001240 */
Dan Sandler86647982015-05-13 23:41:13 -04001241 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001242 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001243
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001244 /**
1245 * Title of the action.
1246 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001247 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001248
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001249 /**
1250 * Intent to send when the user invokes this action. May be null, in which case the action
1251 * may be rendered in a disabled presentation by the system UI.
1252 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001253 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001254
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001255 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001256 if (in.readInt() != 0) {
1257 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001258 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1259 icon = mIcon.getResId();
1260 }
Dan Sandler86647982015-05-13 23:41:13 -04001261 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1263 if (in.readInt() == 1) {
1264 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1265 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001266 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001267 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001268 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001269 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001270
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001271 /**
Dan Sandler86647982015-05-13 23:41:13 -04001272 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001273 */
Dan Sandler86647982015-05-13 23:41:13 -04001274 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001275 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001276 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001277 }
1278
Adrian Roos7af53622016-10-12 13:44:05 -07001279 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001280 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001281 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001282 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001283 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1284 this.icon = icon.getResId();
1285 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001286 this.title = title;
1287 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001288 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001289 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001290 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001291 }
1292
1293 /**
Dan Sandler86647982015-05-13 23:41:13 -04001294 * Return an icon representing the action.
1295 */
1296 public Icon getIcon() {
1297 if (mIcon == null && icon != 0) {
1298 // you snuck an icon in here without using the builder; let's try to keep it
1299 mIcon = Icon.createWithResource("", icon);
1300 }
1301 return mIcon;
1302 }
1303
1304 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001305 * Get additional metadata carried around with this Action.
1306 */
1307 public Bundle getExtras() {
1308 return mExtras;
1309 }
1310
1311 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001312 * Return whether the platform should automatically generate possible replies for this
1313 * {@link Action}
1314 */
1315 public boolean getAllowGeneratedReplies() {
1316 return mAllowGeneratedReplies;
1317 }
1318
1319 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001320 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001321 * May return null if no remote inputs were added. Only returns inputs which accept
1322 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001323 */
1324 public RemoteInput[] getRemoteInputs() {
1325 return mRemoteInputs;
1326 }
1327
1328 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001329 * Get the list of inputs to be collected from the user that ONLY accept data when this
1330 * action is sent. These remote inputs are guaranteed to return true on a call to
1331 * {@link RemoteInput#isDataOnly}.
1332 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001333 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001334 *
1335 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1336 * of non-textual RemoteInputs do not access these remote inputs.
1337 */
1338 public RemoteInput[] getDataOnlyRemoteInputs() {
1339 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1340 }
1341
1342 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001343 * Builder class for {@link Action} objects.
1344 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001345 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001346 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001347 private final CharSequence mTitle;
1348 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001349 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001350 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001351 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001352
1353 /**
1354 * Construct a new builder for {@link Action} object.
1355 * @param icon icon to show for this action
1356 * @param title the title of the action
1357 * @param intent the {@link PendingIntent} to fire when users trigger this action
1358 */
Dan Sandler86647982015-05-13 23:41:13 -04001359 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001360 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001361 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001362 }
1363
1364 /**
1365 * Construct a new builder for {@link Action} object.
1366 * @param icon icon to show for this action
1367 * @param title the title of the action
1368 * @param intent the {@link PendingIntent} to fire when users trigger this action
1369 */
1370 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001371 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001372 }
1373
1374 /**
1375 * Construct a new builder for {@link Action} object using the fields from an
1376 * {@link Action}.
1377 * @param action the action to read fields from.
1378 */
1379 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001380 this(action.getIcon(), action.title, action.actionIntent,
1381 new Bundle(action.mExtras), action.getRemoteInputs(),
1382 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001383 }
1384
Dan Sandler86647982015-05-13 23:41:13 -04001385 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001386 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001387 mIcon = icon;
1388 mTitle = title;
1389 mIntent = intent;
1390 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001391 if (remoteInputs != null) {
1392 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1393 Collections.addAll(mRemoteInputs, remoteInputs);
1394 }
Adrian Roos7af53622016-10-12 13:44:05 -07001395 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001396 }
1397
1398 /**
1399 * Merge additional metadata into this builder.
1400 *
1401 * <p>Values within the Bundle will replace existing extras values in this Builder.
1402 *
1403 * @see Notification.Action#extras
1404 */
1405 public Builder addExtras(Bundle extras) {
1406 if (extras != null) {
1407 mExtras.putAll(extras);
1408 }
1409 return this;
1410 }
1411
1412 /**
1413 * Get the metadata Bundle used by this Builder.
1414 *
1415 * <p>The returned Bundle is shared with this Builder.
1416 */
1417 public Bundle getExtras() {
1418 return mExtras;
1419 }
1420
1421 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001422 * Add an input to be collected from the user when this action is sent.
1423 * Response values can be retrieved from the fired intent by using the
1424 * {@link RemoteInput#getResultsFromIntent} function.
1425 * @param remoteInput a {@link RemoteInput} to add to the action
1426 * @return this object for method chaining
1427 */
1428 public Builder addRemoteInput(RemoteInput remoteInput) {
1429 if (mRemoteInputs == null) {
1430 mRemoteInputs = new ArrayList<RemoteInput>();
1431 }
1432 mRemoteInputs.add(remoteInput);
1433 return this;
1434 }
1435
1436 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001437 * Set whether the platform should automatically generate possible replies to add to
1438 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1439 * {@link RemoteInput}, this has no effect.
1440 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1441 * otherwise
1442 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001443 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001444 */
1445 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1446 mAllowGeneratedReplies = allowGeneratedReplies;
1447 return this;
1448 }
1449
1450 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001451 * Apply an extender to this action builder. Extenders may be used to add
1452 * metadata or change options on this builder.
1453 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001454 public Builder extend(Extender extender) {
1455 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001456 return this;
1457 }
1458
1459 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001460 * Combine all of the options that have been set and return a new {@link Action}
1461 * object.
1462 * @return the built action
1463 */
1464 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001465 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1466 RemoteInput[] previousDataInputs =
1467 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001468 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001469 for (RemoteInput input : previousDataInputs) {
1470 dataOnlyInputs.add(input);
1471 }
1472 }
1473 List<RemoteInput> textInputs = new ArrayList<>();
1474 if (mRemoteInputs != null) {
1475 for (RemoteInput input : mRemoteInputs) {
1476 if (input.isDataOnly()) {
1477 dataOnlyInputs.add(input);
1478 } else {
1479 textInputs.add(input);
1480 }
1481 }
1482 }
1483 if (!dataOnlyInputs.isEmpty()) {
1484 RemoteInput[] dataInputsArr =
1485 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1486 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1487 }
1488 RemoteInput[] textInputsArr = textInputs.isEmpty()
1489 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1490 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001491 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001492 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001493 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001494
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001495 @Override
1496 public Action clone() {
1497 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001498 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001499 title,
1500 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001501 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001502 getRemoteInputs(),
1503 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001504 }
1505 @Override
1506 public int describeContents() {
1507 return 0;
1508 }
1509 @Override
1510 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001511 final Icon ic = getIcon();
1512 if (ic != null) {
1513 out.writeInt(1);
1514 ic.writeToParcel(out, 0);
1515 } else {
1516 out.writeInt(0);
1517 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001518 TextUtils.writeToParcel(title, out, flags);
1519 if (actionIntent != null) {
1520 out.writeInt(1);
1521 actionIntent.writeToParcel(out, flags);
1522 } else {
1523 out.writeInt(0);
1524 }
Griff Hazen959591e2014-05-15 22:26:18 -07001525 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001526 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001527 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001528 }
Griff Hazen959591e2014-05-15 22:26:18 -07001529 public static final Parcelable.Creator<Action> CREATOR =
1530 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001531 public Action createFromParcel(Parcel in) {
1532 return new Action(in);
1533 }
1534 public Action[] newArray(int size) {
1535 return new Action[size];
1536 }
1537 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001538
1539 /**
1540 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1541 * metadata or change options on an action builder.
1542 */
1543 public interface Extender {
1544 /**
1545 * Apply this extender to a notification action builder.
1546 * @param builder the builder to be modified.
1547 * @return the build object for chaining.
1548 */
1549 public Builder extend(Builder builder);
1550 }
1551
1552 /**
1553 * Wearable extender for notification actions. To add extensions to an action,
1554 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1555 * the {@code WearableExtender()} constructor and apply it to a
1556 * {@link android.app.Notification.Action.Builder} using
1557 * {@link android.app.Notification.Action.Builder#extend}.
1558 *
1559 * <pre class="prettyprint">
1560 * Notification.Action action = new Notification.Action.Builder(
1561 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001562 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001563 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001564 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001565 */
1566 public static final class WearableExtender implements Extender {
1567 /** Notification action extra which contains wearable extensions */
1568 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1569
Pete Gastaf6781d2014-10-07 15:17:05 -04001570 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001571 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001572 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1573 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1574 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001575
1576 // Flags bitwise-ored to mFlags
1577 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001578 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001579 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001580
1581 // Default value for flags integer
1582 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1583
1584 private int mFlags = DEFAULT_FLAGS;
1585
Pete Gastaf6781d2014-10-07 15:17:05 -04001586 private CharSequence mInProgressLabel;
1587 private CharSequence mConfirmLabel;
1588 private CharSequence mCancelLabel;
1589
Griff Hazen61a9e862014-05-22 16:05:19 -07001590 /**
1591 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1592 * options.
1593 */
1594 public WearableExtender() {
1595 }
1596
1597 /**
1598 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1599 * wearable options present in an existing notification action.
1600 * @param action the notification action to inspect.
1601 */
1602 public WearableExtender(Action action) {
1603 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1604 if (wearableBundle != null) {
1605 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001606 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1607 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1608 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001609 }
1610 }
1611
1612 /**
1613 * Apply wearable extensions to a notification action that is being built. This is
1614 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1615 * method of {@link android.app.Notification.Action.Builder}.
1616 */
1617 @Override
1618 public Action.Builder extend(Action.Builder builder) {
1619 Bundle wearableBundle = new Bundle();
1620
1621 if (mFlags != DEFAULT_FLAGS) {
1622 wearableBundle.putInt(KEY_FLAGS, mFlags);
1623 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001624 if (mInProgressLabel != null) {
1625 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1626 }
1627 if (mConfirmLabel != null) {
1628 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1629 }
1630 if (mCancelLabel != null) {
1631 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1632 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001633
1634 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1635 return builder;
1636 }
1637
1638 @Override
1639 public WearableExtender clone() {
1640 WearableExtender that = new WearableExtender();
1641 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001642 that.mInProgressLabel = this.mInProgressLabel;
1643 that.mConfirmLabel = this.mConfirmLabel;
1644 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001645 return that;
1646 }
1647
1648 /**
1649 * Set whether this action is available when the wearable device is not connected to
1650 * a companion device. The user can still trigger this action when the wearable device is
1651 * offline, but a visual hint will indicate that the action may not be available.
1652 * Defaults to true.
1653 */
1654 public WearableExtender setAvailableOffline(boolean availableOffline) {
1655 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1656 return this;
1657 }
1658
1659 /**
1660 * Get whether this action is available when the wearable device is not connected to
1661 * a companion device. The user can still trigger this action when the wearable device is
1662 * offline, but a visual hint will indicate that the action may not be available.
1663 * Defaults to true.
1664 */
1665 public boolean isAvailableOffline() {
1666 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1667 }
1668
1669 private void setFlag(int mask, boolean value) {
1670 if (value) {
1671 mFlags |= mask;
1672 } else {
1673 mFlags &= ~mask;
1674 }
1675 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001676
1677 /**
1678 * Set a label to display while the wearable is preparing to automatically execute the
1679 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1680 *
1681 * @param label the label to display while the action is being prepared to execute
1682 * @return this object for method chaining
1683 */
1684 public WearableExtender setInProgressLabel(CharSequence label) {
1685 mInProgressLabel = label;
1686 return this;
1687 }
1688
1689 /**
1690 * Get the label to display while the wearable is preparing to automatically execute
1691 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1692 *
1693 * @return the label to display while the action is being prepared to execute
1694 */
1695 public CharSequence getInProgressLabel() {
1696 return mInProgressLabel;
1697 }
1698
1699 /**
1700 * Set a label to display to confirm that the action should be executed.
1701 * This is usually an imperative verb like "Send".
1702 *
1703 * @param label the label to confirm the action should be executed
1704 * @return this object for method chaining
1705 */
1706 public WearableExtender setConfirmLabel(CharSequence label) {
1707 mConfirmLabel = label;
1708 return this;
1709 }
1710
1711 /**
1712 * Get the label to display to confirm that the action should be executed.
1713 * This is usually an imperative verb like "Send".
1714 *
1715 * @return the label to confirm the action should be executed
1716 */
1717 public CharSequence getConfirmLabel() {
1718 return mConfirmLabel;
1719 }
1720
1721 /**
1722 * Set a label to display to cancel the action.
1723 * This is usually an imperative verb, like "Cancel".
1724 *
1725 * @param label the label to display to cancel the action
1726 * @return this object for method chaining
1727 */
1728 public WearableExtender setCancelLabel(CharSequence label) {
1729 mCancelLabel = label;
1730 return this;
1731 }
1732
1733 /**
1734 * Get the label to display to cancel the action.
1735 * This is usually an imperative verb like "Cancel".
1736 *
1737 * @return the label to display to cancel the action
1738 */
1739 public CharSequence getCancelLabel() {
1740 return mCancelLabel;
1741 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001742
1743 /**
1744 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1745 * platform that it can generate the appropriate transitions.
1746 * @param hintLaunchesActivity {@code true} if the content intent will launch
1747 * an activity and transitions should be generated, false otherwise.
1748 * @return this object for method chaining
1749 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001750 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001751 boolean hintLaunchesActivity) {
1752 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1753 return this;
1754 }
1755
1756 /**
1757 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1758 * platform that it can generate the appropriate transitions
1759 * @return {@code true} if the content intent will launch an activity and transitions
1760 * should be generated, false otherwise. The default value is {@code false} if this was
1761 * never set.
1762 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001763 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001764 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1765 }
Alex Hills9f087612016-06-07 09:08:59 -04001766
1767 /**
1768 * Set a hint that this Action should be displayed inline.
1769 *
1770 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1771 * otherwise
1772 * @return this object for method chaining
1773 */
1774 public WearableExtender setHintDisplayActionInline(
1775 boolean hintDisplayInline) {
1776 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1777 return this;
1778 }
1779
1780 /**
1781 * Get a hint that this Action should be displayed inline.
1782 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001783 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001784 * otherwise. The default value is {@code false} if this was never set.
1785 */
1786 public boolean getHintDisplayActionInline() {
1787 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1788 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001789 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001790 }
1791
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001792 /**
1793 * Array of all {@link Action} structures attached to this notification by
1794 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1795 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1796 * interface for invoking actions.
1797 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001798 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001799
1800 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001801 * Replacement version of this notification whose content will be shown
1802 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1803 * and {@link #VISIBILITY_PUBLIC}.
1804 */
1805 public Notification publicVersion;
1806
1807 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001808 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001809 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 */
1811 public Notification()
1812 {
1813 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001814 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001815 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817
1818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 * @hide
1820 */
1821 public Notification(Context context, int icon, CharSequence tickerText, long when,
1822 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1823 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001824 new Builder(context)
1825 .setWhen(when)
1826 .setSmallIcon(icon)
1827 .setTicker(tickerText)
1828 .setContentTitle(contentTitle)
1829 .setContentText(contentText)
1830 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1831 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833
1834 /**
1835 * Constructs a Notification object with the information needed to
1836 * have a status bar icon without the standard expanded view.
1837 *
1838 * @param icon The resource id of the icon to put in the status bar.
1839 * @param tickerText The text that flows by in the status bar when the notification first
1840 * activates.
1841 * @param when The time to show in the time field. In the System.currentTimeMillis
1842 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001843 *
1844 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001846 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 public Notification(int icon, CharSequence tickerText, long when)
1848 {
1849 this.icon = icon;
1850 this.tickerText = tickerText;
1851 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001852 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
1855 /**
1856 * Unflatten the notification from a parcel.
1857 */
Svet Ganovddb94882016-06-23 19:55:24 -07001858 @SuppressWarnings("unchecked")
1859 public Notification(Parcel parcel) {
1860 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1861 // intents in extras are always written as the last entry.
1862 readFromParcelImpl(parcel);
1863 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001864 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001865 }
1866
1867 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 {
1869 int version = parcel.readInt();
1870
Dianne Hackborn98305522017-05-05 17:53:53 -07001871 whitelistToken = parcel.readStrongBinder();
1872 if (whitelistToken == null) {
1873 whitelistToken = processWhitelistToken;
1874 }
1875 // Propagate this token to all pending intents that are unmarshalled from the parcel.
1876 parcel.setClassCookie(PendingIntent.class, whitelistToken);
1877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001879 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001880 if (parcel.readInt() != 0) {
1881 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001882 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1883 icon = mSmallIcon.getResId();
1884 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 number = parcel.readInt();
1887 if (parcel.readInt() != 0) {
1888 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1889 }
1890 if (parcel.readInt() != 0) {
1891 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1892 }
1893 if (parcel.readInt() != 0) {
1894 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1895 }
1896 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001897 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001898 }
1899 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1901 }
Joe Onorato561d3852010-11-20 18:09:34 -08001902 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001903 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 defaults = parcel.readInt();
1906 flags = parcel.readInt();
1907 if (parcel.readInt() != 0) {
1908 sound = Uri.CREATOR.createFromParcel(parcel);
1909 }
1910
1911 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001912 if (parcel.readInt() != 0) {
1913 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 vibrate = parcel.createLongArray();
1916 ledARGB = parcel.readInt();
1917 ledOnMS = parcel.readInt();
1918 ledOffMS = parcel.readInt();
1919 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001920
1921 if (parcel.readInt() != 0) {
1922 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1923 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001924
1925 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001926
John Spurlockfd7f1e02014-03-18 16:41:57 -04001927 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001928
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001929 mGroupKey = parcel.readString();
1930
1931 mSortKey = parcel.readString();
1932
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001933 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001934
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001935 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1936
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001937 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001938 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1939 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001940
Chris Wren8fd39ec2014-02-27 17:43:26 -05001941 if (parcel.readInt() != 0) {
1942 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1943 }
1944
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001945 visibility = parcel.readInt();
1946
1947 if (parcel.readInt() != 0) {
1948 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1949 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001950
1951 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001952
1953 if (parcel.readInt() != 0) {
1954 mChannelId = parcel.readString();
1955 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001956 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001957
1958 if (parcel.readInt() != 0) {
1959 mShortcutId = parcel.readString();
1960 }
1961
1962 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001963
1964 if (parcel.readInt() != 0) {
1965 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1966 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001967
1968 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
1970
Andy Stadler110988c2010-12-03 14:29:16 -08001971 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001972 public Notification clone() {
1973 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001974 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001975 return that;
1976 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001977
Daniel Sandler1a497d32013-04-18 14:52:45 -04001978 /**
1979 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1980 * of this into that.
1981 * @hide
1982 */
1983 public void cloneInto(Notification that, boolean heavy) {
Dianne Hackborn98305522017-05-05 17:53:53 -07001984 that.whitelistToken = this.whitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001985 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001986 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001987 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001988 that.number = this.number;
1989
1990 // PendingIntents are global, so there's no reason (or way) to clone them.
1991 that.contentIntent = this.contentIntent;
1992 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001993 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001994
1995 if (this.tickerText != null) {
1996 that.tickerText = this.tickerText.toString();
1997 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001998 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001999 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002000 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002001 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002002 that.contentView = this.contentView.clone();
2003 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002004 if (heavy && this.mLargeIcon != null) {
2005 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002006 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002007 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002008 that.sound = this.sound; // android.net.Uri is immutable
2009 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002010 if (this.audioAttributes != null) {
2011 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2012 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002013
2014 final long[] vibrate = this.vibrate;
2015 if (vibrate != null) {
2016 final int N = vibrate.length;
2017 final long[] vib = that.vibrate = new long[N];
2018 System.arraycopy(vibrate, 0, vib, 0, N);
2019 }
2020
2021 that.ledARGB = this.ledARGB;
2022 that.ledOnMS = this.ledOnMS;
2023 that.ledOffMS = this.ledOffMS;
2024 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002025
Joe Onorato18e69df2010-05-17 22:26:12 -07002026 that.flags = this.flags;
2027
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002028 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002029
John Spurlockfd7f1e02014-03-18 16:41:57 -04002030 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002031
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002032 that.mGroupKey = this.mGroupKey;
2033
2034 that.mSortKey = this.mSortKey;
2035
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002036 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002037 try {
2038 that.extras = new Bundle(this.extras);
2039 // will unparcel
2040 that.extras.size();
2041 } catch (BadParcelableException e) {
2042 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2043 that.extras = null;
2044 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002045 }
2046
Felipe Lemedd85da62016-06-28 11:29:54 -07002047 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2048 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002049 }
2050
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002051 if (this.actions != null) {
2052 that.actions = new Action[this.actions.length];
2053 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002054 if ( this.actions[i] != null) {
2055 that.actions[i] = this.actions[i].clone();
2056 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002057 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002058 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002059
Daniel Sandler1a497d32013-04-18 14:52:45 -04002060 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002061 that.bigContentView = this.bigContentView.clone();
2062 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002063
Chris Wren8fd39ec2014-02-27 17:43:26 -05002064 if (heavy && this.headsUpContentView != null) {
2065 that.headsUpContentView = this.headsUpContentView.clone();
2066 }
2067
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002068 that.visibility = this.visibility;
2069
2070 if (this.publicVersion != null) {
2071 that.publicVersion = new Notification();
2072 this.publicVersion.cloneInto(that.publicVersion, heavy);
2073 }
2074
Dan Sandler26e81cf2014-05-06 10:01:27 -04002075 that.color = this.color;
2076
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002077 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002078 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002079 that.mShortcutId = this.mShortcutId;
2080 that.mBadgeIcon = this.mBadgeIcon;
2081 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002082 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002083
Daniel Sandler1a497d32013-04-18 14:52:45 -04002084 if (!heavy) {
2085 that.lightenPayload(); // will clean out extras
2086 }
2087 }
2088
2089 /**
2090 * Removes heavyweight parts of the Notification object for archival or for sending to
2091 * listeners when the full contents are not necessary.
2092 * @hide
2093 */
2094 public final void lightenPayload() {
2095 tickerView = null;
2096 contentView = null;
2097 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002098 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002099 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002100 if (extras != null && !extras.isEmpty()) {
2101 final Set<String> keyset = extras.keySet();
2102 final int N = keyset.size();
2103 final String[] keys = keyset.toArray(new String[N]);
2104 for (int i=0; i<N; i++) {
2105 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002106 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2107 continue;
2108 }
Dan Sandler50128532015-12-08 15:42:41 -05002109 final Object obj = extras.get(key);
2110 if (obj != null &&
2111 ( obj instanceof Parcelable
2112 || obj instanceof Parcelable[]
2113 || obj instanceof SparseArray
2114 || obj instanceof ArrayList)) {
2115 extras.remove(key);
2116 }
2117 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002118 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002119 }
2120
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002121 /**
2122 * Make sure this CharSequence is safe to put into a bundle, which basically
2123 * means it had better not be some custom Parcelable implementation.
2124 * @hide
2125 */
2126 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002127 if (cs == null) return cs;
2128 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2129 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2130 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002131 if (cs instanceof Parcelable) {
2132 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2133 + " instance is a custom Parcelable and not allowed in Notification");
2134 return cs.toString();
2135 }
Selim Cinek60a54252016-02-26 17:03:25 -08002136 return removeTextSizeSpans(cs);
2137 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002138
Selim Cinek60a54252016-02-26 17:03:25 -08002139 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2140 if (charSequence instanceof Spanned) {
2141 Spanned ss = (Spanned) charSequence;
2142 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2143 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2144 for (Object span : spans) {
2145 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002146 if (resultSpan instanceof CharacterStyle) {
2147 resultSpan = ((CharacterStyle) span).getUnderlying();
2148 }
2149 if (resultSpan instanceof TextAppearanceSpan) {
2150 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002151 resultSpan = new TextAppearanceSpan(
2152 originalSpan.getFamily(),
2153 originalSpan.getTextStyle(),
2154 -1,
2155 originalSpan.getTextColor(),
2156 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002157 } else if (resultSpan instanceof RelativeSizeSpan
2158 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002159 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002160 } else {
2161 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002162 }
2163 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2164 ss.getSpanFlags(span));
2165 }
2166 return builder;
2167 }
2168 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002169 }
2170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 public int describeContents() {
2172 return 0;
2173 }
2174
2175 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002176 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 */
Svet Ganovddb94882016-06-23 19:55:24 -07002178 public void writeToParcel(Parcel parcel, int flags) {
2179 // We need to mark all pending intents getting into the notification
2180 // system as being put there to later allow the notification ranker
2181 // to launch them and by doing so add the app to the battery saver white
2182 // list for a short period of time. The problem is that the system
2183 // cannot look into the extras as there may be parcelables there that
2184 // the platform does not know how to handle. To go around that we have
2185 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002186 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002187 if (collectPendingIntents) {
2188 PendingIntent.setOnMarshaledListener(
2189 (PendingIntent intent, Parcel out, int outFlags) -> {
2190 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002191 if (allPendingIntents == null) {
2192 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002193 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002194 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002195 }
2196 });
2197 }
2198 try {
2199 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002200 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002201 writeToParcelImpl(parcel, flags);
2202 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002203 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002204 } finally {
2205 if (collectPendingIntents) {
2206 PendingIntent.setOnMarshaledListener(null);
2207 }
2208 }
2209 }
2210
2211 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 parcel.writeInt(1);
2213
Dianne Hackborn98305522017-05-05 17:53:53 -07002214 parcel.writeStrongBinder(whitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002216 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002217 if (mSmallIcon == null && icon != 0) {
2218 // you snuck an icon in here without using the builder; let's try to keep it
2219 mSmallIcon = Icon.createWithResource("", icon);
2220 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002221 if (mSmallIcon != null) {
2222 parcel.writeInt(1);
2223 mSmallIcon.writeToParcel(parcel, 0);
2224 } else {
2225 parcel.writeInt(0);
2226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 parcel.writeInt(number);
2228 if (contentIntent != null) {
2229 parcel.writeInt(1);
2230 contentIntent.writeToParcel(parcel, 0);
2231 } else {
2232 parcel.writeInt(0);
2233 }
2234 if (deleteIntent != null) {
2235 parcel.writeInt(1);
2236 deleteIntent.writeToParcel(parcel, 0);
2237 } else {
2238 parcel.writeInt(0);
2239 }
2240 if (tickerText != null) {
2241 parcel.writeInt(1);
2242 TextUtils.writeToParcel(tickerText, parcel, flags);
2243 } else {
2244 parcel.writeInt(0);
2245 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002246 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002247 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002248 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002249 } else {
2250 parcel.writeInt(0);
2251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 if (contentView != null) {
2253 parcel.writeInt(1);
2254 contentView.writeToParcel(parcel, 0);
2255 } else {
2256 parcel.writeInt(0);
2257 }
Selim Cinek279fa862016-06-14 10:57:25 -07002258 if (mLargeIcon == null && largeIcon != null) {
2259 // you snuck an icon in here without using the builder; let's try to keep it
2260 mLargeIcon = Icon.createWithBitmap(largeIcon);
2261 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002262 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002263 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002264 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002265 } else {
2266 parcel.writeInt(0);
2267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268
2269 parcel.writeInt(defaults);
2270 parcel.writeInt(this.flags);
2271
2272 if (sound != null) {
2273 parcel.writeInt(1);
2274 sound.writeToParcel(parcel, 0);
2275 } else {
2276 parcel.writeInt(0);
2277 }
2278 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002279
2280 if (audioAttributes != null) {
2281 parcel.writeInt(1);
2282 audioAttributes.writeToParcel(parcel, 0);
2283 } else {
2284 parcel.writeInt(0);
2285 }
2286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 parcel.writeLongArray(vibrate);
2288 parcel.writeInt(ledARGB);
2289 parcel.writeInt(ledOnMS);
2290 parcel.writeInt(ledOffMS);
2291 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002292
2293 if (fullScreenIntent != null) {
2294 parcel.writeInt(1);
2295 fullScreenIntent.writeToParcel(parcel, 0);
2296 } else {
2297 parcel.writeInt(0);
2298 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002299
2300 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002301
John Spurlockfd7f1e02014-03-18 16:41:57 -04002302 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002303
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002304 parcel.writeString(mGroupKey);
2305
2306 parcel.writeString(mSortKey);
2307
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002308 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002309
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002310 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002311
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002312 if (bigContentView != null) {
2313 parcel.writeInt(1);
2314 bigContentView.writeToParcel(parcel, 0);
2315 } else {
2316 parcel.writeInt(0);
2317 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002318
Chris Wren8fd39ec2014-02-27 17:43:26 -05002319 if (headsUpContentView != null) {
2320 parcel.writeInt(1);
2321 headsUpContentView.writeToParcel(parcel, 0);
2322 } else {
2323 parcel.writeInt(0);
2324 }
2325
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002326 parcel.writeInt(visibility);
2327
2328 if (publicVersion != null) {
2329 parcel.writeInt(1);
2330 publicVersion.writeToParcel(parcel, 0);
2331 } else {
2332 parcel.writeInt(0);
2333 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002334
2335 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002336
2337 if (mChannelId != null) {
2338 parcel.writeInt(1);
2339 parcel.writeString(mChannelId);
2340 } else {
2341 parcel.writeInt(0);
2342 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002343 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002344
2345 if (mShortcutId != null) {
2346 parcel.writeInt(1);
2347 parcel.writeString(mShortcutId);
2348 } else {
2349 parcel.writeInt(0);
2350 }
2351
2352 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002353
2354 if (mSettingsText != null) {
2355 parcel.writeInt(1);
2356 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2357 } else {
2358 parcel.writeInt(0);
2359 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002360
2361 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
2363
2364 /**
2365 * Parcelable.Creator that instantiates Notification objects
2366 */
2367 public static final Parcelable.Creator<Notification> CREATOR
2368 = new Parcelable.Creator<Notification>()
2369 {
2370 public Notification createFromParcel(Parcel parcel)
2371 {
2372 return new Notification(parcel);
2373 }
2374
2375 public Notification[] newArray(int size)
2376 {
2377 return new Notification[size];
2378 }
2379 };
2380
2381 /**
2382 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2383 * layout.
2384 *
2385 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2386 * in the view.</p>
2387 * @param context The context for your application / activity.
2388 * @param contentTitle The title that goes in the expanded entry.
2389 * @param contentText The text that goes in the expanded entry.
2390 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2391 * If this is an activity, it must include the
2392 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002393 * that you take care of task management as described in the
2394 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2395 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002396 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002397 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002398 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002400 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 public void setLatestEventInfo(Context context,
2402 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002403 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2404 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2405 new Throwable());
2406 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002407
Selim Cinek4ac6f602016-06-13 15:47:03 -07002408 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2409 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2410 }
2411
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002412 // ensure that any information already set directly is preserved
2413 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002414
2415 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002417 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002420 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002422 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002423
2424 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
2426
Julia Reynoldsda303542015-11-23 14:00:20 -05002427 /**
2428 * @hide
2429 */
2430 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002431 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002432 }
2433
2434 /**
2435 * @hide
2436 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002437 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002438 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002439 }
2440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 @Override
2442 public String toString() {
2443 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002444 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002445 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002446 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002447 sb.append(priority);
2448 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002449 if (contentView != null) {
2450 sb.append(contentView.getPackage());
2451 sb.append("/0x");
2452 sb.append(Integer.toHexString(contentView.getLayoutId()));
2453 } else {
2454 sb.append("null");
2455 }
2456 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002457 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2458 sb.append("default");
2459 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 int N = this.vibrate.length-1;
2461 sb.append("[");
2462 for (int i=0; i<N; i++) {
2463 sb.append(this.vibrate[i]);
2464 sb.append(',');
2465 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002466 if (N != -1) {
2467 sb.append(this.vibrate[N]);
2468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 } else {
2471 sb.append("null");
2472 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002473 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002474 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002476 } else if (this.sound != null) {
2477 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 } else {
2479 sb.append("null");
2480 }
Chris Wren365b6d32015-07-16 10:39:26 -04002481 if (this.tickerText != null) {
2482 sb.append(" tick");
2483 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002484 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002486 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002487 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002488 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002489 if (this.category != null) {
2490 sb.append(" category=");
2491 sb.append(this.category);
2492 }
2493 if (this.mGroupKey != null) {
2494 sb.append(" groupKey=");
2495 sb.append(this.mGroupKey);
2496 }
2497 if (this.mSortKey != null) {
2498 sb.append(" sortKey=");
2499 sb.append(this.mSortKey);
2500 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002501 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002502 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002503 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002504 }
2505 sb.append(" vis=");
2506 sb.append(visibilityToString(this.visibility));
2507 if (this.publicVersion != null) {
2508 sb.append(" publicVersion=");
2509 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002510 }
2511 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 return sb.toString();
2513 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002514
Dan Sandler1b718782014-07-18 12:43:45 -04002515 /**
2516 * {@hide}
2517 */
2518 public static String visibilityToString(int vis) {
2519 switch (vis) {
2520 case VISIBILITY_PRIVATE:
2521 return "PRIVATE";
2522 case VISIBILITY_PUBLIC:
2523 return "PUBLIC";
2524 case VISIBILITY_SECRET:
2525 return "SECRET";
2526 default:
2527 return "UNKNOWN(" + String.valueOf(vis) + ")";
2528 }
2529 }
2530
Joe Onoratocb109a02011-01-18 17:57:41 -08002531 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002532 * {@hide}
2533 */
2534 public static String priorityToString(@Priority int pri) {
2535 switch (pri) {
2536 case PRIORITY_MIN:
2537 return "MIN";
2538 case PRIORITY_LOW:
2539 return "LOW";
2540 case PRIORITY_DEFAULT:
2541 return "DEFAULT";
2542 case PRIORITY_HIGH:
2543 return "HIGH";
2544 case PRIORITY_MAX:
2545 return "MAX";
2546 default:
2547 return "UNKNOWN(" + String.valueOf(pri) + ")";
2548 }
2549 }
2550
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002551 /**
2552 * @hide
2553 */
2554 public boolean hasCompletedProgress() {
2555 // not a progress notification; can't be complete
2556 if (!extras.containsKey(EXTRA_PROGRESS)
2557 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2558 return false;
2559 }
2560 // many apps use max 0 for 'indeterminate'; not complete
2561 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2562 return false;
2563 }
2564 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2565 }
2566
Jeff Sharkey000ce802017-04-29 13:13:27 -06002567 /** @removed */
2568 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002569 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002570 return mChannelId;
2571 }
2572
2573 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002574 * Returns the id of the channel this notification posts to.
2575 */
2576 public String getChannelId() {
2577 return mChannelId;
2578 }
2579
Jeff Sharkey000ce802017-04-29 13:13:27 -06002580 /** @removed */
2581 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002582 public long getTimeout() {
2583 return mTimeout;
2584 }
2585
2586 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002587 * Returns the duration from posting after which this notification should be canceled by the
2588 * system, if it's not canceled already.
2589 */
2590 public long getTimeoutAfter() {
2591 return mTimeout;
2592 }
2593
2594 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002595 * Returns what icon should be shown for this notification if it is being displayed in a
2596 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2597 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2598 */
2599 public int getBadgeIconType() {
2600 return mBadgeIcon;
2601 }
2602
2603 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002604 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002605 *
2606 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2607 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002608 */
2609 public String getShortcutId() {
2610 return mShortcutId;
2611 }
2612
Julia Reynolds3aedded2017-03-31 14:42:09 -04002613
2614 /**
2615 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2616 */
2617 public CharSequence getSettingsText() {
2618 return mSettingsText;
2619 }
2620
Julia Reynolds13d898c2017-02-02 12:22:05 -05002621 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002622 * Returns which type of notifications in a group are responsible for audibly alerting the
2623 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2624 * {@link #GROUP_ALERT_SUMMARY}.
2625 */
2626 public @GroupAlertBehavior int getGroupAlertBehavior() {
2627 return mGroupAlertBehavior;
2628 }
2629
2630 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002631 * The small icon representing this notification in the status bar and content view.
2632 *
2633 * @return the small icon representing this notification.
2634 *
2635 * @see Builder#getSmallIcon()
2636 * @see Builder#setSmallIcon(Icon)
2637 */
2638 public Icon getSmallIcon() {
2639 return mSmallIcon;
2640 }
2641
2642 /**
2643 * Used when notifying to clean up legacy small icons.
2644 * @hide
2645 */
2646 public void setSmallIcon(Icon icon) {
2647 mSmallIcon = icon;
2648 }
2649
2650 /**
2651 * The large icon shown in this notification's content view.
2652 * @see Builder#getLargeIcon()
2653 * @see Builder#setLargeIcon(Icon)
2654 */
2655 public Icon getLargeIcon() {
2656 return mLargeIcon;
2657 }
2658
2659 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002660 * @hide
2661 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002662 public boolean isGroupSummary() {
2663 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2664 }
2665
2666 /**
2667 * @hide
2668 */
2669 public boolean isGroupChild() {
2670 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2671 }
2672
2673 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002674 * @hide
2675 */
2676 public boolean suppressAlertingDueToGrouping() {
2677 if (isGroupSummary()
2678 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2679 return true;
2680 } else if (isGroupChild()
2681 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2682 return true;
2683 }
2684 return false;
2685 }
2686
2687 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002690 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002691 * content views using the platform's notification layout template. If your app supports
2692 * versions of Android as old as API level 4, you can instead use
2693 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2694 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2695 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002696 *
Scott Main183bf112012-08-13 19:12:13 -07002697 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002698 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002700 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002701 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2702 * .setContentText(subject)
2703 * .setSmallIcon(R.drawable.new_mail)
2704 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002705 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002706 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002707 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002708 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002709 /**
2710 * @hide
2711 */
2712 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2713 "android.rebuild.contentViewActionCount";
2714 /**
2715 * @hide
2716 */
2717 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2718 = "android.rebuild.bigViewActionCount";
2719 /**
2720 * @hide
2721 */
2722 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2723 = "android.rebuild.hudViewActionCount";
2724
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002725 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002726
Selim Cinek6743c0b2017-01-18 18:24:01 -08002727 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2728 SystemProperties.getBoolean("notifications.only_title", true);
2729
Selim Cinek389edcd2017-05-11 19:16:44 -07002730 /**
2731 * The lightness difference that has to be added to the primary text color to obtain the
2732 * secondary text color when the background is light.
2733 */
2734 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2735
2736 /**
2737 * The lightness difference that has to be added to the primary text color to obtain the
2738 * secondary text color when the background is dark.
2739 * A bit less then the above value, since it looks better on dark backgrounds.
2740 */
2741 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2742
Joe Onorato46439ce2010-11-19 13:56:21 -08002743 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002744 private Notification mN;
2745 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002746 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002747 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2748 private ArrayList<String> mPersonList = new ArrayList<String>();
2749 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002750 private boolean mIsLegacy;
2751 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002752
2753 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002754 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2755 */
2756 private int mCachedContrastColor = COLOR_INVALID;
2757 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002758 /**
2759 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2760 */
2761 private int mCachedAmbientColor = COLOR_INVALID;
2762 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002763
2764 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002765 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2766 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2767 */
2768 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002769 private int mTextColorsAreForBackground = COLOR_INVALID;
2770 private int mPrimaryTextColor = COLOR_INVALID;
2771 private int mSecondaryTextColor = COLOR_INVALID;
2772 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002773 private int mBackgroundColor = COLOR_INVALID;
2774 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002775 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002776 /**
2777 * A temporary location where actions are stored. If != null the view originally has action
2778 * but doesn't have any for this inflation.
2779 */
2780 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002781 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002782
Anthony Chenad4d1582017-04-10 16:07:58 -07002783 private boolean mTintActionButtons;
2784 private boolean mInNightMode;
2785
Adrian Roos70d7aa32017-01-11 15:39:06 -08002786 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002787 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002788 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002789 * @param context
2790 * A {@link Context} that will be used by the Builder to construct the
2791 * RemoteViews. The Context will not be held past the lifetime of this Builder
2792 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002793 * @param channelId
2794 * The constructed Notification will be posted on this
2795 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2796 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002797 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002798 public Builder(Context context, String channelId) {
2799 this(context, (Notification) null);
2800 mN.mChannelId = channelId;
2801 }
2802
2803 /**
2804 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2805 * instead. All posted Notifications must specify a NotificationChannel Id.
2806 */
2807 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002808 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002809 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002810 }
2811
Joe Onoratocb109a02011-01-18 17:57:41 -08002812 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002813 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002814 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002815 public Builder(Context context, Notification toAdopt) {
2816 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002817 Resources res = mContext.getResources();
2818 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2819
2820 if (res.getBoolean(R.bool.config_enableNightMode)) {
2821 Configuration currentConfig = res.getConfiguration();
2822 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2823 == Configuration.UI_MODE_NIGHT_YES;
2824 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002825
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002826 if (toAdopt == null) {
2827 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002828 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2829 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2830 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002831 mN.priority = PRIORITY_DEFAULT;
2832 mN.visibility = VISIBILITY_PRIVATE;
2833 } else {
2834 mN = toAdopt;
2835 if (mN.actions != null) {
2836 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002837 }
2838
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002839 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2840 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2841 }
2842
Selim Cinek4ac6f602016-06-13 15:47:03 -07002843 if (mN.getSmallIcon() == null && mN.icon != 0) {
2844 setSmallIcon(mN.icon);
2845 }
2846
2847 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2848 setLargeIcon(mN.largeIcon);
2849 }
2850
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002851 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2852 if (!TextUtils.isEmpty(templateClass)) {
2853 final Class<? extends Style> styleClass
2854 = getNotificationStyleClass(templateClass);
2855 if (styleClass == null) {
2856 Log.d(TAG, "Unknown style class: " + templateClass);
2857 } else {
2858 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002859 final Constructor<? extends Style> ctor =
2860 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002861 ctor.setAccessible(true);
2862 final Style style = ctor.newInstance();
2863 style.restoreFromExtras(mN.extras);
2864
2865 if (style != null) {
2866 setStyle(style);
2867 }
2868 } catch (Throwable t) {
2869 Log.e(TAG, "Could not create Style", t);
2870 }
2871 }
2872 }
2873
2874 }
2875 }
2876
2877 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002878 if (mColorUtil == null) {
2879 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002880 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002881 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002882 }
2883
2884 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002885 * If this notification is duplicative of a Launcher shortcut, sets the
2886 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2887 * the shortcut.
2888 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002889 * This field will be ignored by Launchers that don't support badging, don't show
2890 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002891 *
2892 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2893 * supersedes
2894 */
2895 public Builder setShortcutId(String shortcutId) {
2896 mN.mShortcutId = shortcutId;
2897 return this;
2898 }
2899
2900 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002901 * Sets which icon to display as a badge for this notification.
2902 *
2903 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2904 * {@link #BADGE_ICON_LARGE}.
2905 *
2906 * Note: This value might be ignored, for launchers that don't support badge icons.
2907 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002908 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002909 mN.mBadgeIcon = icon;
2910 return this;
2911 }
2912
2913 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002914 * Sets the group alert behavior for this notification. Use this method to mute this
2915 * notification if alerts for this notification's group should be handled by a different
2916 * notification. This is only applicable for notifications that belong to a
2917 * {@link #setGroup(String) group}.
2918 *
2919 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2920 */
2921 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2922 mN.mGroupAlertBehavior = groupAlertBehavior;
2923 return this;
2924 }
2925
Jeff Sharkey000ce802017-04-29 13:13:27 -06002926 /** @removed */
2927 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002928 public Builder setChannel(String channelId) {
2929 mN.mChannelId = channelId;
2930 return this;
2931 }
2932
2933 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002934 * Specifies the channel the notification should be delivered on.
2935 */
2936 public Builder setChannelId(String channelId) {
2937 mN.mChannelId = channelId;
2938 return this;
2939 }
2940
Jeff Sharkey000ce802017-04-29 13:13:27 -06002941 /** @removed */
2942 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002943 public Builder setTimeout(long durationMs) {
2944 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002945 return this;
2946 }
2947
2948 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002949 * Specifies a duration in milliseconds after which this notification should be canceled,
2950 * if it is not already canceled.
2951 */
2952 public Builder setTimeoutAfter(long durationMs) {
2953 mN.mTimeout = durationMs;
2954 return this;
2955 }
2956
2957 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002958 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002959 *
2960 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2961 * shown anymore by default and must be opted into by using
2962 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002963 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002964 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002965 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002966 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002967 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002968 return this;
2969 }
2970
Joe Onoratocb109a02011-01-18 17:57:41 -08002971 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002972 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002973 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002974 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2975 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002976 */
2977 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002978 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002979 return this;
2980 }
2981
2982 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002983 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002984 *
2985 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002986 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002987 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002988 * Useful when showing an elapsed time (like an ongoing phone call).
2989 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002990 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002991 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002992 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002993 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002994 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002995 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002996 */
2997 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002998 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002999 return this;
3000 }
3001
3002 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003003 * Sets the Chronometer to count down instead of counting up.
3004 *
3005 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3006 * If it isn't set the chronometer will count up.
3007 *
3008 * @see #setUsesChronometer(boolean)
3009 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003010 public Builder setChronometerCountDown(boolean countDown) {
3011 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003012 return this;
3013 }
3014
3015 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003016 * Set the small icon resource, which will be used to represent the notification in the
3017 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003018 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003019
3020 * The platform template for the expanded view will draw this icon in the left, unless a
3021 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3022 * icon will be moved to the right-hand side.
3023 *
3024
3025 * @param icon
3026 * A resource ID in the application's package of the drawable to use.
3027 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003028 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003029 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003030 return setSmallIcon(icon != 0
3031 ? Icon.createWithResource(mContext, icon)
3032 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003033 }
3034
Joe Onoratocb109a02011-01-18 17:57:41 -08003035 /**
3036 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3037 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3038 * LevelListDrawable}.
3039 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003040 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003041 * @param level The level to use for the icon.
3042 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003043 * @see Notification#icon
3044 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003045 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003046 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003047 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003048 return setSmallIcon(icon);
3049 }
3050
3051 /**
3052 * Set the small icon, which will be used to represent the notification in the
3053 * status bar and content view (unless overriden there by a
3054 * {@link #setLargeIcon(Bitmap) large icon}).
3055 *
3056 * @param icon An Icon object to use.
3057 * @see Notification#icon
3058 */
3059 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003060 mN.setSmallIcon(icon);
3061 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3062 mN.icon = icon.getResId();
3063 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 return this;
3065 }
3066
Joe Onoratocb109a02011-01-18 17:57:41 -08003067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003068 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003069 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003070 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003071 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003072 return this;
3073 }
3074
Joe Onoratocb109a02011-01-18 17:57:41 -08003075 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003076 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003077 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003078 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003079 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003080 return this;
3081 }
3082
Joe Onoratocb109a02011-01-18 17:57:41 -08003083 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003084 * This provides some additional information that is displayed in the notification. No
3085 * guarantees are given where exactly it is displayed.
3086 *
3087 * <p>This information should only be provided if it provides an essential
3088 * benefit to the understanding of the notification. The more text you provide the
3089 * less readable it becomes. For example, an email client should only provide the account
3090 * name here if more than one email account has been added.</p>
3091 *
3092 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3093 * notification header area.
3094 *
3095 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3096 * this will be shown in the third line of text in the platform notification template.
3097 * You should not be using {@link #setProgress(int, int, boolean)} at the
3098 * same time on those versions; they occupy the same place.
3099 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003100 */
3101 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003102 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003103 return this;
3104 }
3105
3106 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003107 * Provides text that will appear as a link to your application's settings.
3108 *
3109 * <p>This text does not appear within notification {@link Style templates} but may
3110 * appear when the user uses an affordance to learn more about the notification.
3111 * Additionally, this text will not appear unless you provide a valid link target by
3112 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3113 *
3114 * <p>This text is meant to be concise description about what the user can customize
3115 * when they click on this link. The recommended maximum length is 40 characters.
3116 * @param text
3117 * @return
3118 */
3119 public Builder setSettingsText(CharSequence text) {
3120 mN.mSettingsText = safeCharSequence(text);
3121 return this;
3122 }
3123
3124 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003125 * Set the remote input history.
3126 *
3127 * This should be set to the most recent inputs that have been sent
3128 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3129 * longer relevant (e.g. for chat notifications once the other party has responded).
3130 *
3131 * The most recent input must be stored at the 0 index, the second most recent at the
3132 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3133 * and how much of each individual input is shown.
3134 *
3135 * <p>Note: The reply text will only be shown on notifications that have least one action
3136 * with a {@code RemoteInput}.</p>
3137 */
3138 public Builder setRemoteInputHistory(CharSequence[] text) {
3139 if (text == null) {
3140 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3141 } else {
3142 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3143 CharSequence[] safe = new CharSequence[N];
3144 for (int i = 0; i < N; i++) {
3145 safe[i] = safeCharSequence(text[i]);
3146 }
3147 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3148 }
3149 return this;
3150 }
3151
3152 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003153 * Sets the number of items this notification represents. May be displayed as a badge count
3154 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003155 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003156 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003157 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003158 return this;
3159 }
3160
Joe Onoratocb109a02011-01-18 17:57:41 -08003161 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003162 * A small piece of additional information pertaining to this notification.
3163 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003164 * The platform template will draw this on the last line of the notification, at the far
3165 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003166 *
3167 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3168 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3169 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003170 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003171 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003172 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003173 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003174 return this;
3175 }
3176
Joe Onoratocb109a02011-01-18 17:57:41 -08003177 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003178 * Set the progress this notification represents.
3179 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003180 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003181 */
3182 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003183 mN.extras.putInt(EXTRA_PROGRESS, progress);
3184 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3185 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003186 return this;
3187 }
3188
3189 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003190 * Supply a custom RemoteViews to use instead of the platform template.
3191 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003192 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003193 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003194 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003195 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003196 return setCustomContentView(views);
3197 }
3198
3199 /**
3200 * Supply custom RemoteViews to use instead of the platform template.
3201 *
3202 * This will override the layout that would otherwise be constructed by this Builder
3203 * object.
3204 */
3205 public Builder setCustomContentView(RemoteViews contentView) {
3206 mN.contentView = contentView;
3207 return this;
3208 }
3209
3210 /**
3211 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3212 *
3213 * This will override the expanded layout that would otherwise be constructed by this
3214 * Builder object.
3215 */
3216 public Builder setCustomBigContentView(RemoteViews contentView) {
3217 mN.bigContentView = contentView;
3218 return this;
3219 }
3220
3221 /**
3222 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3223 *
3224 * This will override the heads-up layout that would otherwise be constructed by this
3225 * Builder object.
3226 */
3227 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3228 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003229 return this;
3230 }
3231
Joe Onoratocb109a02011-01-18 17:57:41 -08003232 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003233 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3234 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3236 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3237 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003238 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003239 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003240 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003241 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003242 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003243 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003244 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003245 return this;
3246 }
3247
Joe Onoratocb109a02011-01-18 17:57:41 -08003248 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003249 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3250 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003251 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003252 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003253 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003254 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003255 return this;
3256 }
3257
Joe Onoratocb109a02011-01-18 17:57:41 -08003258 /**
3259 * An intent to launch instead of posting the notification to the status bar.
3260 * Only for use with extremely high-priority notifications demanding the user's
3261 * <strong>immediate</strong> attention, such as an incoming phone call or
3262 * alarm clock that the user has explicitly set to a particular time.
3263 * If this facility is used for something else, please give the user an option
3264 * to turn it off and use a normal notification, as this can be extremely
3265 * disruptive.
3266 *
Chris Wren47c20a12014-06-18 17:27:29 -04003267 * <p>
3268 * The system UI may choose to display a heads-up notification, instead of
3269 * launching this intent, while the user is using the device.
3270 * </p>
3271 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003272 * @param intent The pending intent to launch.
3273 * @param highPriority Passing true will cause this notification to be sent
3274 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003275 *
3276 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003277 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003278 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003279 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003280 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3281 return this;
3282 }
3283
Joe Onoratocb109a02011-01-18 17:57:41 -08003284 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003285 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003287 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 return this;
3292 }
3293
Joe Onoratocb109a02011-01-18 17:57:41 -08003294 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003295 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003296 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003297 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003298 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003299 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003300 setTicker(tickerText);
3301 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003302 return this;
3303 }
3304
Joe Onoratocb109a02011-01-18 17:57:41 -08003305 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003306 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003307 *
3308 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003309 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3310 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003311 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003312 public Builder setLargeIcon(Bitmap b) {
3313 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3314 }
3315
3316 /**
3317 * Add a large icon to the notification content view.
3318 *
3319 * In the platform template, this image will be shown on the left of the notification view
3320 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3321 * badge atop the large icon).
3322 */
3323 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003324 mN.mLargeIcon = icon;
3325 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003326 return this;
3327 }
3328
Joe Onoratocb109a02011-01-18 17:57:41 -08003329 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003330 * Set the sound to play.
3331 *
John Spurlockc0650f022014-07-19 13:22:39 -04003332 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3333 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003334 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003335 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003336 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003337 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003338 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003339 mN.sound = sound;
3340 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003341 return this;
3342 }
3343
Joe Onoratocb109a02011-01-18 17:57:41 -08003344 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003345 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003346 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003347 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3348 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003349 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003350 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003351 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003352 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003353 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003354 mN.sound = sound;
3355 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003356 return this;
3357 }
3358
Joe Onoratocb109a02011-01-18 17:57:41 -08003359 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003360 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3361 * use during playback.
3362 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003363 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003364 * @see Notification#sound
3365 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003366 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003367 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003368 mN.sound = sound;
3369 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003370 return this;
3371 }
3372
3373 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003374 * Set the vibration pattern to use.
3375 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003376 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3377 * <code>pattern</code> parameter.
3378 *
Chris Wren47c20a12014-06-18 17:27:29 -04003379 * <p>
3380 * A notification that vibrates is more likely to be presented as a heads-up notification.
3381 * </p>
3382 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003383 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003384 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003385 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003386 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003387 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003388 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003389 return this;
3390 }
3391
Joe Onoratocb109a02011-01-18 17:57:41 -08003392 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003393 * Set the desired color for the indicator LED on the device, as well as the
3394 * blink duty cycle (specified in milliseconds).
3395 *
3396
3397 * Not all devices will honor all (or even any) of these values.
3398 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003399 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003400 * @see Notification#ledARGB
3401 * @see Notification#ledOnMS
3402 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003403 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003404 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003405 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003406 mN.ledARGB = argb;
3407 mN.ledOnMS = onMs;
3408 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003409 if (onMs != 0 || offMs != 0) {
3410 mN.flags |= FLAG_SHOW_LIGHTS;
3411 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003412 return this;
3413 }
3414
Joe Onoratocb109a02011-01-18 17:57:41 -08003415 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003416 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003417 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003418
3419 * Ongoing notifications cannot be dismissed by the user, so your application or service
3420 * must take care of canceling them.
3421 *
3422
3423 * They are typically used to indicate a background task that the user is actively engaged
3424 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3425 * (e.g., a file download, sync operation, active network connection).
3426 *
3427
3428 * @see Notification#FLAG_ONGOING_EVENT
3429 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003430 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003431 public Builder setOngoing(boolean ongoing) {
3432 setFlag(FLAG_ONGOING_EVENT, ongoing);
3433 return this;
3434 }
3435
Joe Onoratocb109a02011-01-18 17:57:41 -08003436 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003437 * Set whether this notification should be colorized. When set, the color set with
3438 * {@link #setColor(int)} will be used as the background color of this notification.
3439 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003440 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3441 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003442 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003443 * For most styles, the coloring will only be applied if the notification is for a
3444 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003445 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003446 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003447 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003448 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003449 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003450 */
3451 public Builder setColorized(boolean colorize) {
3452 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3453 return this;
3454 }
3455
3456 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003457 * Set this flag if you would only like the sound, vibrate
3458 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003459 *
3460 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003462 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3463 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3464 return this;
3465 }
3466
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003468 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 *
3470 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003473 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003478 * Set whether or not this notification should not bridge to other devices.
3479 *
3480 * <p>Some notifications can be bridged to other devices for remote display.
3481 * This hint can be set to recommend this notification not be bridged.
3482 */
3483 public Builder setLocalOnly(boolean localOnly) {
3484 setFlag(FLAG_LOCAL_ONLY, localOnly);
3485 return this;
3486 }
3487
3488 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003489 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003490 * <p>
3491 * The value should be one or more of the following fields combined with
3492 * bitwise-or:
3493 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3494 * <p>
3495 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003496 *
3497 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003498 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003499 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003501 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003502 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003503 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003504 return this;
3505 }
3506
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003507 /**
3508 * Set the priority of this notification.
3509 *
3510 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003511 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003512 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003513 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003514 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003515 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003516 return this;
3517 }
Joe Malin8d40d042012-11-05 11:36:40 -08003518
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003519 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003520 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003521 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003522 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003523 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003524 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003525 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 return this;
3527 }
3528
3529 /**
Chris Wrendde75302014-03-26 17:24:15 -04003530 * Add a person that is relevant to this notification.
3531 *
Chris Wrene6c48932014-09-29 17:19:27 -04003532 * <P>
3533 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003534 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3535 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3536 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003537 * </P>
3538 *
3539 * <P>
3540 * The person should be specified by the {@code String} representation of a
3541 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3542 * </P>
3543 *
3544 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3545 * URIs. The path part of these URIs must exist in the contacts database, in the
3546 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3547 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3548 * </P>
3549 *
3550 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003551 * @see Notification#EXTRA_PEOPLE
3552 */
Chris Wrene6c48932014-09-29 17:19:27 -04003553 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003554 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003555 return this;
3556 }
3557
3558 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003559 * Set this notification to be part of a group of notifications sharing the same key.
3560 * Grouped notifications may display in a cluster or stack on devices which
3561 * support such rendering.
3562 *
3563 * <p>To make this notification the summary for its group, also call
3564 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3565 * {@link #setSortKey}.
3566 * @param groupKey The group key of the group.
3567 * @return this object for method chaining
3568 */
3569 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003570 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003571 return this;
3572 }
3573
3574 /**
3575 * Set this notification to be the group summary for a group of notifications.
3576 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003577 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3578 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003579 * @param isGroupSummary Whether this notification should be a group summary.
3580 * @return this object for method chaining
3581 */
3582 public Builder setGroupSummary(boolean isGroupSummary) {
3583 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3584 return this;
3585 }
3586
3587 /**
3588 * Set a sort key that orders this notification among other notifications from the
3589 * same package. This can be useful if an external sort was already applied and an app
3590 * would like to preserve this. Notifications will be sorted lexicographically using this
3591 * value, although providing different priorities in addition to providing sort key may
3592 * cause this value to be ignored.
3593 *
3594 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003595 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003596 *
3597 * @see String#compareTo(String)
3598 */
3599 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003600 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003601 return this;
3602 }
3603
3604 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003605 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003606 *
Griff Hazen720042b2014-02-24 15:46:56 -08003607 * <p>Values within the Bundle will replace existing extras values in this Builder.
3608 *
3609 * @see Notification#extras
3610 */
Griff Hazen959591e2014-05-15 22:26:18 -07003611 public Builder addExtras(Bundle extras) {
3612 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003613 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003614 }
3615 return this;
3616 }
3617
3618 /**
3619 * Set metadata for this notification.
3620 *
3621 * <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 -04003622 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003623 * called.
3624 *
Griff Hazen720042b2014-02-24 15:46:56 -08003625 * <p>Replaces any existing extras values with those from the provided Bundle.
3626 * Use {@link #addExtras} to merge in metadata instead.
3627 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003628 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003629 */
Griff Hazen959591e2014-05-15 22:26:18 -07003630 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003631 if (extras != null) {
3632 mUserExtras = extras;
3633 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003634 return this;
3635 }
3636
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003637 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003638 * Get the current metadata Bundle used by this notification Builder.
3639 *
3640 * <p>The returned Bundle is shared with this Builder.
3641 *
3642 * <p>The current contents of this Bundle are copied into the Notification each time
3643 * {@link #build()} is called.
3644 *
3645 * @see Notification#extras
3646 */
3647 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003648 return mUserExtras;
3649 }
3650
3651 private Bundle getAllExtras() {
3652 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3653 saveExtras.putAll(mN.extras);
3654 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003655 }
3656
3657 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003658 * Add an action to this notification. Actions are typically displayed by
3659 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003660 * <p>
3661 * Every action must have an icon (32dp square and matching the
3662 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3663 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3664 * <p>
3665 * A notification in its expanded form can display up to 3 actions, from left to right in
3666 * the order they were added. Actions will not be displayed when the notification is
3667 * collapsed, however, so be sure that any essential functions may be accessed by the user
3668 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003669 *
3670 * @param icon Resource ID of a drawable that represents the action.
3671 * @param title Text describing the action.
3672 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003673 *
3674 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003675 */
Dan Sandler86647982015-05-13 23:41:13 -04003676 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003677 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003678 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003679 return this;
3680 }
3681
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003682 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003683 * Add an action to this notification. Actions are typically displayed by
3684 * the system as a button adjacent to the notification content.
3685 * <p>
3686 * Every action must have an icon (32dp square and matching the
3687 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3688 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3689 * <p>
3690 * A notification in its expanded form can display up to 3 actions, from left to right in
3691 * the order they were added. Actions will not be displayed when the notification is
3692 * collapsed, however, so be sure that any essential functions may be accessed by the user
3693 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3694 *
3695 * @param action The action to add.
3696 */
3697 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003698 if (action != null) {
3699 mActions.add(action);
3700 }
Griff Hazen959591e2014-05-15 22:26:18 -07003701 return this;
3702 }
3703
3704 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003705 * Alter the complete list of actions attached to this notification.
3706 * @see #addAction(Action).
3707 *
3708 * @param actions
3709 * @return
3710 */
3711 public Builder setActions(Action... actions) {
3712 mActions.clear();
3713 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003714 if (actions[i] != null) {
3715 mActions.add(actions[i]);
3716 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003717 }
3718 return this;
3719 }
3720
3721 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003722 * Add a rich notification style to be applied at build time.
3723 *
3724 * @param style Object responsible for modifying the notification style.
3725 */
3726 public Builder setStyle(Style style) {
3727 if (mStyle != style) {
3728 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003729 if (mStyle != null) {
3730 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003731 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3732 } else {
3733 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003734 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003735 }
3736 return this;
3737 }
3738
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003739 /**
3740 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003741 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003742 * @return The same Builder.
3743 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003744 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003745 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003746 return this;
3747 }
3748
3749 /**
3750 * Supply a replacement Notification whose contents should be shown in insecure contexts
3751 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3752 * @param n A replacement notification, presumably with some or all info redacted.
3753 * @return The same Builder.
3754 */
3755 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003756 if (n != null) {
3757 mN.publicVersion = new Notification();
3758 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3759 } else {
3760 mN.publicVersion = null;
3761 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003762 return this;
3763 }
3764
Griff Hazenb720abe2014-05-20 13:15:30 -07003765 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003766 * Apply an extender to this notification builder. Extenders may be used to add
3767 * metadata or change options on this builder.
3768 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003769 public Builder extend(Extender extender) {
3770 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003771 return this;
3772 }
3773
Dan Sandler4e787062015-06-17 15:09:48 -04003774 /**
3775 * @hide
3776 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003777 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003778 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003780 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003781 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003782 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003783 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003784 }
3785
Dan Sandler26e81cf2014-05-06 10:01:27 -04003786 /**
3787 * Sets {@link Notification#color}.
3788 *
3789 * @param argb The accent color to use
3790 *
3791 * @return The same Builder.
3792 */
Tor Norbye80756e32015-03-02 09:39:27 -08003793 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003794 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003795 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003796 return this;
3797 }
3798
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003799 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003800 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3801 // This user can never be a badged profile,
3802 // and also includes USER_ALL system notifications.
3803 return null;
3804 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003805 // Note: This assumes that the current user can read the profile badge of the
3806 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003807 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003808 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003809 }
3810
3811 private Bitmap getProfileBadge() {
3812 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003813 if (badge == null) {
3814 return null;
3815 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003816 final int size = mContext.getResources().getDimensionPixelSize(
3817 R.dimen.notification_badge_size);
3818 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003819 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003820 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003821 badge.draw(canvas);
3822 return bitmap;
3823 }
3824
Selim Cinekc848c3a2016-01-13 15:27:30 -08003825 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003826 Bitmap profileBadge = getProfileBadge();
3827
Kenny Guy98193ea2014-07-24 19:54:37 +01003828 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003829 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3830 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003831 if (isColorized()) {
3832 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3833 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3834 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003835 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003836 }
3837
Christoph Studerfe718432014-09-01 18:21:18 +02003838 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003839 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003840 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003841 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003842 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003843 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003844 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003845 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003846 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003847 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003848 }
3849
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003850 /**
3851 * Resets the notification header to its original state
3852 */
3853 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003854 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3855 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003856 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003857 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003858 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003859 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003860 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003861 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003862 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003863 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003864 contentView.setImageViewIcon(R.id.profile_badge, null);
3865 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003866 }
3867
3868 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003869 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3870 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003871 }
3872
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003873 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003874 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003875 }
3876
3877 /**
3878 * @param hasProgress whether the progress bar should be shown and set
3879 */
3880 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003881 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3882 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003883 }
3884
Adrian Roos70d7aa32017-01-11 15:39:06 -08003885 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003886 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003887
Christoph Studerfe718432014-09-01 18:21:18 +02003888 resetStandardTemplate(contentView);
3889
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003890 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003891 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003892 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003893 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003894 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3895 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003896 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003897 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003898 if (!p.ambient) {
3899 setTextViewColorPrimary(contentView, R.id.title);
3900 }
Selim Cinek954cc232016-05-20 13:29:23 -07003901 contentView.setViewLayoutWidth(R.id.title, showProgress
3902 ? ViewGroup.LayoutParams.WRAP_CONTENT
3903 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003904 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003905 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003906 int textId = showProgress ? com.android.internal.R.id.text_line_1
3907 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003908 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003909 if (!p.ambient) {
3910 setTextViewColorSecondary(contentView, textId);
3911 }
Selim Cinek41598732016-01-11 16:58:37 -08003912 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003913 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003914
Selim Cinek279fa862016-06-14 10:57:25 -07003915 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003916
Selim Cinek29603462015-11-17 19:04:39 -08003917 return contentView;
3918 }
3919
Selim Cinek7b9605b2017-01-19 17:36:00 -08003920 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3921 ensureColors();
3922 contentView.setTextColor(id, mPrimaryTextColor);
3923 }
3924
Selim Cinek389edcd2017-05-11 19:16:44 -07003925 /**
3926 * @return the primary text color
3927 * @hide
3928 */
3929 @VisibleForTesting
3930 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003931 ensureColors();
3932 return mPrimaryTextColor;
3933 }
3934
Selim Cinek389edcd2017-05-11 19:16:44 -07003935 /**
3936 * @return the secondary text color
3937 * @hide
3938 */
3939 @VisibleForTesting
3940 public int getSecondaryTextColor() {
3941 ensureColors();
3942 return mSecondaryTextColor;
3943 }
3944
Selim Cinek7b9605b2017-01-19 17:36:00 -08003945 private int getActionBarColor() {
3946 ensureColors();
3947 return mActionBarColor;
3948 }
3949
Selim Cinek622c64a2017-04-17 17:10:05 -07003950 private int getActionBarColorDeEmphasized() {
3951 int backgroundColor = getBackgroundColor();
3952 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3953 }
3954
Selim Cinek7b9605b2017-01-19 17:36:00 -08003955 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3956 ensureColors();
3957 contentView.setTextColor(id, mSecondaryTextColor);
3958 }
3959
3960 private void ensureColors() {
3961 int backgroundColor = getBackgroundColor();
3962 if (mPrimaryTextColor == COLOR_INVALID
3963 || mSecondaryTextColor == COLOR_INVALID
3964 || mActionBarColor == COLOR_INVALID
3965 || mTextColorsAreForBackground != backgroundColor) {
3966 mTextColorsAreForBackground = backgroundColor;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003967 if (mForegroundColor == COLOR_INVALID || !isColorized()) {
3968 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3969 backgroundColor);
3970 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3971 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003972 if (backgroundColor != COLOR_DEFAULT
3973 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3974 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3975 mPrimaryTextColor, backgroundColor, 4.5);
3976 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3977 mSecondaryTextColor, backgroundColor, 4.5);
3978 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07003979 } else {
3980 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
3981 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
3982 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
3983 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07003984 // We only respect the given colors if worst case Black or White still has
3985 // contrast
3986 boolean backgroundLight = backLum > textLum
3987 && satisfiesTextContrast(backgroundColor, Color.BLACK)
3988 || backLum <= textLum
3989 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003990 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07003991 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003992 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
3993 mForegroundColor,
3994 backgroundColor,
3995 true /* findFG */,
3996 4.5f);
3997 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07003998 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07003999 } else {
4000 mSecondaryTextColor =
4001 NotificationColorUtil.findContrastColorAgainstDark(
4002 mForegroundColor,
4003 backgroundColor,
4004 true /* findFG */,
4005 4.5f);
4006 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004007 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004008 }
4009 } else {
4010 mPrimaryTextColor = mForegroundColor;
4011 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004012 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4013 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004014 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4015 backgroundColor) < 4.5f) {
4016 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004017 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004018 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4019 mSecondaryTextColor,
4020 backgroundColor,
4021 true /* findFG */,
4022 4.5f);
4023 } else {
4024 mSecondaryTextColor
4025 = NotificationColorUtil.findContrastColorAgainstDark(
4026 mSecondaryTextColor,
4027 backgroundColor,
4028 true /* findFG */,
4029 4.5f);
4030 }
4031 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004032 mSecondaryTextColor, backgroundLight
4033 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4034 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004035 }
4036 }
4037 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004038 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4039 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004040 }
4041 }
4042
4043 private void updateBackgroundColor(RemoteViews contentView) {
4044 if (isColorized()) {
4045 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4046 getBackgroundColor());
4047 } else {
4048 // Clear it!
4049 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4050 0);
4051 }
4052 }
4053
Selim Cinek860b6da2015-12-16 19:02:19 -08004054 /**
4055 * @param remoteView the remote view to update the minheight in
4056 * @param hasMinHeight does it have a mimHeight
4057 * @hide
4058 */
4059 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4060 int minHeight = 0;
4061 if (hasMinHeight) {
4062 // we need to set the minHeight of the notification
4063 minHeight = mContext.getResources().getDimensionPixelSize(
4064 com.android.internal.R.dimen.notification_min_content_height);
4065 }
4066 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4067 }
4068
Selim Cinek29603462015-11-17 19:04:39 -08004069 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004070 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4071 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4072 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4073 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004074 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004075 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004076 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004077 contentView.setProgressBackgroundTintList(
4078 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4079 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004080 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004081 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004082 contentView.setProgressTintList(R.id.progress, colorStateList);
4083 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004084 }
Selim Cinek29603462015-11-17 19:04:39 -08004085 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004086 } else {
4087 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004088 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004089 }
Joe Onorato561d3852010-11-20 18:09:34 -08004090 }
4091
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004092 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07004093 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4094 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4095 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004096 if (mN.mLargeIcon != null) {
4097 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4098 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4099 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004100 int endMargin = R.dimen.notification_content_picture_margin;
4101 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4102 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4103 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004104 // Bind the reply action
4105 Action action = findReplyAction();
4106 contentView.setViewVisibility(R.id.reply_icon_action, action != null
4107 ? View.VISIBLE
4108 : View.GONE);
4109
4110 if (action != null) {
4111 int contrastColor = resolveContrastColor();
4112 contentView.setDrawableParameters(R.id.reply_icon_action,
4113 true /* targetBackground */,
4114 -1,
4115 contrastColor,
4116 PorterDuff.Mode.SRC_ATOP, -1);
4117 int iconColor = NotificationColorUtil.isColorLight(contrastColor)
4118 ? Color.BLACK : Color.WHITE;
4119 contentView.setDrawableParameters(R.id.reply_icon_action,
4120 false /* targetBackground */,
4121 -1,
4122 iconColor,
4123 PorterDuff.Mode.SRC_ATOP, -1);
4124 contentView.setOnClickPendingIntent(R.id.right_icon,
4125 action.actionIntent);
4126 contentView.setOnClickPendingIntent(R.id.reply_icon_action,
4127 action.actionIntent);
4128 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
4129 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4130
4131 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004132 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004133 contentView.setViewVisibility(R.id.right_icon_container, mN.mLargeIcon != null
4134 ? View.VISIBLE
4135 : View.GONE);
4136 }
4137
4138 private Action findReplyAction() {
4139 ArrayList<Action> actions = mActions;
4140 if (mOriginalActions != null) {
4141 actions = mOriginalActions;
4142 }
4143 int numActions = actions.size();
4144 for (int i = 0; i < numActions; i++) {
4145 Action action = actions.get(i);
4146 if (hasValidRemoteInput(action)) {
4147 return action;
4148 }
4149 }
4150 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004151 }
4152
Adrian Roos487374f2017-01-11 15:48:14 -08004153 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4154 bindSmallIcon(contentView, ambient);
4155 bindHeaderAppName(contentView, ambient);
4156 if (!ambient) {
4157 // Ambient view does not have these
4158 bindHeaderText(contentView);
4159 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004160 bindProfileBadge(contentView);
4161 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004162 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004163 }
4164
4165 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004166 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08004167 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004168 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004169 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004170 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004171 }
4172
Selim Cinek99104832017-01-25 14:47:33 -08004173 /**
4174 * @return the color that is used as the first primary highlight color. This is applied
4175 * in several places like the action buttons or the app name in the header.
4176 */
4177 private int getPrimaryHighlightColor() {
4178 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4179 }
4180
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004181 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4182 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004183 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004184 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004185 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4186 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4187 contentView.setLong(R.id.chronometer, "setBase",
4188 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4189 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004190 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004191 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004192 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004193 } else {
4194 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4195 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004196 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004197 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004198 } else {
4199 // We still want a time to be set but gone, such that we can show and hide it
4200 // on demand in case it's a child notification without anything in the header
4201 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004202 }
4203 }
4204
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004205 private void bindHeaderText(RemoteViews contentView) {
4206 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4207 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004208 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004209 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004210 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004211 if (headerText == null
4212 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4213 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4214 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4215 }
4216 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004217 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004218 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004219 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004220 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4221 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004222 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004223 }
4224 }
4225
Adrian Rooseba05822016-04-22 17:09:27 -07004226 /**
4227 * @hide
4228 */
4229 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004230 CharSequence name = null;
4231 final PackageManager pm = mContext.getPackageManager();
4232 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4233 // only system packages which lump together a bunch of unrelated stuff
4234 // may substitute a different name to make the purpose of the
4235 // notification more clear. the correct package label should always
4236 // be accessible via SystemUI.
4237 final String pkg = mContext.getPackageName();
4238 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4239 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4240 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4241 name = subName;
4242 } else {
4243 Log.w(TAG, "warning: pkg "
4244 + pkg + " attempting to substitute app name '" + subName
4245 + "' without holding perm "
4246 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4247 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004248 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004249 if (TextUtils.isEmpty(name)) {
4250 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4251 }
4252 if (TextUtils.isEmpty(name)) {
4253 // still nothing?
4254 return null;
4255 }
4256
4257 return String.valueOf(name);
4258 }
Adrian Roos487374f2017-01-11 15:48:14 -08004259 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004260 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004261 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004262 setTextViewColorPrimary(contentView, R.id.app_name_text);
4263 } else {
4264 contentView.setTextColor(R.id.app_name_text,
4265 ambient ? resolveAmbientColor() : resolveContrastColor());
4266 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004267 }
4268
Adrian Roos487374f2017-01-11 15:48:14 -08004269 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004270 if (mN.mSmallIcon == null && mN.icon != 0) {
4271 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4272 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004273 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07004274 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
4275 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004276 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004277 }
4278
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004279 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004280 * @return true if the built notification will show the time or the chronometer; false
4281 * otherwise
4282 */
4283 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004284 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004285 }
4286
Christoph Studerfe718432014-09-01 18:21:18 +02004287 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004288 // actions_container is only reset when there are no actions to avoid focus issues with
4289 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004290 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004291 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004292
4293 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4294 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4295
4296 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4297 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4298 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4299 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004300
4301 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004302 }
4303
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004304 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004305 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004306 }
4307
Adrian Roos70d7aa32017-01-11 15:39:06 -08004308 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4309 StandardTemplateParams p) {
4310 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004311
Christoph Studerfe718432014-09-01 18:21:18 +02004312 resetStandardTemplateWithActions(big);
4313
Adrian Roose458aa82015-12-08 16:17:19 -08004314 boolean validRemoteInput = false;
4315
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004316 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004317 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004318 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004319 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004320 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004321 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004322 if (p.ambient) {
4323 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004324 } else if (isColorized()) {
4325 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4326 } else {
4327 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4328 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004329 }
Adrian Roosf852a422016-06-03 13:33:43 -07004330 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4331 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004332 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004333 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004334 Action action = mActions.get(i);
4335 validRemoteInput |= hasValidRemoteInput(action);
4336
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004337 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004338 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004339 big.addView(R.id.actions, button);
4340 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004341 } else {
4342 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004343 }
Adrian Roose458aa82015-12-08 16:17:19 -08004344
4345 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004346 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004347 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4348 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4349 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004350 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004351
4352 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4353 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4354 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004355 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004356
4357 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4358 big.setViewVisibility(
4359 R.id.notification_material_reply_text_3, View.VISIBLE);
4360 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004361 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004362 }
4363 }
4364 }
4365
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004366 return big;
4367 }
4368
Adrian Roose458aa82015-12-08 16:17:19 -08004369 private boolean hasValidRemoteInput(Action action) {
4370 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4371 // Weird actions
4372 return false;
4373 }
4374
4375 RemoteInput[] remoteInputs = action.getRemoteInputs();
4376 if (remoteInputs == null) {
4377 return false;
4378 }
4379
4380 for (RemoteInput r : remoteInputs) {
4381 CharSequence[] choices = r.getChoices();
4382 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4383 return true;
4384 }
4385 }
4386 return false;
4387 }
4388
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004389 /**
4390 * Construct a RemoteViews for the final 1U notification layout. In order:
4391 * 1. Custom contentView from the caller
4392 * 2. Style's proposed content view
4393 * 3. Standard template view
4394 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004395 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004396 return createContentView(false /* increasedheight */ );
4397 }
4398
4399 /**
4400 * Construct a RemoteViews for the smaller content view.
4401 *
4402 * @param increasedHeight true if this layout be created with an increased height. Some
4403 * styles may support showing more then just that basic 1U size
4404 * and the system may decide to render important notifications
4405 * slightly bigger even when collapsed.
4406 *
4407 * @hide
4408 */
4409 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004410 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004411 return mN.contentView;
4412 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004413 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004414 if (styleView != null) {
4415 return styleView;
4416 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004417 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004418 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004419 }
4420
Selim Cineka7679b62017-05-10 16:33:25 -07004421 private boolean useExistingRemoteView() {
4422 return mStyle == null || (!mStyle.displayCustomViewInline()
4423 && !mRebuildStyledRemoteViews);
4424 }
4425
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004426 /**
4427 * Construct a RemoteViews for the final big notification layout.
4428 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004429 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004430 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004431 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004432 return mN.bigContentView;
4433 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004434 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004435 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004436 } else if (mActions.size() != 0) {
4437 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004438 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004439 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004440 return result;
4441 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004442
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004443 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004444 * Construct a RemoteViews for the final notification header only. This will not be
4445 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004446 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004447 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004448 * @hide
4449 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004450 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004451 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4452 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004453 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004454 ambient ? R.layout.notification_template_ambient_header
4455 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004456 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004457 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004458 if (colorized != null) {
4459 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4460 } else {
4461 mN.extras.remove(EXTRA_COLORIZED);
4462 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004463 return header;
4464 }
4465
Adrian Roos487374f2017-01-11 15:48:14 -08004466 /**
4467 * Construct a RemoteViews for the ambient version of the notification.
4468 *
4469 * @hide
4470 */
4471 public RemoteViews makeAmbientNotification() {
4472 RemoteViews ambient = applyStandardTemplateWithActions(
4473 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004474 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004475 return ambient;
4476 }
4477
Selim Cinek29603462015-11-17 19:04:39 -08004478 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004479 if (result != null) {
4480 result.setViewVisibility(R.id.text_line_1, View.GONE);
4481 }
Selim Cinek29603462015-11-17 19:04:39 -08004482 }
4483
Selim Cinek6743c0b2017-01-18 18:24:01 -08004484 /**
4485 * Adapt the Notification header if this view is used as an expanded view.
4486 *
4487 * @hide
4488 */
4489 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004490 if (result != null) {
4491 result.setBoolean(R.id.notification_header, "setExpanded", true);
4492 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004493 }
4494
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004495 /**
4496 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004497 *
4498 * @param increasedHeight true if this layout be created with an increased height. Some
4499 * styles may support showing more then just that basic 1U size
4500 * and the system may decide to render important notifications
4501 * slightly bigger even when collapsed.
4502 *
4503 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004504 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004505 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004506 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004507 return mN.headsUpContentView;
4508 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004509 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4510 if (styleView != null) {
4511 return styleView;
4512 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004513 } else if (mActions.size() == 0) {
4514 return null;
4515 }
4516
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004517 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004518 }
4519
Selim Cinek624c02db2015-12-14 21:00:02 -08004520 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004521 * Construct a RemoteViews for the final heads-up notification layout.
4522 */
4523 public RemoteViews createHeadsUpContentView() {
4524 return createHeadsUpContentView(false /* useIncreasedHeight */);
4525 }
4526
4527 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004528 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4529 *
4530 * @hide
4531 */
4532 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004533 return makePublicView(false /* ambient */);
4534 }
4535
4536 /**
4537 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4538 *
4539 * @hide
4540 */
4541 public RemoteViews makePublicAmbientNotification() {
4542 return makePublicView(true /* ambient */);
4543 }
4544
4545 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004546 if (mN.publicVersion != null) {
4547 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004548 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004549 }
4550 Bundle savedBundle = mN.extras;
4551 Style style = mStyle;
4552 mStyle = null;
4553 Icon largeIcon = mN.mLargeIcon;
4554 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004555 Bitmap largeIconLegacy = mN.largeIcon;
4556 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004557 ArrayList<Action> actions = mActions;
4558 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004559 Bundle publicExtras = new Bundle();
4560 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4561 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4562 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4563 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004564 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4565 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004566 publicExtras.putCharSequence(EXTRA_TITLE,
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004567 mContext.getString(com.android.internal.R.string.notification_hidden_text));
Selim Cinek624c02db2015-12-14 21:00:02 -08004568 mN.extras = publicExtras;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004569 final RemoteViews view = ambient ? makeAmbientNotification()
4570 : applyStandardTemplate(getBaseLayoutResource());
Selim Cinek624c02db2015-12-14 21:00:02 -08004571 mN.extras = savedBundle;
4572 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004573 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004574 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004575 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004576 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004577 }
4578
Selim Cinek6743c0b2017-01-18 18:24:01 -08004579 /**
4580 * Construct a content view for the display when low - priority
4581 *
4582 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4583 * a new subtext is created consisting of the content of the
4584 * notification.
4585 * @hide
4586 */
4587 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4588 int color = mN.color;
4589 mN.color = COLOR_DEFAULT;
4590 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4591 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4592 CharSequence newSummary = createSummaryText();
4593 if (!TextUtils.isEmpty(newSummary)) {
4594 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4595 }
4596 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004597
Adrian Roos6f6e1592017-05-02 16:22:53 -07004598 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004599 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004600 if (summary != null) {
4601 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4602 } else {
4603 mN.extras.remove(EXTRA_SUB_TEXT);
4604 }
4605 mN.color = color;
4606 return header;
4607 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004608
Selim Cinek6743c0b2017-01-18 18:24:01 -08004609 private CharSequence createSummaryText() {
4610 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4611 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4612 return titleText;
4613 }
4614 SpannableStringBuilder summary = new SpannableStringBuilder();
4615 if (titleText == null) {
4616 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4617 }
4618 BidiFormatter bidi = BidiFormatter.getInstance();
4619 if (titleText != null) {
4620 summary.append(bidi.unicodeWrap(titleText));
4621 }
4622 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4623 if (titleText != null && contentText != null) {
4624 summary.append(bidi.unicodeWrap(mContext.getText(
4625 R.string.notification_header_divider_symbol_with_spaces)));
4626 }
4627 if (contentText != null) {
4628 summary.append(bidi.unicodeWrap(contentText));
4629 }
4630 return summary;
4631 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004632
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004633 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004634 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004635 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004636 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004637 emphazisedMode ? getEmphasizedActionLayoutResource()
4638 : tombstone ? getActionTombstoneLayoutResource()
4639 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004640 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004641 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004642 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004643 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004644 if (action.mRemoteInputs != null) {
4645 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4646 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004647 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004648 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004649 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004650 int bgColor;
4651 if (isColorized()) {
4652 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4653 } else {
4654 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4655 : R.color.notification_action_list_dark);
4656 }
Selim Cinek981962e2016-07-20 20:41:58 -07004657 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004658 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004659 CharSequence title = action.title;
4660 ColorStateList[] outResultColor = null;
4661 if (isLegacy()) {
4662 title = clearColorSpans(title);
4663 } else {
4664 outResultColor = new ColorStateList[1];
4665 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4666 }
4667 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004668 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004669 if (outResultColor != null && outResultColor[0] != null) {
4670 // We need to set the text color as well since changing a text to uppercase
4671 // clears its spans.
4672 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004673 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004674 button.setTextColor(R.id.action0,resolveContrastColor());
4675 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004676 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004677 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004678 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004679 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004680 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004681 button.setTextColor(R.id.action0,
4682 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004683 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004684 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004685 return button;
4686 }
4687
Joe Onoratocb109a02011-01-18 17:57:41 -08004688 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004689 * Clears all color spans of a text
4690 * @param charSequence the input text
4691 * @return the same text but without color spans
4692 */
4693 private CharSequence clearColorSpans(CharSequence charSequence) {
4694 if (charSequence instanceof Spanned) {
4695 Spanned ss = (Spanned) charSequence;
4696 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4697 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4698 for (Object span : spans) {
4699 Object resultSpan = span;
4700 if (resultSpan instanceof CharacterStyle) {
4701 resultSpan = ((CharacterStyle) span).getUnderlying();
4702 }
4703 if (resultSpan instanceof TextAppearanceSpan) {
4704 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4705 if (originalSpan.getTextColor() != null) {
4706 resultSpan = new TextAppearanceSpan(
4707 originalSpan.getFamily(),
4708 originalSpan.getTextStyle(),
4709 originalSpan.getTextSize(),
4710 null,
4711 originalSpan.getLinkTextColor());
4712 }
4713 } else if (resultSpan instanceof ForegroundColorSpan
4714 || (resultSpan instanceof BackgroundColorSpan)) {
4715 continue;
4716 } else {
4717 resultSpan = span;
4718 }
4719 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4720 ss.getSpanFlags(span));
4721 }
4722 return builder;
4723 }
4724 return charSequence;
4725 }
4726
4727 /**
4728 * Ensures contrast on color spans against a background color. also returns the color of the
4729 * text if a span was found that spans over the whole text.
4730 *
4731 * @param charSequence the charSequence on which the spans are
4732 * @param background the background color to ensure the contrast against
4733 * @param outResultColor an array in which a color will be returned as the first element if
4734 * there exists a full length color span.
4735 * @return the contrasted charSequence
4736 */
4737 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4738 ColorStateList[] outResultColor) {
4739 if (charSequence instanceof Spanned) {
4740 Spanned ss = (Spanned) charSequence;
4741 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4742 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4743 for (Object span : spans) {
4744 Object resultSpan = span;
4745 int spanStart = ss.getSpanStart(span);
4746 int spanEnd = ss.getSpanEnd(span);
4747 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4748 if (resultSpan instanceof CharacterStyle) {
4749 resultSpan = ((CharacterStyle) span).getUnderlying();
4750 }
4751 if (resultSpan instanceof TextAppearanceSpan) {
4752 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4753 ColorStateList textColor = originalSpan.getTextColor();
4754 if (textColor != null) {
4755 int[] colors = textColor.getColors();
4756 int[] newColors = new int[colors.length];
4757 for (int i = 0; i < newColors.length; i++) {
4758 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004759 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004760 }
4761 textColor = new ColorStateList(textColor.getStates().clone(),
4762 newColors);
4763 resultSpan = new TextAppearanceSpan(
4764 originalSpan.getFamily(),
4765 originalSpan.getTextStyle(),
4766 originalSpan.getTextSize(),
4767 textColor,
4768 originalSpan.getLinkTextColor());
4769 if (fullLength) {
4770 outResultColor[0] = new ColorStateList(
4771 textColor.getStates().clone(), newColors);
4772 }
4773 }
4774 } else if (resultSpan instanceof ForegroundColorSpan) {
4775 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4776 int foregroundColor = originalSpan.getForegroundColor();
4777 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004778 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004779 resultSpan = new ForegroundColorSpan(foregroundColor);
4780 if (fullLength) {
4781 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4782 }
4783 } else {
4784 resultSpan = span;
4785 }
4786
4787 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4788 }
4789 return builder;
4790 }
4791 return charSequence;
4792 }
4793
4794 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004795 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004796 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004797 */
4798 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004799 if (!mIsLegacyInitialized) {
4800 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4801 < Build.VERSION_CODES.LOLLIPOP;
4802 mIsLegacyInitialized = true;
4803 }
4804 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004805 }
4806
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004807 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004808 return processLegacyText(charSequence, false /* ambient */);
4809 }
4810
4811 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4812 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4813 boolean wantLightText = ambient;
4814 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004815 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004816 } else {
4817 return charSequence;
4818 }
4819 }
4820
Dan Sandler26e81cf2014-05-06 10:01:27 -04004821 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004822 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004823 */
Adrian Roos487374f2017-01-11 15:48:14 -08004824 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4825 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004826 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004827 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004828 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004829 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004830 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004831
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004832 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004833 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004834 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004835 }
4836
Dan Sandler26e81cf2014-05-06 10:01:27 -04004837 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004838 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004839 * if it's grayscale).
4840 */
4841 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004842 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4843 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004844 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004845 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004846 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004847 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004848 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004849 }
4850
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004851 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004852 if (mN.color != COLOR_DEFAULT) {
4853 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004854 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004855 }
4856
Adrian Roos4ff3b122016-02-01 12:26:13 -08004857 int resolveContrastColor() {
4858 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4859 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004860 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004861
Selim Cinekac5f0272017-05-02 16:05:41 -07004862 int color;
4863 int background = mBackgroundColorHint;
4864 if (mBackgroundColorHint == COLOR_INVALID) {
4865 background = mContext.getColor(
4866 com.android.internal.R.color.notification_material_background_color);
4867 }
4868 if (mN.color == COLOR_DEFAULT) {
4869 ensureColors();
4870 color = mSecondaryTextColor;
4871 } else {
4872 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004873 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004874 }
4875 if (Color.alpha(color) < 255) {
4876 // alpha doesn't go well for color filters, so let's blend it manually
4877 color = NotificationColorUtil.compositeColors(color, background);
4878 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004879 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004880 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004881 }
4882
Adrian Roos487374f2017-01-11 15:48:14 -08004883 int resolveAmbientColor() {
4884 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4885 return mCachedAmbientColor;
4886 }
4887 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4888
4889 mCachedAmbientColorIsFor = mN.color;
4890 return mCachedAmbientColor = contrasted;
4891 }
4892
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004893 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004894 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004895 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004896 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004897 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004898 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004899 mN.actions = new Action[mActions.size()];
4900 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004901 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004902 if (!mPersonList.isEmpty()) {
4903 mN.extras.putStringArray(EXTRA_PEOPLE,
4904 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004905 }
Selim Cinek247fa012016-02-18 09:50:48 -08004906 if (mN.bigContentView != null || mN.contentView != null
4907 || mN.headsUpContentView != null) {
4908 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4909 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004910 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004911 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004912
Julia Reynolds3b848122016-02-26 10:45:32 -05004913 /**
4914 * Creates a Builder from an existing notification so further changes can be made.
4915 * @param context The context for your application / activity.
4916 * @param n The notification to create a Builder from.
4917 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004918 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004919 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004920 ApplicationInfo applicationInfo = n.extras.getParcelable(
4921 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004922 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004923 if (applicationInfo != null) {
4924 try {
4925 builderContext = context.createApplicationContext(applicationInfo,
4926 Context.CONTEXT_RESTRICTED);
4927 } catch (NameNotFoundException e) {
4928 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4929 builderContext = context; // try with our context
4930 }
4931 } else {
4932 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004933 }
4934
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004935 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004936 }
4937
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004938 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004939 * @deprecated Use {@link #build()} instead.
4940 */
4941 @Deprecated
4942 public Notification getNotification() {
4943 return build();
4944 }
4945
4946 /**
4947 * Combine all of the options that have been set and return a new {@link Notification}
4948 * object.
4949 */
4950 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004951 // first, add any extras from the calling code
4952 if (mUserExtras != null) {
4953 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004954 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004955
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004956 mN.creationTime = System.currentTimeMillis();
4957
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004958 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004959 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004960
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004961 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004962
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004963 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02004964 mStyle.reduceImageSizes(mContext);
4965 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004966 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004967 }
Selim Cinekd0426622017-07-11 13:19:59 +02004968 mN.reduceImageSizes(mContext);
4969
Adrian Roos5081c0d2016-02-26 16:04:19 -08004970 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004971 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004972 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004973 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004974 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4975 mN.contentView.getSequenceNumber());
4976 }
4977 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004978 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004979 if (mN.bigContentView != null) {
4980 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4981 mN.bigContentView.getSequenceNumber());
4982 }
4983 }
4984 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004985 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004986 if (mN.headsUpContentView != null) {
4987 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4988 mN.headsUpContentView.getSequenceNumber());
4989 }
4990 }
4991 }
4992
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004993 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4994 mN.flags |= FLAG_SHOW_LIGHTS;
4995 }
4996
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004997 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004998 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004999
5000 /**
5001 * Apply this Builder to an existing {@link Notification} object.
5002 *
5003 * @hide
5004 */
5005 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005006 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005007 return n;
5008 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005009
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005010 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005011 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005012 * change. Also removes extenders on low ram devices, as
5013 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005014 *
5015 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5016 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005017 * @hide
5018 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005019 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005020 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005021
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005022 // Only strip views for known Styles because we won't know how to
5023 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005024 if (!isLowRam
5025 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005026 && getNotificationStyleClass(templateClass) == null) {
5027 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005028 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005029
5030 // Only strip unmodified BuilderRemoteViews.
5031 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005032 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005033 n.contentView.getSequenceNumber();
5034 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005035 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005036 n.bigContentView.getSequenceNumber();
5037 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005038 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005039 n.headsUpContentView.getSequenceNumber();
5040
5041 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005042 if (!isLowRam
5043 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005044 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005045 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005046
5047 Notification clone = n.clone();
5048 if (stripContentView) {
5049 clone.contentView = null;
5050 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5051 }
5052 if (stripBigContentView) {
5053 clone.bigContentView = null;
5054 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5055 }
5056 if (stripHeadsUpContentView) {
5057 clone.headsUpContentView = null;
5058 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5059 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005060 if (isLowRam) {
5061 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5062 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5063 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5064 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005065 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005066 }
5067
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005068 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005069 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005070 }
5071
5072 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005073 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005074 }
5075
5076 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005077 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005078 }
5079
5080 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005081 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005082 }
5083
5084 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005085 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005086 }
5087
Adrian Roosc1a80b02016-04-05 14:54:55 -07005088 private int getMessagingLayoutResource() {
5089 return R.layout.notification_template_material_messaging;
5090 }
5091
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005092 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005093 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005094 }
5095
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005096 private int getEmphasizedActionLayoutResource() {
5097 return R.layout.notification_material_action_emphasized;
5098 }
5099
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005100 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005101 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005102 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005103
5104 private int getBackgroundColor() {
5105 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005106 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005107 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005108 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5109 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005110 }
5111 }
5112
5113 private boolean isColorized() {
5114 return mN.isColorized();
5115 }
Selim Cinek99104832017-01-25 14:47:33 -08005116
Anthony Chenad4d1582017-04-10 16:07:58 -07005117 private boolean shouldTintActionButtons() {
5118 return mTintActionButtons;
5119 }
5120
Selim Cinek99104832017-01-25 14:47:33 -08005121 private boolean textColorsNeedInversion() {
5122 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5123 return false;
5124 }
5125 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5126 return targetSdkVersion > Build.VERSION_CODES.M
5127 && targetSdkVersion < Build.VERSION_CODES.O;
5128 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005129
5130 /**
5131 * Set a color palette to be used as the background and textColors
5132 *
5133 * @param backgroundColor the color to be used as the background
5134 * @param foregroundColor the color to be used as the foreground
5135 *
5136 * @hide
5137 */
5138 public void setColorPalette(int backgroundColor, int foregroundColor) {
5139 mBackgroundColor = backgroundColor;
5140 mForegroundColor = foregroundColor;
5141 mTextColorsAreForBackground = COLOR_INVALID;
5142 ensureColors();
5143 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005144
5145 /**
5146 * Sets the background color for this notification to be a different one then the default.
5147 * This is mainly used to calculate contrast and won't necessarily be applied to the
5148 * background.
5149 *
5150 * @hide
5151 */
5152 public void setBackgroundColorHint(int backgroundColor) {
5153 mBackgroundColorHint = backgroundColor;
5154 }
Selim Cineka7679b62017-05-10 16:33:25 -07005155
5156
5157 /**
5158 * Forces all styled remoteViews to be built from scratch and not use any cached
5159 * RemoteViews.
5160 * This is needed for legacy apps that are baking in their remoteviews into the
5161 * notification.
5162 *
5163 * @hide
5164 */
5165 public void setRebuildStyledRemoteViews(boolean rebuild) {
5166 mRebuildStyledRemoteViews = rebuild;
5167 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005168 }
5169
5170 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005171 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5172 * remote views.
5173 *
5174 * @hide
5175 */
5176 void reduceImageSizes(Context context) {
5177 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5178 return;
5179 }
5180 if (mLargeIcon != null || largeIcon != null) {
5181 Resources resources = context.getResources();
5182 Class<? extends Style> style = getNotificationStyle();
5183 int maxWidth = resources.getDimensionPixelSize(R.dimen.notification_right_icon_size);
5184 int maxHeight = maxWidth;
5185 if (MediaStyle.class.equals(style)
5186 || DecoratedMediaCustomViewStyle.class.equals(style)) {
5187 maxHeight = resources.getDimensionPixelSize(
5188 R.dimen.notification_media_image_max_height);
5189 maxWidth = resources.getDimensionPixelSize(
5190 R.dimen.notification_media_image_max_width);
5191 }
5192 if (mLargeIcon != null) {
5193 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5194 }
5195 if (largeIcon != null) {
5196 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5197 }
5198 }
5199 reduceImageSizesForRemoteView(contentView, context);
5200 reduceImageSizesForRemoteView(headsUpContentView, context);
5201 reduceImageSizesForRemoteView(bigContentView, context);
5202 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5203 }
5204
5205 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context) {
5206 if (remoteView != null) {
5207 Resources resources = context.getResources();
5208 int maxWidth = resources.getDimensionPixelSize(
5209 R.dimen.notification_custom_view_max_image_width);
5210 int maxHeight = resources.getDimensionPixelSize(
5211 R.dimen.notification_custom_view_max_image_height);
5212 remoteView.reduceImageSizes(maxWidth, maxHeight);
5213 }
5214 }
5215
5216 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005217 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005218 */
Selim Cinek22714f12017-04-13 16:23:53 -07005219 private boolean isForegroundService() {
5220 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005221 }
5222
5223 /**
Selim Cinek99104832017-01-25 14:47:33 -08005224 * @return whether this notification has a media session attached
5225 * @hide
5226 */
5227 public boolean hasMediaSession() {
5228 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5229 }
5230
5231 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005232 * @return the style class of this notification
5233 * @hide
5234 */
5235 public Class<? extends Notification.Style> getNotificationStyle() {
5236 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5237
5238 if (!TextUtils.isEmpty(templateClass)) {
5239 return Notification.getNotificationStyleClass(templateClass);
5240 }
5241 return null;
5242 }
5243
5244 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005245 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005246 *
5247 * @hide
5248 */
5249 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005250 if (isColorizedMedia()) {
5251 return true;
5252 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005253 return extras.getBoolean(EXTRA_COLORIZED)
5254 && (hasColorizedPermission() || isForegroundService());
5255 }
5256
5257 /**
5258 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5259 * permission. The permission is checked when a notification is enqueued.
5260 */
5261 private boolean hasColorizedPermission() {
5262 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005263 }
5264
5265 /**
5266 * @return true if this notification is colorized and it is a media notification
5267 *
5268 * @hide
5269 */
5270 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005271 Class<? extends Style> style = getNotificationStyle();
5272 if (MediaStyle.class.equals(style)) {
5273 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5274 if ((colorized == null || colorized) && hasMediaSession()) {
5275 return true;
5276 }
5277 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5278 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5279 return true;
5280 }
5281 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005282 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005283 }
5284
Selim Cinek0847acd2017-04-24 19:48:29 -07005285
5286 /**
5287 * @return true if this is a media notification
5288 *
5289 * @hide
5290 */
5291 public boolean isMediaNotification() {
5292 Class<? extends Style> style = getNotificationStyle();
5293 if (MediaStyle.class.equals(style)) {
5294 return true;
5295 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5296 return true;
5297 }
5298 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005299 }
5300
Selim Cinek279fa862016-06-14 10:57:25 -07005301 private boolean hasLargeIcon() {
5302 return mLargeIcon != null || largeIcon != null;
5303 }
5304
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005305 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005306 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005307 * @hide
5308 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005309 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005310 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5311 }
5312
5313 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005314 * @return true if the notification will show a chronometer; false otherwise
5315 * @hide
5316 */
5317 public boolean showsChronometer() {
5318 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5319 }
5320
5321 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005322 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005323 */
5324 @SystemApi
5325 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5326 Class<? extends Style>[] classes = new Class[] {
5327 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5328 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5329 MessagingStyle.class };
5330 for (Class<? extends Style> innerClass : classes) {
5331 if (templateClass.equals(innerClass.getName())) {
5332 return innerClass;
5333 }
5334 }
5335 return null;
5336 }
5337
5338 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005339 * An object that can apply a rich notification style to a {@link Notification.Builder}
5340 * object.
5341 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005342 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005343 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005344
5345 /**
5346 * @hide
5347 */
5348 protected CharSequence mSummaryText = null;
5349
5350 /**
5351 * @hide
5352 */
5353 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005354
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005355 protected Builder mBuilder;
5356
Chris Wrend6297db2012-05-03 16:20:13 -04005357 /**
5358 * Overrides ContentTitle in the big form of the template.
5359 * This defaults to the value passed to setContentTitle().
5360 */
5361 protected void internalSetBigContentTitle(CharSequence title) {
5362 mBigContentTitle = title;
5363 }
5364
5365 /**
5366 * Set the first line of text after the detail section in the big form of the template.
5367 */
5368 protected void internalSetSummaryText(CharSequence cs) {
5369 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005370 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005371 }
5372
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005373 public void setBuilder(Builder builder) {
5374 if (mBuilder != builder) {
5375 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005376 if (mBuilder != null) {
5377 mBuilder.setStyle(this);
5378 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005379 }
5380 }
5381
Chris Wrend6297db2012-05-03 16:20:13 -04005382 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005383 if (mBuilder == null) {
5384 throw new IllegalArgumentException("Style requires a valid Builder object");
5385 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005386 }
Chris Wrend6297db2012-05-03 16:20:13 -04005387
5388 protected RemoteViews getStandardView(int layoutId) {
5389 checkBuilder();
5390
Christoph Studer4600f9b2014-07-22 22:44:43 +02005391 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005392 CharSequence oldBuilderContentTitle =
5393 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005394 if (mBigContentTitle != null) {
5395 mBuilder.setContentTitle(mBigContentTitle);
5396 }
5397
Chris Wrend6297db2012-05-03 16:20:13 -04005398 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5399
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005400 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005401
Chris Wrend6297db2012-05-03 16:20:13 -04005402 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5403 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005404 } else {
5405 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005406 }
5407
Chris Wrend6297db2012-05-03 16:20:13 -04005408 return contentView;
5409 }
5410
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005411 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005412 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005413 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005414 *
5415 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005416 * @hide
5417 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005418 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005419 return null;
5420 }
5421
5422 /**
5423 * Construct a Style-specific RemoteViews for the final big notification layout.
5424 * @hide
5425 */
5426 public RemoteViews makeBigContentView() {
5427 return null;
5428 }
5429
5430 /**
5431 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005432 *
5433 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005434 * @hide
5435 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005436 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005437 return null;
5438 }
5439
5440 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005441 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005442 * @hide
5443 */
5444 public void addExtras(Bundle extras) {
5445 if (mSummaryTextSet) {
5446 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5447 }
5448 if (mBigContentTitle != null) {
5449 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5450 }
Chris Wren91ad5632013-06-05 15:05:57 -04005451 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005452 }
5453
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005454 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005455 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005456 * @hide
5457 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005458 protected void restoreFromExtras(Bundle extras) {
5459 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5460 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5461 mSummaryTextSet = true;
5462 }
5463 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5464 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5465 }
5466 }
5467
5468
5469 /**
5470 * @hide
5471 */
5472 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005473 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005474 return wip;
5475 }
5476
Daniel Sandler0ec46202015-06-24 01:27:05 -04005477 /**
5478 * @hide
5479 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005480 public void purgeResources() {}
5481
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005482 /**
5483 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5484 * attached to.
5485 *
5486 * @return the fully constructed Notification.
5487 */
5488 public Notification build() {
5489 checkBuilder();
5490 return mBuilder.build();
5491 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005492
5493 /**
5494 * @hide
5495 * @return true if the style positions the progress bar on the second line; false if the
5496 * style hides the progress bar
5497 */
5498 protected boolean hasProgress() {
5499 return true;
5500 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005501
5502 /**
5503 * @hide
5504 * @return Whether we should put the summary be put into the notification header
5505 */
5506 public boolean hasSummaryInHeader() {
5507 return true;
5508 }
Selim Cinek593610c2016-02-16 18:42:57 -08005509
5510 /**
5511 * @hide
5512 * @return Whether custom content views are displayed inline in the style
5513 */
5514 public boolean displayCustomViewInline() {
5515 return false;
5516 }
Selim Cinekd0426622017-07-11 13:19:59 +02005517
5518 /**
5519 * Reduces the image sizes contained in this style.
5520 *
5521 * @hide
5522 */
5523 public void reduceImageSizes(Context context) {
5524 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005525 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005526
5527 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005528 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005529 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005530 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005531 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005532 * Notification notif = new Notification.Builder(mContext)
5533 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5534 * .setContentText(subject)
5535 * .setSmallIcon(R.drawable.new_post)
5536 * .setLargeIcon(aBitmap)
5537 * .setStyle(new Notification.BigPictureStyle()
5538 * .bigPicture(aBigBitmap))
5539 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005540 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005541 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005542 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005543 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005544 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005545 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005546 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005547 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005548
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005549 public BigPictureStyle() {
5550 }
5551
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005552 /**
5553 * @deprecated use {@code BigPictureStyle()}.
5554 */
5555 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005556 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005557 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005558 }
5559
Chris Wrend6297db2012-05-03 16:20:13 -04005560 /**
5561 * Overrides ContentTitle in the big form of the template.
5562 * This defaults to the value passed to setContentTitle().
5563 */
5564 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005565 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005566 return this;
5567 }
5568
5569 /**
5570 * Set the first line of text after the detail section in the big form of the template.
5571 */
5572 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005573 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005574 return this;
5575 }
5576
Chris Wren0bd664d2012-08-01 13:56:56 -04005577 /**
5578 * Provide the bitmap to be used as the payload for the BigPicture notification.
5579 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005580 public BigPictureStyle bigPicture(Bitmap b) {
5581 mPicture = b;
5582 return this;
5583 }
5584
Chris Wren3745a3d2012-05-22 15:11:52 -04005585 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005586 * Override the large icon when the big notification is shown.
5587 */
5588 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005589 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5590 }
5591
5592 /**
5593 * Override the large icon when the big notification is shown.
5594 */
5595 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005596 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005597 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005598 return this;
5599 }
5600
Riley Andrews0394a0c2015-11-03 23:36:52 -08005601 /** @hide */
5602 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5603
Daniel Sandler0ec46202015-06-24 01:27:05 -04005604 /**
5605 * @hide
5606 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005607 @Override
5608 public void purgeResources() {
5609 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005610 if (mPicture != null &&
5611 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005612 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005613 mPicture = mPicture.createAshmemBitmap();
5614 }
5615 if (mBigLargeIcon != null) {
5616 mBigLargeIcon.convertToAshmem();
5617 }
5618 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005619
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005620 /**
5621 * @hide
5622 */
Selim Cinekd0426622017-07-11 13:19:59 +02005623 @Override
5624 public void reduceImageSizes(Context context) {
5625 super.reduceImageSizes(context);
5626 Resources resources = context.getResources();
5627 if (mPicture != null) {
5628 int maxPictureWidth = resources.getDimensionPixelSize(
5629 R.dimen.notification_big_picture_max_height);
5630 int maxPictureHeight = resources.getDimensionPixelSize(
5631 R.dimen.notification_big_picture_max_width);
5632 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5633 }
5634 if (mBigLargeIcon != null) {
5635 int rightIconSize = resources.getDimensionPixelSize(
5636 R.dimen.notification_right_icon_size);
5637 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5638 }
5639 }
5640
5641 /**
5642 * @hide
5643 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005644 public RemoteViews makeBigContentView() {
5645 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005646 // This covers the following cases:
5647 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005648 // mN.mLargeIcon
5649 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005650 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005651 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005652 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005653 oldLargeIcon = mBuilder.mN.mLargeIcon;
5654 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005655 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5656 // replacement if the other one is null. Because we're restoring these legacy icons
5657 // for old listeners, this is in general non-null.
5658 largeIconLegacy = mBuilder.mN.largeIcon;
5659 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005660 }
5661
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005662 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005663 if (mSummaryTextSet) {
5664 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005665 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005666 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005667 }
Selim Cinek279fa862016-06-14 10:57:25 -07005668 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005669
Christoph Studer5c510ee2014-12-15 16:32:27 +01005670 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005671 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005672 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005673 }
5674
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005675 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005676 return contentView;
5677 }
5678
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005679 /**
5680 * @hide
5681 */
5682 public void addExtras(Bundle extras) {
5683 super.addExtras(extras);
5684
5685 if (mBigLargeIconSet) {
5686 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5687 }
5688 extras.putParcelable(EXTRA_PICTURE, mPicture);
5689 }
5690
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005691 /**
5692 * @hide
5693 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005694 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005695 protected void restoreFromExtras(Bundle extras) {
5696 super.restoreFromExtras(extras);
5697
5698 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005699 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005700 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005701 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005702 mPicture = extras.getParcelable(EXTRA_PICTURE);
5703 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005704
5705 /**
5706 * @hide
5707 */
5708 @Override
5709 public boolean hasSummaryInHeader() {
5710 return false;
5711 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005712 }
5713
5714 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005715 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005716 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005717 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005718 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005719 * Notification notif = new Notification.Builder(mContext)
5720 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5721 * .setContentText(subject)
5722 * .setSmallIcon(R.drawable.new_mail)
5723 * .setLargeIcon(aBitmap)
5724 * .setStyle(new Notification.BigTextStyle()
5725 * .bigText(aVeryLongString))
5726 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005727 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005728 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005729 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005730 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005731 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005732
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005733 private CharSequence mBigText;
5734
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005735 public BigTextStyle() {
5736 }
5737
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005738 /**
5739 * @deprecated use {@code BigTextStyle()}.
5740 */
5741 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005742 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005743 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005744 }
5745
Chris Wrend6297db2012-05-03 16:20:13 -04005746 /**
5747 * Overrides ContentTitle in the big form of the template.
5748 * This defaults to the value passed to setContentTitle().
5749 */
5750 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005751 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005752 return this;
5753 }
5754
5755 /**
5756 * Set the first line of text after the detail section in the big form of the template.
5757 */
5758 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005759 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005760 return this;
5761 }
5762
Chris Wren0bd664d2012-08-01 13:56:56 -04005763 /**
5764 * Provide the longer text to be displayed in the big form of the
5765 * template in place of the content text.
5766 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005767 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005768 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005769 return this;
5770 }
5771
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005772 /**
5773 * @hide
5774 */
5775 public void addExtras(Bundle extras) {
5776 super.addExtras(extras);
5777
Christoph Studer4600f9b2014-07-22 22:44:43 +02005778 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5779 }
5780
5781 /**
5782 * @hide
5783 */
5784 @Override
5785 protected void restoreFromExtras(Bundle extras) {
5786 super.restoreFromExtras(extras);
5787
5788 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005789 }
5790
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005791 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005792 * @param increasedHeight true if this layout be created with an increased height.
5793 *
5794 * @hide
5795 */
5796 @Override
5797 public RemoteViews makeContentView(boolean increasedHeight) {
5798 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005799 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005800 mBuilder.mActions = new ArrayList<>();
5801 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005802 mBuilder.mActions = mBuilder.mOriginalActions;
5803 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005804 return remoteViews;
5805 }
5806 return super.makeContentView(increasedHeight);
5807 }
5808
5809 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005810 * @hide
5811 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005812 @Override
5813 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5814 if (increasedHeight && mBuilder.mActions.size() > 0) {
5815 return makeBigContentView();
5816 }
5817 return super.makeHeadsUpContentView(increasedHeight);
5818 }
5819
5820 /**
5821 * @hide
5822 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005823 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005824
5825 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005826 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005827 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005828
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005829 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005830
Selim Cinek75998782016-04-26 10:39:17 -07005831 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005832
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005833 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005834 if (TextUtils.isEmpty(bigTextText)) {
5835 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5836 // experience
5837 bigTextText = mBuilder.processLegacyText(text);
5838 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005839 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005840
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005841 return contentView;
5842 }
5843
Adrian Roosb1f427c2016-05-26 12:27:15 -07005844 static void applyBigTextContentView(Builder builder,
5845 RemoteViews contentView, CharSequence bigTextText) {
5846 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005847 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005848 contentView.setViewVisibility(R.id.big_text,
5849 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005850 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005851 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005852 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005853
5854 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005855 * Helper class for generating large-format notifications that include multiple back-and-forth
5856 * messages of varying types between any number of people.
5857 *
5858 * <br>
5859 * If the platform does not provide large-format notifications, this method has no effect. The
5860 * user will always see the normal notification view.
5861 * <br>
5862 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5863 * so:
5864 * <pre class="prettyprint">
5865 *
5866 * Notification noti = new Notification.Builder()
5867 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5868 * .setContentText(subject)
5869 * .setSmallIcon(R.drawable.new_message)
5870 * .setLargeIcon(aBitmap)
5871 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5872 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5873 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5874 * .build();
5875 * </pre>
5876 */
5877 public static class MessagingStyle extends Style {
5878
5879 /**
5880 * The maximum number of messages that will be retained in the Notification itself (the
5881 * number displayed is up to the platform).
5882 */
5883 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5884
5885 CharSequence mUserDisplayName;
5886 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005887 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005888 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005889
5890 MessagingStyle() {
5891 }
5892
5893 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005894 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5895 * user before the posting app reposts the notification with those messages after they've
5896 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005897 * {@link #addMessage(Notification.MessagingStyle.Message)}
5898 */
Alex Hillsfd590442016-10-07 09:52:44 -04005899 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005900 mUserDisplayName = userDisplayName;
5901 }
5902
5903 /**
5904 * Returns the name to be displayed for any replies sent by the user
5905 */
5906 public CharSequence getUserDisplayName() {
5907 return mUserDisplayName;
5908 }
5909
5910 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005911 * Sets the title to be displayed on this conversation. This should only be used for
5912 * group messaging and left unset for one-on-one conversations.
5913 * @param conversationTitle
5914 * @return this object for method chaining.
5915 */
5916 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5917 mConversationTitle = conversationTitle;
5918 return this;
5919 }
5920
5921 /**
5922 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5923 * should be for one-on-one conversations
5924 */
5925 public CharSequence getConversationTitle() {
5926 return mConversationTitle;
5927 }
5928
5929 /**
5930 * Adds a message for display by this notification. Convenience call for a simple
5931 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5932 * @param text A {@link CharSequence} to be displayed as the message content
5933 * @param timestamp Time at which the message arrived
5934 * @param sender A {@link CharSequence} to be used for displaying the name of the
5935 * sender. Should be <code>null</code> for messages by the current user, in which case
5936 * the platform will insert {@link #getUserDisplayName()}.
5937 * Should be unique amongst all individuals in the conversation, and should be
5938 * consistent during re-posts of the notification.
5939 *
5940 * @see Message#Message(CharSequence, long, CharSequence)
5941 *
5942 * @return this object for method chaining
5943 */
5944 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005945 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005946 }
5947
5948 /**
5949 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005950 *
5951 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5952 * the newest last.
5953 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005954 * @param message The {@link Message} to be displayed
5955 * @return this object for method chaining
5956 */
5957 public MessagingStyle addMessage(Message message) {
5958 mMessages.add(message);
5959 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5960 mMessages.remove(0);
5961 }
5962 return this;
5963 }
5964
5965 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005966 * Adds a {@link Message} for historic context in this notification.
5967 *
5968 * <p>Messages should be added as historic if they are not the main subject of the
5969 * notification but may give context to a conversation. The system may choose to present
5970 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5971 *
5972 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5973 * the newest last.
5974 *
5975 * @param message The historic {@link Message} to be added
5976 * @return this object for method chaining
5977 */
5978 public MessagingStyle addHistoricMessage(Message message) {
5979 mHistoricMessages.add(message);
5980 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5981 mHistoricMessages.remove(0);
5982 }
5983 return this;
5984 }
5985
5986 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005987 * Gets the list of {@code Message} objects that represent the notification
5988 */
5989 public List<Message> getMessages() {
5990 return mMessages;
5991 }
5992
5993 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005994 * Gets the list of historic {@code Message}s in the notification.
5995 */
5996 public List<Message> getHistoricMessages() {
5997 return mHistoricMessages;
5998 }
5999
6000 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006001 * @hide
6002 */
6003 @Override
6004 public void addExtras(Bundle extras) {
6005 super.addExtras(extras);
6006 if (mUserDisplayName != null) {
6007 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6008 }
6009 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006010 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006011 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006012 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6013 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006014 }
Adrian Roos437cd562017-01-18 15:47:03 -08006015 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6016 Message.getBundleArrayForMessages(mHistoricMessages));
6017 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006018
6019 fixTitleAndTextExtras(extras);
6020 }
6021
6022 private void fixTitleAndTextExtras(Bundle extras) {
6023 Message m = findLatestIncomingMessage();
6024 CharSequence text = (m == null) ? null : m.mText;
6025 CharSequence sender = m == null ? null
6026 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6027 CharSequence title;
6028 if (!TextUtils.isEmpty(mConversationTitle)) {
6029 if (!TextUtils.isEmpty(sender)) {
6030 BidiFormatter bidi = BidiFormatter.getInstance();
6031 title = mBuilder.mContext.getString(
6032 com.android.internal.R.string.notification_messaging_title_template,
6033 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6034 } else {
6035 title = mConversationTitle;
6036 }
6037 } else {
6038 title = sender;
6039 }
6040
6041 if (title != null) {
6042 extras.putCharSequence(EXTRA_TITLE, title);
6043 }
6044 if (text != null) {
6045 extras.putCharSequence(EXTRA_TEXT, text);
6046 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006047 }
6048
6049 /**
6050 * @hide
6051 */
6052 @Override
6053 protected void restoreFromExtras(Bundle extras) {
6054 super.restoreFromExtras(extras);
6055
6056 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08006057 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07006058 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6059 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006060 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
6061 if (messages != null && messages instanceof Parcelable[]) {
6062 mMessages = Message.getMessagesFromBundleArray(messages);
6063 }
6064 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
6065 if (histMessages != null && histMessages instanceof Parcelable[]) {
6066 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04006067 }
6068 }
6069
6070 /**
6071 * @hide
6072 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006073 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006074 public RemoteViews makeContentView(boolean increasedHeight) {
6075 if (!increasedHeight) {
6076 Message m = findLatestIncomingMessage();
6077 CharSequence title = mConversationTitle != null
6078 ? mConversationTitle
6079 : (m == null) ? null : m.mSender;
6080 CharSequence text = (m == null)
6081 ? null
6082 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04006083
Selim Cinek7d1009b2017-01-25 15:28:28 -08006084 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
6085 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
6086 } else {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006087 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006088 mBuilder.mActions = new ArrayList<>();
6089 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006090 mBuilder.mActions = mBuilder.mOriginalActions;
6091 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006092 return remoteViews;
6093 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006094 }
6095
6096 private Message findLatestIncomingMessage() {
6097 for (int i = mMessages.size() - 1; i >= 0; i--) {
6098 Message m = mMessages.get(i);
6099 // Incoming messages have a non-empty sender.
6100 if (!TextUtils.isEmpty(m.mSender)) {
6101 return m;
6102 }
6103 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006104 if (!mMessages.isEmpty()) {
6105 // No incoming messages, fall back to outgoing message
6106 return mMessages.get(mMessages.size() - 1);
6107 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006108 return null;
6109 }
6110
6111 /**
6112 * @hide
6113 */
6114 @Override
6115 public RemoteViews makeBigContentView() {
6116 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
6117 ? super.mBigContentTitle
6118 : mConversationTitle;
6119 boolean hasTitle = !TextUtils.isEmpty(title);
6120
Adrian Roosfeafa052016-06-01 17:09:45 -07006121 if (mMessages.size() == 1) {
6122 // Special case for a single message: Use the big text style
6123 // so the collapsed and expanded versions match nicely.
6124 CharSequence bigTitle;
6125 CharSequence text;
6126 if (hasTitle) {
6127 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006128 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07006129 } else {
6130 bigTitle = mMessages.get(0).mSender;
6131 text = mMessages.get(0).mText;
6132 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006133 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
6134 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006135 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07006136 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
6137 return contentView;
6138 }
6139
Adrian Roos48d746a2016-04-12 14:57:28 -07006140 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006141 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006142 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006143
6144 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
6145 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
6146
6147 // Make sure all rows are gone in case we reuse a view.
6148 for (int rowId : rowIds) {
6149 contentView.setViewVisibility(rowId, View.GONE);
6150 }
6151
6152 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006153 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
6154 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006155 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07006156 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07006157
Adrian Roosfeafa052016-06-01 17:09:45 -07006158 int contractedChildId = View.NO_ID;
6159 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08006160 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
6161 - (rowIds.length - mMessages.size()));
6162 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
6163 Message m = mHistoricMessages.get(firstHistoricMessage + i);
6164 int rowId = rowIds[i];
6165
Selim Cinek7b9605b2017-01-19 17:36:00 -08006166 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08006167
6168 if (contractedMessage == m) {
6169 contractedChildId = rowId;
6170 }
6171
6172 i++;
6173 }
6174
Adrian Roosc1a80b02016-04-05 14:54:55 -07006175 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
6176 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
6177 Message m = mMessages.get(firstMessage + i);
6178 int rowId = rowIds[i];
6179
6180 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006181 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
6182 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006183
Adrian Roosfeafa052016-06-01 17:09:45 -07006184 if (contractedMessage == m) {
6185 contractedChildId = rowId;
6186 }
6187
Adrian Roosc1a80b02016-04-05 14:54:55 -07006188 i++;
6189 }
Adrian Roos437cd562017-01-18 15:47:03 -08006190 // Clear the remaining views for reapply. Ensures that historic message views can
6191 // reliably be identified as being GONE and having non-null text.
6192 while (i < rowIds.length) {
6193 int rowId = rowIds[i];
6194 contentView.setTextViewText(rowId, null);
6195 i++;
6196 }
6197
Adrian Roosfeafa052016-06-01 17:09:45 -07006198 // Record this here to allow transformation between the contracted and expanded views.
6199 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
6200 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04006201 return contentView;
6202 }
6203
Selim Cinek7b9605b2017-01-19 17:36:00 -08006204 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006205 BidiFormatter bidi = BidiFormatter.getInstance();
6206 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08006207 boolean colorize = builder.isColorized();
Anthony Chenad4d1582017-04-10 16:07:58 -07006208 TextAppearanceSpan colorSpan;
6209 CharSequence messageName;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006210 if (TextUtils.isEmpty(m.mSender)) {
6211 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
6212 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006213 makeFontColorSpan(colorize
6214 ? builder.getPrimaryTextColor()
6215 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006216 0 /* flags */);
6217 } else {
6218 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08006219 makeFontColorSpan(colorize
6220 ? builder.getPrimaryTextColor()
6221 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07006222 0 /* flags */);
6223 }
6224 CharSequence text = m.mText == null ? "" : m.mText;
6225 sb.append(" ").append(bidi.unicodeWrap(text));
6226 return sb;
6227 }
6228
Adrian Roosdedd1df2016-04-26 16:38:47 -07006229 /**
6230 * @hide
6231 */
6232 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006233 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6234 if (increasedHeight) {
6235 return makeBigContentView();
6236 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07006237 Message m = findLatestIncomingMessage();
6238 CharSequence title = mConversationTitle != null
6239 ? mConversationTitle
6240 : (m == null) ? null : m.mSender;
6241 CharSequence text = (m == null)
6242 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08006243 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006244
6245 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08006246 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07006247 }
6248
Adrian Roosc1a80b02016-04-05 14:54:55 -07006249 private static TextAppearanceSpan makeFontColorSpan(int color) {
6250 return new TextAppearanceSpan(null, 0, 0,
6251 ColorStateList.valueOf(color), null);
6252 }
6253
Alex Hillsd9b04d92016-04-11 16:38:16 -04006254 public static final class Message {
6255
6256 static final String KEY_TEXT = "text";
6257 static final String KEY_TIMESTAMP = "time";
6258 static final String KEY_SENDER = "sender";
6259 static final String KEY_DATA_MIME_TYPE = "type";
6260 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006261 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006262
6263 private final CharSequence mText;
6264 private final long mTimestamp;
6265 private final CharSequence mSender;
6266
Shane Brennan5a871862017-03-11 13:14:17 -08006267 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006268 private String mDataMimeType;
6269 private Uri mDataUri;
6270
6271 /**
6272 * Constructor
6273 * @param text A {@link CharSequence} to be displayed as the message content
6274 * @param timestamp Time at which the message arrived
6275 * @param sender A {@link CharSequence} to be used for displaying the name of the
6276 * sender. Should be <code>null</code> for messages by the current user, in which case
6277 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6278 * Should be unique amongst all individuals in the conversation, and should be
6279 * consistent during re-posts of the notification.
6280 */
6281 public Message(CharSequence text, long timestamp, CharSequence sender){
6282 mText = text;
6283 mTimestamp = timestamp;
6284 mSender = sender;
6285 }
6286
6287 /**
6288 * Sets a binary blob of data and an associated MIME type for a message. In the case
6289 * where the platform doesn't support the MIME type, the original text provided in the
6290 * constructor will be used.
6291 * @param dataMimeType The MIME type of the content. See
6292 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6293 * types on Android and Android Wear.
6294 * @param dataUri The uri containing the content whose type is given by the MIME type.
6295 * <p class="note">
6296 * <ol>
6297 * <li>Notification Listeners including the System UI need permission to access the
6298 * data the Uri points to. The recommended ways to do this are:</li>
6299 * <li>Store the data in your own ContentProvider, making sure that other apps have
6300 * the correct permission to access your provider. The preferred mechanism for
6301 * providing access is to use per-URI permissions which are temporary and only
6302 * grant access to the receiving application. An easy way to create a
6303 * ContentProvider like this is to use the FileProvider helper class.</li>
6304 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6305 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6306 * also store non-media types (see MediaStore.Files for more info). Files can be
6307 * inserted into the MediaStore using scanFile() after which a content:// style
6308 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6309 * Note that once added to the system MediaStore the content is accessible to any
6310 * app on the device.</li>
6311 * </ol>
6312 * @return this object for method chaining
6313 */
6314 public Message setData(String dataMimeType, Uri dataUri) {
6315 mDataMimeType = dataMimeType;
6316 mDataUri = dataUri;
6317 return this;
6318 }
6319
Alex Hillsfc737de2016-03-23 17:33:02 -04006320 /**
6321 * Get the text to be used for this message, or the fallback text if a type and content
6322 * Uri have been set
6323 */
6324 public CharSequence getText() {
6325 return mText;
6326 }
6327
6328 /**
6329 * Get the time at which this message arrived
6330 */
6331 public long getTimestamp() {
6332 return mTimestamp;
6333 }
6334
6335 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006336 * Get the extras Bundle for this message.
6337 */
6338 public Bundle getExtras() {
6339 return mExtras;
6340 }
6341
6342 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006343 * Get the text used to display the contact's name in the messaging experience
6344 */
6345 public CharSequence getSender() {
6346 return mSender;
6347 }
6348
6349 /**
6350 * Get the MIME type of the data pointed to by the Uri
6351 */
6352 public String getDataMimeType() {
6353 return mDataMimeType;
6354 }
6355
6356 /**
6357 * Get the the Uri pointing to the content of the message. Can be null, in which case
6358 * {@see #getText()} is used.
6359 */
6360 public Uri getDataUri() {
6361 return mDataUri;
6362 }
6363
Alex Hillsd9b04d92016-04-11 16:38:16 -04006364 private Bundle toBundle() {
6365 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006366 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006367 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006368 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006369 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006370 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006371 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006372 }
6373 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006374 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006375 }
6376 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006377 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006378 }
Shane Brennan5a871862017-03-11 13:14:17 -08006379 if (mExtras != null) {
6380 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6381 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006382 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006383 }
6384
Alex Hillsd9b04d92016-04-11 16:38:16 -04006385 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6386 Bundle[] bundles = new Bundle[messages.size()];
6387 final int N = messages.size();
6388 for (int i = 0; i < N; i++) {
6389 bundles[i] = messages.get(i).toBundle();
6390 }
6391 return bundles;
6392 }
6393
Adrian Roosdedd1df2016-04-26 16:38:47 -07006394 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006395 List<Message> messages = new ArrayList<>(bundles.length);
6396 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006397 if (bundles[i] instanceof Bundle) {
6398 Message message = getMessageFromBundle((Bundle)bundles[i]);
6399 if (message != null) {
6400 messages.add(message);
6401 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006402 }
6403 }
6404 return messages;
6405 }
6406
6407 static Message getMessageFromBundle(Bundle bundle) {
6408 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006409 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006410 return null;
6411 } else {
6412 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6413 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6414 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6415 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006416 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6417 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006418 }
Shane Brennan5a871862017-03-11 13:14:17 -08006419 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6420 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6421 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006422 return message;
6423 }
6424 } catch (ClassCastException e) {
6425 return null;
6426 }
6427 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006428 }
6429 }
6430
6431 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006432 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006433 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006434 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006435 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006436 * Notification notif = new Notification.Builder(mContext)
6437 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6438 * .setContentText(subject)
6439 * .setSmallIcon(R.drawable.new_mail)
6440 * .setLargeIcon(aBitmap)
6441 * .setStyle(new Notification.InboxStyle()
6442 * .addLine(str1)
6443 * .addLine(str2)
6444 * .setContentTitle(&quot;&quot;)
6445 * .setSummaryText(&quot;+3 more&quot;))
6446 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006447 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006448 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006449 * @see Notification#bigContentView
6450 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006451 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006452 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6453
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006454 public InboxStyle() {
6455 }
6456
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006457 /**
6458 * @deprecated use {@code InboxStyle()}.
6459 */
6460 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006461 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006462 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006463 }
6464
Chris Wrend6297db2012-05-03 16:20:13 -04006465 /**
6466 * Overrides ContentTitle in the big form of the template.
6467 * This defaults to the value passed to setContentTitle().
6468 */
6469 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006470 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006471 return this;
6472 }
6473
6474 /**
6475 * Set the first line of text after the detail section in the big form of the template.
6476 */
6477 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006478 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006479 return this;
6480 }
6481
Chris Wren0bd664d2012-08-01 13:56:56 -04006482 /**
6483 * Append a line to the digest section of the Inbox notification.
6484 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006485 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006486 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006487 return this;
6488 }
6489
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006490 /**
6491 * @hide
6492 */
6493 public void addExtras(Bundle extras) {
6494 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006495
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006496 CharSequence[] a = new CharSequence[mTexts.size()];
6497 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6498 }
6499
Christoph Studer4600f9b2014-07-22 22:44:43 +02006500 /**
6501 * @hide
6502 */
6503 @Override
6504 protected void restoreFromExtras(Bundle extras) {
6505 super.restoreFromExtras(extras);
6506
6507 mTexts.clear();
6508 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6509 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6510 }
6511 }
6512
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006513 /**
6514 * @hide
6515 */
6516 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006517 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006518 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006519 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6520 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006521
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006522 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006523
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006524 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006525
Chris Wrend6297db2012-05-03 16:20:13 -04006526 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 -04006527 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006528
Chris Wren4ed80d52012-05-17 09:30:03 -04006529 // Make sure all rows are gone in case we reuse a view.
6530 for (int rowId : rowIds) {
6531 contentView.setViewVisibility(rowId, View.GONE);
6532 }
6533
Daniel Sandler879c5e02012-04-17 16:46:51 -04006534 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006535 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6536 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006537 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006538 int onlyViewId = 0;
6539 int maxRows = rowIds.length;
6540 if (mBuilder.mActions.size() > 0) {
6541 maxRows--;
6542 }
6543 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006544 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006545 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006546 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006547 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006548 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006549 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006550 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006551 if (first) {
6552 onlyViewId = rowIds[i];
6553 } else {
6554 onlyViewId = 0;
6555 }
Selim Cinek247fa012016-02-18 09:50:48 -08006556 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006557 }
6558 i++;
6559 }
Selim Cinek07c80172016-04-21 16:40:47 -07006560 if (onlyViewId != 0) {
6561 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6562 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6563 R.dimen.notification_text_margin_top);
6564 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6565 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006566
Daniel Sandler879c5e02012-04-17 16:46:51 -04006567 return contentView;
6568 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006569
Selim Cinek247fa012016-02-18 09:50:48 -08006570 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006571 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006572 if (first) {
6573 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6574 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6575 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006576 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006577 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006578 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006579 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006580 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006581 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006582 }
Dan Sandler842dd772014-05-15 09:36:47 -04006583
6584 /**
6585 * Notification style for media playback notifications.
6586 *
6587 * In the expanded form, {@link Notification#bigContentView}, up to 5
6588 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006589 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006590 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6591 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6592 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006593 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006594 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6595 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006596 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006597 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006598 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006599 * Notifications created with MediaStyle will have their category set to
6600 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6601 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006602 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006603 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6604 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006605 * the System UI can identify this as a notification representing an active media session
6606 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6607 *
Selim Cinek99104832017-01-25 14:47:33 -08006608 * <p>
6609 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6610 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6611 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6612 * <p>
6613 *
Dan Sandler842dd772014-05-15 09:36:47 -04006614 * To use this style with your Notification, feed it to
6615 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6616 * <pre class="prettyprint">
6617 * Notification noti = new Notification.Builder()
6618 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006619 * .setContentTitle(&quot;Track title&quot;)
6620 * .setContentText(&quot;Artist - Album&quot;)
6621 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006622 * .setStyle(<b>new Notification.MediaStyle()</b>
6623 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006624 * .build();
6625 * </pre>
6626 *
6627 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006628 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006629 */
6630 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006631 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006632 static final int MAX_MEDIA_BUTTONS = 5;
6633
6634 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006635 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006636
6637 public MediaStyle() {
6638 }
6639
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006640 /**
6641 * @deprecated use {@code MediaStyle()}.
6642 */
6643 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006644 public MediaStyle(Builder builder) {
6645 setBuilder(builder);
6646 }
6647
6648 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006649 * 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 -04006650 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006651 *
6652 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006653 */
6654 public MediaStyle setShowActionsInCompactView(int...actions) {
6655 mActionsToShowInCompact = actions;
6656 return this;
6657 }
6658
6659 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006660 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6661 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006662 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006663 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006664 mToken = token;
6665 return this;
6666 }
6667
Christoph Studer4600f9b2014-07-22 22:44:43 +02006668 /**
6669 * @hide
6670 */
Dan Sandler842dd772014-05-15 09:36:47 -04006671 @Override
6672 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006673 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006674 if (wip.category == null) {
6675 wip.category = Notification.CATEGORY_TRANSPORT;
6676 }
Dan Sandler842dd772014-05-15 09:36:47 -04006677 return wip;
6678 }
6679
Christoph Studer4600f9b2014-07-22 22:44:43 +02006680 /**
6681 * @hide
6682 */
6683 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006684 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006685 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006686 }
6687
6688 /**
6689 * @hide
6690 */
6691 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006692 public RemoteViews makeBigContentView() {
6693 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006694 }
6695
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006696 /**
6697 * @hide
6698 */
6699 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006700 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006701 RemoteViews expanded = makeMediaBigContentView();
6702 return expanded != null ? expanded : makeMediaContentView();
6703 }
6704
Dan Sandler842dd772014-05-15 09:36:47 -04006705 /** @hide */
6706 @Override
6707 public void addExtras(Bundle extras) {
6708 super.addExtras(extras);
6709
6710 if (mToken != null) {
6711 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6712 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006713 if (mActionsToShowInCompact != null) {
6714 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6715 }
Dan Sandler842dd772014-05-15 09:36:47 -04006716 }
6717
Christoph Studer4600f9b2014-07-22 22:44:43 +02006718 /**
6719 * @hide
6720 */
6721 @Override
6722 protected void restoreFromExtras(Bundle extras) {
6723 super.restoreFromExtras(extras);
6724
6725 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6726 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6727 }
6728 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6729 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6730 }
6731 }
6732
Selim Cinek5bf069a2015-11-10 19:14:27 -05006733 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006734 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006735 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006736 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006737 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006738
6739 // If the action buttons should not be tinted, then just use the default
6740 // notification color. Otherwise, just use the passed-in color.
6741 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6742 ? color
6743 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6744 Notification.COLOR_DEFAULT);
6745
6746 button.setDrawableParameters(R.id.action0, false, -1, tintColor,
6747 PorterDuff.Mode.SRC_ATOP, -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006748 if (!tombstone) {
6749 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6750 }
6751 button.setContentDescription(R.id.action0, action.title);
6752 return button;
6753 }
6754
6755 private RemoteViews makeMediaContentView() {
6756 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006757 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006758
6759 final int numActions = mBuilder.mActions.size();
6760 final int N = mActionsToShowInCompact == null
6761 ? 0
6762 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6763 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006764 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006765 for (int i = 0; i < N; i++) {
6766 if (i >= numActions) {
6767 throw new IllegalArgumentException(String.format(
6768 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6769 i, numActions - 1));
6770 }
6771
6772 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006773 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006774 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006775 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006776 }
6777 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006778 handleImage(view);
6779 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006780 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006781 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006782 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006783 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006784 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006785 return view;
6786 }
6787
Selim Cinek99104832017-01-25 14:47:33 -08006788 private int getPrimaryHighlightColor() {
6789 return mBuilder.getPrimaryHighlightColor();
6790 }
6791
Dan Sandler842dd772014-05-15 09:36:47 -04006792 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006793 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006794 // Dont add an expanded view if there is no more content to be revealed
6795 int actionsInCompact = mActionsToShowInCompact == null
6796 ? 0
6797 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006798 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006799 return null;
6800 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006801 RemoteViews big = mBuilder.applyStandardTemplate(
6802 R.layout.notification_template_material_big_media,
6803 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006804
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006805 if (actionCount > 0) {
6806 big.removeAllViews(com.android.internal.R.id.media_actions);
6807 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006808 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006809 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006810 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006811 }
6812 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006813 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006814 return big;
6815 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006816
Selim Cinek5bf069a2015-11-10 19:14:27 -05006817 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006818 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006819 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6820 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006821 }
6822 }
6823
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006824 /**
6825 * @hide
6826 */
6827 @Override
6828 protected boolean hasProgress() {
6829 return false;
6830 }
Dan Sandler842dd772014-05-15 09:36:47 -04006831 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006832
Selim Cinek593610c2016-02-16 18:42:57 -08006833 /**
6834 * Notification style for custom views that are decorated by the system
6835 *
6836 * <p>Instead of providing a notification that is completely custom, a developer can set this
6837 * style and still obtain system decorations like the notification header with the expand
6838 * affordance and actions.
6839 *
6840 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6841 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6842 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6843 * corresponding custom views to display.
6844 *
6845 * To use this style with your Notification, feed it to
6846 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6847 * <pre class="prettyprint">
6848 * Notification noti = new Notification.Builder()
6849 * .setSmallIcon(R.drawable.ic_stat_player)
6850 * .setLargeIcon(albumArtBitmap))
6851 * .setCustomContentView(contentView);
6852 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6853 * .build();
6854 * </pre>
6855 */
6856 public static class DecoratedCustomViewStyle extends Style {
6857
6858 public DecoratedCustomViewStyle() {
6859 }
6860
Selim Cinek593610c2016-02-16 18:42:57 -08006861 /**
6862 * @hide
6863 */
6864 public boolean displayCustomViewInline() {
6865 return true;
6866 }
6867
6868 /**
6869 * @hide
6870 */
6871 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006872 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006873 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6874 }
6875
6876 /**
6877 * @hide
6878 */
6879 @Override
6880 public RemoteViews makeBigContentView() {
6881 return makeDecoratedBigContentView();
6882 }
6883
6884 /**
6885 * @hide
6886 */
6887 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006888 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006889 return makeDecoratedHeadsUpContentView();
6890 }
6891
Selim Cinek593610c2016-02-16 18:42:57 -08006892 private RemoteViews makeDecoratedHeadsUpContentView() {
6893 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6894 ? mBuilder.mN.contentView
6895 : mBuilder.mN.headsUpContentView;
6896 if (mBuilder.mActions.size() == 0) {
6897 return makeStandardTemplateWithCustomContent(headsUpContentView);
6898 }
6899 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6900 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006901 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006902 return remoteViews;
6903 }
6904
Selim Cinek593610c2016-02-16 18:42:57 -08006905 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6906 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6907 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006908 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006909 return remoteViews;
6910 }
6911
Selim Cinek593610c2016-02-16 18:42:57 -08006912 private RemoteViews makeDecoratedBigContentView() {
6913 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6914 ? mBuilder.mN.contentView
6915 : mBuilder.mN.bigContentView;
6916 if (mBuilder.mActions.size() == 0) {
6917 return makeStandardTemplateWithCustomContent(bigContentView);
6918 }
6919 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6920 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006921 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006922 return remoteViews;
6923 }
Selim Cinek247fa012016-02-18 09:50:48 -08006924
6925 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6926 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006927 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006928 // Need to clone customContent before adding, because otherwise it can no longer be
6929 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006930 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006931 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6932 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006933 }
Selim Cinek247fa012016-02-18 09:50:48 -08006934 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006935 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006936 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006937 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006938 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006939 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006940 }
Selim Cinek593610c2016-02-16 18:42:57 -08006941 }
6942
Selim Cinek03eb3b72016-02-18 10:39:45 -08006943 /**
6944 * Notification style for media custom views that are decorated by the system
6945 *
6946 * <p>Instead of providing a media notification that is completely custom, a developer can set
6947 * this style and still obtain system decorations like the notification header with the expand
6948 * affordance and actions.
6949 *
6950 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6951 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6952 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6953 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006954 * <p>
6955 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6956 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6957 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006958 * To use this style with your Notification, feed it to
6959 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6960 * <pre class="prettyprint">
6961 * Notification noti = new Notification.Builder()
6962 * .setSmallIcon(R.drawable.ic_stat_player)
6963 * .setLargeIcon(albumArtBitmap))
6964 * .setCustomContentView(contentView);
6965 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6966 * .setMediaSession(mySession))
6967 * .build();
6968 * </pre>
6969 *
6970 * @see android.app.Notification.DecoratedCustomViewStyle
6971 * @see android.app.Notification.MediaStyle
6972 */
6973 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6974
6975 public DecoratedMediaCustomViewStyle() {
6976 }
6977
Selim Cinek03eb3b72016-02-18 10:39:45 -08006978 /**
6979 * @hide
6980 */
6981 public boolean displayCustomViewInline() {
6982 return true;
6983 }
6984
6985 /**
6986 * @hide
6987 */
6988 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006989 public RemoteViews makeContentView(boolean increasedHeight) {
6990 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006991 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6992 mBuilder.mN.contentView);
6993 }
6994
6995 /**
6996 * @hide
6997 */
6998 @Override
6999 public RemoteViews makeBigContentView() {
7000 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7001 ? mBuilder.mN.bigContentView
7002 : mBuilder.mN.contentView;
7003 return makeBigContentViewWithCustomContent(customRemoteView);
7004 }
7005
7006 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7007 RemoteViews remoteViews = super.makeBigContentView();
7008 if (remoteViews != null) {
7009 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7010 customRemoteView);
7011 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007012 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007013 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7014 customRemoteView);
7015 } else {
7016 return null;
7017 }
7018 }
7019
7020 /**
7021 * @hide
7022 */
7023 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007024 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007025 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7026 ? mBuilder.mN.headsUpContentView
7027 : mBuilder.mN.contentView;
7028 return makeBigContentViewWithCustomContent(customRemoteView);
7029 }
7030
7031 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7032 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007033 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007034 // Need to clone customContent before adding, because otherwise it can no longer be
7035 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007036 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07007037 remoteViews.removeAllViews(id);
7038 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08007039 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007040 return remoteViews;
7041 }
7042 }
7043
Christoph Studer4600f9b2014-07-22 22:44:43 +02007044 // When adding a new Style subclass here, don't forget to update
7045 // Builder.getNotificationStyleClass.
7046
Griff Hazen61a9e862014-05-22 16:05:19 -07007047 /**
7048 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7049 * metadata or change options on a notification builder.
7050 */
7051 public interface Extender {
7052 /**
7053 * Apply this extender to a notification builder.
7054 * @param builder the builder to be modified.
7055 * @return the build object for chaining.
7056 */
7057 public Builder extend(Builder builder);
7058 }
7059
7060 /**
7061 * Helper class to add wearable extensions to notifications.
7062 * <p class="note"> See
7063 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7064 * for Android Wear</a> for more information on how to use this class.
7065 * <p>
7066 * To create a notification with wearable extensions:
7067 * <ol>
7068 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7069 * properties.
7070 * <li>Create a {@link android.app.Notification.WearableExtender}.
7071 * <li>Set wearable-specific properties using the
7072 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7073 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7074 * notification.
7075 * <li>Post the notification to the notification system with the
7076 * {@code NotificationManager.notify(...)} methods.
7077 * </ol>
7078 *
7079 * <pre class="prettyprint">
7080 * Notification notif = new Notification.Builder(mContext)
7081 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7082 * .setContentText(subject)
7083 * .setSmallIcon(R.drawable.new_mail)
7084 * .extend(new Notification.WearableExtender()
7085 * .setContentIcon(R.drawable.new_mail))
7086 * .build();
7087 * NotificationManager notificationManger =
7088 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7089 * notificationManger.notify(0, notif);</pre>
7090 *
7091 * <p>Wearable extensions can be accessed on an existing notification by using the
7092 * {@code WearableExtender(Notification)} constructor,
7093 * and then using the {@code get} methods to access values.
7094 *
7095 * <pre class="prettyprint">
7096 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7097 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007098 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007099 */
7100 public static final class WearableExtender implements Extender {
7101 /**
7102 * Sentinel value for an action index that is unset.
7103 */
7104 public static final int UNSET_ACTION_INDEX = -1;
7105
7106 /**
7107 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7108 * default sizing.
7109 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007110 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007111 * on their content.
7112 */
7113 public static final int SIZE_DEFAULT = 0;
7114
7115 /**
7116 * Size value for use with {@link #setCustomSizePreset} to show this notification
7117 * with an extra small size.
7118 * <p>This value is only applicable for custom display notifications created using
7119 * {@link #setDisplayIntent}.
7120 */
7121 public static final int SIZE_XSMALL = 1;
7122
7123 /**
7124 * Size value for use with {@link #setCustomSizePreset} to show this notification
7125 * with a small size.
7126 * <p>This value is only applicable for custom display notifications created using
7127 * {@link #setDisplayIntent}.
7128 */
7129 public static final int SIZE_SMALL = 2;
7130
7131 /**
7132 * Size value for use with {@link #setCustomSizePreset} to show this notification
7133 * with a medium size.
7134 * <p>This value is only applicable for custom display notifications created using
7135 * {@link #setDisplayIntent}.
7136 */
7137 public static final int SIZE_MEDIUM = 3;
7138
7139 /**
7140 * Size value for use with {@link #setCustomSizePreset} to show this notification
7141 * with a large size.
7142 * <p>This value is only applicable for custom display notifications created using
7143 * {@link #setDisplayIntent}.
7144 */
7145 public static final int SIZE_LARGE = 4;
7146
Griff Hazend5f11f92014-05-27 15:40:09 -07007147 /**
7148 * Size value for use with {@link #setCustomSizePreset} to show this notification
7149 * full screen.
7150 * <p>This value is only applicable for custom display notifications created using
7151 * {@link #setDisplayIntent}.
7152 */
7153 public static final int SIZE_FULL_SCREEN = 5;
7154
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007155 /**
7156 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7157 * short amount of time when this notification is displayed on the screen. This
7158 * is the default value.
7159 */
7160 public static final int SCREEN_TIMEOUT_SHORT = 0;
7161
7162 /**
7163 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7164 * for a longer amount of time when this notification is displayed on the screen.
7165 */
7166 public static final int SCREEN_TIMEOUT_LONG = -1;
7167
Griff Hazen61a9e862014-05-22 16:05:19 -07007168 /** Notification extra which contains wearable extensions */
7169 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7170
Pete Gastaf6781d2014-10-07 15:17:05 -04007171 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007172 private static final String KEY_ACTIONS = "actions";
7173 private static final String KEY_FLAGS = "flags";
7174 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7175 private static final String KEY_PAGES = "pages";
7176 private static final String KEY_BACKGROUND = "background";
7177 private static final String KEY_CONTENT_ICON = "contentIcon";
7178 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7179 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7180 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7181 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7182 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007183 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007184 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007185 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007186
7187 // Flags bitwise-ored to mFlags
7188 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7189 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7190 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7191 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007192 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007193 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007194 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007195
7196 // Default value for flags integer
7197 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7198
7199 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7200 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7201
7202 private ArrayList<Action> mActions = new ArrayList<Action>();
7203 private int mFlags = DEFAULT_FLAGS;
7204 private PendingIntent mDisplayIntent;
7205 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7206 private Bitmap mBackground;
7207 private int mContentIcon;
7208 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7209 private int mContentActionIndex = UNSET_ACTION_INDEX;
7210 private int mCustomSizePreset = SIZE_DEFAULT;
7211 private int mCustomContentHeight;
7212 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007213 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007214 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007215 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007216
7217 /**
7218 * Create a {@link android.app.Notification.WearableExtender} with default
7219 * options.
7220 */
7221 public WearableExtender() {
7222 }
7223
7224 public WearableExtender(Notification notif) {
7225 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7226 if (wearableBundle != null) {
7227 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7228 if (actions != null) {
7229 mActions.addAll(actions);
7230 }
7231
7232 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7233 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7234
7235 Notification[] pages = getNotificationArrayFromBundle(
7236 wearableBundle, KEY_PAGES);
7237 if (pages != null) {
7238 Collections.addAll(mPages, pages);
7239 }
7240
7241 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7242 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7243 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7244 DEFAULT_CONTENT_ICON_GRAVITY);
7245 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7246 UNSET_ACTION_INDEX);
7247 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7248 SIZE_DEFAULT);
7249 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7250 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007251 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007252 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007253 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007254 }
7255 }
7256
7257 /**
7258 * Apply wearable extensions to a notification that is being built. This is typically
7259 * called by the {@link android.app.Notification.Builder#extend} method of
7260 * {@link android.app.Notification.Builder}.
7261 */
7262 @Override
7263 public Notification.Builder extend(Notification.Builder builder) {
7264 Bundle wearableBundle = new Bundle();
7265
7266 if (!mActions.isEmpty()) {
7267 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7268 }
7269 if (mFlags != DEFAULT_FLAGS) {
7270 wearableBundle.putInt(KEY_FLAGS, mFlags);
7271 }
7272 if (mDisplayIntent != null) {
7273 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7274 }
7275 if (!mPages.isEmpty()) {
7276 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7277 new Notification[mPages.size()]));
7278 }
7279 if (mBackground != null) {
7280 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7281 }
7282 if (mContentIcon != 0) {
7283 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7284 }
7285 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7286 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7287 }
7288 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7289 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7290 mContentActionIndex);
7291 }
7292 if (mCustomSizePreset != SIZE_DEFAULT) {
7293 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7294 }
7295 if (mCustomContentHeight != 0) {
7296 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7297 }
7298 if (mGravity != DEFAULT_GRAVITY) {
7299 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7300 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007301 if (mHintScreenTimeout != 0) {
7302 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7303 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007304 if (mDismissalId != null) {
7305 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7306 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007307 if (mBridgeTag != null) {
7308 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7309 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007310
7311 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7312 return builder;
7313 }
7314
7315 @Override
7316 public WearableExtender clone() {
7317 WearableExtender that = new WearableExtender();
7318 that.mActions = new ArrayList<Action>(this.mActions);
7319 that.mFlags = this.mFlags;
7320 that.mDisplayIntent = this.mDisplayIntent;
7321 that.mPages = new ArrayList<Notification>(this.mPages);
7322 that.mBackground = this.mBackground;
7323 that.mContentIcon = this.mContentIcon;
7324 that.mContentIconGravity = this.mContentIconGravity;
7325 that.mContentActionIndex = this.mContentActionIndex;
7326 that.mCustomSizePreset = this.mCustomSizePreset;
7327 that.mCustomContentHeight = this.mCustomContentHeight;
7328 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007329 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007330 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007331 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007332 return that;
7333 }
7334
7335 /**
7336 * Add a wearable action to this notification.
7337 *
7338 * <p>When wearable actions are added using this method, the set of actions that
7339 * show on a wearable device splits from devices that only show actions added
7340 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7341 * of which actions display on different devices.
7342 *
7343 * @param action the action to add to this notification
7344 * @return this object for method chaining
7345 * @see android.app.Notification.Action
7346 */
7347 public WearableExtender addAction(Action action) {
7348 mActions.add(action);
7349 return this;
7350 }
7351
7352 /**
7353 * Adds wearable actions to this notification.
7354 *
7355 * <p>When wearable actions are added using this method, the set of actions that
7356 * show on a wearable device splits from devices that only show actions added
7357 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7358 * of which actions display on different devices.
7359 *
7360 * @param actions the actions to add to this notification
7361 * @return this object for method chaining
7362 * @see android.app.Notification.Action
7363 */
7364 public WearableExtender addActions(List<Action> actions) {
7365 mActions.addAll(actions);
7366 return this;
7367 }
7368
7369 /**
7370 * Clear all wearable actions present on this builder.
7371 * @return this object for method chaining.
7372 * @see #addAction
7373 */
7374 public WearableExtender clearActions() {
7375 mActions.clear();
7376 return this;
7377 }
7378
7379 /**
7380 * Get the wearable actions present on this notification.
7381 */
7382 public List<Action> getActions() {
7383 return mActions;
7384 }
7385
7386 /**
7387 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007388 * this notification. The {@link PendingIntent} provided should be for an activity.
7389 *
7390 * <pre class="prettyprint">
7391 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7392 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7393 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7394 * Notification notif = new Notification.Builder(context)
7395 * .extend(new Notification.WearableExtender()
7396 * .setDisplayIntent(displayPendingIntent)
7397 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7398 * .build();</pre>
7399 *
7400 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007401 * should have an empty task affinity. It is also recommended to use the device
7402 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007403 *
7404 * <p>Example AndroidManifest.xml entry:
7405 * <pre class="prettyprint">
7406 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7407 * android:exported=&quot;true&quot;
7408 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007409 * android:taskAffinity=&quot;&quot;
7410 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007411 *
7412 * @param intent the {@link PendingIntent} for an activity
7413 * @return this object for method chaining
7414 * @see android.app.Notification.WearableExtender#getDisplayIntent
7415 */
7416 public WearableExtender setDisplayIntent(PendingIntent intent) {
7417 mDisplayIntent = intent;
7418 return this;
7419 }
7420
7421 /**
7422 * Get the intent to launch inside of an activity view when displaying this
7423 * notification. This {@code PendingIntent} should be for an activity.
7424 */
7425 public PendingIntent getDisplayIntent() {
7426 return mDisplayIntent;
7427 }
7428
7429 /**
7430 * Add an additional page of content to display with this notification. The current
7431 * notification forms the first page, and pages added using this function form
7432 * subsequent pages. This field can be used to separate a notification into multiple
7433 * sections.
7434 *
7435 * @param page the notification to add as another page
7436 * @return this object for method chaining
7437 * @see android.app.Notification.WearableExtender#getPages
7438 */
7439 public WearableExtender addPage(Notification page) {
7440 mPages.add(page);
7441 return this;
7442 }
7443
7444 /**
7445 * Add additional pages of content to display with this notification. The current
7446 * notification forms the first page, and pages added using this function form
7447 * subsequent pages. This field can be used to separate a notification into multiple
7448 * sections.
7449 *
7450 * @param pages a list of notifications
7451 * @return this object for method chaining
7452 * @see android.app.Notification.WearableExtender#getPages
7453 */
7454 public WearableExtender addPages(List<Notification> pages) {
7455 mPages.addAll(pages);
7456 return this;
7457 }
7458
7459 /**
7460 * Clear all additional pages present on this builder.
7461 * @return this object for method chaining.
7462 * @see #addPage
7463 */
7464 public WearableExtender clearPages() {
7465 mPages.clear();
7466 return this;
7467 }
7468
7469 /**
7470 * Get the array of additional pages of content for displaying this notification. The
7471 * current notification forms the first page, and elements within this array form
7472 * subsequent pages. This field can be used to separate a notification into multiple
7473 * sections.
7474 * @return the pages for this notification
7475 */
7476 public List<Notification> getPages() {
7477 return mPages;
7478 }
7479
7480 /**
7481 * Set a background image to be displayed behind the notification content.
7482 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7483 * will work with any notification style.
7484 *
7485 * @param background the background bitmap
7486 * @return this object for method chaining
7487 * @see android.app.Notification.WearableExtender#getBackground
7488 */
7489 public WearableExtender setBackground(Bitmap background) {
7490 mBackground = background;
7491 return this;
7492 }
7493
7494 /**
7495 * Get a background image to be displayed behind the notification content.
7496 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7497 * will work with any notification style.
7498 *
7499 * @return the background image
7500 * @see android.app.Notification.WearableExtender#setBackground
7501 */
7502 public Bitmap getBackground() {
7503 return mBackground;
7504 }
7505
7506 /**
7507 * Set an icon that goes with the content of this notification.
7508 */
7509 public WearableExtender setContentIcon(int icon) {
7510 mContentIcon = icon;
7511 return this;
7512 }
7513
7514 /**
7515 * Get an icon that goes with the content of this notification.
7516 */
7517 public int getContentIcon() {
7518 return mContentIcon;
7519 }
7520
7521 /**
7522 * Set the gravity that the content icon should have within the notification display.
7523 * Supported values include {@link android.view.Gravity#START} and
7524 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7525 * @see #setContentIcon
7526 */
7527 public WearableExtender setContentIconGravity(int contentIconGravity) {
7528 mContentIconGravity = contentIconGravity;
7529 return this;
7530 }
7531
7532 /**
7533 * Get the gravity that the content icon should have within the notification display.
7534 * Supported values include {@link android.view.Gravity#START} and
7535 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7536 * @see #getContentIcon
7537 */
7538 public int getContentIconGravity() {
7539 return mContentIconGravity;
7540 }
7541
7542 /**
7543 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007544 * this notification. This action will no longer display separately from the
7545 * notification's content.
7546 *
Griff Hazenca48d352014-05-28 22:37:13 -07007547 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007548 * set, although the list of available actions comes from the main notification and not
7549 * from the child page's notification.
7550 *
7551 * @param actionIndex The index of the action to hoist onto the current notification page.
7552 * If wearable actions were added to the main notification, this index
7553 * will apply to that list, otherwise it will apply to the regular
7554 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007555 */
7556 public WearableExtender setContentAction(int actionIndex) {
7557 mContentActionIndex = actionIndex;
7558 return this;
7559 }
7560
7561 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007562 * Get the index of the notification action, if any, that was specified as being clickable
7563 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007564 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007565 *
Griff Hazenca48d352014-05-28 22:37:13 -07007566 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007567 * set, although the list of available actions comes from the main notification and not
7568 * from the child page's notification.
7569 *
7570 * <p>If wearable specific actions were added to the main notification, this index will
7571 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007572 *
7573 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007574 */
7575 public int getContentAction() {
7576 return mContentActionIndex;
7577 }
7578
7579 /**
7580 * Set the gravity that this notification should have within the available viewport space.
7581 * Supported values include {@link android.view.Gravity#TOP},
7582 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7583 * The default value is {@link android.view.Gravity#BOTTOM}.
7584 */
7585 public WearableExtender setGravity(int gravity) {
7586 mGravity = gravity;
7587 return this;
7588 }
7589
7590 /**
7591 * Get the gravity that this notification should have within the available viewport space.
7592 * Supported values include {@link android.view.Gravity#TOP},
7593 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7594 * The default value is {@link android.view.Gravity#BOTTOM}.
7595 */
7596 public int getGravity() {
7597 return mGravity;
7598 }
7599
7600 /**
7601 * Set the custom size preset for the display of this notification out of the available
7602 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7603 * {@link #SIZE_LARGE}.
7604 * <p>Some custom size presets are only applicable for custom display notifications created
7605 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7606 * documentation for the preset in question. See also
7607 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7608 */
7609 public WearableExtender setCustomSizePreset(int sizePreset) {
7610 mCustomSizePreset = sizePreset;
7611 return this;
7612 }
7613
7614 /**
7615 * Get the custom size preset for the display of this notification out of the available
7616 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7617 * {@link #SIZE_LARGE}.
7618 * <p>Some custom size presets are only applicable for custom display notifications created
7619 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7620 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7621 */
7622 public int getCustomSizePreset() {
7623 return mCustomSizePreset;
7624 }
7625
7626 /**
7627 * Set the custom height in pixels for the display of this notification's content.
7628 * <p>This option is only available for custom display notifications created
7629 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7630 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7631 * {@link #getCustomContentHeight}.
7632 */
7633 public WearableExtender setCustomContentHeight(int height) {
7634 mCustomContentHeight = height;
7635 return this;
7636 }
7637
7638 /**
7639 * Get the custom height in pixels for the display of this notification's content.
7640 * <p>This option is only available for custom display notifications created
7641 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7642 * {@link #setCustomContentHeight}.
7643 */
7644 public int getCustomContentHeight() {
7645 return mCustomContentHeight;
7646 }
7647
7648 /**
7649 * Set whether the scrolling position for the contents of this notification should start
7650 * at the bottom of the contents instead of the top when the contents are too long to
7651 * display within the screen. Default is false (start scroll at the top).
7652 */
7653 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7654 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7655 return this;
7656 }
7657
7658 /**
7659 * Get whether the scrolling position for the contents of this notification should start
7660 * at the bottom of the contents instead of the top when the contents are too long to
7661 * display within the screen. Default is false (start scroll at the top).
7662 */
7663 public boolean getStartScrollBottom() {
7664 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7665 }
7666
7667 /**
7668 * Set whether the content intent is available when the wearable device is not connected
7669 * to a companion device. The user can still trigger this intent when the wearable device
7670 * is offline, but a visual hint will indicate that the content intent may not be available.
7671 * Defaults to true.
7672 */
7673 public WearableExtender setContentIntentAvailableOffline(
7674 boolean contentIntentAvailableOffline) {
7675 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7676 return this;
7677 }
7678
7679 /**
7680 * Get whether the content intent is available when the wearable device is not connected
7681 * to a companion device. The user can still trigger this intent when the wearable device
7682 * is offline, but a visual hint will indicate that the content intent may not be available.
7683 * Defaults to true.
7684 */
7685 public boolean getContentIntentAvailableOffline() {
7686 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7687 }
7688
7689 /**
7690 * Set a hint that this notification's icon should not be displayed.
7691 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7692 * @return this object for method chaining
7693 */
7694 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7695 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7696 return this;
7697 }
7698
7699 /**
7700 * Get a hint that this notification's icon should not be displayed.
7701 * @return {@code true} if this icon should not be displayed, false otherwise.
7702 * The default value is {@code false} if this was never set.
7703 */
7704 public boolean getHintHideIcon() {
7705 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7706 }
7707
7708 /**
7709 * Set a visual hint that only the background image of this notification should be
7710 * displayed, and other semantic content should be hidden. This hint is only applicable
7711 * to sub-pages added using {@link #addPage}.
7712 */
7713 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7714 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7715 return this;
7716 }
7717
7718 /**
7719 * Get a visual hint that only the background image of this notification should be
7720 * displayed, and other semantic content should be hidden. This hint is only applicable
7721 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7722 */
7723 public boolean getHintShowBackgroundOnly() {
7724 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7725 }
7726
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007727 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007728 * Set a hint that this notification's background should not be clipped if possible,
7729 * and should instead be resized to fully display on the screen, retaining the aspect
7730 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007731 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7732 * @return this object for method chaining
7733 */
7734 public WearableExtender setHintAvoidBackgroundClipping(
7735 boolean hintAvoidBackgroundClipping) {
7736 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7737 return this;
7738 }
7739
7740 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007741 * Get a hint that this notification's background should not be clipped if possible,
7742 * and should instead be resized to fully display on the screen, retaining the aspect
7743 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007744 * @return {@code true} if it's ok if the background is clipped on the screen, false
7745 * otherwise. The default value is {@code false} if this was never set.
7746 */
7747 public boolean getHintAvoidBackgroundClipping() {
7748 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7749 }
7750
7751 /**
7752 * Set a hint that the screen should remain on for at least this duration when
7753 * this notification is displayed on the screen.
7754 * @param timeout The requested screen timeout in milliseconds. Can also be either
7755 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7756 * @return this object for method chaining
7757 */
7758 public WearableExtender setHintScreenTimeout(int timeout) {
7759 mHintScreenTimeout = timeout;
7760 return this;
7761 }
7762
7763 /**
7764 * Get the duration, in milliseconds, that the screen should remain on for
7765 * when this notification is displayed.
7766 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7767 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7768 */
7769 public int getHintScreenTimeout() {
7770 return mHintScreenTimeout;
7771 }
7772
Alex Hills9ab3a232016-04-05 14:54:56 -04007773 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007774 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7775 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7776 * qr codes, as well as other simple black-and-white tickets.
7777 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7778 * @return this object for method chaining
7779 */
7780 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7781 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7782 return this;
7783 }
7784
7785 /**
7786 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7787 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7788 * qr codes, as well as other simple black-and-white tickets.
7789 * @return {@code true} if it should be displayed in ambient, false otherwise
7790 * otherwise. The default value is {@code false} if this was never set.
7791 */
7792 public boolean getHintAmbientBigPicture() {
7793 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7794 }
7795
7796 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007797 * Set a hint that this notification's content intent will launch an {@link Activity}
7798 * directly, telling the platform that it can generate the appropriate transitions.
7799 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7800 * an activity and transitions should be generated, false otherwise.
7801 * @return this object for method chaining
7802 */
7803 public WearableExtender setHintContentIntentLaunchesActivity(
7804 boolean hintContentIntentLaunchesActivity) {
7805 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7806 return this;
7807 }
7808
7809 /**
7810 * Get a hint that this notification's content intent will launch an {@link Activity}
7811 * directly, telling the platform that it can generate the appropriate transitions
7812 * @return {@code true} if the content intent will launch an activity and transitions should
7813 * be generated, false otherwise. The default value is {@code false} if this was never set.
7814 */
7815 public boolean getHintContentIntentLaunchesActivity() {
7816 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7817 }
7818
Nadia Benbernou948627e2016-04-14 14:41:08 -04007819 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007820 * Sets the dismissal id for this notification. If a notification is posted with a
7821 * dismissal id, then when that notification is canceled, notifications on other wearables
7822 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007823 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007824 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007825 * @param dismissalId the dismissal id of the notification.
7826 * @return this object for method chaining
7827 */
7828 public WearableExtender setDismissalId(String dismissalId) {
7829 mDismissalId = dismissalId;
7830 return this;
7831 }
7832
7833 /**
7834 * Returns the dismissal id of the notification.
7835 * @return the dismissal id of the notification or null if it has not been set.
7836 */
7837 public String getDismissalId() {
7838 return mDismissalId;
7839 }
7840
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007841 /**
7842 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7843 * posted from a phone to provide finer-grained control on what notifications are bridged
7844 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7845 * Features to Notifications</a> for more information.
7846 * @param bridgeTag the bridge tag of the notification.
7847 * @return this object for method chaining
7848 */
7849 public WearableExtender setBridgeTag(String bridgeTag) {
7850 mBridgeTag = bridgeTag;
7851 return this;
7852 }
7853
7854 /**
7855 * Returns the bridge tag of the notification.
7856 * @return the bridge tag or null if not present.
7857 */
7858 public String getBridgeTag() {
7859 return mBridgeTag;
7860 }
7861
Griff Hazen61a9e862014-05-22 16:05:19 -07007862 private void setFlag(int mask, boolean value) {
7863 if (value) {
7864 mFlags |= mask;
7865 } else {
7866 mFlags &= ~mask;
7867 }
7868 }
7869 }
7870
7871 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007872 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7873 * with car extensions:
7874 *
7875 * <ol>
7876 * <li>Create an {@link Notification.Builder}, setting any desired
7877 * properties.
7878 * <li>Create a {@link CarExtender}.
7879 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7880 * {@link CarExtender}.
7881 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7882 * to apply the extensions to a notification.
7883 * </ol>
7884 *
7885 * <pre class="prettyprint">
7886 * Notification notification = new Notification.Builder(context)
7887 * ...
7888 * .extend(new CarExtender()
7889 * .set*(...))
7890 * .build();
7891 * </pre>
7892 *
7893 * <p>Car extensions can be accessed on an existing notification by using the
7894 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7895 * to access values.
7896 */
7897 public static final class CarExtender implements Extender {
7898 private static final String TAG = "CarExtender";
7899
7900 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7901 private static final String EXTRA_LARGE_ICON = "large_icon";
7902 private static final String EXTRA_CONVERSATION = "car_conversation";
7903 private static final String EXTRA_COLOR = "app_color";
7904
7905 private Bitmap mLargeIcon;
7906 private UnreadConversation mUnreadConversation;
7907 private int mColor = Notification.COLOR_DEFAULT;
7908
7909 /**
7910 * Create a {@link CarExtender} with default options.
7911 */
7912 public CarExtender() {
7913 }
7914
7915 /**
7916 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7917 *
7918 * @param notif The notification from which to copy options.
7919 */
7920 public CarExtender(Notification notif) {
7921 Bundle carBundle = notif.extras == null ?
7922 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7923 if (carBundle != null) {
7924 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7925 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7926
7927 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7928 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7929 }
7930 }
7931
7932 /**
7933 * Apply car extensions to a notification that is being built. This is typically called by
7934 * the {@link Notification.Builder#extend(Notification.Extender)}
7935 * method of {@link Notification.Builder}.
7936 */
7937 @Override
7938 public Notification.Builder extend(Notification.Builder builder) {
7939 Bundle carExtensions = new Bundle();
7940
7941 if (mLargeIcon != null) {
7942 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7943 }
7944 if (mColor != Notification.COLOR_DEFAULT) {
7945 carExtensions.putInt(EXTRA_COLOR, mColor);
7946 }
7947
7948 if (mUnreadConversation != null) {
7949 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7950 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7951 }
7952
7953 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7954 return builder;
7955 }
7956
7957 /**
7958 * Sets the accent color to use when Android Auto presents the notification.
7959 *
7960 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7961 * to accent the displayed notification. However, not all colors are acceptable in an
7962 * automotive setting. This method can be used to override the color provided in the
7963 * notification in such a situation.
7964 */
Tor Norbye80756e32015-03-02 09:39:27 -08007965 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007966 mColor = color;
7967 return this;
7968 }
7969
7970 /**
7971 * Gets the accent color.
7972 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007973 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007974 */
Tor Norbye80756e32015-03-02 09:39:27 -08007975 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007976 public int getColor() {
7977 return mColor;
7978 }
7979
7980 /**
7981 * Sets the large icon of the car notification.
7982 *
7983 * If no large icon is set in the extender, Android Auto will display the icon
7984 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7985 *
7986 * @param largeIcon The large icon to use in the car notification.
7987 * @return This object for method chaining.
7988 */
7989 public CarExtender setLargeIcon(Bitmap largeIcon) {
7990 mLargeIcon = largeIcon;
7991 return this;
7992 }
7993
7994 /**
7995 * Gets the large icon used in this car notification, or null if no icon has been set.
7996 *
7997 * @return The large icon for the car notification.
7998 * @see CarExtender#setLargeIcon
7999 */
8000 public Bitmap getLargeIcon() {
8001 return mLargeIcon;
8002 }
8003
8004 /**
8005 * Sets the unread conversation in a message notification.
8006 *
8007 * @param unreadConversation The unread part of the conversation this notification conveys.
8008 * @return This object for method chaining.
8009 */
8010 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8011 mUnreadConversation = unreadConversation;
8012 return this;
8013 }
8014
8015 /**
8016 * Returns the unread conversation conveyed by this notification.
8017 * @see #setUnreadConversation(UnreadConversation)
8018 */
8019 public UnreadConversation getUnreadConversation() {
8020 return mUnreadConversation;
8021 }
8022
8023 /**
8024 * A class which holds the unread messages from a conversation.
8025 */
8026 public static class UnreadConversation {
8027 private static final String KEY_AUTHOR = "author";
8028 private static final String KEY_TEXT = "text";
8029 private static final String KEY_MESSAGES = "messages";
8030 private static final String KEY_REMOTE_INPUT = "remote_input";
8031 private static final String KEY_ON_REPLY = "on_reply";
8032 private static final String KEY_ON_READ = "on_read";
8033 private static final String KEY_PARTICIPANTS = "participants";
8034 private static final String KEY_TIMESTAMP = "timestamp";
8035
8036 private final String[] mMessages;
8037 private final RemoteInput mRemoteInput;
8038 private final PendingIntent mReplyPendingIntent;
8039 private final PendingIntent mReadPendingIntent;
8040 private final String[] mParticipants;
8041 private final long mLatestTimestamp;
8042
8043 UnreadConversation(String[] messages, RemoteInput remoteInput,
8044 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8045 String[] participants, long latestTimestamp) {
8046 mMessages = messages;
8047 mRemoteInput = remoteInput;
8048 mReadPendingIntent = readPendingIntent;
8049 mReplyPendingIntent = replyPendingIntent;
8050 mParticipants = participants;
8051 mLatestTimestamp = latestTimestamp;
8052 }
8053
8054 /**
8055 * Gets the list of messages conveyed by this notification.
8056 */
8057 public String[] getMessages() {
8058 return mMessages;
8059 }
8060
8061 /**
8062 * Gets the remote input that will be used to convey the response to a message list, or
8063 * null if no such remote input exists.
8064 */
8065 public RemoteInput getRemoteInput() {
8066 return mRemoteInput;
8067 }
8068
8069 /**
8070 * Gets the pending intent that will be triggered when the user replies to this
8071 * notification.
8072 */
8073 public PendingIntent getReplyPendingIntent() {
8074 return mReplyPendingIntent;
8075 }
8076
8077 /**
8078 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8079 * in this object's message list.
8080 */
8081 public PendingIntent getReadPendingIntent() {
8082 return mReadPendingIntent;
8083 }
8084
8085 /**
8086 * Gets the participants in the conversation.
8087 */
8088 public String[] getParticipants() {
8089 return mParticipants;
8090 }
8091
8092 /**
8093 * Gets the firs participant in the conversation.
8094 */
8095 public String getParticipant() {
8096 return mParticipants.length > 0 ? mParticipants[0] : null;
8097 }
8098
8099 /**
8100 * Gets the timestamp of the conversation.
8101 */
8102 public long getLatestTimestamp() {
8103 return mLatestTimestamp;
8104 }
8105
8106 Bundle getBundleForUnreadConversation() {
8107 Bundle b = new Bundle();
8108 String author = null;
8109 if (mParticipants != null && mParticipants.length > 1) {
8110 author = mParticipants[0];
8111 }
8112 Parcelable[] messages = new Parcelable[mMessages.length];
8113 for (int i = 0; i < messages.length; i++) {
8114 Bundle m = new Bundle();
8115 m.putString(KEY_TEXT, mMessages[i]);
8116 m.putString(KEY_AUTHOR, author);
8117 messages[i] = m;
8118 }
8119 b.putParcelableArray(KEY_MESSAGES, messages);
8120 if (mRemoteInput != null) {
8121 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8122 }
8123 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8124 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8125 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8126 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8127 return b;
8128 }
8129
8130 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8131 if (b == null) {
8132 return null;
8133 }
8134 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8135 String[] messages = null;
8136 if (parcelableMessages != null) {
8137 String[] tmp = new String[parcelableMessages.length];
8138 boolean success = true;
8139 for (int i = 0; i < tmp.length; i++) {
8140 if (!(parcelableMessages[i] instanceof Bundle)) {
8141 success = false;
8142 break;
8143 }
8144 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8145 if (tmp[i] == null) {
8146 success = false;
8147 break;
8148 }
8149 }
8150 if (success) {
8151 messages = tmp;
8152 } else {
8153 return null;
8154 }
8155 }
8156
8157 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8158 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8159
8160 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8161
8162 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8163 if (participants == null || participants.length != 1) {
8164 return null;
8165 }
8166
8167 return new UnreadConversation(messages,
8168 remoteInput,
8169 onReply,
8170 onRead,
8171 participants, b.getLong(KEY_TIMESTAMP));
8172 }
8173 };
8174
8175 /**
8176 * Builder class for {@link CarExtender.UnreadConversation} objects.
8177 */
8178 public static class Builder {
8179 private final List<String> mMessages = new ArrayList<String>();
8180 private final String mParticipant;
8181 private RemoteInput mRemoteInput;
8182 private PendingIntent mReadPendingIntent;
8183 private PendingIntent mReplyPendingIntent;
8184 private long mLatestTimestamp;
8185
8186 /**
8187 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8188 *
8189 * @param name The name of the other participant in the conversation.
8190 */
8191 public Builder(String name) {
8192 mParticipant = name;
8193 }
8194
8195 /**
8196 * Appends a new unread message to the list of messages for this conversation.
8197 *
8198 * The messages should be added from oldest to newest.
8199 *
8200 * @param message The text of the new unread message.
8201 * @return This object for method chaining.
8202 */
8203 public Builder addMessage(String message) {
8204 mMessages.add(message);
8205 return this;
8206 }
8207
8208 /**
8209 * Sets the pending intent and remote input which will convey the reply to this
8210 * notification.
8211 *
8212 * @param pendingIntent The pending intent which will be triggered on a reply.
8213 * @param remoteInput The remote input parcelable which will carry the reply.
8214 * @return This object for method chaining.
8215 *
8216 * @see CarExtender.UnreadConversation#getRemoteInput
8217 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8218 */
8219 public Builder setReplyAction(
8220 PendingIntent pendingIntent, RemoteInput remoteInput) {
8221 mRemoteInput = remoteInput;
8222 mReplyPendingIntent = pendingIntent;
8223
8224 return this;
8225 }
8226
8227 /**
8228 * Sets the pending intent that will be sent once the messages in this notification
8229 * are read.
8230 *
8231 * @param pendingIntent The pending intent to use.
8232 * @return This object for method chaining.
8233 */
8234 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8235 mReadPendingIntent = pendingIntent;
8236 return this;
8237 }
8238
8239 /**
8240 * Sets the timestamp of the most recent message in an unread conversation.
8241 *
8242 * If a messaging notification has been posted by your application and has not
8243 * yet been cancelled, posting a later notification with the same id and tag
8244 * but without a newer timestamp may result in Android Auto not displaying a
8245 * heads up notification for the later notification.
8246 *
8247 * @param timestamp The timestamp of the most recent message in the conversation.
8248 * @return This object for method chaining.
8249 */
8250 public Builder setLatestTimestamp(long timestamp) {
8251 mLatestTimestamp = timestamp;
8252 return this;
8253 }
8254
8255 /**
8256 * Builds a new unread conversation object.
8257 *
8258 * @return The new unread conversation object.
8259 */
8260 public UnreadConversation build() {
8261 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8262 String[] participants = { mParticipant };
8263 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8264 mReadPendingIntent, participants, mLatestTimestamp);
8265 }
8266 }
8267 }
8268
8269 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008270 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8271 * with a TV extension:
8272 *
8273 * <ol>
8274 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8275 * <li>Create a {@link TvExtender}.
8276 * <li>Set TV-specific properties using the {@code set} methods of
8277 * {@link TvExtender}.
8278 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8279 * to apply the extension to a notification.
8280 * </ol>
8281 *
8282 * <pre class="prettyprint">
8283 * Notification notification = new Notification.Builder(context)
8284 * ...
8285 * .extend(new TvExtender()
8286 * .set*(...))
8287 * .build();
8288 * </pre>
8289 *
8290 * <p>TV extensions can be accessed on an existing notification by using the
8291 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8292 * to access values.
8293 *
8294 * @hide
8295 */
8296 @SystemApi
8297 public static final class TvExtender implements Extender {
8298 private static final String TAG = "TvExtender";
8299
8300 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8301 private static final String EXTRA_FLAGS = "flags";
8302 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8303 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008304 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008305
8306 // Flags bitwise-ored to mFlags
8307 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8308
8309 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008310 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008311 private PendingIntent mContentIntent;
8312 private PendingIntent mDeleteIntent;
8313
8314 /**
8315 * Create a {@link TvExtender} with default options.
8316 */
8317 public TvExtender() {
8318 mFlags = FLAG_AVAILABLE_ON_TV;
8319 }
8320
8321 /**
8322 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8323 *
8324 * @param notif The notification from which to copy options.
8325 */
8326 public TvExtender(Notification notif) {
8327 Bundle bundle = notif.extras == null ?
8328 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8329 if (bundle != null) {
8330 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008331 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008332 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8333 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8334 }
8335 }
8336
8337 /**
8338 * Apply a TV extension to a notification that is being built. This is typically called by
8339 * the {@link Notification.Builder#extend(Notification.Extender)}
8340 * method of {@link Notification.Builder}.
8341 */
8342 @Override
8343 public Notification.Builder extend(Notification.Builder builder) {
8344 Bundle bundle = new Bundle();
8345
8346 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008347 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008348 if (mContentIntent != null) {
8349 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8350 }
8351
8352 if (mDeleteIntent != null) {
8353 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8354 }
8355
8356 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8357 return builder;
8358 }
8359
8360 /**
8361 * Returns true if this notification should be shown on TV. This method return true
8362 * if the notification was extended with a TvExtender.
8363 */
8364 public boolean isAvailableOnTv() {
8365 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8366 }
8367
8368 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008369 * Specifies the channel the notification should be delivered on when shown on TV.
8370 * It can be different from the channel that the notification is delivered to when
8371 * posting on a non-TV device.
8372 */
8373 public TvExtender setChannel(String channelId) {
8374 mChannelId = channelId;
8375 return this;
8376 }
8377
8378 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008379 * Specifies the channel the notification should be delivered on when shown on TV.
8380 * It can be different from the channel that the notification is delivered to when
8381 * posting on a non-TV device.
8382 */
8383 public TvExtender setChannelId(String channelId) {
8384 mChannelId = channelId;
8385 return this;
8386 }
8387
Jeff Sharkey000ce802017-04-29 13:13:27 -06008388 /** @removed */
8389 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008390 public String getChannel() {
8391 return mChannelId;
8392 }
8393
8394 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008395 * Returns the id of the channel this notification posts to on TV.
8396 */
8397 public String getChannelId() {
8398 return mChannelId;
8399 }
8400
8401 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008402 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8403 * If provided, it is used instead of the content intent specified
8404 * at the level of Notification.
8405 */
8406 public TvExtender setContentIntent(PendingIntent intent) {
8407 mContentIntent = intent;
8408 return this;
8409 }
8410
8411 /**
8412 * Returns the TV-specific content intent. If this method returns null, the
8413 * main content intent on the notification should be used.
8414 *
8415 * @see {@link Notification#contentIntent}
8416 */
8417 public PendingIntent getContentIntent() {
8418 return mContentIntent;
8419 }
8420
8421 /**
8422 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8423 * by the user on TV. If provided, it is used instead of the delete intent specified
8424 * at the level of Notification.
8425 */
8426 public TvExtender setDeleteIntent(PendingIntent intent) {
8427 mDeleteIntent = intent;
8428 return this;
8429 }
8430
8431 /**
8432 * Returns the TV-specific delete intent. If this method returns null, the
8433 * main delete intent on the notification should be used.
8434 *
8435 * @see {@link Notification#deleteIntent}
8436 */
8437 public PendingIntent getDeleteIntent() {
8438 return mDeleteIntent;
8439 }
8440 }
8441
8442 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008443 * Get an array of Notification objects from a parcelable array bundle field.
8444 * Update the bundle to have a typed array so fetches in the future don't need
8445 * to do an array copy.
8446 */
8447 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8448 Parcelable[] array = bundle.getParcelableArray(key);
8449 if (array instanceof Notification[] || array == null) {
8450 return (Notification[]) array;
8451 }
8452 Notification[] typedArray = Arrays.copyOf(array, array.length,
8453 Notification[].class);
8454 bundle.putParcelableArray(key, typedArray);
8455 return typedArray;
8456 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008457
8458 private static class BuilderRemoteViews extends RemoteViews {
8459 public BuilderRemoteViews(Parcel parcel) {
8460 super(parcel);
8461 }
8462
Kenny Guy77320062014-08-27 21:37:15 +01008463 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8464 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008465 }
8466
8467 @Override
8468 public BuilderRemoteViews clone() {
8469 Parcel p = Parcel.obtain();
8470 writeToParcel(p, 0);
8471 p.setDataPosition(0);
8472 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8473 p.recycle();
8474 return brv;
8475 }
8476 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008477
8478 private static class StandardTemplateParams {
8479 boolean hasProgress = true;
8480 boolean ambient = false;
8481 CharSequence title;
8482 CharSequence text;
8483
8484 final StandardTemplateParams reset() {
8485 hasProgress = true;
8486 ambient = false;
8487 title = null;
8488 text = null;
8489 return this;
8490 }
8491
8492 final StandardTemplateParams hasProgress(boolean hasProgress) {
8493 this.hasProgress = hasProgress;
8494 return this;
8495 }
8496
8497 final StandardTemplateParams title(CharSequence title) {
8498 this.title = title;
8499 return this;
8500 }
8501
8502 final StandardTemplateParams text(CharSequence text) {
8503 this.text = text;
8504 return this;
8505 }
8506
8507 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008508 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008509 this.ambient = ambient;
8510 return this;
8511 }
8512
8513 final StandardTemplateParams fillTextsFrom(Builder b) {
8514 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008515 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8516 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008517 return this;
8518 }
8519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520}