blob: 6004a83b9dcfbd38459dc014415e9776b67b6a20 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050092import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094/**
95 * A class that represents how a persistent notification is to be presented to
96 * the user using the {@link android.app.NotificationManager}.
97 *
Joe Onoratocb109a02011-01-18 17:57:41 -080098 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
99 * easier to construct Notifications.</p>
100 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For a guide to creating notifications, read the
104 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
105 * developer guide.</p>
106 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
108public class Notification implements Parcelable
109{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400110 private static final String TAG = "Notification";
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400114 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 */
116 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
117 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
118 = "android.intent.category.NOTIFICATION_PREFERENCES";
119
120 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
122 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 */
125 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
126
127 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400129 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
130 * what settings should be shown in the target app.
131 */
132 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
133
134 /**
135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
137 * that can be used to narrow down what settings should be shown in the target app.
138 */
139 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
143 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Use all default values (where applicable).
150 */
151 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Use the default notification sound. This will ignore any given
155 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
Chris Wren47c20a12014-06-18 17:27:29 -0400157 * <p>
158 * A notification that is noisy is more likely to be presented as a heads-up notification.
159 * </p>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 */
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int DEFAULT_SOUND = 1;
165
166 /**
167 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700169 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 *
Chris Wren47c20a12014-06-18 17:27:29 -0400171 * <p>
172 * A notification that vibrates is more likely to be presented as a heads-up notification.
173 * </p>
174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * Use the default notification lights. This will ignore the
182 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
183 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 */
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200191 * Maximum length of CharSequences accepted by Builder and friends.
192 *
193 * <p>
194 * Avoids spamming the system with overly large strings such as full e-mails.
195 */
196 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
197
198 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800199 * Maximum entries of reply text that are accepted by Builder and friends.
200 */
201 private static final int MAX_REPLY_HISTORY = 5;
202
203 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800205 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500206 * Default value: {@link System#currentTimeMillis() Now}.
207 *
208 * Choose a timestamp that will be most relevant to the user. For most finite events, this
209 * corresponds to the time the event happened (or will happen, in the case of events that have
210 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800211 * timestamped according to when the activity began.
212 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800214 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * <ul>
216 * <li>Notification of a new chat message should be stamped when the message was received.</li>
217 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
218 * <li>Notification of a completed file download should be stamped when the download finished.</li>
219 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
220 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
221 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 * </ul>
223 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700224 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
225 * anymore by default and must be opted into by using
226 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public long when;
229
230 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700231 * The creation time of the notification
232 */
233 private long creationTime;
234
235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400237 *
238 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
Dan Sandler86647982015-05-13 23:41:13 -0400240 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700241 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public int icon;
243
244 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800245 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
246 * leave it at its default value of 0.
247 *
248 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700249 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 */
251 public int iconLevel;
252
253 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500254 * The number of events that this notification represents. For example, in a new mail
255 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800256 *
Julia Reynolds30331982017-04-27 10:12:50 -0400257 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500258 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
259 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 */
Julia Reynolds30331982017-04-27 10:12:50 -0400261 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 /**
264 * The intent to execute when the expanded status entry is clicked. If
265 * this is an activity, it must include the
266 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800267 * that you take care of task management as described in the
268 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800269 * Stack</a> document. In particular, make sure to read the notification section
270 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
271 * Notifications</a> for the correct ways to launch an application from a
272 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
274 public PendingIntent contentIntent;
275
276 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500277 * The intent to execute when the notification is explicitly dismissed by the user, either with
278 * the "Clear All" button or by swiping it away individually.
279 *
280 * This probably shouldn't be launching an activity since several of those will be sent
281 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public PendingIntent deleteIntent;
284
285 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700286 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800287 *
Chris Wren47c20a12014-06-18 17:27:29 -0400288 * <p>
289 * The system UI may choose to display a heads-up notification, instead of
290 * launching this intent, while the user is using the device.
291 * </p>
292 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800293 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400294 */
295 public PendingIntent fullScreenIntent;
296
297 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400298 * Text that summarizes this notification for accessibility services.
299 *
300 * As of the L release, this text is no longer shown on screen, but it is still useful to
301 * accessibility services (where it serves as an audible announcement of the notification's
302 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400303 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 */
306 public CharSequence tickerText;
307
308 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400309 * Formerly, a view showing the {@link #tickerText}.
310 *
311 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400312 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400313 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400315
316 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 * The view that will represent this notification in the notification list (which is pulled
318 * down from the status bar).
319 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500320 * As of N, this field may be null. The notification view is determined by the inputs
321 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400322 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400324 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public RemoteViews contentView;
326
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400327 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400328 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400329 * opportunity to show more detail. The system UI may choose to show this
330 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400331 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500332 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400333 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
334 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400336 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400337 public RemoteViews bigContentView;
338
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339
340 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400341 * A medium-format version of {@link #contentView}, providing the Notification an
342 * opportunity to add action buttons to contentView. At its discretion, the system UI may
343 * choose to show this as a heads-up notification, which will pop up so the user can see
344 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500346 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
348 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500351 public RemoteViews headsUpContentView;
352
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400353 /**
Dan Sandler86647982015-05-13 23:41:13 -0400354 * A large bitmap to be shown in the notification content area.
355 *
356 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Dan Sandler86647982015-05-13 23:41:13 -0400358 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800359 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
361 /**
362 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400365 * A notification that is noisy is more likely to be presented as a heads-up notification.
366 * </p>
367 *
368 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public Uri sound;
375
376 /**
377 * Use this constant as the value for audioStreamType to request that
378 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700379 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public static final int STREAM_DEFAULT = -1;
385
386 /**
387 * The audio stream type to use when playing the sound.
388 * Should be one of the STREAM_ constants from
389 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400390 *
391 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public int audioStreamType = STREAM_DEFAULT;
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400397 * The default value of {@link #audioAttributes}.
398 */
399 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
400 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
401 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
402 .build();
403
404 /**
405 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500406 *
407 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400408 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400410 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
411
412 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500413 * The pattern with which to vibrate.
414 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * <p>
416 * To vibrate the default pattern, see {@link #defaults}.
417 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public long[] vibrate;
424
425 /**
426 * The color of the led. The hardware will do its best approximation.
427 *
428 * @see #FLAG_SHOW_LIGHTS
429 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Tor Norbye80756e32015-03-02 09:39:27 -0800432 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public int ledARGB;
435
436 /**
437 * The number of milliseconds for the LED to be on while it's flashing.
438 * The hardware will do its best approximation.
439 *
440 * @see #FLAG_SHOW_LIGHTS
441 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500444 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 public int ledOnMS;
446
447 /**
448 * The number of milliseconds for the LED to be off while it's flashing.
449 * The hardware will do its best approximation.
450 *
451 * @see #FLAG_SHOW_LIGHTS
452 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500453 *
454 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public int ledOffMS;
458
459 /**
460 * Specifies which values should be taken from the defaults.
461 * <p>
462 * To set, OR the desired from {@link #DEFAULT_SOUND},
463 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
464 * values, use {@link #DEFAULT_ALL}.
465 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 *
467 * @deprecated use {@link NotificationChannel#getSound()} and
468 * {@link NotificationChannel#shouldShowLights()} and
469 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 public int defaults;
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
475 * Bit to be bitwise-ored into the {@link #flags} field that should be
476 * set if you want the LED on for this notification.
477 * <ul>
478 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
479 * or 0 for both ledOnMS and ledOffMS.</li>
480 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
481 * <li>To flash the LED, pass the number of milliseconds that it should
482 * be on and off to ledOnMS and ledOffMS.</li>
483 * </ul>
484 * <p>
485 * Since hardware varies, you are not guaranteed that any of the values
486 * you pass are honored exactly. Use the system defaults (TODO) if possible
487 * because they will be set to values that work on any given hardware.
488 * <p>
489 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500491 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500493 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
495
496 /**
497 * Bit to be bitwise-ored into the {@link #flags} field that should be
498 * set if this notification is in reference to something that is ongoing,
499 * like a phone call. It should not be set if this notification is in
500 * reference to something that happened at a particular point in time,
501 * like a missed phone call.
502 */
503 public static final int FLAG_ONGOING_EVENT = 0x00000002;
504
505 /**
506 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700507 * the audio will be repeated until the notification is
508 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 */
510 public static final int FLAG_INSISTENT = 0x00000004;
511
512 /**
513 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700514 * set if you would only like the sound, vibrate and ticker to be played
515 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
517 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
518
519 /**
520 * Bit to be bitwise-ored into the {@link #flags} field that should be
521 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500522 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 */
524 public static final int FLAG_AUTO_CANCEL = 0x00000010;
525
526 /**
527 * Bit to be bitwise-ored into the {@link #flags} field that should be
528 * set if the notification should not be canceled when the user clicks
529 * the Clear all button.
530 */
531 public static final int FLAG_NO_CLEAR = 0x00000020;
532
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700533 /**
534 * Bit to be bitwise-ored into the {@link #flags} field that should be
535 * set if this notification represents a currently running service. This
536 * will normally be set for you by {@link Service#startForeground}.
537 */
538 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
539
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400540 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500541 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500543 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400544 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700545 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400547
Griff Hazendfcb0802014-02-11 12:00:00 -0800548 /**
549 * Bit to be bitswise-ored into the {@link #flags} field that should be
550 * set if this notification is relevant to the current device only
551 * and it is not recommended that it bridge to other devices.
552 */
553 public static final int FLAG_LOCAL_ONLY = 0x00000100;
554
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700555 /**
556 * Bit to be bitswise-ored into the {@link #flags} field that should be
557 * set if this notification is the group summary for a group of notifications.
558 * Grouped notifications may display in a cluster or stack on devices which
559 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
560 */
561 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
562
Julia Reynoldse46bb372016-03-17 11:05:58 -0400563 /**
564 * Bit to be bitswise-ored into the {@link #flags} field that should be
565 * set if this notification is the group summary for an auto-group of notifications.
566 *
567 * @hide
568 */
569 @SystemApi
570 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
571
Julia Reynolds4db59552017-06-30 13:34:01 -0400572 /**
573 * @hide
574 */
575 public static final int FLAG_CAN_COLORIZE = 0x00000800;
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public int flags;
578
Tor Norbyed9273d62013-05-30 15:59:53 -0700579 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700580 @IntDef(prefix = { "PRIORITY_" }, value = {
581 PRIORITY_DEFAULT,
582 PRIORITY_LOW,
583 PRIORITY_MIN,
584 PRIORITY_HIGH,
585 PRIORITY_MAX
586 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700587 @Retention(RetentionPolicy.SOURCE)
588 public @interface Priority {}
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 * Default notification {@link #priority}. If your application does not prioritize its own
592 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500593 *
594 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500597 public static final int PRIORITY_DEFAULT = 0;
598
599 /**
600 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
601 * items smaller, or at a different position in the list, compared with your app's
602 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500603 *
604 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 public static final int PRIORITY_LOW = -1;
608
609 /**
610 * Lowest {@link #priority}; these items might not be shown to the user except under special
611 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500612 *
613 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500616 public static final int PRIORITY_MIN = -2;
617
618 /**
619 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
620 * show these items larger, or at a different position in notification lists, compared with
621 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 public static final int PRIORITY_HIGH = 1;
627
628 /**
629 * Highest {@link #priority}, for your application's most important items that require the
630 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_MAX = 2;
636
637 /**
638 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800639 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500640 * Priority is an indication of how much of the user's valuable attention should be consumed by
641 * this notification. Low-priority notifications may be hidden from the user in certain
642 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500643 * system will make a determination about how to interpret this priority when presenting
644 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400645 *
646 * <p>
647 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
648 * as a heads-up notification.
649 * </p>
650 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700653 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800656
Dan Sandler26e81cf2014-05-06 10:01:27 -0400657 /**
658 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
659 * to be applied by the standard Style templates when presenting this notification.
660 *
661 * The current template design constructs a colorful header image by overlaying the
662 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
663 * ignored.
664 */
Tor Norbye80756e32015-03-02 09:39:27 -0800665 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400666 public int color = COLOR_DEFAULT;
667
668 /**
669 * Special value of {@link #color} telling the system not to decorate this notification with
670 * any special color but instead use default colors when presenting this notification.
671 */
Tor Norbye80756e32015-03-02 09:39:27 -0800672 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400673 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600674
675 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800676 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800677 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800678 */
679 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800680 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800681
682 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700683 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
684 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600685 * lockscreen).
686 *
687 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
688 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
689 * shown in all situations, but the contents are only available if the device is unlocked for
690 * the appropriate user.
691 *
692 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
693 * can be read even in an "insecure" context (that is, above a secure lockscreen).
694 * To modify the public version of this notification—for example, to redact some portions—see
695 * {@link Builder#setPublicVersion(Notification)}.
696 *
697 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
698 * and ticker until the user has bypassed the lockscreen.
699 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600700 public @Visibility int visibility;
701
702 /** @hide */
703 @IntDef(prefix = { "VISIBILITY_" }, value = {
704 VISIBILITY_PUBLIC,
705 VISIBILITY_PRIVATE,
706 VISIBILITY_SECRET,
707 })
708 @Retention(RetentionPolicy.SOURCE)
709 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600710
Griff Hazenfc3922d2014-08-20 11:56:44 -0700711 /**
712 * Notification visibility: Show this notification in its entirety on all lockscreens.
713 *
714 * {@see #visibility}
715 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600716 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700717
718 /**
719 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
720 * private information on secure lockscreens.
721 *
722 * {@see #visibility}
723 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700725
726 /**
727 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
728 *
729 * {@see #visibility}
730 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600731 public static final int VISIBILITY_SECRET = -1;
732
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400734 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400736 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500737
738 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400739 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500740 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400741 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500742
743 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747
748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400756 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757
758 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400759 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 public static final String CATEGORY_ALARM = "alarm";
762
763 /**
764 * Notification category: progress of a long-running background operation.
765 */
766 public static final String CATEGORY_PROGRESS = "progress";
767
768 /**
769 * Notification category: social network or sharing update.
770 */
771 public static final String CATEGORY_SOCIAL = "social";
772
773 /**
774 * Notification category: error in background operation or authentication status.
775 */
776 public static final String CATEGORY_ERROR = "err";
777
778 /**
779 * Notification category: media transport control for playback.
780 */
781 public static final String CATEGORY_TRANSPORT = "transport";
782
783 /**
784 * Notification category: system or device status update. Reserved for system use.
785 */
786 public static final String CATEGORY_SYSTEM = "sys";
787
788 /**
789 * Notification category: indication of running background service.
790 */
791 public static final String CATEGORY_SERVICE = "service";
792
793 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400794 * Notification category: a specific, timely recommendation for a single thing.
795 * For example, a news app might want to recommend a news story it believes the user will
796 * want to read next.
797 */
798 public static final String CATEGORY_RECOMMENDATION = "recommendation";
799
800 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400801 * Notification category: ongoing information about device or contextual status.
802 */
803 public static final String CATEGORY_STATUS = "status";
804
805 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400806 * Notification category: user-scheduled reminder.
807 */
808 public static final String CATEGORY_REMINDER = "reminder";
809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
812 * that best describes this Notification. May be used by the system for ranking and filtering.
813 */
814 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500815
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700816 private String mGroupKey;
817
818 /**
819 * Get the key used to group this notification into a cluster or stack
820 * with other notifications on devices which support such rendering.
821 */
822 public String getGroup() {
823 return mGroupKey;
824 }
825
826 private String mSortKey;
827
828 /**
829 * Get a sort key that orders this notification among other notifications from the
830 * same package. This can be useful if an external sort was already applied and an app
831 * would like to preserve this. Notifications will be sorted lexicographically using this
832 * value, although providing different priorities in addition to providing sort key may
833 * cause this value to be ignored.
834 *
835 * <p>This sort key can also be used to order members of a notification group. See
836 * {@link Builder#setGroup}.
837 *
838 * @see String#compareTo(String)
839 */
840 public String getSortKey() {
841 return mSortKey;
842 }
843
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500844 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846 * <p>
847 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
848 * APIs, and are intended to be used by
849 * {@link android.service.notification.NotificationListenerService} implementations to extract
850 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public Bundle extras = new Bundle();
853
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700855 * All pending intents in the notification as the system needs to be able to access them but
856 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700857 * custom parcelable objects.
858 *
859 * @hide
860 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700861 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700862
863 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700864 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
865 * pending intents inside of it, so only those will get the behavior.
866 *
867 * @hide
868 */
Adrian Roosfb921842017-10-26 14:49:56 +0200869 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700870
871 /**
872 * Must be set by a process to start associating tokens with Notification objects
873 * coming in to it. This is set by NotificationManagerService.
874 *
875 * @hide
876 */
877 static public IBinder processWhitelistToken;
878
879 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880 * {@link #extras} key: this is the title of the notification,
881 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
882 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500883 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: this is the title of the notification when shown in expanded form,
887 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
888 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400889 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890
891 /**
892 * {@link #extras} key: this is the main text payload, as supplied to
893 * {@link Builder#setContentText(CharSequence)}.
894 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500895 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896
897 /**
898 * {@link #extras} key: this is a third line of text, as supplied to
899 * {@link Builder#setSubText(CharSequence)}.
900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902
903 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800904 * {@link #extras} key: this is the remote input history, as supplied to
905 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700906 *
907 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
908 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
909 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
910 * notifications once the other party has responded).
911 *
912 * The extra with this key is of type CharSequence[] and contains the most recent entry at
913 * the 0 index, the second most recent at the 1 index, etc.
914 *
915 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800916 */
917 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
918
919 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 * {@link #extras} key: this is a small piece of additional text as supplied to
921 * {@link Builder#setContentInfo(CharSequence)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
926 * {@link #extras} key: this is a line of summary information intended to be shown
927 * alongside expanded notifications, as supplied to (e.g.)
928 * {@link BigTextStyle#setSummaryText(CharSequence)}.
929 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400930 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200933 * {@link #extras} key: this is the longer text shown in the big form of a
934 * {@link BigTextStyle} notification, as supplied to
935 * {@link BigTextStyle#bigText(CharSequence)}.
936 */
937 public static final String EXTRA_BIG_TEXT = "android.bigText";
938
939 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
941 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400942 *
943 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400945 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500946 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947
948 /**
949 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
950 * notification payload, as
951 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400952 *
953 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400955 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be used instead of the one from
960 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
961 * shown in its expanded form, as supplied to
962 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
963 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400964 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965
966 /**
967 * {@link #extras} key: this is the progress value supplied to
968 * {@link Builder#setProgress(int, int, boolean)}.
969 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400970 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971
972 /**
973 * {@link #extras} key: this is the maximum value supplied to
974 * {@link Builder#setProgress(int, int, boolean)}.
975 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400976 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977
978 /**
979 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
980 * {@link Builder#setProgress(int, int, boolean)}.
981 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400982 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983
984 /**
985 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
986 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
987 * {@link Builder#setUsesChronometer(boolean)}.
988 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400989 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400990
991 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800992 * {@link #extras} key: whether the chronometer set on the notification should count down
993 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700994 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800995 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700996 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800997
998 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 * {@link #extras} key: whether {@link #when} should be shown,
1000 * as supplied to {@link Builder#setShowWhen(boolean)}.
1001 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001002 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003
1004 /**
1005 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1006 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1007 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001008 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009
1010 /**
1011 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1012 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1013 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001014 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001015
1016 /**
1017 * {@link #extras} key: A string representing the name of the specific
1018 * {@link android.app.Notification.Style} used to create this notification.
1019 */
Chris Wren91ad5632013-06-05 15:05:57 -04001020 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001023 * {@link #extras} key: A String array containing the people that this notification relates to,
1024 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001025 *
1026 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001028 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001029
1030 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001031 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1032 * this notification relates to.
1033 */
1034 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1035
1036 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001037 * Allow certain system-generated notifications to appear before the device is provisioned.
1038 * Only available to notifications coming from the android package.
1039 * @hide
1040 */
Maurice Lam96c10032017-03-29 15:42:38 -07001041 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001042 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001043 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1044
1045 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001046 * {@link #extras} key: A
1047 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1048 * in the background when the notification is selected. The URI must point to an image stream
1049 * suitable for passing into
1050 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1051 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1052 * URI used for this purpose must require no permissions to read the image data.
1053 */
1054 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1055
1056 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001057 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001058 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001059 * {@link android.app.Notification.MediaStyle} notification.
1060 */
1061 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1062
1063 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001064 * {@link #extras} key: the indices of actions to be shown in the compact view,
1065 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1066 */
1067 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1068
Christoph Studer943aa672014-08-03 20:31:16 +02001069 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001070 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1071 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001072 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1073 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001074 *
1075 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001076 */
1077 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1078
1079 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001080 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1081 * direct replies
1082 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1083 * {@link Person}
1084 */
1085 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1086
1087 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001088 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001089 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001090 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001091 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001092
1093 /**
1094 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1095 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001096 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1097 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001098 */
1099 public static final String EXTRA_MESSAGES = "android.messages";
1100
1101 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001102 * {@link #extras} key: an array of
1103 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1104 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1105 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1106 * array of bundles.
1107 */
1108 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1109
1110 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001111 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1112 * represents a group conversation.
1113 */
1114 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1115
1116 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001117 * {@link #extras} key: whether the notification should be colorized as
1118 * supplied to {@link Builder#setColorized(boolean)}}.
1119 */
1120 public static final String EXTRA_COLORIZED = "android.colorized";
1121
1122 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001123 * @hide
1124 */
1125 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1126
Selim Cinek247fa012016-02-18 09:50:48 -08001127 /**
1128 * @hide
1129 */
1130 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1131
Shane Brennan472a3b32016-12-12 15:28:10 -08001132 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001133 * @hide
1134 */
1135 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1136
1137 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001138 * {@link #extras} key: the audio contents of this notification.
1139 *
1140 * This is for use when rendering the notification on an audio-focused interface;
1141 * the audio contents are a complete sound sample that contains the contents/body of the
1142 * notification. This may be used in substitute of a Text-to-Speech reading of the
1143 * notification. For example if the notification represents a voice message this should point
1144 * to the audio of that message.
1145 *
1146 * The data stored under this key should be a String representation of a Uri that contains the
1147 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1148 *
1149 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1150 * has a field for holding data URI. That field can be used for audio.
1151 * See {@code Message#setData}.
1152 *
1153 * Example usage:
1154 * <pre>
1155 * {@code
1156 * Notification.Builder myBuilder = (build your Notification as normal);
1157 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1158 * }
1159 * </pre>
1160 */
1161 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1162
Dan Sandler80eaa592016-04-14 23:34:54 -04001163 /** @hide */
1164 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001165 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001166 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1167
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001168 /**
1169 * This is set on the notification shown by the activity manager about all apps
1170 * running in the background. It indicates that the notification should be shown
1171 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1172 * notification currently visible to the user. This is a string array of all
1173 * package names of the apps.
1174 * @hide
1175 */
1176 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1177
Dan Sandlerd63f9322015-05-06 15:18:49 -04001178 private Icon mSmallIcon;
1179 private Icon mLargeIcon;
1180
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001181 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001182 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001183
Julia Reynolds13d898c2017-02-02 12:22:05 -05001184 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001185 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001186
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001187 /** @hide */
1188 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1189 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1190 })
1191 @Retention(RetentionPolicy.SOURCE)
1192 public @interface GroupAlertBehavior {}
1193
1194 /**
1195 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1196 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1197 * notification will not be muted when it is in a group.
1198 */
1199 public static final int GROUP_ALERT_ALL = 0;
1200
1201 /**
1202 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1203 * notification in a group should be silenced (no sound or vibration) even if they are posted
1204 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001205 * mute this notification if this notification is a group child. This must be applied to all
1206 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001207 *
1208 * <p> For example, you might want to use this constant if you post a number of children
1209 * notifications at once (say, after a periodic sync), and only need to notify the user
1210 * audibly once.
1211 */
1212 public static final int GROUP_ALERT_SUMMARY = 1;
1213
1214 /**
1215 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1216 * notification in a group should be silenced (no sound or vibration) even if they are
1217 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1218 * to mute this notification if this notification is a group summary.
1219 *
1220 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001221 * in your group have content and the summary is only used to visually group notifications
1222 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001223 */
1224 public static final int GROUP_ALERT_CHILDREN = 2;
1225
Julia Reynolds2f431e22017-06-07 14:12:09 +00001226 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001227
Julia Reynolds13d898c2017-02-02 12:22:05 -05001228 /**
1229 * If this notification is being shown as a badge, always show as a number.
1230 */
1231 public static final int BADGE_ICON_NONE = 0;
1232
1233 /**
1234 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1235 * represent this notification.
1236 */
1237 public static final int BADGE_ICON_SMALL = 1;
1238
1239 /**
1240 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1241 * represent this notification.
1242 */
1243 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001244 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001245
Chris Wren51c75102013-07-16 20:49:17 -04001246 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001247 * Structure to encapsulate a named action that can be shown as part of this notification.
1248 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1249 * selected by the user.
1250 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001251 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1252 * or {@link Notification.Builder#addAction(Notification.Action)}
1253 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001254 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001255 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001256 /**
1257 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1258 * {@link RemoteInput}s.
1259 *
1260 * This is intended for {@link RemoteInput}s that only accept data, meaning
1261 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1262 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1263 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1264 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1265 *
1266 * You can test if a RemoteInput matches these constraints using
1267 * {@link RemoteInput#isDataOnly}.
1268 */
1269 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1270
Griff Hazen959591e2014-05-15 22:26:18 -07001271 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001272 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001273 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001274 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001275
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001276 /**
1277 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001278 *
1279 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001280 */
Dan Sandler86647982015-05-13 23:41:13 -04001281 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001282 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001283
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001284 /**
1285 * Title of the action.
1286 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001287 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001288
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001289 /**
1290 * Intent to send when the user invokes this action. May be null, in which case the action
1291 * may be rendered in a disabled presentation by the system UI.
1292 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001293 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001294
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001295 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001296 if (in.readInt() != 0) {
1297 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001298 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1299 icon = mIcon.getResId();
1300 }
Dan Sandler86647982015-05-13 23:41:13 -04001301 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001302 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1303 if (in.readInt() == 1) {
1304 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1305 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001306 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001307 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001308 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001309 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001310
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001311 /**
Dan Sandler86647982015-05-13 23:41:13 -04001312 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001313 */
Dan Sandler86647982015-05-13 23:41:13 -04001314 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001315 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001316 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001317 }
1318
Adrian Roos7af53622016-10-12 13:44:05 -07001319 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001320 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001321 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001322 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001323 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1324 this.icon = icon.getResId();
1325 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001326 this.title = title;
1327 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001328 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001329 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001330 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001331 }
1332
1333 /**
Dan Sandler86647982015-05-13 23:41:13 -04001334 * Return an icon representing the action.
1335 */
1336 public Icon getIcon() {
1337 if (mIcon == null && icon != 0) {
1338 // you snuck an icon in here without using the builder; let's try to keep it
1339 mIcon = Icon.createWithResource("", icon);
1340 }
1341 return mIcon;
1342 }
1343
1344 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001345 * Get additional metadata carried around with this Action.
1346 */
1347 public Bundle getExtras() {
1348 return mExtras;
1349 }
1350
1351 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001352 * Return whether the platform should automatically generate possible replies for this
1353 * {@link Action}
1354 */
1355 public boolean getAllowGeneratedReplies() {
1356 return mAllowGeneratedReplies;
1357 }
1358
1359 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001360 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001361 * May return null if no remote inputs were added. Only returns inputs which accept
1362 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001363 */
1364 public RemoteInput[] getRemoteInputs() {
1365 return mRemoteInputs;
1366 }
1367
1368 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001369 * Get the list of inputs to be collected from the user that ONLY accept data when this
1370 * action is sent. These remote inputs are guaranteed to return true on a call to
1371 * {@link RemoteInput#isDataOnly}.
1372 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001373 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001374 *
1375 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1376 * of non-textual RemoteInputs do not access these remote inputs.
1377 */
1378 public RemoteInput[] getDataOnlyRemoteInputs() {
1379 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1380 }
1381
1382 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001383 * Builder class for {@link Action} objects.
1384 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001385 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001386 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001387 private final CharSequence mTitle;
1388 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001389 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001390 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001391 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001392
1393 /**
1394 * Construct a new builder for {@link Action} object.
1395 * @param icon icon to show for this action
1396 * @param title the title of the action
1397 * @param intent the {@link PendingIntent} to fire when users trigger this action
1398 */
Dan Sandler86647982015-05-13 23:41:13 -04001399 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001400 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001401 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001402 }
1403
1404 /**
1405 * Construct a new builder for {@link Action} object.
1406 * @param icon icon to show for this action
1407 * @param title the title of the action
1408 * @param intent the {@link PendingIntent} to fire when users trigger this action
1409 */
1410 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001411 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001412 }
1413
1414 /**
1415 * Construct a new builder for {@link Action} object using the fields from an
1416 * {@link Action}.
1417 * @param action the action to read fields from.
1418 */
1419 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001420 this(action.getIcon(), action.title, action.actionIntent,
1421 new Bundle(action.mExtras), action.getRemoteInputs(),
1422 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001423 }
1424
Dan Sandler86647982015-05-13 23:41:13 -04001425 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001426 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001427 mIcon = icon;
1428 mTitle = title;
1429 mIntent = intent;
1430 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431 if (remoteInputs != null) {
1432 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1433 Collections.addAll(mRemoteInputs, remoteInputs);
1434 }
Adrian Roos7af53622016-10-12 13:44:05 -07001435 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001436 }
1437
1438 /**
1439 * Merge additional metadata into this builder.
1440 *
1441 * <p>Values within the Bundle will replace existing extras values in this Builder.
1442 *
1443 * @see Notification.Action#extras
1444 */
1445 public Builder addExtras(Bundle extras) {
1446 if (extras != null) {
1447 mExtras.putAll(extras);
1448 }
1449 return this;
1450 }
1451
1452 /**
1453 * Get the metadata Bundle used by this Builder.
1454 *
1455 * <p>The returned Bundle is shared with this Builder.
1456 */
1457 public Bundle getExtras() {
1458 return mExtras;
1459 }
1460
1461 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001462 * Add an input to be collected from the user when this action is sent.
1463 * Response values can be retrieved from the fired intent by using the
1464 * {@link RemoteInput#getResultsFromIntent} function.
1465 * @param remoteInput a {@link RemoteInput} to add to the action
1466 * @return this object for method chaining
1467 */
1468 public Builder addRemoteInput(RemoteInput remoteInput) {
1469 if (mRemoteInputs == null) {
1470 mRemoteInputs = new ArrayList<RemoteInput>();
1471 }
1472 mRemoteInputs.add(remoteInput);
1473 return this;
1474 }
1475
1476 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001477 * Set whether the platform should automatically generate possible replies to add to
1478 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1479 * {@link RemoteInput}, this has no effect.
1480 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1481 * otherwise
1482 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001483 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001484 */
1485 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1486 mAllowGeneratedReplies = allowGeneratedReplies;
1487 return this;
1488 }
1489
1490 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001491 * Apply an extender to this action builder. Extenders may be used to add
1492 * metadata or change options on this builder.
1493 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001494 public Builder extend(Extender extender) {
1495 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001496 return this;
1497 }
1498
1499 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001500 * Combine all of the options that have been set and return a new {@link Action}
1501 * object.
1502 * @return the built action
1503 */
1504 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001505 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1506 RemoteInput[] previousDataInputs =
1507 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001508 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001509 for (RemoteInput input : previousDataInputs) {
1510 dataOnlyInputs.add(input);
1511 }
1512 }
1513 List<RemoteInput> textInputs = new ArrayList<>();
1514 if (mRemoteInputs != null) {
1515 for (RemoteInput input : mRemoteInputs) {
1516 if (input.isDataOnly()) {
1517 dataOnlyInputs.add(input);
1518 } else {
1519 textInputs.add(input);
1520 }
1521 }
1522 }
1523 if (!dataOnlyInputs.isEmpty()) {
1524 RemoteInput[] dataInputsArr =
1525 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1526 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1527 }
1528 RemoteInput[] textInputsArr = textInputs.isEmpty()
1529 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1530 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001531 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001532 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001533 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001534
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001535 @Override
1536 public Action clone() {
1537 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001538 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001539 title,
1540 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001541 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001542 getRemoteInputs(),
1543 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001544 }
1545 @Override
1546 public int describeContents() {
1547 return 0;
1548 }
1549 @Override
1550 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001551 final Icon ic = getIcon();
1552 if (ic != null) {
1553 out.writeInt(1);
1554 ic.writeToParcel(out, 0);
1555 } else {
1556 out.writeInt(0);
1557 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001558 TextUtils.writeToParcel(title, out, flags);
1559 if (actionIntent != null) {
1560 out.writeInt(1);
1561 actionIntent.writeToParcel(out, flags);
1562 } else {
1563 out.writeInt(0);
1564 }
Griff Hazen959591e2014-05-15 22:26:18 -07001565 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001566 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001567 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001568 }
Griff Hazen959591e2014-05-15 22:26:18 -07001569 public static final Parcelable.Creator<Action> CREATOR =
1570 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001571 public Action createFromParcel(Parcel in) {
1572 return new Action(in);
1573 }
1574 public Action[] newArray(int size) {
1575 return new Action[size];
1576 }
1577 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001578
1579 /**
1580 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1581 * metadata or change options on an action builder.
1582 */
1583 public interface Extender {
1584 /**
1585 * Apply this extender to a notification action builder.
1586 * @param builder the builder to be modified.
1587 * @return the build object for chaining.
1588 */
1589 public Builder extend(Builder builder);
1590 }
1591
1592 /**
1593 * Wearable extender for notification actions. To add extensions to an action,
1594 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1595 * the {@code WearableExtender()} constructor and apply it to a
1596 * {@link android.app.Notification.Action.Builder} using
1597 * {@link android.app.Notification.Action.Builder#extend}.
1598 *
1599 * <pre class="prettyprint">
1600 * Notification.Action action = new Notification.Action.Builder(
1601 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001602 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001603 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001604 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001605 */
1606 public static final class WearableExtender implements Extender {
1607 /** Notification action extra which contains wearable extensions */
1608 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1609
Pete Gastaf6781d2014-10-07 15:17:05 -04001610 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001611 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001612 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1613 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1614 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001615
1616 // Flags bitwise-ored to mFlags
1617 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001618 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001619 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001620
1621 // Default value for flags integer
1622 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1623
1624 private int mFlags = DEFAULT_FLAGS;
1625
Pete Gastaf6781d2014-10-07 15:17:05 -04001626 private CharSequence mInProgressLabel;
1627 private CharSequence mConfirmLabel;
1628 private CharSequence mCancelLabel;
1629
Griff Hazen61a9e862014-05-22 16:05:19 -07001630 /**
1631 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1632 * options.
1633 */
1634 public WearableExtender() {
1635 }
1636
1637 /**
1638 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1639 * wearable options present in an existing notification action.
1640 * @param action the notification action to inspect.
1641 */
1642 public WearableExtender(Action action) {
1643 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1644 if (wearableBundle != null) {
1645 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001646 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1647 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1648 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001649 }
1650 }
1651
1652 /**
1653 * Apply wearable extensions to a notification action that is being built. This is
1654 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1655 * method of {@link android.app.Notification.Action.Builder}.
1656 */
1657 @Override
1658 public Action.Builder extend(Action.Builder builder) {
1659 Bundle wearableBundle = new Bundle();
1660
1661 if (mFlags != DEFAULT_FLAGS) {
1662 wearableBundle.putInt(KEY_FLAGS, mFlags);
1663 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001664 if (mInProgressLabel != null) {
1665 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1666 }
1667 if (mConfirmLabel != null) {
1668 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1669 }
1670 if (mCancelLabel != null) {
1671 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1672 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001673
1674 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1675 return builder;
1676 }
1677
1678 @Override
1679 public WearableExtender clone() {
1680 WearableExtender that = new WearableExtender();
1681 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001682 that.mInProgressLabel = this.mInProgressLabel;
1683 that.mConfirmLabel = this.mConfirmLabel;
1684 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001685 return that;
1686 }
1687
1688 /**
1689 * Set whether this action is available when the wearable device is not connected to
1690 * a companion device. The user can still trigger this action when the wearable device is
1691 * offline, but a visual hint will indicate that the action may not be available.
1692 * Defaults to true.
1693 */
1694 public WearableExtender setAvailableOffline(boolean availableOffline) {
1695 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1696 return this;
1697 }
1698
1699 /**
1700 * Get whether this action is available when the wearable device is not connected to
1701 * a companion device. The user can still trigger this action when the wearable device is
1702 * offline, but a visual hint will indicate that the action may not be available.
1703 * Defaults to true.
1704 */
1705 public boolean isAvailableOffline() {
1706 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1707 }
1708
1709 private void setFlag(int mask, boolean value) {
1710 if (value) {
1711 mFlags |= mask;
1712 } else {
1713 mFlags &= ~mask;
1714 }
1715 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001716
1717 /**
1718 * Set a label to display while the wearable is preparing to automatically execute the
1719 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1720 *
1721 * @param label the label to display while the action is being prepared to execute
1722 * @return this object for method chaining
1723 */
1724 public WearableExtender setInProgressLabel(CharSequence label) {
1725 mInProgressLabel = label;
1726 return this;
1727 }
1728
1729 /**
1730 * Get the label to display while the wearable is preparing to automatically execute
1731 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1732 *
1733 * @return the label to display while the action is being prepared to execute
1734 */
1735 public CharSequence getInProgressLabel() {
1736 return mInProgressLabel;
1737 }
1738
1739 /**
1740 * Set a label to display to confirm that the action should be executed.
1741 * This is usually an imperative verb like "Send".
1742 *
1743 * @param label the label to confirm the action should be executed
1744 * @return this object for method chaining
1745 */
1746 public WearableExtender setConfirmLabel(CharSequence label) {
1747 mConfirmLabel = label;
1748 return this;
1749 }
1750
1751 /**
1752 * Get the label to display to confirm that the action should be executed.
1753 * This is usually an imperative verb like "Send".
1754 *
1755 * @return the label to confirm the action should be executed
1756 */
1757 public CharSequence getConfirmLabel() {
1758 return mConfirmLabel;
1759 }
1760
1761 /**
1762 * Set a label to display to cancel the action.
1763 * This is usually an imperative verb, like "Cancel".
1764 *
1765 * @param label the label to display to cancel the action
1766 * @return this object for method chaining
1767 */
1768 public WearableExtender setCancelLabel(CharSequence label) {
1769 mCancelLabel = label;
1770 return this;
1771 }
1772
1773 /**
1774 * Get the label to display to cancel the action.
1775 * This is usually an imperative verb like "Cancel".
1776 *
1777 * @return the label to display to cancel the action
1778 */
1779 public CharSequence getCancelLabel() {
1780 return mCancelLabel;
1781 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001782
1783 /**
1784 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1785 * platform that it can generate the appropriate transitions.
1786 * @param hintLaunchesActivity {@code true} if the content intent will launch
1787 * an activity and transitions should be generated, false otherwise.
1788 * @return this object for method chaining
1789 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001790 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001791 boolean hintLaunchesActivity) {
1792 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1793 return this;
1794 }
1795
1796 /**
1797 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1798 * platform that it can generate the appropriate transitions
1799 * @return {@code true} if the content intent will launch an activity and transitions
1800 * should be generated, false otherwise. The default value is {@code false} if this was
1801 * never set.
1802 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001803 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001804 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1805 }
Alex Hills9f087612016-06-07 09:08:59 -04001806
1807 /**
1808 * Set a hint that this Action should be displayed inline.
1809 *
1810 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1811 * otherwise
1812 * @return this object for method chaining
1813 */
1814 public WearableExtender setHintDisplayActionInline(
1815 boolean hintDisplayInline) {
1816 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1817 return this;
1818 }
1819
1820 /**
1821 * Get a hint that this Action should be displayed inline.
1822 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001823 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001824 * otherwise. The default value is {@code false} if this was never set.
1825 */
1826 public boolean getHintDisplayActionInline() {
1827 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1828 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001829 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001830 }
1831
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001832 /**
1833 * Array of all {@link Action} structures attached to this notification by
1834 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1835 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1836 * interface for invoking actions.
1837 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001838 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001839
1840 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001841 * Replacement version of this notification whose content will be shown
1842 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1843 * and {@link #VISIBILITY_PUBLIC}.
1844 */
1845 public Notification publicVersion;
1846
1847 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001848 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001849 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 */
1851 public Notification()
1852 {
1853 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001854 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001855 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
1857
1858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 * @hide
1860 */
1861 public Notification(Context context, int icon, CharSequence tickerText, long when,
1862 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1863 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001864 new Builder(context)
1865 .setWhen(when)
1866 .setSmallIcon(icon)
1867 .setTicker(tickerText)
1868 .setContentTitle(contentTitle)
1869 .setContentText(contentText)
1870 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1871 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
1874 /**
1875 * Constructs a Notification object with the information needed to
1876 * have a status bar icon without the standard expanded view.
1877 *
1878 * @param icon The resource id of the icon to put in the status bar.
1879 * @param tickerText The text that flows by in the status bar when the notification first
1880 * activates.
1881 * @param when The time to show in the time field. In the System.currentTimeMillis
1882 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 *
1884 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001886 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 public Notification(int icon, CharSequence tickerText, long when)
1888 {
1889 this.icon = icon;
1890 this.tickerText = tickerText;
1891 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001892 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
1894
1895 /**
1896 * Unflatten the notification from a parcel.
1897 */
Svet Ganovddb94882016-06-23 19:55:24 -07001898 @SuppressWarnings("unchecked")
1899 public Notification(Parcel parcel) {
1900 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1901 // intents in extras are always written as the last entry.
1902 readFromParcelImpl(parcel);
1903 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001904 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001905 }
1906
1907 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 {
1909 int version = parcel.readInt();
1910
Adrian Roosfb921842017-10-26 14:49:56 +02001911 mWhitelistToken = parcel.readStrongBinder();
1912 if (mWhitelistToken == null) {
1913 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07001914 }
1915 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02001916 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07001917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001919 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001920 if (parcel.readInt() != 0) {
1921 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001922 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1923 icon = mSmallIcon.getResId();
1924 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 number = parcel.readInt();
1927 if (parcel.readInt() != 0) {
1928 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1929 }
1930 if (parcel.readInt() != 0) {
1931 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1932 }
1933 if (parcel.readInt() != 0) {
1934 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1935 }
1936 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001937 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001938 }
1939 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1941 }
Joe Onorato561d3852010-11-20 18:09:34 -08001942 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001943 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 defaults = parcel.readInt();
1946 flags = parcel.readInt();
1947 if (parcel.readInt() != 0) {
1948 sound = Uri.CREATOR.createFromParcel(parcel);
1949 }
1950
1951 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001952 if (parcel.readInt() != 0) {
1953 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 vibrate = parcel.createLongArray();
1956 ledARGB = parcel.readInt();
1957 ledOnMS = parcel.readInt();
1958 ledOffMS = parcel.readInt();
1959 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001960
1961 if (parcel.readInt() != 0) {
1962 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1963 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001964
1965 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001966
John Spurlockfd7f1e02014-03-18 16:41:57 -04001967 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001968
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001969 mGroupKey = parcel.readString();
1970
1971 mSortKey = parcel.readString();
1972
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001973 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01001974 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001975
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001976 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1977
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001978 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001979 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1980 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001981
Chris Wren8fd39ec2014-02-27 17:43:26 -05001982 if (parcel.readInt() != 0) {
1983 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1984 }
1985
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001986 visibility = parcel.readInt();
1987
1988 if (parcel.readInt() != 0) {
1989 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1990 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001991
1992 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001993
1994 if (parcel.readInt() != 0) {
1995 mChannelId = parcel.readString();
1996 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001997 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001998
1999 if (parcel.readInt() != 0) {
2000 mShortcutId = parcel.readString();
2001 }
2002
2003 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002004
2005 if (parcel.readInt() != 0) {
2006 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2007 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002008
2009 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
2011
Andy Stadler110988c2010-12-03 14:29:16 -08002012 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002013 public Notification clone() {
2014 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002015 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002016 return that;
2017 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002018
Daniel Sandler1a497d32013-04-18 14:52:45 -04002019 /**
2020 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2021 * of this into that.
2022 * @hide
2023 */
2024 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002025 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002026 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002027 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002028 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002029 that.number = this.number;
2030
2031 // PendingIntents are global, so there's no reason (or way) to clone them.
2032 that.contentIntent = this.contentIntent;
2033 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002034 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002035
2036 if (this.tickerText != null) {
2037 that.tickerText = this.tickerText.toString();
2038 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002039 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002040 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002041 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002042 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002043 that.contentView = this.contentView.clone();
2044 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002045 if (heavy && this.mLargeIcon != null) {
2046 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002047 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002048 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002049 that.sound = this.sound; // android.net.Uri is immutable
2050 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002051 if (this.audioAttributes != null) {
2052 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2053 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002054
2055 final long[] vibrate = this.vibrate;
2056 if (vibrate != null) {
2057 final int N = vibrate.length;
2058 final long[] vib = that.vibrate = new long[N];
2059 System.arraycopy(vibrate, 0, vib, 0, N);
2060 }
2061
2062 that.ledARGB = this.ledARGB;
2063 that.ledOnMS = this.ledOnMS;
2064 that.ledOffMS = this.ledOffMS;
2065 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002066
Joe Onorato18e69df2010-05-17 22:26:12 -07002067 that.flags = this.flags;
2068
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002069 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002070
John Spurlockfd7f1e02014-03-18 16:41:57 -04002071 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002072
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002073 that.mGroupKey = this.mGroupKey;
2074
2075 that.mSortKey = this.mSortKey;
2076
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002077 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002078 try {
2079 that.extras = new Bundle(this.extras);
2080 // will unparcel
2081 that.extras.size();
2082 } catch (BadParcelableException e) {
2083 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2084 that.extras = null;
2085 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002086 }
2087
Felipe Lemedd85da62016-06-28 11:29:54 -07002088 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2089 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002090 }
2091
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002092 if (this.actions != null) {
2093 that.actions = new Action[this.actions.length];
2094 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002095 if ( this.actions[i] != null) {
2096 that.actions[i] = this.actions[i].clone();
2097 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002098 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002099 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002100
Daniel Sandler1a497d32013-04-18 14:52:45 -04002101 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002102 that.bigContentView = this.bigContentView.clone();
2103 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002104
Chris Wren8fd39ec2014-02-27 17:43:26 -05002105 if (heavy && this.headsUpContentView != null) {
2106 that.headsUpContentView = this.headsUpContentView.clone();
2107 }
2108
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002109 that.visibility = this.visibility;
2110
2111 if (this.publicVersion != null) {
2112 that.publicVersion = new Notification();
2113 this.publicVersion.cloneInto(that.publicVersion, heavy);
2114 }
2115
Dan Sandler26e81cf2014-05-06 10:01:27 -04002116 that.color = this.color;
2117
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002118 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002119 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002120 that.mShortcutId = this.mShortcutId;
2121 that.mBadgeIcon = this.mBadgeIcon;
2122 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002123 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002124
Daniel Sandler1a497d32013-04-18 14:52:45 -04002125 if (!heavy) {
2126 that.lightenPayload(); // will clean out extras
2127 }
2128 }
2129
2130 /**
2131 * Removes heavyweight parts of the Notification object for archival or for sending to
2132 * listeners when the full contents are not necessary.
2133 * @hide
2134 */
2135 public final void lightenPayload() {
2136 tickerView = null;
2137 contentView = null;
2138 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002139 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002140 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002141 if (extras != null && !extras.isEmpty()) {
2142 final Set<String> keyset = extras.keySet();
2143 final int N = keyset.size();
2144 final String[] keys = keyset.toArray(new String[N]);
2145 for (int i=0; i<N; i++) {
2146 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002147 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2148 continue;
2149 }
Dan Sandler50128532015-12-08 15:42:41 -05002150 final Object obj = extras.get(key);
2151 if (obj != null &&
2152 ( obj instanceof Parcelable
2153 || obj instanceof Parcelable[]
2154 || obj instanceof SparseArray
2155 || obj instanceof ArrayList)) {
2156 extras.remove(key);
2157 }
2158 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002159 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002160 }
2161
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002162 /**
2163 * Make sure this CharSequence is safe to put into a bundle, which basically
2164 * means it had better not be some custom Parcelable implementation.
2165 * @hide
2166 */
2167 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002168 if (cs == null) return cs;
2169 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2170 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2171 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002172 if (cs instanceof Parcelable) {
2173 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2174 + " instance is a custom Parcelable and not allowed in Notification");
2175 return cs.toString();
2176 }
Selim Cinek60a54252016-02-26 17:03:25 -08002177 return removeTextSizeSpans(cs);
2178 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002179
Selim Cinek60a54252016-02-26 17:03:25 -08002180 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2181 if (charSequence instanceof Spanned) {
2182 Spanned ss = (Spanned) charSequence;
2183 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2184 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2185 for (Object span : spans) {
2186 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002187 if (resultSpan instanceof CharacterStyle) {
2188 resultSpan = ((CharacterStyle) span).getUnderlying();
2189 }
2190 if (resultSpan instanceof TextAppearanceSpan) {
2191 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002192 resultSpan = new TextAppearanceSpan(
2193 originalSpan.getFamily(),
2194 originalSpan.getTextStyle(),
2195 -1,
2196 originalSpan.getTextColor(),
2197 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002198 } else if (resultSpan instanceof RelativeSizeSpan
2199 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002200 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002201 } else {
2202 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002203 }
2204 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2205 ss.getSpanFlags(span));
2206 }
2207 return builder;
2208 }
2209 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002210 }
2211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 public int describeContents() {
2213 return 0;
2214 }
2215
2216 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002217 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 */
Svet Ganovddb94882016-06-23 19:55:24 -07002219 public void writeToParcel(Parcel parcel, int flags) {
2220 // We need to mark all pending intents getting into the notification
2221 // system as being put there to later allow the notification ranker
2222 // to launch them and by doing so add the app to the battery saver white
2223 // list for a short period of time. The problem is that the system
2224 // cannot look into the extras as there may be parcelables there that
2225 // the platform does not know how to handle. To go around that we have
2226 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002227 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002228 if (collectPendingIntents) {
2229 PendingIntent.setOnMarshaledListener(
2230 (PendingIntent intent, Parcel out, int outFlags) -> {
2231 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002232 if (allPendingIntents == null) {
2233 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002234 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002235 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002236 }
2237 });
2238 }
2239 try {
2240 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002241 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002242 writeToParcelImpl(parcel, flags);
2243 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002244 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002245 } finally {
2246 if (collectPendingIntents) {
2247 PendingIntent.setOnMarshaledListener(null);
2248 }
2249 }
2250 }
2251
2252 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 parcel.writeInt(1);
2254
Adrian Roosfb921842017-10-26 14:49:56 +02002255 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002257 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002258 if (mSmallIcon == null && icon != 0) {
2259 // you snuck an icon in here without using the builder; let's try to keep it
2260 mSmallIcon = Icon.createWithResource("", icon);
2261 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002262 if (mSmallIcon != null) {
2263 parcel.writeInt(1);
2264 mSmallIcon.writeToParcel(parcel, 0);
2265 } else {
2266 parcel.writeInt(0);
2267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 parcel.writeInt(number);
2269 if (contentIntent != null) {
2270 parcel.writeInt(1);
2271 contentIntent.writeToParcel(parcel, 0);
2272 } else {
2273 parcel.writeInt(0);
2274 }
2275 if (deleteIntent != null) {
2276 parcel.writeInt(1);
2277 deleteIntent.writeToParcel(parcel, 0);
2278 } else {
2279 parcel.writeInt(0);
2280 }
2281 if (tickerText != null) {
2282 parcel.writeInt(1);
2283 TextUtils.writeToParcel(tickerText, parcel, flags);
2284 } else {
2285 parcel.writeInt(0);
2286 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002287 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002288 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002289 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002290 } else {
2291 parcel.writeInt(0);
2292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 if (contentView != null) {
2294 parcel.writeInt(1);
2295 contentView.writeToParcel(parcel, 0);
2296 } else {
2297 parcel.writeInt(0);
2298 }
Selim Cinek279fa862016-06-14 10:57:25 -07002299 if (mLargeIcon == null && largeIcon != null) {
2300 // you snuck an icon in here without using the builder; let's try to keep it
2301 mLargeIcon = Icon.createWithBitmap(largeIcon);
2302 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002303 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002304 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002305 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002306 } else {
2307 parcel.writeInt(0);
2308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309
2310 parcel.writeInt(defaults);
2311 parcel.writeInt(this.flags);
2312
2313 if (sound != null) {
2314 parcel.writeInt(1);
2315 sound.writeToParcel(parcel, 0);
2316 } else {
2317 parcel.writeInt(0);
2318 }
2319 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002320
2321 if (audioAttributes != null) {
2322 parcel.writeInt(1);
2323 audioAttributes.writeToParcel(parcel, 0);
2324 } else {
2325 parcel.writeInt(0);
2326 }
2327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 parcel.writeLongArray(vibrate);
2329 parcel.writeInt(ledARGB);
2330 parcel.writeInt(ledOnMS);
2331 parcel.writeInt(ledOffMS);
2332 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002333
2334 if (fullScreenIntent != null) {
2335 parcel.writeInt(1);
2336 fullScreenIntent.writeToParcel(parcel, 0);
2337 } else {
2338 parcel.writeInt(0);
2339 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340
2341 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002342
John Spurlockfd7f1e02014-03-18 16:41:57 -04002343 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002344
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002345 parcel.writeString(mGroupKey);
2346
2347 parcel.writeString(mSortKey);
2348
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002349 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002350
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002351 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002352
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002353 if (bigContentView != null) {
2354 parcel.writeInt(1);
2355 bigContentView.writeToParcel(parcel, 0);
2356 } else {
2357 parcel.writeInt(0);
2358 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002359
Chris Wren8fd39ec2014-02-27 17:43:26 -05002360 if (headsUpContentView != null) {
2361 parcel.writeInt(1);
2362 headsUpContentView.writeToParcel(parcel, 0);
2363 } else {
2364 parcel.writeInt(0);
2365 }
2366
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002367 parcel.writeInt(visibility);
2368
2369 if (publicVersion != null) {
2370 parcel.writeInt(1);
2371 publicVersion.writeToParcel(parcel, 0);
2372 } else {
2373 parcel.writeInt(0);
2374 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002375
2376 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002377
2378 if (mChannelId != null) {
2379 parcel.writeInt(1);
2380 parcel.writeString(mChannelId);
2381 } else {
2382 parcel.writeInt(0);
2383 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002384 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002385
2386 if (mShortcutId != null) {
2387 parcel.writeInt(1);
2388 parcel.writeString(mShortcutId);
2389 } else {
2390 parcel.writeInt(0);
2391 }
2392
2393 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002394
2395 if (mSettingsText != null) {
2396 parcel.writeInt(1);
2397 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2398 } else {
2399 parcel.writeInt(0);
2400 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002401
2402 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
2405 /**
2406 * Parcelable.Creator that instantiates Notification objects
2407 */
2408 public static final Parcelable.Creator<Notification> CREATOR
2409 = new Parcelable.Creator<Notification>()
2410 {
2411 public Notification createFromParcel(Parcel parcel)
2412 {
2413 return new Notification(parcel);
2414 }
2415
2416 public Notification[] newArray(int size)
2417 {
2418 return new Notification[size];
2419 }
2420 };
2421
2422 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002423 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2424 * <p>
2425 * For backwards compatibility {@code extras} holds some references to "real" member data such
2426 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2427 * fine as long as the object stays in one process.
2428 * <p>
2429 * However, once the notification goes into a parcel each reference gets marshalled separately,
2430 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2431 */
2432 private void fixDuplicateExtras() {
2433 if (extras != null) {
2434 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2435 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2436 }
2437 }
2438
2439 /**
2440 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2441 * separate object, replace it with the field's version to avoid holding duplicate copies.
2442 */
2443 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2444 if (original != null && extras.getParcelable(extraName) != null) {
2445 extras.putParcelable(extraName, original);
2446 }
2447 }
2448
2449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2451 * layout.
2452 *
2453 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2454 * in the view.</p>
2455 * @param context The context for your application / activity.
2456 * @param contentTitle The title that goes in the expanded entry.
2457 * @param contentText The text that goes in the expanded entry.
2458 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2459 * If this is an activity, it must include the
2460 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002461 * that you take care of task management as described in the
2462 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2463 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002465 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002466 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 public void setLatestEventInfo(Context context,
2470 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002471 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2472 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2473 new Throwable());
2474 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002475
Selim Cinek4ac6f602016-06-13 15:47:03 -07002476 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2477 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2478 }
2479
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002480 // ensure that any information already set directly is preserved
2481 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002482
2483 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002485 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
2487 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002488 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002490 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002491
2492 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494
Julia Reynoldsda303542015-11-23 14:00:20 -05002495 /**
2496 * @hide
2497 */
2498 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002499 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002500 }
2501
2502 /**
2503 * @hide
2504 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002505 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002506 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002507 }
2508
Yi Jin6b514142017-10-30 14:54:12 -07002509 /**
2510 * @hide
2511 */
2512 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2513 long token = proto.start(fieldId);
2514 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2515 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2516 proto.write(NotificationProto.FLAGS, this.flags);
2517 proto.write(NotificationProto.COLOR, this.color);
2518 proto.write(NotificationProto.CATEGORY, this.category);
2519 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2520 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2521 if (this.actions != null) {
2522 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2523 }
2524 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2525 proto.write(NotificationProto.VISIBILITY, this.visibility);
2526 }
2527 if (publicVersion != null) {
2528 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2529 }
2530 proto.end(token);
2531 }
2532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 @Override
2534 public String toString() {
2535 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002536 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002537 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002538 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002539 sb.append(priority);
2540 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002541 if (contentView != null) {
2542 sb.append(contentView.getPackage());
2543 sb.append("/0x");
2544 sb.append(Integer.toHexString(contentView.getLayoutId()));
2545 } else {
2546 sb.append("null");
2547 }
2548 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002549 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2550 sb.append("default");
2551 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 int N = this.vibrate.length-1;
2553 sb.append("[");
2554 for (int i=0; i<N; i++) {
2555 sb.append(this.vibrate[i]);
2556 sb.append(',');
2557 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002558 if (N != -1) {
2559 sb.append(this.vibrate[N]);
2560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 } else {
2563 sb.append("null");
2564 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002565 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002566 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002568 } else if (this.sound != null) {
2569 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 } else {
2571 sb.append("null");
2572 }
Chris Wren365b6d32015-07-16 10:39:26 -04002573 if (this.tickerText != null) {
2574 sb.append(" tick");
2575 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002576 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002578 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002579 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002580 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002581 if (this.category != null) {
2582 sb.append(" category=");
2583 sb.append(this.category);
2584 }
2585 if (this.mGroupKey != null) {
2586 sb.append(" groupKey=");
2587 sb.append(this.mGroupKey);
2588 }
2589 if (this.mSortKey != null) {
2590 sb.append(" sortKey=");
2591 sb.append(this.mSortKey);
2592 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002593 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002594 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002595 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002596 }
2597 sb.append(" vis=");
2598 sb.append(visibilityToString(this.visibility));
2599 if (this.publicVersion != null) {
2600 sb.append(" publicVersion=");
2601 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002602 }
2603 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 return sb.toString();
2605 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002606
Dan Sandler1b718782014-07-18 12:43:45 -04002607 /**
2608 * {@hide}
2609 */
2610 public static String visibilityToString(int vis) {
2611 switch (vis) {
2612 case VISIBILITY_PRIVATE:
2613 return "PRIVATE";
2614 case VISIBILITY_PUBLIC:
2615 return "PUBLIC";
2616 case VISIBILITY_SECRET:
2617 return "SECRET";
2618 default:
2619 return "UNKNOWN(" + String.valueOf(vis) + ")";
2620 }
2621 }
2622
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002624 * {@hide}
2625 */
2626 public static String priorityToString(@Priority int pri) {
2627 switch (pri) {
2628 case PRIORITY_MIN:
2629 return "MIN";
2630 case PRIORITY_LOW:
2631 return "LOW";
2632 case PRIORITY_DEFAULT:
2633 return "DEFAULT";
2634 case PRIORITY_HIGH:
2635 return "HIGH";
2636 case PRIORITY_MAX:
2637 return "MAX";
2638 default:
2639 return "UNKNOWN(" + String.valueOf(pri) + ")";
2640 }
2641 }
2642
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002643 /**
2644 * @hide
2645 */
2646 public boolean hasCompletedProgress() {
2647 // not a progress notification; can't be complete
2648 if (!extras.containsKey(EXTRA_PROGRESS)
2649 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2650 return false;
2651 }
2652 // many apps use max 0 for 'indeterminate'; not complete
2653 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2654 return false;
2655 }
2656 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2657 }
2658
Jeff Sharkey000ce802017-04-29 13:13:27 -06002659 /** @removed */
2660 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002661 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002662 return mChannelId;
2663 }
2664
2665 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002666 * Returns the id of the channel this notification posts to.
2667 */
2668 public String getChannelId() {
2669 return mChannelId;
2670 }
2671
Jeff Sharkey000ce802017-04-29 13:13:27 -06002672 /** @removed */
2673 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002674 public long getTimeout() {
2675 return mTimeout;
2676 }
2677
2678 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002679 * Returns the duration from posting after which this notification should be canceled by the
2680 * system, if it's not canceled already.
2681 */
2682 public long getTimeoutAfter() {
2683 return mTimeout;
2684 }
2685
2686 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002687 * Returns what icon should be shown for this notification if it is being displayed in a
2688 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2689 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2690 */
2691 public int getBadgeIconType() {
2692 return mBadgeIcon;
2693 }
2694
2695 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002696 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002697 *
2698 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2699 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002700 */
2701 public String getShortcutId() {
2702 return mShortcutId;
2703 }
2704
Julia Reynolds3aedded2017-03-31 14:42:09 -04002705
2706 /**
2707 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2708 */
2709 public CharSequence getSettingsText() {
2710 return mSettingsText;
2711 }
2712
Julia Reynolds13d898c2017-02-02 12:22:05 -05002713 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002714 * Returns which type of notifications in a group are responsible for audibly alerting the
2715 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2716 * {@link #GROUP_ALERT_SUMMARY}.
2717 */
2718 public @GroupAlertBehavior int getGroupAlertBehavior() {
2719 return mGroupAlertBehavior;
2720 }
2721
2722 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002723 * The small icon representing this notification in the status bar and content view.
2724 *
2725 * @return the small icon representing this notification.
2726 *
2727 * @see Builder#getSmallIcon()
2728 * @see Builder#setSmallIcon(Icon)
2729 */
2730 public Icon getSmallIcon() {
2731 return mSmallIcon;
2732 }
2733
2734 /**
2735 * Used when notifying to clean up legacy small icons.
2736 * @hide
2737 */
2738 public void setSmallIcon(Icon icon) {
2739 mSmallIcon = icon;
2740 }
2741
2742 /**
2743 * The large icon shown in this notification's content view.
2744 * @see Builder#getLargeIcon()
2745 * @see Builder#setLargeIcon(Icon)
2746 */
2747 public Icon getLargeIcon() {
2748 return mLargeIcon;
2749 }
2750
2751 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002752 * @hide
2753 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002754 public boolean isGroupSummary() {
2755 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2756 }
2757
2758 /**
2759 * @hide
2760 */
2761 public boolean isGroupChild() {
2762 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2763 }
2764
2765 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002766 * @hide
2767 */
2768 public boolean suppressAlertingDueToGrouping() {
2769 if (isGroupSummary()
2770 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2771 return true;
2772 } else if (isGroupChild()
2773 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2774 return true;
2775 }
2776 return false;
2777 }
2778
2779 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002780 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002781 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002783 * content views using the platform's notification layout template. If your app supports
2784 * versions of Android as old as API level 4, you can instead use
2785 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2786 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2787 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002788 *
Scott Main183bf112012-08-13 19:12:13 -07002789 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002790 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002791 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002792 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002793 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2794 * .setContentText(subject)
2795 * .setSmallIcon(R.drawable.new_mail)
2796 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002797 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002798 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002799 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002800 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002801 /**
2802 * @hide
2803 */
2804 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2805 "android.rebuild.contentViewActionCount";
2806 /**
2807 * @hide
2808 */
2809 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2810 = "android.rebuild.bigViewActionCount";
2811 /**
2812 * @hide
2813 */
2814 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2815 = "android.rebuild.hudViewActionCount";
2816
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002817 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002818
Selim Cinek6743c0b2017-01-18 18:24:01 -08002819 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2820 SystemProperties.getBoolean("notifications.only_title", true);
2821
Selim Cinek389edcd2017-05-11 19:16:44 -07002822 /**
2823 * The lightness difference that has to be added to the primary text color to obtain the
2824 * secondary text color when the background is light.
2825 */
2826 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2827
2828 /**
2829 * The lightness difference that has to be added to the primary text color to obtain the
2830 * secondary text color when the background is dark.
2831 * A bit less then the above value, since it looks better on dark backgrounds.
2832 */
2833 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2834
Joe Onorato46439ce2010-11-19 13:56:21 -08002835 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002836 private Notification mN;
2837 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002838 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002839 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002840 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002841 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002842 private boolean mIsLegacy;
2843 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002844
2845 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002846 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2847 */
2848 private int mCachedContrastColor = COLOR_INVALID;
2849 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002850 /**
2851 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2852 */
2853 private int mCachedAmbientColor = COLOR_INVALID;
2854 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002855
2856 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002857 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2858 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2859 */
2860 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002861 private int mTextColorsAreForBackground = COLOR_INVALID;
2862 private int mPrimaryTextColor = COLOR_INVALID;
2863 private int mSecondaryTextColor = COLOR_INVALID;
2864 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002865 private int mBackgroundColor = COLOR_INVALID;
2866 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002867 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002868 /**
2869 * A temporary location where actions are stored. If != null the view originally has action
2870 * but doesn't have any for this inflation.
2871 */
2872 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002873 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002874
Anthony Chenad4d1582017-04-10 16:07:58 -07002875 private boolean mTintActionButtons;
2876 private boolean mInNightMode;
2877
Adrian Roos70d7aa32017-01-11 15:39:06 -08002878 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002879 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002880 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002881 * @param context
2882 * A {@link Context} that will be used by the Builder to construct the
2883 * RemoteViews. The Context will not be held past the lifetime of this Builder
2884 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002885 * @param channelId
2886 * The constructed Notification will be posted on this
2887 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2888 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002889 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002890 public Builder(Context context, String channelId) {
2891 this(context, (Notification) null);
2892 mN.mChannelId = channelId;
2893 }
2894
2895 /**
2896 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2897 * instead. All posted Notifications must specify a NotificationChannel Id.
2898 */
2899 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002900 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002901 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002902 }
2903
Joe Onoratocb109a02011-01-18 17:57:41 -08002904 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002905 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002906 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002907 public Builder(Context context, Notification toAdopt) {
2908 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002909 Resources res = mContext.getResources();
2910 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2911
2912 if (res.getBoolean(R.bool.config_enableNightMode)) {
2913 Configuration currentConfig = res.getConfiguration();
2914 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2915 == Configuration.UI_MODE_NIGHT_YES;
2916 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002917
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002918 if (toAdopt == null) {
2919 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002920 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2921 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2922 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002923 mN.priority = PRIORITY_DEFAULT;
2924 mN.visibility = VISIBILITY_PRIVATE;
2925 } else {
2926 mN = toAdopt;
2927 if (mN.actions != null) {
2928 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002929 }
2930
Selim Cineke7238dd2017-12-14 17:48:32 -08002931 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
2932 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
2933 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002934 }
2935
Selim Cinek4ac6f602016-06-13 15:47:03 -07002936 if (mN.getSmallIcon() == null && mN.icon != 0) {
2937 setSmallIcon(mN.icon);
2938 }
2939
2940 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2941 setLargeIcon(mN.largeIcon);
2942 }
2943
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002944 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2945 if (!TextUtils.isEmpty(templateClass)) {
2946 final Class<? extends Style> styleClass
2947 = getNotificationStyleClass(templateClass);
2948 if (styleClass == null) {
2949 Log.d(TAG, "Unknown style class: " + templateClass);
2950 } else {
2951 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002952 final Constructor<? extends Style> ctor =
2953 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002954 ctor.setAccessible(true);
2955 final Style style = ctor.newInstance();
2956 style.restoreFromExtras(mN.extras);
2957
2958 if (style != null) {
2959 setStyle(style);
2960 }
2961 } catch (Throwable t) {
2962 Log.e(TAG, "Could not create Style", t);
2963 }
2964 }
2965 }
2966
2967 }
2968 }
2969
2970 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002971 if (mColorUtil == null) {
2972 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002973 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002974 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002975 }
2976
2977 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002978 * If this notification is duplicative of a Launcher shortcut, sets the
2979 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2980 * the shortcut.
2981 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002982 * This field will be ignored by Launchers that don't support badging, don't show
2983 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002984 *
2985 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2986 * supersedes
2987 */
2988 public Builder setShortcutId(String shortcutId) {
2989 mN.mShortcutId = shortcutId;
2990 return this;
2991 }
2992
2993 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002994 * Sets which icon to display as a badge for this notification.
2995 *
2996 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2997 * {@link #BADGE_ICON_LARGE}.
2998 *
2999 * Note: This value might be ignored, for launchers that don't support badge icons.
3000 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003001 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003002 mN.mBadgeIcon = icon;
3003 return this;
3004 }
3005
3006 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003007 * Sets the group alert behavior for this notification. Use this method to mute this
3008 * notification if alerts for this notification's group should be handled by a different
3009 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003010 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3011 * mute. For example, if you want only the summary of your group to make noise, all
3012 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003013 *
3014 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3015 */
3016 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3017 mN.mGroupAlertBehavior = groupAlertBehavior;
3018 return this;
3019 }
3020
Jeff Sharkey000ce802017-04-29 13:13:27 -06003021 /** @removed */
3022 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003023 public Builder setChannel(String channelId) {
3024 mN.mChannelId = channelId;
3025 return this;
3026 }
3027
3028 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003029 * Specifies the channel the notification should be delivered on.
3030 */
3031 public Builder setChannelId(String channelId) {
3032 mN.mChannelId = channelId;
3033 return this;
3034 }
3035
Jeff Sharkey000ce802017-04-29 13:13:27 -06003036 /** @removed */
3037 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003038 public Builder setTimeout(long durationMs) {
3039 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003040 return this;
3041 }
3042
3043 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003044 * Specifies a duration in milliseconds after which this notification should be canceled,
3045 * if it is not already canceled.
3046 */
3047 public Builder setTimeoutAfter(long durationMs) {
3048 mN.mTimeout = durationMs;
3049 return this;
3050 }
3051
3052 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003053 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003054 *
3055 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3056 * shown anymore by default and must be opted into by using
3057 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003058 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003059 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003060 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003061 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003063 return this;
3064 }
3065
Joe Onoratocb109a02011-01-18 17:57:41 -08003066 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003067 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003068 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003069 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3070 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003071 */
3072 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003073 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003074 return this;
3075 }
3076
3077 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003078 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003079 *
3080 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003081 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003082 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003083 * Useful when showing an elapsed time (like an ongoing phone call).
3084 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003085 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003086 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003087 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003088 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003089 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003090 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003091 */
3092 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003093 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003094 return this;
3095 }
3096
3097 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003098 * Sets the Chronometer to count down instead of counting up.
3099 *
3100 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3101 * If it isn't set the chronometer will count up.
3102 *
3103 * @see #setUsesChronometer(boolean)
3104 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003105 public Builder setChronometerCountDown(boolean countDown) {
3106 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003107 return this;
3108 }
3109
3110 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003111 * Set the small icon resource, which will be used to represent the notification in the
3112 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003113 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003114
3115 * The platform template for the expanded view will draw this icon in the left, unless a
3116 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3117 * icon will be moved to the right-hand side.
3118 *
3119
3120 * @param icon
3121 * A resource ID in the application's package of the drawable to use.
3122 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003123 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003124 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003125 return setSmallIcon(icon != 0
3126 ? Icon.createWithResource(mContext, icon)
3127 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003128 }
3129
Joe Onoratocb109a02011-01-18 17:57:41 -08003130 /**
3131 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3132 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3133 * LevelListDrawable}.
3134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003135 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003136 * @param level The level to use for the icon.
3137 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003138 * @see Notification#icon
3139 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003140 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003141 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003142 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003143 return setSmallIcon(icon);
3144 }
3145
3146 /**
3147 * Set the small icon, which will be used to represent the notification in the
3148 * status bar and content view (unless overriden there by a
3149 * {@link #setLargeIcon(Bitmap) large icon}).
3150 *
3151 * @param icon An Icon object to use.
3152 * @see Notification#icon
3153 */
3154 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003155 mN.setSmallIcon(icon);
3156 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3157 mN.icon = icon.getResId();
3158 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003159 return this;
3160 }
3161
Joe Onoratocb109a02011-01-18 17:57:41 -08003162 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003163 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003164 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003165 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003166 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003167 return this;
3168 }
3169
Joe Onoratocb109a02011-01-18 17:57:41 -08003170 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003171 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003172 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003173 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003174 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003175 return this;
3176 }
3177
Joe Onoratocb109a02011-01-18 17:57:41 -08003178 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003179 * This provides some additional information that is displayed in the notification. No
3180 * guarantees are given where exactly it is displayed.
3181 *
3182 * <p>This information should only be provided if it provides an essential
3183 * benefit to the understanding of the notification. The more text you provide the
3184 * less readable it becomes. For example, an email client should only provide the account
3185 * name here if more than one email account has been added.</p>
3186 *
3187 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3188 * notification header area.
3189 *
3190 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3191 * this will be shown in the third line of text in the platform notification template.
3192 * You should not be using {@link #setProgress(int, int, boolean)} at the
3193 * same time on those versions; they occupy the same place.
3194 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003195 */
3196 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003197 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003198 return this;
3199 }
3200
3201 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003202 * Provides text that will appear as a link to your application's settings.
3203 *
3204 * <p>This text does not appear within notification {@link Style templates} but may
3205 * appear when the user uses an affordance to learn more about the notification.
3206 * Additionally, this text will not appear unless you provide a valid link target by
3207 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3208 *
3209 * <p>This text is meant to be concise description about what the user can customize
3210 * when they click on this link. The recommended maximum length is 40 characters.
3211 * @param text
3212 * @return
3213 */
3214 public Builder setSettingsText(CharSequence text) {
3215 mN.mSettingsText = safeCharSequence(text);
3216 return this;
3217 }
3218
3219 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003220 * Set the remote input history.
3221 *
3222 * This should be set to the most recent inputs that have been sent
3223 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3224 * longer relevant (e.g. for chat notifications once the other party has responded).
3225 *
3226 * The most recent input must be stored at the 0 index, the second most recent at the
3227 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3228 * and how much of each individual input is shown.
3229 *
3230 * <p>Note: The reply text will only be shown on notifications that have least one action
3231 * with a {@code RemoteInput}.</p>
3232 */
3233 public Builder setRemoteInputHistory(CharSequence[] text) {
3234 if (text == null) {
3235 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3236 } else {
3237 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3238 CharSequence[] safe = new CharSequence[N];
3239 for (int i = 0; i < N; i++) {
3240 safe[i] = safeCharSequence(text[i]);
3241 }
3242 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3243 }
3244 return this;
3245 }
3246
3247 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003248 * Sets the number of items this notification represents. May be displayed as a badge count
3249 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003250 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003251 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003252 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003253 return this;
3254 }
3255
Joe Onoratocb109a02011-01-18 17:57:41 -08003256 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003257 * A small piece of additional information pertaining to this notification.
3258 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003259 * The platform template will draw this on the last line of the notification, at the far
3260 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003261 *
3262 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3263 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3264 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003265 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003266 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003267 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003268 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003269 return this;
3270 }
3271
Joe Onoratocb109a02011-01-18 17:57:41 -08003272 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003273 * Set the progress this notification represents.
3274 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003275 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003276 */
3277 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003278 mN.extras.putInt(EXTRA_PROGRESS, progress);
3279 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3280 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003281 return this;
3282 }
3283
3284 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003285 * Supply a custom RemoteViews to use instead of the platform template.
3286 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003287 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003289 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003290 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003291 return setCustomContentView(views);
3292 }
3293
3294 /**
3295 * Supply custom RemoteViews to use instead of the platform template.
3296 *
3297 * This will override the layout that would otherwise be constructed by this Builder
3298 * object.
3299 */
3300 public Builder setCustomContentView(RemoteViews contentView) {
3301 mN.contentView = contentView;
3302 return this;
3303 }
3304
3305 /**
3306 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3307 *
3308 * This will override the expanded layout that would otherwise be constructed by this
3309 * Builder object.
3310 */
3311 public Builder setCustomBigContentView(RemoteViews contentView) {
3312 mN.bigContentView = contentView;
3313 return this;
3314 }
3315
3316 /**
3317 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3318 *
3319 * This will override the heads-up layout that would otherwise be constructed by this
3320 * Builder object.
3321 */
3322 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3323 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003324 return this;
3325 }
3326
Joe Onoratocb109a02011-01-18 17:57:41 -08003327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003328 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3329 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003330 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3331 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3332 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003333 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003334 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003335 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003336 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003339 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003340 return this;
3341 }
3342
Joe Onoratocb109a02011-01-18 17:57:41 -08003343 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003344 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3345 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003346 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003347 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003348 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003349 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003350 return this;
3351 }
3352
Joe Onoratocb109a02011-01-18 17:57:41 -08003353 /**
3354 * An intent to launch instead of posting the notification to the status bar.
3355 * Only for use with extremely high-priority notifications demanding the user's
3356 * <strong>immediate</strong> attention, such as an incoming phone call or
3357 * alarm clock that the user has explicitly set to a particular time.
3358 * If this facility is used for something else, please give the user an option
3359 * to turn it off and use a normal notification, as this can be extremely
3360 * disruptive.
3361 *
Chris Wren47c20a12014-06-18 17:27:29 -04003362 * <p>
3363 * The system UI may choose to display a heads-up notification, instead of
3364 * launching this intent, while the user is using the device.
3365 * </p>
3366 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003367 * @param intent The pending intent to launch.
3368 * @param highPriority Passing true will cause this notification to be sent
3369 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003370 *
3371 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003372 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003373 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003374 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003375 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3376 return this;
3377 }
3378
Joe Onoratocb109a02011-01-18 17:57:41 -08003379 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003380 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003381 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003382 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003383 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003384 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003385 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003386 return this;
3387 }
3388
Joe Onoratocb109a02011-01-18 17:57:41 -08003389 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003390 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003391 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003392 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003393 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003394 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003395 setTicker(tickerText);
3396 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003397 return this;
3398 }
3399
Joe Onoratocb109a02011-01-18 17:57:41 -08003400 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003401 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003402 *
3403 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003404 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3405 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003406 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003407 public Builder setLargeIcon(Bitmap b) {
3408 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3409 }
3410
3411 /**
3412 * Add a large icon to the notification content view.
3413 *
3414 * In the platform template, this image will be shown on the left of the notification view
3415 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3416 * badge atop the large icon).
3417 */
3418 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003419 mN.mLargeIcon = icon;
3420 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003421 return this;
3422 }
3423
Joe Onoratocb109a02011-01-18 17:57:41 -08003424 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003425 * Set the sound to play.
3426 *
John Spurlockc0650f022014-07-19 13:22:39 -04003427 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3428 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003429 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003430 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003431 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003432 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003433 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003434 mN.sound = sound;
3435 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003436 return this;
3437 }
3438
Joe Onoratocb109a02011-01-18 17:57:41 -08003439 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003440 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003442 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3443 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003444 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003445 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003446 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003447 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003448 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003449 mN.sound = sound;
3450 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003451 return this;
3452 }
3453
Joe Onoratocb109a02011-01-18 17:57:41 -08003454 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003455 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3456 * use during playback.
3457 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003458 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003459 * @see Notification#sound
3460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003461 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003462 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 mN.sound = sound;
3464 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003465 return this;
3466 }
3467
3468 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003469 * Set the vibration pattern to use.
3470 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003471 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3472 * <code>pattern</code> parameter.
3473 *
Chris Wren47c20a12014-06-18 17:27:29 -04003474 * <p>
3475 * A notification that vibrates is more likely to be presented as a heads-up notification.
3476 * </p>
3477 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003478 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003479 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003480 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003481 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003482 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003484 return this;
3485 }
3486
Joe Onoratocb109a02011-01-18 17:57:41 -08003487 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003488 * Set the desired color for the indicator LED on the device, as well as the
3489 * blink duty cycle (specified in milliseconds).
3490 *
3491
3492 * Not all devices will honor all (or even any) of these values.
3493 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003494 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003495 * @see Notification#ledARGB
3496 * @see Notification#ledOnMS
3497 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003498 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003499 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003500 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003501 mN.ledARGB = argb;
3502 mN.ledOnMS = onMs;
3503 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003504 if (onMs != 0 || offMs != 0) {
3505 mN.flags |= FLAG_SHOW_LIGHTS;
3506 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003507 return this;
3508 }
3509
Joe Onoratocb109a02011-01-18 17:57:41 -08003510 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003511 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003512 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003513
3514 * Ongoing notifications cannot be dismissed by the user, so your application or service
3515 * must take care of canceling them.
3516 *
3517
3518 * They are typically used to indicate a background task that the user is actively engaged
3519 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3520 * (e.g., a file download, sync operation, active network connection).
3521 *
3522
3523 * @see Notification#FLAG_ONGOING_EVENT
3524 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003525 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003526 public Builder setOngoing(boolean ongoing) {
3527 setFlag(FLAG_ONGOING_EVENT, ongoing);
3528 return this;
3529 }
3530
Joe Onoratocb109a02011-01-18 17:57:41 -08003531 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003532 * Set whether this notification should be colorized. When set, the color set with
3533 * {@link #setColor(int)} will be used as the background color of this notification.
3534 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003535 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3536 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003537 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003538 * For most styles, the coloring will only be applied if the notification is for a
3539 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003540 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003541 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003542 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003543 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003544 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003545 */
3546 public Builder setColorized(boolean colorize) {
3547 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3548 return this;
3549 }
3550
3551 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 * Set this flag if you would only like the sound, vibrate
3553 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003554 *
3555 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003556 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003557 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3558 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3559 return this;
3560 }
3561
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003563 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 *
3565 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003566 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003567 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003568 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003569 return this;
3570 }
3571
Joe Onoratocb109a02011-01-18 17:57:41 -08003572 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003573 * Set whether or not this notification should not bridge to other devices.
3574 *
3575 * <p>Some notifications can be bridged to other devices for remote display.
3576 * This hint can be set to recommend this notification not be bridged.
3577 */
3578 public Builder setLocalOnly(boolean localOnly) {
3579 setFlag(FLAG_LOCAL_ONLY, localOnly);
3580 return this;
3581 }
3582
3583 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003584 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003585 * <p>
3586 * The value should be one or more of the following fields combined with
3587 * bitwise-or:
3588 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3589 * <p>
3590 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003591 *
3592 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003593 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003594 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003595 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003596 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003597 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003598 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003599 return this;
3600 }
3601
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 /**
3603 * Set the priority of this notification.
3604 *
3605 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003606 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003607 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003608 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003609 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003610 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 return this;
3612 }
Joe Malin8d40d042012-11-05 11:36:40 -08003613
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003614 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003615 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003616 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003617 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003618 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003619 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003620 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003621 return this;
3622 }
3623
3624 /**
Chris Wrendde75302014-03-26 17:24:15 -04003625 * Add a person that is relevant to this notification.
3626 *
Chris Wrene6c48932014-09-29 17:19:27 -04003627 * <P>
3628 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003629 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3630 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3631 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003632 * </P>
3633 *
3634 * <P>
3635 * The person should be specified by the {@code String} representation of a
3636 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3637 * </P>
3638 *
3639 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3640 * URIs. The path part of these URIs must exist in the contacts database, in the
3641 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3642 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003643 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3644 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003645 * </P>
3646 *
3647 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003648 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003649 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003650 */
Chris Wrene6c48932014-09-29 17:19:27 -04003651 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003652 addPerson(new Person().setUri(uri));
3653 return this;
3654 }
3655
3656 /**
3657 * Add a person that is relevant to this notification.
3658 *
3659 * <P>
3660 * Depending on user preferences, this annotation may allow the notification to pass
3661 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3662 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3663 * appear more prominently in the user interface.
3664 * </P>
3665 *
3666 * <P>
3667 * A person should usually contain a uri in order to benefit from the ranking boost.
3668 * However, even if no uri is provided, it's beneficial to provide other people in the
3669 * notification, such that listeners and voice only devices can announce and handle them
3670 * properly.
3671 * </P>
3672 *
3673 * @param person the person to add.
3674 * @see Notification#EXTRA_PEOPLE_LIST
3675 */
3676 public Builder addPerson(Person person) {
3677 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003678 return this;
3679 }
3680
3681 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003682 * Set this notification to be part of a group of notifications sharing the same key.
3683 * Grouped notifications may display in a cluster or stack on devices which
3684 * support such rendering.
3685 *
3686 * <p>To make this notification the summary for its group, also call
3687 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3688 * {@link #setSortKey}.
3689 * @param groupKey The group key of the group.
3690 * @return this object for method chaining
3691 */
3692 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003693 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003694 return this;
3695 }
3696
3697 /**
3698 * Set this notification to be the group summary for a group of notifications.
3699 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003700 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3701 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003702 * @param isGroupSummary Whether this notification should be a group summary.
3703 * @return this object for method chaining
3704 */
3705 public Builder setGroupSummary(boolean isGroupSummary) {
3706 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3707 return this;
3708 }
3709
3710 /**
3711 * Set a sort key that orders this notification among other notifications from the
3712 * same package. This can be useful if an external sort was already applied and an app
3713 * would like to preserve this. Notifications will be sorted lexicographically using this
3714 * value, although providing different priorities in addition to providing sort key may
3715 * cause this value to be ignored.
3716 *
3717 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003718 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003719 *
3720 * @see String#compareTo(String)
3721 */
3722 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003723 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003724 return this;
3725 }
3726
3727 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003728 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003729 *
Griff Hazen720042b2014-02-24 15:46:56 -08003730 * <p>Values within the Bundle will replace existing extras values in this Builder.
3731 *
3732 * @see Notification#extras
3733 */
Griff Hazen959591e2014-05-15 22:26:18 -07003734 public Builder addExtras(Bundle extras) {
3735 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003736 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003737 }
3738 return this;
3739 }
3740
3741 /**
3742 * Set metadata for this notification.
3743 *
3744 * <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 -04003745 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003746 * called.
3747 *
Griff Hazen720042b2014-02-24 15:46:56 -08003748 * <p>Replaces any existing extras values with those from the provided Bundle.
3749 * Use {@link #addExtras} to merge in metadata instead.
3750 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003751 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003752 */
Griff Hazen959591e2014-05-15 22:26:18 -07003753 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003754 if (extras != null) {
3755 mUserExtras = extras;
3756 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003757 return this;
3758 }
3759
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003760 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003761 * Get the current metadata Bundle used by this notification Builder.
3762 *
3763 * <p>The returned Bundle is shared with this Builder.
3764 *
3765 * <p>The current contents of this Bundle are copied into the Notification each time
3766 * {@link #build()} is called.
3767 *
3768 * @see Notification#extras
3769 */
3770 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003771 return mUserExtras;
3772 }
3773
3774 private Bundle getAllExtras() {
3775 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3776 saveExtras.putAll(mN.extras);
3777 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003778 }
3779
3780 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003781 * Add an action to this notification. Actions are typically displayed by
3782 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003783 * <p>
3784 * Every action must have an icon (32dp square and matching the
3785 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3786 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3787 * <p>
3788 * A notification in its expanded form can display up to 3 actions, from left to right in
3789 * the order they were added. Actions will not be displayed when the notification is
3790 * collapsed, however, so be sure that any essential functions may be accessed by the user
3791 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003792 *
3793 * @param icon Resource ID of a drawable that represents the action.
3794 * @param title Text describing the action.
3795 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003796 *
3797 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003798 */
Dan Sandler86647982015-05-13 23:41:13 -04003799 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003800 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003801 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003802 return this;
3803 }
3804
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003805 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003806 * Add an action to this notification. Actions are typically displayed by
3807 * the system as a button adjacent to the notification content.
3808 * <p>
3809 * Every action must have an icon (32dp square and matching the
3810 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3811 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3812 * <p>
3813 * A notification in its expanded form can display up to 3 actions, from left to right in
3814 * the order they were added. Actions will not be displayed when the notification is
3815 * collapsed, however, so be sure that any essential functions may be accessed by the user
3816 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3817 *
3818 * @param action The action to add.
3819 */
3820 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003821 if (action != null) {
3822 mActions.add(action);
3823 }
Griff Hazen959591e2014-05-15 22:26:18 -07003824 return this;
3825 }
3826
3827 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003828 * Alter the complete list of actions attached to this notification.
3829 * @see #addAction(Action).
3830 *
3831 * @param actions
3832 * @return
3833 */
3834 public Builder setActions(Action... actions) {
3835 mActions.clear();
3836 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003837 if (actions[i] != null) {
3838 mActions.add(actions[i]);
3839 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003840 }
3841 return this;
3842 }
3843
3844 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003845 * Add a rich notification style to be applied at build time.
3846 *
3847 * @param style Object responsible for modifying the notification style.
3848 */
3849 public Builder setStyle(Style style) {
3850 if (mStyle != style) {
3851 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003852 if (mStyle != null) {
3853 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003854 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3855 } else {
3856 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003857 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003858 }
3859 return this;
3860 }
3861
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003862 /**
3863 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003864 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003865 * @return The same Builder.
3866 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003867 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003868 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003869 return this;
3870 }
3871
3872 /**
3873 * Supply a replacement Notification whose contents should be shown in insecure contexts
3874 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3875 * @param n A replacement notification, presumably with some or all info redacted.
3876 * @return The same Builder.
3877 */
3878 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003879 if (n != null) {
3880 mN.publicVersion = new Notification();
3881 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3882 } else {
3883 mN.publicVersion = null;
3884 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003885 return this;
3886 }
3887
Griff Hazenb720abe2014-05-20 13:15:30 -07003888 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003889 * Apply an extender to this notification builder. Extenders may be used to add
3890 * metadata or change options on this builder.
3891 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003892 public Builder extend(Extender extender) {
3893 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003894 return this;
3895 }
3896
Dan Sandler4e787062015-06-17 15:09:48 -04003897 /**
3898 * @hide
3899 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003900 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003901 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003902 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003903 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003904 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003905 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003906 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003907 }
3908
Dan Sandler26e81cf2014-05-06 10:01:27 -04003909 /**
3910 * Sets {@link Notification#color}.
3911 *
3912 * @param argb The accent color to use
3913 *
3914 * @return The same Builder.
3915 */
Tor Norbye80756e32015-03-02 09:39:27 -08003916 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003917 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003918 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003919 return this;
3920 }
3921
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003922 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003923 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3924 // This user can never be a badged profile,
3925 // and also includes USER_ALL system notifications.
3926 return null;
3927 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003928 // Note: This assumes that the current user can read the profile badge of the
3929 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003930 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003931 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003932 }
3933
3934 private Bitmap getProfileBadge() {
3935 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003936 if (badge == null) {
3937 return null;
3938 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003939 final int size = mContext.getResources().getDimensionPixelSize(
3940 R.dimen.notification_badge_size);
3941 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003942 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003943 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003944 badge.draw(canvas);
3945 return bitmap;
3946 }
3947
Selim Cinekc848c3a2016-01-13 15:27:30 -08003948 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003949 Bitmap profileBadge = getProfileBadge();
3950
Kenny Guy98193ea2014-07-24 19:54:37 +01003951 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003952 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3953 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003954 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003955 contentView.setDrawableTint(R.id.profile_badge, false,
3956 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003957 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003958 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003959 }
3960
Christoph Studerfe718432014-09-01 18:21:18 +02003961 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003962 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003963 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003964 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003965 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003966 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003967 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003968 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003969 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003970 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003971 }
3972
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003973 /**
3974 * Resets the notification header to its original state
3975 */
3976 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003977 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3978 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003979 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003980 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003981 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003982 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003983 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08003984 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
3985 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003986 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08003987 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003988 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003989 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003990 contentView.setImageViewIcon(R.id.profile_badge, null);
3991 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003992 }
3993
3994 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003995 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3996 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003997 }
3998
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003999 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004000 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004001 }
4002
4003 /**
4004 * @param hasProgress whether the progress bar should be shown and set
4005 */
4006 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004007 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4008 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004009 }
4010
Adrian Roos70d7aa32017-01-11 15:39:06 -08004011 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004012 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004013
Christoph Studerfe718432014-09-01 18:21:18 +02004014 resetStandardTemplate(contentView);
4015
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004016 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004017 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004018 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek88188f22017-09-19 16:46:56 -07004019 bindLargeIcon(contentView, p.hideLargeIcon, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004020 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4021 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004022 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004023 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004024 if (!p.ambient) {
4025 setTextViewColorPrimary(contentView, R.id.title);
4026 }
Selim Cinek954cc232016-05-20 13:29:23 -07004027 contentView.setViewLayoutWidth(R.id.title, showProgress
4028 ? ViewGroup.LayoutParams.WRAP_CONTENT
4029 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004030 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004031 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004032 int textId = showProgress ? com.android.internal.R.id.text_line_1
4033 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004034 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004035 if (!p.ambient) {
4036 setTextViewColorSecondary(contentView, textId);
4037 }
Selim Cinek41598732016-01-11 16:58:37 -08004038 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004039 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004040
Selim Cinek279fa862016-06-14 10:57:25 -07004041 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004042
Selim Cinek29603462015-11-17 19:04:39 -08004043 return contentView;
4044 }
4045
Selim Cinek48f66b72017-08-18 16:17:51 -07004046 private CharSequence processTextSpans(CharSequence text) {
4047 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004048 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004049 }
4050 return text;
4051 }
4052
Selim Cinek7b9605b2017-01-19 17:36:00 -08004053 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4054 ensureColors();
4055 contentView.setTextColor(id, mPrimaryTextColor);
4056 }
4057
Selim Cinek48f66b72017-08-18 16:17:51 -07004058 private boolean hasForegroundColor() {
4059 return mForegroundColor != COLOR_INVALID;
4060 }
4061
Selim Cinek389edcd2017-05-11 19:16:44 -07004062 /**
4063 * @return the primary text color
4064 * @hide
4065 */
4066 @VisibleForTesting
4067 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004068 ensureColors();
4069 return mPrimaryTextColor;
4070 }
4071
Selim Cinek389edcd2017-05-11 19:16:44 -07004072 /**
4073 * @return the secondary text color
4074 * @hide
4075 */
4076 @VisibleForTesting
4077 public int getSecondaryTextColor() {
4078 ensureColors();
4079 return mSecondaryTextColor;
4080 }
4081
Selim Cinek7b9605b2017-01-19 17:36:00 -08004082 private int getActionBarColor() {
4083 ensureColors();
4084 return mActionBarColor;
4085 }
4086
Selim Cinek622c64a2017-04-17 17:10:05 -07004087 private int getActionBarColorDeEmphasized() {
4088 int backgroundColor = getBackgroundColor();
4089 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4090 }
4091
Selim Cinek7b9605b2017-01-19 17:36:00 -08004092 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4093 ensureColors();
4094 contentView.setTextColor(id, mSecondaryTextColor);
4095 }
4096
4097 private void ensureColors() {
4098 int backgroundColor = getBackgroundColor();
4099 if (mPrimaryTextColor == COLOR_INVALID
4100 || mSecondaryTextColor == COLOR_INVALID
4101 || mActionBarColor == COLOR_INVALID
4102 || mTextColorsAreForBackground != backgroundColor) {
4103 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004104 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004105 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4106 backgroundColor);
4107 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4108 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004109 if (backgroundColor != COLOR_DEFAULT
4110 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4111 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4112 mPrimaryTextColor, backgroundColor, 4.5);
4113 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4114 mSecondaryTextColor, backgroundColor, 4.5);
4115 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004116 } else {
4117 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4118 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4119 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4120 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004121 // We only respect the given colors if worst case Black or White still has
4122 // contrast
4123 boolean backgroundLight = backLum > textLum
4124 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4125 || backLum <= textLum
4126 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004127 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004128 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004129 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4130 mForegroundColor,
4131 backgroundColor,
4132 true /* findFG */,
4133 4.5f);
4134 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004135 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004136 } else {
4137 mSecondaryTextColor =
4138 NotificationColorUtil.findContrastColorAgainstDark(
4139 mForegroundColor,
4140 backgroundColor,
4141 true /* findFG */,
4142 4.5f);
4143 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004144 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004145 }
4146 } else {
4147 mPrimaryTextColor = mForegroundColor;
4148 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004149 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4150 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004151 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4152 backgroundColor) < 4.5f) {
4153 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004154 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004155 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4156 mSecondaryTextColor,
4157 backgroundColor,
4158 true /* findFG */,
4159 4.5f);
4160 } else {
4161 mSecondaryTextColor
4162 = NotificationColorUtil.findContrastColorAgainstDark(
4163 mSecondaryTextColor,
4164 backgroundColor,
4165 true /* findFG */,
4166 4.5f);
4167 }
4168 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004169 mSecondaryTextColor, backgroundLight
4170 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4171 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004172 }
4173 }
4174 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004175 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4176 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004177 }
4178 }
4179
4180 private void updateBackgroundColor(RemoteViews contentView) {
4181 if (isColorized()) {
4182 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4183 getBackgroundColor());
4184 } else {
4185 // Clear it!
4186 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4187 0);
4188 }
4189 }
4190
Selim Cinek860b6da2015-12-16 19:02:19 -08004191 /**
4192 * @param remoteView the remote view to update the minheight in
4193 * @param hasMinHeight does it have a mimHeight
4194 * @hide
4195 */
4196 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4197 int minHeight = 0;
4198 if (hasMinHeight) {
4199 // we need to set the minHeight of the notification
4200 minHeight = mContext.getResources().getDimensionPixelSize(
4201 com.android.internal.R.dimen.notification_min_content_height);
4202 }
4203 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4204 }
4205
Selim Cinek29603462015-11-17 19:04:39 -08004206 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004207 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4208 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4209 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4210 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004211 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004212 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004213 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004214 contentView.setProgressBackgroundTintList(
4215 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4216 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004217 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004218 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004219 contentView.setProgressTintList(R.id.progress, colorStateList);
4220 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004221 }
Selim Cinek29603462015-11-17 19:04:39 -08004222 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004223 } else {
4224 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004225 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004226 }
Joe Onorato561d3852010-11-20 18:09:34 -08004227 }
4228
Selim Cinek88188f22017-09-19 16:46:56 -07004229 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4230 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004231 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4232 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4233 }
Selim Cinek88188f22017-09-19 16:46:56 -07004234 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4235 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004236 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4237 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4238 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004239 int endMargin = R.dimen.notification_content_picture_margin;
4240 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4241 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4242 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004243 }
4244 // Bind the reply action
4245 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004246
Selim Cinek88188f22017-09-19 16:46:56 -07004247 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4248 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4249 if (actionVisible) {
4250 // We're only showing the icon as big if we're hiding the large icon
4251 int contrastColor = resolveContrastColor();
4252 int iconColor;
4253 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004254 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004255 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004256 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004257 contentView.setOnClickPendingIntent(R.id.right_icon,
4258 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004259 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004260 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4261 ? Color.BLACK : Color.WHITE;
4262 } else {
4263 contentView.setImageViewResource(R.id.right_icon,
4264 R.drawable.ic_reply_notification_large);
4265 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4266 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004267 }
Selim Cinek88188f22017-09-19 16:46:56 -07004268 contentView.setDrawableTint(replyId,
4269 false /* targetBackground */,
4270 iconColor,
4271 PorterDuff.Mode.SRC_ATOP);
4272 contentView.setOnClickPendingIntent(replyId,
4273 action.actionIntent);
4274 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4275 } else {
4276 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004277 }
Selim Cinek88188f22017-09-19 16:46:56 -07004278 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4279 ? View.VISIBLE
4280 : View.GONE);
4281 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004282 ? View.VISIBLE
4283 : View.GONE);
4284 }
4285
4286 private Action findReplyAction() {
4287 ArrayList<Action> actions = mActions;
4288 if (mOriginalActions != null) {
4289 actions = mOriginalActions;
4290 }
4291 int numActions = actions.size();
4292 for (int i = 0; i < numActions; i++) {
4293 Action action = actions.get(i);
4294 if (hasValidRemoteInput(action)) {
4295 return action;
4296 }
4297 }
4298 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004299 }
4300
Selim Cinekafeed292017-12-12 17:32:44 -08004301 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4302 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004303 bindSmallIcon(contentView, ambient);
4304 bindHeaderAppName(contentView, ambient);
4305 if (!ambient) {
4306 // Ambient view does not have these
4307 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004308 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004309 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004310 bindProfileBadge(contentView);
4311 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004312 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004313 }
4314
4315 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004316 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004317 contentView.setDrawableTint(R.id.expand_button, false, color,
4318 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004319 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004320 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004321 }
4322
Selim Cinek99104832017-01-25 14:47:33 -08004323 /**
4324 * @return the color that is used as the first primary highlight color. This is applied
4325 * in several places like the action buttons or the app name in the header.
4326 */
4327 private int getPrimaryHighlightColor() {
4328 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4329 }
4330
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004331 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4332 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004333 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004334 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004335 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4336 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4337 contentView.setLong(R.id.chronometer, "setBase",
4338 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4339 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004340 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004341 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004342 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004343 } else {
4344 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4345 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004346 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004347 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004348 } else {
4349 // We still want a time to be set but gone, such that we can show and hide it
4350 // on demand in case it's a child notification without anything in the header
4351 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004352 }
4353 }
4354
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004355 private void bindHeaderText(RemoteViews contentView) {
4356 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4357 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004358 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004359 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004360 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004361 if (headerText == null
4362 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4363 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4364 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4365 }
4366 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004367 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004368 contentView.setTextViewText(R.id.header_text, processTextSpans(
4369 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004370 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004371 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4372 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004373 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004374 }
4375 }
4376
Selim Cinekafeed292017-12-12 17:32:44 -08004377 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4378 if (!TextUtils.isEmpty(secondaryText)) {
4379 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4380 processLegacyText(secondaryText)));
4381 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4382 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4383 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4384 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4385 }
4386 }
4387
Adrian Rooseba05822016-04-22 17:09:27 -07004388 /**
4389 * @hide
4390 */
4391 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004392 CharSequence name = null;
4393 final PackageManager pm = mContext.getPackageManager();
4394 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4395 // only system packages which lump together a bunch of unrelated stuff
4396 // may substitute a different name to make the purpose of the
4397 // notification more clear. the correct package label should always
4398 // be accessible via SystemUI.
4399 final String pkg = mContext.getPackageName();
4400 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4401 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4402 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4403 name = subName;
4404 } else {
4405 Log.w(TAG, "warning: pkg "
4406 + pkg + " attempting to substitute app name '" + subName
4407 + "' without holding perm "
4408 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4409 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004410 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004411 if (TextUtils.isEmpty(name)) {
4412 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4413 }
4414 if (TextUtils.isEmpty(name)) {
4415 // still nothing?
4416 return null;
4417 }
4418
4419 return String.valueOf(name);
4420 }
Adrian Roos487374f2017-01-11 15:48:14 -08004421 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004422 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004423 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004424 setTextViewColorPrimary(contentView, R.id.app_name_text);
4425 } else {
4426 contentView.setTextColor(R.id.app_name_text,
4427 ambient ? resolveAmbientColor() : resolveContrastColor());
4428 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004429 }
4430
Adrian Roos487374f2017-01-11 15:48:14 -08004431 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004432 if (mN.mSmallIcon == null && mN.icon != 0) {
4433 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4434 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004435 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004436 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004437 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004438 }
4439
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004440 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004441 * @return true if the built notification will show the time or the chronometer; false
4442 * otherwise
4443 */
4444 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004445 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004446 }
4447
Christoph Studerfe718432014-09-01 18:21:18 +02004448 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004449 // actions_container is only reset when there are no actions to avoid focus issues with
4450 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004451 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004452 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004453
4454 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4455 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4456
4457 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4458 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4459 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4460 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004461
4462 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004463 }
4464
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004465 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004466 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004467 }
4468
Adrian Roos70d7aa32017-01-11 15:39:06 -08004469 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4470 StandardTemplateParams p) {
4471 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004472
Christoph Studerfe718432014-09-01 18:21:18 +02004473 resetStandardTemplateWithActions(big);
4474
Adrian Roose458aa82015-12-08 16:17:19 -08004475 boolean validRemoteInput = false;
4476
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004477 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004478 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004479 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004480 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004481 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004482 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004483 if (p.ambient) {
4484 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004485 } else if (isColorized()) {
4486 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4487 } else {
4488 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4489 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004490 }
Adrian Roosf852a422016-06-03 13:33:43 -07004491 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4492 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004493 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004494 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004495 Action action = mActions.get(i);
4496 validRemoteInput |= hasValidRemoteInput(action);
4497
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004498 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004499 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004500 big.addView(R.id.actions, button);
4501 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004502 } else {
4503 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004504 }
Adrian Roose458aa82015-12-08 16:17:19 -08004505
4506 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004507 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004508 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4509 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004510 big.setTextViewText(R.id.notification_material_reply_text_1,
4511 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004512 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004513
4514 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4515 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004516 big.setTextViewText(R.id.notification_material_reply_text_2,
4517 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004518 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004519
4520 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4521 big.setViewVisibility(
4522 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004523 big.setTextViewText(R.id.notification_material_reply_text_3,
4524 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004525 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004526 }
4527 }
4528 }
4529
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004530 return big;
4531 }
4532
Adrian Roose458aa82015-12-08 16:17:19 -08004533 private boolean hasValidRemoteInput(Action action) {
4534 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4535 // Weird actions
4536 return false;
4537 }
4538
4539 RemoteInput[] remoteInputs = action.getRemoteInputs();
4540 if (remoteInputs == null) {
4541 return false;
4542 }
4543
4544 for (RemoteInput r : remoteInputs) {
4545 CharSequence[] choices = r.getChoices();
4546 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4547 return true;
4548 }
4549 }
4550 return false;
4551 }
4552
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004553 /**
4554 * Construct a RemoteViews for the final 1U notification layout. In order:
4555 * 1. Custom contentView from the caller
4556 * 2. Style's proposed content view
4557 * 3. Standard template view
4558 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004559 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004560 return createContentView(false /* increasedheight */ );
4561 }
4562
4563 /**
4564 * Construct a RemoteViews for the smaller content view.
4565 *
4566 * @param increasedHeight true if this layout be created with an increased height. Some
4567 * styles may support showing more then just that basic 1U size
4568 * and the system may decide to render important notifications
4569 * slightly bigger even when collapsed.
4570 *
4571 * @hide
4572 */
4573 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004574 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004575 return mN.contentView;
4576 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004577 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004578 if (styleView != null) {
4579 return styleView;
4580 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004581 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004582 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004583 }
4584
Selim Cineka7679b62017-05-10 16:33:25 -07004585 private boolean useExistingRemoteView() {
4586 return mStyle == null || (!mStyle.displayCustomViewInline()
4587 && !mRebuildStyledRemoteViews);
4588 }
4589
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004590 /**
4591 * Construct a RemoteViews for the final big notification layout.
4592 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004593 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004594 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004595 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004596 return mN.bigContentView;
4597 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004598 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004599 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004600 } else if (mActions.size() != 0) {
4601 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004602 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004603 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004604 return result;
4605 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004606
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004607 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004608 * Construct a RemoteViews for the final notification header only. This will not be
4609 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004610 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004611 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004612 * @hide
4613 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004614 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004615 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4616 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004617 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004618 ambient ? R.layout.notification_template_ambient_header
4619 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004620 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004621 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004622 if (colorized != null) {
4623 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4624 } else {
4625 mN.extras.remove(EXTRA_COLORIZED);
4626 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004627 return header;
4628 }
4629
Adrian Roos487374f2017-01-11 15:48:14 -08004630 /**
4631 * Construct a RemoteViews for the ambient version of the notification.
4632 *
4633 * @hide
4634 */
4635 public RemoteViews makeAmbientNotification() {
4636 RemoteViews ambient = applyStandardTemplateWithActions(
4637 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004638 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004639 return ambient;
4640 }
4641
Selim Cinek29603462015-11-17 19:04:39 -08004642 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004643 if (result != null) {
4644 result.setViewVisibility(R.id.text_line_1, View.GONE);
4645 }
Selim Cinek29603462015-11-17 19:04:39 -08004646 }
4647
Selim Cinek6743c0b2017-01-18 18:24:01 -08004648 /**
4649 * Adapt the Notification header if this view is used as an expanded view.
4650 *
4651 * @hide
4652 */
4653 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004654 if (result != null) {
4655 result.setBoolean(R.id.notification_header, "setExpanded", true);
4656 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004657 }
4658
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004659 /**
4660 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004661 *
4662 * @param increasedHeight true if this layout be created with an increased height. Some
4663 * styles may support showing more then just that basic 1U size
4664 * and the system may decide to render important notifications
4665 * slightly bigger even when collapsed.
4666 *
4667 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004668 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004669 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004670 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004671 return mN.headsUpContentView;
4672 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004673 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4674 if (styleView != null) {
4675 return styleView;
4676 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004677 } else if (mActions.size() == 0) {
4678 return null;
4679 }
4680
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004681 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004682 }
4683
Selim Cinek624c02db2015-12-14 21:00:02 -08004684 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004685 * Construct a RemoteViews for the final heads-up notification layout.
4686 */
4687 public RemoteViews createHeadsUpContentView() {
4688 return createHeadsUpContentView(false /* useIncreasedHeight */);
4689 }
4690
4691 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004692 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4693 *
4694 * @hide
4695 */
4696 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004697 return makePublicView(false /* ambient */);
4698 }
4699
4700 /**
4701 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4702 *
4703 * @hide
4704 */
4705 public RemoteViews makePublicAmbientNotification() {
4706 return makePublicView(true /* ambient */);
4707 }
4708
4709 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004710 if (mN.publicVersion != null) {
4711 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004712 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004713 }
4714 Bundle savedBundle = mN.extras;
4715 Style style = mStyle;
4716 mStyle = null;
4717 Icon largeIcon = mN.mLargeIcon;
4718 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004719 Bitmap largeIconLegacy = mN.largeIcon;
4720 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004721 ArrayList<Action> actions = mActions;
4722 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004723 Bundle publicExtras = new Bundle();
4724 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4725 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4726 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4727 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004728 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4729 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004730 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004731 RemoteViews view;
4732 if (ambient) {
4733 publicExtras.putCharSequence(EXTRA_TITLE,
4734 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4735 view = makeAmbientNotification();
4736 } else{
4737 view = makeNotificationHeader(false /* ambient */);
4738 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4739 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004740 mN.extras = savedBundle;
4741 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004742 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004743 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004744 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004745 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004746 }
4747
Selim Cinek6743c0b2017-01-18 18:24:01 -08004748 /**
4749 * Construct a content view for the display when low - priority
4750 *
4751 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4752 * a new subtext is created consisting of the content of the
4753 * notification.
4754 * @hide
4755 */
4756 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4757 int color = mN.color;
4758 mN.color = COLOR_DEFAULT;
4759 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4760 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4761 CharSequence newSummary = createSummaryText();
4762 if (!TextUtils.isEmpty(newSummary)) {
4763 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4764 }
4765 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004766
Adrian Roos6f6e1592017-05-02 16:22:53 -07004767 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004768 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004769 if (summary != null) {
4770 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4771 } else {
4772 mN.extras.remove(EXTRA_SUB_TEXT);
4773 }
4774 mN.color = color;
4775 return header;
4776 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004777
Selim Cinek6743c0b2017-01-18 18:24:01 -08004778 private CharSequence createSummaryText() {
4779 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4780 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4781 return titleText;
4782 }
4783 SpannableStringBuilder summary = new SpannableStringBuilder();
4784 if (titleText == null) {
4785 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4786 }
4787 BidiFormatter bidi = BidiFormatter.getInstance();
4788 if (titleText != null) {
4789 summary.append(bidi.unicodeWrap(titleText));
4790 }
4791 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4792 if (titleText != null && contentText != null) {
4793 summary.append(bidi.unicodeWrap(mContext.getText(
4794 R.string.notification_header_divider_symbol_with_spaces)));
4795 }
4796 if (contentText != null) {
4797 summary.append(bidi.unicodeWrap(contentText));
4798 }
4799 return summary;
4800 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004801
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004802 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004803 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004804 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004805 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004806 emphazisedMode ? getEmphasizedActionLayoutResource()
4807 : tombstone ? getActionTombstoneLayoutResource()
4808 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004809 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004810 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004811 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004812 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004813 if (action.mRemoteInputs != null) {
4814 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4815 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004816 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004817 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004818 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004819 int bgColor;
4820 if (isColorized()) {
4821 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4822 } else {
4823 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4824 : R.color.notification_action_list_dark);
4825 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004826 button.setDrawableTint(R.id.button_holder, true,
4827 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004828 CharSequence title = action.title;
4829 ColorStateList[] outResultColor = null;
4830 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004831 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004832 } else {
4833 outResultColor = new ColorStateList[1];
4834 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4835 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004836 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004837 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004838 if (outResultColor != null && outResultColor[0] != null) {
4839 // We need to set the text color as well since changing a text to uppercase
4840 // clears its spans.
4841 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004842 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004843 button.setTextColor(R.id.action0,resolveContrastColor());
4844 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004845 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004846 button.setTextViewText(R.id.action0, processTextSpans(
4847 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004848 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004849 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004850 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004851 button.setTextColor(R.id.action0,
4852 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004853 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004854 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004855 return button;
4856 }
4857
Joe Onoratocb109a02011-01-18 17:57:41 -08004858 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004859 * Ensures contrast on color spans against a background color. also returns the color of the
4860 * text if a span was found that spans over the whole text.
4861 *
4862 * @param charSequence the charSequence on which the spans are
4863 * @param background the background color to ensure the contrast against
4864 * @param outResultColor an array in which a color will be returned as the first element if
4865 * there exists a full length color span.
4866 * @return the contrasted charSequence
4867 */
4868 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4869 ColorStateList[] outResultColor) {
4870 if (charSequence instanceof Spanned) {
4871 Spanned ss = (Spanned) charSequence;
4872 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4873 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4874 for (Object span : spans) {
4875 Object resultSpan = span;
4876 int spanStart = ss.getSpanStart(span);
4877 int spanEnd = ss.getSpanEnd(span);
4878 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4879 if (resultSpan instanceof CharacterStyle) {
4880 resultSpan = ((CharacterStyle) span).getUnderlying();
4881 }
4882 if (resultSpan instanceof TextAppearanceSpan) {
4883 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4884 ColorStateList textColor = originalSpan.getTextColor();
4885 if (textColor != null) {
4886 int[] colors = textColor.getColors();
4887 int[] newColors = new int[colors.length];
4888 for (int i = 0; i < newColors.length; i++) {
4889 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004890 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004891 }
4892 textColor = new ColorStateList(textColor.getStates().clone(),
4893 newColors);
4894 resultSpan = new TextAppearanceSpan(
4895 originalSpan.getFamily(),
4896 originalSpan.getTextStyle(),
4897 originalSpan.getTextSize(),
4898 textColor,
4899 originalSpan.getLinkTextColor());
4900 if (fullLength) {
4901 outResultColor[0] = new ColorStateList(
4902 textColor.getStates().clone(), newColors);
4903 }
4904 }
4905 } else if (resultSpan instanceof ForegroundColorSpan) {
4906 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4907 int foregroundColor = originalSpan.getForegroundColor();
4908 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004909 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004910 resultSpan = new ForegroundColorSpan(foregroundColor);
4911 if (fullLength) {
4912 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4913 }
4914 } else {
4915 resultSpan = span;
4916 }
4917
4918 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4919 }
4920 return builder;
4921 }
4922 return charSequence;
4923 }
4924
4925 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004926 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004927 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004928 */
4929 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004930 if (!mIsLegacyInitialized) {
4931 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4932 < Build.VERSION_CODES.LOLLIPOP;
4933 mIsLegacyInitialized = true;
4934 }
4935 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004936 }
4937
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004938 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004939 return processLegacyText(charSequence, false /* ambient */);
4940 }
4941
4942 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4943 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4944 boolean wantLightText = ambient;
4945 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004946 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004947 } else {
4948 return charSequence;
4949 }
4950 }
4951
Dan Sandler26e81cf2014-05-06 10:01:27 -04004952 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004953 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004954 */
Adrian Roos487374f2017-01-11 15:48:14 -08004955 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4956 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004957 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004958 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004959 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004960 contentView.setDrawableTint(R.id.icon, false, color,
4961 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004962
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004963 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004964 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004965 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004966 }
4967
Dan Sandler26e81cf2014-05-06 10:01:27 -04004968 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004969 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004970 * if it's grayscale).
4971 */
4972 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004973 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4974 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004975 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004976 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004977 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4978 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004979 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004980 }
4981
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004982 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004983 if (mN.color != COLOR_DEFAULT) {
4984 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004985 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004986 }
4987
Adrian Roos4ff3b122016-02-01 12:26:13 -08004988 int resolveContrastColor() {
4989 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4990 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004991 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004992
Selim Cinekac5f0272017-05-02 16:05:41 -07004993 int color;
4994 int background = mBackgroundColorHint;
4995 if (mBackgroundColorHint == COLOR_INVALID) {
4996 background = mContext.getColor(
4997 com.android.internal.R.color.notification_material_background_color);
4998 }
4999 if (mN.color == COLOR_DEFAULT) {
5000 ensureColors();
5001 color = mSecondaryTextColor;
5002 } else {
5003 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005004 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005005 }
5006 if (Color.alpha(color) < 255) {
5007 // alpha doesn't go well for color filters, so let's blend it manually
5008 color = NotificationColorUtil.compositeColors(color, background);
5009 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005010 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005011 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005012 }
5013
Adrian Roos487374f2017-01-11 15:48:14 -08005014 int resolveAmbientColor() {
5015 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5016 return mCachedAmbientColor;
5017 }
5018 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5019
5020 mCachedAmbientColorIsFor = mN.color;
5021 return mCachedAmbientColor = contrasted;
5022 }
5023
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005024 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005025 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005026 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005027 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005028 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005029 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005030 mN.actions = new Action[mActions.size()];
5031 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005032 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005033 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005034 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005035 }
Selim Cinek247fa012016-02-18 09:50:48 -08005036 if (mN.bigContentView != null || mN.contentView != null
5037 || mN.headsUpContentView != null) {
5038 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5039 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005040 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005041 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005042
Julia Reynolds3b848122016-02-26 10:45:32 -05005043 /**
5044 * Creates a Builder from an existing notification so further changes can be made.
5045 * @param context The context for your application / activity.
5046 * @param n The notification to create a Builder from.
5047 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005048 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005049 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005050 ApplicationInfo applicationInfo = n.extras.getParcelable(
5051 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005052 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005053 if (applicationInfo != null) {
5054 try {
5055 builderContext = context.createApplicationContext(applicationInfo,
5056 Context.CONTEXT_RESTRICTED);
5057 } catch (NameNotFoundException e) {
5058 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5059 builderContext = context; // try with our context
5060 }
5061 } else {
5062 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005063 }
5064
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005065 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005066 }
5067
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005068 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005069 * @deprecated Use {@link #build()} instead.
5070 */
5071 @Deprecated
5072 public Notification getNotification() {
5073 return build();
5074 }
5075
5076 /**
5077 * Combine all of the options that have been set and return a new {@link Notification}
5078 * object.
5079 */
5080 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005081 // first, add any extras from the calling code
5082 if (mUserExtras != null) {
5083 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005084 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005085
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005086 mN.creationTime = System.currentTimeMillis();
5087
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005088 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005089 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005090
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005091 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005092
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005093 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005094 mStyle.reduceImageSizes(mContext);
5095 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005096 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005097 }
Selim Cinekd0426622017-07-11 13:19:59 +02005098 mN.reduceImageSizes(mContext);
5099
Adrian Roos5081c0d2016-02-26 16:04:19 -08005100 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005101 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005102 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005103 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005104 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5105 mN.contentView.getSequenceNumber());
5106 }
5107 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005108 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005109 if (mN.bigContentView != null) {
5110 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5111 mN.bigContentView.getSequenceNumber());
5112 }
5113 }
5114 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005115 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005116 if (mN.headsUpContentView != null) {
5117 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5118 mN.headsUpContentView.getSequenceNumber());
5119 }
5120 }
5121 }
5122
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005123 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5124 mN.flags |= FLAG_SHOW_LIGHTS;
5125 }
5126
Adrian Roosfb921842017-10-26 14:49:56 +02005127 mN.allPendingIntents = null;
5128
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005129 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005130 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005131
5132 /**
5133 * Apply this Builder to an existing {@link Notification} object.
5134 *
5135 * @hide
5136 */
5137 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005138 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005139 return n;
5140 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005141
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005142 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005143 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005144 * change. Also removes extenders on low ram devices, as
5145 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005146 *
5147 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5148 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005149 * @hide
5150 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005151 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005152 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005153
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005154 // Only strip views for known Styles because we won't know how to
5155 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005156 if (!isLowRam
5157 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005158 && getNotificationStyleClass(templateClass) == null) {
5159 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005160 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005161
5162 // Only strip unmodified BuilderRemoteViews.
5163 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005164 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005165 n.contentView.getSequenceNumber();
5166 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005167 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005168 n.bigContentView.getSequenceNumber();
5169 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005170 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005171 n.headsUpContentView.getSequenceNumber();
5172
5173 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005174 if (!isLowRam
5175 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005176 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005177 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005178
5179 Notification clone = n.clone();
5180 if (stripContentView) {
5181 clone.contentView = null;
5182 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5183 }
5184 if (stripBigContentView) {
5185 clone.bigContentView = null;
5186 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5187 }
5188 if (stripHeadsUpContentView) {
5189 clone.headsUpContentView = null;
5190 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5191 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005192 if (isLowRam) {
5193 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5194 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5195 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5196 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005197 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005198 }
5199
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005200 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005201 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005202 }
5203
5204 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005205 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005206 }
5207
5208 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005209 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005210 }
5211
5212 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005213 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005214 }
5215
5216 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005217 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005218 }
5219
Adrian Roosc1a80b02016-04-05 14:54:55 -07005220 private int getMessagingLayoutResource() {
5221 return R.layout.notification_template_material_messaging;
5222 }
5223
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005224 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005225 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005226 }
5227
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005228 private int getEmphasizedActionLayoutResource() {
5229 return R.layout.notification_material_action_emphasized;
5230 }
5231
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005232 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005233 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005234 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005235
5236 private int getBackgroundColor() {
5237 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005238 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005239 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005240 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5241 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005242 }
5243 }
5244
5245 private boolean isColorized() {
5246 return mN.isColorized();
5247 }
Selim Cinek99104832017-01-25 14:47:33 -08005248
Anthony Chenad4d1582017-04-10 16:07:58 -07005249 private boolean shouldTintActionButtons() {
5250 return mTintActionButtons;
5251 }
5252
Selim Cinek99104832017-01-25 14:47:33 -08005253 private boolean textColorsNeedInversion() {
5254 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5255 return false;
5256 }
5257 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5258 return targetSdkVersion > Build.VERSION_CODES.M
5259 && targetSdkVersion < Build.VERSION_CODES.O;
5260 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005261
5262 /**
5263 * Set a color palette to be used as the background and textColors
5264 *
5265 * @param backgroundColor the color to be used as the background
5266 * @param foregroundColor the color to be used as the foreground
5267 *
5268 * @hide
5269 */
5270 public void setColorPalette(int backgroundColor, int foregroundColor) {
5271 mBackgroundColor = backgroundColor;
5272 mForegroundColor = foregroundColor;
5273 mTextColorsAreForBackground = COLOR_INVALID;
5274 ensureColors();
5275 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005276
5277 /**
5278 * Sets the background color for this notification to be a different one then the default.
5279 * This is mainly used to calculate contrast and won't necessarily be applied to the
5280 * background.
5281 *
5282 * @hide
5283 */
5284 public void setBackgroundColorHint(int backgroundColor) {
5285 mBackgroundColorHint = backgroundColor;
5286 }
Selim Cineka7679b62017-05-10 16:33:25 -07005287
5288
5289 /**
5290 * Forces all styled remoteViews to be built from scratch and not use any cached
5291 * RemoteViews.
5292 * This is needed for legacy apps that are baking in their remoteviews into the
5293 * notification.
5294 *
5295 * @hide
5296 */
5297 public void setRebuildStyledRemoteViews(boolean rebuild) {
5298 mRebuildStyledRemoteViews = rebuild;
5299 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005300 }
5301
5302 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005303 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5304 * remote views.
5305 *
5306 * @hide
5307 */
5308 void reduceImageSizes(Context context) {
5309 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5310 return;
5311 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005312 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005313 if (mLargeIcon != null || largeIcon != null) {
5314 Resources resources = context.getResources();
5315 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005316 int maxWidth = resources.getDimensionPixelSize(isLowRam
5317 ? R.dimen.notification_right_icon_size_low_ram
5318 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005319 int maxHeight = maxWidth;
5320 if (MediaStyle.class.equals(style)
5321 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005322 maxHeight = resources.getDimensionPixelSize(isLowRam
5323 ? R.dimen.notification_media_image_max_height_low_ram
5324 : R.dimen.notification_media_image_max_height);
5325 maxWidth = resources.getDimensionPixelSize(isLowRam
5326 ? R.dimen.notification_media_image_max_width_low_ram
5327 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005328 }
5329 if (mLargeIcon != null) {
5330 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5331 }
5332 if (largeIcon != null) {
5333 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5334 }
5335 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005336 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5337 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5338 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005339 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5340 }
5341
Selim Cineka8cb1262017-08-15 16:53:44 -07005342 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5343 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005344 if (remoteView != null) {
5345 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005346 int maxWidth = resources.getDimensionPixelSize(isLowRam
5347 ? R.dimen.notification_custom_view_max_image_width_low_ram
5348 : R.dimen.notification_custom_view_max_image_width);
5349 int maxHeight = resources.getDimensionPixelSize(isLowRam
5350 ? R.dimen.notification_custom_view_max_image_height_low_ram
5351 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005352 remoteView.reduceImageSizes(maxWidth, maxHeight);
5353 }
5354 }
5355
5356 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005357 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005358 */
Selim Cinek22714f12017-04-13 16:23:53 -07005359 private boolean isForegroundService() {
5360 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005361 }
5362
5363 /**
Selim Cinek99104832017-01-25 14:47:33 -08005364 * @return whether this notification has a media session attached
5365 * @hide
5366 */
5367 public boolean hasMediaSession() {
5368 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5369 }
5370
5371 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005372 * @return the style class of this notification
5373 * @hide
5374 */
5375 public Class<? extends Notification.Style> getNotificationStyle() {
5376 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5377
5378 if (!TextUtils.isEmpty(templateClass)) {
5379 return Notification.getNotificationStyleClass(templateClass);
5380 }
5381 return null;
5382 }
5383
5384 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005385 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005386 *
5387 * @hide
5388 */
5389 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005390 if (isColorizedMedia()) {
5391 return true;
5392 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005393 return extras.getBoolean(EXTRA_COLORIZED)
5394 && (hasColorizedPermission() || isForegroundService());
5395 }
5396
5397 /**
5398 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5399 * permission. The permission is checked when a notification is enqueued.
5400 */
5401 private boolean hasColorizedPermission() {
5402 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005403 }
5404
5405 /**
5406 * @return true if this notification is colorized and it is a media notification
5407 *
5408 * @hide
5409 */
5410 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005411 Class<? extends Style> style = getNotificationStyle();
5412 if (MediaStyle.class.equals(style)) {
5413 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5414 if ((colorized == null || colorized) && hasMediaSession()) {
5415 return true;
5416 }
5417 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5418 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5419 return true;
5420 }
5421 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005422 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005423 }
5424
Selim Cinek0847acd2017-04-24 19:48:29 -07005425
5426 /**
5427 * @return true if this is a media notification
5428 *
5429 * @hide
5430 */
5431 public boolean isMediaNotification() {
5432 Class<? extends Style> style = getNotificationStyle();
5433 if (MediaStyle.class.equals(style)) {
5434 return true;
5435 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5436 return true;
5437 }
5438 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005439 }
5440
Selim Cinek279fa862016-06-14 10:57:25 -07005441 private boolean hasLargeIcon() {
5442 return mLargeIcon != null || largeIcon != null;
5443 }
5444
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005445 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005446 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005447 * @hide
5448 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005449 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005450 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5451 }
5452
5453 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005454 * @return true if the notification will show a chronometer; false otherwise
5455 * @hide
5456 */
5457 public boolean showsChronometer() {
5458 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5459 }
5460
5461 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005462 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005463 */
5464 @SystemApi
5465 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5466 Class<? extends Style>[] classes = new Class[] {
5467 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5468 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5469 MessagingStyle.class };
5470 for (Class<? extends Style> innerClass : classes) {
5471 if (templateClass.equals(innerClass.getName())) {
5472 return innerClass;
5473 }
5474 }
5475 return null;
5476 }
5477
5478 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005479 * An object that can apply a rich notification style to a {@link Notification.Builder}
5480 * object.
5481 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005482 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005483 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005484
5485 /**
5486 * @hide
5487 */
5488 protected CharSequence mSummaryText = null;
5489
5490 /**
5491 * @hide
5492 */
5493 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005494
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005495 protected Builder mBuilder;
5496
Chris Wrend6297db2012-05-03 16:20:13 -04005497 /**
5498 * Overrides ContentTitle in the big form of the template.
5499 * This defaults to the value passed to setContentTitle().
5500 */
5501 protected void internalSetBigContentTitle(CharSequence title) {
5502 mBigContentTitle = title;
5503 }
5504
5505 /**
5506 * Set the first line of text after the detail section in the big form of the template.
5507 */
5508 protected void internalSetSummaryText(CharSequence cs) {
5509 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005510 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005511 }
5512
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005513 public void setBuilder(Builder builder) {
5514 if (mBuilder != builder) {
5515 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005516 if (mBuilder != null) {
5517 mBuilder.setStyle(this);
5518 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005519 }
5520 }
5521
Chris Wrend6297db2012-05-03 16:20:13 -04005522 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005523 if (mBuilder == null) {
5524 throw new IllegalArgumentException("Style requires a valid Builder object");
5525 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005526 }
Chris Wrend6297db2012-05-03 16:20:13 -04005527
5528 protected RemoteViews getStandardView(int layoutId) {
5529 checkBuilder();
5530
Christoph Studer4600f9b2014-07-22 22:44:43 +02005531 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005532 CharSequence oldBuilderContentTitle =
5533 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005534 if (mBigContentTitle != null) {
5535 mBuilder.setContentTitle(mBigContentTitle);
5536 }
5537
Chris Wrend6297db2012-05-03 16:20:13 -04005538 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5539
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005540 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005541
Chris Wrend6297db2012-05-03 16:20:13 -04005542 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5543 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005544 } else {
5545 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005546 }
5547
Chris Wrend6297db2012-05-03 16:20:13 -04005548 return contentView;
5549 }
5550
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005551 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005552 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005553 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005554 *
5555 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005556 * @hide
5557 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005558 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005559 return null;
5560 }
5561
5562 /**
5563 * Construct a Style-specific RemoteViews for the final big notification layout.
5564 * @hide
5565 */
5566 public RemoteViews makeBigContentView() {
5567 return null;
5568 }
5569
5570 /**
5571 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005572 *
5573 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005574 * @hide
5575 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005576 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005577 return null;
5578 }
5579
5580 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005581 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005582 * @hide
5583 */
5584 public void addExtras(Bundle extras) {
5585 if (mSummaryTextSet) {
5586 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5587 }
5588 if (mBigContentTitle != null) {
5589 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5590 }
Chris Wren91ad5632013-06-05 15:05:57 -04005591 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005592 }
5593
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005594 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005595 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005596 * @hide
5597 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005598 protected void restoreFromExtras(Bundle extras) {
5599 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5600 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5601 mSummaryTextSet = true;
5602 }
5603 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5604 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5605 }
5606 }
5607
5608
5609 /**
5610 * @hide
5611 */
5612 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005613 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005614 return wip;
5615 }
5616
Daniel Sandler0ec46202015-06-24 01:27:05 -04005617 /**
5618 * @hide
5619 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005620 public void purgeResources() {}
5621
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005622 /**
5623 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5624 * attached to.
5625 *
5626 * @return the fully constructed Notification.
5627 */
5628 public Notification build() {
5629 checkBuilder();
5630 return mBuilder.build();
5631 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005632
5633 /**
5634 * @hide
5635 * @return true if the style positions the progress bar on the second line; false if the
5636 * style hides the progress bar
5637 */
5638 protected boolean hasProgress() {
5639 return true;
5640 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005641
5642 /**
5643 * @hide
5644 * @return Whether we should put the summary be put into the notification header
5645 */
5646 public boolean hasSummaryInHeader() {
5647 return true;
5648 }
Selim Cinek593610c2016-02-16 18:42:57 -08005649
5650 /**
5651 * @hide
5652 * @return Whether custom content views are displayed inline in the style
5653 */
5654 public boolean displayCustomViewInline() {
5655 return false;
5656 }
Selim Cinekd0426622017-07-11 13:19:59 +02005657
5658 /**
5659 * Reduces the image sizes contained in this style.
5660 *
5661 * @hide
5662 */
5663 public void reduceImageSizes(Context context) {
5664 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005665 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005666
5667 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005668 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005669 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005670 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005671 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005672 * Notification notif = new Notification.Builder(mContext)
5673 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5674 * .setContentText(subject)
5675 * .setSmallIcon(R.drawable.new_post)
5676 * .setLargeIcon(aBitmap)
5677 * .setStyle(new Notification.BigPictureStyle()
5678 * .bigPicture(aBigBitmap))
5679 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005680 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005681 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005682 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005683 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005684 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005685 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005686 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005687 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005688
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005689 public BigPictureStyle() {
5690 }
5691
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005692 /**
5693 * @deprecated use {@code BigPictureStyle()}.
5694 */
5695 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005696 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005697 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005698 }
5699
Chris Wrend6297db2012-05-03 16:20:13 -04005700 /**
5701 * Overrides ContentTitle in the big form of the template.
5702 * This defaults to the value passed to setContentTitle().
5703 */
5704 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005705 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005706 return this;
5707 }
5708
5709 /**
5710 * Set the first line of text after the detail section in the big form of the template.
5711 */
5712 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005713 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005714 return this;
5715 }
5716
Chris Wren0bd664d2012-08-01 13:56:56 -04005717 /**
5718 * Provide the bitmap to be used as the payload for the BigPicture notification.
5719 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005720 public BigPictureStyle bigPicture(Bitmap b) {
5721 mPicture = b;
5722 return this;
5723 }
5724
Chris Wren3745a3d2012-05-22 15:11:52 -04005725 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005726 * Override the large icon when the big notification is shown.
5727 */
5728 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005729 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5730 }
5731
5732 /**
5733 * Override the large icon when the big notification is shown.
5734 */
5735 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005736 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005737 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005738 return this;
5739 }
5740
Riley Andrews0394a0c2015-11-03 23:36:52 -08005741 /** @hide */
5742 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5743
Daniel Sandler0ec46202015-06-24 01:27:05 -04005744 /**
5745 * @hide
5746 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005747 @Override
5748 public void purgeResources() {
5749 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005750 if (mPicture != null &&
5751 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005752 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005753 mPicture = mPicture.createAshmemBitmap();
5754 }
5755 if (mBigLargeIcon != null) {
5756 mBigLargeIcon.convertToAshmem();
5757 }
5758 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005759
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005760 /**
5761 * @hide
5762 */
Selim Cinekd0426622017-07-11 13:19:59 +02005763 @Override
5764 public void reduceImageSizes(Context context) {
5765 super.reduceImageSizes(context);
5766 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005767 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005768 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005769 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5770 ? R.dimen.notification_big_picture_max_height_low_ram
5771 : R.dimen.notification_big_picture_max_height);
5772 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5773 ? R.dimen.notification_big_picture_max_width_low_ram
5774 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005775 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5776 }
5777 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005778 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5779 ? R.dimen.notification_right_icon_size_low_ram
5780 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005781 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5782 }
5783 }
5784
5785 /**
5786 * @hide
5787 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005788 public RemoteViews makeBigContentView() {
5789 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005790 // This covers the following cases:
5791 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005792 // mN.mLargeIcon
5793 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005794 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005795 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005796 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005797 oldLargeIcon = mBuilder.mN.mLargeIcon;
5798 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005799 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5800 // replacement if the other one is null. Because we're restoring these legacy icons
5801 // for old listeners, this is in general non-null.
5802 largeIconLegacy = mBuilder.mN.largeIcon;
5803 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005804 }
5805
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005806 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005807 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005808 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5809 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005810 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005811 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005812 }
Selim Cinek279fa862016-06-14 10:57:25 -07005813 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005814
Christoph Studer5c510ee2014-12-15 16:32:27 +01005815 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005816 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005817 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005818 }
5819
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005820 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005821 return contentView;
5822 }
5823
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005824 /**
5825 * @hide
5826 */
5827 public void addExtras(Bundle extras) {
5828 super.addExtras(extras);
5829
5830 if (mBigLargeIconSet) {
5831 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5832 }
5833 extras.putParcelable(EXTRA_PICTURE, mPicture);
5834 }
5835
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005836 /**
5837 * @hide
5838 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005839 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005840 protected void restoreFromExtras(Bundle extras) {
5841 super.restoreFromExtras(extras);
5842
5843 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005844 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005845 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005846 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005847 mPicture = extras.getParcelable(EXTRA_PICTURE);
5848 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005849
5850 /**
5851 * @hide
5852 */
5853 @Override
5854 public boolean hasSummaryInHeader() {
5855 return false;
5856 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005857 }
5858
5859 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005860 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005861 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005862 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005863 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005864 * Notification notif = new Notification.Builder(mContext)
5865 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5866 * .setContentText(subject)
5867 * .setSmallIcon(R.drawable.new_mail)
5868 * .setLargeIcon(aBitmap)
5869 * .setStyle(new Notification.BigTextStyle()
5870 * .bigText(aVeryLongString))
5871 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005872 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005873 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005874 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005875 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005876 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005877
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005878 private CharSequence mBigText;
5879
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005880 public BigTextStyle() {
5881 }
5882
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005883 /**
5884 * @deprecated use {@code BigTextStyle()}.
5885 */
5886 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005887 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005888 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005889 }
5890
Chris Wrend6297db2012-05-03 16:20:13 -04005891 /**
5892 * Overrides ContentTitle in the big form of the template.
5893 * This defaults to the value passed to setContentTitle().
5894 */
5895 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005896 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005897 return this;
5898 }
5899
5900 /**
5901 * Set the first line of text after the detail section in the big form of the template.
5902 */
5903 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005904 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005905 return this;
5906 }
5907
Chris Wren0bd664d2012-08-01 13:56:56 -04005908 /**
5909 * Provide the longer text to be displayed in the big form of the
5910 * template in place of the content text.
5911 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005912 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005913 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005914 return this;
5915 }
5916
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005917 /**
5918 * @hide
5919 */
5920 public void addExtras(Bundle extras) {
5921 super.addExtras(extras);
5922
Christoph Studer4600f9b2014-07-22 22:44:43 +02005923 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5924 }
5925
5926 /**
5927 * @hide
5928 */
5929 @Override
5930 protected void restoreFromExtras(Bundle extras) {
5931 super.restoreFromExtras(extras);
5932
5933 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005934 }
5935
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005936 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005937 * @param increasedHeight true if this layout be created with an increased height.
5938 *
5939 * @hide
5940 */
5941 @Override
5942 public RemoteViews makeContentView(boolean increasedHeight) {
5943 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005944 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005945 mBuilder.mActions = new ArrayList<>();
5946 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005947 mBuilder.mActions = mBuilder.mOriginalActions;
5948 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005949 return remoteViews;
5950 }
5951 return super.makeContentView(increasedHeight);
5952 }
5953
5954 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005955 * @hide
5956 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005957 @Override
5958 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5959 if (increasedHeight && mBuilder.mActions.size() > 0) {
5960 return makeBigContentView();
5961 }
5962 return super.makeHeadsUpContentView(increasedHeight);
5963 }
5964
5965 /**
5966 * @hide
5967 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005968 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005969
5970 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005971 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005972 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005973
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005974 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005975
Selim Cinek75998782016-04-26 10:39:17 -07005976 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005977
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005978 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005979 if (TextUtils.isEmpty(bigTextText)) {
5980 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5981 // experience
5982 bigTextText = mBuilder.processLegacyText(text);
5983 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005984 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005985
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005986 return contentView;
5987 }
5988
Adrian Roosb1f427c2016-05-26 12:27:15 -07005989 static void applyBigTextContentView(Builder builder,
5990 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005991 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005992 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005993 contentView.setViewVisibility(R.id.big_text,
5994 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005995 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005996 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005997 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005998
5999 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006000 * Helper class for generating large-format notifications that include multiple back-and-forth
6001 * messages of varying types between any number of people.
6002 *
6003 * <br>
6004 * If the platform does not provide large-format notifications, this method has no effect. The
6005 * user will always see the normal notification view.
6006 * <br>
6007 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6008 * so:
6009 * <pre class="prettyprint">
6010 *
6011 * Notification noti = new Notification.Builder()
6012 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6013 * .setContentText(subject)
6014 * .setSmallIcon(R.drawable.new_message)
6015 * .setLargeIcon(aBitmap)
6016 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6017 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6018 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6019 * .build();
6020 * </pre>
6021 */
6022 public static class MessagingStyle extends Style {
6023
6024 /**
6025 * The maximum number of messages that will be retained in the Notification itself (the
6026 * number displayed is up to the platform).
6027 */
6028 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6029
Selim Cinekcb8b9852017-12-15 18:01:52 -08006030 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006031 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006032 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006033 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006034 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006035
6036 MessagingStyle() {
6037 }
6038
6039 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006040 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6041 * user before the posting app reposts the notification with those messages after they've
6042 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006043 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006044 *
6045 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006046 */
Alex Hillsfd590442016-10-07 09:52:44 -04006047 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006048 this(new Person().setName(userDisplayName));
6049 }
6050
6051 /**
6052 * @param user Required - The person displayed for any messages that are sent by the
6053 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6054 * who don't have a Person associated with it will be displayed as if they were sent
6055 * by this user. The user also needs to have a valid name associated with it.
6056 */
6057 public MessagingStyle(@NonNull Person user) {
6058 mUser = user;
6059 if (user == null || user.getName() == null) {
6060 throw new RuntimeException("user must be valid and have a name");
6061 }
6062 }
6063
6064 /**
6065 * @return the user to be displayed for any replies sent by the user
6066 */
6067 public Person getUser() {
6068 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006069 }
6070
6071 /**
6072 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006073 *
6074 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006075 */
6076 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006077 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006078 }
6079
6080 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006081 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6082 *
6083 * @param conversationTitle A name for the conversation, or {@code null}
Alex Hillsfc737de2016-03-23 17:33:02 -04006084 * @return this object for method chaining.
6085 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006086 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006087 mConversationTitle = conversationTitle;
6088 return this;
6089 }
6090
6091 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006092 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006093 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006094 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006095 public CharSequence getConversationTitle() {
6096 return mConversationTitle;
6097 }
6098
6099 /**
6100 * Adds a message for display by this notification. Convenience call for a simple
6101 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6102 * @param text A {@link CharSequence} to be displayed as the message content
6103 * @param timestamp Time at which the message arrived
6104 * @param sender A {@link CharSequence} to be used for displaying the name of the
6105 * sender. Should be <code>null</code> for messages by the current user, in which case
6106 * the platform will insert {@link #getUserDisplayName()}.
6107 * Should be unique amongst all individuals in the conversation, and should be
6108 * consistent during re-posts of the notification.
6109 *
6110 * @see Message#Message(CharSequence, long, CharSequence)
6111 *
6112 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006113 *
6114 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006115 */
6116 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006117 return addMessage(text, timestamp,
6118 sender == null ? null : new Person().setName(sender));
6119 }
6120
6121 /**
6122 * Adds a message for display by this notification. Convenience call for a simple
6123 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6124 * @param text A {@link CharSequence} to be displayed as the message content
6125 * @param timestamp Time at which the message arrived
6126 * @param sender The {@link Person} who sent the message.
6127 * Should be <code>null</code> for messages by the current user, in which case
6128 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6129 *
6130 * @see Message#Message(CharSequence, long, CharSequence)
6131 *
6132 * @return this object for method chaining
6133 */
6134 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006135 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006136 }
6137
6138 /**
6139 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006140 *
6141 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6142 * the newest last.
6143 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006144 * @param message The {@link Message} to be displayed
6145 * @return this object for method chaining
6146 */
6147 public MessagingStyle addMessage(Message message) {
6148 mMessages.add(message);
6149 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6150 mMessages.remove(0);
6151 }
6152 return this;
6153 }
6154
6155 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006156 * Adds a {@link Message} for historic context in this notification.
6157 *
6158 * <p>Messages should be added as historic if they are not the main subject of the
6159 * notification but may give context to a conversation. The system may choose to present
6160 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6161 *
6162 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6163 * the newest last.
6164 *
6165 * @param message The historic {@link Message} to be added
6166 * @return this object for method chaining
6167 */
6168 public MessagingStyle addHistoricMessage(Message message) {
6169 mHistoricMessages.add(message);
6170 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6171 mHistoricMessages.remove(0);
6172 }
6173 return this;
6174 }
6175
6176 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006177 * Gets the list of {@code Message} objects that represent the notification
6178 */
6179 public List<Message> getMessages() {
6180 return mMessages;
6181 }
6182
6183 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006184 * Gets the list of historic {@code Message}s in the notification.
6185 */
6186 public List<Message> getHistoricMessages() {
6187 return mHistoricMessages;
6188 }
6189
6190 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006191 * Sets whether this conversation notification represents a group.
6192 * @param isGroupConversation {@code true} if the conversation represents a group,
6193 * {@code false} otherwise.
6194 * @return this object for method chaining
6195 */
6196 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6197 mIsGroupConversation = isGroupConversation;
6198 return this;
6199 }
6200
6201 /**
6202 * Returns {@code true} if this notification represents a group conversation.
6203 */
6204 public boolean isGroupConversation() {
6205 return mIsGroupConversation;
6206 }
6207
6208 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006209 * @hide
6210 */
6211 @Override
6212 public void addExtras(Bundle extras) {
6213 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006214 if (mUser != null) {
6215 // For legacy usages
6216 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6217 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006218 }
6219 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006220 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006221 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006222 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6223 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006224 }
Adrian Roos437cd562017-01-18 15:47:03 -08006225 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6226 Message.getBundleArrayForMessages(mHistoricMessages));
6227 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006228
6229 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006230 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006231 }
6232
6233 private void fixTitleAndTextExtras(Bundle extras) {
6234 Message m = findLatestIncomingMessage();
6235 CharSequence text = (m == null) ? null : m.mText;
6236 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006237 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6238 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006239 CharSequence title;
6240 if (!TextUtils.isEmpty(mConversationTitle)) {
6241 if (!TextUtils.isEmpty(sender)) {
6242 BidiFormatter bidi = BidiFormatter.getInstance();
6243 title = mBuilder.mContext.getString(
6244 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006245 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006246 } else {
6247 title = mConversationTitle;
6248 }
6249 } else {
6250 title = sender;
6251 }
6252
6253 if (title != null) {
6254 extras.putCharSequence(EXTRA_TITLE, title);
6255 }
6256 if (text != null) {
6257 extras.putCharSequence(EXTRA_TEXT, text);
6258 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006259 }
6260
6261 /**
6262 * @hide
6263 */
6264 @Override
6265 protected void restoreFromExtras(Bundle extras) {
6266 super.restoreFromExtras(extras);
6267
Selim Cinekcb8b9852017-12-15 18:01:52 -08006268 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6269 if (mUser == null) {
6270 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6271 mUser = new Person().setName(displayName);
6272 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006273 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006274 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006275 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006276 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006277 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006278 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006279 }
6280
6281 /**
6282 * @hide
6283 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006284 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006285 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006286 mBuilder.mOriginalActions = mBuilder.mActions;
6287 mBuilder.mActions = new ArrayList<>();
Selim Cinekafeed292017-12-12 17:32:44 -08006288 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006289 mBuilder.mActions = mBuilder.mOriginalActions;
6290 mBuilder.mOriginalActions = null;
6291 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006292 }
6293
6294 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006295 return findLatestIncomingMessage(mMessages);
6296 }
6297
6298 /**
6299 * @hide
6300 */
6301 @Nullable
6302 public static Message findLatestIncomingMessage(
6303 List<Message> messages) {
6304 for (int i = messages.size() - 1; i >= 0; i--) {
6305 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006306 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006307 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006308 return m;
6309 }
6310 }
Selim Cinek88188f22017-09-19 16:46:56 -07006311 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006312 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006313 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006314 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006315 return null;
6316 }
6317
6318 /**
6319 * @hide
6320 */
6321 @Override
6322 public RemoteViews makeBigContentView() {
Selim Cinekafeed292017-12-12 17:32:44 -08006323 return makeBigContentView(false /* showRightIcon */);
6324 }
6325
6326 @NonNull
6327 private RemoteViews makeBigContentView(boolean showRightIcon) {
Selim Cinek88188f22017-09-19 16:46:56 -07006328 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006329 ? super.mBigContentTitle
6330 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006331 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinekafeed292017-12-12 17:32:44 -08006332 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006333 isOneToOne = true;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006334 }
Adrian Roos48d746a2016-04-12 14:57:28 -07006335 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006336 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006337 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinekafeed292017-12-12 17:32:44 -08006338 .hideLargeIcon(!showRightIcon || isOneToOne)
6339 .headerTextSecondary(conversationTitle)
6340 .alwaysShowReply(showRightIcon));
Selim Cinek88188f22017-09-19 16:46:56 -07006341 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006342 // also update the end margin if there is an image
6343 int endMargin = R.dimen.notification_content_margin_end;
6344 if (mBuilder.mN.hasLargeIcon() && showRightIcon) {
6345 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6346 }
6347 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006348 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6349 mBuilder.resolveContrastColor());
6350 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6351 mBuilder.mN.mLargeIcon);
6352 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6353 isOneToOne);
6354 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6355 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006356 return contentView;
6357 }
6358
Selim Cinekf7409db2017-10-24 16:17:14 -07006359 private boolean hasOnlyWhiteSpaceSenders() {
6360 for (int i = 0; i < mMessages.size(); i++) {
6361 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006362 Person sender = m.getSenderPerson();
6363 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006364 return false;
6365 }
6366 }
6367 return true;
6368 }
6369
6370 private boolean isWhiteSpace(CharSequence sender) {
6371 if (TextUtils.isEmpty(sender)) {
6372 return true;
6373 }
6374 if (sender.toString().matches("^\\s*$")) {
6375 return true;
6376 }
6377 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6378 // For the presentation that we had.
6379 for (int i = 0; i < sender.length(); i++) {
6380 char c = sender.charAt(i);
6381 if (c != '\u200B') {
6382 return false;
6383 }
6384 }
6385 return true;
6386 }
6387
Selim Cinek88188f22017-09-19 16:46:56 -07006388 private CharSequence createConversationTitleFromMessages() {
6389 ArraySet<CharSequence> names = new ArraySet<>();
6390 for (int i = 0; i < mMessages.size(); i++) {
6391 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006392 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006393 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006394 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006395 }
6396 }
6397 SpannableStringBuilder title = new SpannableStringBuilder();
6398 int size = names.size();
6399 for (int i = 0; i < size; i++) {
6400 CharSequence name = names.valueAt(i);
6401 if (!TextUtils.isEmpty(title)) {
6402 title.append(", ");
6403 }
6404 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6405 }
6406 return title;
6407 }
6408
Adrian Roosdedd1df2016-04-26 16:38:47 -07006409 /**
6410 * @hide
6411 */
6412 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006413 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekafeed292017-12-12 17:32:44 -08006414 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006415 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6416 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006417 }
6418
Adrian Roosc1a80b02016-04-05 14:54:55 -07006419 private static TextAppearanceSpan makeFontColorSpan(int color) {
6420 return new TextAppearanceSpan(null, 0, 0,
6421 ColorStateList.valueOf(color), null);
6422 }
6423
Alex Hillsd9b04d92016-04-11 16:38:16 -04006424 public static final class Message {
6425
6426 static final String KEY_TEXT = "text";
6427 static final String KEY_TIMESTAMP = "time";
6428 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006429 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006430 static final String KEY_DATA_MIME_TYPE = "type";
6431 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006432 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006433
6434 private final CharSequence mText;
6435 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006436 @Nullable
6437 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006438
Shane Brennan5a871862017-03-11 13:14:17 -08006439 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006440 private String mDataMimeType;
6441 private Uri mDataUri;
6442
6443 /**
6444 * Constructor
6445 * @param text A {@link CharSequence} to be displayed as the message content
6446 * @param timestamp Time at which the message arrived
6447 * @param sender A {@link CharSequence} to be used for displaying the name of the
6448 * sender. Should be <code>null</code> for messages by the current user, in which case
6449 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6450 * Should be unique amongst all individuals in the conversation, and should be
6451 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006452 *
6453 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006454 */
6455 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006456 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6457 }
6458
6459 /**
6460 * Constructor
6461 * @param text A {@link CharSequence} to be displayed as the message content
6462 * @param timestamp Time at which the message arrived
6463 * @param sender The {@link Person} who sent the message.
6464 * Should be <code>null</code> for messages by the current user, in which case
6465 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6466 * <p>
6467 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6468 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6469 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6470 * in order to differentiate between the different users.
6471 * </p>
6472 */
6473 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006474 mText = text;
6475 mTimestamp = timestamp;
6476 mSender = sender;
6477 }
6478
6479 /**
6480 * Sets a binary blob of data and an associated MIME type for a message. In the case
6481 * where the platform doesn't support the MIME type, the original text provided in the
6482 * constructor will be used.
6483 * @param dataMimeType The MIME type of the content. See
6484 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6485 * types on Android and Android Wear.
6486 * @param dataUri The uri containing the content whose type is given by the MIME type.
6487 * <p class="note">
6488 * <ol>
6489 * <li>Notification Listeners including the System UI need permission to access the
6490 * data the Uri points to. The recommended ways to do this are:</li>
6491 * <li>Store the data in your own ContentProvider, making sure that other apps have
6492 * the correct permission to access your provider. The preferred mechanism for
6493 * providing access is to use per-URI permissions which are temporary and only
6494 * grant access to the receiving application. An easy way to create a
6495 * ContentProvider like this is to use the FileProvider helper class.</li>
6496 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6497 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6498 * also store non-media types (see MediaStore.Files for more info). Files can be
6499 * inserted into the MediaStore using scanFile() after which a content:// style
6500 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6501 * Note that once added to the system MediaStore the content is accessible to any
6502 * app on the device.</li>
6503 * </ol>
6504 * @return this object for method chaining
6505 */
6506 public Message setData(String dataMimeType, Uri dataUri) {
6507 mDataMimeType = dataMimeType;
6508 mDataUri = dataUri;
6509 return this;
6510 }
6511
Alex Hillsfc737de2016-03-23 17:33:02 -04006512 /**
6513 * Get the text to be used for this message, or the fallback text if a type and content
6514 * Uri have been set
6515 */
6516 public CharSequence getText() {
6517 return mText;
6518 }
6519
6520 /**
6521 * Get the time at which this message arrived
6522 */
6523 public long getTimestamp() {
6524 return mTimestamp;
6525 }
6526
6527 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006528 * Get the extras Bundle for this message.
6529 */
6530 public Bundle getExtras() {
6531 return mExtras;
6532 }
6533
6534 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006535 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006536 *
6537 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006538 */
6539 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006540 return mSender == null ? null : mSender.getName();
6541 }
6542
6543 /**
6544 * Get the sender associated with this message.
6545 */
6546 @Nullable
6547 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006548 return mSender;
6549 }
6550
6551 /**
6552 * Get the MIME type of the data pointed to by the Uri
6553 */
6554 public String getDataMimeType() {
6555 return mDataMimeType;
6556 }
6557
6558 /**
6559 * Get the the Uri pointing to the content of the message. Can be null, in which case
6560 * {@see #getText()} is used.
6561 */
6562 public Uri getDataUri() {
6563 return mDataUri;
6564 }
6565
Alex Hillsd9b04d92016-04-11 16:38:16 -04006566 private Bundle toBundle() {
6567 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006568 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006569 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006570 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006571 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006572 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006573 // Legacy listeners need this
6574 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6575 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006576 }
6577 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006578 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006579 }
6580 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006581 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006582 }
Shane Brennan5a871862017-03-11 13:14:17 -08006583 if (mExtras != null) {
6584 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6585 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006586 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006587 }
6588
Alex Hillsd9b04d92016-04-11 16:38:16 -04006589 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6590 Bundle[] bundles = new Bundle[messages.size()];
6591 final int N = messages.size();
6592 for (int i = 0; i < N; i++) {
6593 bundles[i] = messages.get(i).toBundle();
6594 }
6595 return bundles;
6596 }
6597
Selim Cinek88188f22017-09-19 16:46:56 -07006598 /**
6599 * @return A list of messages read from the bundles.
6600 *
6601 * @hide
6602 */
6603 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6604 if (bundles == null) {
6605 return new ArrayList<>();
6606 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006607 List<Message> messages = new ArrayList<>(bundles.length);
6608 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006609 if (bundles[i] instanceof Bundle) {
6610 Message message = getMessageFromBundle((Bundle)bundles[i]);
6611 if (message != null) {
6612 messages.add(message);
6613 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006614 }
6615 }
6616 return messages;
6617 }
6618
6619 static Message getMessageFromBundle(Bundle bundle) {
6620 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006621 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006622 return null;
6623 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006624
6625 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6626 if (senderPerson == null) {
6627 // Legacy apps that use compat don't actually provide the sender objects
6628 // We need to fix the compat version to provide people / use
6629 // the native api instead
6630 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6631 if (senderName != null) {
6632 senderPerson = new Person().setName(senderName);
6633 }
6634 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006635 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006636 bundle.getLong(KEY_TIMESTAMP),
6637 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006638 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6639 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006640 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6641 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006642 }
Shane Brennan5a871862017-03-11 13:14:17 -08006643 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6644 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6645 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006646 return message;
6647 }
6648 } catch (ClassCastException e) {
6649 return null;
6650 }
6651 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006652 }
6653 }
6654
6655 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006656 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006657 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006658 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006659 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006660 * Notification notif = new Notification.Builder(mContext)
6661 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6662 * .setContentText(subject)
6663 * .setSmallIcon(R.drawable.new_mail)
6664 * .setLargeIcon(aBitmap)
6665 * .setStyle(new Notification.InboxStyle()
6666 * .addLine(str1)
6667 * .addLine(str2)
6668 * .setContentTitle(&quot;&quot;)
6669 * .setSummaryText(&quot;+3 more&quot;))
6670 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006671 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006672 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006673 * @see Notification#bigContentView
6674 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006675 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006676 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6677
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006678 public InboxStyle() {
6679 }
6680
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006681 /**
6682 * @deprecated use {@code InboxStyle()}.
6683 */
6684 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006685 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006686 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006687 }
6688
Chris Wrend6297db2012-05-03 16:20:13 -04006689 /**
6690 * Overrides ContentTitle in the big form of the template.
6691 * This defaults to the value passed to setContentTitle().
6692 */
6693 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006694 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006695 return this;
6696 }
6697
6698 /**
6699 * Set the first line of text after the detail section in the big form of the template.
6700 */
6701 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006702 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006703 return this;
6704 }
6705
Chris Wren0bd664d2012-08-01 13:56:56 -04006706 /**
6707 * Append a line to the digest section of the Inbox notification.
6708 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006709 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006710 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006711 return this;
6712 }
6713
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006714 /**
6715 * @hide
6716 */
6717 public void addExtras(Bundle extras) {
6718 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006719
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006720 CharSequence[] a = new CharSequence[mTexts.size()];
6721 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6722 }
6723
Christoph Studer4600f9b2014-07-22 22:44:43 +02006724 /**
6725 * @hide
6726 */
6727 @Override
6728 protected void restoreFromExtras(Bundle extras) {
6729 super.restoreFromExtras(extras);
6730
6731 mTexts.clear();
6732 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6733 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6734 }
6735 }
6736
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006737 /**
6738 * @hide
6739 */
6740 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006741 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006742 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006743 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6744 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006745
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006746 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006747
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006748 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006749
Chris Wrend6297db2012-05-03 16:20:13 -04006750 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 -04006751 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006752
Chris Wren4ed80d52012-05-17 09:30:03 -04006753 // Make sure all rows are gone in case we reuse a view.
6754 for (int rowId : rowIds) {
6755 contentView.setViewVisibility(rowId, View.GONE);
6756 }
6757
Daniel Sandler879c5e02012-04-17 16:46:51 -04006758 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006759 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6760 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006761 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006762 int onlyViewId = 0;
6763 int maxRows = rowIds.length;
6764 if (mBuilder.mActions.size() > 0) {
6765 maxRows--;
6766 }
6767 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006768 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006769 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006770 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006771 contentView.setTextViewText(rowIds[i],
6772 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006773 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006774 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006775 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006776 if (first) {
6777 onlyViewId = rowIds[i];
6778 } else {
6779 onlyViewId = 0;
6780 }
Selim Cinek247fa012016-02-18 09:50:48 -08006781 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006782 }
6783 i++;
6784 }
Selim Cinek07c80172016-04-21 16:40:47 -07006785 if (onlyViewId != 0) {
6786 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6787 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6788 R.dimen.notification_text_margin_top);
6789 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6790 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006791
Daniel Sandler879c5e02012-04-17 16:46:51 -04006792 return contentView;
6793 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006794
Selim Cinek247fa012016-02-18 09:50:48 -08006795 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006796 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006797 if (first) {
6798 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6799 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6800 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006801 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006802 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006803 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006804 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006805 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006806 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006807 }
Dan Sandler842dd772014-05-15 09:36:47 -04006808
6809 /**
6810 * Notification style for media playback notifications.
6811 *
6812 * In the expanded form, {@link Notification#bigContentView}, up to 5
6813 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006814 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006815 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6816 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6817 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006818 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006819 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6820 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006821 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006822 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006823 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006824 * Notifications created with MediaStyle will have their category set to
6825 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6826 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006827 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006828 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6829 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006830 * the System UI can identify this as a notification representing an active media session
6831 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6832 *
Selim Cinek99104832017-01-25 14:47:33 -08006833 * <p>
6834 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6835 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6836 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6837 * <p>
6838 *
Dan Sandler842dd772014-05-15 09:36:47 -04006839 * To use this style with your Notification, feed it to
6840 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6841 * <pre class="prettyprint">
6842 * Notification noti = new Notification.Builder()
6843 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006844 * .setContentTitle(&quot;Track title&quot;)
6845 * .setContentText(&quot;Artist - Album&quot;)
6846 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006847 * .setStyle(<b>new Notification.MediaStyle()</b>
6848 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006849 * .build();
6850 * </pre>
6851 *
6852 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006853 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006854 */
6855 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006856 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006857 static final int MAX_MEDIA_BUTTONS = 5;
6858
6859 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006860 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006861
6862 public MediaStyle() {
6863 }
6864
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006865 /**
6866 * @deprecated use {@code MediaStyle()}.
6867 */
6868 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006869 public MediaStyle(Builder builder) {
6870 setBuilder(builder);
6871 }
6872
6873 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006874 * 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 -04006875 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006876 *
6877 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006878 */
6879 public MediaStyle setShowActionsInCompactView(int...actions) {
6880 mActionsToShowInCompact = actions;
6881 return this;
6882 }
6883
6884 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006885 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6886 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006887 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006888 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006889 mToken = token;
6890 return this;
6891 }
6892
Christoph Studer4600f9b2014-07-22 22:44:43 +02006893 /**
6894 * @hide
6895 */
Dan Sandler842dd772014-05-15 09:36:47 -04006896 @Override
6897 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006898 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006899 if (wip.category == null) {
6900 wip.category = Notification.CATEGORY_TRANSPORT;
6901 }
Dan Sandler842dd772014-05-15 09:36:47 -04006902 return wip;
6903 }
6904
Christoph Studer4600f9b2014-07-22 22:44:43 +02006905 /**
6906 * @hide
6907 */
6908 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006909 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006910 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006911 }
6912
6913 /**
6914 * @hide
6915 */
6916 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006917 public RemoteViews makeBigContentView() {
6918 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006919 }
6920
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006921 /**
6922 * @hide
6923 */
6924 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006925 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006926 RemoteViews expanded = makeMediaBigContentView();
6927 return expanded != null ? expanded : makeMediaContentView();
6928 }
6929
Dan Sandler842dd772014-05-15 09:36:47 -04006930 /** @hide */
6931 @Override
6932 public void addExtras(Bundle extras) {
6933 super.addExtras(extras);
6934
6935 if (mToken != null) {
6936 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6937 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006938 if (mActionsToShowInCompact != null) {
6939 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6940 }
Dan Sandler842dd772014-05-15 09:36:47 -04006941 }
6942
Christoph Studer4600f9b2014-07-22 22:44:43 +02006943 /**
6944 * @hide
6945 */
6946 @Override
6947 protected void restoreFromExtras(Bundle extras) {
6948 super.restoreFromExtras(extras);
6949
6950 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6951 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6952 }
6953 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6954 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6955 }
6956 }
6957
Selim Cinek5bf069a2015-11-10 19:14:27 -05006958 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006959 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006960 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006961 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006962 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006963
6964 // If the action buttons should not be tinted, then just use the default
6965 // notification color. Otherwise, just use the passed-in color.
6966 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6967 ? color
6968 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6969 Notification.COLOR_DEFAULT);
6970
Sunny Goyal5b153922017-09-21 21:00:36 -07006971 button.setDrawableTint(R.id.action0, false, tintColor,
6972 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006973 if (!tombstone) {
6974 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6975 }
6976 button.setContentDescription(R.id.action0, action.title);
6977 return button;
6978 }
6979
6980 private RemoteViews makeMediaContentView() {
6981 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006982 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006983
6984 final int numActions = mBuilder.mActions.size();
6985 final int N = mActionsToShowInCompact == null
6986 ? 0
6987 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6988 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006989 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006990 for (int i = 0; i < N; i++) {
6991 if (i >= numActions) {
6992 throw new IllegalArgumentException(String.format(
6993 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6994 i, numActions - 1));
6995 }
6996
6997 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006998 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006999 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007000 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007001 }
7002 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007003 handleImage(view);
7004 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007005 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007006 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007007 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007008 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007009 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007010 return view;
7011 }
7012
Selim Cinek99104832017-01-25 14:47:33 -08007013 private int getPrimaryHighlightColor() {
7014 return mBuilder.getPrimaryHighlightColor();
7015 }
7016
Dan Sandler842dd772014-05-15 09:36:47 -04007017 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007018 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007019 // Dont add an expanded view if there is no more content to be revealed
7020 int actionsInCompact = mActionsToShowInCompact == null
7021 ? 0
7022 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007023 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007024 return null;
7025 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007026 RemoteViews big = mBuilder.applyStandardTemplate(
7027 R.layout.notification_template_material_big_media,
7028 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007029
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007030 if (actionCount > 0) {
7031 big.removeAllViews(com.android.internal.R.id.media_actions);
7032 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007033 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007034 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007035 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007036 }
7037 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007038 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007039 return big;
7040 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007041
Selim Cinek5bf069a2015-11-10 19:14:27 -05007042 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007043 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007044 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7045 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007046 }
7047 }
7048
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007049 /**
7050 * @hide
7051 */
7052 @Override
7053 protected boolean hasProgress() {
7054 return false;
7055 }
Dan Sandler842dd772014-05-15 09:36:47 -04007056 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007057
Selim Cinek593610c2016-02-16 18:42:57 -08007058 /**
7059 * Notification style for custom views that are decorated by the system
7060 *
7061 * <p>Instead of providing a notification that is completely custom, a developer can set this
7062 * style and still obtain system decorations like the notification header with the expand
7063 * affordance and actions.
7064 *
7065 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7066 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7067 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7068 * corresponding custom views to display.
7069 *
7070 * To use this style with your Notification, feed it to
7071 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7072 * <pre class="prettyprint">
7073 * Notification noti = new Notification.Builder()
7074 * .setSmallIcon(R.drawable.ic_stat_player)
7075 * .setLargeIcon(albumArtBitmap))
7076 * .setCustomContentView(contentView);
7077 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7078 * .build();
7079 * </pre>
7080 */
7081 public static class DecoratedCustomViewStyle extends Style {
7082
7083 public DecoratedCustomViewStyle() {
7084 }
7085
Selim Cinek593610c2016-02-16 18:42:57 -08007086 /**
7087 * @hide
7088 */
7089 public boolean displayCustomViewInline() {
7090 return true;
7091 }
7092
7093 /**
7094 * @hide
7095 */
7096 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007097 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007098 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7099 }
7100
7101 /**
7102 * @hide
7103 */
7104 @Override
7105 public RemoteViews makeBigContentView() {
7106 return makeDecoratedBigContentView();
7107 }
7108
7109 /**
7110 * @hide
7111 */
7112 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007113 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007114 return makeDecoratedHeadsUpContentView();
7115 }
7116
Selim Cinek593610c2016-02-16 18:42:57 -08007117 private RemoteViews makeDecoratedHeadsUpContentView() {
7118 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7119 ? mBuilder.mN.contentView
7120 : mBuilder.mN.headsUpContentView;
7121 if (mBuilder.mActions.size() == 0) {
7122 return makeStandardTemplateWithCustomContent(headsUpContentView);
7123 }
7124 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7125 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007126 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007127 return remoteViews;
7128 }
7129
Selim Cinek593610c2016-02-16 18:42:57 -08007130 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7131 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7132 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007133 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007134 return remoteViews;
7135 }
7136
Selim Cinek593610c2016-02-16 18:42:57 -08007137 private RemoteViews makeDecoratedBigContentView() {
7138 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7139 ? mBuilder.mN.contentView
7140 : mBuilder.mN.bigContentView;
7141 if (mBuilder.mActions.size() == 0) {
7142 return makeStandardTemplateWithCustomContent(bigContentView);
7143 }
7144 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7145 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007146 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007147 return remoteViews;
7148 }
Selim Cinek247fa012016-02-18 09:50:48 -08007149
7150 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7151 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007152 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007153 // Need to clone customContent before adding, because otherwise it can no longer be
7154 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007155 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007156 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7157 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007158 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007159 }
Selim Cinek247fa012016-02-18 09:50:48 -08007160 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007161 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007162 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007163 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007164 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007165 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007166 }
Selim Cinek593610c2016-02-16 18:42:57 -08007167 }
7168
Selim Cinek03eb3b72016-02-18 10:39:45 -08007169 /**
7170 * Notification style for media custom views that are decorated by the system
7171 *
7172 * <p>Instead of providing a media notification that is completely custom, a developer can set
7173 * this style and still obtain system decorations like the notification header with the expand
7174 * affordance and actions.
7175 *
7176 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7177 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7178 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7179 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007180 * <p>
7181 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7182 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7183 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007184 * To use this style with your Notification, feed it to
7185 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7186 * <pre class="prettyprint">
7187 * Notification noti = new Notification.Builder()
7188 * .setSmallIcon(R.drawable.ic_stat_player)
7189 * .setLargeIcon(albumArtBitmap))
7190 * .setCustomContentView(contentView);
7191 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7192 * .setMediaSession(mySession))
7193 * .build();
7194 * </pre>
7195 *
7196 * @see android.app.Notification.DecoratedCustomViewStyle
7197 * @see android.app.Notification.MediaStyle
7198 */
7199 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7200
7201 public DecoratedMediaCustomViewStyle() {
7202 }
7203
Selim Cinek03eb3b72016-02-18 10:39:45 -08007204 /**
7205 * @hide
7206 */
7207 public boolean displayCustomViewInline() {
7208 return true;
7209 }
7210
7211 /**
7212 * @hide
7213 */
7214 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007215 public RemoteViews makeContentView(boolean increasedHeight) {
7216 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007217 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7218 mBuilder.mN.contentView);
7219 }
7220
7221 /**
7222 * @hide
7223 */
7224 @Override
7225 public RemoteViews makeBigContentView() {
7226 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7227 ? mBuilder.mN.bigContentView
7228 : mBuilder.mN.contentView;
7229 return makeBigContentViewWithCustomContent(customRemoteView);
7230 }
7231
7232 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7233 RemoteViews remoteViews = super.makeBigContentView();
7234 if (remoteViews != null) {
7235 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7236 customRemoteView);
7237 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007238 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007239 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7240 customRemoteView);
7241 } else {
7242 return null;
7243 }
7244 }
7245
7246 /**
7247 * @hide
7248 */
7249 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007250 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007251 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7252 ? mBuilder.mN.headsUpContentView
7253 : mBuilder.mN.contentView;
7254 return makeBigContentViewWithCustomContent(customRemoteView);
7255 }
7256
7257 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7258 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007259 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007260 // Need to clone customContent before adding, because otherwise it can no longer be
7261 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007262 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007263 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007264 remoteViews.removeAllViews(id);
7265 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007266 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007267 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007268 return remoteViews;
7269 }
7270 }
7271
Selim Cineke7238dd2017-12-14 17:48:32 -08007272 /**
7273 * A Person associated with this Notification.
7274 */
7275 public static final class Person implements Parcelable {
7276 @Nullable private CharSequence mName;
7277 @Nullable private Icon mIcon;
7278 @Nullable private String mUri;
7279 @Nullable private String mKey;
7280
7281 protected Person(Parcel in) {
7282 mName = in.readCharSequence();
7283 if (in.readInt() != 0) {
7284 mIcon = Icon.CREATOR.createFromParcel(in);
7285 }
7286 mUri = in.readString();
7287 mKey = in.readString();
7288 }
7289
7290 /**
7291 * Create a new person.
7292 */
7293 public Person() {
7294 }
7295
7296 /**
7297 * Give this person a name.
7298 *
7299 * @param name the name of this person
7300 */
7301 public Person setName(@Nullable CharSequence name) {
7302 this.mName = name;
7303 return this;
7304 }
7305
7306 /**
7307 * Add an icon for this person.
7308 * <br />
7309 * This is currently only used for {@link MessagingStyle} notifications and should not be
7310 * provided otherwise, in order to save memory. The system will prefer this icon over any
7311 * images that are resolved from the URI.
7312 *
7313 * @param icon the icon of the person
7314 */
7315 public Person setIcon(@Nullable Icon icon) {
7316 this.mIcon = icon;
7317 return this;
7318 }
7319
7320 /**
7321 * Set a URI associated with this person.
7322 *
7323 * <P>
7324 * Depending on user preferences, adding a URI to a Person may allow the notification to
7325 * pass through interruption filters, if this notification is of
7326 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7327 * The addition of people may also cause this notification to appear more prominently in
7328 * the user interface.
7329 * </P>
7330 *
7331 * <P>
7332 * The person should be specified by the {@code String} representation of a
7333 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7334 * </P>
7335 *
7336 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7337 * URIs. The path part of these URIs must exist in the contacts database, in the
7338 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7339 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7340 * </P>
7341 *
7342 * @param uri a URI for the person
7343 */
7344 public Person setUri(@Nullable String uri) {
7345 mUri = uri;
7346 return this;
7347 }
7348
7349 /**
7350 * Add a key to this person in order to uniquely identify it.
7351 * This is especially useful if the name doesn't uniquely identify this person or if the
7352 * display name is a short handle of the actual name.
7353 *
7354 * <P>If no key is provided, the name serves as as the key for the purpose of
7355 * identification.</P>
7356 *
7357 * @param key the key that uniquely identifies this person
7358 */
7359 public Person setKey(@Nullable String key) {
7360 mKey = key;
7361 return this;
7362 }
7363
7364
7365 /**
7366 * @return the uri provided for this person or {@code null} if no Uri was provided
7367 */
7368 @Nullable
7369 public String getUri() {
7370 return mUri;
7371 }
7372
7373 /**
7374 * @return the name provided for this person or {@code null} if no name was provided
7375 */
7376 @Nullable
7377 public CharSequence getName() {
7378 return mName;
7379 }
7380
7381 /**
7382 * @return the icon provided for this person or {@code null} if no icon was provided
7383 */
7384 @Nullable
7385 public Icon getIcon() {
7386 return mIcon;
7387 }
7388
7389 /**
7390 * @return the key provided for this person or {@code null} if no key was provided
7391 */
7392 @Nullable
7393 public String getKey() {
7394 return mKey;
7395 }
7396
7397 /**
7398 * @return the URI associated with this person, or "name:mName" otherwise
7399 * @hide
7400 */
7401 public String resolveToLegacyUri() {
7402 if (mUri != null) {
7403 return mUri;
7404 }
7405 if (mName != null) {
7406 return "name:" + mName;
7407 }
7408 return "";
7409 }
7410
7411 @Override
7412 public int describeContents() {
7413 return 0;
7414 }
7415
7416 @Override
7417 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7418 dest.writeCharSequence(mName);
7419 if (mIcon != null) {
7420 dest.writeInt(1);
7421 mIcon.writeToParcel(dest, 0);
7422 } else {
7423 dest.writeInt(0);
7424 }
7425 dest.writeString(mUri);
7426 dest.writeString(mKey);
7427 }
7428
7429 public static final Creator<Person> CREATOR = new Creator<Person>() {
7430 @Override
7431 public Person createFromParcel(Parcel in) {
7432 return new Person(in);
7433 }
7434
7435 @Override
7436 public Person[] newArray(int size) {
7437 return new Person[size];
7438 }
7439 };
7440 }
7441
Christoph Studer4600f9b2014-07-22 22:44:43 +02007442 // When adding a new Style subclass here, don't forget to update
7443 // Builder.getNotificationStyleClass.
7444
Griff Hazen61a9e862014-05-22 16:05:19 -07007445 /**
7446 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7447 * metadata or change options on a notification builder.
7448 */
7449 public interface Extender {
7450 /**
7451 * Apply this extender to a notification builder.
7452 * @param builder the builder to be modified.
7453 * @return the build object for chaining.
7454 */
7455 public Builder extend(Builder builder);
7456 }
7457
7458 /**
7459 * Helper class to add wearable extensions to notifications.
7460 * <p class="note"> See
7461 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7462 * for Android Wear</a> for more information on how to use this class.
7463 * <p>
7464 * To create a notification with wearable extensions:
7465 * <ol>
7466 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7467 * properties.
7468 * <li>Create a {@link android.app.Notification.WearableExtender}.
7469 * <li>Set wearable-specific properties using the
7470 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7471 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7472 * notification.
7473 * <li>Post the notification to the notification system with the
7474 * {@code NotificationManager.notify(...)} methods.
7475 * </ol>
7476 *
7477 * <pre class="prettyprint">
7478 * Notification notif = new Notification.Builder(mContext)
7479 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7480 * .setContentText(subject)
7481 * .setSmallIcon(R.drawable.new_mail)
7482 * .extend(new Notification.WearableExtender()
7483 * .setContentIcon(R.drawable.new_mail))
7484 * .build();
7485 * NotificationManager notificationManger =
7486 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7487 * notificationManger.notify(0, notif);</pre>
7488 *
7489 * <p>Wearable extensions can be accessed on an existing notification by using the
7490 * {@code WearableExtender(Notification)} constructor,
7491 * and then using the {@code get} methods to access values.
7492 *
7493 * <pre class="prettyprint">
7494 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7495 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007496 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007497 */
7498 public static final class WearableExtender implements Extender {
7499 /**
7500 * Sentinel value for an action index that is unset.
7501 */
7502 public static final int UNSET_ACTION_INDEX = -1;
7503
7504 /**
7505 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7506 * default sizing.
7507 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007508 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007509 * on their content.
7510 */
7511 public static final int SIZE_DEFAULT = 0;
7512
7513 /**
7514 * Size value for use with {@link #setCustomSizePreset} to show this notification
7515 * with an extra small size.
7516 * <p>This value is only applicable for custom display notifications created using
7517 * {@link #setDisplayIntent}.
7518 */
7519 public static final int SIZE_XSMALL = 1;
7520
7521 /**
7522 * Size value for use with {@link #setCustomSizePreset} to show this notification
7523 * with a small size.
7524 * <p>This value is only applicable for custom display notifications created using
7525 * {@link #setDisplayIntent}.
7526 */
7527 public static final int SIZE_SMALL = 2;
7528
7529 /**
7530 * Size value for use with {@link #setCustomSizePreset} to show this notification
7531 * with a medium size.
7532 * <p>This value is only applicable for custom display notifications created using
7533 * {@link #setDisplayIntent}.
7534 */
7535 public static final int SIZE_MEDIUM = 3;
7536
7537 /**
7538 * Size value for use with {@link #setCustomSizePreset} to show this notification
7539 * with a large size.
7540 * <p>This value is only applicable for custom display notifications created using
7541 * {@link #setDisplayIntent}.
7542 */
7543 public static final int SIZE_LARGE = 4;
7544
Griff Hazend5f11f92014-05-27 15:40:09 -07007545 /**
7546 * Size value for use with {@link #setCustomSizePreset} to show this notification
7547 * full screen.
7548 * <p>This value is only applicable for custom display notifications created using
7549 * {@link #setDisplayIntent}.
7550 */
7551 public static final int SIZE_FULL_SCREEN = 5;
7552
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007553 /**
7554 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7555 * short amount of time when this notification is displayed on the screen. This
7556 * is the default value.
7557 */
7558 public static final int SCREEN_TIMEOUT_SHORT = 0;
7559
7560 /**
7561 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7562 * for a longer amount of time when this notification is displayed on the screen.
7563 */
7564 public static final int SCREEN_TIMEOUT_LONG = -1;
7565
Griff Hazen61a9e862014-05-22 16:05:19 -07007566 /** Notification extra which contains wearable extensions */
7567 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7568
Pete Gastaf6781d2014-10-07 15:17:05 -04007569 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007570 private static final String KEY_ACTIONS = "actions";
7571 private static final String KEY_FLAGS = "flags";
7572 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7573 private static final String KEY_PAGES = "pages";
7574 private static final String KEY_BACKGROUND = "background";
7575 private static final String KEY_CONTENT_ICON = "contentIcon";
7576 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7577 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7578 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7579 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7580 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007581 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007582 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007583 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007584
7585 // Flags bitwise-ored to mFlags
7586 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7587 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7588 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7589 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007590 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007591 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007592 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007593
7594 // Default value for flags integer
7595 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7596
7597 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7598 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7599
7600 private ArrayList<Action> mActions = new ArrayList<Action>();
7601 private int mFlags = DEFAULT_FLAGS;
7602 private PendingIntent mDisplayIntent;
7603 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7604 private Bitmap mBackground;
7605 private int mContentIcon;
7606 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7607 private int mContentActionIndex = UNSET_ACTION_INDEX;
7608 private int mCustomSizePreset = SIZE_DEFAULT;
7609 private int mCustomContentHeight;
7610 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007611 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007612 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007613 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007614
7615 /**
7616 * Create a {@link android.app.Notification.WearableExtender} with default
7617 * options.
7618 */
7619 public WearableExtender() {
7620 }
7621
7622 public WearableExtender(Notification notif) {
7623 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7624 if (wearableBundle != null) {
7625 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7626 if (actions != null) {
7627 mActions.addAll(actions);
7628 }
7629
7630 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7631 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7632
7633 Notification[] pages = getNotificationArrayFromBundle(
7634 wearableBundle, KEY_PAGES);
7635 if (pages != null) {
7636 Collections.addAll(mPages, pages);
7637 }
7638
7639 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7640 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7641 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7642 DEFAULT_CONTENT_ICON_GRAVITY);
7643 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7644 UNSET_ACTION_INDEX);
7645 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7646 SIZE_DEFAULT);
7647 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7648 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007649 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007650 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007651 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007652 }
7653 }
7654
7655 /**
7656 * Apply wearable extensions to a notification that is being built. This is typically
7657 * called by the {@link android.app.Notification.Builder#extend} method of
7658 * {@link android.app.Notification.Builder}.
7659 */
7660 @Override
7661 public Notification.Builder extend(Notification.Builder builder) {
7662 Bundle wearableBundle = new Bundle();
7663
7664 if (!mActions.isEmpty()) {
7665 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7666 }
7667 if (mFlags != DEFAULT_FLAGS) {
7668 wearableBundle.putInt(KEY_FLAGS, mFlags);
7669 }
7670 if (mDisplayIntent != null) {
7671 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7672 }
7673 if (!mPages.isEmpty()) {
7674 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7675 new Notification[mPages.size()]));
7676 }
7677 if (mBackground != null) {
7678 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7679 }
7680 if (mContentIcon != 0) {
7681 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7682 }
7683 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7684 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7685 }
7686 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7687 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7688 mContentActionIndex);
7689 }
7690 if (mCustomSizePreset != SIZE_DEFAULT) {
7691 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7692 }
7693 if (mCustomContentHeight != 0) {
7694 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7695 }
7696 if (mGravity != DEFAULT_GRAVITY) {
7697 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7698 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007699 if (mHintScreenTimeout != 0) {
7700 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7701 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007702 if (mDismissalId != null) {
7703 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7704 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007705 if (mBridgeTag != null) {
7706 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7707 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007708
7709 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7710 return builder;
7711 }
7712
7713 @Override
7714 public WearableExtender clone() {
7715 WearableExtender that = new WearableExtender();
7716 that.mActions = new ArrayList<Action>(this.mActions);
7717 that.mFlags = this.mFlags;
7718 that.mDisplayIntent = this.mDisplayIntent;
7719 that.mPages = new ArrayList<Notification>(this.mPages);
7720 that.mBackground = this.mBackground;
7721 that.mContentIcon = this.mContentIcon;
7722 that.mContentIconGravity = this.mContentIconGravity;
7723 that.mContentActionIndex = this.mContentActionIndex;
7724 that.mCustomSizePreset = this.mCustomSizePreset;
7725 that.mCustomContentHeight = this.mCustomContentHeight;
7726 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007727 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007728 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007729 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007730 return that;
7731 }
7732
7733 /**
7734 * Add a wearable action to this notification.
7735 *
7736 * <p>When wearable actions are added using this method, the set of actions that
7737 * show on a wearable device splits from devices that only show actions added
7738 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7739 * of which actions display on different devices.
7740 *
7741 * @param action the action to add to this notification
7742 * @return this object for method chaining
7743 * @see android.app.Notification.Action
7744 */
7745 public WearableExtender addAction(Action action) {
7746 mActions.add(action);
7747 return this;
7748 }
7749
7750 /**
7751 * Adds wearable actions to this notification.
7752 *
7753 * <p>When wearable actions are added using this method, the set of actions that
7754 * show on a wearable device splits from devices that only show actions added
7755 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7756 * of which actions display on different devices.
7757 *
7758 * @param actions the actions to add to this notification
7759 * @return this object for method chaining
7760 * @see android.app.Notification.Action
7761 */
7762 public WearableExtender addActions(List<Action> actions) {
7763 mActions.addAll(actions);
7764 return this;
7765 }
7766
7767 /**
7768 * Clear all wearable actions present on this builder.
7769 * @return this object for method chaining.
7770 * @see #addAction
7771 */
7772 public WearableExtender clearActions() {
7773 mActions.clear();
7774 return this;
7775 }
7776
7777 /**
7778 * Get the wearable actions present on this notification.
7779 */
7780 public List<Action> getActions() {
7781 return mActions;
7782 }
7783
7784 /**
7785 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007786 * this notification. The {@link PendingIntent} provided should be for an activity.
7787 *
7788 * <pre class="prettyprint">
7789 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7790 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7791 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7792 * Notification notif = new Notification.Builder(context)
7793 * .extend(new Notification.WearableExtender()
7794 * .setDisplayIntent(displayPendingIntent)
7795 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7796 * .build();</pre>
7797 *
7798 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007799 * should have an empty task affinity. It is also recommended to use the device
7800 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007801 *
7802 * <p>Example AndroidManifest.xml entry:
7803 * <pre class="prettyprint">
7804 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7805 * android:exported=&quot;true&quot;
7806 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007807 * android:taskAffinity=&quot;&quot;
7808 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007809 *
7810 * @param intent the {@link PendingIntent} for an activity
7811 * @return this object for method chaining
7812 * @see android.app.Notification.WearableExtender#getDisplayIntent
7813 */
7814 public WearableExtender setDisplayIntent(PendingIntent intent) {
7815 mDisplayIntent = intent;
7816 return this;
7817 }
7818
7819 /**
7820 * Get the intent to launch inside of an activity view when displaying this
7821 * notification. This {@code PendingIntent} should be for an activity.
7822 */
7823 public PendingIntent getDisplayIntent() {
7824 return mDisplayIntent;
7825 }
7826
7827 /**
7828 * Add an additional page of content to display with this notification. The current
7829 * notification forms the first page, and pages added using this function form
7830 * subsequent pages. This field can be used to separate a notification into multiple
7831 * sections.
7832 *
7833 * @param page the notification to add as another page
7834 * @return this object for method chaining
7835 * @see android.app.Notification.WearableExtender#getPages
7836 */
7837 public WearableExtender addPage(Notification page) {
7838 mPages.add(page);
7839 return this;
7840 }
7841
7842 /**
7843 * Add additional pages of content to display with this notification. The current
7844 * notification forms the first page, and pages added using this function form
7845 * subsequent pages. This field can be used to separate a notification into multiple
7846 * sections.
7847 *
7848 * @param pages a list of notifications
7849 * @return this object for method chaining
7850 * @see android.app.Notification.WearableExtender#getPages
7851 */
7852 public WearableExtender addPages(List<Notification> pages) {
7853 mPages.addAll(pages);
7854 return this;
7855 }
7856
7857 /**
7858 * Clear all additional pages present on this builder.
7859 * @return this object for method chaining.
7860 * @see #addPage
7861 */
7862 public WearableExtender clearPages() {
7863 mPages.clear();
7864 return this;
7865 }
7866
7867 /**
7868 * Get the array of additional pages of content for displaying this notification. The
7869 * current notification forms the first page, and elements within this array form
7870 * subsequent pages. This field can be used to separate a notification into multiple
7871 * sections.
7872 * @return the pages for this notification
7873 */
7874 public List<Notification> getPages() {
7875 return mPages;
7876 }
7877
7878 /**
7879 * Set a background image to be displayed behind the notification content.
7880 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7881 * will work with any notification style.
7882 *
7883 * @param background the background bitmap
7884 * @return this object for method chaining
7885 * @see android.app.Notification.WearableExtender#getBackground
7886 */
7887 public WearableExtender setBackground(Bitmap background) {
7888 mBackground = background;
7889 return this;
7890 }
7891
7892 /**
7893 * Get a background image to be displayed behind the notification content.
7894 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7895 * will work with any notification style.
7896 *
7897 * @return the background image
7898 * @see android.app.Notification.WearableExtender#setBackground
7899 */
7900 public Bitmap getBackground() {
7901 return mBackground;
7902 }
7903
7904 /**
7905 * Set an icon that goes with the content of this notification.
7906 */
7907 public WearableExtender setContentIcon(int icon) {
7908 mContentIcon = icon;
7909 return this;
7910 }
7911
7912 /**
7913 * Get an icon that goes with the content of this notification.
7914 */
7915 public int getContentIcon() {
7916 return mContentIcon;
7917 }
7918
7919 /**
7920 * Set the gravity that the content icon should have within the notification display.
7921 * Supported values include {@link android.view.Gravity#START} and
7922 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7923 * @see #setContentIcon
7924 */
7925 public WearableExtender setContentIconGravity(int contentIconGravity) {
7926 mContentIconGravity = contentIconGravity;
7927 return this;
7928 }
7929
7930 /**
7931 * Get the gravity that the content icon should have within the notification display.
7932 * Supported values include {@link android.view.Gravity#START} and
7933 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7934 * @see #getContentIcon
7935 */
7936 public int getContentIconGravity() {
7937 return mContentIconGravity;
7938 }
7939
7940 /**
7941 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007942 * this notification. This action will no longer display separately from the
7943 * notification's content.
7944 *
Griff Hazenca48d352014-05-28 22:37:13 -07007945 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007946 * set, although the list of available actions comes from the main notification and not
7947 * from the child page's notification.
7948 *
7949 * @param actionIndex The index of the action to hoist onto the current notification page.
7950 * If wearable actions were added to the main notification, this index
7951 * will apply to that list, otherwise it will apply to the regular
7952 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007953 */
7954 public WearableExtender setContentAction(int actionIndex) {
7955 mContentActionIndex = actionIndex;
7956 return this;
7957 }
7958
7959 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007960 * Get the index of the notification action, if any, that was specified as being clickable
7961 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007962 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007963 *
Griff Hazenca48d352014-05-28 22:37:13 -07007964 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007965 * set, although the list of available actions comes from the main notification and not
7966 * from the child page's notification.
7967 *
7968 * <p>If wearable specific actions were added to the main notification, this index will
7969 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007970 *
7971 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007972 */
7973 public int getContentAction() {
7974 return mContentActionIndex;
7975 }
7976
7977 /**
7978 * Set the gravity that this notification should have within the available viewport space.
7979 * Supported values include {@link android.view.Gravity#TOP},
7980 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7981 * The default value is {@link android.view.Gravity#BOTTOM}.
7982 */
7983 public WearableExtender setGravity(int gravity) {
7984 mGravity = gravity;
7985 return this;
7986 }
7987
7988 /**
7989 * Get the gravity that this notification should have within the available viewport space.
7990 * Supported values include {@link android.view.Gravity#TOP},
7991 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7992 * The default value is {@link android.view.Gravity#BOTTOM}.
7993 */
7994 public int getGravity() {
7995 return mGravity;
7996 }
7997
7998 /**
7999 * Set the custom size preset for the display of this notification out of the available
8000 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8001 * {@link #SIZE_LARGE}.
8002 * <p>Some custom size presets are only applicable for custom display notifications created
8003 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8004 * documentation for the preset in question. See also
8005 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8006 */
8007 public WearableExtender setCustomSizePreset(int sizePreset) {
8008 mCustomSizePreset = sizePreset;
8009 return this;
8010 }
8011
8012 /**
8013 * Get the custom size preset for the display of this notification out of the available
8014 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8015 * {@link #SIZE_LARGE}.
8016 * <p>Some custom size presets are only applicable for custom display notifications created
8017 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8018 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8019 */
8020 public int getCustomSizePreset() {
8021 return mCustomSizePreset;
8022 }
8023
8024 /**
8025 * Set the custom height in pixels for the display of this notification's content.
8026 * <p>This option is only available for custom display notifications created
8027 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8028 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8029 * {@link #getCustomContentHeight}.
8030 */
8031 public WearableExtender setCustomContentHeight(int height) {
8032 mCustomContentHeight = height;
8033 return this;
8034 }
8035
8036 /**
8037 * Get the custom height in pixels for the display of this notification's content.
8038 * <p>This option is only available for custom display notifications created
8039 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8040 * {@link #setCustomContentHeight}.
8041 */
8042 public int getCustomContentHeight() {
8043 return mCustomContentHeight;
8044 }
8045
8046 /**
8047 * Set whether the scrolling position for the contents of this notification should start
8048 * at the bottom of the contents instead of the top when the contents are too long to
8049 * display within the screen. Default is false (start scroll at the top).
8050 */
8051 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8052 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8053 return this;
8054 }
8055
8056 /**
8057 * Get whether the scrolling position for the contents of this notification should start
8058 * at the bottom of the contents instead of the top when the contents are too long to
8059 * display within the screen. Default is false (start scroll at the top).
8060 */
8061 public boolean getStartScrollBottom() {
8062 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8063 }
8064
8065 /**
8066 * Set whether the content intent is available when the wearable device is not connected
8067 * to a companion device. The user can still trigger this intent when the wearable device
8068 * is offline, but a visual hint will indicate that the content intent may not be available.
8069 * Defaults to true.
8070 */
8071 public WearableExtender setContentIntentAvailableOffline(
8072 boolean contentIntentAvailableOffline) {
8073 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8074 return this;
8075 }
8076
8077 /**
8078 * Get whether the content intent is available when the wearable device is not connected
8079 * to a companion device. The user can still trigger this intent when the wearable device
8080 * is offline, but a visual hint will indicate that the content intent may not be available.
8081 * Defaults to true.
8082 */
8083 public boolean getContentIntentAvailableOffline() {
8084 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8085 }
8086
8087 /**
8088 * Set a hint that this notification's icon should not be displayed.
8089 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8090 * @return this object for method chaining
8091 */
8092 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8093 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8094 return this;
8095 }
8096
8097 /**
8098 * Get a hint that this notification's icon should not be displayed.
8099 * @return {@code true} if this icon should not be displayed, false otherwise.
8100 * The default value is {@code false} if this was never set.
8101 */
8102 public boolean getHintHideIcon() {
8103 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8104 }
8105
8106 /**
8107 * Set a visual hint that only the background image of this notification should be
8108 * displayed, and other semantic content should be hidden. This hint is only applicable
8109 * to sub-pages added using {@link #addPage}.
8110 */
8111 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8112 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8113 return this;
8114 }
8115
8116 /**
8117 * Get a visual hint that only the background image of this notification should be
8118 * displayed, and other semantic content should be hidden. This hint is only applicable
8119 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8120 */
8121 public boolean getHintShowBackgroundOnly() {
8122 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8123 }
8124
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008125 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008126 * Set a hint that this notification's background should not be clipped if possible,
8127 * and should instead be resized to fully display on the screen, retaining the aspect
8128 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008129 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8130 * @return this object for method chaining
8131 */
8132 public WearableExtender setHintAvoidBackgroundClipping(
8133 boolean hintAvoidBackgroundClipping) {
8134 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8135 return this;
8136 }
8137
8138 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008139 * Get a hint that this notification's background should not be clipped if possible,
8140 * and should instead be resized to fully display on the screen, retaining the aspect
8141 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008142 * @return {@code true} if it's ok if the background is clipped on the screen, false
8143 * otherwise. The default value is {@code false} if this was never set.
8144 */
8145 public boolean getHintAvoidBackgroundClipping() {
8146 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8147 }
8148
8149 /**
8150 * Set a hint that the screen should remain on for at least this duration when
8151 * this notification is displayed on the screen.
8152 * @param timeout The requested screen timeout in milliseconds. Can also be either
8153 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8154 * @return this object for method chaining
8155 */
8156 public WearableExtender setHintScreenTimeout(int timeout) {
8157 mHintScreenTimeout = timeout;
8158 return this;
8159 }
8160
8161 /**
8162 * Get the duration, in milliseconds, that the screen should remain on for
8163 * when this notification is displayed.
8164 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8165 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8166 */
8167 public int getHintScreenTimeout() {
8168 return mHintScreenTimeout;
8169 }
8170
Alex Hills9ab3a232016-04-05 14:54:56 -04008171 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008172 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8173 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8174 * qr codes, as well as other simple black-and-white tickets.
8175 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8176 * @return this object for method chaining
8177 */
8178 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8179 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8180 return this;
8181 }
8182
8183 /**
8184 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8185 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8186 * qr codes, as well as other simple black-and-white tickets.
8187 * @return {@code true} if it should be displayed in ambient, false otherwise
8188 * otherwise. The default value is {@code false} if this was never set.
8189 */
8190 public boolean getHintAmbientBigPicture() {
8191 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8192 }
8193
8194 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008195 * Set a hint that this notification's content intent will launch an {@link Activity}
8196 * directly, telling the platform that it can generate the appropriate transitions.
8197 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8198 * an activity and transitions should be generated, false otherwise.
8199 * @return this object for method chaining
8200 */
8201 public WearableExtender setHintContentIntentLaunchesActivity(
8202 boolean hintContentIntentLaunchesActivity) {
8203 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8204 return this;
8205 }
8206
8207 /**
8208 * Get a hint that this notification's content intent will launch an {@link Activity}
8209 * directly, telling the platform that it can generate the appropriate transitions
8210 * @return {@code true} if the content intent will launch an activity and transitions should
8211 * be generated, false otherwise. The default value is {@code false} if this was never set.
8212 */
8213 public boolean getHintContentIntentLaunchesActivity() {
8214 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8215 }
8216
Nadia Benbernou948627e2016-04-14 14:41:08 -04008217 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008218 * Sets the dismissal id for this notification. If a notification is posted with a
8219 * dismissal id, then when that notification is canceled, notifications on other wearables
8220 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008221 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008222 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008223 * @param dismissalId the dismissal id of the notification.
8224 * @return this object for method chaining
8225 */
8226 public WearableExtender setDismissalId(String dismissalId) {
8227 mDismissalId = dismissalId;
8228 return this;
8229 }
8230
8231 /**
8232 * Returns the dismissal id of the notification.
8233 * @return the dismissal id of the notification or null if it has not been set.
8234 */
8235 public String getDismissalId() {
8236 return mDismissalId;
8237 }
8238
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008239 /**
8240 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8241 * posted from a phone to provide finer-grained control on what notifications are bridged
8242 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8243 * Features to Notifications</a> for more information.
8244 * @param bridgeTag the bridge tag of the notification.
8245 * @return this object for method chaining
8246 */
8247 public WearableExtender setBridgeTag(String bridgeTag) {
8248 mBridgeTag = bridgeTag;
8249 return this;
8250 }
8251
8252 /**
8253 * Returns the bridge tag of the notification.
8254 * @return the bridge tag or null if not present.
8255 */
8256 public String getBridgeTag() {
8257 return mBridgeTag;
8258 }
8259
Griff Hazen61a9e862014-05-22 16:05:19 -07008260 private void setFlag(int mask, boolean value) {
8261 if (value) {
8262 mFlags |= mask;
8263 } else {
8264 mFlags &= ~mask;
8265 }
8266 }
8267 }
8268
8269 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008270 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8271 * with car extensions:
8272 *
8273 * <ol>
8274 * <li>Create an {@link Notification.Builder}, setting any desired
8275 * properties.
8276 * <li>Create a {@link CarExtender}.
8277 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8278 * {@link CarExtender}.
8279 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8280 * to apply the extensions to a notification.
8281 * </ol>
8282 *
8283 * <pre class="prettyprint">
8284 * Notification notification = new Notification.Builder(context)
8285 * ...
8286 * .extend(new CarExtender()
8287 * .set*(...))
8288 * .build();
8289 * </pre>
8290 *
8291 * <p>Car extensions can be accessed on an existing notification by using the
8292 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8293 * to access values.
8294 */
8295 public static final class CarExtender implements Extender {
8296 private static final String TAG = "CarExtender";
8297
8298 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8299 private static final String EXTRA_LARGE_ICON = "large_icon";
8300 private static final String EXTRA_CONVERSATION = "car_conversation";
8301 private static final String EXTRA_COLOR = "app_color";
8302
8303 private Bitmap mLargeIcon;
8304 private UnreadConversation mUnreadConversation;
8305 private int mColor = Notification.COLOR_DEFAULT;
8306
8307 /**
8308 * Create a {@link CarExtender} with default options.
8309 */
8310 public CarExtender() {
8311 }
8312
8313 /**
8314 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8315 *
8316 * @param notif The notification from which to copy options.
8317 */
8318 public CarExtender(Notification notif) {
8319 Bundle carBundle = notif.extras == null ?
8320 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8321 if (carBundle != null) {
8322 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8323 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8324
8325 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8326 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8327 }
8328 }
8329
8330 /**
8331 * Apply car extensions to a notification that is being built. This is typically called by
8332 * the {@link Notification.Builder#extend(Notification.Extender)}
8333 * method of {@link Notification.Builder}.
8334 */
8335 @Override
8336 public Notification.Builder extend(Notification.Builder builder) {
8337 Bundle carExtensions = new Bundle();
8338
8339 if (mLargeIcon != null) {
8340 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8341 }
8342 if (mColor != Notification.COLOR_DEFAULT) {
8343 carExtensions.putInt(EXTRA_COLOR, mColor);
8344 }
8345
8346 if (mUnreadConversation != null) {
8347 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8348 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8349 }
8350
8351 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8352 return builder;
8353 }
8354
8355 /**
8356 * Sets the accent color to use when Android Auto presents the notification.
8357 *
8358 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8359 * to accent the displayed notification. However, not all colors are acceptable in an
8360 * automotive setting. This method can be used to override the color provided in the
8361 * notification in such a situation.
8362 */
Tor Norbye80756e32015-03-02 09:39:27 -08008363 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008364 mColor = color;
8365 return this;
8366 }
8367
8368 /**
8369 * Gets the accent color.
8370 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008371 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008372 */
Tor Norbye80756e32015-03-02 09:39:27 -08008373 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008374 public int getColor() {
8375 return mColor;
8376 }
8377
8378 /**
8379 * Sets the large icon of the car notification.
8380 *
8381 * If no large icon is set in the extender, Android Auto will display the icon
8382 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8383 *
8384 * @param largeIcon The large icon to use in the car notification.
8385 * @return This object for method chaining.
8386 */
8387 public CarExtender setLargeIcon(Bitmap largeIcon) {
8388 mLargeIcon = largeIcon;
8389 return this;
8390 }
8391
8392 /**
8393 * Gets the large icon used in this car notification, or null if no icon has been set.
8394 *
8395 * @return The large icon for the car notification.
8396 * @see CarExtender#setLargeIcon
8397 */
8398 public Bitmap getLargeIcon() {
8399 return mLargeIcon;
8400 }
8401
8402 /**
8403 * Sets the unread conversation in a message notification.
8404 *
8405 * @param unreadConversation The unread part of the conversation this notification conveys.
8406 * @return This object for method chaining.
8407 */
8408 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8409 mUnreadConversation = unreadConversation;
8410 return this;
8411 }
8412
8413 /**
8414 * Returns the unread conversation conveyed by this notification.
8415 * @see #setUnreadConversation(UnreadConversation)
8416 */
8417 public UnreadConversation getUnreadConversation() {
8418 return mUnreadConversation;
8419 }
8420
8421 /**
8422 * A class which holds the unread messages from a conversation.
8423 */
8424 public static class UnreadConversation {
8425 private static final String KEY_AUTHOR = "author";
8426 private static final String KEY_TEXT = "text";
8427 private static final String KEY_MESSAGES = "messages";
8428 private static final String KEY_REMOTE_INPUT = "remote_input";
8429 private static final String KEY_ON_REPLY = "on_reply";
8430 private static final String KEY_ON_READ = "on_read";
8431 private static final String KEY_PARTICIPANTS = "participants";
8432 private static final String KEY_TIMESTAMP = "timestamp";
8433
8434 private final String[] mMessages;
8435 private final RemoteInput mRemoteInput;
8436 private final PendingIntent mReplyPendingIntent;
8437 private final PendingIntent mReadPendingIntent;
8438 private final String[] mParticipants;
8439 private final long mLatestTimestamp;
8440
8441 UnreadConversation(String[] messages, RemoteInput remoteInput,
8442 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8443 String[] participants, long latestTimestamp) {
8444 mMessages = messages;
8445 mRemoteInput = remoteInput;
8446 mReadPendingIntent = readPendingIntent;
8447 mReplyPendingIntent = replyPendingIntent;
8448 mParticipants = participants;
8449 mLatestTimestamp = latestTimestamp;
8450 }
8451
8452 /**
8453 * Gets the list of messages conveyed by this notification.
8454 */
8455 public String[] getMessages() {
8456 return mMessages;
8457 }
8458
8459 /**
8460 * Gets the remote input that will be used to convey the response to a message list, or
8461 * null if no such remote input exists.
8462 */
8463 public RemoteInput getRemoteInput() {
8464 return mRemoteInput;
8465 }
8466
8467 /**
8468 * Gets the pending intent that will be triggered when the user replies to this
8469 * notification.
8470 */
8471 public PendingIntent getReplyPendingIntent() {
8472 return mReplyPendingIntent;
8473 }
8474
8475 /**
8476 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8477 * in this object's message list.
8478 */
8479 public PendingIntent getReadPendingIntent() {
8480 return mReadPendingIntent;
8481 }
8482
8483 /**
8484 * Gets the participants in the conversation.
8485 */
8486 public String[] getParticipants() {
8487 return mParticipants;
8488 }
8489
8490 /**
8491 * Gets the firs participant in the conversation.
8492 */
8493 public String getParticipant() {
8494 return mParticipants.length > 0 ? mParticipants[0] : null;
8495 }
8496
8497 /**
8498 * Gets the timestamp of the conversation.
8499 */
8500 public long getLatestTimestamp() {
8501 return mLatestTimestamp;
8502 }
8503
8504 Bundle getBundleForUnreadConversation() {
8505 Bundle b = new Bundle();
8506 String author = null;
8507 if (mParticipants != null && mParticipants.length > 1) {
8508 author = mParticipants[0];
8509 }
8510 Parcelable[] messages = new Parcelable[mMessages.length];
8511 for (int i = 0; i < messages.length; i++) {
8512 Bundle m = new Bundle();
8513 m.putString(KEY_TEXT, mMessages[i]);
8514 m.putString(KEY_AUTHOR, author);
8515 messages[i] = m;
8516 }
8517 b.putParcelableArray(KEY_MESSAGES, messages);
8518 if (mRemoteInput != null) {
8519 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8520 }
8521 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8522 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8523 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8524 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8525 return b;
8526 }
8527
8528 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8529 if (b == null) {
8530 return null;
8531 }
8532 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8533 String[] messages = null;
8534 if (parcelableMessages != null) {
8535 String[] tmp = new String[parcelableMessages.length];
8536 boolean success = true;
8537 for (int i = 0; i < tmp.length; i++) {
8538 if (!(parcelableMessages[i] instanceof Bundle)) {
8539 success = false;
8540 break;
8541 }
8542 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8543 if (tmp[i] == null) {
8544 success = false;
8545 break;
8546 }
8547 }
8548 if (success) {
8549 messages = tmp;
8550 } else {
8551 return null;
8552 }
8553 }
8554
8555 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8556 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8557
8558 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8559
8560 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8561 if (participants == null || participants.length != 1) {
8562 return null;
8563 }
8564
8565 return new UnreadConversation(messages,
8566 remoteInput,
8567 onReply,
8568 onRead,
8569 participants, b.getLong(KEY_TIMESTAMP));
8570 }
8571 };
8572
8573 /**
8574 * Builder class for {@link CarExtender.UnreadConversation} objects.
8575 */
8576 public static class Builder {
8577 private final List<String> mMessages = new ArrayList<String>();
8578 private final String mParticipant;
8579 private RemoteInput mRemoteInput;
8580 private PendingIntent mReadPendingIntent;
8581 private PendingIntent mReplyPendingIntent;
8582 private long mLatestTimestamp;
8583
8584 /**
8585 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8586 *
8587 * @param name The name of the other participant in the conversation.
8588 */
8589 public Builder(String name) {
8590 mParticipant = name;
8591 }
8592
8593 /**
8594 * Appends a new unread message to the list of messages for this conversation.
8595 *
8596 * The messages should be added from oldest to newest.
8597 *
8598 * @param message The text of the new unread message.
8599 * @return This object for method chaining.
8600 */
8601 public Builder addMessage(String message) {
8602 mMessages.add(message);
8603 return this;
8604 }
8605
8606 /**
8607 * Sets the pending intent and remote input which will convey the reply to this
8608 * notification.
8609 *
8610 * @param pendingIntent The pending intent which will be triggered on a reply.
8611 * @param remoteInput The remote input parcelable which will carry the reply.
8612 * @return This object for method chaining.
8613 *
8614 * @see CarExtender.UnreadConversation#getRemoteInput
8615 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8616 */
8617 public Builder setReplyAction(
8618 PendingIntent pendingIntent, RemoteInput remoteInput) {
8619 mRemoteInput = remoteInput;
8620 mReplyPendingIntent = pendingIntent;
8621
8622 return this;
8623 }
8624
8625 /**
8626 * Sets the pending intent that will be sent once the messages in this notification
8627 * are read.
8628 *
8629 * @param pendingIntent The pending intent to use.
8630 * @return This object for method chaining.
8631 */
8632 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8633 mReadPendingIntent = pendingIntent;
8634 return this;
8635 }
8636
8637 /**
8638 * Sets the timestamp of the most recent message in an unread conversation.
8639 *
8640 * If a messaging notification has been posted by your application and has not
8641 * yet been cancelled, posting a later notification with the same id and tag
8642 * but without a newer timestamp may result in Android Auto not displaying a
8643 * heads up notification for the later notification.
8644 *
8645 * @param timestamp The timestamp of the most recent message in the conversation.
8646 * @return This object for method chaining.
8647 */
8648 public Builder setLatestTimestamp(long timestamp) {
8649 mLatestTimestamp = timestamp;
8650 return this;
8651 }
8652
8653 /**
8654 * Builds a new unread conversation object.
8655 *
8656 * @return The new unread conversation object.
8657 */
8658 public UnreadConversation build() {
8659 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8660 String[] participants = { mParticipant };
8661 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8662 mReadPendingIntent, participants, mLatestTimestamp);
8663 }
8664 }
8665 }
8666
8667 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008668 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8669 * with a TV extension:
8670 *
8671 * <ol>
8672 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8673 * <li>Create a {@link TvExtender}.
8674 * <li>Set TV-specific properties using the {@code set} methods of
8675 * {@link TvExtender}.
8676 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8677 * to apply the extension to a notification.
8678 * </ol>
8679 *
8680 * <pre class="prettyprint">
8681 * Notification notification = new Notification.Builder(context)
8682 * ...
8683 * .extend(new TvExtender()
8684 * .set*(...))
8685 * .build();
8686 * </pre>
8687 *
8688 * <p>TV extensions can be accessed on an existing notification by using the
8689 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8690 * to access values.
8691 *
8692 * @hide
8693 */
8694 @SystemApi
8695 public static final class TvExtender implements Extender {
8696 private static final String TAG = "TvExtender";
8697
8698 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8699 private static final String EXTRA_FLAGS = "flags";
8700 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8701 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008702 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008703
8704 // Flags bitwise-ored to mFlags
8705 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8706
8707 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008708 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008709 private PendingIntent mContentIntent;
8710 private PendingIntent mDeleteIntent;
8711
8712 /**
8713 * Create a {@link TvExtender} with default options.
8714 */
8715 public TvExtender() {
8716 mFlags = FLAG_AVAILABLE_ON_TV;
8717 }
8718
8719 /**
8720 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8721 *
8722 * @param notif The notification from which to copy options.
8723 */
8724 public TvExtender(Notification notif) {
8725 Bundle bundle = notif.extras == null ?
8726 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8727 if (bundle != null) {
8728 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008729 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008730 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8731 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8732 }
8733 }
8734
8735 /**
8736 * Apply a TV extension to a notification that is being built. This is typically called by
8737 * the {@link Notification.Builder#extend(Notification.Extender)}
8738 * method of {@link Notification.Builder}.
8739 */
8740 @Override
8741 public Notification.Builder extend(Notification.Builder builder) {
8742 Bundle bundle = new Bundle();
8743
8744 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008745 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008746 if (mContentIntent != null) {
8747 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8748 }
8749
8750 if (mDeleteIntent != null) {
8751 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8752 }
8753
8754 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8755 return builder;
8756 }
8757
8758 /**
8759 * Returns true if this notification should be shown on TV. This method return true
8760 * if the notification was extended with a TvExtender.
8761 */
8762 public boolean isAvailableOnTv() {
8763 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8764 }
8765
8766 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008767 * Specifies the channel the notification should be delivered on when shown on TV.
8768 * It can be different from the channel that the notification is delivered to when
8769 * posting on a non-TV device.
8770 */
8771 public TvExtender setChannel(String channelId) {
8772 mChannelId = channelId;
8773 return this;
8774 }
8775
8776 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008777 * Specifies the channel the notification should be delivered on when shown on TV.
8778 * It can be different from the channel that the notification is delivered to when
8779 * posting on a non-TV device.
8780 */
8781 public TvExtender setChannelId(String channelId) {
8782 mChannelId = channelId;
8783 return this;
8784 }
8785
Jeff Sharkey000ce802017-04-29 13:13:27 -06008786 /** @removed */
8787 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008788 public String getChannel() {
8789 return mChannelId;
8790 }
8791
8792 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008793 * Returns the id of the channel this notification posts to on TV.
8794 */
8795 public String getChannelId() {
8796 return mChannelId;
8797 }
8798
8799 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008800 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8801 * If provided, it is used instead of the content intent specified
8802 * at the level of Notification.
8803 */
8804 public TvExtender setContentIntent(PendingIntent intent) {
8805 mContentIntent = intent;
8806 return this;
8807 }
8808
8809 /**
8810 * Returns the TV-specific content intent. If this method returns null, the
8811 * main content intent on the notification should be used.
8812 *
8813 * @see {@link Notification#contentIntent}
8814 */
8815 public PendingIntent getContentIntent() {
8816 return mContentIntent;
8817 }
8818
8819 /**
8820 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8821 * by the user on TV. If provided, it is used instead of the delete intent specified
8822 * at the level of Notification.
8823 */
8824 public TvExtender setDeleteIntent(PendingIntent intent) {
8825 mDeleteIntent = intent;
8826 return this;
8827 }
8828
8829 /**
8830 * Returns the TV-specific delete intent. If this method returns null, the
8831 * main delete intent on the notification should be used.
8832 *
8833 * @see {@link Notification#deleteIntent}
8834 */
8835 public PendingIntent getDeleteIntent() {
8836 return mDeleteIntent;
8837 }
8838 }
8839
8840 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008841 * Get an array of Notification objects from a parcelable array bundle field.
8842 * Update the bundle to have a typed array so fetches in the future don't need
8843 * to do an array copy.
8844 */
8845 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8846 Parcelable[] array = bundle.getParcelableArray(key);
8847 if (array instanceof Notification[] || array == null) {
8848 return (Notification[]) array;
8849 }
8850 Notification[] typedArray = Arrays.copyOf(array, array.length,
8851 Notification[].class);
8852 bundle.putParcelableArray(key, typedArray);
8853 return typedArray;
8854 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008855
8856 private static class BuilderRemoteViews extends RemoteViews {
8857 public BuilderRemoteViews(Parcel parcel) {
8858 super(parcel);
8859 }
8860
Kenny Guy77320062014-08-27 21:37:15 +01008861 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8862 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008863 }
8864
8865 @Override
8866 public BuilderRemoteViews clone() {
8867 Parcel p = Parcel.obtain();
8868 writeToParcel(p, 0);
8869 p.setDataPosition(0);
8870 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8871 p.recycle();
8872 return brv;
8873 }
8874 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008875
8876 private static class StandardTemplateParams {
8877 boolean hasProgress = true;
8878 boolean ambient = false;
8879 CharSequence title;
8880 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08008881 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07008882 boolean hideLargeIcon;
8883 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008884
8885 final StandardTemplateParams reset() {
8886 hasProgress = true;
8887 ambient = false;
8888 title = null;
8889 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08008890 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008891 return this;
8892 }
8893
8894 final StandardTemplateParams hasProgress(boolean hasProgress) {
8895 this.hasProgress = hasProgress;
8896 return this;
8897 }
8898
8899 final StandardTemplateParams title(CharSequence title) {
8900 this.title = title;
8901 return this;
8902 }
8903
8904 final StandardTemplateParams text(CharSequence text) {
8905 this.text = text;
8906 return this;
8907 }
8908
Selim Cinekafeed292017-12-12 17:32:44 -08008909 final StandardTemplateParams headerTextSecondary(CharSequence text) {
8910 this.headerTextSecondary = text;
8911 return this;
8912 }
8913
Selim Cinek88188f22017-09-19 16:46:56 -07008914 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
8915 this.alwaysShowReply = alwaysShowReply;
8916 return this;
8917 }
8918
8919 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
8920 this.hideLargeIcon = hideLargeIcon;
8921 return this;
8922 }
8923
Adrian Roos70d7aa32017-01-11 15:39:06 -08008924 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008925 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008926 this.ambient = ambient;
8927 return this;
8928 }
8929
8930 final StandardTemplateParams fillTextsFrom(Builder b) {
8931 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008932 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8933
8934 // Big text notifications should contain their content when viewed in ambient mode.
8935 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8936 if (!ambient || TextUtils.isEmpty(text)) {
8937 text = extras.getCharSequence(EXTRA_TEXT);
8938 }
8939 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008940 return this;
8941 }
8942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943}