blob: ebfba837f32e1db17f6cc3ccc37ea988c035c745 [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}
Alex Hillsfc737de2016-03-23 17:33:02 -04001074 */
1075 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1076
1077 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001078 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001079 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001080 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001081 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001082
1083 /**
1084 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1085 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001086 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1087 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001088 */
1089 public static final String EXTRA_MESSAGES = "android.messages";
1090
1091 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001092 * {@link #extras} key: an array of
1093 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1094 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1095 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1096 * array of bundles.
1097 */
1098 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1099
1100 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001101 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1102 * represents a group conversation.
1103 */
1104 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1105
1106 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001107 * {@link #extras} key: whether the notification should be colorized as
1108 * supplied to {@link Builder#setColorized(boolean)}}.
1109 */
1110 public static final String EXTRA_COLORIZED = "android.colorized";
1111
1112 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001113 * @hide
1114 */
1115 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1116
Selim Cinek247fa012016-02-18 09:50:48 -08001117 /**
1118 * @hide
1119 */
1120 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1121
Shane Brennan472a3b32016-12-12 15:28:10 -08001122 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001123 * @hide
1124 */
1125 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1126
1127 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001128 * {@link #extras} key: the audio contents of this notification.
1129 *
1130 * This is for use when rendering the notification on an audio-focused interface;
1131 * the audio contents are a complete sound sample that contains the contents/body of the
1132 * notification. This may be used in substitute of a Text-to-Speech reading of the
1133 * notification. For example if the notification represents a voice message this should point
1134 * to the audio of that message.
1135 *
1136 * The data stored under this key should be a String representation of a Uri that contains the
1137 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1138 *
1139 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1140 * has a field for holding data URI. That field can be used for audio.
1141 * See {@code Message#setData}.
1142 *
1143 * Example usage:
1144 * <pre>
1145 * {@code
1146 * Notification.Builder myBuilder = (build your Notification as normal);
1147 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1148 * }
1149 * </pre>
1150 */
1151 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1152
Dan Sandler80eaa592016-04-14 23:34:54 -04001153 /** @hide */
1154 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001155 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001156 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1157
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001158 /**
1159 * This is set on the notification shown by the activity manager about all apps
1160 * running in the background. It indicates that the notification should be shown
1161 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1162 * notification currently visible to the user. This is a string array of all
1163 * package names of the apps.
1164 * @hide
1165 */
1166 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1167
Dan Sandlerd63f9322015-05-06 15:18:49 -04001168 private Icon mSmallIcon;
1169 private Icon mLargeIcon;
1170
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001171 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001172 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001173
Julia Reynolds13d898c2017-02-02 12:22:05 -05001174 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001175 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001176
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001177 /** @hide */
1178 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1179 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1180 })
1181 @Retention(RetentionPolicy.SOURCE)
1182 public @interface GroupAlertBehavior {}
1183
1184 /**
1185 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1186 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1187 * notification will not be muted when it is in a group.
1188 */
1189 public static final int GROUP_ALERT_ALL = 0;
1190
1191 /**
1192 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1193 * notification in a group should be silenced (no sound or vibration) even if they are posted
1194 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001195 * mute this notification if this notification is a group child. This must be applied to all
1196 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001197 *
1198 * <p> For example, you might want to use this constant if you post a number of children
1199 * notifications at once (say, after a periodic sync), and only need to notify the user
1200 * audibly once.
1201 */
1202 public static final int GROUP_ALERT_SUMMARY = 1;
1203
1204 /**
1205 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1206 * notification in a group should be silenced (no sound or vibration) even if they are
1207 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1208 * to mute this notification if this notification is a group summary.
1209 *
1210 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001211 * in your group have content and the summary is only used to visually group notifications
1212 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001213 */
1214 public static final int GROUP_ALERT_CHILDREN = 2;
1215
Julia Reynolds2f431e22017-06-07 14:12:09 +00001216 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001217
Julia Reynolds13d898c2017-02-02 12:22:05 -05001218 /**
1219 * If this notification is being shown as a badge, always show as a number.
1220 */
1221 public static final int BADGE_ICON_NONE = 0;
1222
1223 /**
1224 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1225 * represent this notification.
1226 */
1227 public static final int BADGE_ICON_SMALL = 1;
1228
1229 /**
1230 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1231 * represent this notification.
1232 */
1233 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001234 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001235
Chris Wren51c75102013-07-16 20:49:17 -04001236 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001237 * Structure to encapsulate a named action that can be shown as part of this notification.
1238 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1239 * selected by the user.
1240 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001241 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1242 * or {@link Notification.Builder#addAction(Notification.Action)}
1243 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001244 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001245 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001246 /**
1247 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1248 * {@link RemoteInput}s.
1249 *
1250 * This is intended for {@link RemoteInput}s that only accept data, meaning
1251 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1252 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1253 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1254 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1255 *
1256 * You can test if a RemoteInput matches these constraints using
1257 * {@link RemoteInput#isDataOnly}.
1258 */
1259 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1260
Griff Hazen959591e2014-05-15 22:26:18 -07001261 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001262 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001263 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001264 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001265
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001266 /**
1267 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001268 *
1269 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001270 */
Dan Sandler86647982015-05-13 23:41:13 -04001271 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001272 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001273
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001274 /**
1275 * Title of the action.
1276 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001277 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001278
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001279 /**
1280 * Intent to send when the user invokes this action. May be null, in which case the action
1281 * may be rendered in a disabled presentation by the system UI.
1282 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001283 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001284
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001285 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001286 if (in.readInt() != 0) {
1287 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001288 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1289 icon = mIcon.getResId();
1290 }
Dan Sandler86647982015-05-13 23:41:13 -04001291 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001292 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1293 if (in.readInt() == 1) {
1294 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1295 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001296 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001297 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001298 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001299 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001300
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001301 /**
Dan Sandler86647982015-05-13 23:41:13 -04001302 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001303 */
Dan Sandler86647982015-05-13 23:41:13 -04001304 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001305 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001306 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001307 }
1308
Adrian Roos7af53622016-10-12 13:44:05 -07001309 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001310 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001311 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001312 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001313 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1314 this.icon = icon.getResId();
1315 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001316 this.title = title;
1317 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001318 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001319 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001320 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001321 }
1322
1323 /**
Dan Sandler86647982015-05-13 23:41:13 -04001324 * Return an icon representing the action.
1325 */
1326 public Icon getIcon() {
1327 if (mIcon == null && icon != 0) {
1328 // you snuck an icon in here without using the builder; let's try to keep it
1329 mIcon = Icon.createWithResource("", icon);
1330 }
1331 return mIcon;
1332 }
1333
1334 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001335 * Get additional metadata carried around with this Action.
1336 */
1337 public Bundle getExtras() {
1338 return mExtras;
1339 }
1340
1341 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001342 * Return whether the platform should automatically generate possible replies for this
1343 * {@link Action}
1344 */
1345 public boolean getAllowGeneratedReplies() {
1346 return mAllowGeneratedReplies;
1347 }
1348
1349 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001350 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001351 * May return null if no remote inputs were added. Only returns inputs which accept
1352 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001353 */
1354 public RemoteInput[] getRemoteInputs() {
1355 return mRemoteInputs;
1356 }
1357
1358 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001359 * Get the list of inputs to be collected from the user that ONLY accept data when this
1360 * action is sent. These remote inputs are guaranteed to return true on a call to
1361 * {@link RemoteInput#isDataOnly}.
1362 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001363 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001364 *
1365 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1366 * of non-textual RemoteInputs do not access these remote inputs.
1367 */
1368 public RemoteInput[] getDataOnlyRemoteInputs() {
1369 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1370 }
1371
1372 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001373 * Builder class for {@link Action} objects.
1374 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001375 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001376 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001377 private final CharSequence mTitle;
1378 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001379 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001380 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001381 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001382
1383 /**
1384 * Construct a new builder for {@link Action} object.
1385 * @param icon icon to show for this action
1386 * @param title the title of the action
1387 * @param intent the {@link PendingIntent} to fire when users trigger this action
1388 */
Dan Sandler86647982015-05-13 23:41:13 -04001389 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001390 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001391 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001392 }
1393
1394 /**
1395 * Construct a new builder for {@link Action} object.
1396 * @param icon icon to show for this action
1397 * @param title the title of the action
1398 * @param intent the {@link PendingIntent} to fire when users trigger this action
1399 */
1400 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001401 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001402 }
1403
1404 /**
1405 * Construct a new builder for {@link Action} object using the fields from an
1406 * {@link Action}.
1407 * @param action the action to read fields from.
1408 */
1409 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001410 this(action.getIcon(), action.title, action.actionIntent,
1411 new Bundle(action.mExtras), action.getRemoteInputs(),
1412 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001413 }
1414
Dan Sandler86647982015-05-13 23:41:13 -04001415 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001416 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001417 mIcon = icon;
1418 mTitle = title;
1419 mIntent = intent;
1420 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001421 if (remoteInputs != null) {
1422 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1423 Collections.addAll(mRemoteInputs, remoteInputs);
1424 }
Adrian Roos7af53622016-10-12 13:44:05 -07001425 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001426 }
1427
1428 /**
1429 * Merge additional metadata into this builder.
1430 *
1431 * <p>Values within the Bundle will replace existing extras values in this Builder.
1432 *
1433 * @see Notification.Action#extras
1434 */
1435 public Builder addExtras(Bundle extras) {
1436 if (extras != null) {
1437 mExtras.putAll(extras);
1438 }
1439 return this;
1440 }
1441
1442 /**
1443 * Get the metadata Bundle used by this Builder.
1444 *
1445 * <p>The returned Bundle is shared with this Builder.
1446 */
1447 public Bundle getExtras() {
1448 return mExtras;
1449 }
1450
1451 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001452 * Add an input to be collected from the user when this action is sent.
1453 * Response values can be retrieved from the fired intent by using the
1454 * {@link RemoteInput#getResultsFromIntent} function.
1455 * @param remoteInput a {@link RemoteInput} to add to the action
1456 * @return this object for method chaining
1457 */
1458 public Builder addRemoteInput(RemoteInput remoteInput) {
1459 if (mRemoteInputs == null) {
1460 mRemoteInputs = new ArrayList<RemoteInput>();
1461 }
1462 mRemoteInputs.add(remoteInput);
1463 return this;
1464 }
1465
1466 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001467 * Set whether the platform should automatically generate possible replies to add to
1468 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1469 * {@link RemoteInput}, this has no effect.
1470 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1471 * otherwise
1472 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001473 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001474 */
1475 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1476 mAllowGeneratedReplies = allowGeneratedReplies;
1477 return this;
1478 }
1479
1480 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001481 * Apply an extender to this action builder. Extenders may be used to add
1482 * metadata or change options on this builder.
1483 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001484 public Builder extend(Extender extender) {
1485 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001486 return this;
1487 }
1488
1489 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001490 * Combine all of the options that have been set and return a new {@link Action}
1491 * object.
1492 * @return the built action
1493 */
1494 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001495 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1496 RemoteInput[] previousDataInputs =
1497 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001498 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001499 for (RemoteInput input : previousDataInputs) {
1500 dataOnlyInputs.add(input);
1501 }
1502 }
1503 List<RemoteInput> textInputs = new ArrayList<>();
1504 if (mRemoteInputs != null) {
1505 for (RemoteInput input : mRemoteInputs) {
1506 if (input.isDataOnly()) {
1507 dataOnlyInputs.add(input);
1508 } else {
1509 textInputs.add(input);
1510 }
1511 }
1512 }
1513 if (!dataOnlyInputs.isEmpty()) {
1514 RemoteInput[] dataInputsArr =
1515 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1516 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1517 }
1518 RemoteInput[] textInputsArr = textInputs.isEmpty()
1519 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1520 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001521 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001522 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001523 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001524
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001525 @Override
1526 public Action clone() {
1527 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001528 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001529 title,
1530 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001531 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001532 getRemoteInputs(),
1533 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001534 }
1535 @Override
1536 public int describeContents() {
1537 return 0;
1538 }
1539 @Override
1540 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001541 final Icon ic = getIcon();
1542 if (ic != null) {
1543 out.writeInt(1);
1544 ic.writeToParcel(out, 0);
1545 } else {
1546 out.writeInt(0);
1547 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001548 TextUtils.writeToParcel(title, out, flags);
1549 if (actionIntent != null) {
1550 out.writeInt(1);
1551 actionIntent.writeToParcel(out, flags);
1552 } else {
1553 out.writeInt(0);
1554 }
Griff Hazen959591e2014-05-15 22:26:18 -07001555 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001556 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001557 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001558 }
Griff Hazen959591e2014-05-15 22:26:18 -07001559 public static final Parcelable.Creator<Action> CREATOR =
1560 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001561 public Action createFromParcel(Parcel in) {
1562 return new Action(in);
1563 }
1564 public Action[] newArray(int size) {
1565 return new Action[size];
1566 }
1567 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001568
1569 /**
1570 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1571 * metadata or change options on an action builder.
1572 */
1573 public interface Extender {
1574 /**
1575 * Apply this extender to a notification action builder.
1576 * @param builder the builder to be modified.
1577 * @return the build object for chaining.
1578 */
1579 public Builder extend(Builder builder);
1580 }
1581
1582 /**
1583 * Wearable extender for notification actions. To add extensions to an action,
1584 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1585 * the {@code WearableExtender()} constructor and apply it to a
1586 * {@link android.app.Notification.Action.Builder} using
1587 * {@link android.app.Notification.Action.Builder#extend}.
1588 *
1589 * <pre class="prettyprint">
1590 * Notification.Action action = new Notification.Action.Builder(
1591 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001592 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001593 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001594 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001595 */
1596 public static final class WearableExtender implements Extender {
1597 /** Notification action extra which contains wearable extensions */
1598 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1599
Pete Gastaf6781d2014-10-07 15:17:05 -04001600 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001601 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001602 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1603 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1604 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001605
1606 // Flags bitwise-ored to mFlags
1607 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001608 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001609 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001610
1611 // Default value for flags integer
1612 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1613
1614 private int mFlags = DEFAULT_FLAGS;
1615
Pete Gastaf6781d2014-10-07 15:17:05 -04001616 private CharSequence mInProgressLabel;
1617 private CharSequence mConfirmLabel;
1618 private CharSequence mCancelLabel;
1619
Griff Hazen61a9e862014-05-22 16:05:19 -07001620 /**
1621 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1622 * options.
1623 */
1624 public WearableExtender() {
1625 }
1626
1627 /**
1628 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1629 * wearable options present in an existing notification action.
1630 * @param action the notification action to inspect.
1631 */
1632 public WearableExtender(Action action) {
1633 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1634 if (wearableBundle != null) {
1635 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001636 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1637 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1638 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001639 }
1640 }
1641
1642 /**
1643 * Apply wearable extensions to a notification action that is being built. This is
1644 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1645 * method of {@link android.app.Notification.Action.Builder}.
1646 */
1647 @Override
1648 public Action.Builder extend(Action.Builder builder) {
1649 Bundle wearableBundle = new Bundle();
1650
1651 if (mFlags != DEFAULT_FLAGS) {
1652 wearableBundle.putInt(KEY_FLAGS, mFlags);
1653 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001654 if (mInProgressLabel != null) {
1655 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1656 }
1657 if (mConfirmLabel != null) {
1658 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1659 }
1660 if (mCancelLabel != null) {
1661 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1662 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001663
1664 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1665 return builder;
1666 }
1667
1668 @Override
1669 public WearableExtender clone() {
1670 WearableExtender that = new WearableExtender();
1671 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001672 that.mInProgressLabel = this.mInProgressLabel;
1673 that.mConfirmLabel = this.mConfirmLabel;
1674 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001675 return that;
1676 }
1677
1678 /**
1679 * Set whether this action is available when the wearable device is not connected to
1680 * a companion device. The user can still trigger this action when the wearable device is
1681 * offline, but a visual hint will indicate that the action may not be available.
1682 * Defaults to true.
1683 */
1684 public WearableExtender setAvailableOffline(boolean availableOffline) {
1685 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1686 return this;
1687 }
1688
1689 /**
1690 * Get whether this action is available when the wearable device is not connected to
1691 * a companion device. The user can still trigger this action when the wearable device is
1692 * offline, but a visual hint will indicate that the action may not be available.
1693 * Defaults to true.
1694 */
1695 public boolean isAvailableOffline() {
1696 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1697 }
1698
1699 private void setFlag(int mask, boolean value) {
1700 if (value) {
1701 mFlags |= mask;
1702 } else {
1703 mFlags &= ~mask;
1704 }
1705 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001706
1707 /**
1708 * Set a label to display while the wearable is preparing to automatically execute the
1709 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1710 *
1711 * @param label the label to display while the action is being prepared to execute
1712 * @return this object for method chaining
1713 */
1714 public WearableExtender setInProgressLabel(CharSequence label) {
1715 mInProgressLabel = label;
1716 return this;
1717 }
1718
1719 /**
1720 * Get the label to display while the wearable is preparing to automatically execute
1721 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1722 *
1723 * @return the label to display while the action is being prepared to execute
1724 */
1725 public CharSequence getInProgressLabel() {
1726 return mInProgressLabel;
1727 }
1728
1729 /**
1730 * Set a label to display to confirm that the action should be executed.
1731 * This is usually an imperative verb like "Send".
1732 *
1733 * @param label the label to confirm the action should be executed
1734 * @return this object for method chaining
1735 */
1736 public WearableExtender setConfirmLabel(CharSequence label) {
1737 mConfirmLabel = label;
1738 return this;
1739 }
1740
1741 /**
1742 * Get the label to display to confirm that the action should be executed.
1743 * This is usually an imperative verb like "Send".
1744 *
1745 * @return the label to confirm the action should be executed
1746 */
1747 public CharSequence getConfirmLabel() {
1748 return mConfirmLabel;
1749 }
1750
1751 /**
1752 * Set a label to display to cancel the action.
1753 * This is usually an imperative verb, like "Cancel".
1754 *
1755 * @param label the label to display to cancel the action
1756 * @return this object for method chaining
1757 */
1758 public WearableExtender setCancelLabel(CharSequence label) {
1759 mCancelLabel = label;
1760 return this;
1761 }
1762
1763 /**
1764 * Get the label to display to cancel the action.
1765 * This is usually an imperative verb like "Cancel".
1766 *
1767 * @return the label to display to cancel the action
1768 */
1769 public CharSequence getCancelLabel() {
1770 return mCancelLabel;
1771 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001772
1773 /**
1774 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1775 * platform that it can generate the appropriate transitions.
1776 * @param hintLaunchesActivity {@code true} if the content intent will launch
1777 * an activity and transitions should be generated, false otherwise.
1778 * @return this object for method chaining
1779 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001780 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001781 boolean hintLaunchesActivity) {
1782 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1783 return this;
1784 }
1785
1786 /**
1787 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1788 * platform that it can generate the appropriate transitions
1789 * @return {@code true} if the content intent will launch an activity and transitions
1790 * should be generated, false otherwise. The default value is {@code false} if this was
1791 * never set.
1792 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001793 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001794 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1795 }
Alex Hills9f087612016-06-07 09:08:59 -04001796
1797 /**
1798 * Set a hint that this Action should be displayed inline.
1799 *
1800 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1801 * otherwise
1802 * @return this object for method chaining
1803 */
1804 public WearableExtender setHintDisplayActionInline(
1805 boolean hintDisplayInline) {
1806 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1807 return this;
1808 }
1809
1810 /**
1811 * Get a hint that this Action should be displayed inline.
1812 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001813 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001814 * otherwise. The default value is {@code false} if this was never set.
1815 */
1816 public boolean getHintDisplayActionInline() {
1817 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1818 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001819 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001820 }
1821
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001822 /**
1823 * Array of all {@link Action} structures attached to this notification by
1824 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1825 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1826 * interface for invoking actions.
1827 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001828 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001829
1830 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001831 * Replacement version of this notification whose content will be shown
1832 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1833 * and {@link #VISIBILITY_PUBLIC}.
1834 */
1835 public Notification publicVersion;
1836
1837 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001838 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001839 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 */
1841 public Notification()
1842 {
1843 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001844 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001845 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847
1848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 * @hide
1850 */
1851 public Notification(Context context, int icon, CharSequence tickerText, long when,
1852 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1853 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001854 new Builder(context)
1855 .setWhen(when)
1856 .setSmallIcon(icon)
1857 .setTicker(tickerText)
1858 .setContentTitle(contentTitle)
1859 .setContentText(contentText)
1860 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1861 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863
1864 /**
1865 * Constructs a Notification object with the information needed to
1866 * have a status bar icon without the standard expanded view.
1867 *
1868 * @param icon The resource id of the icon to put in the status bar.
1869 * @param tickerText The text that flows by in the status bar when the notification first
1870 * activates.
1871 * @param when The time to show in the time field. In the System.currentTimeMillis
1872 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001873 *
1874 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001876 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 public Notification(int icon, CharSequence tickerText, long when)
1878 {
1879 this.icon = icon;
1880 this.tickerText = tickerText;
1881 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001882 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884
1885 /**
1886 * Unflatten the notification from a parcel.
1887 */
Svet Ganovddb94882016-06-23 19:55:24 -07001888 @SuppressWarnings("unchecked")
1889 public Notification(Parcel parcel) {
1890 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1891 // intents in extras are always written as the last entry.
1892 readFromParcelImpl(parcel);
1893 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001894 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001895 }
1896
1897 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 {
1899 int version = parcel.readInt();
1900
Adrian Roosfb921842017-10-26 14:49:56 +02001901 mWhitelistToken = parcel.readStrongBinder();
1902 if (mWhitelistToken == null) {
1903 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07001904 }
1905 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02001906 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001909 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001910 if (parcel.readInt() != 0) {
1911 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001912 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1913 icon = mSmallIcon.getResId();
1914 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 number = parcel.readInt();
1917 if (parcel.readInt() != 0) {
1918 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1919 }
1920 if (parcel.readInt() != 0) {
1921 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1922 }
1923 if (parcel.readInt() != 0) {
1924 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1925 }
1926 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001927 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001928 }
1929 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1931 }
Joe Onorato561d3852010-11-20 18:09:34 -08001932 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001933 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 defaults = parcel.readInt();
1936 flags = parcel.readInt();
1937 if (parcel.readInt() != 0) {
1938 sound = Uri.CREATOR.createFromParcel(parcel);
1939 }
1940
1941 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001942 if (parcel.readInt() != 0) {
1943 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 vibrate = parcel.createLongArray();
1946 ledARGB = parcel.readInt();
1947 ledOnMS = parcel.readInt();
1948 ledOffMS = parcel.readInt();
1949 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001950
1951 if (parcel.readInt() != 0) {
1952 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1953 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001954
1955 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001956
John Spurlockfd7f1e02014-03-18 16:41:57 -04001957 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001958
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001959 mGroupKey = parcel.readString();
1960
1961 mSortKey = parcel.readString();
1962
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001963 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01001964 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001965
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001966 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1967
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001968 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001969 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1970 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001971
Chris Wren8fd39ec2014-02-27 17:43:26 -05001972 if (parcel.readInt() != 0) {
1973 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1974 }
1975
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001976 visibility = parcel.readInt();
1977
1978 if (parcel.readInt() != 0) {
1979 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1980 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001981
1982 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001983
1984 if (parcel.readInt() != 0) {
1985 mChannelId = parcel.readString();
1986 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001987 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001988
1989 if (parcel.readInt() != 0) {
1990 mShortcutId = parcel.readString();
1991 }
1992
1993 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001994
1995 if (parcel.readInt() != 0) {
1996 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1997 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001998
1999 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
Andy Stadler110988c2010-12-03 14:29:16 -08002002 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002003 public Notification clone() {
2004 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002005 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002006 return that;
2007 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002008
Daniel Sandler1a497d32013-04-18 14:52:45 -04002009 /**
2010 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2011 * of this into that.
2012 * @hide
2013 */
2014 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002015 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002016 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002017 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002018 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002019 that.number = this.number;
2020
2021 // PendingIntents are global, so there's no reason (or way) to clone them.
2022 that.contentIntent = this.contentIntent;
2023 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002024 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002025
2026 if (this.tickerText != null) {
2027 that.tickerText = this.tickerText.toString();
2028 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002029 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002030 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002031 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002032 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002033 that.contentView = this.contentView.clone();
2034 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002035 if (heavy && this.mLargeIcon != null) {
2036 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002037 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002038 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002039 that.sound = this.sound; // android.net.Uri is immutable
2040 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002041 if (this.audioAttributes != null) {
2042 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2043 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002044
2045 final long[] vibrate = this.vibrate;
2046 if (vibrate != null) {
2047 final int N = vibrate.length;
2048 final long[] vib = that.vibrate = new long[N];
2049 System.arraycopy(vibrate, 0, vib, 0, N);
2050 }
2051
2052 that.ledARGB = this.ledARGB;
2053 that.ledOnMS = this.ledOnMS;
2054 that.ledOffMS = this.ledOffMS;
2055 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002056
Joe Onorato18e69df2010-05-17 22:26:12 -07002057 that.flags = this.flags;
2058
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002059 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002060
John Spurlockfd7f1e02014-03-18 16:41:57 -04002061 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002062
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002063 that.mGroupKey = this.mGroupKey;
2064
2065 that.mSortKey = this.mSortKey;
2066
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002067 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002068 try {
2069 that.extras = new Bundle(this.extras);
2070 // will unparcel
2071 that.extras.size();
2072 } catch (BadParcelableException e) {
2073 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2074 that.extras = null;
2075 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002076 }
2077
Felipe Lemedd85da62016-06-28 11:29:54 -07002078 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2079 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002080 }
2081
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002082 if (this.actions != null) {
2083 that.actions = new Action[this.actions.length];
2084 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002085 if ( this.actions[i] != null) {
2086 that.actions[i] = this.actions[i].clone();
2087 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002088 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002089 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002090
Daniel Sandler1a497d32013-04-18 14:52:45 -04002091 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002092 that.bigContentView = this.bigContentView.clone();
2093 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002094
Chris Wren8fd39ec2014-02-27 17:43:26 -05002095 if (heavy && this.headsUpContentView != null) {
2096 that.headsUpContentView = this.headsUpContentView.clone();
2097 }
2098
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002099 that.visibility = this.visibility;
2100
2101 if (this.publicVersion != null) {
2102 that.publicVersion = new Notification();
2103 this.publicVersion.cloneInto(that.publicVersion, heavy);
2104 }
2105
Dan Sandler26e81cf2014-05-06 10:01:27 -04002106 that.color = this.color;
2107
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002108 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002109 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002110 that.mShortcutId = this.mShortcutId;
2111 that.mBadgeIcon = this.mBadgeIcon;
2112 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002113 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002114
Daniel Sandler1a497d32013-04-18 14:52:45 -04002115 if (!heavy) {
2116 that.lightenPayload(); // will clean out extras
2117 }
2118 }
2119
2120 /**
2121 * Removes heavyweight parts of the Notification object for archival or for sending to
2122 * listeners when the full contents are not necessary.
2123 * @hide
2124 */
2125 public final void lightenPayload() {
2126 tickerView = null;
2127 contentView = null;
2128 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002129 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002130 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002131 if (extras != null && !extras.isEmpty()) {
2132 final Set<String> keyset = extras.keySet();
2133 final int N = keyset.size();
2134 final String[] keys = keyset.toArray(new String[N]);
2135 for (int i=0; i<N; i++) {
2136 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002137 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2138 continue;
2139 }
Dan Sandler50128532015-12-08 15:42:41 -05002140 final Object obj = extras.get(key);
2141 if (obj != null &&
2142 ( obj instanceof Parcelable
2143 || obj instanceof Parcelable[]
2144 || obj instanceof SparseArray
2145 || obj instanceof ArrayList)) {
2146 extras.remove(key);
2147 }
2148 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002149 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002150 }
2151
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002152 /**
2153 * Make sure this CharSequence is safe to put into a bundle, which basically
2154 * means it had better not be some custom Parcelable implementation.
2155 * @hide
2156 */
2157 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002158 if (cs == null) return cs;
2159 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2160 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2161 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002162 if (cs instanceof Parcelable) {
2163 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2164 + " instance is a custom Parcelable and not allowed in Notification");
2165 return cs.toString();
2166 }
Selim Cinek60a54252016-02-26 17:03:25 -08002167 return removeTextSizeSpans(cs);
2168 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002169
Selim Cinek60a54252016-02-26 17:03:25 -08002170 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2171 if (charSequence instanceof Spanned) {
2172 Spanned ss = (Spanned) charSequence;
2173 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2174 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2175 for (Object span : spans) {
2176 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002177 if (resultSpan instanceof CharacterStyle) {
2178 resultSpan = ((CharacterStyle) span).getUnderlying();
2179 }
2180 if (resultSpan instanceof TextAppearanceSpan) {
2181 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002182 resultSpan = new TextAppearanceSpan(
2183 originalSpan.getFamily(),
2184 originalSpan.getTextStyle(),
2185 -1,
2186 originalSpan.getTextColor(),
2187 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002188 } else if (resultSpan instanceof RelativeSizeSpan
2189 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002190 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002191 } else {
2192 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002193 }
2194 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2195 ss.getSpanFlags(span));
2196 }
2197 return builder;
2198 }
2199 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002200 }
2201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 public int describeContents() {
2203 return 0;
2204 }
2205
2206 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002207 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 */
Svet Ganovddb94882016-06-23 19:55:24 -07002209 public void writeToParcel(Parcel parcel, int flags) {
2210 // We need to mark all pending intents getting into the notification
2211 // system as being put there to later allow the notification ranker
2212 // to launch them and by doing so add the app to the battery saver white
2213 // list for a short period of time. The problem is that the system
2214 // cannot look into the extras as there may be parcelables there that
2215 // the platform does not know how to handle. To go around that we have
2216 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002217 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002218 if (collectPendingIntents) {
2219 PendingIntent.setOnMarshaledListener(
2220 (PendingIntent intent, Parcel out, int outFlags) -> {
2221 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002222 if (allPendingIntents == null) {
2223 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002224 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002225 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002226 }
2227 });
2228 }
2229 try {
2230 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002231 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002232 writeToParcelImpl(parcel, flags);
2233 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002234 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002235 } finally {
2236 if (collectPendingIntents) {
2237 PendingIntent.setOnMarshaledListener(null);
2238 }
2239 }
2240 }
2241
2242 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 parcel.writeInt(1);
2244
Adrian Roosfb921842017-10-26 14:49:56 +02002245 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002247 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002248 if (mSmallIcon == null && icon != 0) {
2249 // you snuck an icon in here without using the builder; let's try to keep it
2250 mSmallIcon = Icon.createWithResource("", icon);
2251 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002252 if (mSmallIcon != null) {
2253 parcel.writeInt(1);
2254 mSmallIcon.writeToParcel(parcel, 0);
2255 } else {
2256 parcel.writeInt(0);
2257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 parcel.writeInt(number);
2259 if (contentIntent != null) {
2260 parcel.writeInt(1);
2261 contentIntent.writeToParcel(parcel, 0);
2262 } else {
2263 parcel.writeInt(0);
2264 }
2265 if (deleteIntent != null) {
2266 parcel.writeInt(1);
2267 deleteIntent.writeToParcel(parcel, 0);
2268 } else {
2269 parcel.writeInt(0);
2270 }
2271 if (tickerText != null) {
2272 parcel.writeInt(1);
2273 TextUtils.writeToParcel(tickerText, parcel, flags);
2274 } else {
2275 parcel.writeInt(0);
2276 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002277 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002278 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002279 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002280 } else {
2281 parcel.writeInt(0);
2282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 if (contentView != null) {
2284 parcel.writeInt(1);
2285 contentView.writeToParcel(parcel, 0);
2286 } else {
2287 parcel.writeInt(0);
2288 }
Selim Cinek279fa862016-06-14 10:57:25 -07002289 if (mLargeIcon == null && largeIcon != null) {
2290 // you snuck an icon in here without using the builder; let's try to keep it
2291 mLargeIcon = Icon.createWithBitmap(largeIcon);
2292 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002293 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002294 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002295 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002296 } else {
2297 parcel.writeInt(0);
2298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
2300 parcel.writeInt(defaults);
2301 parcel.writeInt(this.flags);
2302
2303 if (sound != null) {
2304 parcel.writeInt(1);
2305 sound.writeToParcel(parcel, 0);
2306 } else {
2307 parcel.writeInt(0);
2308 }
2309 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002310
2311 if (audioAttributes != null) {
2312 parcel.writeInt(1);
2313 audioAttributes.writeToParcel(parcel, 0);
2314 } else {
2315 parcel.writeInt(0);
2316 }
2317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 parcel.writeLongArray(vibrate);
2319 parcel.writeInt(ledARGB);
2320 parcel.writeInt(ledOnMS);
2321 parcel.writeInt(ledOffMS);
2322 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002323
2324 if (fullScreenIntent != null) {
2325 parcel.writeInt(1);
2326 fullScreenIntent.writeToParcel(parcel, 0);
2327 } else {
2328 parcel.writeInt(0);
2329 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002330
2331 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002332
John Spurlockfd7f1e02014-03-18 16:41:57 -04002333 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002334
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002335 parcel.writeString(mGroupKey);
2336
2337 parcel.writeString(mSortKey);
2338
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002339 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002340
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002341 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002342
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002343 if (bigContentView != null) {
2344 parcel.writeInt(1);
2345 bigContentView.writeToParcel(parcel, 0);
2346 } else {
2347 parcel.writeInt(0);
2348 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002349
Chris Wren8fd39ec2014-02-27 17:43:26 -05002350 if (headsUpContentView != null) {
2351 parcel.writeInt(1);
2352 headsUpContentView.writeToParcel(parcel, 0);
2353 } else {
2354 parcel.writeInt(0);
2355 }
2356
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002357 parcel.writeInt(visibility);
2358
2359 if (publicVersion != null) {
2360 parcel.writeInt(1);
2361 publicVersion.writeToParcel(parcel, 0);
2362 } else {
2363 parcel.writeInt(0);
2364 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002365
2366 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002367
2368 if (mChannelId != null) {
2369 parcel.writeInt(1);
2370 parcel.writeString(mChannelId);
2371 } else {
2372 parcel.writeInt(0);
2373 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002374 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002375
2376 if (mShortcutId != null) {
2377 parcel.writeInt(1);
2378 parcel.writeString(mShortcutId);
2379 } else {
2380 parcel.writeInt(0);
2381 }
2382
2383 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002384
2385 if (mSettingsText != null) {
2386 parcel.writeInt(1);
2387 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2388 } else {
2389 parcel.writeInt(0);
2390 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002391
2392 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394
2395 /**
2396 * Parcelable.Creator that instantiates Notification objects
2397 */
2398 public static final Parcelable.Creator<Notification> CREATOR
2399 = new Parcelable.Creator<Notification>()
2400 {
2401 public Notification createFromParcel(Parcel parcel)
2402 {
2403 return new Notification(parcel);
2404 }
2405
2406 public Notification[] newArray(int size)
2407 {
2408 return new Notification[size];
2409 }
2410 };
2411
2412 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002413 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2414 * <p>
2415 * For backwards compatibility {@code extras} holds some references to "real" member data such
2416 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2417 * fine as long as the object stays in one process.
2418 * <p>
2419 * However, once the notification goes into a parcel each reference gets marshalled separately,
2420 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2421 */
2422 private void fixDuplicateExtras() {
2423 if (extras != null) {
2424 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2425 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2426 }
2427 }
2428
2429 /**
2430 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2431 * separate object, replace it with the field's version to avoid holding duplicate copies.
2432 */
2433 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2434 if (original != null && extras.getParcelable(extraName) != null) {
2435 extras.putParcelable(extraName, original);
2436 }
2437 }
2438
2439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2441 * layout.
2442 *
2443 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2444 * in the view.</p>
2445 * @param context The context for your application / activity.
2446 * @param contentTitle The title that goes in the expanded entry.
2447 * @param contentText The text that goes in the expanded entry.
2448 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2449 * If this is an activity, it must include the
2450 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002451 * that you take care of task management as described in the
2452 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2453 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002454 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002455 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002456 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002458 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 public void setLatestEventInfo(Context context,
2460 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002461 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2462 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2463 new Throwable());
2464 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002465
Selim Cinek4ac6f602016-06-13 15:47:03 -07002466 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2467 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2468 }
2469
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002470 // ensure that any information already set directly is preserved
2471 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002472
2473 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002475 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002478 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002480 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002481
2482 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
2484
Julia Reynoldsda303542015-11-23 14:00:20 -05002485 /**
2486 * @hide
2487 */
2488 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002489 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002490 }
2491
2492 /**
2493 * @hide
2494 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002495 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002496 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002497 }
2498
Yi Jin6b514142017-10-30 14:54:12 -07002499 /**
2500 * @hide
2501 */
2502 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2503 long token = proto.start(fieldId);
2504 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2505 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2506 proto.write(NotificationProto.FLAGS, this.flags);
2507 proto.write(NotificationProto.COLOR, this.color);
2508 proto.write(NotificationProto.CATEGORY, this.category);
2509 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2510 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2511 if (this.actions != null) {
2512 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2513 }
2514 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2515 proto.write(NotificationProto.VISIBILITY, this.visibility);
2516 }
2517 if (publicVersion != null) {
2518 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2519 }
2520 proto.end(token);
2521 }
2522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 @Override
2524 public String toString() {
2525 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002526 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002527 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002528 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002529 sb.append(priority);
2530 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002531 if (contentView != null) {
2532 sb.append(contentView.getPackage());
2533 sb.append("/0x");
2534 sb.append(Integer.toHexString(contentView.getLayoutId()));
2535 } else {
2536 sb.append("null");
2537 }
2538 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002539 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2540 sb.append("default");
2541 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 int N = this.vibrate.length-1;
2543 sb.append("[");
2544 for (int i=0; i<N; i++) {
2545 sb.append(this.vibrate[i]);
2546 sb.append(',');
2547 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002548 if (N != -1) {
2549 sb.append(this.vibrate[N]);
2550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 } else {
2553 sb.append("null");
2554 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002555 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002556 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002558 } else if (this.sound != null) {
2559 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 } else {
2561 sb.append("null");
2562 }
Chris Wren365b6d32015-07-16 10:39:26 -04002563 if (this.tickerText != null) {
2564 sb.append(" tick");
2565 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002568 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002569 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002570 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002571 if (this.category != null) {
2572 sb.append(" category=");
2573 sb.append(this.category);
2574 }
2575 if (this.mGroupKey != null) {
2576 sb.append(" groupKey=");
2577 sb.append(this.mGroupKey);
2578 }
2579 if (this.mSortKey != null) {
2580 sb.append(" sortKey=");
2581 sb.append(this.mSortKey);
2582 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002583 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002584 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002585 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002586 }
2587 sb.append(" vis=");
2588 sb.append(visibilityToString(this.visibility));
2589 if (this.publicVersion != null) {
2590 sb.append(" publicVersion=");
2591 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002592 }
2593 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 return sb.toString();
2595 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002596
Dan Sandler1b718782014-07-18 12:43:45 -04002597 /**
2598 * {@hide}
2599 */
2600 public static String visibilityToString(int vis) {
2601 switch (vis) {
2602 case VISIBILITY_PRIVATE:
2603 return "PRIVATE";
2604 case VISIBILITY_PUBLIC:
2605 return "PUBLIC";
2606 case VISIBILITY_SECRET:
2607 return "SECRET";
2608 default:
2609 return "UNKNOWN(" + String.valueOf(vis) + ")";
2610 }
2611 }
2612
Joe Onoratocb109a02011-01-18 17:57:41 -08002613 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002614 * {@hide}
2615 */
2616 public static String priorityToString(@Priority int pri) {
2617 switch (pri) {
2618 case PRIORITY_MIN:
2619 return "MIN";
2620 case PRIORITY_LOW:
2621 return "LOW";
2622 case PRIORITY_DEFAULT:
2623 return "DEFAULT";
2624 case PRIORITY_HIGH:
2625 return "HIGH";
2626 case PRIORITY_MAX:
2627 return "MAX";
2628 default:
2629 return "UNKNOWN(" + String.valueOf(pri) + ")";
2630 }
2631 }
2632
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002633 /**
2634 * @hide
2635 */
2636 public boolean hasCompletedProgress() {
2637 // not a progress notification; can't be complete
2638 if (!extras.containsKey(EXTRA_PROGRESS)
2639 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2640 return false;
2641 }
2642 // many apps use max 0 for 'indeterminate'; not complete
2643 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2644 return false;
2645 }
2646 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2647 }
2648
Jeff Sharkey000ce802017-04-29 13:13:27 -06002649 /** @removed */
2650 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002651 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002652 return mChannelId;
2653 }
2654
2655 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002656 * Returns the id of the channel this notification posts to.
2657 */
2658 public String getChannelId() {
2659 return mChannelId;
2660 }
2661
Jeff Sharkey000ce802017-04-29 13:13:27 -06002662 /** @removed */
2663 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002664 public long getTimeout() {
2665 return mTimeout;
2666 }
2667
2668 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002669 * Returns the duration from posting after which this notification should be canceled by the
2670 * system, if it's not canceled already.
2671 */
2672 public long getTimeoutAfter() {
2673 return mTimeout;
2674 }
2675
2676 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002677 * Returns what icon should be shown for this notification if it is being displayed in a
2678 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2679 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2680 */
2681 public int getBadgeIconType() {
2682 return mBadgeIcon;
2683 }
2684
2685 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002686 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002687 *
2688 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2689 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002690 */
2691 public String getShortcutId() {
2692 return mShortcutId;
2693 }
2694
Julia Reynolds3aedded2017-03-31 14:42:09 -04002695
2696 /**
2697 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2698 */
2699 public CharSequence getSettingsText() {
2700 return mSettingsText;
2701 }
2702
Julia Reynolds13d898c2017-02-02 12:22:05 -05002703 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002704 * Returns which type of notifications in a group are responsible for audibly alerting the
2705 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2706 * {@link #GROUP_ALERT_SUMMARY}.
2707 */
2708 public @GroupAlertBehavior int getGroupAlertBehavior() {
2709 return mGroupAlertBehavior;
2710 }
2711
2712 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002713 * The small icon representing this notification in the status bar and content view.
2714 *
2715 * @return the small icon representing this notification.
2716 *
2717 * @see Builder#getSmallIcon()
2718 * @see Builder#setSmallIcon(Icon)
2719 */
2720 public Icon getSmallIcon() {
2721 return mSmallIcon;
2722 }
2723
2724 /**
2725 * Used when notifying to clean up legacy small icons.
2726 * @hide
2727 */
2728 public void setSmallIcon(Icon icon) {
2729 mSmallIcon = icon;
2730 }
2731
2732 /**
2733 * The large icon shown in this notification's content view.
2734 * @see Builder#getLargeIcon()
2735 * @see Builder#setLargeIcon(Icon)
2736 */
2737 public Icon getLargeIcon() {
2738 return mLargeIcon;
2739 }
2740
2741 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002742 * @hide
2743 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002744 public boolean isGroupSummary() {
2745 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2746 }
2747
2748 /**
2749 * @hide
2750 */
2751 public boolean isGroupChild() {
2752 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2753 }
2754
2755 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002756 * @hide
2757 */
2758 public boolean suppressAlertingDueToGrouping() {
2759 if (isGroupSummary()
2760 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2761 return true;
2762 } else if (isGroupChild()
2763 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2764 return true;
2765 }
2766 return false;
2767 }
2768
2769 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002770 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002771 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002772 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002773 * content views using the platform's notification layout template. If your app supports
2774 * versions of Android as old as API level 4, you can instead use
2775 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2776 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2777 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002778 *
Scott Main183bf112012-08-13 19:12:13 -07002779 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002780 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002781 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002782 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002783 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2784 * .setContentText(subject)
2785 * .setSmallIcon(R.drawable.new_mail)
2786 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002787 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002788 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002789 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002790 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002791 /**
2792 * @hide
2793 */
2794 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2795 "android.rebuild.contentViewActionCount";
2796 /**
2797 * @hide
2798 */
2799 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2800 = "android.rebuild.bigViewActionCount";
2801 /**
2802 * @hide
2803 */
2804 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2805 = "android.rebuild.hudViewActionCount";
2806
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002807 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002808
Selim Cinek6743c0b2017-01-18 18:24:01 -08002809 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2810 SystemProperties.getBoolean("notifications.only_title", true);
2811
Selim Cinek389edcd2017-05-11 19:16:44 -07002812 /**
2813 * The lightness difference that has to be added to the primary text color to obtain the
2814 * secondary text color when the background is light.
2815 */
2816 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2817
2818 /**
2819 * The lightness difference that has to be added to the primary text color to obtain the
2820 * secondary text color when the background is dark.
2821 * A bit less then the above value, since it looks better on dark backgrounds.
2822 */
2823 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2824
Joe Onorato46439ce2010-11-19 13:56:21 -08002825 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002826 private Notification mN;
2827 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002828 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002829 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002830 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002831 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002832 private boolean mIsLegacy;
2833 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002834
2835 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002836 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2837 */
2838 private int mCachedContrastColor = COLOR_INVALID;
2839 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002840 /**
2841 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2842 */
2843 private int mCachedAmbientColor = COLOR_INVALID;
2844 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002845
2846 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002847 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2848 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2849 */
2850 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002851 private int mTextColorsAreForBackground = COLOR_INVALID;
2852 private int mPrimaryTextColor = COLOR_INVALID;
2853 private int mSecondaryTextColor = COLOR_INVALID;
2854 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002855 private int mBackgroundColor = COLOR_INVALID;
2856 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002857 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002858 /**
2859 * A temporary location where actions are stored. If != null the view originally has action
2860 * but doesn't have any for this inflation.
2861 */
2862 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002863 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002864
Anthony Chenad4d1582017-04-10 16:07:58 -07002865 private boolean mTintActionButtons;
2866 private boolean mInNightMode;
2867
Adrian Roos70d7aa32017-01-11 15:39:06 -08002868 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002869 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002870 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002871 * @param context
2872 * A {@link Context} that will be used by the Builder to construct the
2873 * RemoteViews. The Context will not be held past the lifetime of this Builder
2874 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002875 * @param channelId
2876 * The constructed Notification will be posted on this
2877 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2878 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002879 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002880 public Builder(Context context, String channelId) {
2881 this(context, (Notification) null);
2882 mN.mChannelId = channelId;
2883 }
2884
2885 /**
2886 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2887 * instead. All posted Notifications must specify a NotificationChannel Id.
2888 */
2889 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002890 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002891 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002892 }
2893
Joe Onoratocb109a02011-01-18 17:57:41 -08002894 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002895 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002896 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002897 public Builder(Context context, Notification toAdopt) {
2898 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002899 Resources res = mContext.getResources();
2900 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2901
2902 if (res.getBoolean(R.bool.config_enableNightMode)) {
2903 Configuration currentConfig = res.getConfiguration();
2904 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2905 == Configuration.UI_MODE_NIGHT_YES;
2906 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002907
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002908 if (toAdopt == null) {
2909 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002910 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2911 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2912 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002913 mN.priority = PRIORITY_DEFAULT;
2914 mN.visibility = VISIBILITY_PRIVATE;
2915 } else {
2916 mN = toAdopt;
2917 if (mN.actions != null) {
2918 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002919 }
2920
Selim Cineke7238dd2017-12-14 17:48:32 -08002921 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
2922 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
2923 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002924 }
2925
Selim Cinek4ac6f602016-06-13 15:47:03 -07002926 if (mN.getSmallIcon() == null && mN.icon != 0) {
2927 setSmallIcon(mN.icon);
2928 }
2929
2930 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2931 setLargeIcon(mN.largeIcon);
2932 }
2933
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002934 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2935 if (!TextUtils.isEmpty(templateClass)) {
2936 final Class<? extends Style> styleClass
2937 = getNotificationStyleClass(templateClass);
2938 if (styleClass == null) {
2939 Log.d(TAG, "Unknown style class: " + templateClass);
2940 } else {
2941 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002942 final Constructor<? extends Style> ctor =
2943 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002944 ctor.setAccessible(true);
2945 final Style style = ctor.newInstance();
2946 style.restoreFromExtras(mN.extras);
2947
2948 if (style != null) {
2949 setStyle(style);
2950 }
2951 } catch (Throwable t) {
2952 Log.e(TAG, "Could not create Style", t);
2953 }
2954 }
2955 }
2956
2957 }
2958 }
2959
2960 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002961 if (mColorUtil == null) {
2962 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002963 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002965 }
2966
2967 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002968 * If this notification is duplicative of a Launcher shortcut, sets the
2969 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2970 * the shortcut.
2971 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002972 * This field will be ignored by Launchers that don't support badging, don't show
2973 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002974 *
2975 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2976 * supersedes
2977 */
2978 public Builder setShortcutId(String shortcutId) {
2979 mN.mShortcutId = shortcutId;
2980 return this;
2981 }
2982
2983 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002984 * Sets which icon to display as a badge for this notification.
2985 *
2986 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2987 * {@link #BADGE_ICON_LARGE}.
2988 *
2989 * Note: This value might be ignored, for launchers that don't support badge icons.
2990 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002991 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002992 mN.mBadgeIcon = icon;
2993 return this;
2994 }
2995
2996 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002997 * Sets the group alert behavior for this notification. Use this method to mute this
2998 * notification if alerts for this notification's group should be handled by a different
2999 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003000 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3001 * mute. For example, if you want only the summary of your group to make noise, all
3002 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003003 *
3004 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3005 */
3006 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3007 mN.mGroupAlertBehavior = groupAlertBehavior;
3008 return this;
3009 }
3010
Jeff Sharkey000ce802017-04-29 13:13:27 -06003011 /** @removed */
3012 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003013 public Builder setChannel(String channelId) {
3014 mN.mChannelId = channelId;
3015 return this;
3016 }
3017
3018 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003019 * Specifies the channel the notification should be delivered on.
3020 */
3021 public Builder setChannelId(String channelId) {
3022 mN.mChannelId = channelId;
3023 return this;
3024 }
3025
Jeff Sharkey000ce802017-04-29 13:13:27 -06003026 /** @removed */
3027 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003028 public Builder setTimeout(long durationMs) {
3029 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003030 return this;
3031 }
3032
3033 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003034 * Specifies a duration in milliseconds after which this notification should be canceled,
3035 * if it is not already canceled.
3036 */
3037 public Builder setTimeoutAfter(long durationMs) {
3038 mN.mTimeout = durationMs;
3039 return this;
3040 }
3041
3042 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003043 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003044 *
3045 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3046 * shown anymore by default and must be opted into by using
3047 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003048 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003049 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003050 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003051 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003052 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003053 return this;
3054 }
3055
Joe Onoratocb109a02011-01-18 17:57:41 -08003056 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003057 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003058 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003059 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3060 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003061 */
3062 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003064 return this;
3065 }
3066
3067 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003068 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003069 *
3070 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003071 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003072 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003073 * Useful when showing an elapsed time (like an ongoing phone call).
3074 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003075 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003076 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003077 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003078 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003079 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003080 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003081 */
3082 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003083 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003084 return this;
3085 }
3086
3087 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003088 * Sets the Chronometer to count down instead of counting up.
3089 *
3090 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3091 * If it isn't set the chronometer will count up.
3092 *
3093 * @see #setUsesChronometer(boolean)
3094 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003095 public Builder setChronometerCountDown(boolean countDown) {
3096 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003097 return this;
3098 }
3099
3100 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003101 * Set the small icon resource, which will be used to represent the notification in the
3102 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003103 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003104
3105 * The platform template for the expanded view will draw this icon in the left, unless a
3106 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3107 * icon will be moved to the right-hand side.
3108 *
3109
3110 * @param icon
3111 * A resource ID in the application's package of the drawable to use.
3112 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003113 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003114 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003115 return setSmallIcon(icon != 0
3116 ? Icon.createWithResource(mContext, icon)
3117 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003118 }
3119
Joe Onoratocb109a02011-01-18 17:57:41 -08003120 /**
3121 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3122 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3123 * LevelListDrawable}.
3124 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003125 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003126 * @param level The level to use for the icon.
3127 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003128 * @see Notification#icon
3129 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003130 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003131 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003132 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003133 return setSmallIcon(icon);
3134 }
3135
3136 /**
3137 * Set the small icon, which will be used to represent the notification in the
3138 * status bar and content view (unless overriden there by a
3139 * {@link #setLargeIcon(Bitmap) large icon}).
3140 *
3141 * @param icon An Icon object to use.
3142 * @see Notification#icon
3143 */
3144 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003145 mN.setSmallIcon(icon);
3146 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3147 mN.icon = icon.getResId();
3148 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003149 return this;
3150 }
3151
Joe Onoratocb109a02011-01-18 17:57:41 -08003152 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003153 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003154 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003155 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003156 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003157 return this;
3158 }
3159
Joe Onoratocb109a02011-01-18 17:57:41 -08003160 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003161 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003162 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003163 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003164 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003165 return this;
3166 }
3167
Joe Onoratocb109a02011-01-18 17:57:41 -08003168 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003169 * This provides some additional information that is displayed in the notification. No
3170 * guarantees are given where exactly it is displayed.
3171 *
3172 * <p>This information should only be provided if it provides an essential
3173 * benefit to the understanding of the notification. The more text you provide the
3174 * less readable it becomes. For example, an email client should only provide the account
3175 * name here if more than one email account has been added.</p>
3176 *
3177 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3178 * notification header area.
3179 *
3180 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3181 * this will be shown in the third line of text in the platform notification template.
3182 * You should not be using {@link #setProgress(int, int, boolean)} at the
3183 * same time on those versions; they occupy the same place.
3184 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003185 */
3186 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003187 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003188 return this;
3189 }
3190
3191 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003192 * Provides text that will appear as a link to your application's settings.
3193 *
3194 * <p>This text does not appear within notification {@link Style templates} but may
3195 * appear when the user uses an affordance to learn more about the notification.
3196 * Additionally, this text will not appear unless you provide a valid link target by
3197 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3198 *
3199 * <p>This text is meant to be concise description about what the user can customize
3200 * when they click on this link. The recommended maximum length is 40 characters.
3201 * @param text
3202 * @return
3203 */
3204 public Builder setSettingsText(CharSequence text) {
3205 mN.mSettingsText = safeCharSequence(text);
3206 return this;
3207 }
3208
3209 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003210 * Set the remote input history.
3211 *
3212 * This should be set to the most recent inputs that have been sent
3213 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3214 * longer relevant (e.g. for chat notifications once the other party has responded).
3215 *
3216 * The most recent input must be stored at the 0 index, the second most recent at the
3217 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3218 * and how much of each individual input is shown.
3219 *
3220 * <p>Note: The reply text will only be shown on notifications that have least one action
3221 * with a {@code RemoteInput}.</p>
3222 */
3223 public Builder setRemoteInputHistory(CharSequence[] text) {
3224 if (text == null) {
3225 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3226 } else {
3227 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3228 CharSequence[] safe = new CharSequence[N];
3229 for (int i = 0; i < N; i++) {
3230 safe[i] = safeCharSequence(text[i]);
3231 }
3232 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3233 }
3234 return this;
3235 }
3236
3237 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003238 * Sets the number of items this notification represents. May be displayed as a badge count
3239 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003240 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003241 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003242 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003243 return this;
3244 }
3245
Joe Onoratocb109a02011-01-18 17:57:41 -08003246 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003247 * A small piece of additional information pertaining to this notification.
3248 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003249 * The platform template will draw this on the last line of the notification, at the far
3250 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003251 *
3252 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3253 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3254 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003255 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003256 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003257 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003258 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003259 return this;
3260 }
3261
Joe Onoratocb109a02011-01-18 17:57:41 -08003262 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003263 * Set the progress this notification represents.
3264 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003265 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003266 */
3267 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003268 mN.extras.putInt(EXTRA_PROGRESS, progress);
3269 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3270 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003271 return this;
3272 }
3273
3274 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003275 * Supply a custom RemoteViews to use instead of the platform template.
3276 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003277 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003278 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003279 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003280 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003281 return setCustomContentView(views);
3282 }
3283
3284 /**
3285 * Supply custom RemoteViews to use instead of the platform template.
3286 *
3287 * This will override the layout that would otherwise be constructed by this Builder
3288 * object.
3289 */
3290 public Builder setCustomContentView(RemoteViews contentView) {
3291 mN.contentView = contentView;
3292 return this;
3293 }
3294
3295 /**
3296 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3297 *
3298 * This will override the expanded layout that would otherwise be constructed by this
3299 * Builder object.
3300 */
3301 public Builder setCustomBigContentView(RemoteViews contentView) {
3302 mN.bigContentView = contentView;
3303 return this;
3304 }
3305
3306 /**
3307 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3308 *
3309 * This will override the heads-up layout that would otherwise be constructed by this
3310 * Builder object.
3311 */
3312 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3313 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003314 return this;
3315 }
3316
Joe Onoratocb109a02011-01-18 17:57:41 -08003317 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003318 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3319 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003320 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3321 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3322 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003323 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003324 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003325 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003326 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003327 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003328 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003329 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003330 return this;
3331 }
3332
Joe Onoratocb109a02011-01-18 17:57:41 -08003333 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003334 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3335 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003336 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003338 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003339 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003340 return this;
3341 }
3342
Joe Onoratocb109a02011-01-18 17:57:41 -08003343 /**
3344 * An intent to launch instead of posting the notification to the status bar.
3345 * Only for use with extremely high-priority notifications demanding the user's
3346 * <strong>immediate</strong> attention, such as an incoming phone call or
3347 * alarm clock that the user has explicitly set to a particular time.
3348 * If this facility is used for something else, please give the user an option
3349 * to turn it off and use a normal notification, as this can be extremely
3350 * disruptive.
3351 *
Chris Wren47c20a12014-06-18 17:27:29 -04003352 * <p>
3353 * The system UI may choose to display a heads-up notification, instead of
3354 * launching this intent, while the user is using the device.
3355 * </p>
3356 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003357 * @param intent The pending intent to launch.
3358 * @param highPriority Passing true will cause this notification to be sent
3359 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003360 *
3361 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003362 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003363 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003364 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003365 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3366 return this;
3367 }
3368
Joe Onoratocb109a02011-01-18 17:57:41 -08003369 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003370 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003371 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003372 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003373 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003374 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003375 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003376 return this;
3377 }
3378
Joe Onoratocb109a02011-01-18 17:57:41 -08003379 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003380 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003381 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003382 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003383 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003384 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003385 setTicker(tickerText);
3386 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003387 return this;
3388 }
3389
Joe Onoratocb109a02011-01-18 17:57:41 -08003390 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003391 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003392 *
3393 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003394 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3395 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003396 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003397 public Builder setLargeIcon(Bitmap b) {
3398 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3399 }
3400
3401 /**
3402 * Add a large icon to the notification content view.
3403 *
3404 * In the platform template, this image will be shown on the left of the notification view
3405 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3406 * badge atop the large icon).
3407 */
3408 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003409 mN.mLargeIcon = icon;
3410 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003411 return this;
3412 }
3413
Joe Onoratocb109a02011-01-18 17:57:41 -08003414 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003415 * Set the sound to play.
3416 *
John Spurlockc0650f022014-07-19 13:22:39 -04003417 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3418 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003419 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003420 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003421 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003422 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003423 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003424 mN.sound = sound;
3425 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003426 return this;
3427 }
3428
Joe Onoratocb109a02011-01-18 17:57:41 -08003429 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003430 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003431 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003432 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3433 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003434 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003435 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003436 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003437 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003438 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003439 mN.sound = sound;
3440 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003441 return this;
3442 }
3443
Joe Onoratocb109a02011-01-18 17:57:41 -08003444 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003445 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3446 * use during playback.
3447 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003448 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003449 * @see Notification#sound
3450 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003451 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003452 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003453 mN.sound = sound;
3454 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003455 return this;
3456 }
3457
3458 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003459 * Set the vibration pattern to use.
3460 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003461 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3462 * <code>pattern</code> parameter.
3463 *
Chris Wren47c20a12014-06-18 17:27:29 -04003464 * <p>
3465 * A notification that vibrates is more likely to be presented as a heads-up notification.
3466 * </p>
3467 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003468 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003470 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003471 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003478 * Set the desired color for the indicator LED on the device, as well as the
3479 * blink duty cycle (specified in milliseconds).
3480 *
3481
3482 * Not all devices will honor all (or even any) of these values.
3483 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003484 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003485 * @see Notification#ledARGB
3486 * @see Notification#ledOnMS
3487 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003488 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003489 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003490 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003491 mN.ledARGB = argb;
3492 mN.ledOnMS = onMs;
3493 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003494 if (onMs != 0 || offMs != 0) {
3495 mN.flags |= FLAG_SHOW_LIGHTS;
3496 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003497 return this;
3498 }
3499
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003501 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003502 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003503
3504 * Ongoing notifications cannot be dismissed by the user, so your application or service
3505 * must take care of canceling them.
3506 *
3507
3508 * They are typically used to indicate a background task that the user is actively engaged
3509 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3510 * (e.g., a file download, sync operation, active network connection).
3511 *
3512
3513 * @see Notification#FLAG_ONGOING_EVENT
3514 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003515 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003516 public Builder setOngoing(boolean ongoing) {
3517 setFlag(FLAG_ONGOING_EVENT, ongoing);
3518 return this;
3519 }
3520
Joe Onoratocb109a02011-01-18 17:57:41 -08003521 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003522 * Set whether this notification should be colorized. When set, the color set with
3523 * {@link #setColor(int)} will be used as the background color of this notification.
3524 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003525 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3526 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003527 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003528 * For most styles, the coloring will only be applied if the notification is for a
3529 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003530 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003531 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003532 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003533 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003534 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003535 */
3536 public Builder setColorized(boolean colorize) {
3537 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3538 return this;
3539 }
3540
3541 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003542 * Set this flag if you would only like the sound, vibrate
3543 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003544 *
3545 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003546 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3548 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3549 return this;
3550 }
3551
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003553 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003554 *
3555 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003556 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003557 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003558 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003559 return this;
3560 }
3561
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003563 * Set whether or not this notification should not bridge to other devices.
3564 *
3565 * <p>Some notifications can be bridged to other devices for remote display.
3566 * This hint can be set to recommend this notification not be bridged.
3567 */
3568 public Builder setLocalOnly(boolean localOnly) {
3569 setFlag(FLAG_LOCAL_ONLY, localOnly);
3570 return this;
3571 }
3572
3573 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003574 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003575 * <p>
3576 * The value should be one or more of the following fields combined with
3577 * bitwise-or:
3578 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3579 * <p>
3580 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003581 *
3582 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003583 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003584 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003585 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003586 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003587 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003588 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003589 return this;
3590 }
3591
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003592 /**
3593 * Set the priority of this notification.
3594 *
3595 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003596 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003597 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003598 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003599 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003600 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003601 return this;
3602 }
Joe Malin8d40d042012-11-05 11:36:40 -08003603
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003604 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003605 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003606 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003607 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003608 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003609 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003610 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 return this;
3612 }
3613
3614 /**
Chris Wrendde75302014-03-26 17:24:15 -04003615 * Add a person that is relevant to this notification.
3616 *
Chris Wrene6c48932014-09-29 17:19:27 -04003617 * <P>
3618 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003619 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3620 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3621 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003622 * </P>
3623 *
3624 * <P>
3625 * The person should be specified by the {@code String} representation of a
3626 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3627 * </P>
3628 *
3629 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3630 * URIs. The path part of these URIs must exist in the contacts database, in the
3631 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3632 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003633 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3634 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003635 * </P>
3636 *
3637 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003638 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003639 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003640 */
Chris Wrene6c48932014-09-29 17:19:27 -04003641 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003642 addPerson(new Person().setUri(uri));
3643 return this;
3644 }
3645
3646 /**
3647 * Add a person that is relevant to this notification.
3648 *
3649 * <P>
3650 * Depending on user preferences, this annotation may allow the notification to pass
3651 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3652 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3653 * appear more prominently in the user interface.
3654 * </P>
3655 *
3656 * <P>
3657 * A person should usually contain a uri in order to benefit from the ranking boost.
3658 * However, even if no uri is provided, it's beneficial to provide other people in the
3659 * notification, such that listeners and voice only devices can announce and handle them
3660 * properly.
3661 * </P>
3662 *
3663 * @param person the person to add.
3664 * @see Notification#EXTRA_PEOPLE_LIST
3665 */
3666 public Builder addPerson(Person person) {
3667 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003668 return this;
3669 }
3670
3671 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003672 * Set this notification to be part of a group of notifications sharing the same key.
3673 * Grouped notifications may display in a cluster or stack on devices which
3674 * support such rendering.
3675 *
3676 * <p>To make this notification the summary for its group, also call
3677 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3678 * {@link #setSortKey}.
3679 * @param groupKey The group key of the group.
3680 * @return this object for method chaining
3681 */
3682 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003683 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003684 return this;
3685 }
3686
3687 /**
3688 * Set this notification to be the group summary for a group of notifications.
3689 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003690 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3691 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003692 * @param isGroupSummary Whether this notification should be a group summary.
3693 * @return this object for method chaining
3694 */
3695 public Builder setGroupSummary(boolean isGroupSummary) {
3696 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3697 return this;
3698 }
3699
3700 /**
3701 * Set a sort key that orders this notification among other notifications from the
3702 * same package. This can be useful if an external sort was already applied and an app
3703 * would like to preserve this. Notifications will be sorted lexicographically using this
3704 * value, although providing different priorities in addition to providing sort key may
3705 * cause this value to be ignored.
3706 *
3707 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003708 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003709 *
3710 * @see String#compareTo(String)
3711 */
3712 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003713 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003714 return this;
3715 }
3716
3717 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003718 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003719 *
Griff Hazen720042b2014-02-24 15:46:56 -08003720 * <p>Values within the Bundle will replace existing extras values in this Builder.
3721 *
3722 * @see Notification#extras
3723 */
Griff Hazen959591e2014-05-15 22:26:18 -07003724 public Builder addExtras(Bundle extras) {
3725 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003726 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003727 }
3728 return this;
3729 }
3730
3731 /**
3732 * Set metadata for this notification.
3733 *
3734 * <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 -04003735 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003736 * called.
3737 *
Griff Hazen720042b2014-02-24 15:46:56 -08003738 * <p>Replaces any existing extras values with those from the provided Bundle.
3739 * Use {@link #addExtras} to merge in metadata instead.
3740 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003741 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 */
Griff Hazen959591e2014-05-15 22:26:18 -07003743 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 if (extras != null) {
3745 mUserExtras = extras;
3746 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003747 return this;
3748 }
3749
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003750 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003751 * Get the current metadata Bundle used by this notification Builder.
3752 *
3753 * <p>The returned Bundle is shared with this Builder.
3754 *
3755 * <p>The current contents of this Bundle are copied into the Notification each time
3756 * {@link #build()} is called.
3757 *
3758 * @see Notification#extras
3759 */
3760 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 return mUserExtras;
3762 }
3763
3764 private Bundle getAllExtras() {
3765 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3766 saveExtras.putAll(mN.extras);
3767 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003768 }
3769
3770 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003771 * Add an action to this notification. Actions are typically displayed by
3772 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003773 * <p>
3774 * Every action must have an icon (32dp square and matching the
3775 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3776 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3777 * <p>
3778 * A notification in its expanded form can display up to 3 actions, from left to right in
3779 * the order they were added. Actions will not be displayed when the notification is
3780 * collapsed, however, so be sure that any essential functions may be accessed by the user
3781 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003782 *
3783 * @param icon Resource ID of a drawable that represents the action.
3784 * @param title Text describing the action.
3785 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003786 *
3787 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003788 */
Dan Sandler86647982015-05-13 23:41:13 -04003789 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003790 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003791 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003792 return this;
3793 }
3794
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003795 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003796 * Add an action to this notification. Actions are typically displayed by
3797 * the system as a button adjacent to the notification content.
3798 * <p>
3799 * Every action must have an icon (32dp square and matching the
3800 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3801 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3802 * <p>
3803 * A notification in its expanded form can display up to 3 actions, from left to right in
3804 * the order they were added. Actions will not be displayed when the notification is
3805 * collapsed, however, so be sure that any essential functions may be accessed by the user
3806 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3807 *
3808 * @param action The action to add.
3809 */
3810 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003811 if (action != null) {
3812 mActions.add(action);
3813 }
Griff Hazen959591e2014-05-15 22:26:18 -07003814 return this;
3815 }
3816
3817 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003818 * Alter the complete list of actions attached to this notification.
3819 * @see #addAction(Action).
3820 *
3821 * @param actions
3822 * @return
3823 */
3824 public Builder setActions(Action... actions) {
3825 mActions.clear();
3826 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003827 if (actions[i] != null) {
3828 mActions.add(actions[i]);
3829 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003830 }
3831 return this;
3832 }
3833
3834 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003835 * Add a rich notification style to be applied at build time.
3836 *
3837 * @param style Object responsible for modifying the notification style.
3838 */
3839 public Builder setStyle(Style style) {
3840 if (mStyle != style) {
3841 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003842 if (mStyle != null) {
3843 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003844 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3845 } else {
3846 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003847 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003848 }
3849 return this;
3850 }
3851
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003852 /**
3853 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003854 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003855 * @return The same Builder.
3856 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003857 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003858 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003859 return this;
3860 }
3861
3862 /**
3863 * Supply a replacement Notification whose contents should be shown in insecure contexts
3864 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3865 * @param n A replacement notification, presumably with some or all info redacted.
3866 * @return The same Builder.
3867 */
3868 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003869 if (n != null) {
3870 mN.publicVersion = new Notification();
3871 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3872 } else {
3873 mN.publicVersion = null;
3874 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003875 return this;
3876 }
3877
Griff Hazenb720abe2014-05-20 13:15:30 -07003878 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003879 * Apply an extender to this notification builder. Extenders may be used to add
3880 * metadata or change options on this builder.
3881 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003882 public Builder extend(Extender extender) {
3883 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003884 return this;
3885 }
3886
Dan Sandler4e787062015-06-17 15:09:48 -04003887 /**
3888 * @hide
3889 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003890 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003891 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003893 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003894 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003895 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003896 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003897 }
3898
Dan Sandler26e81cf2014-05-06 10:01:27 -04003899 /**
3900 * Sets {@link Notification#color}.
3901 *
3902 * @param argb The accent color to use
3903 *
3904 * @return The same Builder.
3905 */
Tor Norbye80756e32015-03-02 09:39:27 -08003906 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003907 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003908 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003909 return this;
3910 }
3911
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003912 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003913 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3914 // This user can never be a badged profile,
3915 // and also includes USER_ALL system notifications.
3916 return null;
3917 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003918 // Note: This assumes that the current user can read the profile badge of the
3919 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003920 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003921 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003922 }
3923
3924 private Bitmap getProfileBadge() {
3925 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003926 if (badge == null) {
3927 return null;
3928 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003929 final int size = mContext.getResources().getDimensionPixelSize(
3930 R.dimen.notification_badge_size);
3931 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003932 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003933 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003934 badge.draw(canvas);
3935 return bitmap;
3936 }
3937
Selim Cinekc848c3a2016-01-13 15:27:30 -08003938 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003939 Bitmap profileBadge = getProfileBadge();
3940
Kenny Guy98193ea2014-07-24 19:54:37 +01003941 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003942 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3943 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003944 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003945 contentView.setDrawableTint(R.id.profile_badge, false,
3946 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003947 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003948 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003949 }
3950
Christoph Studerfe718432014-09-01 18:21:18 +02003951 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003952 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003953 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003954 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003955 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003956 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003957 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003958 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003959 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003960 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003961 }
3962
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003963 /**
3964 * Resets the notification header to its original state
3965 */
3966 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003967 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3968 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003969 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003970 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003971 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003972 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003973 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003974 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003975 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003976 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003977 contentView.setImageViewIcon(R.id.profile_badge, null);
3978 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003979 }
3980
3981 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003982 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3983 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003984 }
3985
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003986 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003987 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003988 }
3989
3990 /**
3991 * @param hasProgress whether the progress bar should be shown and set
3992 */
3993 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003994 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3995 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003996 }
3997
Adrian Roos70d7aa32017-01-11 15:39:06 -08003998 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003999 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004000
Christoph Studerfe718432014-09-01 18:21:18 +02004001 resetStandardTemplate(contentView);
4002
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004004 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004005 bindNotificationHeader(contentView, p.ambient);
Selim Cinek88188f22017-09-19 16:46:56 -07004006 bindLargeIcon(contentView, p.hideLargeIcon, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004007 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4008 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004009 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004010 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004011 if (!p.ambient) {
4012 setTextViewColorPrimary(contentView, R.id.title);
4013 }
Selim Cinek954cc232016-05-20 13:29:23 -07004014 contentView.setViewLayoutWidth(R.id.title, showProgress
4015 ? ViewGroup.LayoutParams.WRAP_CONTENT
4016 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004017 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004018 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004019 int textId = showProgress ? com.android.internal.R.id.text_line_1
4020 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004021 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004022 if (!p.ambient) {
4023 setTextViewColorSecondary(contentView, textId);
4024 }
Selim Cinek41598732016-01-11 16:58:37 -08004025 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004026 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004027
Selim Cinek279fa862016-06-14 10:57:25 -07004028 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004029
Selim Cinek29603462015-11-17 19:04:39 -08004030 return contentView;
4031 }
4032
Selim Cinek48f66b72017-08-18 16:17:51 -07004033 private CharSequence processTextSpans(CharSequence text) {
4034 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004035 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004036 }
4037 return text;
4038 }
4039
Selim Cinek7b9605b2017-01-19 17:36:00 -08004040 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4041 ensureColors();
4042 contentView.setTextColor(id, mPrimaryTextColor);
4043 }
4044
Selim Cinek48f66b72017-08-18 16:17:51 -07004045 private boolean hasForegroundColor() {
4046 return mForegroundColor != COLOR_INVALID;
4047 }
4048
Selim Cinek389edcd2017-05-11 19:16:44 -07004049 /**
4050 * @return the primary text color
4051 * @hide
4052 */
4053 @VisibleForTesting
4054 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004055 ensureColors();
4056 return mPrimaryTextColor;
4057 }
4058
Selim Cinek389edcd2017-05-11 19:16:44 -07004059 /**
4060 * @return the secondary text color
4061 * @hide
4062 */
4063 @VisibleForTesting
4064 public int getSecondaryTextColor() {
4065 ensureColors();
4066 return mSecondaryTextColor;
4067 }
4068
Selim Cinek7b9605b2017-01-19 17:36:00 -08004069 private int getActionBarColor() {
4070 ensureColors();
4071 return mActionBarColor;
4072 }
4073
Selim Cinek622c64a2017-04-17 17:10:05 -07004074 private int getActionBarColorDeEmphasized() {
4075 int backgroundColor = getBackgroundColor();
4076 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4077 }
4078
Selim Cinek7b9605b2017-01-19 17:36:00 -08004079 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4080 ensureColors();
4081 contentView.setTextColor(id, mSecondaryTextColor);
4082 }
4083
4084 private void ensureColors() {
4085 int backgroundColor = getBackgroundColor();
4086 if (mPrimaryTextColor == COLOR_INVALID
4087 || mSecondaryTextColor == COLOR_INVALID
4088 || mActionBarColor == COLOR_INVALID
4089 || mTextColorsAreForBackground != backgroundColor) {
4090 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004091 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004092 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4093 backgroundColor);
4094 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4095 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004096 if (backgroundColor != COLOR_DEFAULT
4097 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4098 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4099 mPrimaryTextColor, backgroundColor, 4.5);
4100 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4101 mSecondaryTextColor, backgroundColor, 4.5);
4102 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004103 } else {
4104 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4105 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4106 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4107 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004108 // We only respect the given colors if worst case Black or White still has
4109 // contrast
4110 boolean backgroundLight = backLum > textLum
4111 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4112 || backLum <= textLum
4113 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004114 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004115 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004116 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4117 mForegroundColor,
4118 backgroundColor,
4119 true /* findFG */,
4120 4.5f);
4121 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004122 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004123 } else {
4124 mSecondaryTextColor =
4125 NotificationColorUtil.findContrastColorAgainstDark(
4126 mForegroundColor,
4127 backgroundColor,
4128 true /* findFG */,
4129 4.5f);
4130 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004131 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004132 }
4133 } else {
4134 mPrimaryTextColor = mForegroundColor;
4135 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004136 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4137 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004138 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4139 backgroundColor) < 4.5f) {
4140 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004141 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004142 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4143 mSecondaryTextColor,
4144 backgroundColor,
4145 true /* findFG */,
4146 4.5f);
4147 } else {
4148 mSecondaryTextColor
4149 = NotificationColorUtil.findContrastColorAgainstDark(
4150 mSecondaryTextColor,
4151 backgroundColor,
4152 true /* findFG */,
4153 4.5f);
4154 }
4155 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004156 mSecondaryTextColor, backgroundLight
4157 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4158 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004159 }
4160 }
4161 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004162 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4163 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004164 }
4165 }
4166
4167 private void updateBackgroundColor(RemoteViews contentView) {
4168 if (isColorized()) {
4169 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4170 getBackgroundColor());
4171 } else {
4172 // Clear it!
4173 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4174 0);
4175 }
4176 }
4177
Selim Cinek860b6da2015-12-16 19:02:19 -08004178 /**
4179 * @param remoteView the remote view to update the minheight in
4180 * @param hasMinHeight does it have a mimHeight
4181 * @hide
4182 */
4183 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4184 int minHeight = 0;
4185 if (hasMinHeight) {
4186 // we need to set the minHeight of the notification
4187 minHeight = mContext.getResources().getDimensionPixelSize(
4188 com.android.internal.R.dimen.notification_min_content_height);
4189 }
4190 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4191 }
4192
Selim Cinek29603462015-11-17 19:04:39 -08004193 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004194 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4195 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4196 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4197 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004198 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004199 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004200 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004201 contentView.setProgressBackgroundTintList(
4202 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4203 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004204 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004205 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004206 contentView.setProgressTintList(R.id.progress, colorStateList);
4207 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004208 }
Selim Cinek29603462015-11-17 19:04:39 -08004209 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004210 } else {
4211 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004212 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004213 }
Joe Onorato561d3852010-11-20 18:09:34 -08004214 }
4215
Selim Cinek88188f22017-09-19 16:46:56 -07004216 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4217 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004218 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4219 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4220 }
Selim Cinek88188f22017-09-19 16:46:56 -07004221 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4222 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004223 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4224 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4225 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004226 int endMargin = R.dimen.notification_content_picture_margin;
4227 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4228 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4229 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004230 }
4231 // Bind the reply action
4232 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004233
Selim Cinek88188f22017-09-19 16:46:56 -07004234 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4235 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4236 if (actionVisible) {
4237 // We're only showing the icon as big if we're hiding the large icon
4238 int contrastColor = resolveContrastColor();
4239 int iconColor;
4240 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004241 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004242 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004243 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004244 contentView.setOnClickPendingIntent(R.id.right_icon,
4245 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004246 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004247 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4248 ? Color.BLACK : Color.WHITE;
4249 } else {
4250 contentView.setImageViewResource(R.id.right_icon,
4251 R.drawable.ic_reply_notification_large);
4252 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4253 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004254 }
Selim Cinek88188f22017-09-19 16:46:56 -07004255 contentView.setDrawableTint(replyId,
4256 false /* targetBackground */,
4257 iconColor,
4258 PorterDuff.Mode.SRC_ATOP);
4259 contentView.setOnClickPendingIntent(replyId,
4260 action.actionIntent);
4261 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4262 } else {
4263 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004264 }
Selim Cinek88188f22017-09-19 16:46:56 -07004265 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4266 ? View.VISIBLE
4267 : View.GONE);
4268 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004269 ? View.VISIBLE
4270 : View.GONE);
4271 }
4272
4273 private Action findReplyAction() {
4274 ArrayList<Action> actions = mActions;
4275 if (mOriginalActions != null) {
4276 actions = mOriginalActions;
4277 }
4278 int numActions = actions.size();
4279 for (int i = 0; i < numActions; i++) {
4280 Action action = actions.get(i);
4281 if (hasValidRemoteInput(action)) {
4282 return action;
4283 }
4284 }
4285 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004286 }
4287
Adrian Roos487374f2017-01-11 15:48:14 -08004288 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4289 bindSmallIcon(contentView, ambient);
4290 bindHeaderAppName(contentView, ambient);
4291 if (!ambient) {
4292 // Ambient view does not have these
4293 bindHeaderText(contentView);
4294 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004295 bindProfileBadge(contentView);
4296 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004297 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004298 }
4299
4300 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004301 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004302 contentView.setDrawableTint(R.id.expand_button, false, color,
4303 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004304 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004305 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004306 }
4307
Selim Cinek99104832017-01-25 14:47:33 -08004308 /**
4309 * @return the color that is used as the first primary highlight color. This is applied
4310 * in several places like the action buttons or the app name in the header.
4311 */
4312 private int getPrimaryHighlightColor() {
4313 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4314 }
4315
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004316 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4317 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004318 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004319 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004320 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4321 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4322 contentView.setLong(R.id.chronometer, "setBase",
4323 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4324 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004325 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004326 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004327 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004328 } else {
4329 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4330 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004331 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004332 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004333 } else {
4334 // We still want a time to be set but gone, such that we can show and hide it
4335 // on demand in case it's a child notification without anything in the header
4336 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004337 }
4338 }
4339
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004340 private void bindHeaderText(RemoteViews contentView) {
4341 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4342 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004343 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004344 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004345 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004346 if (headerText == null
4347 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4348 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4349 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4350 }
4351 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004352 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004353 contentView.setTextViewText(R.id.header_text, processTextSpans(
4354 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004355 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004356 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4357 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004358 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004359 }
4360 }
4361
Adrian Rooseba05822016-04-22 17:09:27 -07004362 /**
4363 * @hide
4364 */
4365 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004366 CharSequence name = null;
4367 final PackageManager pm = mContext.getPackageManager();
4368 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4369 // only system packages which lump together a bunch of unrelated stuff
4370 // may substitute a different name to make the purpose of the
4371 // notification more clear. the correct package label should always
4372 // be accessible via SystemUI.
4373 final String pkg = mContext.getPackageName();
4374 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4375 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4376 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4377 name = subName;
4378 } else {
4379 Log.w(TAG, "warning: pkg "
4380 + pkg + " attempting to substitute app name '" + subName
4381 + "' without holding perm "
4382 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4383 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004384 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004385 if (TextUtils.isEmpty(name)) {
4386 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4387 }
4388 if (TextUtils.isEmpty(name)) {
4389 // still nothing?
4390 return null;
4391 }
4392
4393 return String.valueOf(name);
4394 }
Adrian Roos487374f2017-01-11 15:48:14 -08004395 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004396 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004397 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004398 setTextViewColorPrimary(contentView, R.id.app_name_text);
4399 } else {
4400 contentView.setTextColor(R.id.app_name_text,
4401 ambient ? resolveAmbientColor() : resolveContrastColor());
4402 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004403 }
4404
Adrian Roos487374f2017-01-11 15:48:14 -08004405 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004406 if (mN.mSmallIcon == null && mN.icon != 0) {
4407 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4408 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004409 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004410 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004411 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004412 }
4413
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004414 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004415 * @return true if the built notification will show the time or the chronometer; false
4416 * otherwise
4417 */
4418 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004419 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004420 }
4421
Christoph Studerfe718432014-09-01 18:21:18 +02004422 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004423 // actions_container is only reset when there are no actions to avoid focus issues with
4424 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004425 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004426 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004427
4428 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4429 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4430
4431 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4432 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4433 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4434 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004435
4436 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004437 }
4438
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004439 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004440 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004441 }
4442
Adrian Roos70d7aa32017-01-11 15:39:06 -08004443 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4444 StandardTemplateParams p) {
4445 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004446
Christoph Studerfe718432014-09-01 18:21:18 +02004447 resetStandardTemplateWithActions(big);
4448
Adrian Roose458aa82015-12-08 16:17:19 -08004449 boolean validRemoteInput = false;
4450
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004451 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004452 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004453 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004454 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004455 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004456 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004457 if (p.ambient) {
4458 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004459 } else if (isColorized()) {
4460 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4461 } else {
4462 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4463 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004464 }
Adrian Roosf852a422016-06-03 13:33:43 -07004465 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4466 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004467 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004468 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004469 Action action = mActions.get(i);
4470 validRemoteInput |= hasValidRemoteInput(action);
4471
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004472 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004473 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004474 big.addView(R.id.actions, button);
4475 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004476 } else {
4477 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004478 }
Adrian Roose458aa82015-12-08 16:17:19 -08004479
4480 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004481 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004482 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4483 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004484 big.setTextViewText(R.id.notification_material_reply_text_1,
4485 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004486 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004487
4488 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4489 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004490 big.setTextViewText(R.id.notification_material_reply_text_2,
4491 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004492 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004493
4494 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4495 big.setViewVisibility(
4496 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004497 big.setTextViewText(R.id.notification_material_reply_text_3,
4498 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004499 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004500 }
4501 }
4502 }
4503
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004504 return big;
4505 }
4506
Adrian Roose458aa82015-12-08 16:17:19 -08004507 private boolean hasValidRemoteInput(Action action) {
4508 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4509 // Weird actions
4510 return false;
4511 }
4512
4513 RemoteInput[] remoteInputs = action.getRemoteInputs();
4514 if (remoteInputs == null) {
4515 return false;
4516 }
4517
4518 for (RemoteInput r : remoteInputs) {
4519 CharSequence[] choices = r.getChoices();
4520 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4521 return true;
4522 }
4523 }
4524 return false;
4525 }
4526
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004527 /**
4528 * Construct a RemoteViews for the final 1U notification layout. In order:
4529 * 1. Custom contentView from the caller
4530 * 2. Style's proposed content view
4531 * 3. Standard template view
4532 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004533 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004534 return createContentView(false /* increasedheight */ );
4535 }
4536
4537 /**
4538 * Construct a RemoteViews for the smaller content view.
4539 *
4540 * @param increasedHeight true if this layout be created with an increased height. Some
4541 * styles may support showing more then just that basic 1U size
4542 * and the system may decide to render important notifications
4543 * slightly bigger even when collapsed.
4544 *
4545 * @hide
4546 */
4547 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004548 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 return mN.contentView;
4550 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004551 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004552 if (styleView != null) {
4553 return styleView;
4554 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004555 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004556 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004557 }
4558
Selim Cineka7679b62017-05-10 16:33:25 -07004559 private boolean useExistingRemoteView() {
4560 return mStyle == null || (!mStyle.displayCustomViewInline()
4561 && !mRebuildStyledRemoteViews);
4562 }
4563
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004564 /**
4565 * Construct a RemoteViews for the final big notification layout.
4566 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004567 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004568 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004569 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004570 return mN.bigContentView;
4571 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004572 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004573 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004574 } else if (mActions.size() != 0) {
4575 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004576 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004577 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004578 return result;
4579 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004580
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004581 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004582 * Construct a RemoteViews for the final notification header only. This will not be
4583 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004584 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004585 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004586 * @hide
4587 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004588 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004589 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4590 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004591 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004592 ambient ? R.layout.notification_template_ambient_header
4593 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004594 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004595 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004596 if (colorized != null) {
4597 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4598 } else {
4599 mN.extras.remove(EXTRA_COLORIZED);
4600 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004601 return header;
4602 }
4603
Adrian Roos487374f2017-01-11 15:48:14 -08004604 /**
4605 * Construct a RemoteViews for the ambient version of the notification.
4606 *
4607 * @hide
4608 */
4609 public RemoteViews makeAmbientNotification() {
4610 RemoteViews ambient = applyStandardTemplateWithActions(
4611 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004612 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004613 return ambient;
4614 }
4615
Selim Cinek29603462015-11-17 19:04:39 -08004616 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004617 if (result != null) {
4618 result.setViewVisibility(R.id.text_line_1, View.GONE);
4619 }
Selim Cinek29603462015-11-17 19:04:39 -08004620 }
4621
Selim Cinek6743c0b2017-01-18 18:24:01 -08004622 /**
4623 * Adapt the Notification header if this view is used as an expanded view.
4624 *
4625 * @hide
4626 */
4627 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004628 if (result != null) {
4629 result.setBoolean(R.id.notification_header, "setExpanded", true);
4630 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004631 }
4632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004633 /**
4634 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004635 *
4636 * @param increasedHeight true if this layout be created with an increased height. Some
4637 * styles may support showing more then just that basic 1U size
4638 * and the system may decide to render important notifications
4639 * slightly bigger even when collapsed.
4640 *
4641 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004642 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004643 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004644 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004645 return mN.headsUpContentView;
4646 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004647 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4648 if (styleView != null) {
4649 return styleView;
4650 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004651 } else if (mActions.size() == 0) {
4652 return null;
4653 }
4654
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004655 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004656 }
4657
Selim Cinek624c02db2015-12-14 21:00:02 -08004658 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004659 * Construct a RemoteViews for the final heads-up notification layout.
4660 */
4661 public RemoteViews createHeadsUpContentView() {
4662 return createHeadsUpContentView(false /* useIncreasedHeight */);
4663 }
4664
4665 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004666 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4667 *
4668 * @hide
4669 */
4670 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004671 return makePublicView(false /* ambient */);
4672 }
4673
4674 /**
4675 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4676 *
4677 * @hide
4678 */
4679 public RemoteViews makePublicAmbientNotification() {
4680 return makePublicView(true /* ambient */);
4681 }
4682
4683 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004684 if (mN.publicVersion != null) {
4685 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004686 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004687 }
4688 Bundle savedBundle = mN.extras;
4689 Style style = mStyle;
4690 mStyle = null;
4691 Icon largeIcon = mN.mLargeIcon;
4692 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004693 Bitmap largeIconLegacy = mN.largeIcon;
4694 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004695 ArrayList<Action> actions = mActions;
4696 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004697 Bundle publicExtras = new Bundle();
4698 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4699 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4700 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4701 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004702 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4703 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004704 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004705 RemoteViews view;
4706 if (ambient) {
4707 publicExtras.putCharSequence(EXTRA_TITLE,
4708 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4709 view = makeAmbientNotification();
4710 } else{
4711 view = makeNotificationHeader(false /* ambient */);
4712 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4713 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004714 mN.extras = savedBundle;
4715 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004716 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004717 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004718 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004719 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004720 }
4721
Selim Cinek6743c0b2017-01-18 18:24:01 -08004722 /**
4723 * Construct a content view for the display when low - priority
4724 *
4725 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4726 * a new subtext is created consisting of the content of the
4727 * notification.
4728 * @hide
4729 */
4730 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4731 int color = mN.color;
4732 mN.color = COLOR_DEFAULT;
4733 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4734 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4735 CharSequence newSummary = createSummaryText();
4736 if (!TextUtils.isEmpty(newSummary)) {
4737 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4738 }
4739 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004740
Adrian Roos6f6e1592017-05-02 16:22:53 -07004741 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004742 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004743 if (summary != null) {
4744 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4745 } else {
4746 mN.extras.remove(EXTRA_SUB_TEXT);
4747 }
4748 mN.color = color;
4749 return header;
4750 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004751
Selim Cinek6743c0b2017-01-18 18:24:01 -08004752 private CharSequence createSummaryText() {
4753 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4754 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4755 return titleText;
4756 }
4757 SpannableStringBuilder summary = new SpannableStringBuilder();
4758 if (titleText == null) {
4759 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4760 }
4761 BidiFormatter bidi = BidiFormatter.getInstance();
4762 if (titleText != null) {
4763 summary.append(bidi.unicodeWrap(titleText));
4764 }
4765 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4766 if (titleText != null && contentText != null) {
4767 summary.append(bidi.unicodeWrap(mContext.getText(
4768 R.string.notification_header_divider_symbol_with_spaces)));
4769 }
4770 if (contentText != null) {
4771 summary.append(bidi.unicodeWrap(contentText));
4772 }
4773 return summary;
4774 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004775
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004776 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004777 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004778 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004779 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004780 emphazisedMode ? getEmphasizedActionLayoutResource()
4781 : tombstone ? getActionTombstoneLayoutResource()
4782 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004783 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004784 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004785 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004786 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004787 if (action.mRemoteInputs != null) {
4788 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4789 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004790 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004791 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004792 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004793 int bgColor;
4794 if (isColorized()) {
4795 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4796 } else {
4797 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4798 : R.color.notification_action_list_dark);
4799 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004800 button.setDrawableTint(R.id.button_holder, true,
4801 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004802 CharSequence title = action.title;
4803 ColorStateList[] outResultColor = null;
4804 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004805 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004806 } else {
4807 outResultColor = new ColorStateList[1];
4808 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4809 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004810 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004811 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004812 if (outResultColor != null && outResultColor[0] != null) {
4813 // We need to set the text color as well since changing a text to uppercase
4814 // clears its spans.
4815 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004816 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004817 button.setTextColor(R.id.action0,resolveContrastColor());
4818 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004819 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004820 button.setTextViewText(R.id.action0, processTextSpans(
4821 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004822 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004823 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004824 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004825 button.setTextColor(R.id.action0,
4826 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004827 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004828 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004829 return button;
4830 }
4831
Joe Onoratocb109a02011-01-18 17:57:41 -08004832 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004833 * Ensures contrast on color spans against a background color. also returns the color of the
4834 * text if a span was found that spans over the whole text.
4835 *
4836 * @param charSequence the charSequence on which the spans are
4837 * @param background the background color to ensure the contrast against
4838 * @param outResultColor an array in which a color will be returned as the first element if
4839 * there exists a full length color span.
4840 * @return the contrasted charSequence
4841 */
4842 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4843 ColorStateList[] outResultColor) {
4844 if (charSequence instanceof Spanned) {
4845 Spanned ss = (Spanned) charSequence;
4846 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4847 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4848 for (Object span : spans) {
4849 Object resultSpan = span;
4850 int spanStart = ss.getSpanStart(span);
4851 int spanEnd = ss.getSpanEnd(span);
4852 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4853 if (resultSpan instanceof CharacterStyle) {
4854 resultSpan = ((CharacterStyle) span).getUnderlying();
4855 }
4856 if (resultSpan instanceof TextAppearanceSpan) {
4857 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4858 ColorStateList textColor = originalSpan.getTextColor();
4859 if (textColor != null) {
4860 int[] colors = textColor.getColors();
4861 int[] newColors = new int[colors.length];
4862 for (int i = 0; i < newColors.length; i++) {
4863 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004864 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004865 }
4866 textColor = new ColorStateList(textColor.getStates().clone(),
4867 newColors);
4868 resultSpan = new TextAppearanceSpan(
4869 originalSpan.getFamily(),
4870 originalSpan.getTextStyle(),
4871 originalSpan.getTextSize(),
4872 textColor,
4873 originalSpan.getLinkTextColor());
4874 if (fullLength) {
4875 outResultColor[0] = new ColorStateList(
4876 textColor.getStates().clone(), newColors);
4877 }
4878 }
4879 } else if (resultSpan instanceof ForegroundColorSpan) {
4880 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4881 int foregroundColor = originalSpan.getForegroundColor();
4882 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004883 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004884 resultSpan = new ForegroundColorSpan(foregroundColor);
4885 if (fullLength) {
4886 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4887 }
4888 } else {
4889 resultSpan = span;
4890 }
4891
4892 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4893 }
4894 return builder;
4895 }
4896 return charSequence;
4897 }
4898
4899 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004900 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004901 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004902 */
4903 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004904 if (!mIsLegacyInitialized) {
4905 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4906 < Build.VERSION_CODES.LOLLIPOP;
4907 mIsLegacyInitialized = true;
4908 }
4909 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004910 }
4911
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004912 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004913 return processLegacyText(charSequence, false /* ambient */);
4914 }
4915
4916 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4917 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4918 boolean wantLightText = ambient;
4919 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004920 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004921 } else {
4922 return charSequence;
4923 }
4924 }
4925
Dan Sandler26e81cf2014-05-06 10:01:27 -04004926 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004927 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004928 */
Adrian Roos487374f2017-01-11 15:48:14 -08004929 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4930 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004931 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004932 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004933 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004934 contentView.setDrawableTint(R.id.icon, false, color,
4935 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004936
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004937 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004938 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004939 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004940 }
4941
Dan Sandler26e81cf2014-05-06 10:01:27 -04004942 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004943 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004944 * if it's grayscale).
4945 */
4946 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004947 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4948 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004949 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004950 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004951 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4952 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004953 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004954 }
4955
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004956 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004957 if (mN.color != COLOR_DEFAULT) {
4958 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004959 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004960 }
4961
Adrian Roos4ff3b122016-02-01 12:26:13 -08004962 int resolveContrastColor() {
4963 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4964 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004965 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004966
Selim Cinekac5f0272017-05-02 16:05:41 -07004967 int color;
4968 int background = mBackgroundColorHint;
4969 if (mBackgroundColorHint == COLOR_INVALID) {
4970 background = mContext.getColor(
4971 com.android.internal.R.color.notification_material_background_color);
4972 }
4973 if (mN.color == COLOR_DEFAULT) {
4974 ensureColors();
4975 color = mSecondaryTextColor;
4976 } else {
4977 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004978 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004979 }
4980 if (Color.alpha(color) < 255) {
4981 // alpha doesn't go well for color filters, so let's blend it manually
4982 color = NotificationColorUtil.compositeColors(color, background);
4983 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004984 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004985 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004986 }
4987
Adrian Roos487374f2017-01-11 15:48:14 -08004988 int resolveAmbientColor() {
4989 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4990 return mCachedAmbientColor;
4991 }
4992 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4993
4994 mCachedAmbientColorIsFor = mN.color;
4995 return mCachedAmbientColor = contrasted;
4996 }
4997
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004998 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004999 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005000 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005001 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005002 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005003 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005004 mN.actions = new Action[mActions.size()];
5005 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005006 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005007 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005008 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005009 }
Selim Cinek247fa012016-02-18 09:50:48 -08005010 if (mN.bigContentView != null || mN.contentView != null
5011 || mN.headsUpContentView != null) {
5012 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5013 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005014 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005015 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005016
Julia Reynolds3b848122016-02-26 10:45:32 -05005017 /**
5018 * Creates a Builder from an existing notification so further changes can be made.
5019 * @param context The context for your application / activity.
5020 * @param n The notification to create a Builder from.
5021 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005022 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005023 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005024 ApplicationInfo applicationInfo = n.extras.getParcelable(
5025 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005026 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005027 if (applicationInfo != null) {
5028 try {
5029 builderContext = context.createApplicationContext(applicationInfo,
5030 Context.CONTEXT_RESTRICTED);
5031 } catch (NameNotFoundException e) {
5032 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5033 builderContext = context; // try with our context
5034 }
5035 } else {
5036 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005037 }
5038
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005039 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005040 }
5041
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005042 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005043 * @deprecated Use {@link #build()} instead.
5044 */
5045 @Deprecated
5046 public Notification getNotification() {
5047 return build();
5048 }
5049
5050 /**
5051 * Combine all of the options that have been set and return a new {@link Notification}
5052 * object.
5053 */
5054 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005055 // first, add any extras from the calling code
5056 if (mUserExtras != null) {
5057 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005058 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005059
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005060 mN.creationTime = System.currentTimeMillis();
5061
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005062 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005063 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005064
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005065 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005066
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005067 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005068 mStyle.reduceImageSizes(mContext);
5069 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005070 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005071 }
Selim Cinekd0426622017-07-11 13:19:59 +02005072 mN.reduceImageSizes(mContext);
5073
Adrian Roos5081c0d2016-02-26 16:04:19 -08005074 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005075 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005076 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005077 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005078 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5079 mN.contentView.getSequenceNumber());
5080 }
5081 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005082 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005083 if (mN.bigContentView != null) {
5084 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5085 mN.bigContentView.getSequenceNumber());
5086 }
5087 }
5088 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005089 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005090 if (mN.headsUpContentView != null) {
5091 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5092 mN.headsUpContentView.getSequenceNumber());
5093 }
5094 }
5095 }
5096
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005097 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5098 mN.flags |= FLAG_SHOW_LIGHTS;
5099 }
5100
Adrian Roosfb921842017-10-26 14:49:56 +02005101 mN.allPendingIntents = null;
5102
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005103 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005104 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005105
5106 /**
5107 * Apply this Builder to an existing {@link Notification} object.
5108 *
5109 * @hide
5110 */
5111 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005112 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005113 return n;
5114 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005115
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005116 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005117 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005118 * change. Also removes extenders on low ram devices, as
5119 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005120 *
5121 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5122 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005123 * @hide
5124 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005125 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005126 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005127
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005128 // Only strip views for known Styles because we won't know how to
5129 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005130 if (!isLowRam
5131 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005132 && getNotificationStyleClass(templateClass) == null) {
5133 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005134 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005135
5136 // Only strip unmodified BuilderRemoteViews.
5137 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005138 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005139 n.contentView.getSequenceNumber();
5140 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005141 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005142 n.bigContentView.getSequenceNumber();
5143 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005144 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005145 n.headsUpContentView.getSequenceNumber();
5146
5147 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005148 if (!isLowRam
5149 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005150 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005151 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005152
5153 Notification clone = n.clone();
5154 if (stripContentView) {
5155 clone.contentView = null;
5156 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5157 }
5158 if (stripBigContentView) {
5159 clone.bigContentView = null;
5160 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5161 }
5162 if (stripHeadsUpContentView) {
5163 clone.headsUpContentView = null;
5164 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5165 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005166 if (isLowRam) {
5167 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5168 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5169 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5170 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005171 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005172 }
5173
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005174 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005175 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005176 }
5177
5178 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005179 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005180 }
5181
5182 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005183 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005184 }
5185
5186 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005187 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005188 }
5189
5190 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005191 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005192 }
5193
Adrian Roosc1a80b02016-04-05 14:54:55 -07005194 private int getMessagingLayoutResource() {
5195 return R.layout.notification_template_material_messaging;
5196 }
5197
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005198 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005199 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005200 }
5201
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005202 private int getEmphasizedActionLayoutResource() {
5203 return R.layout.notification_material_action_emphasized;
5204 }
5205
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005206 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005207 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005208 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005209
5210 private int getBackgroundColor() {
5211 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005212 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005213 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005214 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5215 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005216 }
5217 }
5218
5219 private boolean isColorized() {
5220 return mN.isColorized();
5221 }
Selim Cinek99104832017-01-25 14:47:33 -08005222
Anthony Chenad4d1582017-04-10 16:07:58 -07005223 private boolean shouldTintActionButtons() {
5224 return mTintActionButtons;
5225 }
5226
Selim Cinek99104832017-01-25 14:47:33 -08005227 private boolean textColorsNeedInversion() {
5228 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5229 return false;
5230 }
5231 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5232 return targetSdkVersion > Build.VERSION_CODES.M
5233 && targetSdkVersion < Build.VERSION_CODES.O;
5234 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005235
5236 /**
5237 * Set a color palette to be used as the background and textColors
5238 *
5239 * @param backgroundColor the color to be used as the background
5240 * @param foregroundColor the color to be used as the foreground
5241 *
5242 * @hide
5243 */
5244 public void setColorPalette(int backgroundColor, int foregroundColor) {
5245 mBackgroundColor = backgroundColor;
5246 mForegroundColor = foregroundColor;
5247 mTextColorsAreForBackground = COLOR_INVALID;
5248 ensureColors();
5249 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005250
5251 /**
5252 * Sets the background color for this notification to be a different one then the default.
5253 * This is mainly used to calculate contrast and won't necessarily be applied to the
5254 * background.
5255 *
5256 * @hide
5257 */
5258 public void setBackgroundColorHint(int backgroundColor) {
5259 mBackgroundColorHint = backgroundColor;
5260 }
Selim Cineka7679b62017-05-10 16:33:25 -07005261
5262
5263 /**
5264 * Forces all styled remoteViews to be built from scratch and not use any cached
5265 * RemoteViews.
5266 * This is needed for legacy apps that are baking in their remoteviews into the
5267 * notification.
5268 *
5269 * @hide
5270 */
5271 public void setRebuildStyledRemoteViews(boolean rebuild) {
5272 mRebuildStyledRemoteViews = rebuild;
5273 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005274 }
5275
5276 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005277 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5278 * remote views.
5279 *
5280 * @hide
5281 */
5282 void reduceImageSizes(Context context) {
5283 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5284 return;
5285 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005286 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005287 if (mLargeIcon != null || largeIcon != null) {
5288 Resources resources = context.getResources();
5289 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005290 int maxWidth = resources.getDimensionPixelSize(isLowRam
5291 ? R.dimen.notification_right_icon_size_low_ram
5292 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005293 int maxHeight = maxWidth;
5294 if (MediaStyle.class.equals(style)
5295 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005296 maxHeight = resources.getDimensionPixelSize(isLowRam
5297 ? R.dimen.notification_media_image_max_height_low_ram
5298 : R.dimen.notification_media_image_max_height);
5299 maxWidth = resources.getDimensionPixelSize(isLowRam
5300 ? R.dimen.notification_media_image_max_width_low_ram
5301 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005302 }
5303 if (mLargeIcon != null) {
5304 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5305 }
5306 if (largeIcon != null) {
5307 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5308 }
5309 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005310 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5311 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5312 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005313 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5314 }
5315
Selim Cineka8cb1262017-08-15 16:53:44 -07005316 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5317 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005318 if (remoteView != null) {
5319 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005320 int maxWidth = resources.getDimensionPixelSize(isLowRam
5321 ? R.dimen.notification_custom_view_max_image_width_low_ram
5322 : R.dimen.notification_custom_view_max_image_width);
5323 int maxHeight = resources.getDimensionPixelSize(isLowRam
5324 ? R.dimen.notification_custom_view_max_image_height_low_ram
5325 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005326 remoteView.reduceImageSizes(maxWidth, maxHeight);
5327 }
5328 }
5329
5330 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005331 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005332 */
Selim Cinek22714f12017-04-13 16:23:53 -07005333 private boolean isForegroundService() {
5334 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005335 }
5336
5337 /**
Selim Cinek99104832017-01-25 14:47:33 -08005338 * @return whether this notification has a media session attached
5339 * @hide
5340 */
5341 public boolean hasMediaSession() {
5342 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5343 }
5344
5345 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005346 * @return the style class of this notification
5347 * @hide
5348 */
5349 public Class<? extends Notification.Style> getNotificationStyle() {
5350 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5351
5352 if (!TextUtils.isEmpty(templateClass)) {
5353 return Notification.getNotificationStyleClass(templateClass);
5354 }
5355 return null;
5356 }
5357
5358 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005359 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005360 *
5361 * @hide
5362 */
5363 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005364 if (isColorizedMedia()) {
5365 return true;
5366 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005367 return extras.getBoolean(EXTRA_COLORIZED)
5368 && (hasColorizedPermission() || isForegroundService());
5369 }
5370
5371 /**
5372 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5373 * permission. The permission is checked when a notification is enqueued.
5374 */
5375 private boolean hasColorizedPermission() {
5376 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005377 }
5378
5379 /**
5380 * @return true if this notification is colorized and it is a media notification
5381 *
5382 * @hide
5383 */
5384 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005385 Class<? extends Style> style = getNotificationStyle();
5386 if (MediaStyle.class.equals(style)) {
5387 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5388 if ((colorized == null || colorized) && hasMediaSession()) {
5389 return true;
5390 }
5391 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5392 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5393 return true;
5394 }
5395 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005396 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005397 }
5398
Selim Cinek0847acd2017-04-24 19:48:29 -07005399
5400 /**
5401 * @return true if this is a media notification
5402 *
5403 * @hide
5404 */
5405 public boolean isMediaNotification() {
5406 Class<? extends Style> style = getNotificationStyle();
5407 if (MediaStyle.class.equals(style)) {
5408 return true;
5409 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5410 return true;
5411 }
5412 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005413 }
5414
Selim Cinek279fa862016-06-14 10:57:25 -07005415 private boolean hasLargeIcon() {
5416 return mLargeIcon != null || largeIcon != null;
5417 }
5418
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005419 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005420 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005421 * @hide
5422 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005423 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005424 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5425 }
5426
5427 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005428 * @return true if the notification will show a chronometer; false otherwise
5429 * @hide
5430 */
5431 public boolean showsChronometer() {
5432 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5433 }
5434
5435 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005436 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005437 */
5438 @SystemApi
5439 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5440 Class<? extends Style>[] classes = new Class[] {
5441 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5442 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5443 MessagingStyle.class };
5444 for (Class<? extends Style> innerClass : classes) {
5445 if (templateClass.equals(innerClass.getName())) {
5446 return innerClass;
5447 }
5448 }
5449 return null;
5450 }
5451
5452 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005453 * An object that can apply a rich notification style to a {@link Notification.Builder}
5454 * object.
5455 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005456 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005457 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005458
5459 /**
5460 * @hide
5461 */
5462 protected CharSequence mSummaryText = null;
5463
5464 /**
5465 * @hide
5466 */
5467 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005468
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005469 protected Builder mBuilder;
5470
Chris Wrend6297db2012-05-03 16:20:13 -04005471 /**
5472 * Overrides ContentTitle in the big form of the template.
5473 * This defaults to the value passed to setContentTitle().
5474 */
5475 protected void internalSetBigContentTitle(CharSequence title) {
5476 mBigContentTitle = title;
5477 }
5478
5479 /**
5480 * Set the first line of text after the detail section in the big form of the template.
5481 */
5482 protected void internalSetSummaryText(CharSequence cs) {
5483 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005484 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005485 }
5486
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005487 public void setBuilder(Builder builder) {
5488 if (mBuilder != builder) {
5489 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005490 if (mBuilder != null) {
5491 mBuilder.setStyle(this);
5492 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005493 }
5494 }
5495
Chris Wrend6297db2012-05-03 16:20:13 -04005496 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005497 if (mBuilder == null) {
5498 throw new IllegalArgumentException("Style requires a valid Builder object");
5499 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005500 }
Chris Wrend6297db2012-05-03 16:20:13 -04005501
5502 protected RemoteViews getStandardView(int layoutId) {
5503 checkBuilder();
5504
Christoph Studer4600f9b2014-07-22 22:44:43 +02005505 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005506 CharSequence oldBuilderContentTitle =
5507 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005508 if (mBigContentTitle != null) {
5509 mBuilder.setContentTitle(mBigContentTitle);
5510 }
5511
Chris Wrend6297db2012-05-03 16:20:13 -04005512 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5513
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005514 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005515
Chris Wrend6297db2012-05-03 16:20:13 -04005516 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5517 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005518 } else {
5519 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005520 }
5521
Chris Wrend6297db2012-05-03 16:20:13 -04005522 return contentView;
5523 }
5524
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005525 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005526 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005527 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005528 *
5529 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005530 * @hide
5531 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005532 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005533 return null;
5534 }
5535
5536 /**
5537 * Construct a Style-specific RemoteViews for the final big notification layout.
5538 * @hide
5539 */
5540 public RemoteViews makeBigContentView() {
5541 return null;
5542 }
5543
5544 /**
5545 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005546 *
5547 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005548 * @hide
5549 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005550 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005551 return null;
5552 }
5553
5554 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005555 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005556 * @hide
5557 */
5558 public void addExtras(Bundle extras) {
5559 if (mSummaryTextSet) {
5560 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5561 }
5562 if (mBigContentTitle != null) {
5563 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5564 }
Chris Wren91ad5632013-06-05 15:05:57 -04005565 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005566 }
5567
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005568 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005569 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005570 * @hide
5571 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005572 protected void restoreFromExtras(Bundle extras) {
5573 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5574 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5575 mSummaryTextSet = true;
5576 }
5577 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5578 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5579 }
5580 }
5581
5582
5583 /**
5584 * @hide
5585 */
5586 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005587 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005588 return wip;
5589 }
5590
Daniel Sandler0ec46202015-06-24 01:27:05 -04005591 /**
5592 * @hide
5593 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005594 public void purgeResources() {}
5595
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005596 /**
5597 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5598 * attached to.
5599 *
5600 * @return the fully constructed Notification.
5601 */
5602 public Notification build() {
5603 checkBuilder();
5604 return mBuilder.build();
5605 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005606
5607 /**
5608 * @hide
5609 * @return true if the style positions the progress bar on the second line; false if the
5610 * style hides the progress bar
5611 */
5612 protected boolean hasProgress() {
5613 return true;
5614 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005615
5616 /**
5617 * @hide
5618 * @return Whether we should put the summary be put into the notification header
5619 */
5620 public boolean hasSummaryInHeader() {
5621 return true;
5622 }
Selim Cinek593610c2016-02-16 18:42:57 -08005623
5624 /**
5625 * @hide
5626 * @return Whether custom content views are displayed inline in the style
5627 */
5628 public boolean displayCustomViewInline() {
5629 return false;
5630 }
Selim Cinekd0426622017-07-11 13:19:59 +02005631
5632 /**
5633 * Reduces the image sizes contained in this style.
5634 *
5635 * @hide
5636 */
5637 public void reduceImageSizes(Context context) {
5638 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005639 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005640
5641 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005642 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005643 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005644 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005645 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005646 * Notification notif = new Notification.Builder(mContext)
5647 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5648 * .setContentText(subject)
5649 * .setSmallIcon(R.drawable.new_post)
5650 * .setLargeIcon(aBitmap)
5651 * .setStyle(new Notification.BigPictureStyle()
5652 * .bigPicture(aBigBitmap))
5653 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005654 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005655 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005656 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005657 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005658 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005659 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005660 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005661 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005662
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005663 public BigPictureStyle() {
5664 }
5665
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005666 /**
5667 * @deprecated use {@code BigPictureStyle()}.
5668 */
5669 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005670 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005671 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005672 }
5673
Chris Wrend6297db2012-05-03 16:20:13 -04005674 /**
5675 * Overrides ContentTitle in the big form of the template.
5676 * This defaults to the value passed to setContentTitle().
5677 */
5678 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005679 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005680 return this;
5681 }
5682
5683 /**
5684 * Set the first line of text after the detail section in the big form of the template.
5685 */
5686 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005687 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005688 return this;
5689 }
5690
Chris Wren0bd664d2012-08-01 13:56:56 -04005691 /**
5692 * Provide the bitmap to be used as the payload for the BigPicture notification.
5693 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005694 public BigPictureStyle bigPicture(Bitmap b) {
5695 mPicture = b;
5696 return this;
5697 }
5698
Chris Wren3745a3d2012-05-22 15:11:52 -04005699 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005700 * Override the large icon when the big notification is shown.
5701 */
5702 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005703 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5704 }
5705
5706 /**
5707 * Override the large icon when the big notification is shown.
5708 */
5709 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005710 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005711 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005712 return this;
5713 }
5714
Riley Andrews0394a0c2015-11-03 23:36:52 -08005715 /** @hide */
5716 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5717
Daniel Sandler0ec46202015-06-24 01:27:05 -04005718 /**
5719 * @hide
5720 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005721 @Override
5722 public void purgeResources() {
5723 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005724 if (mPicture != null &&
5725 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005726 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005727 mPicture = mPicture.createAshmemBitmap();
5728 }
5729 if (mBigLargeIcon != null) {
5730 mBigLargeIcon.convertToAshmem();
5731 }
5732 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005733
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005734 /**
5735 * @hide
5736 */
Selim Cinekd0426622017-07-11 13:19:59 +02005737 @Override
5738 public void reduceImageSizes(Context context) {
5739 super.reduceImageSizes(context);
5740 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005741 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005742 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005743 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5744 ? R.dimen.notification_big_picture_max_height_low_ram
5745 : R.dimen.notification_big_picture_max_height);
5746 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5747 ? R.dimen.notification_big_picture_max_width_low_ram
5748 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005749 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5750 }
5751 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005752 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5753 ? R.dimen.notification_right_icon_size_low_ram
5754 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005755 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5756 }
5757 }
5758
5759 /**
5760 * @hide
5761 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005762 public RemoteViews makeBigContentView() {
5763 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005764 // This covers the following cases:
5765 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005766 // mN.mLargeIcon
5767 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005768 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005769 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005770 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005771 oldLargeIcon = mBuilder.mN.mLargeIcon;
5772 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005773 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5774 // replacement if the other one is null. Because we're restoring these legacy icons
5775 // for old listeners, this is in general non-null.
5776 largeIconLegacy = mBuilder.mN.largeIcon;
5777 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005778 }
5779
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005780 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005781 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005782 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5783 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005784 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005785 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005786 }
Selim Cinek279fa862016-06-14 10:57:25 -07005787 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005788
Christoph Studer5c510ee2014-12-15 16:32:27 +01005789 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005790 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005791 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005792 }
5793
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005794 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005795 return contentView;
5796 }
5797
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005798 /**
5799 * @hide
5800 */
5801 public void addExtras(Bundle extras) {
5802 super.addExtras(extras);
5803
5804 if (mBigLargeIconSet) {
5805 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5806 }
5807 extras.putParcelable(EXTRA_PICTURE, mPicture);
5808 }
5809
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005810 /**
5811 * @hide
5812 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005813 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005814 protected void restoreFromExtras(Bundle extras) {
5815 super.restoreFromExtras(extras);
5816
5817 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005818 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005819 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005820 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005821 mPicture = extras.getParcelable(EXTRA_PICTURE);
5822 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005823
5824 /**
5825 * @hide
5826 */
5827 @Override
5828 public boolean hasSummaryInHeader() {
5829 return false;
5830 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005831 }
5832
5833 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005834 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005835 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005836 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005837 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005838 * Notification notif = new Notification.Builder(mContext)
5839 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5840 * .setContentText(subject)
5841 * .setSmallIcon(R.drawable.new_mail)
5842 * .setLargeIcon(aBitmap)
5843 * .setStyle(new Notification.BigTextStyle()
5844 * .bigText(aVeryLongString))
5845 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005846 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005847 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005848 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005849 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005850 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005851
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005852 private CharSequence mBigText;
5853
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005854 public BigTextStyle() {
5855 }
5856
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005857 /**
5858 * @deprecated use {@code BigTextStyle()}.
5859 */
5860 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005861 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005862 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005863 }
5864
Chris Wrend6297db2012-05-03 16:20:13 -04005865 /**
5866 * Overrides ContentTitle in the big form of the template.
5867 * This defaults to the value passed to setContentTitle().
5868 */
5869 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005870 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005871 return this;
5872 }
5873
5874 /**
5875 * Set the first line of text after the detail section in the big form of the template.
5876 */
5877 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005878 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005879 return this;
5880 }
5881
Chris Wren0bd664d2012-08-01 13:56:56 -04005882 /**
5883 * Provide the longer text to be displayed in the big form of the
5884 * template in place of the content text.
5885 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005886 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005887 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005888 return this;
5889 }
5890
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005891 /**
5892 * @hide
5893 */
5894 public void addExtras(Bundle extras) {
5895 super.addExtras(extras);
5896
Christoph Studer4600f9b2014-07-22 22:44:43 +02005897 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5898 }
5899
5900 /**
5901 * @hide
5902 */
5903 @Override
5904 protected void restoreFromExtras(Bundle extras) {
5905 super.restoreFromExtras(extras);
5906
5907 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005908 }
5909
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005910 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005911 * @param increasedHeight true if this layout be created with an increased height.
5912 *
5913 * @hide
5914 */
5915 @Override
5916 public RemoteViews makeContentView(boolean increasedHeight) {
5917 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005918 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005919 mBuilder.mActions = new ArrayList<>();
5920 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005921 mBuilder.mActions = mBuilder.mOriginalActions;
5922 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005923 return remoteViews;
5924 }
5925 return super.makeContentView(increasedHeight);
5926 }
5927
5928 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005929 * @hide
5930 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005931 @Override
5932 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5933 if (increasedHeight && mBuilder.mActions.size() > 0) {
5934 return makeBigContentView();
5935 }
5936 return super.makeHeadsUpContentView(increasedHeight);
5937 }
5938
5939 /**
5940 * @hide
5941 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005942 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005943
5944 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005945 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005946 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005947
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005948 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005949
Selim Cinek75998782016-04-26 10:39:17 -07005950 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005951
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005952 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005953 if (TextUtils.isEmpty(bigTextText)) {
5954 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5955 // experience
5956 bigTextText = mBuilder.processLegacyText(text);
5957 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005958 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005959
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005960 return contentView;
5961 }
5962
Adrian Roosb1f427c2016-05-26 12:27:15 -07005963 static void applyBigTextContentView(Builder builder,
5964 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005965 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005966 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005967 contentView.setViewVisibility(R.id.big_text,
5968 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005969 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005970 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005971 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005972
5973 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005974 * Helper class for generating large-format notifications that include multiple back-and-forth
5975 * messages of varying types between any number of people.
5976 *
5977 * <br>
5978 * If the platform does not provide large-format notifications, this method has no effect. The
5979 * user will always see the normal notification view.
5980 * <br>
5981 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5982 * so:
5983 * <pre class="prettyprint">
5984 *
5985 * Notification noti = new Notification.Builder()
5986 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5987 * .setContentText(subject)
5988 * .setSmallIcon(R.drawable.new_message)
5989 * .setLargeIcon(aBitmap)
5990 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5991 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5992 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5993 * .build();
5994 * </pre>
5995 */
5996 public static class MessagingStyle extends Style {
5997
5998 /**
5999 * The maximum number of messages that will be retained in the Notification itself (the
6000 * number displayed is up to the platform).
6001 */
6002 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6003
6004 CharSequence mUserDisplayName;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006005 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006006 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006007 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006008 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006009
6010 MessagingStyle() {
6011 }
6012
6013 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006014 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6015 * user before the posting app reposts the notification with those messages after they've
6016 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006017 * {@link #addMessage(Notification.MessagingStyle.Message)}
6018 */
Alex Hillsfd590442016-10-07 09:52:44 -04006019 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006020 mUserDisplayName = userDisplayName;
6021 }
6022
6023 /**
6024 * Returns the name to be displayed for any replies sent by the user
6025 */
6026 public CharSequence getUserDisplayName() {
6027 return mUserDisplayName;
6028 }
6029
6030 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006031 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6032 *
6033 * @param conversationTitle A name for the conversation, or {@code null}
Alex Hillsfc737de2016-03-23 17:33:02 -04006034 * @return this object for method chaining.
6035 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006036 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006037 mConversationTitle = conversationTitle;
6038 return this;
6039 }
6040
6041 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006042 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006043 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006044 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006045 public CharSequence getConversationTitle() {
6046 return mConversationTitle;
6047 }
6048
6049 /**
6050 * Adds a message for display by this notification. Convenience call for a simple
6051 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6052 * @param text A {@link CharSequence} to be displayed as the message content
6053 * @param timestamp Time at which the message arrived
6054 * @param sender A {@link CharSequence} to be used for displaying the name of the
6055 * sender. Should be <code>null</code> for messages by the current user, in which case
6056 * the platform will insert {@link #getUserDisplayName()}.
6057 * Should be unique amongst all individuals in the conversation, and should be
6058 * consistent during re-posts of the notification.
6059 *
6060 * @see Message#Message(CharSequence, long, CharSequence)
6061 *
6062 * @return this object for method chaining
6063 */
6064 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006065 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006066 }
6067
6068 /**
6069 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006070 *
6071 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6072 * the newest last.
6073 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006074 * @param message The {@link Message} to be displayed
6075 * @return this object for method chaining
6076 */
6077 public MessagingStyle addMessage(Message message) {
6078 mMessages.add(message);
6079 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6080 mMessages.remove(0);
6081 }
6082 return this;
6083 }
6084
6085 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006086 * Adds a {@link Message} for historic context in this notification.
6087 *
6088 * <p>Messages should be added as historic if they are not the main subject of the
6089 * notification but may give context to a conversation. The system may choose to present
6090 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6091 *
6092 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6093 * the newest last.
6094 *
6095 * @param message The historic {@link Message} to be added
6096 * @return this object for method chaining
6097 */
6098 public MessagingStyle addHistoricMessage(Message message) {
6099 mHistoricMessages.add(message);
6100 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6101 mHistoricMessages.remove(0);
6102 }
6103 return this;
6104 }
6105
6106 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006107 * Gets the list of {@code Message} objects that represent the notification
6108 */
6109 public List<Message> getMessages() {
6110 return mMessages;
6111 }
6112
6113 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006114 * Gets the list of historic {@code Message}s in the notification.
6115 */
6116 public List<Message> getHistoricMessages() {
6117 return mHistoricMessages;
6118 }
6119
6120 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006121 * Sets whether this conversation notification represents a group.
6122 * @param isGroupConversation {@code true} if the conversation represents a group,
6123 * {@code false} otherwise.
6124 * @return this object for method chaining
6125 */
6126 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6127 mIsGroupConversation = isGroupConversation;
6128 return this;
6129 }
6130
6131 /**
6132 * Returns {@code true} if this notification represents a group conversation.
6133 */
6134 public boolean isGroupConversation() {
6135 return mIsGroupConversation;
6136 }
6137
6138 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006139 * @hide
6140 */
6141 @Override
6142 public void addExtras(Bundle extras) {
6143 super.addExtras(extras);
6144 if (mUserDisplayName != null) {
6145 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6146 }
6147 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006148 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006149 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006150 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6151 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006152 }
Adrian Roos437cd562017-01-18 15:47:03 -08006153 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6154 Message.getBundleArrayForMessages(mHistoricMessages));
6155 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006156
6157 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006158 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006159 }
6160
6161 private void fixTitleAndTextExtras(Bundle extras) {
6162 Message m = findLatestIncomingMessage();
6163 CharSequence text = (m == null) ? null : m.mText;
6164 CharSequence sender = m == null ? null
6165 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6166 CharSequence title;
6167 if (!TextUtils.isEmpty(mConversationTitle)) {
6168 if (!TextUtils.isEmpty(sender)) {
6169 BidiFormatter bidi = BidiFormatter.getInstance();
6170 title = mBuilder.mContext.getString(
6171 com.android.internal.R.string.notification_messaging_title_template,
6172 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6173 } else {
6174 title = mConversationTitle;
6175 }
6176 } else {
6177 title = sender;
6178 }
6179
6180 if (title != null) {
6181 extras.putCharSequence(EXTRA_TITLE, title);
6182 }
6183 if (text != null) {
6184 extras.putCharSequence(EXTRA_TEXT, text);
6185 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006186 }
6187
6188 /**
6189 * @hide
6190 */
6191 @Override
6192 protected void restoreFromExtras(Bundle extras) {
6193 super.restoreFromExtras(extras);
6194
Adrian Roos96b7e202016-05-17 13:50:38 -07006195 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6196 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006197 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006198 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006199 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006200 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006201 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006202 }
6203
6204 /**
6205 * @hide
6206 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006207 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006208 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006209 mBuilder.mOriginalActions = mBuilder.mActions;
6210 mBuilder.mActions = new ArrayList<>();
6211 RemoteViews remoteViews = makeBigContentView();
6212 mBuilder.mActions = mBuilder.mOriginalActions;
6213 mBuilder.mOriginalActions = null;
6214 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006215 }
6216
6217 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006218 return findLatestIncomingMessage(mMessages);
6219 }
6220
6221 /**
6222 * @hide
6223 */
6224 @Nullable
6225 public static Message findLatestIncomingMessage(
6226 List<Message> messages) {
6227 for (int i = messages.size() - 1; i >= 0; i--) {
6228 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006229 // Incoming messages have a non-empty sender.
6230 if (!TextUtils.isEmpty(m.mSender)) {
6231 return m;
6232 }
6233 }
Selim Cinek88188f22017-09-19 16:46:56 -07006234 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006235 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006236 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006237 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006238 return null;
6239 }
6240
6241 /**
6242 * @hide
6243 */
6244 @Override
6245 public RemoteViews makeBigContentView() {
Selim Cinek88188f22017-09-19 16:46:56 -07006246 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006247 ? super.mBigContentTitle
6248 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006249 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
6250 if (isOneToOne) {
6251 // Let's add the conversationTitle in case we didn't have one before and all
6252 // messages are from the same sender
6253 conversationTitle = createConversationTitleFromMessages();
Selim Cinekf7409db2017-10-24 16:17:14 -07006254 } else if (hasOnlyWhiteSpaceSenders()) {
6255 isOneToOne = true;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006256 }
Selim Cinek88188f22017-09-19 16:46:56 -07006257 boolean hasTitle = !TextUtils.isEmpty(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07006258 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006259 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006260 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
6261 .hideLargeIcon(isOneToOne).alwaysShowReply(true));
Selim Cinek88188f22017-09-19 16:46:56 -07006262 addExtras(mBuilder.mN.extras);
6263 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6264 mBuilder.resolveContrastColor());
6265 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6266 mBuilder.mN.mLargeIcon);
6267 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6268 isOneToOne);
6269 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6270 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006271 return contentView;
6272 }
6273
Selim Cinekf7409db2017-10-24 16:17:14 -07006274 private boolean hasOnlyWhiteSpaceSenders() {
6275 for (int i = 0; i < mMessages.size(); i++) {
6276 Message m = mMessages.get(i);
6277 CharSequence sender = m.getSender();
6278 if (!isWhiteSpace(sender)) {
6279 return false;
6280 }
6281 }
6282 return true;
6283 }
6284
6285 private boolean isWhiteSpace(CharSequence sender) {
6286 if (TextUtils.isEmpty(sender)) {
6287 return true;
6288 }
6289 if (sender.toString().matches("^\\s*$")) {
6290 return true;
6291 }
6292 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6293 // For the presentation that we had.
6294 for (int i = 0; i < sender.length(); i++) {
6295 char c = sender.charAt(i);
6296 if (c != '\u200B') {
6297 return false;
6298 }
6299 }
6300 return true;
6301 }
6302
Selim Cinek88188f22017-09-19 16:46:56 -07006303 private CharSequence createConversationTitleFromMessages() {
6304 ArraySet<CharSequence> names = new ArraySet<>();
6305 for (int i = 0; i < mMessages.size(); i++) {
6306 Message m = mMessages.get(i);
6307 CharSequence sender = m.getSender();
6308 if (sender != null) {
6309 names.add(sender);
6310 }
6311 }
6312 SpannableStringBuilder title = new SpannableStringBuilder();
6313 int size = names.size();
6314 for (int i = 0; i < size; i++) {
6315 CharSequence name = names.valueAt(i);
6316 if (!TextUtils.isEmpty(title)) {
6317 title.append(", ");
6318 }
6319 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6320 }
6321 return title;
6322 }
6323
Adrian Roosdedd1df2016-04-26 16:38:47 -07006324 /**
6325 * @hide
6326 */
6327 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006328 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006329 RemoteViews remoteViews = makeBigContentView();
6330 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6331 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006332 }
6333
Adrian Roosc1a80b02016-04-05 14:54:55 -07006334 private static TextAppearanceSpan makeFontColorSpan(int color) {
6335 return new TextAppearanceSpan(null, 0, 0,
6336 ColorStateList.valueOf(color), null);
6337 }
6338
Alex Hillsd9b04d92016-04-11 16:38:16 -04006339 public static final class Message {
6340
6341 static final String KEY_TEXT = "text";
6342 static final String KEY_TIMESTAMP = "time";
6343 static final String KEY_SENDER = "sender";
6344 static final String KEY_DATA_MIME_TYPE = "type";
6345 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006346 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006347
6348 private final CharSequence mText;
6349 private final long mTimestamp;
6350 private final CharSequence mSender;
6351
Shane Brennan5a871862017-03-11 13:14:17 -08006352 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006353 private String mDataMimeType;
6354 private Uri mDataUri;
6355
6356 /**
6357 * Constructor
6358 * @param text A {@link CharSequence} to be displayed as the message content
6359 * @param timestamp Time at which the message arrived
6360 * @param sender A {@link CharSequence} to be used for displaying the name of the
6361 * sender. Should be <code>null</code> for messages by the current user, in which case
6362 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6363 * Should be unique amongst all individuals in the conversation, and should be
6364 * consistent during re-posts of the notification.
6365 */
6366 public Message(CharSequence text, long timestamp, CharSequence sender){
6367 mText = text;
6368 mTimestamp = timestamp;
6369 mSender = sender;
6370 }
6371
6372 /**
6373 * Sets a binary blob of data and an associated MIME type for a message. In the case
6374 * where the platform doesn't support the MIME type, the original text provided in the
6375 * constructor will be used.
6376 * @param dataMimeType The MIME type of the content. See
6377 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6378 * types on Android and Android Wear.
6379 * @param dataUri The uri containing the content whose type is given by the MIME type.
6380 * <p class="note">
6381 * <ol>
6382 * <li>Notification Listeners including the System UI need permission to access the
6383 * data the Uri points to. The recommended ways to do this are:</li>
6384 * <li>Store the data in your own ContentProvider, making sure that other apps have
6385 * the correct permission to access your provider. The preferred mechanism for
6386 * providing access is to use per-URI permissions which are temporary and only
6387 * grant access to the receiving application. An easy way to create a
6388 * ContentProvider like this is to use the FileProvider helper class.</li>
6389 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6390 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6391 * also store non-media types (see MediaStore.Files for more info). Files can be
6392 * inserted into the MediaStore using scanFile() after which a content:// style
6393 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6394 * Note that once added to the system MediaStore the content is accessible to any
6395 * app on the device.</li>
6396 * </ol>
6397 * @return this object for method chaining
6398 */
6399 public Message setData(String dataMimeType, Uri dataUri) {
6400 mDataMimeType = dataMimeType;
6401 mDataUri = dataUri;
6402 return this;
6403 }
6404
Alex Hillsfc737de2016-03-23 17:33:02 -04006405 /**
6406 * Get the text to be used for this message, or the fallback text if a type and content
6407 * Uri have been set
6408 */
6409 public CharSequence getText() {
6410 return mText;
6411 }
6412
6413 /**
6414 * Get the time at which this message arrived
6415 */
6416 public long getTimestamp() {
6417 return mTimestamp;
6418 }
6419
6420 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006421 * Get the extras Bundle for this message.
6422 */
6423 public Bundle getExtras() {
6424 return mExtras;
6425 }
6426
6427 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006428 * Get the text used to display the contact's name in the messaging experience
6429 */
6430 public CharSequence getSender() {
6431 return mSender;
6432 }
6433
6434 /**
6435 * Get the MIME type of the data pointed to by the Uri
6436 */
6437 public String getDataMimeType() {
6438 return mDataMimeType;
6439 }
6440
6441 /**
6442 * Get the the Uri pointing to the content of the message. Can be null, in which case
6443 * {@see #getText()} is used.
6444 */
6445 public Uri getDataUri() {
6446 return mDataUri;
6447 }
6448
Alex Hillsd9b04d92016-04-11 16:38:16 -04006449 private Bundle toBundle() {
6450 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006451 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006452 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006453 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006454 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006455 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006456 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006457 }
6458 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006459 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006460 }
6461 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006462 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006463 }
Shane Brennan5a871862017-03-11 13:14:17 -08006464 if (mExtras != null) {
6465 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6466 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006467 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006468 }
6469
Alex Hillsd9b04d92016-04-11 16:38:16 -04006470 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6471 Bundle[] bundles = new Bundle[messages.size()];
6472 final int N = messages.size();
6473 for (int i = 0; i < N; i++) {
6474 bundles[i] = messages.get(i).toBundle();
6475 }
6476 return bundles;
6477 }
6478
Selim Cinek88188f22017-09-19 16:46:56 -07006479 /**
6480 * @return A list of messages read from the bundles.
6481 *
6482 * @hide
6483 */
6484 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6485 if (bundles == null) {
6486 return new ArrayList<>();
6487 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006488 List<Message> messages = new ArrayList<>(bundles.length);
6489 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006490 if (bundles[i] instanceof Bundle) {
6491 Message message = getMessageFromBundle((Bundle)bundles[i]);
6492 if (message != null) {
6493 messages.add(message);
6494 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006495 }
6496 }
6497 return messages;
6498 }
6499
6500 static Message getMessageFromBundle(Bundle bundle) {
6501 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006502 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006503 return null;
6504 } else {
6505 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6506 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6507 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6508 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006509 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6510 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006511 }
Shane Brennan5a871862017-03-11 13:14:17 -08006512 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6513 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6514 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006515 return message;
6516 }
6517 } catch (ClassCastException e) {
6518 return null;
6519 }
6520 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006521 }
6522 }
6523
6524 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006525 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006526 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006527 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006528 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006529 * Notification notif = new Notification.Builder(mContext)
6530 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6531 * .setContentText(subject)
6532 * .setSmallIcon(R.drawable.new_mail)
6533 * .setLargeIcon(aBitmap)
6534 * .setStyle(new Notification.InboxStyle()
6535 * .addLine(str1)
6536 * .addLine(str2)
6537 * .setContentTitle(&quot;&quot;)
6538 * .setSummaryText(&quot;+3 more&quot;))
6539 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006540 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006541 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006542 * @see Notification#bigContentView
6543 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006544 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006545 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6546
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006547 public InboxStyle() {
6548 }
6549
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006550 /**
6551 * @deprecated use {@code InboxStyle()}.
6552 */
6553 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006554 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006555 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006556 }
6557
Chris Wrend6297db2012-05-03 16:20:13 -04006558 /**
6559 * Overrides ContentTitle in the big form of the template.
6560 * This defaults to the value passed to setContentTitle().
6561 */
6562 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006563 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006564 return this;
6565 }
6566
6567 /**
6568 * Set the first line of text after the detail section in the big form of the template.
6569 */
6570 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006571 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006572 return this;
6573 }
6574
Chris Wren0bd664d2012-08-01 13:56:56 -04006575 /**
6576 * Append a line to the digest section of the Inbox notification.
6577 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006578 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006579 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006580 return this;
6581 }
6582
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006583 /**
6584 * @hide
6585 */
6586 public void addExtras(Bundle extras) {
6587 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006588
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006589 CharSequence[] a = new CharSequence[mTexts.size()];
6590 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6591 }
6592
Christoph Studer4600f9b2014-07-22 22:44:43 +02006593 /**
6594 * @hide
6595 */
6596 @Override
6597 protected void restoreFromExtras(Bundle extras) {
6598 super.restoreFromExtras(extras);
6599
6600 mTexts.clear();
6601 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6602 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6603 }
6604 }
6605
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006606 /**
6607 * @hide
6608 */
6609 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006610 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006611 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006612 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6613 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006614
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006615 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006616
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006617 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006618
Chris Wrend6297db2012-05-03 16:20:13 -04006619 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 -04006620 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006621
Chris Wren4ed80d52012-05-17 09:30:03 -04006622 // Make sure all rows are gone in case we reuse a view.
6623 for (int rowId : rowIds) {
6624 contentView.setViewVisibility(rowId, View.GONE);
6625 }
6626
Daniel Sandler879c5e02012-04-17 16:46:51 -04006627 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006628 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6629 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006630 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006631 int onlyViewId = 0;
6632 int maxRows = rowIds.length;
6633 if (mBuilder.mActions.size() > 0) {
6634 maxRows--;
6635 }
6636 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006637 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006638 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006639 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006640 contentView.setTextViewText(rowIds[i],
6641 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006642 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006643 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006644 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006645 if (first) {
6646 onlyViewId = rowIds[i];
6647 } else {
6648 onlyViewId = 0;
6649 }
Selim Cinek247fa012016-02-18 09:50:48 -08006650 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006651 }
6652 i++;
6653 }
Selim Cinek07c80172016-04-21 16:40:47 -07006654 if (onlyViewId != 0) {
6655 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6656 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6657 R.dimen.notification_text_margin_top);
6658 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6659 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006660
Daniel Sandler879c5e02012-04-17 16:46:51 -04006661 return contentView;
6662 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006663
Selim Cinek247fa012016-02-18 09:50:48 -08006664 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006665 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006666 if (first) {
6667 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6668 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6669 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006670 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006671 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006672 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006673 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006674 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006675 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006676 }
Dan Sandler842dd772014-05-15 09:36:47 -04006677
6678 /**
6679 * Notification style for media playback notifications.
6680 *
6681 * In the expanded form, {@link Notification#bigContentView}, up to 5
6682 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006683 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006684 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6685 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6686 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006687 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006688 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6689 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006690 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006691 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006692 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006693 * Notifications created with MediaStyle will have their category set to
6694 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6695 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006696 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006697 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6698 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006699 * the System UI can identify this as a notification representing an active media session
6700 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6701 *
Selim Cinek99104832017-01-25 14:47:33 -08006702 * <p>
6703 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6704 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6705 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6706 * <p>
6707 *
Dan Sandler842dd772014-05-15 09:36:47 -04006708 * To use this style with your Notification, feed it to
6709 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6710 * <pre class="prettyprint">
6711 * Notification noti = new Notification.Builder()
6712 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006713 * .setContentTitle(&quot;Track title&quot;)
6714 * .setContentText(&quot;Artist - Album&quot;)
6715 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006716 * .setStyle(<b>new Notification.MediaStyle()</b>
6717 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006718 * .build();
6719 * </pre>
6720 *
6721 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006722 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006723 */
6724 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006725 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006726 static final int MAX_MEDIA_BUTTONS = 5;
6727
6728 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006729 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006730
6731 public MediaStyle() {
6732 }
6733
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006734 /**
6735 * @deprecated use {@code MediaStyle()}.
6736 */
6737 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006738 public MediaStyle(Builder builder) {
6739 setBuilder(builder);
6740 }
6741
6742 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006743 * 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 -04006744 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006745 *
6746 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006747 */
6748 public MediaStyle setShowActionsInCompactView(int...actions) {
6749 mActionsToShowInCompact = actions;
6750 return this;
6751 }
6752
6753 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006754 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6755 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006756 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006757 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006758 mToken = token;
6759 return this;
6760 }
6761
Christoph Studer4600f9b2014-07-22 22:44:43 +02006762 /**
6763 * @hide
6764 */
Dan Sandler842dd772014-05-15 09:36:47 -04006765 @Override
6766 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006767 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006768 if (wip.category == null) {
6769 wip.category = Notification.CATEGORY_TRANSPORT;
6770 }
Dan Sandler842dd772014-05-15 09:36:47 -04006771 return wip;
6772 }
6773
Christoph Studer4600f9b2014-07-22 22:44:43 +02006774 /**
6775 * @hide
6776 */
6777 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006778 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006779 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006780 }
6781
6782 /**
6783 * @hide
6784 */
6785 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006786 public RemoteViews makeBigContentView() {
6787 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006788 }
6789
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006790 /**
6791 * @hide
6792 */
6793 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006794 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006795 RemoteViews expanded = makeMediaBigContentView();
6796 return expanded != null ? expanded : makeMediaContentView();
6797 }
6798
Dan Sandler842dd772014-05-15 09:36:47 -04006799 /** @hide */
6800 @Override
6801 public void addExtras(Bundle extras) {
6802 super.addExtras(extras);
6803
6804 if (mToken != null) {
6805 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6806 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006807 if (mActionsToShowInCompact != null) {
6808 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6809 }
Dan Sandler842dd772014-05-15 09:36:47 -04006810 }
6811
Christoph Studer4600f9b2014-07-22 22:44:43 +02006812 /**
6813 * @hide
6814 */
6815 @Override
6816 protected void restoreFromExtras(Bundle extras) {
6817 super.restoreFromExtras(extras);
6818
6819 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6820 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6821 }
6822 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6823 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6824 }
6825 }
6826
Selim Cinek5bf069a2015-11-10 19:14:27 -05006827 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006828 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006829 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006830 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006831 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006832
6833 // If the action buttons should not be tinted, then just use the default
6834 // notification color. Otherwise, just use the passed-in color.
6835 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6836 ? color
6837 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6838 Notification.COLOR_DEFAULT);
6839
Sunny Goyal5b153922017-09-21 21:00:36 -07006840 button.setDrawableTint(R.id.action0, false, tintColor,
6841 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006842 if (!tombstone) {
6843 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6844 }
6845 button.setContentDescription(R.id.action0, action.title);
6846 return button;
6847 }
6848
6849 private RemoteViews makeMediaContentView() {
6850 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006851 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006852
6853 final int numActions = mBuilder.mActions.size();
6854 final int N = mActionsToShowInCompact == null
6855 ? 0
6856 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6857 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006858 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006859 for (int i = 0; i < N; i++) {
6860 if (i >= numActions) {
6861 throw new IllegalArgumentException(String.format(
6862 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6863 i, numActions - 1));
6864 }
6865
6866 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006867 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006868 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006869 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006870 }
6871 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006872 handleImage(view);
6873 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006874 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006875 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006876 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006877 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006878 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006879 return view;
6880 }
6881
Selim Cinek99104832017-01-25 14:47:33 -08006882 private int getPrimaryHighlightColor() {
6883 return mBuilder.getPrimaryHighlightColor();
6884 }
6885
Dan Sandler842dd772014-05-15 09:36:47 -04006886 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006887 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006888 // Dont add an expanded view if there is no more content to be revealed
6889 int actionsInCompact = mActionsToShowInCompact == null
6890 ? 0
6891 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006892 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006893 return null;
6894 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006895 RemoteViews big = mBuilder.applyStandardTemplate(
6896 R.layout.notification_template_material_big_media,
6897 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006898
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006899 if (actionCount > 0) {
6900 big.removeAllViews(com.android.internal.R.id.media_actions);
6901 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006902 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006903 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006904 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006905 }
6906 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006907 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006908 return big;
6909 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006910
Selim Cinek5bf069a2015-11-10 19:14:27 -05006911 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006912 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006913 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6914 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006915 }
6916 }
6917
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006918 /**
6919 * @hide
6920 */
6921 @Override
6922 protected boolean hasProgress() {
6923 return false;
6924 }
Dan Sandler842dd772014-05-15 09:36:47 -04006925 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006926
Selim Cinek593610c2016-02-16 18:42:57 -08006927 /**
6928 * Notification style for custom views that are decorated by the system
6929 *
6930 * <p>Instead of providing a notification that is completely custom, a developer can set this
6931 * style and still obtain system decorations like the notification header with the expand
6932 * affordance and actions.
6933 *
6934 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6935 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6936 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6937 * corresponding custom views to display.
6938 *
6939 * To use this style with your Notification, feed it to
6940 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6941 * <pre class="prettyprint">
6942 * Notification noti = new Notification.Builder()
6943 * .setSmallIcon(R.drawable.ic_stat_player)
6944 * .setLargeIcon(albumArtBitmap))
6945 * .setCustomContentView(contentView);
6946 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6947 * .build();
6948 * </pre>
6949 */
6950 public static class DecoratedCustomViewStyle extends Style {
6951
6952 public DecoratedCustomViewStyle() {
6953 }
6954
Selim Cinek593610c2016-02-16 18:42:57 -08006955 /**
6956 * @hide
6957 */
6958 public boolean displayCustomViewInline() {
6959 return true;
6960 }
6961
6962 /**
6963 * @hide
6964 */
6965 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006966 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006967 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6968 }
6969
6970 /**
6971 * @hide
6972 */
6973 @Override
6974 public RemoteViews makeBigContentView() {
6975 return makeDecoratedBigContentView();
6976 }
6977
6978 /**
6979 * @hide
6980 */
6981 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006982 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006983 return makeDecoratedHeadsUpContentView();
6984 }
6985
Selim Cinek593610c2016-02-16 18:42:57 -08006986 private RemoteViews makeDecoratedHeadsUpContentView() {
6987 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6988 ? mBuilder.mN.contentView
6989 : mBuilder.mN.headsUpContentView;
6990 if (mBuilder.mActions.size() == 0) {
6991 return makeStandardTemplateWithCustomContent(headsUpContentView);
6992 }
6993 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6994 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006995 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006996 return remoteViews;
6997 }
6998
Selim Cinek593610c2016-02-16 18:42:57 -08006999 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7000 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7001 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007002 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007003 return remoteViews;
7004 }
7005
Selim Cinek593610c2016-02-16 18:42:57 -08007006 private RemoteViews makeDecoratedBigContentView() {
7007 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7008 ? mBuilder.mN.contentView
7009 : mBuilder.mN.bigContentView;
7010 if (mBuilder.mActions.size() == 0) {
7011 return makeStandardTemplateWithCustomContent(bigContentView);
7012 }
7013 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7014 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007015 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007016 return remoteViews;
7017 }
Selim Cinek247fa012016-02-18 09:50:48 -08007018
7019 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7020 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007021 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007022 // Need to clone customContent before adding, because otherwise it can no longer be
7023 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007024 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007025 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7026 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007027 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007028 }
Selim Cinek247fa012016-02-18 09:50:48 -08007029 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007030 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007031 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007032 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007033 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007034 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007035 }
Selim Cinek593610c2016-02-16 18:42:57 -08007036 }
7037
Selim Cinek03eb3b72016-02-18 10:39:45 -08007038 /**
7039 * Notification style for media custom views that are decorated by the system
7040 *
7041 * <p>Instead of providing a media notification that is completely custom, a developer can set
7042 * this style and still obtain system decorations like the notification header with the expand
7043 * affordance and actions.
7044 *
7045 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7046 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7047 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7048 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007049 * <p>
7050 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7051 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7052 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007053 * To use this style with your Notification, feed it to
7054 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7055 * <pre class="prettyprint">
7056 * Notification noti = new Notification.Builder()
7057 * .setSmallIcon(R.drawable.ic_stat_player)
7058 * .setLargeIcon(albumArtBitmap))
7059 * .setCustomContentView(contentView);
7060 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7061 * .setMediaSession(mySession))
7062 * .build();
7063 * </pre>
7064 *
7065 * @see android.app.Notification.DecoratedCustomViewStyle
7066 * @see android.app.Notification.MediaStyle
7067 */
7068 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7069
7070 public DecoratedMediaCustomViewStyle() {
7071 }
7072
Selim Cinek03eb3b72016-02-18 10:39:45 -08007073 /**
7074 * @hide
7075 */
7076 public boolean displayCustomViewInline() {
7077 return true;
7078 }
7079
7080 /**
7081 * @hide
7082 */
7083 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007084 public RemoteViews makeContentView(boolean increasedHeight) {
7085 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007086 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7087 mBuilder.mN.contentView);
7088 }
7089
7090 /**
7091 * @hide
7092 */
7093 @Override
7094 public RemoteViews makeBigContentView() {
7095 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7096 ? mBuilder.mN.bigContentView
7097 : mBuilder.mN.contentView;
7098 return makeBigContentViewWithCustomContent(customRemoteView);
7099 }
7100
7101 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7102 RemoteViews remoteViews = super.makeBigContentView();
7103 if (remoteViews != null) {
7104 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7105 customRemoteView);
7106 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007107 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007108 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7109 customRemoteView);
7110 } else {
7111 return null;
7112 }
7113 }
7114
7115 /**
7116 * @hide
7117 */
7118 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007119 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007120 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7121 ? mBuilder.mN.headsUpContentView
7122 : mBuilder.mN.contentView;
7123 return makeBigContentViewWithCustomContent(customRemoteView);
7124 }
7125
7126 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7127 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007128 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007129 // Need to clone customContent before adding, because otherwise it can no longer be
7130 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007131 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007132 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007133 remoteViews.removeAllViews(id);
7134 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007135 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007136 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007137 return remoteViews;
7138 }
7139 }
7140
Selim Cineke7238dd2017-12-14 17:48:32 -08007141 /**
7142 * A Person associated with this Notification.
7143 */
7144 public static final class Person implements Parcelable {
7145 @Nullable private CharSequence mName;
7146 @Nullable private Icon mIcon;
7147 @Nullable private String mUri;
7148 @Nullable private String mKey;
7149
7150 protected Person(Parcel in) {
7151 mName = in.readCharSequence();
7152 if (in.readInt() != 0) {
7153 mIcon = Icon.CREATOR.createFromParcel(in);
7154 }
7155 mUri = in.readString();
7156 mKey = in.readString();
7157 }
7158
7159 /**
7160 * Create a new person.
7161 */
7162 public Person() {
7163 }
7164
7165 /**
7166 * Give this person a name.
7167 *
7168 * @param name the name of this person
7169 */
7170 public Person setName(@Nullable CharSequence name) {
7171 this.mName = name;
7172 return this;
7173 }
7174
7175 /**
7176 * Add an icon for this person.
7177 * <br />
7178 * This is currently only used for {@link MessagingStyle} notifications and should not be
7179 * provided otherwise, in order to save memory. The system will prefer this icon over any
7180 * images that are resolved from the URI.
7181 *
7182 * @param icon the icon of the person
7183 */
7184 public Person setIcon(@Nullable Icon icon) {
7185 this.mIcon = icon;
7186 return this;
7187 }
7188
7189 /**
7190 * Set a URI associated with this person.
7191 *
7192 * <P>
7193 * Depending on user preferences, adding a URI to a Person may allow the notification to
7194 * pass through interruption filters, if this notification is of
7195 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7196 * The addition of people may also cause this notification to appear more prominently in
7197 * the user interface.
7198 * </P>
7199 *
7200 * <P>
7201 * The person should be specified by the {@code String} representation of a
7202 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7203 * </P>
7204 *
7205 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7206 * URIs. The path part of these URIs must exist in the contacts database, in the
7207 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7208 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7209 * </P>
7210 *
7211 * @param uri a URI for the person
7212 */
7213 public Person setUri(@Nullable String uri) {
7214 mUri = uri;
7215 return this;
7216 }
7217
7218 /**
7219 * Add a key to this person in order to uniquely identify it.
7220 * This is especially useful if the name doesn't uniquely identify this person or if the
7221 * display name is a short handle of the actual name.
7222 *
7223 * <P>If no key is provided, the name serves as as the key for the purpose of
7224 * identification.</P>
7225 *
7226 * @param key the key that uniquely identifies this person
7227 */
7228 public Person setKey(@Nullable String key) {
7229 mKey = key;
7230 return this;
7231 }
7232
7233
7234 /**
7235 * @return the uri provided for this person or {@code null} if no Uri was provided
7236 */
7237 @Nullable
7238 public String getUri() {
7239 return mUri;
7240 }
7241
7242 /**
7243 * @return the name provided for this person or {@code null} if no name was provided
7244 */
7245 @Nullable
7246 public CharSequence getName() {
7247 return mName;
7248 }
7249
7250 /**
7251 * @return the icon provided for this person or {@code null} if no icon was provided
7252 */
7253 @Nullable
7254 public Icon getIcon() {
7255 return mIcon;
7256 }
7257
7258 /**
7259 * @return the key provided for this person or {@code null} if no key was provided
7260 */
7261 @Nullable
7262 public String getKey() {
7263 return mKey;
7264 }
7265
7266 /**
7267 * @return the URI associated with this person, or "name:mName" otherwise
7268 * @hide
7269 */
7270 public String resolveToLegacyUri() {
7271 if (mUri != null) {
7272 return mUri;
7273 }
7274 if (mName != null) {
7275 return "name:" + mName;
7276 }
7277 return "";
7278 }
7279
7280 @Override
7281 public int describeContents() {
7282 return 0;
7283 }
7284
7285 @Override
7286 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7287 dest.writeCharSequence(mName);
7288 if (mIcon != null) {
7289 dest.writeInt(1);
7290 mIcon.writeToParcel(dest, 0);
7291 } else {
7292 dest.writeInt(0);
7293 }
7294 dest.writeString(mUri);
7295 dest.writeString(mKey);
7296 }
7297
7298 public static final Creator<Person> CREATOR = new Creator<Person>() {
7299 @Override
7300 public Person createFromParcel(Parcel in) {
7301 return new Person(in);
7302 }
7303
7304 @Override
7305 public Person[] newArray(int size) {
7306 return new Person[size];
7307 }
7308 };
7309 }
7310
Christoph Studer4600f9b2014-07-22 22:44:43 +02007311 // When adding a new Style subclass here, don't forget to update
7312 // Builder.getNotificationStyleClass.
7313
Griff Hazen61a9e862014-05-22 16:05:19 -07007314 /**
7315 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7316 * metadata or change options on a notification builder.
7317 */
7318 public interface Extender {
7319 /**
7320 * Apply this extender to a notification builder.
7321 * @param builder the builder to be modified.
7322 * @return the build object for chaining.
7323 */
7324 public Builder extend(Builder builder);
7325 }
7326
7327 /**
7328 * Helper class to add wearable extensions to notifications.
7329 * <p class="note"> See
7330 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7331 * for Android Wear</a> for more information on how to use this class.
7332 * <p>
7333 * To create a notification with wearable extensions:
7334 * <ol>
7335 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7336 * properties.
7337 * <li>Create a {@link android.app.Notification.WearableExtender}.
7338 * <li>Set wearable-specific properties using the
7339 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7340 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7341 * notification.
7342 * <li>Post the notification to the notification system with the
7343 * {@code NotificationManager.notify(...)} methods.
7344 * </ol>
7345 *
7346 * <pre class="prettyprint">
7347 * Notification notif = new Notification.Builder(mContext)
7348 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7349 * .setContentText(subject)
7350 * .setSmallIcon(R.drawable.new_mail)
7351 * .extend(new Notification.WearableExtender()
7352 * .setContentIcon(R.drawable.new_mail))
7353 * .build();
7354 * NotificationManager notificationManger =
7355 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7356 * notificationManger.notify(0, notif);</pre>
7357 *
7358 * <p>Wearable extensions can be accessed on an existing notification by using the
7359 * {@code WearableExtender(Notification)} constructor,
7360 * and then using the {@code get} methods to access values.
7361 *
7362 * <pre class="prettyprint">
7363 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7364 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007365 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007366 */
7367 public static final class WearableExtender implements Extender {
7368 /**
7369 * Sentinel value for an action index that is unset.
7370 */
7371 public static final int UNSET_ACTION_INDEX = -1;
7372
7373 /**
7374 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7375 * default sizing.
7376 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007377 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007378 * on their content.
7379 */
7380 public static final int SIZE_DEFAULT = 0;
7381
7382 /**
7383 * Size value for use with {@link #setCustomSizePreset} to show this notification
7384 * with an extra small size.
7385 * <p>This value is only applicable for custom display notifications created using
7386 * {@link #setDisplayIntent}.
7387 */
7388 public static final int SIZE_XSMALL = 1;
7389
7390 /**
7391 * Size value for use with {@link #setCustomSizePreset} to show this notification
7392 * with a small size.
7393 * <p>This value is only applicable for custom display notifications created using
7394 * {@link #setDisplayIntent}.
7395 */
7396 public static final int SIZE_SMALL = 2;
7397
7398 /**
7399 * Size value for use with {@link #setCustomSizePreset} to show this notification
7400 * with a medium size.
7401 * <p>This value is only applicable for custom display notifications created using
7402 * {@link #setDisplayIntent}.
7403 */
7404 public static final int SIZE_MEDIUM = 3;
7405
7406 /**
7407 * Size value for use with {@link #setCustomSizePreset} to show this notification
7408 * with a large size.
7409 * <p>This value is only applicable for custom display notifications created using
7410 * {@link #setDisplayIntent}.
7411 */
7412 public static final int SIZE_LARGE = 4;
7413
Griff Hazend5f11f92014-05-27 15:40:09 -07007414 /**
7415 * Size value for use with {@link #setCustomSizePreset} to show this notification
7416 * full screen.
7417 * <p>This value is only applicable for custom display notifications created using
7418 * {@link #setDisplayIntent}.
7419 */
7420 public static final int SIZE_FULL_SCREEN = 5;
7421
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007422 /**
7423 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7424 * short amount of time when this notification is displayed on the screen. This
7425 * is the default value.
7426 */
7427 public static final int SCREEN_TIMEOUT_SHORT = 0;
7428
7429 /**
7430 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7431 * for a longer amount of time when this notification is displayed on the screen.
7432 */
7433 public static final int SCREEN_TIMEOUT_LONG = -1;
7434
Griff Hazen61a9e862014-05-22 16:05:19 -07007435 /** Notification extra which contains wearable extensions */
7436 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7437
Pete Gastaf6781d2014-10-07 15:17:05 -04007438 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007439 private static final String KEY_ACTIONS = "actions";
7440 private static final String KEY_FLAGS = "flags";
7441 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7442 private static final String KEY_PAGES = "pages";
7443 private static final String KEY_BACKGROUND = "background";
7444 private static final String KEY_CONTENT_ICON = "contentIcon";
7445 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7446 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7447 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7448 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7449 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007450 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007451 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007452 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007453
7454 // Flags bitwise-ored to mFlags
7455 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7456 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7457 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7458 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007459 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007460 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007461 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007462
7463 // Default value for flags integer
7464 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7465
7466 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7467 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7468
7469 private ArrayList<Action> mActions = new ArrayList<Action>();
7470 private int mFlags = DEFAULT_FLAGS;
7471 private PendingIntent mDisplayIntent;
7472 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7473 private Bitmap mBackground;
7474 private int mContentIcon;
7475 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7476 private int mContentActionIndex = UNSET_ACTION_INDEX;
7477 private int mCustomSizePreset = SIZE_DEFAULT;
7478 private int mCustomContentHeight;
7479 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007480 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007481 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007482 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007483
7484 /**
7485 * Create a {@link android.app.Notification.WearableExtender} with default
7486 * options.
7487 */
7488 public WearableExtender() {
7489 }
7490
7491 public WearableExtender(Notification notif) {
7492 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7493 if (wearableBundle != null) {
7494 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7495 if (actions != null) {
7496 mActions.addAll(actions);
7497 }
7498
7499 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7500 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7501
7502 Notification[] pages = getNotificationArrayFromBundle(
7503 wearableBundle, KEY_PAGES);
7504 if (pages != null) {
7505 Collections.addAll(mPages, pages);
7506 }
7507
7508 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7509 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7510 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7511 DEFAULT_CONTENT_ICON_GRAVITY);
7512 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7513 UNSET_ACTION_INDEX);
7514 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7515 SIZE_DEFAULT);
7516 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7517 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007518 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007519 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007520 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007521 }
7522 }
7523
7524 /**
7525 * Apply wearable extensions to a notification that is being built. This is typically
7526 * called by the {@link android.app.Notification.Builder#extend} method of
7527 * {@link android.app.Notification.Builder}.
7528 */
7529 @Override
7530 public Notification.Builder extend(Notification.Builder builder) {
7531 Bundle wearableBundle = new Bundle();
7532
7533 if (!mActions.isEmpty()) {
7534 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7535 }
7536 if (mFlags != DEFAULT_FLAGS) {
7537 wearableBundle.putInt(KEY_FLAGS, mFlags);
7538 }
7539 if (mDisplayIntent != null) {
7540 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7541 }
7542 if (!mPages.isEmpty()) {
7543 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7544 new Notification[mPages.size()]));
7545 }
7546 if (mBackground != null) {
7547 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7548 }
7549 if (mContentIcon != 0) {
7550 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7551 }
7552 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7553 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7554 }
7555 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7556 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7557 mContentActionIndex);
7558 }
7559 if (mCustomSizePreset != SIZE_DEFAULT) {
7560 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7561 }
7562 if (mCustomContentHeight != 0) {
7563 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7564 }
7565 if (mGravity != DEFAULT_GRAVITY) {
7566 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7567 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007568 if (mHintScreenTimeout != 0) {
7569 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7570 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007571 if (mDismissalId != null) {
7572 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7573 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007574 if (mBridgeTag != null) {
7575 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7576 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007577
7578 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7579 return builder;
7580 }
7581
7582 @Override
7583 public WearableExtender clone() {
7584 WearableExtender that = new WearableExtender();
7585 that.mActions = new ArrayList<Action>(this.mActions);
7586 that.mFlags = this.mFlags;
7587 that.mDisplayIntent = this.mDisplayIntent;
7588 that.mPages = new ArrayList<Notification>(this.mPages);
7589 that.mBackground = this.mBackground;
7590 that.mContentIcon = this.mContentIcon;
7591 that.mContentIconGravity = this.mContentIconGravity;
7592 that.mContentActionIndex = this.mContentActionIndex;
7593 that.mCustomSizePreset = this.mCustomSizePreset;
7594 that.mCustomContentHeight = this.mCustomContentHeight;
7595 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007596 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007597 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007598 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007599 return that;
7600 }
7601
7602 /**
7603 * Add a wearable action to this notification.
7604 *
7605 * <p>When wearable actions are added using this method, the set of actions that
7606 * show on a wearable device splits from devices that only show actions added
7607 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7608 * of which actions display on different devices.
7609 *
7610 * @param action the action to add to this notification
7611 * @return this object for method chaining
7612 * @see android.app.Notification.Action
7613 */
7614 public WearableExtender addAction(Action action) {
7615 mActions.add(action);
7616 return this;
7617 }
7618
7619 /**
7620 * Adds wearable actions to this notification.
7621 *
7622 * <p>When wearable actions are added using this method, the set of actions that
7623 * show on a wearable device splits from devices that only show actions added
7624 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7625 * of which actions display on different devices.
7626 *
7627 * @param actions the actions to add to this notification
7628 * @return this object for method chaining
7629 * @see android.app.Notification.Action
7630 */
7631 public WearableExtender addActions(List<Action> actions) {
7632 mActions.addAll(actions);
7633 return this;
7634 }
7635
7636 /**
7637 * Clear all wearable actions present on this builder.
7638 * @return this object for method chaining.
7639 * @see #addAction
7640 */
7641 public WearableExtender clearActions() {
7642 mActions.clear();
7643 return this;
7644 }
7645
7646 /**
7647 * Get the wearable actions present on this notification.
7648 */
7649 public List<Action> getActions() {
7650 return mActions;
7651 }
7652
7653 /**
7654 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007655 * this notification. The {@link PendingIntent} provided should be for an activity.
7656 *
7657 * <pre class="prettyprint">
7658 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7659 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7660 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7661 * Notification notif = new Notification.Builder(context)
7662 * .extend(new Notification.WearableExtender()
7663 * .setDisplayIntent(displayPendingIntent)
7664 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7665 * .build();</pre>
7666 *
7667 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007668 * should have an empty task affinity. It is also recommended to use the device
7669 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007670 *
7671 * <p>Example AndroidManifest.xml entry:
7672 * <pre class="prettyprint">
7673 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7674 * android:exported=&quot;true&quot;
7675 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007676 * android:taskAffinity=&quot;&quot;
7677 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007678 *
7679 * @param intent the {@link PendingIntent} for an activity
7680 * @return this object for method chaining
7681 * @see android.app.Notification.WearableExtender#getDisplayIntent
7682 */
7683 public WearableExtender setDisplayIntent(PendingIntent intent) {
7684 mDisplayIntent = intent;
7685 return this;
7686 }
7687
7688 /**
7689 * Get the intent to launch inside of an activity view when displaying this
7690 * notification. This {@code PendingIntent} should be for an activity.
7691 */
7692 public PendingIntent getDisplayIntent() {
7693 return mDisplayIntent;
7694 }
7695
7696 /**
7697 * Add an additional page of content to display with this notification. The current
7698 * notification forms the first page, and pages added using this function form
7699 * subsequent pages. This field can be used to separate a notification into multiple
7700 * sections.
7701 *
7702 * @param page the notification to add as another page
7703 * @return this object for method chaining
7704 * @see android.app.Notification.WearableExtender#getPages
7705 */
7706 public WearableExtender addPage(Notification page) {
7707 mPages.add(page);
7708 return this;
7709 }
7710
7711 /**
7712 * Add additional pages of content to display with this notification. The current
7713 * notification forms the first page, and pages added using this function form
7714 * subsequent pages. This field can be used to separate a notification into multiple
7715 * sections.
7716 *
7717 * @param pages a list of notifications
7718 * @return this object for method chaining
7719 * @see android.app.Notification.WearableExtender#getPages
7720 */
7721 public WearableExtender addPages(List<Notification> pages) {
7722 mPages.addAll(pages);
7723 return this;
7724 }
7725
7726 /**
7727 * Clear all additional pages present on this builder.
7728 * @return this object for method chaining.
7729 * @see #addPage
7730 */
7731 public WearableExtender clearPages() {
7732 mPages.clear();
7733 return this;
7734 }
7735
7736 /**
7737 * Get the array of additional pages of content for displaying this notification. The
7738 * current notification forms the first page, and elements within this array form
7739 * subsequent pages. This field can be used to separate a notification into multiple
7740 * sections.
7741 * @return the pages for this notification
7742 */
7743 public List<Notification> getPages() {
7744 return mPages;
7745 }
7746
7747 /**
7748 * Set a background image to be displayed behind the notification content.
7749 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7750 * will work with any notification style.
7751 *
7752 * @param background the background bitmap
7753 * @return this object for method chaining
7754 * @see android.app.Notification.WearableExtender#getBackground
7755 */
7756 public WearableExtender setBackground(Bitmap background) {
7757 mBackground = background;
7758 return this;
7759 }
7760
7761 /**
7762 * Get a background image to be displayed behind the notification content.
7763 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7764 * will work with any notification style.
7765 *
7766 * @return the background image
7767 * @see android.app.Notification.WearableExtender#setBackground
7768 */
7769 public Bitmap getBackground() {
7770 return mBackground;
7771 }
7772
7773 /**
7774 * Set an icon that goes with the content of this notification.
7775 */
7776 public WearableExtender setContentIcon(int icon) {
7777 mContentIcon = icon;
7778 return this;
7779 }
7780
7781 /**
7782 * Get an icon that goes with the content of this notification.
7783 */
7784 public int getContentIcon() {
7785 return mContentIcon;
7786 }
7787
7788 /**
7789 * Set the gravity that the content icon should have within the notification display.
7790 * Supported values include {@link android.view.Gravity#START} and
7791 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7792 * @see #setContentIcon
7793 */
7794 public WearableExtender setContentIconGravity(int contentIconGravity) {
7795 mContentIconGravity = contentIconGravity;
7796 return this;
7797 }
7798
7799 /**
7800 * Get the gravity that the content icon should have within the notification display.
7801 * Supported values include {@link android.view.Gravity#START} and
7802 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7803 * @see #getContentIcon
7804 */
7805 public int getContentIconGravity() {
7806 return mContentIconGravity;
7807 }
7808
7809 /**
7810 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007811 * this notification. This action will no longer display separately from the
7812 * notification's content.
7813 *
Griff Hazenca48d352014-05-28 22:37:13 -07007814 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007815 * set, although the list of available actions comes from the main notification and not
7816 * from the child page's notification.
7817 *
7818 * @param actionIndex The index of the action to hoist onto the current notification page.
7819 * If wearable actions were added to the main notification, this index
7820 * will apply to that list, otherwise it will apply to the regular
7821 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007822 */
7823 public WearableExtender setContentAction(int actionIndex) {
7824 mContentActionIndex = actionIndex;
7825 return this;
7826 }
7827
7828 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007829 * Get the index of the notification action, if any, that was specified as being clickable
7830 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007831 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007832 *
Griff Hazenca48d352014-05-28 22:37:13 -07007833 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007834 * set, although the list of available actions comes from the main notification and not
7835 * from the child page's notification.
7836 *
7837 * <p>If wearable specific actions were added to the main notification, this index will
7838 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007839 *
7840 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007841 */
7842 public int getContentAction() {
7843 return mContentActionIndex;
7844 }
7845
7846 /**
7847 * Set the gravity that this notification should have within the available viewport space.
7848 * Supported values include {@link android.view.Gravity#TOP},
7849 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7850 * The default value is {@link android.view.Gravity#BOTTOM}.
7851 */
7852 public WearableExtender setGravity(int gravity) {
7853 mGravity = gravity;
7854 return this;
7855 }
7856
7857 /**
7858 * Get the gravity that this notification should have within the available viewport space.
7859 * Supported values include {@link android.view.Gravity#TOP},
7860 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7861 * The default value is {@link android.view.Gravity#BOTTOM}.
7862 */
7863 public int getGravity() {
7864 return mGravity;
7865 }
7866
7867 /**
7868 * Set the custom size preset for the display of this notification out of the available
7869 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7870 * {@link #SIZE_LARGE}.
7871 * <p>Some custom size presets are only applicable for custom display notifications created
7872 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7873 * documentation for the preset in question. See also
7874 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7875 */
7876 public WearableExtender setCustomSizePreset(int sizePreset) {
7877 mCustomSizePreset = sizePreset;
7878 return this;
7879 }
7880
7881 /**
7882 * Get the custom size preset for the display of this notification out of the available
7883 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7884 * {@link #SIZE_LARGE}.
7885 * <p>Some custom size presets are only applicable for custom display notifications created
7886 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7887 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7888 */
7889 public int getCustomSizePreset() {
7890 return mCustomSizePreset;
7891 }
7892
7893 /**
7894 * Set the custom height in pixels for the display of this notification's content.
7895 * <p>This option is only available for custom display notifications created
7896 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7897 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7898 * {@link #getCustomContentHeight}.
7899 */
7900 public WearableExtender setCustomContentHeight(int height) {
7901 mCustomContentHeight = height;
7902 return this;
7903 }
7904
7905 /**
7906 * Get the custom height in pixels for the display of this notification's content.
7907 * <p>This option is only available for custom display notifications created
7908 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7909 * {@link #setCustomContentHeight}.
7910 */
7911 public int getCustomContentHeight() {
7912 return mCustomContentHeight;
7913 }
7914
7915 /**
7916 * Set whether the scrolling position for the contents of this notification should start
7917 * at the bottom of the contents instead of the top when the contents are too long to
7918 * display within the screen. Default is false (start scroll at the top).
7919 */
7920 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7921 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7922 return this;
7923 }
7924
7925 /**
7926 * Get whether the scrolling position for the contents of this notification should start
7927 * at the bottom of the contents instead of the top when the contents are too long to
7928 * display within the screen. Default is false (start scroll at the top).
7929 */
7930 public boolean getStartScrollBottom() {
7931 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7932 }
7933
7934 /**
7935 * Set whether the content intent is available when the wearable device is not connected
7936 * to a companion device. The user can still trigger this intent when the wearable device
7937 * is offline, but a visual hint will indicate that the content intent may not be available.
7938 * Defaults to true.
7939 */
7940 public WearableExtender setContentIntentAvailableOffline(
7941 boolean contentIntentAvailableOffline) {
7942 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7943 return this;
7944 }
7945
7946 /**
7947 * Get whether the content intent is available when the wearable device is not connected
7948 * to a companion device. The user can still trigger this intent when the wearable device
7949 * is offline, but a visual hint will indicate that the content intent may not be available.
7950 * Defaults to true.
7951 */
7952 public boolean getContentIntentAvailableOffline() {
7953 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7954 }
7955
7956 /**
7957 * Set a hint that this notification's icon should not be displayed.
7958 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7959 * @return this object for method chaining
7960 */
7961 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7962 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7963 return this;
7964 }
7965
7966 /**
7967 * Get a hint that this notification's icon should not be displayed.
7968 * @return {@code true} if this icon should not be displayed, false otherwise.
7969 * The default value is {@code false} if this was never set.
7970 */
7971 public boolean getHintHideIcon() {
7972 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7973 }
7974
7975 /**
7976 * Set a visual hint that only the background image of this notification should be
7977 * displayed, and other semantic content should be hidden. This hint is only applicable
7978 * to sub-pages added using {@link #addPage}.
7979 */
7980 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7981 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7982 return this;
7983 }
7984
7985 /**
7986 * Get a visual hint that only the background image of this notification should be
7987 * displayed, and other semantic content should be hidden. This hint is only applicable
7988 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7989 */
7990 public boolean getHintShowBackgroundOnly() {
7991 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7992 }
7993
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007994 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007995 * Set a hint that this notification's background should not be clipped if possible,
7996 * and should instead be resized to fully display on the screen, retaining the aspect
7997 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007998 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7999 * @return this object for method chaining
8000 */
8001 public WearableExtender setHintAvoidBackgroundClipping(
8002 boolean hintAvoidBackgroundClipping) {
8003 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8004 return this;
8005 }
8006
8007 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008008 * Get a hint that this notification's background should not be clipped if possible,
8009 * and should instead be resized to fully display on the screen, retaining the aspect
8010 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008011 * @return {@code true} if it's ok if the background is clipped on the screen, false
8012 * otherwise. The default value is {@code false} if this was never set.
8013 */
8014 public boolean getHintAvoidBackgroundClipping() {
8015 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8016 }
8017
8018 /**
8019 * Set a hint that the screen should remain on for at least this duration when
8020 * this notification is displayed on the screen.
8021 * @param timeout The requested screen timeout in milliseconds. Can also be either
8022 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8023 * @return this object for method chaining
8024 */
8025 public WearableExtender setHintScreenTimeout(int timeout) {
8026 mHintScreenTimeout = timeout;
8027 return this;
8028 }
8029
8030 /**
8031 * Get the duration, in milliseconds, that the screen should remain on for
8032 * when this notification is displayed.
8033 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8034 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8035 */
8036 public int getHintScreenTimeout() {
8037 return mHintScreenTimeout;
8038 }
8039
Alex Hills9ab3a232016-04-05 14:54:56 -04008040 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008041 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8042 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8043 * qr codes, as well as other simple black-and-white tickets.
8044 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8045 * @return this object for method chaining
8046 */
8047 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8048 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8049 return this;
8050 }
8051
8052 /**
8053 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8054 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8055 * qr codes, as well as other simple black-and-white tickets.
8056 * @return {@code true} if it should be displayed in ambient, false otherwise
8057 * otherwise. The default value is {@code false} if this was never set.
8058 */
8059 public boolean getHintAmbientBigPicture() {
8060 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8061 }
8062
8063 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008064 * Set a hint that this notification's content intent will launch an {@link Activity}
8065 * directly, telling the platform that it can generate the appropriate transitions.
8066 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8067 * an activity and transitions should be generated, false otherwise.
8068 * @return this object for method chaining
8069 */
8070 public WearableExtender setHintContentIntentLaunchesActivity(
8071 boolean hintContentIntentLaunchesActivity) {
8072 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8073 return this;
8074 }
8075
8076 /**
8077 * Get a hint that this notification's content intent will launch an {@link Activity}
8078 * directly, telling the platform that it can generate the appropriate transitions
8079 * @return {@code true} if the content intent will launch an activity and transitions should
8080 * be generated, false otherwise. The default value is {@code false} if this was never set.
8081 */
8082 public boolean getHintContentIntentLaunchesActivity() {
8083 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8084 }
8085
Nadia Benbernou948627e2016-04-14 14:41:08 -04008086 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008087 * Sets the dismissal id for this notification. If a notification is posted with a
8088 * dismissal id, then when that notification is canceled, notifications on other wearables
8089 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008090 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008091 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008092 * @param dismissalId the dismissal id of the notification.
8093 * @return this object for method chaining
8094 */
8095 public WearableExtender setDismissalId(String dismissalId) {
8096 mDismissalId = dismissalId;
8097 return this;
8098 }
8099
8100 /**
8101 * Returns the dismissal id of the notification.
8102 * @return the dismissal id of the notification or null if it has not been set.
8103 */
8104 public String getDismissalId() {
8105 return mDismissalId;
8106 }
8107
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008108 /**
8109 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8110 * posted from a phone to provide finer-grained control on what notifications are bridged
8111 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8112 * Features to Notifications</a> for more information.
8113 * @param bridgeTag the bridge tag of the notification.
8114 * @return this object for method chaining
8115 */
8116 public WearableExtender setBridgeTag(String bridgeTag) {
8117 mBridgeTag = bridgeTag;
8118 return this;
8119 }
8120
8121 /**
8122 * Returns the bridge tag of the notification.
8123 * @return the bridge tag or null if not present.
8124 */
8125 public String getBridgeTag() {
8126 return mBridgeTag;
8127 }
8128
Griff Hazen61a9e862014-05-22 16:05:19 -07008129 private void setFlag(int mask, boolean value) {
8130 if (value) {
8131 mFlags |= mask;
8132 } else {
8133 mFlags &= ~mask;
8134 }
8135 }
8136 }
8137
8138 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008139 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8140 * with car extensions:
8141 *
8142 * <ol>
8143 * <li>Create an {@link Notification.Builder}, setting any desired
8144 * properties.
8145 * <li>Create a {@link CarExtender}.
8146 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8147 * {@link CarExtender}.
8148 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8149 * to apply the extensions to a notification.
8150 * </ol>
8151 *
8152 * <pre class="prettyprint">
8153 * Notification notification = new Notification.Builder(context)
8154 * ...
8155 * .extend(new CarExtender()
8156 * .set*(...))
8157 * .build();
8158 * </pre>
8159 *
8160 * <p>Car extensions can be accessed on an existing notification by using the
8161 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8162 * to access values.
8163 */
8164 public static final class CarExtender implements Extender {
8165 private static final String TAG = "CarExtender";
8166
8167 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8168 private static final String EXTRA_LARGE_ICON = "large_icon";
8169 private static final String EXTRA_CONVERSATION = "car_conversation";
8170 private static final String EXTRA_COLOR = "app_color";
8171
8172 private Bitmap mLargeIcon;
8173 private UnreadConversation mUnreadConversation;
8174 private int mColor = Notification.COLOR_DEFAULT;
8175
8176 /**
8177 * Create a {@link CarExtender} with default options.
8178 */
8179 public CarExtender() {
8180 }
8181
8182 /**
8183 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8184 *
8185 * @param notif The notification from which to copy options.
8186 */
8187 public CarExtender(Notification notif) {
8188 Bundle carBundle = notif.extras == null ?
8189 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8190 if (carBundle != null) {
8191 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8192 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8193
8194 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8195 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8196 }
8197 }
8198
8199 /**
8200 * Apply car extensions to a notification that is being built. This is typically called by
8201 * the {@link Notification.Builder#extend(Notification.Extender)}
8202 * method of {@link Notification.Builder}.
8203 */
8204 @Override
8205 public Notification.Builder extend(Notification.Builder builder) {
8206 Bundle carExtensions = new Bundle();
8207
8208 if (mLargeIcon != null) {
8209 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8210 }
8211 if (mColor != Notification.COLOR_DEFAULT) {
8212 carExtensions.putInt(EXTRA_COLOR, mColor);
8213 }
8214
8215 if (mUnreadConversation != null) {
8216 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8217 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8218 }
8219
8220 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8221 return builder;
8222 }
8223
8224 /**
8225 * Sets the accent color to use when Android Auto presents the notification.
8226 *
8227 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8228 * to accent the displayed notification. However, not all colors are acceptable in an
8229 * automotive setting. This method can be used to override the color provided in the
8230 * notification in such a situation.
8231 */
Tor Norbye80756e32015-03-02 09:39:27 -08008232 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008233 mColor = color;
8234 return this;
8235 }
8236
8237 /**
8238 * Gets the accent color.
8239 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008240 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008241 */
Tor Norbye80756e32015-03-02 09:39:27 -08008242 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008243 public int getColor() {
8244 return mColor;
8245 }
8246
8247 /**
8248 * Sets the large icon of the car notification.
8249 *
8250 * If no large icon is set in the extender, Android Auto will display the icon
8251 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8252 *
8253 * @param largeIcon The large icon to use in the car notification.
8254 * @return This object for method chaining.
8255 */
8256 public CarExtender setLargeIcon(Bitmap largeIcon) {
8257 mLargeIcon = largeIcon;
8258 return this;
8259 }
8260
8261 /**
8262 * Gets the large icon used in this car notification, or null if no icon has been set.
8263 *
8264 * @return The large icon for the car notification.
8265 * @see CarExtender#setLargeIcon
8266 */
8267 public Bitmap getLargeIcon() {
8268 return mLargeIcon;
8269 }
8270
8271 /**
8272 * Sets the unread conversation in a message notification.
8273 *
8274 * @param unreadConversation The unread part of the conversation this notification conveys.
8275 * @return This object for method chaining.
8276 */
8277 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8278 mUnreadConversation = unreadConversation;
8279 return this;
8280 }
8281
8282 /**
8283 * Returns the unread conversation conveyed by this notification.
8284 * @see #setUnreadConversation(UnreadConversation)
8285 */
8286 public UnreadConversation getUnreadConversation() {
8287 return mUnreadConversation;
8288 }
8289
8290 /**
8291 * A class which holds the unread messages from a conversation.
8292 */
8293 public static class UnreadConversation {
8294 private static final String KEY_AUTHOR = "author";
8295 private static final String KEY_TEXT = "text";
8296 private static final String KEY_MESSAGES = "messages";
8297 private static final String KEY_REMOTE_INPUT = "remote_input";
8298 private static final String KEY_ON_REPLY = "on_reply";
8299 private static final String KEY_ON_READ = "on_read";
8300 private static final String KEY_PARTICIPANTS = "participants";
8301 private static final String KEY_TIMESTAMP = "timestamp";
8302
8303 private final String[] mMessages;
8304 private final RemoteInput mRemoteInput;
8305 private final PendingIntent mReplyPendingIntent;
8306 private final PendingIntent mReadPendingIntent;
8307 private final String[] mParticipants;
8308 private final long mLatestTimestamp;
8309
8310 UnreadConversation(String[] messages, RemoteInput remoteInput,
8311 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8312 String[] participants, long latestTimestamp) {
8313 mMessages = messages;
8314 mRemoteInput = remoteInput;
8315 mReadPendingIntent = readPendingIntent;
8316 mReplyPendingIntent = replyPendingIntent;
8317 mParticipants = participants;
8318 mLatestTimestamp = latestTimestamp;
8319 }
8320
8321 /**
8322 * Gets the list of messages conveyed by this notification.
8323 */
8324 public String[] getMessages() {
8325 return mMessages;
8326 }
8327
8328 /**
8329 * Gets the remote input that will be used to convey the response to a message list, or
8330 * null if no such remote input exists.
8331 */
8332 public RemoteInput getRemoteInput() {
8333 return mRemoteInput;
8334 }
8335
8336 /**
8337 * Gets the pending intent that will be triggered when the user replies to this
8338 * notification.
8339 */
8340 public PendingIntent getReplyPendingIntent() {
8341 return mReplyPendingIntent;
8342 }
8343
8344 /**
8345 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8346 * in this object's message list.
8347 */
8348 public PendingIntent getReadPendingIntent() {
8349 return mReadPendingIntent;
8350 }
8351
8352 /**
8353 * Gets the participants in the conversation.
8354 */
8355 public String[] getParticipants() {
8356 return mParticipants;
8357 }
8358
8359 /**
8360 * Gets the firs participant in the conversation.
8361 */
8362 public String getParticipant() {
8363 return mParticipants.length > 0 ? mParticipants[0] : null;
8364 }
8365
8366 /**
8367 * Gets the timestamp of the conversation.
8368 */
8369 public long getLatestTimestamp() {
8370 return mLatestTimestamp;
8371 }
8372
8373 Bundle getBundleForUnreadConversation() {
8374 Bundle b = new Bundle();
8375 String author = null;
8376 if (mParticipants != null && mParticipants.length > 1) {
8377 author = mParticipants[0];
8378 }
8379 Parcelable[] messages = new Parcelable[mMessages.length];
8380 for (int i = 0; i < messages.length; i++) {
8381 Bundle m = new Bundle();
8382 m.putString(KEY_TEXT, mMessages[i]);
8383 m.putString(KEY_AUTHOR, author);
8384 messages[i] = m;
8385 }
8386 b.putParcelableArray(KEY_MESSAGES, messages);
8387 if (mRemoteInput != null) {
8388 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8389 }
8390 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8391 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8392 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8393 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8394 return b;
8395 }
8396
8397 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8398 if (b == null) {
8399 return null;
8400 }
8401 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8402 String[] messages = null;
8403 if (parcelableMessages != null) {
8404 String[] tmp = new String[parcelableMessages.length];
8405 boolean success = true;
8406 for (int i = 0; i < tmp.length; i++) {
8407 if (!(parcelableMessages[i] instanceof Bundle)) {
8408 success = false;
8409 break;
8410 }
8411 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8412 if (tmp[i] == null) {
8413 success = false;
8414 break;
8415 }
8416 }
8417 if (success) {
8418 messages = tmp;
8419 } else {
8420 return null;
8421 }
8422 }
8423
8424 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8425 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8426
8427 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8428
8429 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8430 if (participants == null || participants.length != 1) {
8431 return null;
8432 }
8433
8434 return new UnreadConversation(messages,
8435 remoteInput,
8436 onReply,
8437 onRead,
8438 participants, b.getLong(KEY_TIMESTAMP));
8439 }
8440 };
8441
8442 /**
8443 * Builder class for {@link CarExtender.UnreadConversation} objects.
8444 */
8445 public static class Builder {
8446 private final List<String> mMessages = new ArrayList<String>();
8447 private final String mParticipant;
8448 private RemoteInput mRemoteInput;
8449 private PendingIntent mReadPendingIntent;
8450 private PendingIntent mReplyPendingIntent;
8451 private long mLatestTimestamp;
8452
8453 /**
8454 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8455 *
8456 * @param name The name of the other participant in the conversation.
8457 */
8458 public Builder(String name) {
8459 mParticipant = name;
8460 }
8461
8462 /**
8463 * Appends a new unread message to the list of messages for this conversation.
8464 *
8465 * The messages should be added from oldest to newest.
8466 *
8467 * @param message The text of the new unread message.
8468 * @return This object for method chaining.
8469 */
8470 public Builder addMessage(String message) {
8471 mMessages.add(message);
8472 return this;
8473 }
8474
8475 /**
8476 * Sets the pending intent and remote input which will convey the reply to this
8477 * notification.
8478 *
8479 * @param pendingIntent The pending intent which will be triggered on a reply.
8480 * @param remoteInput The remote input parcelable which will carry the reply.
8481 * @return This object for method chaining.
8482 *
8483 * @see CarExtender.UnreadConversation#getRemoteInput
8484 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8485 */
8486 public Builder setReplyAction(
8487 PendingIntent pendingIntent, RemoteInput remoteInput) {
8488 mRemoteInput = remoteInput;
8489 mReplyPendingIntent = pendingIntent;
8490
8491 return this;
8492 }
8493
8494 /**
8495 * Sets the pending intent that will be sent once the messages in this notification
8496 * are read.
8497 *
8498 * @param pendingIntent The pending intent to use.
8499 * @return This object for method chaining.
8500 */
8501 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8502 mReadPendingIntent = pendingIntent;
8503 return this;
8504 }
8505
8506 /**
8507 * Sets the timestamp of the most recent message in an unread conversation.
8508 *
8509 * If a messaging notification has been posted by your application and has not
8510 * yet been cancelled, posting a later notification with the same id and tag
8511 * but without a newer timestamp may result in Android Auto not displaying a
8512 * heads up notification for the later notification.
8513 *
8514 * @param timestamp The timestamp of the most recent message in the conversation.
8515 * @return This object for method chaining.
8516 */
8517 public Builder setLatestTimestamp(long timestamp) {
8518 mLatestTimestamp = timestamp;
8519 return this;
8520 }
8521
8522 /**
8523 * Builds a new unread conversation object.
8524 *
8525 * @return The new unread conversation object.
8526 */
8527 public UnreadConversation build() {
8528 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8529 String[] participants = { mParticipant };
8530 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8531 mReadPendingIntent, participants, mLatestTimestamp);
8532 }
8533 }
8534 }
8535
8536 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008537 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8538 * with a TV extension:
8539 *
8540 * <ol>
8541 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8542 * <li>Create a {@link TvExtender}.
8543 * <li>Set TV-specific properties using the {@code set} methods of
8544 * {@link TvExtender}.
8545 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8546 * to apply the extension to a notification.
8547 * </ol>
8548 *
8549 * <pre class="prettyprint">
8550 * Notification notification = new Notification.Builder(context)
8551 * ...
8552 * .extend(new TvExtender()
8553 * .set*(...))
8554 * .build();
8555 * </pre>
8556 *
8557 * <p>TV extensions can be accessed on an existing notification by using the
8558 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8559 * to access values.
8560 *
8561 * @hide
8562 */
8563 @SystemApi
8564 public static final class TvExtender implements Extender {
8565 private static final String TAG = "TvExtender";
8566
8567 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8568 private static final String EXTRA_FLAGS = "flags";
8569 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8570 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008571 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008572
8573 // Flags bitwise-ored to mFlags
8574 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8575
8576 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008577 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008578 private PendingIntent mContentIntent;
8579 private PendingIntent mDeleteIntent;
8580
8581 /**
8582 * Create a {@link TvExtender} with default options.
8583 */
8584 public TvExtender() {
8585 mFlags = FLAG_AVAILABLE_ON_TV;
8586 }
8587
8588 /**
8589 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8590 *
8591 * @param notif The notification from which to copy options.
8592 */
8593 public TvExtender(Notification notif) {
8594 Bundle bundle = notif.extras == null ?
8595 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8596 if (bundle != null) {
8597 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008598 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008599 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8600 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8601 }
8602 }
8603
8604 /**
8605 * Apply a TV extension to a notification that is being built. This is typically called by
8606 * the {@link Notification.Builder#extend(Notification.Extender)}
8607 * method of {@link Notification.Builder}.
8608 */
8609 @Override
8610 public Notification.Builder extend(Notification.Builder builder) {
8611 Bundle bundle = new Bundle();
8612
8613 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008614 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008615 if (mContentIntent != null) {
8616 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8617 }
8618
8619 if (mDeleteIntent != null) {
8620 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8621 }
8622
8623 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8624 return builder;
8625 }
8626
8627 /**
8628 * Returns true if this notification should be shown on TV. This method return true
8629 * if the notification was extended with a TvExtender.
8630 */
8631 public boolean isAvailableOnTv() {
8632 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8633 }
8634
8635 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008636 * Specifies the channel the notification should be delivered on when shown on TV.
8637 * It can be different from the channel that the notification is delivered to when
8638 * posting on a non-TV device.
8639 */
8640 public TvExtender setChannel(String channelId) {
8641 mChannelId = channelId;
8642 return this;
8643 }
8644
8645 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008646 * Specifies the channel the notification should be delivered on when shown on TV.
8647 * It can be different from the channel that the notification is delivered to when
8648 * posting on a non-TV device.
8649 */
8650 public TvExtender setChannelId(String channelId) {
8651 mChannelId = channelId;
8652 return this;
8653 }
8654
Jeff Sharkey000ce802017-04-29 13:13:27 -06008655 /** @removed */
8656 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008657 public String getChannel() {
8658 return mChannelId;
8659 }
8660
8661 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008662 * Returns the id of the channel this notification posts to on TV.
8663 */
8664 public String getChannelId() {
8665 return mChannelId;
8666 }
8667
8668 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008669 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8670 * If provided, it is used instead of the content intent specified
8671 * at the level of Notification.
8672 */
8673 public TvExtender setContentIntent(PendingIntent intent) {
8674 mContentIntent = intent;
8675 return this;
8676 }
8677
8678 /**
8679 * Returns the TV-specific content intent. If this method returns null, the
8680 * main content intent on the notification should be used.
8681 *
8682 * @see {@link Notification#contentIntent}
8683 */
8684 public PendingIntent getContentIntent() {
8685 return mContentIntent;
8686 }
8687
8688 /**
8689 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8690 * by the user on TV. If provided, it is used instead of the delete intent specified
8691 * at the level of Notification.
8692 */
8693 public TvExtender setDeleteIntent(PendingIntent intent) {
8694 mDeleteIntent = intent;
8695 return this;
8696 }
8697
8698 /**
8699 * Returns the TV-specific delete intent. If this method returns null, the
8700 * main delete intent on the notification should be used.
8701 *
8702 * @see {@link Notification#deleteIntent}
8703 */
8704 public PendingIntent getDeleteIntent() {
8705 return mDeleteIntent;
8706 }
8707 }
8708
8709 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008710 * Get an array of Notification objects from a parcelable array bundle field.
8711 * Update the bundle to have a typed array so fetches in the future don't need
8712 * to do an array copy.
8713 */
8714 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8715 Parcelable[] array = bundle.getParcelableArray(key);
8716 if (array instanceof Notification[] || array == null) {
8717 return (Notification[]) array;
8718 }
8719 Notification[] typedArray = Arrays.copyOf(array, array.length,
8720 Notification[].class);
8721 bundle.putParcelableArray(key, typedArray);
8722 return typedArray;
8723 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008724
8725 private static class BuilderRemoteViews extends RemoteViews {
8726 public BuilderRemoteViews(Parcel parcel) {
8727 super(parcel);
8728 }
8729
Kenny Guy77320062014-08-27 21:37:15 +01008730 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8731 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008732 }
8733
8734 @Override
8735 public BuilderRemoteViews clone() {
8736 Parcel p = Parcel.obtain();
8737 writeToParcel(p, 0);
8738 p.setDataPosition(0);
8739 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8740 p.recycle();
8741 return brv;
8742 }
8743 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008744
8745 private static class StandardTemplateParams {
8746 boolean hasProgress = true;
8747 boolean ambient = false;
8748 CharSequence title;
8749 CharSequence text;
Selim Cinek88188f22017-09-19 16:46:56 -07008750 boolean hideLargeIcon;
8751 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008752
8753 final StandardTemplateParams reset() {
8754 hasProgress = true;
8755 ambient = false;
8756 title = null;
8757 text = null;
8758 return this;
8759 }
8760
8761 final StandardTemplateParams hasProgress(boolean hasProgress) {
8762 this.hasProgress = hasProgress;
8763 return this;
8764 }
8765
8766 final StandardTemplateParams title(CharSequence title) {
8767 this.title = title;
8768 return this;
8769 }
8770
8771 final StandardTemplateParams text(CharSequence text) {
8772 this.text = text;
8773 return this;
8774 }
8775
Selim Cinek88188f22017-09-19 16:46:56 -07008776 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
8777 this.alwaysShowReply = alwaysShowReply;
8778 return this;
8779 }
8780
8781 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
8782 this.hideLargeIcon = hideLargeIcon;
8783 return this;
8784 }
8785
Adrian Roos70d7aa32017-01-11 15:39:06 -08008786 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008787 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008788 this.ambient = ambient;
8789 return this;
8790 }
8791
8792 final StandardTemplateParams fillTextsFrom(Builder b) {
8793 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008794 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8795
8796 // Big text notifications should contain their content when viewed in ambient mode.
8797 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8798 if (!ambient || TextUtils.isEmpty(text)) {
8799 text = extras.getCharSequence(EXTRA_TEXT);
8800 }
8801 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008802 return this;
8803 }
8804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805}