blob: 01e6f138161dc5b18ebf32422fd05982c6416542 [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
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
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;
Lucas Dupina291d192018-06-07 13:59:42 -070082import com.android.internal.util.ContrastColorUtil;
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;
Julia Reynolds7217dc92018-03-07 12:12:09 -050092import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050093import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060094import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096/**
97 * A class that represents how a persistent notification is to be presented to
98 * the user using the {@link android.app.NotificationManager}.
99 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800100 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
101 * easier to construct Notifications.</p>
102 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700103 * <div class="special reference">
104 * <h3>Developer Guides</h3>
105 * <p>For a guide to creating notifications, read the
106 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
107 * developer guide.</p>
108 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
110public class Notification implements Parcelable
111{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400112 private static final String TAG = "Notification";
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400116 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400117 */
118 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
119 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
120 = "android.intent.category.NOTIFICATION_PREFERENCES";
121
122 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500123 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
124 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500126 */
127 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
128
129 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400130 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400131 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
132 * what settings should be shown in the target app.
133 */
134 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
135
136 /**
137 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400138 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
139 * that can be used to narrow down what settings should be shown in the target app.
140 */
141 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
142
143 /**
144 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
145 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
146 * that can be used to narrow down what settings should be shown in the target app.
147 */
148 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
149
150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * Use all default values (where applicable).
152 */
153 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /**
156 * Use the default notification sound. This will ignore any given
157 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 *
Chris Wren47c20a12014-06-18 17:27:29 -0400159 * <p>
160 * A notification that is noisy is more likely to be presented as a heads-up notification.
161 * </p>
162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 */
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int DEFAULT_SOUND = 1;
167
168 /**
169 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700171 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500172 *
Chris Wren47c20a12014-06-18 17:27:29 -0400173 * <p>
174 * A notification that vibrates is more likely to be presented as a heads-up notification.
175 * </p>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 */
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
183 * Use the default notification lights. This will ignore the
184 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
185 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188 */
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200193 * Maximum length of CharSequences accepted by Builder and friends.
194 *
195 * <p>
196 * Avoids spamming the system with overly large strings such as full e-mails.
197 */
198 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
199
200 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800201 * Maximum entries of reply text that are accepted by Builder and friends.
202 */
203 private static final int MAX_REPLY_HISTORY = 5;
204
Selim Cinekde4de0e2018-01-24 16:21:07 -0800205
206 /**
207 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
208 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
209 *
210 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
211 * sends messages.</p>
212 */
213 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
214
Adrian Roose458aa82015-12-08 16:17:19 -0800215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500218 * Default value: {@link System#currentTimeMillis() Now}.
219 *
220 * Choose a timestamp that will be most relevant to the user. For most finite events, this
221 * corresponds to the time the event happened (or will happen, in the case of events that have
222 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800223 * timestamped according to when the activity began.
224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500227 * <ul>
228 * <li>Notification of a new chat message should be stamped when the message was received.</li>
229 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
230 * <li>Notification of a completed file download should be stamped when the download finished.</li>
231 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
232 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
233 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800234 * </ul>
235 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700236 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
237 * anymore by default and must be opted into by using
238 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 */
240 public long when;
241
242 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700243 * The creation time of the notification
244 */
245 private long creationTime;
246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400249 *
250 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
Dan Sandler86647982015-05-13 23:41:13 -0400252 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700253 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 public int icon;
255
256 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800257 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
258 * leave it at its default value of 0.
259 *
260 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700261 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800262 */
263 public int iconLevel;
264
265 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500266 * The number of events that this notification represents. For example, in a new mail
267 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800268 *
Julia Reynolds30331982017-04-27 10:12:50 -0400269 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500270 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
271 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
Julia Reynolds30331982017-04-27 10:12:50 -0400273 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
275 /**
276 * The intent to execute when the expanded status entry is clicked. If
277 * this is an activity, it must include the
278 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800279 * that you take care of task management as described in the
280 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800281 * Stack</a> document. In particular, make sure to read the notification section
282 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
283 * Notifications</a> for the correct ways to launch an application from a
284 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 */
286 public PendingIntent contentIntent;
287
288 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500289 * The intent to execute when the notification is explicitly dismissed by the user, either with
290 * the "Clear All" button or by swiping it away individually.
291 *
292 * This probably shouldn't be launching an activity since several of those will be sent
293 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 */
295 public PendingIntent deleteIntent;
296
297 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700298 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800299 *
Chris Wren47c20a12014-06-18 17:27:29 -0400300 * <p>
301 * The system UI may choose to display a heads-up notification, instead of
302 * launching this intent, while the user is using the device.
303 * </p>
304 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800305 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400306 */
307 public PendingIntent fullScreenIntent;
308
309 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400310 * Text that summarizes this notification for accessibility services.
311 *
312 * As of the L release, this text is no longer shown on screen, but it is still useful to
313 * accessibility services (where it serves as an audible announcement of the notification's
314 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400315 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800316 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 */
318 public CharSequence tickerText;
319
320 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400321 * Formerly, a view showing the {@link #tickerText}.
322 *
323 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400324 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400325 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400327
328 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400329 * The view that will represent this notification in the notification list (which is pulled
330 * down from the status bar).
331 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500332 * As of N, this field may be null. The notification view is determined by the inputs
333 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public RemoteViews contentView;
338
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400339 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400340 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400341 * opportunity to show more detail. The system UI may choose to show this
342 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500344 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400345 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
346 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400347 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400348 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400349 public RemoteViews bigContentView;
350
Chris Wren8fd39ec2014-02-27 17:43:26 -0500351
352 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400353 * A medium-format version of {@link #contentView}, providing the Notification an
354 * opportunity to add action buttons to contentView. At its discretion, the system UI may
355 * choose to show this as a heads-up notification, which will pop up so the user can see
356 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500358 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400359 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
360 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400362 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500363 public RemoteViews headsUpContentView;
364
Julia Reynoldsfc640012018-02-21 12:25:27 -0500365 private boolean mUsesStandardHeader;
366
367 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
368 static {
369 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
370 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
371 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
372 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
373 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
378 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
380 }
381
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400382 /**
Dan Sandler86647982015-05-13 23:41:13 -0400383 * A large bitmap to be shown in the notification content area.
384 *
385 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 */
Dan Sandler86647982015-05-13 23:41:13 -0400387 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800388 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
390 /**
391 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400394 * A notification that is noisy is more likely to be presented as a heads-up notification.
395 * </p>
396 *
397 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500398 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500400 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500402 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 public Uri sound;
404
405 /**
406 * Use this constant as the value for audioStreamType to request that
407 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700408 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400409 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public static final int STREAM_DEFAULT = -1;
414
415 /**
416 * The audio stream type to use when playing the sound.
417 * Should be one of the STREAM_ constants from
418 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400419 *
420 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700422 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public int audioStreamType = STREAM_DEFAULT;
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400426 * The default value of {@link #audioAttributes}.
427 */
428 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
429 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
430 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
431 .build();
432
433 /**
434 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500435 *
436 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400437 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500438 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400439 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
440
441 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 * The pattern with which to vibrate.
443 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 * <p>
445 * To vibrate the default pattern, see {@link #defaults}.
446 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500449 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500451 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 public long[] vibrate;
453
454 /**
455 * The color of the led. The hardware will do its best approximation.
456 *
457 * @see #FLAG_SHOW_LIGHTS
458 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500459 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Tor Norbye80756e32015-03-02 09:39:27 -0800461 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500462 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 public int ledARGB;
464
465 /**
466 * The number of milliseconds for the LED to be on while it's flashing.
467 * The hardware will do its best approximation.
468 *
469 * @see #FLAG_SHOW_LIGHTS
470 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500471 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500473 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 public int ledOnMS;
475
476 /**
477 * The number of milliseconds for the LED to be off while it's flashing.
478 * The hardware will do its best approximation.
479 *
480 * @see #FLAG_SHOW_LIGHTS
481 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500482 *
483 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500485 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 public int ledOffMS;
487
488 /**
489 * Specifies which values should be taken from the defaults.
490 * <p>
491 * To set, OR the desired from {@link #DEFAULT_SOUND},
492 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
493 * values, use {@link #DEFAULT_ALL}.
494 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 *
496 * @deprecated use {@link NotificationChannel#getSound()} and
497 * {@link NotificationChannel#shouldShowLights()} and
498 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500500 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 public int defaults;
502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
504 * Bit to be bitwise-ored into the {@link #flags} field that should be
505 * set if you want the LED on for this notification.
506 * <ul>
507 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
508 * or 0 for both ledOnMS and ledOffMS.</li>
509 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
510 * <li>To flash the LED, pass the number of milliseconds that it should
511 * be on and off to ledOnMS and ledOffMS.</li>
512 * </ul>
513 * <p>
514 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800515 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 * because they will be set to values that work on any given hardware.
517 * <p>
518 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500519 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500520 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500522 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
524
525 /**
526 * Bit to be bitwise-ored into the {@link #flags} field that should be
527 * set if this notification is in reference to something that is ongoing,
528 * like a phone call. It should not be set if this notification is in
529 * reference to something that happened at a particular point in time,
530 * like a missed phone call.
531 */
532 public static final int FLAG_ONGOING_EVENT = 0x00000002;
533
534 /**
535 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700536 * the audio will be repeated until the notification is
537 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 */
539 public static final int FLAG_INSISTENT = 0x00000004;
540
541 /**
542 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700543 * set if you would only like the sound, vibrate and ticker to be played
544 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 */
546 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
547
548 /**
549 * Bit to be bitwise-ored into the {@link #flags} field that should be
550 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500551 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 */
553 public static final int FLAG_AUTO_CANCEL = 0x00000010;
554
555 /**
556 * Bit to be bitwise-ored into the {@link #flags} field that should be
557 * set if the notification should not be canceled when the user clicks
558 * the Clear all button.
559 */
560 public static final int FLAG_NO_CLEAR = 0x00000020;
561
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700562 /**
563 * Bit to be bitwise-ored into the {@link #flags} field that should be
564 * set if this notification represents a currently running service. This
565 * will normally be set for you by {@link Service#startForeground}.
566 */
567 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
568
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800571 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500572 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400573 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700574 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400576
Griff Hazendfcb0802014-02-11 12:00:00 -0800577 /**
578 * Bit to be bitswise-ored into the {@link #flags} field that should be
579 * set if this notification is relevant to the current device only
580 * and it is not recommended that it bridge to other devices.
581 */
582 public static final int FLAG_LOCAL_ONLY = 0x00000100;
583
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700584 /**
585 * Bit to be bitswise-ored into the {@link #flags} field that should be
586 * set if this notification is the group summary for a group of notifications.
587 * Grouped notifications may display in a cluster or stack on devices which
588 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
589 */
590 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
591
Julia Reynoldse46bb372016-03-17 11:05:58 -0400592 /**
593 * Bit to be bitswise-ored into the {@link #flags} field that should be
594 * set if this notification is the group summary for an auto-group of notifications.
595 *
596 * @hide
597 */
598 @SystemApi
599 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
600
Julia Reynolds4db59552017-06-30 13:34:01 -0400601 /**
602 * @hide
603 */
604 public static final int FLAG_CAN_COLORIZE = 0x00000800;
605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 public int flags;
607
Tor Norbyed9273d62013-05-30 15:59:53 -0700608 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700609 @IntDef(prefix = { "PRIORITY_" }, value = {
610 PRIORITY_DEFAULT,
611 PRIORITY_LOW,
612 PRIORITY_MIN,
613 PRIORITY_HIGH,
614 PRIORITY_MAX
615 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700616 @Retention(RetentionPolicy.SOURCE)
617 public @interface Priority {}
618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 * Default notification {@link #priority}. If your application does not prioritize its own
621 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} 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_DEFAULT = 0;
627
628 /**
629 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
630 * items smaller, or at a different position in the list, compared with your app's
631 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_LOW = -1;
637
638 /**
639 * Lowest {@link #priority}; these items might not be shown to the user except under special
640 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_MIN = -2;
646
647 /**
648 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
649 * show these items larger, or at a different position in notification lists, compared with
650 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500651 *
652 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500654 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 public static final int PRIORITY_HIGH = 1;
656
657 /**
658 * Highest {@link #priority}, for your application's most important items that require the
659 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500660 *
661 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500663 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500664 public static final int PRIORITY_MAX = 2;
665
666 /**
667 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800668 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 * Priority is an indication of how much of the user's valuable attention should be consumed by
670 * this notification. Low-priority notifications may be hidden from the user in certain
671 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500672 * system will make a determination about how to interpret this priority when presenting
673 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400674 *
675 * <p>
676 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
677 * as a heads-up notification.
678 * </p>
679 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500680 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500681 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700682 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500683 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500684 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800685
Dan Sandler26e81cf2014-05-06 10:01:27 -0400686 /**
687 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
688 * to be applied by the standard Style templates when presenting this notification.
689 *
690 * The current template design constructs a colorful header image by overlaying the
691 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
692 * ignored.
693 */
Tor Norbye80756e32015-03-02 09:39:27 -0800694 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400695 public int color = COLOR_DEFAULT;
696
697 /**
698 * Special value of {@link #color} telling the system not to decorate this notification with
699 * any special color but instead use default colors when presenting this notification.
700 */
Tor Norbye80756e32015-03-02 09:39:27 -0800701 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400702 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600703
704 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800705 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800706 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800707 */
708 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800709 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800710
711 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700712 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
713 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600714 * lockscreen).
715 *
716 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
717 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
718 * shown in all situations, but the contents are only available if the device is unlocked for
719 * the appropriate user.
720 *
721 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
722 * can be read even in an "insecure" context (that is, above a secure lockscreen).
723 * To modify the public version of this notification—for example, to redact some portions—see
724 * {@link Builder#setPublicVersion(Notification)}.
725 *
726 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
727 * and ticker until the user has bypassed the lockscreen.
728 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600729 public @Visibility int visibility;
730
731 /** @hide */
732 @IntDef(prefix = { "VISIBILITY_" }, value = {
733 VISIBILITY_PUBLIC,
734 VISIBILITY_PRIVATE,
735 VISIBILITY_SECRET,
736 })
737 @Retention(RetentionPolicy.SOURCE)
738 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600739
Griff Hazenfc3922d2014-08-20 11:56:44 -0700740 /**
741 * Notification visibility: Show this notification in its entirety on all lockscreens.
742 *
743 * {@see #visibility}
744 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600745 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700746
747 /**
748 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
749 * private information on secure lockscreens.
750 *
751 * {@see #visibility}
752 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600753 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700754
755 /**
756 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
757 *
758 * {@see #visibility}
759 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600760 public static final int VISIBILITY_SECRET = -1;
761
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400763 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500764 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400765 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500766
767 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700768 * Notification category: map turn-by-turn navigation.
769 */
770 public static final String CATEGORY_NAVIGATION = "navigation";
771
772 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400773 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500774 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400775 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500776
777 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400778 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500779 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_ALARM = "alarm";
796
797 /**
798 * Notification category: progress of a long-running background operation.
799 */
800 public static final String CATEGORY_PROGRESS = "progress";
801
802 /**
803 * Notification category: social network or sharing update.
804 */
805 public static final String CATEGORY_SOCIAL = "social";
806
807 /**
808 * Notification category: error in background operation or authentication status.
809 */
810 public static final String CATEGORY_ERROR = "err";
811
812 /**
813 * Notification category: media transport control for playback.
814 */
815 public static final String CATEGORY_TRANSPORT = "transport";
816
817 /**
818 * Notification category: system or device status update. Reserved for system use.
819 */
820 public static final String CATEGORY_SYSTEM = "sys";
821
822 /**
823 * Notification category: indication of running background service.
824 */
825 public static final String CATEGORY_SERVICE = "service";
826
827 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400828 * Notification category: a specific, timely recommendation for a single thing.
829 * For example, a news app might want to recommend a news story it believes the user will
830 * want to read next.
831 */
832 public static final String CATEGORY_RECOMMENDATION = "recommendation";
833
834 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400835 * Notification category: ongoing information about device or contextual status.
836 */
837 public static final String CATEGORY_STATUS = "status";
838
839 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400840 * Notification category: user-scheduled reminder.
841 */
842 public static final String CATEGORY_REMINDER = "reminder";
843
844 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700845 * Notification category: extreme car emergencies.
846 * @hide
847 */
848 @SystemApi
849 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
850
851 /**
852 * Notification category: car warnings.
853 * @hide
854 */
855 @SystemApi
856 public static final String CATEGORY_CAR_WARNING = "car_warning";
857
858 /**
859 * Notification category: general car system information.
860 * @hide
861 */
862 @SystemApi
863 public static final String CATEGORY_CAR_INFORMATION = "car_information";
864
865 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400866 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
867 * that best describes this Notification. May be used by the system for ranking and filtering.
868 */
869 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500870
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700871 private String mGroupKey;
872
873 /**
874 * Get the key used to group this notification into a cluster or stack
875 * with other notifications on devices which support such rendering.
876 */
877 public String getGroup() {
878 return mGroupKey;
879 }
880
881 private String mSortKey;
882
883 /**
884 * Get a sort key that orders this notification among other notifications from the
885 * same package. This can be useful if an external sort was already applied and an app
886 * would like to preserve this. Notifications will be sorted lexicographically using this
887 * value, although providing different priorities in addition to providing sort key may
888 * cause this value to be ignored.
889 *
890 * <p>This sort key can also be used to order members of a notification group. See
891 * {@link Builder#setGroup}.
892 *
893 * @see String#compareTo(String)
894 */
895 public String getSortKey() {
896 return mSortKey;
897 }
898
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500899 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400900 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901 * <p>
902 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
903 * APIs, and are intended to be used by
904 * {@link android.service.notification.NotificationListenerService} implementations to extract
905 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500906 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400907 public Bundle extras = new Bundle();
908
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400909 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700910 * All pending intents in the notification as the system needs to be able to access them but
911 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700912 * custom parcelable objects.
913 *
914 * @hide
915 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700916 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700917
918 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700919 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
920 * pending intents inside of it, so only those will get the behavior.
921 *
922 * @hide
923 */
Adrian Roosfb921842017-10-26 14:49:56 +0200924 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700925
926 /**
927 * Must be set by a process to start associating tokens with Notification objects
928 * coming in to it. This is set by NotificationManagerService.
929 *
930 * @hide
931 */
932 static public IBinder processWhitelistToken;
933
934 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400935 * {@link #extras} key: this is the title of the notification,
936 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
937 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500938 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400939
940 /**
941 * {@link #extras} key: this is the title of the notification when shown in expanded form,
942 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
943 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400944 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400945
946 /**
947 * {@link #extras} key: this is the main text payload, as supplied to
948 * {@link Builder#setContentText(CharSequence)}.
949 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500950 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951
952 /**
953 * {@link #extras} key: this is a third line of text, as supplied to
954 * {@link Builder#setSubText(CharSequence)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800959 * {@link #extras} key: this is the remote input history, as supplied to
960 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700961 *
962 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
963 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
964 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
965 * notifications once the other party has responded).
966 *
967 * The extra with this key is of type CharSequence[] and contains the most recent entry at
968 * the 0 index, the second most recent at the 1 index, etc.
969 *
970 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800971 */
972 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
973
974 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100975 * {@link #extras} key: boolean as supplied to
976 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
977 *
978 * If set to true, then the view displaying the remote input history from
979 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
980 *
981 * @see Builder#setShowRemoteInputSpinner(boolean)
982 * @hide
983 */
984 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
985
986 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100987 * {@link #extras} key: boolean as supplied to
988 * {@link Builder#setHideSmartReplies(boolean)}.
989 *
990 * If set to true, then any smart reply buttons will be hidden.
991 *
992 * @see Builder#setHideSmartReplies(boolean)
993 * @hide
994 */
995 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
996
997 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998 * {@link #extras} key: this is a small piece of additional text as supplied to
999 * {@link Builder#setContentInfo(CharSequence)}.
1000 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001001 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001002
1003 /**
1004 * {@link #extras} key: this is a line of summary information intended to be shown
1005 * alongside expanded notifications, as supplied to (e.g.)
1006 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1007 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001008 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009
1010 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001011 * {@link #extras} key: this is the longer text shown in the big form of a
1012 * {@link BigTextStyle} notification, as supplied to
1013 * {@link BigTextStyle#bigText(CharSequence)}.
1014 */
1015 public static final String EXTRA_BIG_TEXT = "android.bigText";
1016
1017 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001018 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1019 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001020 *
1021 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001023 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001024 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025
1026 /**
1027 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1028 * notification payload, as
1029 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001030 *
1031 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001033 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001034 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001035
1036 /**
1037 * {@link #extras} key: this is a bitmap to be used instead of the one from
1038 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1039 * shown in its expanded form, as supplied to
1040 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1041 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001042 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001043
1044 /**
1045 * {@link #extras} key: this is the progress value supplied to
1046 * {@link Builder#setProgress(int, int, boolean)}.
1047 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001048 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001049
1050 /**
1051 * {@link #extras} key: this is the maximum value supplied to
1052 * {@link Builder#setProgress(int, int, boolean)}.
1053 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001054 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001055
1056 /**
1057 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1058 * {@link Builder#setProgress(int, int, boolean)}.
1059 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001060 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061
1062 /**
1063 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1064 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1065 * {@link Builder#setUsesChronometer(boolean)}.
1066 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001067 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001068
1069 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001070 * {@link #extras} key: whether the chronometer set on the notification should count down
1071 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001072 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001073 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001074 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001075
1076 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001077 * {@link #extras} key: whether {@link #when} should be shown,
1078 * as supplied to {@link Builder#setShowWhen(boolean)}.
1079 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001080 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001081
1082 /**
1083 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1084 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1085 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001086 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001087
1088 /**
1089 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1090 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1091 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001092 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001093
1094 /**
1095 * {@link #extras} key: A string representing the name of the specific
1096 * {@link android.app.Notification.Style} used to create this notification.
1097 */
Chris Wren91ad5632013-06-05 15:05:57 -04001098 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001099
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001100 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001101 * {@link #extras} key: A String array containing the people that this notification relates to,
1102 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001103 *
1104 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001105 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001106 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001107
1108 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001109 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1110 * this notification relates to.
1111 */
1112 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1113
1114 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001115 * Allow certain system-generated notifications to appear before the device is provisioned.
1116 * Only available to notifications coming from the android package.
1117 * @hide
1118 */
Maurice Lam96c10032017-03-29 15:42:38 -07001119 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001120 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001121 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1122
1123 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001124 * {@link #extras} key: A
1125 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001126 * in the background when the notification is selected. Used on television platforms.
1127 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001128 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001129 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001130 */
1131 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1132
1133 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001134 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001135 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001136 * {@link android.app.Notification.MediaStyle} notification.
1137 */
1138 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1139
1140 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001141 * {@link #extras} key: the indices of actions to be shown in the compact view,
1142 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1143 */
1144 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1145
Christoph Studer943aa672014-08-03 20:31:16 +02001146 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001147 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1148 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001149 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1150 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001151 *
1152 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001153 */
1154 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1155
1156 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001157 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1158 * direct replies
1159 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1160 * {@link Person}
1161 */
1162 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1163
1164 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001165 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001166 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001167 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001168 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001169
1170 /**
1171 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1172 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001173 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1174 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001175 */
1176 public static final String EXTRA_MESSAGES = "android.messages";
1177
1178 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001179 * {@link #extras} key: an array of
1180 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1181 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1182 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1183 * array of bundles.
1184 */
1185 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1186
1187 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001188 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1189 * represents a group conversation.
1190 */
1191 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1192
1193 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001194 * {@link #extras} key: whether the notification should be colorized as
1195 * supplied to {@link Builder#setColorized(boolean)}}.
1196 */
1197 public static final String EXTRA_COLORIZED = "android.colorized";
1198
1199 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001200 * @hide
1201 */
1202 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1203
Selim Cinek247fa012016-02-18 09:50:48 -08001204 /**
1205 * @hide
1206 */
1207 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1208
Shane Brennan472a3b32016-12-12 15:28:10 -08001209 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001210 * @hide
1211 */
1212 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1213
1214 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001215 * {@link #extras} key: the audio contents of this notification.
1216 *
1217 * This is for use when rendering the notification on an audio-focused interface;
1218 * the audio contents are a complete sound sample that contains the contents/body of the
1219 * notification. This may be used in substitute of a Text-to-Speech reading of the
1220 * notification. For example if the notification represents a voice message this should point
1221 * to the audio of that message.
1222 *
1223 * The data stored under this key should be a String representation of a Uri that contains the
1224 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1225 *
1226 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1227 * has a field for holding data URI. That field can be used for audio.
1228 * See {@code Message#setData}.
1229 *
1230 * Example usage:
1231 * <pre>
1232 * {@code
1233 * Notification.Builder myBuilder = (build your Notification as normal);
1234 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1235 * }
1236 * </pre>
1237 */
1238 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1239
Dan Sandler80eaa592016-04-14 23:34:54 -04001240 /** @hide */
1241 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001242 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001243 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1244
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001245 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001246 * This is set on the notifications shown by system_server about apps running foreground
1247 * services. It indicates that the notification should be shown
1248 * only if any of the given apps do not already have a properly tagged
1249 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1250 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001251 * @hide
1252 */
1253 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1254
Dan Sandlerd63f9322015-05-06 15:18:49 -04001255 private Icon mSmallIcon;
1256 private Icon mLargeIcon;
1257
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001258 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001259 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001260
Julia Reynolds13d898c2017-02-02 12:22:05 -05001261 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001262 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001263
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001264 /** @hide */
1265 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1266 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1267 })
1268 @Retention(RetentionPolicy.SOURCE)
1269 public @interface GroupAlertBehavior {}
1270
1271 /**
1272 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1273 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1274 * notification will not be muted when it is in a group.
1275 */
1276 public static final int GROUP_ALERT_ALL = 0;
1277
1278 /**
1279 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1280 * notification in a group should be silenced (no sound or vibration) even if they are posted
1281 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001282 * mute this notification if this notification is a group child. This must be applied to all
1283 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001284 *
1285 * <p> For example, you might want to use this constant if you post a number of children
1286 * notifications at once (say, after a periodic sync), and only need to notify the user
1287 * audibly once.
1288 */
1289 public static final int GROUP_ALERT_SUMMARY = 1;
1290
1291 /**
1292 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1293 * notification in a group should be silenced (no sound or vibration) even if they are
1294 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1295 * to mute this notification if this notification is a group summary.
1296 *
1297 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001298 * in your group have content and the summary is only used to visually group notifications
1299 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001300 */
1301 public static final int GROUP_ALERT_CHILDREN = 2;
1302
Julia Reynolds2f431e22017-06-07 14:12:09 +00001303 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001304
Julia Reynolds13d898c2017-02-02 12:22:05 -05001305 /**
1306 * If this notification is being shown as a badge, always show as a number.
1307 */
1308 public static final int BADGE_ICON_NONE = 0;
1309
1310 /**
1311 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1312 * represent this notification.
1313 */
1314 public static final int BADGE_ICON_SMALL = 1;
1315
1316 /**
1317 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1318 * represent this notification.
1319 */
1320 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001321 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001322
Chris Wren51c75102013-07-16 20:49:17 -04001323 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001324 * Structure to encapsulate a named action that can be shown as part of this notification.
1325 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1326 * selected by the user.
1327 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001328 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1329 * or {@link Notification.Builder#addAction(Notification.Action)}
1330 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001331 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001332 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001333 /**
1334 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1335 * {@link RemoteInput}s.
1336 *
1337 * This is intended for {@link RemoteInput}s that only accept data, meaning
1338 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1339 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1340 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1341 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1342 *
1343 * You can test if a RemoteInput matches these constraints using
1344 * {@link RemoteInput#isDataOnly}.
1345 */
1346 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1347
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001348 /**
1349 * {@link }: No semantic action defined.
1350 */
1351 public static final int SEMANTIC_ACTION_NONE = 0;
1352
1353 /**
1354 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1355 * may be appropriate.
1356 */
1357 public static final int SEMANTIC_ACTION_REPLY = 1;
1358
1359 /**
1360 * {@code SemanticAction}: Mark content as read.
1361 */
1362 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1363
1364 /**
1365 * {@code SemanticAction}: Mark content as unread.
1366 */
1367 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1368
1369 /**
1370 * {@code SemanticAction}: Delete the content associated with the notification. This
1371 * could mean deleting an email, message, etc.
1372 */
1373 public static final int SEMANTIC_ACTION_DELETE = 4;
1374
1375 /**
1376 * {@code SemanticAction}: Archive the content associated with the notification. This
1377 * could mean archiving an email, message, etc.
1378 */
1379 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1380
1381 /**
1382 * {@code SemanticAction}: Mute the content associated with the notification. This could
1383 * mean silencing a conversation or currently playing media.
1384 */
1385 public static final int SEMANTIC_ACTION_MUTE = 6;
1386
1387 /**
1388 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1389 * mean un-silencing a conversation or currently playing media.
1390 */
1391 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1392
1393 /**
1394 * {@code SemanticAction}: Mark content with a thumbs up.
1395 */
1396 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1397
1398 /**
1399 * {@code SemanticAction}: Mark content with a thumbs down.
1400 */
1401 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1402
Kodlee Yincda5b092018-02-15 15:34:53 -08001403 /**
1404 * {@code SemanticAction}: Call a contact, group, etc.
1405 */
1406 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001407
Griff Hazen959591e2014-05-15 22:26:18 -07001408 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001409 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001410 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001411 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001412 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001413
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001414 /**
1415 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001416 *
1417 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001418 */
Dan Sandler86647982015-05-13 23:41:13 -04001419 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001420 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001421
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001422 /**
1423 * Title of the action.
1424 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001425 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001426
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001427 /**
1428 * Intent to send when the user invokes this action. May be null, in which case the action
1429 * may be rendered in a disabled presentation by the system UI.
1430 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001431 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001432
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001433 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001434 if (in.readInt() != 0) {
1435 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001436 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1437 icon = mIcon.getResId();
1438 }
Dan Sandler86647982015-05-13 23:41:13 -04001439 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001440 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1441 if (in.readInt() == 1) {
1442 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1443 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001444 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001445 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001446 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001447 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001448 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001449
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001450 /**
Dan Sandler86647982015-05-13 23:41:13 -04001451 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001452 */
Dan Sandler86647982015-05-13 23:41:13 -04001453 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001454 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001455 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1456 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001457 }
1458
Adrian Roos7af53622016-10-12 13:44:05 -07001459 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001460 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001461 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1462 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001463 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001464 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1465 this.icon = icon.getResId();
1466 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001467 this.title = title;
1468 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001469 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001470 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001471 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001473 }
1474
1475 /**
Dan Sandler86647982015-05-13 23:41:13 -04001476 * Return an icon representing the action.
1477 */
1478 public Icon getIcon() {
1479 if (mIcon == null && icon != 0) {
1480 // you snuck an icon in here without using the builder; let's try to keep it
1481 mIcon = Icon.createWithResource("", icon);
1482 }
1483 return mIcon;
1484 }
1485
1486 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001487 * Get additional metadata carried around with this Action.
1488 */
1489 public Bundle getExtras() {
1490 return mExtras;
1491 }
1492
1493 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001494 * Return whether the platform should automatically generate possible replies for this
1495 * {@link Action}
1496 */
1497 public boolean getAllowGeneratedReplies() {
1498 return mAllowGeneratedReplies;
1499 }
1500
1501 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001502 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001503 * May return null if no remote inputs were added. Only returns inputs which accept
1504 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001505 */
1506 public RemoteInput[] getRemoteInputs() {
1507 return mRemoteInputs;
1508 }
1509
1510 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001511 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1512 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1513 * (eg. reply, mark as read, delete, etc).
1514 */
1515 public @SemanticAction int getSemanticAction() {
1516 return mSemanticAction;
1517 }
1518
1519 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001520 * Get the list of inputs to be collected from the user that ONLY accept data when this
1521 * action is sent. These remote inputs are guaranteed to return true on a call to
1522 * {@link RemoteInput#isDataOnly}.
1523 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001524 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001525 *
1526 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1527 * of non-textual RemoteInputs do not access these remote inputs.
1528 */
1529 public RemoteInput[] getDataOnlyRemoteInputs() {
1530 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1531 }
1532
1533 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001534 * Builder class for {@link Action} objects.
1535 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001536 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001537 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001538 private final CharSequence mTitle;
1539 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001540 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001541 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001542 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001543 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001544
1545 /**
1546 * Construct a new builder for {@link Action} object.
1547 * @param icon icon to show for this action
1548 * @param title the title of the action
1549 * @param intent the {@link PendingIntent} to fire when users trigger this action
1550 */
Dan Sandler86647982015-05-13 23:41:13 -04001551 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001552 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001553 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001554 }
1555
1556 /**
1557 * Construct a new builder for {@link Action} object.
1558 * @param icon icon to show for this action
1559 * @param title the title of the action
1560 * @param intent the {@link PendingIntent} to fire when users trigger this action
1561 */
1562 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001563 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001564 }
1565
1566 /**
1567 * Construct a new builder for {@link Action} object using the fields from an
1568 * {@link Action}.
1569 * @param action the action to read fields from.
1570 */
1571 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001572 this(action.getIcon(), action.title, action.actionIntent,
1573 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001574 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001575 }
1576
Dan Sandler86647982015-05-13 23:41:13 -04001577 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001578 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1579 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001580 mIcon = icon;
1581 mTitle = title;
1582 mIntent = intent;
1583 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001584 if (remoteInputs != null) {
1585 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1586 Collections.addAll(mRemoteInputs, remoteInputs);
1587 }
Adrian Roos7af53622016-10-12 13:44:05 -07001588 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001589 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001590 }
1591
1592 /**
1593 * Merge additional metadata into this builder.
1594 *
1595 * <p>Values within the Bundle will replace existing extras values in this Builder.
1596 *
1597 * @see Notification.Action#extras
1598 */
1599 public Builder addExtras(Bundle extras) {
1600 if (extras != null) {
1601 mExtras.putAll(extras);
1602 }
1603 return this;
1604 }
1605
1606 /**
1607 * Get the metadata Bundle used by this Builder.
1608 *
1609 * <p>The returned Bundle is shared with this Builder.
1610 */
1611 public Bundle getExtras() {
1612 return mExtras;
1613 }
1614
1615 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001616 * Add an input to be collected from the user when this action is sent.
1617 * Response values can be retrieved from the fired intent by using the
1618 * {@link RemoteInput#getResultsFromIntent} function.
1619 * @param remoteInput a {@link RemoteInput} to add to the action
1620 * @return this object for method chaining
1621 */
1622 public Builder addRemoteInput(RemoteInput remoteInput) {
1623 if (mRemoteInputs == null) {
1624 mRemoteInputs = new ArrayList<RemoteInput>();
1625 }
1626 mRemoteInputs.add(remoteInput);
1627 return this;
1628 }
1629
1630 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001631 * Set whether the platform should automatically generate possible replies to add to
1632 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1633 * {@link RemoteInput}, this has no effect.
1634 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1635 * otherwise
1636 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001637 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001638 */
1639 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1640 mAllowGeneratedReplies = allowGeneratedReplies;
1641 return this;
1642 }
1643
1644 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001645 * Sets the {@code SemanticAction} for this {@link Action}. A
1646 * {@code SemanticAction} denotes what an {@link Action}'s
1647 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1648 * @param semanticAction a SemanticAction defined within {@link Action} with
1649 * {@code SEMANTIC_ACTION_} prefixes
1650 * @return this object for method chaining
1651 */
1652 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1653 mSemanticAction = semanticAction;
1654 return this;
1655 }
1656
1657 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001658 * Apply an extender to this action builder. Extenders may be used to add
1659 * metadata or change options on this builder.
1660 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001661 public Builder extend(Extender extender) {
1662 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001663 return this;
1664 }
1665
1666 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001667 * Combine all of the options that have been set and return a new {@link Action}
1668 * object.
1669 * @return the built action
1670 */
1671 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001672 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1673 RemoteInput[] previousDataInputs =
1674 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001675 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001676 for (RemoteInput input : previousDataInputs) {
1677 dataOnlyInputs.add(input);
1678 }
1679 }
1680 List<RemoteInput> textInputs = new ArrayList<>();
1681 if (mRemoteInputs != null) {
1682 for (RemoteInput input : mRemoteInputs) {
1683 if (input.isDataOnly()) {
1684 dataOnlyInputs.add(input);
1685 } else {
1686 textInputs.add(input);
1687 }
1688 }
1689 }
1690 if (!dataOnlyInputs.isEmpty()) {
1691 RemoteInput[] dataInputsArr =
1692 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1693 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1694 }
1695 RemoteInput[] textInputsArr = textInputs.isEmpty()
1696 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1697 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001698 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001699 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001700 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001701
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001702 @Override
1703 public Action clone() {
1704 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001705 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001706 title,
1707 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001708 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001709 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001710 getAllowGeneratedReplies(),
1711 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001712 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001713
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001714 @Override
1715 public int describeContents() {
1716 return 0;
1717 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001718
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 @Override
1720 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001721 final Icon ic = getIcon();
1722 if (ic != null) {
1723 out.writeInt(1);
1724 ic.writeToParcel(out, 0);
1725 } else {
1726 out.writeInt(0);
1727 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001728 TextUtils.writeToParcel(title, out, flags);
1729 if (actionIntent != null) {
1730 out.writeInt(1);
1731 actionIntent.writeToParcel(out, flags);
1732 } else {
1733 out.writeInt(0);
1734 }
Griff Hazen959591e2014-05-15 22:26:18 -07001735 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001736 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001737 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001738 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001739 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001740
Griff Hazen959591e2014-05-15 22:26:18 -07001741 public static final Parcelable.Creator<Action> CREATOR =
1742 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001743 public Action createFromParcel(Parcel in) {
1744 return new Action(in);
1745 }
1746 public Action[] newArray(int size) {
1747 return new Action[size];
1748 }
1749 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001750
1751 /**
1752 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1753 * metadata or change options on an action builder.
1754 */
1755 public interface Extender {
1756 /**
1757 * Apply this extender to a notification action builder.
1758 * @param builder the builder to be modified.
1759 * @return the build object for chaining.
1760 */
1761 public Builder extend(Builder builder);
1762 }
1763
1764 /**
1765 * Wearable extender for notification actions. To add extensions to an action,
1766 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1767 * the {@code WearableExtender()} constructor and apply it to a
1768 * {@link android.app.Notification.Action.Builder} using
1769 * {@link android.app.Notification.Action.Builder#extend}.
1770 *
1771 * <pre class="prettyprint">
1772 * Notification.Action action = new Notification.Action.Builder(
1773 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001774 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001775 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001776 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001777 */
1778 public static final class WearableExtender implements Extender {
1779 /** Notification action extra which contains wearable extensions */
1780 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1781
Pete Gastaf6781d2014-10-07 15:17:05 -04001782 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001783 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001784 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1785 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1786 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001787
1788 // Flags bitwise-ored to mFlags
1789 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001790 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001791 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001792
1793 // Default value for flags integer
1794 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1795
1796 private int mFlags = DEFAULT_FLAGS;
1797
Pete Gastaf6781d2014-10-07 15:17:05 -04001798 private CharSequence mInProgressLabel;
1799 private CharSequence mConfirmLabel;
1800 private CharSequence mCancelLabel;
1801
Griff Hazen61a9e862014-05-22 16:05:19 -07001802 /**
1803 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1804 * options.
1805 */
1806 public WearableExtender() {
1807 }
1808
1809 /**
1810 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1811 * wearable options present in an existing notification action.
1812 * @param action the notification action to inspect.
1813 */
1814 public WearableExtender(Action action) {
1815 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1816 if (wearableBundle != null) {
1817 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001818 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1819 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1820 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001821 }
1822 }
1823
1824 /**
1825 * Apply wearable extensions to a notification action that is being built. This is
1826 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1827 * method of {@link android.app.Notification.Action.Builder}.
1828 */
1829 @Override
1830 public Action.Builder extend(Action.Builder builder) {
1831 Bundle wearableBundle = new Bundle();
1832
1833 if (mFlags != DEFAULT_FLAGS) {
1834 wearableBundle.putInt(KEY_FLAGS, mFlags);
1835 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001836 if (mInProgressLabel != null) {
1837 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1838 }
1839 if (mConfirmLabel != null) {
1840 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1841 }
1842 if (mCancelLabel != null) {
1843 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1844 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001845
1846 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1847 return builder;
1848 }
1849
1850 @Override
1851 public WearableExtender clone() {
1852 WearableExtender that = new WearableExtender();
1853 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001854 that.mInProgressLabel = this.mInProgressLabel;
1855 that.mConfirmLabel = this.mConfirmLabel;
1856 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001857 return that;
1858 }
1859
1860 /**
1861 * Set whether this action is available when the wearable device is not connected to
1862 * a companion device. The user can still trigger this action when the wearable device is
1863 * offline, but a visual hint will indicate that the action may not be available.
1864 * Defaults to true.
1865 */
1866 public WearableExtender setAvailableOffline(boolean availableOffline) {
1867 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1868 return this;
1869 }
1870
1871 /**
1872 * Get whether this action is available when the wearable device is not connected to
1873 * a companion device. The user can still trigger this action when the wearable device is
1874 * offline, but a visual hint will indicate that the action may not be available.
1875 * Defaults to true.
1876 */
1877 public boolean isAvailableOffline() {
1878 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1879 }
1880
1881 private void setFlag(int mask, boolean value) {
1882 if (value) {
1883 mFlags |= mask;
1884 } else {
1885 mFlags &= ~mask;
1886 }
1887 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001888
1889 /**
1890 * Set a label to display while the wearable is preparing to automatically execute the
1891 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1892 *
1893 * @param label the label to display while the action is being prepared to execute
1894 * @return this object for method chaining
1895 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001896 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001897 public WearableExtender setInProgressLabel(CharSequence label) {
1898 mInProgressLabel = label;
1899 return this;
1900 }
1901
1902 /**
1903 * Get the label to display while the wearable is preparing to automatically execute
1904 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1905 *
1906 * @return the label to display while the action is being prepared to execute
1907 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001908 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001909 public CharSequence getInProgressLabel() {
1910 return mInProgressLabel;
1911 }
1912
1913 /**
1914 * Set a label to display to confirm that the action should be executed.
1915 * This is usually an imperative verb like "Send".
1916 *
1917 * @param label the label to confirm the action should be executed
1918 * @return this object for method chaining
1919 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001920 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001921 public WearableExtender setConfirmLabel(CharSequence label) {
1922 mConfirmLabel = label;
1923 return this;
1924 }
1925
1926 /**
1927 * Get the label to display to confirm that the action should be executed.
1928 * This is usually an imperative verb like "Send".
1929 *
1930 * @return the label to confirm the action should be executed
1931 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001932 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001933 public CharSequence getConfirmLabel() {
1934 return mConfirmLabel;
1935 }
1936
1937 /**
1938 * Set a label to display to cancel the action.
1939 * This is usually an imperative verb, like "Cancel".
1940 *
1941 * @param label the label to display to cancel the action
1942 * @return this object for method chaining
1943 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001944 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001945 public WearableExtender setCancelLabel(CharSequence label) {
1946 mCancelLabel = label;
1947 return this;
1948 }
1949
1950 /**
1951 * Get the label to display to cancel the action.
1952 * This is usually an imperative verb like "Cancel".
1953 *
1954 * @return the label to display to cancel the action
1955 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001956 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001957 public CharSequence getCancelLabel() {
1958 return mCancelLabel;
1959 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001960
1961 /**
1962 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1963 * platform that it can generate the appropriate transitions.
1964 * @param hintLaunchesActivity {@code true} if the content intent will launch
1965 * an activity and transitions should be generated, false otherwise.
1966 * @return this object for method chaining
1967 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001968 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001969 boolean hintLaunchesActivity) {
1970 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1971 return this;
1972 }
1973
1974 /**
1975 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1976 * platform that it can generate the appropriate transitions
1977 * @return {@code true} if the content intent will launch an activity and transitions
1978 * should be generated, false otherwise. The default value is {@code false} if this was
1979 * never set.
1980 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001981 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001982 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1983 }
Alex Hills9f087612016-06-07 09:08:59 -04001984
1985 /**
1986 * Set a hint that this Action should be displayed inline.
1987 *
1988 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1989 * otherwise
1990 * @return this object for method chaining
1991 */
1992 public WearableExtender setHintDisplayActionInline(
1993 boolean hintDisplayInline) {
1994 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1995 return this;
1996 }
1997
1998 /**
1999 * Get a hint that this Action should be displayed inline.
2000 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002001 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002002 * otherwise. The default value is {@code false} if this was never set.
2003 */
2004 public boolean getHintDisplayActionInline() {
2005 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2006 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002007 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002008
2009 /**
2010 * Provides meaning to an {@link Action} that hints at what the associated
2011 * {@link PendingIntent} will do. For example, an {@link Action} with a
2012 * {@link PendingIntent} that replies to a text message notification may have the
2013 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2014 *
2015 * @hide
2016 */
2017 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2018 SEMANTIC_ACTION_NONE,
2019 SEMANTIC_ACTION_REPLY,
2020 SEMANTIC_ACTION_MARK_AS_READ,
2021 SEMANTIC_ACTION_MARK_AS_UNREAD,
2022 SEMANTIC_ACTION_DELETE,
2023 SEMANTIC_ACTION_ARCHIVE,
2024 SEMANTIC_ACTION_MUTE,
2025 SEMANTIC_ACTION_UNMUTE,
2026 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002027 SEMANTIC_ACTION_THUMBS_DOWN,
2028 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002029 })
2030 @Retention(RetentionPolicy.SOURCE)
2031 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002032 }
2033
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002034 /**
2035 * Array of all {@link Action} structures attached to this notification by
2036 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2037 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2038 * interface for invoking actions.
2039 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002040 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002041
2042 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002043 * Replacement version of this notification whose content will be shown
2044 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2045 * and {@link #VISIBILITY_PUBLIC}.
2046 */
2047 public Notification publicVersion;
2048
2049 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002050 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002051 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 */
2053 public Notification()
2054 {
2055 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002056 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002057 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059
2060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 * @hide
2062 */
2063 public Notification(Context context, int icon, CharSequence tickerText, long when,
2064 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2065 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002066 new Builder(context)
2067 .setWhen(when)
2068 .setSmallIcon(icon)
2069 .setTicker(tickerText)
2070 .setContentTitle(contentTitle)
2071 .setContentText(contentText)
2072 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2073 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075
2076 /**
2077 * Constructs a Notification object with the information needed to
2078 * have a status bar icon without the standard expanded view.
2079 *
2080 * @param icon The resource id of the icon to put in the status bar.
2081 * @param tickerText The text that flows by in the status bar when the notification first
2082 * activates.
2083 * @param when The time to show in the time field. In the System.currentTimeMillis
2084 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002085 *
2086 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002088 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 public Notification(int icon, CharSequence tickerText, long when)
2090 {
2091 this.icon = icon;
2092 this.tickerText = tickerText;
2093 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002094 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096
2097 /**
2098 * Unflatten the notification from a parcel.
2099 */
Svet Ganovddb94882016-06-23 19:55:24 -07002100 @SuppressWarnings("unchecked")
2101 public Notification(Parcel parcel) {
2102 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2103 // intents in extras are always written as the last entry.
2104 readFromParcelImpl(parcel);
2105 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002106 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002107 }
2108
2109 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 {
2111 int version = parcel.readInt();
2112
Adrian Roosfb921842017-10-26 14:49:56 +02002113 mWhitelistToken = parcel.readStrongBinder();
2114 if (mWhitelistToken == null) {
2115 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002116 }
2117 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002118 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002121 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002122 if (parcel.readInt() != 0) {
2123 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002124 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2125 icon = mSmallIcon.getResId();
2126 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 number = parcel.readInt();
2129 if (parcel.readInt() != 0) {
2130 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2131 }
2132 if (parcel.readInt() != 0) {
2133 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2134 }
2135 if (parcel.readInt() != 0) {
2136 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2137 }
2138 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002139 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002140 }
2141 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2143 }
Joe Onorato561d3852010-11-20 18:09:34 -08002144 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002145 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 defaults = parcel.readInt();
2148 flags = parcel.readInt();
2149 if (parcel.readInt() != 0) {
2150 sound = Uri.CREATOR.createFromParcel(parcel);
2151 }
2152
2153 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002154 if (parcel.readInt() != 0) {
2155 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 vibrate = parcel.createLongArray();
2158 ledARGB = parcel.readInt();
2159 ledOnMS = parcel.readInt();
2160 ledOffMS = parcel.readInt();
2161 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002162
2163 if (parcel.readInt() != 0) {
2164 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2165 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002166
2167 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002168
John Spurlockfd7f1e02014-03-18 16:41:57 -04002169 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002170
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002171 mGroupKey = parcel.readString();
2172
2173 mSortKey = parcel.readString();
2174
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002175 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002176 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002177
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002178 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2179
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002180 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002181 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2182 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002183
Chris Wren8fd39ec2014-02-27 17:43:26 -05002184 if (parcel.readInt() != 0) {
2185 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2186 }
2187
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002188 visibility = parcel.readInt();
2189
2190 if (parcel.readInt() != 0) {
2191 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2192 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002193
2194 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002195
2196 if (parcel.readInt() != 0) {
2197 mChannelId = parcel.readString();
2198 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002199 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002200
2201 if (parcel.readInt() != 0) {
2202 mShortcutId = parcel.readString();
2203 }
2204
2205 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002206
2207 if (parcel.readInt() != 0) {
2208 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2209 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002210
2211 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 }
2213
Andy Stadler110988c2010-12-03 14:29:16 -08002214 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002215 public Notification clone() {
2216 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002217 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002218 return that;
2219 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002220
Daniel Sandler1a497d32013-04-18 14:52:45 -04002221 /**
2222 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2223 * of this into that.
2224 * @hide
2225 */
2226 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002227 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002228 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002229 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002230 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002231 that.number = this.number;
2232
2233 // PendingIntents are global, so there's no reason (or way) to clone them.
2234 that.contentIntent = this.contentIntent;
2235 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002236 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002237
2238 if (this.tickerText != null) {
2239 that.tickerText = this.tickerText.toString();
2240 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002241 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002242 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002243 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002244 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002245 that.contentView = this.contentView.clone();
2246 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002247 if (heavy && this.mLargeIcon != null) {
2248 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002249 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002250 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002251 that.sound = this.sound; // android.net.Uri is immutable
2252 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002253 if (this.audioAttributes != null) {
2254 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2255 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002256
2257 final long[] vibrate = this.vibrate;
2258 if (vibrate != null) {
2259 final int N = vibrate.length;
2260 final long[] vib = that.vibrate = new long[N];
2261 System.arraycopy(vibrate, 0, vib, 0, N);
2262 }
2263
2264 that.ledARGB = this.ledARGB;
2265 that.ledOnMS = this.ledOnMS;
2266 that.ledOffMS = this.ledOffMS;
2267 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002268
Joe Onorato18e69df2010-05-17 22:26:12 -07002269 that.flags = this.flags;
2270
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002271 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002272
John Spurlockfd7f1e02014-03-18 16:41:57 -04002273 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002275 that.mGroupKey = this.mGroupKey;
2276
2277 that.mSortKey = this.mSortKey;
2278
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002280 try {
2281 that.extras = new Bundle(this.extras);
2282 // will unparcel
2283 that.extras.size();
2284 } catch (BadParcelableException e) {
2285 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2286 that.extras = null;
2287 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 }
2289
Felipe Lemedd85da62016-06-28 11:29:54 -07002290 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2291 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002292 }
2293
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002294 if (this.actions != null) {
2295 that.actions = new Action[this.actions.length];
2296 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002297 if ( this.actions[i] != null) {
2298 that.actions[i] = this.actions[i].clone();
2299 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002300 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002301 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002302
Daniel Sandler1a497d32013-04-18 14:52:45 -04002303 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002304 that.bigContentView = this.bigContentView.clone();
2305 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002306
Chris Wren8fd39ec2014-02-27 17:43:26 -05002307 if (heavy && this.headsUpContentView != null) {
2308 that.headsUpContentView = this.headsUpContentView.clone();
2309 }
2310
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002311 that.visibility = this.visibility;
2312
2313 if (this.publicVersion != null) {
2314 that.publicVersion = new Notification();
2315 this.publicVersion.cloneInto(that.publicVersion, heavy);
2316 }
2317
Dan Sandler26e81cf2014-05-06 10:01:27 -04002318 that.color = this.color;
2319
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002320 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002321 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002322 that.mShortcutId = this.mShortcutId;
2323 that.mBadgeIcon = this.mBadgeIcon;
2324 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002325 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002326
Daniel Sandler1a497d32013-04-18 14:52:45 -04002327 if (!heavy) {
2328 that.lightenPayload(); // will clean out extras
2329 }
2330 }
2331
2332 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002333 * Note all {@link Uri} that are referenced internally, with the expectation
2334 * that Uri permission grants will need to be issued to ensure the recipient
2335 * of this object is able to render its contents.
2336 *
2337 * @hide
2338 */
2339 public void visitUris(@NonNull Consumer<Uri> visitor) {
2340 visitor.accept(sound);
2341
2342 if (tickerView != null) tickerView.visitUris(visitor);
2343 if (contentView != null) contentView.visitUris(visitor);
2344 if (bigContentView != null) bigContentView.visitUris(visitor);
2345 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2346
2347 if (extras != null) {
2348 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
2349 visitor.accept(extras.getParcelable(EXTRA_BACKGROUND_IMAGE_URI));
2350 }
2351
2352 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2353 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2354 if (!ArrayUtils.isEmpty(messages)) {
2355 for (MessagingStyle.Message message : MessagingStyle.Message
2356 .getMessagesFromBundleArray(messages)) {
2357 visitor.accept(message.getDataUri());
2358 }
2359 }
2360
2361 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2362 if (!ArrayUtils.isEmpty(historic)) {
2363 for (MessagingStyle.Message message : MessagingStyle.Message
2364 .getMessagesFromBundleArray(historic)) {
2365 visitor.accept(message.getDataUri());
2366 }
2367 }
2368 }
2369 }
2370
2371 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002372 * Removes heavyweight parts of the Notification object for archival or for sending to
2373 * listeners when the full contents are not necessary.
2374 * @hide
2375 */
2376 public final void lightenPayload() {
2377 tickerView = null;
2378 contentView = null;
2379 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002380 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002381 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002382 if (extras != null && !extras.isEmpty()) {
2383 final Set<String> keyset = extras.keySet();
2384 final int N = keyset.size();
2385 final String[] keys = keyset.toArray(new String[N]);
2386 for (int i=0; i<N; i++) {
2387 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002388 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2389 continue;
2390 }
Dan Sandler50128532015-12-08 15:42:41 -05002391 final Object obj = extras.get(key);
2392 if (obj != null &&
2393 ( obj instanceof Parcelable
2394 || obj instanceof Parcelable[]
2395 || obj instanceof SparseArray
2396 || obj instanceof ArrayList)) {
2397 extras.remove(key);
2398 }
2399 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002400 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002401 }
2402
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002403 /**
2404 * Make sure this CharSequence is safe to put into a bundle, which basically
2405 * means it had better not be some custom Parcelable implementation.
2406 * @hide
2407 */
2408 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002409 if (cs == null) return cs;
2410 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2411 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2412 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002413 if (cs instanceof Parcelable) {
2414 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2415 + " instance is a custom Parcelable and not allowed in Notification");
2416 return cs.toString();
2417 }
Selim Cinek60a54252016-02-26 17:03:25 -08002418 return removeTextSizeSpans(cs);
2419 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002420
Selim Cinek60a54252016-02-26 17:03:25 -08002421 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2422 if (charSequence instanceof Spanned) {
2423 Spanned ss = (Spanned) charSequence;
2424 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2425 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2426 for (Object span : spans) {
2427 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002428 if (resultSpan instanceof CharacterStyle) {
2429 resultSpan = ((CharacterStyle) span).getUnderlying();
2430 }
2431 if (resultSpan instanceof TextAppearanceSpan) {
2432 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002433 resultSpan = new TextAppearanceSpan(
2434 originalSpan.getFamily(),
2435 originalSpan.getTextStyle(),
2436 -1,
2437 originalSpan.getTextColor(),
2438 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002439 } else if (resultSpan instanceof RelativeSizeSpan
2440 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002441 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002442 } else {
2443 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002444 }
2445 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2446 ss.getSpanFlags(span));
2447 }
2448 return builder;
2449 }
2450 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002451 }
2452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 public int describeContents() {
2454 return 0;
2455 }
2456
2457 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002458 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 */
Svet Ganovddb94882016-06-23 19:55:24 -07002460 public void writeToParcel(Parcel parcel, int flags) {
2461 // We need to mark all pending intents getting into the notification
2462 // system as being put there to later allow the notification ranker
2463 // to launch them and by doing so add the app to the battery saver white
2464 // list for a short period of time. The problem is that the system
2465 // cannot look into the extras as there may be parcelables there that
2466 // the platform does not know how to handle. To go around that we have
2467 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002468 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002469 if (collectPendingIntents) {
2470 PendingIntent.setOnMarshaledListener(
2471 (PendingIntent intent, Parcel out, int outFlags) -> {
2472 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002473 if (allPendingIntents == null) {
2474 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002475 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002476 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002477 }
2478 });
2479 }
2480 try {
2481 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002482 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002483 writeToParcelImpl(parcel, flags);
2484 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002485 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002486 } finally {
2487 if (collectPendingIntents) {
2488 PendingIntent.setOnMarshaledListener(null);
2489 }
2490 }
2491 }
2492
2493 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 parcel.writeInt(1);
2495
Adrian Roosfb921842017-10-26 14:49:56 +02002496 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002498 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002499 if (mSmallIcon == null && icon != 0) {
2500 // you snuck an icon in here without using the builder; let's try to keep it
2501 mSmallIcon = Icon.createWithResource("", icon);
2502 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002503 if (mSmallIcon != null) {
2504 parcel.writeInt(1);
2505 mSmallIcon.writeToParcel(parcel, 0);
2506 } else {
2507 parcel.writeInt(0);
2508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 parcel.writeInt(number);
2510 if (contentIntent != null) {
2511 parcel.writeInt(1);
2512 contentIntent.writeToParcel(parcel, 0);
2513 } else {
2514 parcel.writeInt(0);
2515 }
2516 if (deleteIntent != null) {
2517 parcel.writeInt(1);
2518 deleteIntent.writeToParcel(parcel, 0);
2519 } else {
2520 parcel.writeInt(0);
2521 }
2522 if (tickerText != null) {
2523 parcel.writeInt(1);
2524 TextUtils.writeToParcel(tickerText, parcel, flags);
2525 } else {
2526 parcel.writeInt(0);
2527 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002528 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002529 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002531 } else {
2532 parcel.writeInt(0);
2533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 if (contentView != null) {
2535 parcel.writeInt(1);
2536 contentView.writeToParcel(parcel, 0);
2537 } else {
2538 parcel.writeInt(0);
2539 }
Selim Cinek279fa862016-06-14 10:57:25 -07002540 if (mLargeIcon == null && largeIcon != null) {
2541 // you snuck an icon in here without using the builder; let's try to keep it
2542 mLargeIcon = Icon.createWithBitmap(largeIcon);
2543 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002544 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002545 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002546 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002547 } else {
2548 parcel.writeInt(0);
2549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550
2551 parcel.writeInt(defaults);
2552 parcel.writeInt(this.flags);
2553
2554 if (sound != null) {
2555 parcel.writeInt(1);
2556 sound.writeToParcel(parcel, 0);
2557 } else {
2558 parcel.writeInt(0);
2559 }
2560 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002561
2562 if (audioAttributes != null) {
2563 parcel.writeInt(1);
2564 audioAttributes.writeToParcel(parcel, 0);
2565 } else {
2566 parcel.writeInt(0);
2567 }
2568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 parcel.writeLongArray(vibrate);
2570 parcel.writeInt(ledARGB);
2571 parcel.writeInt(ledOnMS);
2572 parcel.writeInt(ledOffMS);
2573 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002574
2575 if (fullScreenIntent != null) {
2576 parcel.writeInt(1);
2577 fullScreenIntent.writeToParcel(parcel, 0);
2578 } else {
2579 parcel.writeInt(0);
2580 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581
2582 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002583
John Spurlockfd7f1e02014-03-18 16:41:57 -04002584 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002585
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002586 parcel.writeString(mGroupKey);
2587
2588 parcel.writeString(mSortKey);
2589
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002590 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002591
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002592 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002593
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002594 if (bigContentView != null) {
2595 parcel.writeInt(1);
2596 bigContentView.writeToParcel(parcel, 0);
2597 } else {
2598 parcel.writeInt(0);
2599 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002600
Chris Wren8fd39ec2014-02-27 17:43:26 -05002601 if (headsUpContentView != null) {
2602 parcel.writeInt(1);
2603 headsUpContentView.writeToParcel(parcel, 0);
2604 } else {
2605 parcel.writeInt(0);
2606 }
2607
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002608 parcel.writeInt(visibility);
2609
2610 if (publicVersion != null) {
2611 parcel.writeInt(1);
2612 publicVersion.writeToParcel(parcel, 0);
2613 } else {
2614 parcel.writeInt(0);
2615 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002616
2617 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002618
2619 if (mChannelId != null) {
2620 parcel.writeInt(1);
2621 parcel.writeString(mChannelId);
2622 } else {
2623 parcel.writeInt(0);
2624 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002625 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002626
2627 if (mShortcutId != null) {
2628 parcel.writeInt(1);
2629 parcel.writeString(mShortcutId);
2630 } else {
2631 parcel.writeInt(0);
2632 }
2633
2634 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002635
2636 if (mSettingsText != null) {
2637 parcel.writeInt(1);
2638 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2639 } else {
2640 parcel.writeInt(0);
2641 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002642
2643 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002644
2645 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647
2648 /**
2649 * Parcelable.Creator that instantiates Notification objects
2650 */
2651 public static final Parcelable.Creator<Notification> CREATOR
2652 = new Parcelable.Creator<Notification>()
2653 {
2654 public Notification createFromParcel(Parcel parcel)
2655 {
2656 return new Notification(parcel);
2657 }
2658
2659 public Notification[] newArray(int size)
2660 {
2661 return new Notification[size];
2662 }
2663 };
2664
2665 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002666 * @hide
2667 */
2668 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2669 Notification.Action[] firstAs = first.actions;
2670 Notification.Action[] secondAs = second.actions;
2671 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2672 return true;
2673 }
2674 if (firstAs != null && secondAs != null) {
2675 if (firstAs.length != secondAs.length) {
2676 return true;
2677 }
2678 for (int i = 0; i < firstAs.length; i++) {
2679 if (!Objects.equals(firstAs[i].title, secondAs[i].title)) {
2680 return true;
2681 }
2682 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2683 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2684 if (firstRs == null) {
2685 firstRs = new RemoteInput[0];
2686 }
2687 if (secondRs == null) {
2688 secondRs = new RemoteInput[0];
2689 }
2690 if (firstRs.length != secondRs.length) {
2691 return true;
2692 }
2693 for (int j = 0; j < firstRs.length; j++) {
2694 if (!Objects.equals(firstRs[j].getLabel(), secondRs[j].getLabel())) {
2695 return true;
2696 }
Julia Reynolds5a366fb2018-03-21 15:40:53 -04002697 CharSequence[] firstCs = firstRs[j].getChoices();
2698 CharSequence[] secondCs = secondRs[j].getChoices();
Julia Reynolds7217dc92018-03-07 12:12:09 -05002699 if (firstCs == null) {
2700 firstCs = new CharSequence[0];
2701 }
2702 if (secondCs == null) {
2703 secondCs = new CharSequence[0];
2704 }
2705 if (firstCs.length != secondCs.length) {
2706 return true;
2707 }
2708 for (int k = 0; k < firstCs.length; k++) {
2709 if (!Objects.equals(firstCs[k], secondCs[k])) {
2710 return true;
2711 }
2712 }
2713 }
2714 }
2715 }
2716 return false;
2717 }
2718
2719 /**
2720 * @hide
2721 */
2722 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2723 if (first.getStyle() == null) {
2724 return second.getStyle() != null;
2725 }
2726 if (second.getStyle() == null) {
2727 return true;
2728 }
2729 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2730 }
2731
2732 /**
2733 * @hide
2734 */
2735 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002736 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2737 return true;
2738 }
2739
2740 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2741 return true;
2742 }
2743 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2744 return true;
2745 }
2746 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2747 return true;
2748 }
2749
2750 return false;
2751 }
2752
2753 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2754 if (first == null && second == null) {
2755 return false;
2756 }
2757 if (first == null && second != null || first != null && second == null) {
2758 return true;
2759 }
2760
2761 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2762 return true;
2763 }
2764
2765 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2766 return true;
2767 }
2768
2769 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002770 }
2771
2772 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002773 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2774 * <p>
2775 * For backwards compatibility {@code extras} holds some references to "real" member data such
2776 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2777 * fine as long as the object stays in one process.
2778 * <p>
2779 * However, once the notification goes into a parcel each reference gets marshalled separately,
2780 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2781 */
2782 private void fixDuplicateExtras() {
2783 if (extras != null) {
2784 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2785 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2786 }
2787 }
2788
2789 /**
2790 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2791 * separate object, replace it with the field's version to avoid holding duplicate copies.
2792 */
2793 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2794 if (original != null && extras.getParcelable(extraName) != null) {
2795 extras.putParcelable(extraName, original);
2796 }
2797 }
2798
2799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2801 * layout.
2802 *
2803 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2804 * in the view.</p>
2805 * @param context The context for your application / activity.
2806 * @param contentTitle The title that goes in the expanded entry.
2807 * @param contentText The text that goes in the expanded entry.
2808 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2809 * If this is an activity, it must include the
2810 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002811 * that you take care of task management as described in the
2812 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2813 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002815 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002816 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 public void setLatestEventInfo(Context context,
2820 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2822 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2823 new Throwable());
2824 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002825
Selim Cinek4ac6f602016-06-13 15:47:03 -07002826 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2827 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2828 }
2829
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002830 // ensure that any information already set directly is preserved
2831 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002832
2833 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002835 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 }
2837 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002838 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002840 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002841
2842 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 }
2844
Julia Reynoldsda303542015-11-23 14:00:20 -05002845 /**
2846 * @hide
2847 */
2848 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002849 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002850 }
2851
2852 /**
2853 * @hide
2854 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002855 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002856 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002857 }
2858
Yi Jin6b514142017-10-30 14:54:12 -07002859 /**
2860 * @hide
2861 */
2862 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2863 long token = proto.start(fieldId);
2864 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2865 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2866 proto.write(NotificationProto.FLAGS, this.flags);
2867 proto.write(NotificationProto.COLOR, this.color);
2868 proto.write(NotificationProto.CATEGORY, this.category);
2869 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2870 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2871 if (this.actions != null) {
2872 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2873 }
2874 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2875 proto.write(NotificationProto.VISIBILITY, this.visibility);
2876 }
2877 if (publicVersion != null) {
2878 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2879 }
2880 proto.end(token);
2881 }
2882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 @Override
2884 public String toString() {
2885 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002886 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002887 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002888 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002889 sb.append(priority);
2890 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002891 if (contentView != null) {
2892 sb.append(contentView.getPackage());
2893 sb.append("/0x");
2894 sb.append(Integer.toHexString(contentView.getLayoutId()));
2895 } else {
2896 sb.append("null");
2897 }
2898 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002899 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2900 sb.append("default");
2901 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 int N = this.vibrate.length-1;
2903 sb.append("[");
2904 for (int i=0; i<N; i++) {
2905 sb.append(this.vibrate[i]);
2906 sb.append(',');
2907 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002908 if (N != -1) {
2909 sb.append(this.vibrate[N]);
2910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 } else {
2913 sb.append("null");
2914 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002915 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002916 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002918 } else if (this.sound != null) {
2919 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 } else {
2921 sb.append("null");
2922 }
Chris Wren365b6d32015-07-16 10:39:26 -04002923 if (this.tickerText != null) {
2924 sb.append(" tick");
2925 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002926 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002928 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002929 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002930 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002931 if (this.category != null) {
2932 sb.append(" category=");
2933 sb.append(this.category);
2934 }
2935 if (this.mGroupKey != null) {
2936 sb.append(" groupKey=");
2937 sb.append(this.mGroupKey);
2938 }
2939 if (this.mSortKey != null) {
2940 sb.append(" sortKey=");
2941 sb.append(this.mSortKey);
2942 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002943 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002944 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002945 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002946 }
2947 sb.append(" vis=");
2948 sb.append(visibilityToString(this.visibility));
2949 if (this.publicVersion != null) {
2950 sb.append(" publicVersion=");
2951 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002952 }
2953 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 return sb.toString();
2955 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002956
Dan Sandler1b718782014-07-18 12:43:45 -04002957 /**
2958 * {@hide}
2959 */
2960 public static String visibilityToString(int vis) {
2961 switch (vis) {
2962 case VISIBILITY_PRIVATE:
2963 return "PRIVATE";
2964 case VISIBILITY_PUBLIC:
2965 return "PUBLIC";
2966 case VISIBILITY_SECRET:
2967 return "SECRET";
2968 default:
2969 return "UNKNOWN(" + String.valueOf(vis) + ")";
2970 }
2971 }
2972
Joe Onoratocb109a02011-01-18 17:57:41 -08002973 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002974 * {@hide}
2975 */
2976 public static String priorityToString(@Priority int pri) {
2977 switch (pri) {
2978 case PRIORITY_MIN:
2979 return "MIN";
2980 case PRIORITY_LOW:
2981 return "LOW";
2982 case PRIORITY_DEFAULT:
2983 return "DEFAULT";
2984 case PRIORITY_HIGH:
2985 return "HIGH";
2986 case PRIORITY_MAX:
2987 return "MAX";
2988 default:
2989 return "UNKNOWN(" + String.valueOf(pri) + ")";
2990 }
2991 }
2992
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002993 /**
2994 * @hide
2995 */
2996 public boolean hasCompletedProgress() {
2997 // not a progress notification; can't be complete
2998 if (!extras.containsKey(EXTRA_PROGRESS)
2999 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3000 return false;
3001 }
3002 // many apps use max 0 for 'indeterminate'; not complete
3003 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3004 return false;
3005 }
3006 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3007 }
3008
Jeff Sharkey000ce802017-04-29 13:13:27 -06003009 /** @removed */
3010 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003011 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003012 return mChannelId;
3013 }
3014
3015 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003016 * Returns the id of the channel this notification posts to.
3017 */
3018 public String getChannelId() {
3019 return mChannelId;
3020 }
3021
Jeff Sharkey000ce802017-04-29 13:13:27 -06003022 /** @removed */
3023 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003024 public long getTimeout() {
3025 return mTimeout;
3026 }
3027
3028 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003029 * Returns the duration from posting after which this notification should be canceled by the
3030 * system, if it's not canceled already.
3031 */
3032 public long getTimeoutAfter() {
3033 return mTimeout;
3034 }
3035
3036 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003037 * Returns what icon should be shown for this notification if it is being displayed in a
3038 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3039 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3040 */
3041 public int getBadgeIconType() {
3042 return mBadgeIcon;
3043 }
3044
3045 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003046 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003047 *
3048 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3049 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003050 */
3051 public String getShortcutId() {
3052 return mShortcutId;
3053 }
3054
Julia Reynolds3aedded2017-03-31 14:42:09 -04003055
3056 /**
3057 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3058 */
3059 public CharSequence getSettingsText() {
3060 return mSettingsText;
3061 }
3062
Julia Reynolds13d898c2017-02-02 12:22:05 -05003063 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003064 * Returns which type of notifications in a group are responsible for audibly alerting the
3065 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3066 * {@link #GROUP_ALERT_SUMMARY}.
3067 */
3068 public @GroupAlertBehavior int getGroupAlertBehavior() {
3069 return mGroupAlertBehavior;
3070 }
3071
3072 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003073 * The small icon representing this notification in the status bar and content view.
3074 *
3075 * @return the small icon representing this notification.
3076 *
3077 * @see Builder#getSmallIcon()
3078 * @see Builder#setSmallIcon(Icon)
3079 */
3080 public Icon getSmallIcon() {
3081 return mSmallIcon;
3082 }
3083
3084 /**
3085 * Used when notifying to clean up legacy small icons.
3086 * @hide
3087 */
3088 public void setSmallIcon(Icon icon) {
3089 mSmallIcon = icon;
3090 }
3091
3092 /**
3093 * The large icon shown in this notification's content view.
3094 * @see Builder#getLargeIcon()
3095 * @see Builder#setLargeIcon(Icon)
3096 */
3097 public Icon getLargeIcon() {
3098 return mLargeIcon;
3099 }
3100
3101 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003102 * @hide
3103 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02003104 public boolean isGroupSummary() {
3105 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3106 }
3107
3108 /**
3109 * @hide
3110 */
3111 public boolean isGroupChild() {
3112 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3113 }
3114
3115 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003116 * @hide
3117 */
3118 public boolean suppressAlertingDueToGrouping() {
3119 if (isGroupSummary()
3120 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3121 return true;
3122 } else if (isGroupChild()
3123 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3124 return true;
3125 }
3126 return false;
3127 }
3128
3129 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003130 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003131 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003132 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003133 * content views using the platform's notification layout template. If your app supports
3134 * versions of Android as old as API level 4, you can instead use
3135 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3136 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3137 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003138 *
Scott Main183bf112012-08-13 19:12:13 -07003139 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003140 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003141 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003142 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003143 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3144 * .setContentText(subject)
3145 * .setSmallIcon(R.drawable.new_mail)
3146 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003147 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003148 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003149 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003150 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003151 /**
3152 * @hide
3153 */
3154 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3155 "android.rebuild.contentViewActionCount";
3156 /**
3157 * @hide
3158 */
3159 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3160 = "android.rebuild.bigViewActionCount";
3161 /**
3162 * @hide
3163 */
3164 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3165 = "android.rebuild.hudViewActionCount";
3166
Daniel Sandler602ad1c2012-06-12 16:06:27 -04003167 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04003168
Selim Cinek6743c0b2017-01-18 18:24:01 -08003169 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3170 SystemProperties.getBoolean("notifications.only_title", true);
3171
Selim Cinek389edcd2017-05-11 19:16:44 -07003172 /**
3173 * The lightness difference that has to be added to the primary text color to obtain the
3174 * secondary text color when the background is light.
3175 */
3176 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3177
3178 /**
3179 * The lightness difference that has to be added to the primary text color to obtain the
3180 * secondary text color when the background is dark.
3181 * A bit less then the above value, since it looks better on dark backgrounds.
3182 */
3183 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3184
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003186 private Notification mN;
3187 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003188 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003189 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003190 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003191 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003192 private boolean mIsLegacy;
3193 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003194
3195 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003196 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3197 */
3198 private int mCachedContrastColor = COLOR_INVALID;
3199 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003200 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003201 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003202 */
3203 private int mCachedAmbientColor = COLOR_INVALID;
3204 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003205 /**
3206 * A neutral color color that can be used for icons.
3207 */
3208 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003209
3210 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003211 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3212 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3213 */
3214 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003215 private int mTextColorsAreForBackground = COLOR_INVALID;
3216 private int mPrimaryTextColor = COLOR_INVALID;
3217 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003218 private int mBackgroundColor = COLOR_INVALID;
3219 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003220 /**
3221 * A temporary location where actions are stored. If != null the view originally has action
3222 * but doesn't have any for this inflation.
3223 */
3224 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003225 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003226
Anthony Chenad4d1582017-04-10 16:07:58 -07003227 private boolean mTintActionButtons;
3228 private boolean mInNightMode;
3229
Adrian Roos70d7aa32017-01-11 15:39:06 -08003230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003231 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003233 * @param context
3234 * A {@link Context} that will be used by the Builder to construct the
3235 * RemoteViews. The Context will not be held past the lifetime of this Builder
3236 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003237 * @param channelId
3238 * The constructed Notification will be posted on this
3239 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3240 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003241 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003242 public Builder(Context context, String channelId) {
3243 this(context, (Notification) null);
3244 mN.mChannelId = channelId;
3245 }
3246
3247 /**
3248 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3249 * instead. All posted Notifications must specify a NotificationChannel Id.
3250 */
3251 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003252 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003253 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003254 }
3255
Joe Onoratocb109a02011-01-18 17:57:41 -08003256 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003257 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003258 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003259 public Builder(Context context, Notification toAdopt) {
3260 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003261 Resources res = mContext.getResources();
3262 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3263
3264 if (res.getBoolean(R.bool.config_enableNightMode)) {
3265 Configuration currentConfig = res.getConfiguration();
3266 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3267 == Configuration.UI_MODE_NIGHT_YES;
3268 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003269
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003270 if (toAdopt == null) {
3271 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003272 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3273 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3274 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003275 mN.priority = PRIORITY_DEFAULT;
3276 mN.visibility = VISIBILITY_PRIVATE;
3277 } else {
3278 mN = toAdopt;
3279 if (mN.actions != null) {
3280 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003281 }
3282
Selim Cineke7238dd2017-12-14 17:48:32 -08003283 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3284 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3285 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003286 }
3287
Selim Cinek4ac6f602016-06-13 15:47:03 -07003288 if (mN.getSmallIcon() == null && mN.icon != 0) {
3289 setSmallIcon(mN.icon);
3290 }
3291
3292 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3293 setLargeIcon(mN.largeIcon);
3294 }
3295
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003296 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3297 if (!TextUtils.isEmpty(templateClass)) {
3298 final Class<? extends Style> styleClass
3299 = getNotificationStyleClass(templateClass);
3300 if (styleClass == null) {
3301 Log.d(TAG, "Unknown style class: " + templateClass);
3302 } else {
3303 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003304 final Constructor<? extends Style> ctor =
3305 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 ctor.setAccessible(true);
3307 final Style style = ctor.newInstance();
3308 style.restoreFromExtras(mN.extras);
3309
3310 if (style != null) {
3311 setStyle(style);
3312 }
3313 } catch (Throwable t) {
3314 Log.e(TAG, "Could not create Style", t);
3315 }
3316 }
3317 }
3318
3319 }
3320 }
3321
Lucas Dupina291d192018-06-07 13:59:42 -07003322 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003323 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003324 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003325 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003326 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003327 }
3328
3329 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003330 * If this notification is duplicative of a Launcher shortcut, sets the
3331 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3332 * the shortcut.
3333 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003334 * This field will be ignored by Launchers that don't support badging, don't show
3335 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003336 *
3337 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3338 * supersedes
3339 */
3340 public Builder setShortcutId(String shortcutId) {
3341 mN.mShortcutId = shortcutId;
3342 return this;
3343 }
3344
3345 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003346 * Sets which icon to display as a badge for this notification.
3347 *
3348 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3349 * {@link #BADGE_ICON_LARGE}.
3350 *
3351 * Note: This value might be ignored, for launchers that don't support badge icons.
3352 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003353 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003354 mN.mBadgeIcon = icon;
3355 return this;
3356 }
3357
3358 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003359 * Sets the group alert behavior for this notification. Use this method to mute this
3360 * notification if alerts for this notification's group should be handled by a different
3361 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003362 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3363 * mute. For example, if you want only the summary of your group to make noise, all
3364 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003365 *
3366 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3367 */
3368 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3369 mN.mGroupAlertBehavior = groupAlertBehavior;
3370 return this;
3371 }
3372
Jeff Sharkey000ce802017-04-29 13:13:27 -06003373 /** @removed */
3374 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003375 public Builder setChannel(String channelId) {
3376 mN.mChannelId = channelId;
3377 return this;
3378 }
3379
3380 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003381 * Specifies the channel the notification should be delivered on.
3382 */
3383 public Builder setChannelId(String channelId) {
3384 mN.mChannelId = channelId;
3385 return this;
3386 }
3387
Jeff Sharkey000ce802017-04-29 13:13:27 -06003388 /** @removed */
3389 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003390 public Builder setTimeout(long durationMs) {
3391 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003392 return this;
3393 }
3394
3395 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003396 * Specifies a duration in milliseconds after which this notification should be canceled,
3397 * if it is not already canceled.
3398 */
3399 public Builder setTimeoutAfter(long durationMs) {
3400 mN.mTimeout = durationMs;
3401 return this;
3402 }
3403
3404 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003405 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003406 *
3407 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3408 * shown anymore by default and must be opted into by using
3409 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003410 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003411 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003413 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003414 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003415 return this;
3416 }
3417
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003419 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003420 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003421 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3422 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003423 */
3424 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003425 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003426 return this;
3427 }
3428
3429 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003430 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003431 *
3432 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003433 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003434 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003435 * Useful when showing an elapsed time (like an ongoing phone call).
3436 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003437 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003438 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003439 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003440 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003441 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003442 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003443 */
3444 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003445 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003446 return this;
3447 }
3448
3449 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003450 * Sets the Chronometer to count down instead of counting up.
3451 *
3452 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3453 * If it isn't set the chronometer will count up.
3454 *
3455 * @see #setUsesChronometer(boolean)
3456 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003457 public Builder setChronometerCountDown(boolean countDown) {
3458 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003459 return this;
3460 }
3461
3462 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003463 * Set the small icon resource, which will be used to represent the notification in the
3464 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003465 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003466
3467 * The platform template for the expanded view will draw this icon in the left, unless a
3468 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3469 * icon will be moved to the right-hand side.
3470 *
3471
3472 * @param icon
3473 * A resource ID in the application's package of the drawable to use.
3474 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003475 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003476 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003477 return setSmallIcon(icon != 0
3478 ? Icon.createWithResource(mContext, icon)
3479 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003480 }
3481
Joe Onoratocb109a02011-01-18 17:57:41 -08003482 /**
3483 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3484 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3485 * LevelListDrawable}.
3486 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003487 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003488 * @param level The level to use for the icon.
3489 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003490 * @see Notification#icon
3491 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003492 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003493 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003494 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003495 return setSmallIcon(icon);
3496 }
3497
3498 /**
3499 * Set the small icon, which will be used to represent the notification in the
3500 * status bar and content view (unless overriden there by a
3501 * {@link #setLargeIcon(Bitmap) large icon}).
3502 *
3503 * @param icon An Icon object to use.
3504 * @see Notification#icon
3505 */
3506 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003507 mN.setSmallIcon(icon);
3508 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3509 mN.icon = icon.getResId();
3510 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003511 return this;
3512 }
3513
Joe Onoratocb109a02011-01-18 17:57:41 -08003514 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003515 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003516 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003517 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003519 return this;
3520 }
3521
Joe Onoratocb109a02011-01-18 17:57:41 -08003522 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003523 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003524 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003525 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003527 return this;
3528 }
3529
Joe Onoratocb109a02011-01-18 17:57:41 -08003530 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003531 * This provides some additional information that is displayed in the notification. No
3532 * guarantees are given where exactly it is displayed.
3533 *
3534 * <p>This information should only be provided if it provides an essential
3535 * benefit to the understanding of the notification. The more text you provide the
3536 * less readable it becomes. For example, an email client should only provide the account
3537 * name here if more than one email account has been added.</p>
3538 *
3539 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3540 * notification header area.
3541 *
3542 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3543 * this will be shown in the third line of text in the platform notification template.
3544 * You should not be using {@link #setProgress(int, int, boolean)} at the
3545 * same time on those versions; they occupy the same place.
3546 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003547 */
3548 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003549 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003550 return this;
3551 }
3552
3553 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003554 * Provides text that will appear as a link to your application's settings.
3555 *
3556 * <p>This text does not appear within notification {@link Style templates} but may
3557 * appear when the user uses an affordance to learn more about the notification.
3558 * Additionally, this text will not appear unless you provide a valid link target by
3559 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3560 *
3561 * <p>This text is meant to be concise description about what the user can customize
3562 * when they click on this link. The recommended maximum length is 40 characters.
3563 * @param text
3564 * @return
3565 */
3566 public Builder setSettingsText(CharSequence text) {
3567 mN.mSettingsText = safeCharSequence(text);
3568 return this;
3569 }
3570
3571 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003572 * Set the remote input history.
3573 *
3574 * This should be set to the most recent inputs that have been sent
3575 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3576 * longer relevant (e.g. for chat notifications once the other party has responded).
3577 *
3578 * The most recent input must be stored at the 0 index, the second most recent at the
3579 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3580 * and how much of each individual input is shown.
3581 *
3582 * <p>Note: The reply text will only be shown on notifications that have least one action
3583 * with a {@code RemoteInput}.</p>
3584 */
3585 public Builder setRemoteInputHistory(CharSequence[] text) {
3586 if (text == null) {
3587 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3588 } else {
3589 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3590 CharSequence[] safe = new CharSequence[N];
3591 for (int i = 0; i < N; i++) {
3592 safe[i] = safeCharSequence(text[i]);
3593 }
3594 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3595 }
3596 return this;
3597 }
3598
3599 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003600 * Sets whether remote history entries view should have a spinner.
3601 * @hide
3602 */
3603 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3604 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3605 return this;
3606 }
3607
3608 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003609 * Sets whether smart reply buttons should be hidden.
3610 * @hide
3611 */
3612 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3613 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3614 return this;
3615 }
3616
3617 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003618 * Sets the number of items this notification represents. May be displayed as a badge count
3619 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003620 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003621 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003622 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003623 return this;
3624 }
3625
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003627 * A small piece of additional information pertaining to this notification.
3628 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003629 * The platform template will draw this on the last line of the notification, at the far
3630 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003631 *
3632 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3633 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3634 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003635 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003636 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003637 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003639 return this;
3640 }
3641
Joe Onoratocb109a02011-01-18 17:57:41 -08003642 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003643 * Set the progress this notification represents.
3644 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003645 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003646 */
3647 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003648 mN.extras.putInt(EXTRA_PROGRESS, progress);
3649 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3650 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003651 return this;
3652 }
3653
3654 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003655 * Supply a custom RemoteViews to use instead of the platform template.
3656 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003657 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003658 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003659 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003660 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003661 return setCustomContentView(views);
3662 }
3663
3664 /**
3665 * Supply custom RemoteViews to use instead of the platform template.
3666 *
3667 * This will override the layout that would otherwise be constructed by this Builder
3668 * object.
3669 */
3670 public Builder setCustomContentView(RemoteViews contentView) {
3671 mN.contentView = contentView;
3672 return this;
3673 }
3674
3675 /**
3676 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3677 *
3678 * This will override the expanded layout that would otherwise be constructed by this
3679 * Builder object.
3680 */
3681 public Builder setCustomBigContentView(RemoteViews contentView) {
3682 mN.bigContentView = contentView;
3683 return this;
3684 }
3685
3686 /**
3687 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3688 *
3689 * This will override the heads-up layout that would otherwise be constructed by this
3690 * Builder object.
3691 */
3692 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3693 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003694 return this;
3695 }
3696
Joe Onoratocb109a02011-01-18 17:57:41 -08003697 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3699 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003700 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3701 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3702 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003703 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003704 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003705 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003706 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003707 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003708 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003709 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003710 return this;
3711 }
3712
Joe Onoratocb109a02011-01-18 17:57:41 -08003713 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003714 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3715 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003716 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003717 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003718 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003719 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003720 return this;
3721 }
3722
Joe Onoratocb109a02011-01-18 17:57:41 -08003723 /**
3724 * An intent to launch instead of posting the notification to the status bar.
3725 * Only for use with extremely high-priority notifications demanding the user's
3726 * <strong>immediate</strong> attention, such as an incoming phone call or
3727 * alarm clock that the user has explicitly set to a particular time.
3728 * If this facility is used for something else, please give the user an option
3729 * to turn it off and use a normal notification, as this can be extremely
3730 * disruptive.
3731 *
Chris Wren47c20a12014-06-18 17:27:29 -04003732 * <p>
3733 * The system UI may choose to display a heads-up notification, instead of
3734 * launching this intent, while the user is using the device.
3735 * </p>
3736 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003737 * @param intent The pending intent to launch.
3738 * @param highPriority Passing true will cause this notification to be sent
3739 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003740 *
3741 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003742 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003743 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003745 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3746 return this;
3747 }
3748
Joe Onoratocb109a02011-01-18 17:57:41 -08003749 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003750 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003751 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003752 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003753 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003756 return this;
3757 }
3758
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003760 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003761 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003762 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003763 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003764 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003765 setTicker(tickerText);
3766 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003767 return this;
3768 }
3769
Joe Onoratocb109a02011-01-18 17:57:41 -08003770 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003771 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003772 *
3773 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003774 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3775 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003776 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003777 public Builder setLargeIcon(Bitmap b) {
3778 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3779 }
3780
3781 /**
3782 * Add a large icon to the notification content view.
3783 *
3784 * In the platform template, this image will be shown on the left of the notification view
3785 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3786 * badge atop the large icon).
3787 */
3788 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003789 mN.mLargeIcon = icon;
3790 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003791 return this;
3792 }
3793
Joe Onoratocb109a02011-01-18 17:57:41 -08003794 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003795 * Set the sound to play.
3796 *
John Spurlockc0650f022014-07-19 13:22:39 -04003797 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3798 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003799 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003800 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003801 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003802 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003803 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003804 mN.sound = sound;
3805 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003806 return this;
3807 }
3808
Joe Onoratocb109a02011-01-18 17:57:41 -08003809 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003810 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003811 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003812 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3813 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003814 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003815 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003816 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003817 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003818 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003819 mN.sound = sound;
3820 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003821 return this;
3822 }
3823
Joe Onoratocb109a02011-01-18 17:57:41 -08003824 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003825 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3826 * use during playback.
3827 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003828 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003829 * @see Notification#sound
3830 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003831 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003832 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003833 mN.sound = sound;
3834 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003835 return this;
3836 }
3837
3838 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003839 * Set the vibration pattern to use.
3840 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003841 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3842 * <code>pattern</code> parameter.
3843 *
Chris Wren47c20a12014-06-18 17:27:29 -04003844 * <p>
3845 * A notification that vibrates is more likely to be presented as a heads-up notification.
3846 * </p>
3847 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003848 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003849 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003850 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003851 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003852 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003853 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003854 return this;
3855 }
3856
Joe Onoratocb109a02011-01-18 17:57:41 -08003857 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003858 * Set the desired color for the indicator LED on the device, as well as the
3859 * blink duty cycle (specified in milliseconds).
3860 *
3861
3862 * Not all devices will honor all (or even any) of these values.
3863 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003864 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003865 * @see Notification#ledARGB
3866 * @see Notification#ledOnMS
3867 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003868 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003869 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003870 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003871 mN.ledARGB = argb;
3872 mN.ledOnMS = onMs;
3873 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003874 if (onMs != 0 || offMs != 0) {
3875 mN.flags |= FLAG_SHOW_LIGHTS;
3876 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003877 return this;
3878 }
3879
Joe Onoratocb109a02011-01-18 17:57:41 -08003880 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003881 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003882 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003883
3884 * Ongoing notifications cannot be dismissed by the user, so your application or service
3885 * must take care of canceling them.
3886 *
3887
3888 * They are typically used to indicate a background task that the user is actively engaged
3889 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3890 * (e.g., a file download, sync operation, active network connection).
3891 *
3892
3893 * @see Notification#FLAG_ONGOING_EVENT
3894 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003895 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003896 public Builder setOngoing(boolean ongoing) {
3897 setFlag(FLAG_ONGOING_EVENT, ongoing);
3898 return this;
3899 }
3900
Joe Onoratocb109a02011-01-18 17:57:41 -08003901 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003902 * Set whether this notification should be colorized. When set, the color set with
3903 * {@link #setColor(int)} will be used as the background color of this notification.
3904 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003905 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3906 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003907 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003908 * For most styles, the coloring will only be applied if the notification is for a
3909 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003910 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003911 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003912 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003913 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003914 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003915 */
3916 public Builder setColorized(boolean colorize) {
3917 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3918 return this;
3919 }
3920
3921 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003922 * Set this flag if you would only like the sound, vibrate
3923 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003924 *
3925 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003926 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003927 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3928 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3929 return this;
3930 }
3931
Joe Onoratocb109a02011-01-18 17:57:41 -08003932 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003933 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003934 *
3935 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003936 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003937 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003938 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003939 return this;
3940 }
3941
Joe Onoratocb109a02011-01-18 17:57:41 -08003942 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003943 * Set whether or not this notification should not bridge to other devices.
3944 *
3945 * <p>Some notifications can be bridged to other devices for remote display.
3946 * This hint can be set to recommend this notification not be bridged.
3947 */
3948 public Builder setLocalOnly(boolean localOnly) {
3949 setFlag(FLAG_LOCAL_ONLY, localOnly);
3950 return this;
3951 }
3952
3953 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003954 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003955 * <p>
3956 * The value should be one or more of the following fields combined with
3957 * bitwise-or:
3958 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3959 * <p>
3960 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003961 *
3962 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003963 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003964 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003965 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003966 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003967 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003969 return this;
3970 }
3971
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003972 /**
3973 * Set the priority of this notification.
3974 *
3975 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003976 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003977 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003978 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003979 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003980 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003981 return this;
3982 }
Joe Malin8d40d042012-11-05 11:36:40 -08003983
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003984 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003985 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003986 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003987 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003988 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003989 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003991 return this;
3992 }
3993
3994 /**
Chris Wrendde75302014-03-26 17:24:15 -04003995 * Add a person that is relevant to this notification.
3996 *
Chris Wrene6c48932014-09-29 17:19:27 -04003997 * <P>
3998 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003999 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4000 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4001 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004002 * </P>
4003 *
4004 * <P>
4005 * The person should be specified by the {@code String} representation of a
4006 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4007 * </P>
4008 *
4009 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4010 * URIs. The path part of these URIs must exist in the contacts database, in the
4011 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4012 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004013 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4014 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004015 * </P>
4016 *
4017 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004018 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004019 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004020 */
Chris Wrene6c48932014-09-29 17:19:27 -04004021 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004022 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004023 return this;
4024 }
4025
4026 /**
4027 * Add a person that is relevant to this notification.
4028 *
4029 * <P>
4030 * Depending on user preferences, this annotation may allow the notification to pass
4031 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4032 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4033 * appear more prominently in the user interface.
4034 * </P>
4035 *
4036 * <P>
4037 * A person should usually contain a uri in order to benefit from the ranking boost.
4038 * However, even if no uri is provided, it's beneficial to provide other people in the
4039 * notification, such that listeners and voice only devices can announce and handle them
4040 * properly.
4041 * </P>
4042 *
4043 * @param person the person to add.
4044 * @see Notification#EXTRA_PEOPLE_LIST
4045 */
4046 public Builder addPerson(Person person) {
4047 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004048 return this;
4049 }
4050
4051 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004052 * Set this notification to be part of a group of notifications sharing the same key.
4053 * Grouped notifications may display in a cluster or stack on devices which
4054 * support such rendering.
4055 *
4056 * <p>To make this notification the summary for its group, also call
4057 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4058 * {@link #setSortKey}.
4059 * @param groupKey The group key of the group.
4060 * @return this object for method chaining
4061 */
4062 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004063 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004064 return this;
4065 }
4066
4067 /**
4068 * Set this notification to be the group summary for a group of notifications.
4069 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004070 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4071 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004072 * @param isGroupSummary Whether this notification should be a group summary.
4073 * @return this object for method chaining
4074 */
4075 public Builder setGroupSummary(boolean isGroupSummary) {
4076 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4077 return this;
4078 }
4079
4080 /**
4081 * Set a sort key that orders this notification among other notifications from the
4082 * same package. This can be useful if an external sort was already applied and an app
4083 * would like to preserve this. Notifications will be sorted lexicographically using this
4084 * value, although providing different priorities in addition to providing sort key may
4085 * cause this value to be ignored.
4086 *
4087 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004088 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004089 *
4090 * @see String#compareTo(String)
4091 */
4092 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004093 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004094 return this;
4095 }
4096
4097 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004098 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004099 *
Griff Hazen720042b2014-02-24 15:46:56 -08004100 * <p>Values within the Bundle will replace existing extras values in this Builder.
4101 *
4102 * @see Notification#extras
4103 */
Griff Hazen959591e2014-05-15 22:26:18 -07004104 public Builder addExtras(Bundle extras) {
4105 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004106 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004107 }
4108 return this;
4109 }
4110
4111 /**
4112 * Set metadata for this notification.
4113 *
4114 * <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 -04004115 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004116 * called.
4117 *
Griff Hazen720042b2014-02-24 15:46:56 -08004118 * <p>Replaces any existing extras values with those from the provided Bundle.
4119 * Use {@link #addExtras} to merge in metadata instead.
4120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004121 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004122 */
Griff Hazen959591e2014-05-15 22:26:18 -07004123 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004124 if (extras != null) {
4125 mUserExtras = extras;
4126 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004127 return this;
4128 }
4129
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004130 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004131 * Get the current metadata Bundle used by this notification Builder.
4132 *
4133 * <p>The returned Bundle is shared with this Builder.
4134 *
4135 * <p>The current contents of this Bundle are copied into the Notification each time
4136 * {@link #build()} is called.
4137 *
4138 * @see Notification#extras
4139 */
4140 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004141 return mUserExtras;
4142 }
4143
4144 private Bundle getAllExtras() {
4145 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4146 saveExtras.putAll(mN.extras);
4147 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004148 }
4149
4150 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004151 * Add an action to this notification. Actions are typically displayed by
4152 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004153 * <p>
4154 * Every action must have an icon (32dp square and matching the
4155 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4156 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4157 * <p>
4158 * A notification in its expanded form can display up to 3 actions, from left to right in
4159 * the order they were added. Actions will not be displayed when the notification is
4160 * collapsed, however, so be sure that any essential functions may be accessed by the user
4161 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004162 *
4163 * @param icon Resource ID of a drawable that represents the action.
4164 * @param title Text describing the action.
4165 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004166 *
4167 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004168 */
Dan Sandler86647982015-05-13 23:41:13 -04004169 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004170 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004171 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004172 return this;
4173 }
4174
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004175 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004176 * Add an action to this notification. Actions are typically displayed by
4177 * the system as a button adjacent to the notification content.
4178 * <p>
4179 * Every action must have an icon (32dp square and matching the
4180 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4181 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4182 * <p>
4183 * A notification in its expanded form can display up to 3 actions, from left to right in
4184 * the order they were added. Actions will not be displayed when the notification is
4185 * collapsed, however, so be sure that any essential functions may be accessed by the user
4186 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4187 *
4188 * @param action The action to add.
4189 */
4190 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004191 if (action != null) {
4192 mActions.add(action);
4193 }
Griff Hazen959591e2014-05-15 22:26:18 -07004194 return this;
4195 }
4196
4197 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004198 * Alter the complete list of actions attached to this notification.
4199 * @see #addAction(Action).
4200 *
4201 * @param actions
4202 * @return
4203 */
4204 public Builder setActions(Action... actions) {
4205 mActions.clear();
4206 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004207 if (actions[i] != null) {
4208 mActions.add(actions[i]);
4209 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004210 }
4211 return this;
4212 }
4213
4214 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004215 * Add a rich notification style to be applied at build time.
4216 *
4217 * @param style Object responsible for modifying the notification style.
4218 */
4219 public Builder setStyle(Style style) {
4220 if (mStyle != style) {
4221 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004222 if (mStyle != null) {
4223 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004224 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4225 } else {
4226 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004227 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004228 }
4229 return this;
4230 }
4231
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004232 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004233 * Returns the style set by {@link #setStyle(Style)}.
4234 */
4235 public Style getStyle() {
4236 return mStyle;
4237 }
4238
4239 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004240 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004241 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004242 * @return The same Builder.
4243 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004244 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004245 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004246 return this;
4247 }
4248
4249 /**
4250 * Supply a replacement Notification whose contents should be shown in insecure contexts
4251 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4252 * @param n A replacement notification, presumably with some or all info redacted.
4253 * @return The same Builder.
4254 */
4255 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004256 if (n != null) {
4257 mN.publicVersion = new Notification();
4258 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4259 } else {
4260 mN.publicVersion = null;
4261 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004262 return this;
4263 }
4264
Griff Hazenb720abe2014-05-20 13:15:30 -07004265 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004266 * Apply an extender to this notification builder. Extenders may be used to add
4267 * metadata or change options on this builder.
4268 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004269 public Builder extend(Extender extender) {
4270 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004271 return this;
4272 }
4273
Dan Sandler4e787062015-06-17 15:09:48 -04004274 /**
4275 * @hide
4276 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004277 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004278 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004279 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004280 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004281 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004282 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004283 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004284 }
4285
Dan Sandler26e81cf2014-05-06 10:01:27 -04004286 /**
4287 * Sets {@link Notification#color}.
4288 *
4289 * @param argb The accent color to use
4290 *
4291 * @return The same Builder.
4292 */
Tor Norbye80756e32015-03-02 09:39:27 -08004293 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004294 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004295 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004296 return this;
4297 }
4298
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004299 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004300 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4301 // This user can never be a badged profile,
4302 // and also includes USER_ALL system notifications.
4303 return null;
4304 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004305 // Note: This assumes that the current user can read the profile badge of the
4306 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004307 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004308 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004309 }
4310
4311 private Bitmap getProfileBadge() {
4312 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004313 if (badge == null) {
4314 return null;
4315 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004316 final int size = mContext.getResources().getDimensionPixelSize(
4317 R.dimen.notification_badge_size);
4318 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004319 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004320 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004321 badge.draw(canvas);
4322 return bitmap;
4323 }
4324
Selim Cinekc848c3a2016-01-13 15:27:30 -08004325 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004326 Bitmap profileBadge = getProfileBadge();
4327
Kenny Guy98193ea2014-07-24 19:54:37 +01004328 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004329 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4330 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004331 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004332 contentView.setDrawableTint(R.id.profile_badge, false,
4333 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004334 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004335 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004336 }
4337
Julia Reynoldsfc640012018-02-21 12:25:27 -05004338 /**
4339 * @hide
4340 */
4341 public boolean usesStandardHeader() {
4342 if (mN.mUsesStandardHeader) {
4343 return true;
4344 }
4345 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4346 if (mN.contentView == null && mN.bigContentView == null) {
4347 return true;
4348 }
4349 }
4350 boolean contentViewUsesHeader = mN.contentView == null
4351 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4352 boolean bigContentViewUsesHeader = mN.bigContentView == null
4353 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4354 return contentViewUsesHeader && bigContentViewUsesHeader;
4355 }
4356
Christoph Studerfe718432014-09-01 18:21:18 +02004357 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004358 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004359 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004360 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004361 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004362 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004363 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004364 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004365 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004366 }
4367
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004368 /**
4369 * Resets the notification header to its original state
4370 */
4371 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004372 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4373 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004374 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004375 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004376 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004377 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004378 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004379 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4380 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004381 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004382 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004383 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004384 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004385 contentView.setImageViewIcon(R.id.profile_badge, null);
4386 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004387 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004388 }
4389
Selim Cinek384804b2018-04-18 14:31:07 +08004390 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4391 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4392 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004393 }
4394
4395 /**
4396 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004397 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004398 */
Selim Cinek384804b2018-04-18 14:31:07 +08004399 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4400 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004401 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004402 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004403 }
4404
Selim Cinek384804b2018-04-18 14:31:07 +08004405 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4406 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004407 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004408
Christoph Studerfe718432014-09-01 18:21:18 +02004409 resetStandardTemplate(contentView);
4410
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004411 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004412 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004413 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004414 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004415 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4416 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004417 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004418 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004419 if (!p.ambient) {
4420 setTextViewColorPrimary(contentView, R.id.title);
4421 }
Selim Cinek954cc232016-05-20 13:29:23 -07004422 contentView.setViewLayoutWidth(R.id.title, showProgress
4423 ? ViewGroup.LayoutParams.WRAP_CONTENT
4424 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004425 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004426 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004427 int textId = showProgress ? com.android.internal.R.id.text_line_1
4428 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004429 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004430 if (!p.ambient) {
4431 setTextViewColorSecondary(contentView, textId);
4432 }
Selim Cinek41598732016-01-11 16:58:37 -08004433 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004434 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004435
Selim Cinek279fa862016-06-14 10:57:25 -07004436 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004437
Selim Cinek29603462015-11-17 19:04:39 -08004438 return contentView;
4439 }
4440
Selim Cinek48f66b72017-08-18 16:17:51 -07004441 private CharSequence processTextSpans(CharSequence text) {
4442 if (hasForegroundColor()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004443 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004444 }
4445 return text;
4446 }
4447
Selim Cinek7b9605b2017-01-19 17:36:00 -08004448 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4449 ensureColors();
4450 contentView.setTextColor(id, mPrimaryTextColor);
4451 }
4452
Selim Cinek48f66b72017-08-18 16:17:51 -07004453 private boolean hasForegroundColor() {
4454 return mForegroundColor != COLOR_INVALID;
4455 }
4456
Selim Cinek389edcd2017-05-11 19:16:44 -07004457 /**
4458 * @return the primary text color
4459 * @hide
4460 */
4461 @VisibleForTesting
4462 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004463 ensureColors();
4464 return mPrimaryTextColor;
4465 }
4466
Selim Cinek389edcd2017-05-11 19:16:44 -07004467 /**
4468 * @return the secondary text color
4469 * @hide
4470 */
4471 @VisibleForTesting
4472 public int getSecondaryTextColor() {
4473 ensureColors();
4474 return mSecondaryTextColor;
4475 }
4476
Selim Cinek7b9605b2017-01-19 17:36:00 -08004477 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4478 ensureColors();
4479 contentView.setTextColor(id, mSecondaryTextColor);
4480 }
4481
4482 private void ensureColors() {
4483 int backgroundColor = getBackgroundColor();
4484 if (mPrimaryTextColor == COLOR_INVALID
4485 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004486 || mTextColorsAreForBackground != backgroundColor) {
4487 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004488 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004489 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004490 backgroundColor);
Lucas Dupina291d192018-06-07 13:59:42 -07004491 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004492 backgroundColor);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004493 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004494 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004495 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004496 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004497 mSecondaryTextColor, backgroundColor, 4.5);
4498 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004499 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004500 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4501 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4502 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004503 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004504 // We only respect the given colors if worst case Black or White still has
4505 // contrast
4506 boolean backgroundLight = backLum > textLum
4507 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4508 || backLum <= textLum
4509 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004510 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004511 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004512 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004513 mForegroundColor,
4514 backgroundColor,
4515 true /* findFG */,
4516 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004517 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004518 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004519 } else {
4520 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004521 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004522 mForegroundColor,
4523 backgroundColor,
4524 true /* findFG */,
4525 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004526 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004527 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004528 }
4529 } else {
4530 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004531 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004532 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4533 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004534 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004535 backgroundColor) < 4.5f) {
4536 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004537 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004538 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004539 mSecondaryTextColor,
4540 backgroundColor,
4541 true /* findFG */,
4542 4.5f);
4543 } else {
4544 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004545 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004546 mSecondaryTextColor,
4547 backgroundColor,
4548 true /* findFG */,
4549 4.5f);
4550 }
Lucas Dupina291d192018-06-07 13:59:42 -07004551 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004552 mSecondaryTextColor, backgroundLight
4553 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4554 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004555 }
4556 }
4557 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004558 }
4559 }
4560
4561 private void updateBackgroundColor(RemoteViews contentView) {
4562 if (isColorized()) {
4563 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4564 getBackgroundColor());
4565 } else {
4566 // Clear it!
4567 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4568 0);
4569 }
4570 }
4571
Selim Cinek860b6da2015-12-16 19:02:19 -08004572 /**
4573 * @param remoteView the remote view to update the minheight in
4574 * @param hasMinHeight does it have a mimHeight
4575 * @hide
4576 */
4577 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4578 int minHeight = 0;
4579 if (hasMinHeight) {
4580 // we need to set the minHeight of the notification
4581 minHeight = mContext.getResources().getDimensionPixelSize(
4582 com.android.internal.R.dimen.notification_min_content_height);
4583 }
4584 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4585 }
4586
Selim Cinek29603462015-11-17 19:04:39 -08004587 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004588 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4589 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4590 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4591 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004592 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004593 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004594 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004595 contentView.setProgressBackgroundTintList(
4596 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4597 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004598 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004599 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004600 contentView.setProgressTintList(R.id.progress, colorStateList);
4601 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004602 }
Selim Cinek29603462015-11-17 19:04:39 -08004603 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004604 } else {
4605 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004606 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004607 }
Joe Onorato561d3852010-11-20 18:09:34 -08004608 }
4609
Selim Cinek384804b2018-04-18 14:31:07 +08004610 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4611 TemplateBindResult result) {
4612 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004613 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004614 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004615 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4616 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4617 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4618 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4619 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004620 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004621 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004622 }
4623 }
4624
Selim Cinek1c72fa02018-04-23 18:00:54 +08004625 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4626 int marginEnd = 0;
4627 int contentMargin = mContext.getResources().getDimensionPixelSize(
4628 R.dimen.notification_content_margin_end);
4629 int iconSize = mContext.getResources().getDimensionPixelSize(
4630 R.dimen.notification_right_icon_size);
4631 if (replyIconShown) {
4632 // The size of the reply icon
4633 marginEnd += iconSize;
4634
4635 int replyInset = mContext.getResources().getDimensionPixelSize(
4636 R.dimen.notification_reply_inset);
4637 // We're subtracting the inset of the reply icon to make sure it's
4638 // aligned nicely on the right, and remove it from the following padding
4639 marginEnd -= replyInset * 2;
4640 }
4641 if (largeIconShown) {
4642 // adding size of the right icon
4643 marginEnd += iconSize;
4644
4645 if (replyIconShown) {
4646 // We also add some padding to the reply icon if it's around
4647 marginEnd += contentMargin;
4648 }
4649 }
4650 if (replyIconShown || largeIconShown) {
4651 // The padding to the content
4652 marginEnd += contentMargin;
4653 }
4654 return marginEnd;
4655 }
4656
Selim Cinek384804b2018-04-18 14:31:07 +08004657 /**
4658 * Bind the large icon.
4659 * @return if the largeIcon is visible
4660 */
4661 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004662 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4663 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4664 }
Selim Cinek88188f22017-09-19 16:46:56 -07004665 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4666 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004667 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4668 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4669 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004670 }
Selim Cinek384804b2018-04-18 14:31:07 +08004671 return showLargeIcon;
4672 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004673
Selim Cinek384804b2018-04-18 14:31:07 +08004674 /**
4675 * Bind the reply icon.
4676 * @return if the reply icon is visible
4677 */
4678 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4679 boolean actionVisible = !hideReplyIcon;
4680 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004681 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004682 action = findReplyAction();
4683 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004684 }
Selim Cinek384804b2018-04-18 14:31:07 +08004685 if (actionVisible) {
4686 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4687 contentView.setDrawableTint(R.id.reply_icon_action,
4688 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004689 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004690 PorterDuff.Mode.SRC_ATOP);
4691 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4692 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4693 } else {
4694 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4695 }
4696 contentView.setViewVisibility(R.id.reply_icon_action,
4697 actionVisible ? View.VISIBLE : View.GONE);
4698 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004699 }
4700
4701 private Action findReplyAction() {
4702 ArrayList<Action> actions = mActions;
4703 if (mOriginalActions != null) {
4704 actions = mOriginalActions;
4705 }
4706 int numActions = actions.size();
4707 for (int i = 0; i < numActions; i++) {
4708 Action action = actions.get(i);
4709 if (hasValidRemoteInput(action)) {
4710 return action;
4711 }
4712 }
4713 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004714 }
4715
Selim Cinekafeed292017-12-12 17:32:44 -08004716 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4717 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004718 bindSmallIcon(contentView, ambient);
4719 bindHeaderAppName(contentView, ambient);
4720 if (!ambient) {
4721 // Ambient view does not have these
4722 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004723 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004724 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004725 bindProfileBadge(contentView);
4726 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004727 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004728 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004729 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004730 }
4731
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004732 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004733 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004734 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4735 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4736 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4737 }
4738
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004739 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004740 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004741 contentView.setDrawableTint(R.id.expand_button, false, color,
4742 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004743 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004744 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004745 }
4746
4747 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4748 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004749 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004750 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004751 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4752 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4753 contentView.setLong(R.id.chronometer, "setBase",
4754 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4755 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004756 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004757 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004758 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004759 } else {
4760 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4761 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004762 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004763 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004764 } else {
4765 // We still want a time to be set but gone, such that we can show and hide it
4766 // on demand in case it's a child notification without anything in the header
4767 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004768 }
4769 }
4770
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004771 private void bindHeaderText(RemoteViews contentView) {
4772 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4773 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004774 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004775 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004776 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004777 if (headerText == null
4778 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4779 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4780 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4781 }
4782 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004783 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004784 contentView.setTextViewText(R.id.header_text, processTextSpans(
4785 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004786 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004787 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4788 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004789 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004790 }
4791 }
4792
Selim Cinekafeed292017-12-12 17:32:44 -08004793 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4794 if (!TextUtils.isEmpty(secondaryText)) {
4795 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4796 processLegacyText(secondaryText)));
4797 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4798 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4799 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4800 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4801 }
4802 }
4803
Adrian Rooseba05822016-04-22 17:09:27 -07004804 /**
4805 * @hide
4806 */
4807 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004808 CharSequence name = null;
4809 final PackageManager pm = mContext.getPackageManager();
4810 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4811 // only system packages which lump together a bunch of unrelated stuff
4812 // may substitute a different name to make the purpose of the
4813 // notification more clear. the correct package label should always
4814 // be accessible via SystemUI.
4815 final String pkg = mContext.getPackageName();
4816 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4817 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4818 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4819 name = subName;
4820 } else {
4821 Log.w(TAG, "warning: pkg "
4822 + pkg + " attempting to substitute app name '" + subName
4823 + "' without holding perm "
4824 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4825 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004826 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004827 if (TextUtils.isEmpty(name)) {
4828 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4829 }
4830 if (TextUtils.isEmpty(name)) {
4831 // still nothing?
4832 return null;
4833 }
4834
4835 return String.valueOf(name);
4836 }
Adrian Roos487374f2017-01-11 15:48:14 -08004837 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004838 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004839 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004840 setTextViewColorPrimary(contentView, R.id.app_name_text);
4841 } else {
4842 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004843 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004844 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004845 }
4846
Adrian Roos487374f2017-01-11 15:48:14 -08004847 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004848 if (mN.mSmallIcon == null && mN.icon != 0) {
4849 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4850 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004851 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004852 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004853 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004854 }
4855
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004856 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004857 * @return true if the built notification will show the time or the chronometer; false
4858 * otherwise
4859 */
4860 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004861 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004862 }
4863
Christoph Studerfe718432014-09-01 18:21:18 +02004864 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004865 // actions_container is only reset when there are no actions to avoid focus issues with
4866 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004867 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004868 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004869
4870 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4871 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004872 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4873 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004874
4875 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4876 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4877 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4878 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004879
Selim Cineked64a142018-02-06 18:06:01 -08004880 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4881 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004882 }
4883
Selim Cinek384804b2018-04-18 14:31:07 +08004884 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4885 TemplateBindResult result) {
4886 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4887 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004888 }
4889
Adrian Roos70d7aa32017-01-11 15:39:06 -08004890 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004891 StandardTemplateParams p, TemplateBindResult result) {
4892 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004893
Christoph Studerfe718432014-09-01 18:21:18 +02004894 resetStandardTemplateWithActions(big);
4895
Adrian Roose458aa82015-12-08 16:17:19 -08004896 boolean validRemoteInput = false;
4897
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004898 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004899 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004900 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004901 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004902 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004903 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004904 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004905 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004906 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004907 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004908 boolean actionHasValidInput = hasValidRemoteInput(action);
4909 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004910
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004911 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004912 p.ambient);
4913 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004914 // Clear the drawable
4915 button.setInt(R.id.action0, "setBackgroundResource", 0);
4916 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004917 big.addView(R.id.actions, button);
4918 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004919 } else {
4920 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004921 }
Adrian Roose458aa82015-12-08 16:17:19 -08004922
4923 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004924 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07004925 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
4926 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01004927 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08004928 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01004929 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
4930 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004931 big.setTextViewText(R.id.notification_material_reply_text_1,
4932 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004933 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01004934 big.setViewVisibility(R.id.notification_material_reply_progress,
4935 showSpinner ? View.VISIBLE : View.GONE);
4936 big.setProgressIndeterminateTintList(
4937 R.id.notification_material_reply_progress,
4938 ColorStateList.valueOf(
4939 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08004940
Selim Cinekbee4e072018-05-21 22:06:43 -07004941 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
4942 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08004943 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004944 big.setTextViewText(R.id.notification_material_reply_text_2,
4945 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004946 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004947
Selim Cinekbee4e072018-05-21 22:06:43 -07004948 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
4949 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08004950 big.setViewVisibility(
4951 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004952 big.setTextViewText(R.id.notification_material_reply_text_3,
4953 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004954 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004955 }
4956 }
4957 }
4958
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004959 return big;
4960 }
4961
Adrian Roose458aa82015-12-08 16:17:19 -08004962 private boolean hasValidRemoteInput(Action action) {
4963 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4964 // Weird actions
4965 return false;
4966 }
4967
4968 RemoteInput[] remoteInputs = action.getRemoteInputs();
4969 if (remoteInputs == null) {
4970 return false;
4971 }
4972
4973 for (RemoteInput r : remoteInputs) {
4974 CharSequence[] choices = r.getChoices();
4975 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4976 return true;
4977 }
4978 }
4979 return false;
4980 }
4981
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004982 /**
4983 * Construct a RemoteViews for the final 1U notification layout. In order:
4984 * 1. Custom contentView from the caller
4985 * 2. Style's proposed content view
4986 * 3. Standard template view
4987 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004988 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004989 return createContentView(false /* increasedheight */ );
4990 }
4991
4992 /**
4993 * Construct a RemoteViews for the smaller content view.
4994 *
4995 * @param increasedHeight true if this layout be created with an increased height. Some
4996 * styles may support showing more then just that basic 1U size
4997 * and the system may decide to render important notifications
4998 * slightly bigger even when collapsed.
4999 *
5000 * @hide
5001 */
5002 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005003 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005004 return mN.contentView;
5005 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005006 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005007 if (styleView != null) {
5008 return styleView;
5009 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005010 }
Selim Cinek384804b2018-04-18 14:31:07 +08005011 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005012 }
5013
Selim Cineka7679b62017-05-10 16:33:25 -07005014 private boolean useExistingRemoteView() {
5015 return mStyle == null || (!mStyle.displayCustomViewInline()
5016 && !mRebuildStyledRemoteViews);
5017 }
5018
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005019 /**
5020 * Construct a RemoteViews for the final big notification layout.
5021 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005022 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005023 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005024 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005025 return mN.bigContentView;
5026 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005027 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005028 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005029 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005030 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5031 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005032 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005033 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005034 return result;
5035 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005036
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005037 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005038 * Construct a RemoteViews for the final notification header only. This will not be
5039 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005040 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005041 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005042 * @hide
5043 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005044 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005045 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5046 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005047 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005048 ambient ? R.layout.notification_template_ambient_header
5049 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005050 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005051 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005052 if (colorized != null) {
5053 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5054 } else {
5055 mN.extras.remove(EXTRA_COLORIZED);
5056 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005057 return header;
5058 }
5059
Adrian Roos487374f2017-01-11 15:48:14 -08005060 /**
5061 * Construct a RemoteViews for the ambient version of the notification.
5062 *
5063 * @hide
5064 */
5065 public RemoteViews makeAmbientNotification() {
5066 RemoteViews ambient = applyStandardTemplateWithActions(
5067 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005068 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5069 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005070 return ambient;
5071 }
5072
Selim Cinek29603462015-11-17 19:04:39 -08005073 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005074 if (result != null) {
5075 result.setViewVisibility(R.id.text_line_1, View.GONE);
5076 }
Selim Cinek29603462015-11-17 19:04:39 -08005077 }
5078
Selim Cinek6743c0b2017-01-18 18:24:01 -08005079 /**
5080 * Adapt the Notification header if this view is used as an expanded view.
5081 *
5082 * @hide
5083 */
5084 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005085 if (result != null) {
5086 result.setBoolean(R.id.notification_header, "setExpanded", true);
5087 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005088 }
5089
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005090 /**
5091 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005092 *
5093 * @param increasedHeight true if this layout be created with an increased height. Some
5094 * styles may support showing more then just that basic 1U size
5095 * and the system may decide to render important notifications
5096 * slightly bigger even when collapsed.
5097 *
5098 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005099 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005100 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005101 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005102 return mN.headsUpContentView;
5103 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005104 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5105 if (styleView != null) {
5106 return styleView;
5107 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005108 } else if (mActions.size() == 0) {
5109 return null;
5110 }
5111
Selim Cinekbee4e072018-05-21 22:06:43 -07005112 // We only want at most a single remote input history to be shown here, otherwise
5113 // the content would become squished.
5114 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5115 .setMaxRemoteInputHistory(1);
5116 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5117 p,
5118 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005119 }
5120
Selim Cinek624c02db2015-12-14 21:00:02 -08005121 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005122 * Construct a RemoteViews for the final heads-up notification layout.
5123 */
5124 public RemoteViews createHeadsUpContentView() {
5125 return createHeadsUpContentView(false /* useIncreasedHeight */);
5126 }
5127
5128 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005129 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5130 *
5131 * @hide
5132 */
5133 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005134 return makePublicView(false /* ambient */);
5135 }
5136
5137 /**
5138 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5139 *
5140 * @hide
5141 */
5142 public RemoteViews makePublicAmbientNotification() {
5143 return makePublicView(true /* ambient */);
5144 }
5145
5146 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005147 if (mN.publicVersion != null) {
5148 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005149 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005150 }
5151 Bundle savedBundle = mN.extras;
5152 Style style = mStyle;
5153 mStyle = null;
5154 Icon largeIcon = mN.mLargeIcon;
5155 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005156 Bitmap largeIconLegacy = mN.largeIcon;
5157 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005158 ArrayList<Action> actions = mActions;
5159 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005160 Bundle publicExtras = new Bundle();
5161 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5162 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5163 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5164 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005165 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5166 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005167 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5168 if (appName != null) {
5169 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5170 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005171 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005172 RemoteViews view;
5173 if (ambient) {
5174 publicExtras.putCharSequence(EXTRA_TITLE,
5175 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5176 view = makeAmbientNotification();
5177 } else{
5178 view = makeNotificationHeader(false /* ambient */);
5179 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5180 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005181 mN.extras = savedBundle;
5182 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005183 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005184 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005185 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005186 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005187 }
5188
Selim Cinek6743c0b2017-01-18 18:24:01 -08005189 /**
5190 * Construct a content view for the display when low - priority
5191 *
5192 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5193 * a new subtext is created consisting of the content of the
5194 * notification.
5195 * @hide
5196 */
5197 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5198 int color = mN.color;
5199 mN.color = COLOR_DEFAULT;
5200 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5201 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5202 CharSequence newSummary = createSummaryText();
5203 if (!TextUtils.isEmpty(newSummary)) {
5204 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5205 }
5206 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005207
Adrian Roos6f6e1592017-05-02 16:22:53 -07005208 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005209 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005210 if (summary != null) {
5211 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5212 } else {
5213 mN.extras.remove(EXTRA_SUB_TEXT);
5214 }
5215 mN.color = color;
5216 return header;
5217 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005218
Selim Cinek6743c0b2017-01-18 18:24:01 -08005219 private CharSequence createSummaryText() {
5220 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5221 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5222 return titleText;
5223 }
5224 SpannableStringBuilder summary = new SpannableStringBuilder();
5225 if (titleText == null) {
5226 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5227 }
5228 BidiFormatter bidi = BidiFormatter.getInstance();
5229 if (titleText != null) {
5230 summary.append(bidi.unicodeWrap(titleText));
5231 }
5232 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5233 if (titleText != null && contentText != null) {
5234 summary.append(bidi.unicodeWrap(mContext.getText(
5235 R.string.notification_header_divider_symbol_with_spaces)));
5236 }
5237 if (contentText != null) {
5238 summary.append(bidi.unicodeWrap(contentText));
5239 }
5240 return summary;
5241 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005242
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005243 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005244 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005245 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005246 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005247 emphazisedMode ? getEmphasizedActionLayoutResource()
5248 : tombstone ? getActionTombstoneLayoutResource()
5249 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005250 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005251 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005252 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005253 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005254 if (action.mRemoteInputs != null) {
5255 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5256 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005257 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005258 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005259 CharSequence title = action.title;
5260 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005261 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005262 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005263 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005264 } else {
5265 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005266 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005267 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005268 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005269 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005270 int rippleColor;
5271 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5272 if (hasColorOverride) {
5273 // There's a span spanning the full text, let's take it and use it as the
5274 // background color
5275 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005276 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Selim Cinek396caca2018-04-10 17:46:46 -07005277 background);
5278 button.setTextColor(R.id.action0, textColor);
5279 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005280 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005281 rippleColor = resolveContrastColor();
5282 button.setTextColor(R.id.action0, rippleColor);
5283 } else {
5284 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005285 }
Selim Cinek396caca2018-04-10 17:46:46 -07005286 // We only want about 20% alpha for the ripple
5287 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5288 button.setColorStateList(R.id.action0, "setRippleColor",
5289 ColorStateList.valueOf(rippleColor));
5290 button.setColorStateList(R.id.action0, "setButtonBackground",
5291 ColorStateList.valueOf(background));
5292 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005293 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005294 button.setTextViewText(R.id.action0, processTextSpans(
5295 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005296 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005297 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005298 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005299 button.setTextColor(R.id.action0,
5300 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005301 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005302 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005303 return button;
5304 }
5305
Joe Onoratocb109a02011-01-18 17:57:41 -08005306 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005307 * Ensures contrast on color spans against a background color. also returns the color of the
5308 * text if a span was found that spans over the whole text.
5309 *
5310 * @param charSequence the charSequence on which the spans are
5311 * @param background the background color to ensure the contrast against
5312 * @param outResultColor an array in which a color will be returned as the first element if
5313 * there exists a full length color span.
5314 * @return the contrasted charSequence
5315 */
5316 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5317 ColorStateList[] outResultColor) {
5318 if (charSequence instanceof Spanned) {
5319 Spanned ss = (Spanned) charSequence;
5320 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5321 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5322 for (Object span : spans) {
5323 Object resultSpan = span;
5324 int spanStart = ss.getSpanStart(span);
5325 int spanEnd = ss.getSpanEnd(span);
5326 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5327 if (resultSpan instanceof CharacterStyle) {
5328 resultSpan = ((CharacterStyle) span).getUnderlying();
5329 }
5330 if (resultSpan instanceof TextAppearanceSpan) {
5331 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5332 ColorStateList textColor = originalSpan.getTextColor();
5333 if (textColor != null) {
5334 int[] colors = textColor.getColors();
5335 int[] newColors = new int[colors.length];
5336 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005337 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005338 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005339 }
5340 textColor = new ColorStateList(textColor.getStates().clone(),
5341 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005342 if (fullLength) {
5343 outResultColor[0] = textColor;
5344 // Let's drop the color from the span
5345 textColor = null;
5346 }
Selim Cinek981962e2016-07-20 20:41:58 -07005347 resultSpan = new TextAppearanceSpan(
5348 originalSpan.getFamily(),
5349 originalSpan.getTextStyle(),
5350 originalSpan.getTextSize(),
5351 textColor,
5352 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005353 }
5354 } else if (resultSpan instanceof ForegroundColorSpan) {
5355 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5356 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005357 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005358 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005359 if (fullLength) {
5360 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005361 resultSpan = null;
5362 } else {
5363 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005364 }
5365 } else {
5366 resultSpan = span;
5367 }
Selim Cinek396caca2018-04-10 17:46:46 -07005368 if (resultSpan != null) {
5369 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5370 }
Selim Cinek981962e2016-07-20 20:41:58 -07005371 }
5372 return builder;
5373 }
5374 return charSequence;
5375 }
5376
5377 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005378 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005379 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005380 */
5381 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005382 if (!mIsLegacyInitialized) {
5383 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5384 < Build.VERSION_CODES.LOLLIPOP;
5385 mIsLegacyInitialized = true;
5386 }
5387 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005388 }
5389
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005390 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005391 return processLegacyText(charSequence, false /* ambient */);
5392 }
5393
5394 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5395 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5396 boolean wantLightText = ambient;
5397 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005398 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005399 } else {
5400 return charSequence;
5401 }
5402 }
5403
Dan Sandler26e81cf2014-05-06 10:01:27 -04005404 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005405 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005406 */
Adrian Roos487374f2017-01-11 15:48:14 -08005407 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5408 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005409 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005410 int color;
5411 if (ambient) {
5412 color = resolveAmbientColor();
5413 } else if (isColorized()) {
5414 color = getPrimaryTextColor();
5415 } else {
5416 color = resolveContrastColor();
5417 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005418 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005419 contentView.setDrawableTint(R.id.icon, false, color,
5420 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005421
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005422 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005423 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005424 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005425 }
5426
Dan Sandler26e81cf2014-05-06 10:01:27 -04005427 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005428 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005429 * if it's grayscale).
5430 */
5431 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005432 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5433 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005434 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005435 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005436 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5437 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005438 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005439 }
5440
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005441 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005442 if (mN.color != COLOR_DEFAULT) {
5443 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005444 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005445 }
5446
Adrian Roos4ff3b122016-02-01 12:26:13 -08005447 int resolveContrastColor() {
5448 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5449 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005450 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005451
Selim Cinekac5f0272017-05-02 16:05:41 -07005452 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005453 int background = mContext.getColor(
5454 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005455 if (mN.color == COLOR_DEFAULT) {
5456 ensureColors();
Lucas Dupina291d192018-06-07 13:59:42 -07005457 color = ContrastColorUtil.resolveDefaultColor(mContext, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005458 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005459 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005460 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005461 }
5462 if (Color.alpha(color) < 255) {
5463 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005464 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005465 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005466 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005467 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005468 }
5469
Selim Cinek4717d862018-04-19 09:19:15 +08005470 int resolveNeutralColor() {
5471 if (mNeutralColor != COLOR_INVALID) {
5472 return mNeutralColor;
5473 }
5474 int background = mContext.getColor(
5475 com.android.internal.R.color.notification_material_background_color);
Lucas Dupina291d192018-06-07 13:59:42 -07005476 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005477 if (Color.alpha(mNeutralColor) < 255) {
5478 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005479 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005480 }
5481 return mNeutralColor;
5482 }
5483
Adrian Roos487374f2017-01-11 15:48:14 -08005484 int resolveAmbientColor() {
5485 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5486 return mCachedAmbientColor;
5487 }
Lucas Dupina291d192018-06-07 13:59:42 -07005488 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005489
5490 mCachedAmbientColorIsFor = mN.color;
5491 return mCachedAmbientColor = contrasted;
5492 }
5493
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005494 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005495 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005496 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005497 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005498 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005499 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005500 mN.actions = new Action[mActions.size()];
5501 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005502 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005503 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005504 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005505 }
Selim Cinek247fa012016-02-18 09:50:48 -08005506 if (mN.bigContentView != null || mN.contentView != null
5507 || mN.headsUpContentView != null) {
5508 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5509 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005510 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005511 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005512
Julia Reynolds3b848122016-02-26 10:45:32 -05005513 /**
5514 * Creates a Builder from an existing notification so further changes can be made.
5515 * @param context The context for your application / activity.
5516 * @param n The notification to create a Builder from.
5517 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005518 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005519 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005520 ApplicationInfo applicationInfo = n.extras.getParcelable(
5521 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005522 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005523 if (applicationInfo != null) {
5524 try {
5525 builderContext = context.createApplicationContext(applicationInfo,
5526 Context.CONTEXT_RESTRICTED);
5527 } catch (NameNotFoundException e) {
5528 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5529 builderContext = context; // try with our context
5530 }
5531 } else {
5532 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005533 }
5534
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005535 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005536 }
5537
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005538 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005539 * @deprecated Use {@link #build()} instead.
5540 */
5541 @Deprecated
5542 public Notification getNotification() {
5543 return build();
5544 }
5545
5546 /**
5547 * Combine all of the options that have been set and return a new {@link Notification}
5548 * object.
5549 */
5550 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005551 // first, add any extras from the calling code
5552 if (mUserExtras != null) {
5553 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005554 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005555
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005556 mN.creationTime = System.currentTimeMillis();
5557
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005558 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005559 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005560
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005561 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005562
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005563 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005564 mStyle.reduceImageSizes(mContext);
5565 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005566 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005567 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005568 }
Selim Cinekd0426622017-07-11 13:19:59 +02005569 mN.reduceImageSizes(mContext);
5570
Adrian Roos5081c0d2016-02-26 16:04:19 -08005571 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005572 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005573 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005574 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005575 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5576 mN.contentView.getSequenceNumber());
5577 }
5578 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005579 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005580 if (mN.bigContentView != null) {
5581 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5582 mN.bigContentView.getSequenceNumber());
5583 }
5584 }
5585 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005586 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005587 if (mN.headsUpContentView != null) {
5588 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5589 mN.headsUpContentView.getSequenceNumber());
5590 }
5591 }
5592 }
5593
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005594 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5595 mN.flags |= FLAG_SHOW_LIGHTS;
5596 }
5597
Adrian Roosfb921842017-10-26 14:49:56 +02005598 mN.allPendingIntents = null;
5599
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005600 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005601 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005602
5603 /**
5604 * Apply this Builder to an existing {@link Notification} object.
5605 *
5606 * @hide
5607 */
5608 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005609 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005610 return n;
5611 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005612
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005613 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005614 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005615 * change. Also removes extenders on low ram devices, as
5616 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005617 *
5618 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5619 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005620 * @hide
5621 */
Kristian Monsen30f59b22018-04-09 10:27:16 +02005622 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5623 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005624 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005625
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005626 // Only strip views for known Styles because we won't know how to
5627 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005628 if (!isLowRam
5629 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005630 && getNotificationStyleClass(templateClass) == null) {
5631 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005632 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005633
5634 // Only strip unmodified BuilderRemoteViews.
5635 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005636 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005637 n.contentView.getSequenceNumber();
5638 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005639 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005640 n.bigContentView.getSequenceNumber();
5641 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005642 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005643 n.headsUpContentView.getSequenceNumber();
5644
5645 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005646 if (!isLowRam
5647 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005648 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005649 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005650
5651 Notification clone = n.clone();
5652 if (stripContentView) {
5653 clone.contentView = null;
5654 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5655 }
5656 if (stripBigContentView) {
5657 clone.bigContentView = null;
5658 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5659 }
5660 if (stripHeadsUpContentView) {
5661 clone.headsUpContentView = null;
5662 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5663 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005664 if (isLowRam) {
Kristian Monsen30f59b22018-04-09 10:27:16 +02005665 String[] allowedServices = context.getResources().getStringArray(
5666 R.array.config_allowedManagedServicesOnLowRamDevices);
5667 if (allowedServices.length == 0) {
5668 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5669 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5670 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5671 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005672 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005673 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005674 }
5675
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005676 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005677 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005678 }
5679
5680 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005681 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005682 }
5683
5684 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005685 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005686 }
5687
5688 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005689 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005690 }
5691
5692 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005693 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005694 }
5695
Adrian Roosc1a80b02016-04-05 14:54:55 -07005696 private int getMessagingLayoutResource() {
5697 return R.layout.notification_template_material_messaging;
5698 }
5699
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005700 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005701 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005702 }
5703
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005704 private int getEmphasizedActionLayoutResource() {
5705 return R.layout.notification_material_action_emphasized;
5706 }
5707
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005708 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005709 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005710 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005711
5712 private int getBackgroundColor() {
5713 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005714 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005715 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005716 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005717 }
5718 }
5719
Selim Cinek396caca2018-04-10 17:46:46 -07005720 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005721 * Gets a neutral color that can be used for icons or similar that should not stand out.
5722 */
5723 private int getNeutralColor() {
5724 if (isColorized()) {
5725 return getSecondaryTextColor();
5726 } else {
5727 return resolveNeutralColor();
5728 }
5729 }
5730
5731 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005732 * Same as getBackgroundColor but also resolved the default color to the background.
5733 */
5734 private int resolveBackgroundColor() {
5735 int backgroundColor = getBackgroundColor();
5736 if (backgroundColor == COLOR_DEFAULT) {
5737 backgroundColor = mContext.getColor(
5738 com.android.internal.R.color.notification_material_background_color);
5739 }
5740 return backgroundColor;
5741 }
5742
Selim Cinek7b9605b2017-01-19 17:36:00 -08005743 private boolean isColorized() {
5744 return mN.isColorized();
5745 }
Selim Cinek99104832017-01-25 14:47:33 -08005746
Anthony Chenad4d1582017-04-10 16:07:58 -07005747 private boolean shouldTintActionButtons() {
5748 return mTintActionButtons;
5749 }
5750
Selim Cinek99104832017-01-25 14:47:33 -08005751 private boolean textColorsNeedInversion() {
5752 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5753 return false;
5754 }
5755 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5756 return targetSdkVersion > Build.VERSION_CODES.M
5757 && targetSdkVersion < Build.VERSION_CODES.O;
5758 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005759
5760 /**
5761 * Set a color palette to be used as the background and textColors
5762 *
5763 * @param backgroundColor the color to be used as the background
5764 * @param foregroundColor the color to be used as the foreground
5765 *
5766 * @hide
5767 */
5768 public void setColorPalette(int backgroundColor, int foregroundColor) {
5769 mBackgroundColor = backgroundColor;
5770 mForegroundColor = foregroundColor;
5771 mTextColorsAreForBackground = COLOR_INVALID;
5772 ensureColors();
5773 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005774
5775 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005776 * Forces all styled remoteViews to be built from scratch and not use any cached
5777 * RemoteViews.
5778 * This is needed for legacy apps that are baking in their remoteviews into the
5779 * notification.
5780 *
5781 * @hide
5782 */
5783 public void setRebuildStyledRemoteViews(boolean rebuild) {
5784 mRebuildStyledRemoteViews = rebuild;
5785 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005786
5787 /**
5788 * Get the text that should be displayed in the statusBar when heads upped. This is
5789 * usually just the app name, but may be different depending on the style.
5790 *
5791 * @param publicMode If true, return a text that is safe to display in public.
5792 *
5793 * @hide
5794 */
5795 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5796 if (mStyle != null && !publicMode) {
5797 CharSequence text = mStyle.getHeadsUpStatusBarText();
5798 if (!TextUtils.isEmpty(text)) {
5799 return text;
5800 }
5801 }
5802 return loadHeaderAppName();
5803 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005804 }
5805
5806 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005807 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5808 * remote views.
5809 *
5810 * @hide
5811 */
5812 void reduceImageSizes(Context context) {
5813 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5814 return;
5815 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005816 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005817 if (mLargeIcon != null || largeIcon != null) {
5818 Resources resources = context.getResources();
5819 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005820 int maxWidth = resources.getDimensionPixelSize(isLowRam
5821 ? R.dimen.notification_right_icon_size_low_ram
5822 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005823 int maxHeight = maxWidth;
5824 if (MediaStyle.class.equals(style)
5825 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005826 maxHeight = resources.getDimensionPixelSize(isLowRam
5827 ? R.dimen.notification_media_image_max_height_low_ram
5828 : R.dimen.notification_media_image_max_height);
5829 maxWidth = resources.getDimensionPixelSize(isLowRam
5830 ? R.dimen.notification_media_image_max_width_low_ram
5831 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005832 }
5833 if (mLargeIcon != null) {
5834 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5835 }
5836 if (largeIcon != null) {
5837 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5838 }
5839 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005840 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5841 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5842 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005843 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5844 }
5845
Selim Cineka8cb1262017-08-15 16:53:44 -07005846 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5847 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005848 if (remoteView != null) {
5849 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005850 int maxWidth = resources.getDimensionPixelSize(isLowRam
5851 ? R.dimen.notification_custom_view_max_image_width_low_ram
5852 : R.dimen.notification_custom_view_max_image_width);
5853 int maxHeight = resources.getDimensionPixelSize(isLowRam
5854 ? R.dimen.notification_custom_view_max_image_height_low_ram
5855 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005856 remoteView.reduceImageSizes(maxWidth, maxHeight);
5857 }
5858 }
5859
5860 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005861 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005862 */
Selim Cinek22714f12017-04-13 16:23:53 -07005863 private boolean isForegroundService() {
5864 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005865 }
5866
5867 /**
Selim Cinek99104832017-01-25 14:47:33 -08005868 * @return whether this notification has a media session attached
5869 * @hide
5870 */
5871 public boolean hasMediaSession() {
5872 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5873 }
5874
5875 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005876 * @return the style class of this notification
5877 * @hide
5878 */
5879 public Class<? extends Notification.Style> getNotificationStyle() {
5880 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5881
5882 if (!TextUtils.isEmpty(templateClass)) {
5883 return Notification.getNotificationStyleClass(templateClass);
5884 }
5885 return null;
5886 }
5887
5888 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005889 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005890 *
5891 * @hide
5892 */
5893 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005894 if (isColorizedMedia()) {
5895 return true;
5896 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005897 return extras.getBoolean(EXTRA_COLORIZED)
5898 && (hasColorizedPermission() || isForegroundService());
5899 }
5900
5901 /**
5902 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5903 * permission. The permission is checked when a notification is enqueued.
5904 */
5905 private boolean hasColorizedPermission() {
5906 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005907 }
5908
5909 /**
5910 * @return true if this notification is colorized and it is a media notification
5911 *
5912 * @hide
5913 */
5914 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005915 Class<? extends Style> style = getNotificationStyle();
5916 if (MediaStyle.class.equals(style)) {
5917 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5918 if ((colorized == null || colorized) && hasMediaSession()) {
5919 return true;
5920 }
5921 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5922 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5923 return true;
5924 }
5925 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005926 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005927 }
5928
Selim Cinek0847acd2017-04-24 19:48:29 -07005929
5930 /**
5931 * @return true if this is a media notification
5932 *
5933 * @hide
5934 */
5935 public boolean isMediaNotification() {
5936 Class<? extends Style> style = getNotificationStyle();
5937 if (MediaStyle.class.equals(style)) {
5938 return true;
5939 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5940 return true;
5941 }
5942 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005943 }
5944
Selim Cinek279fa862016-06-14 10:57:25 -07005945 private boolean hasLargeIcon() {
5946 return mLargeIcon != null || largeIcon != null;
5947 }
5948
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005949 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005950 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005951 * @hide
5952 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005953 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005954 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5955 }
5956
5957 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005958 * @return true if the notification will show a chronometer; false otherwise
5959 * @hide
5960 */
5961 public boolean showsChronometer() {
5962 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5963 }
5964
5965 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005966 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005967 */
5968 @SystemApi
5969 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5970 Class<? extends Style>[] classes = new Class[] {
5971 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5972 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5973 MessagingStyle.class };
5974 for (Class<? extends Style> innerClass : classes) {
5975 if (templateClass.equals(innerClass.getName())) {
5976 return innerClass;
5977 }
5978 }
5979 return null;
5980 }
5981
5982 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005983 * An object that can apply a rich notification style to a {@link Notification.Builder}
5984 * object.
5985 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005986 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07005987
5988 /**
5989 * The number of items allowed simulatanously in the remote input history.
5990 * @hide
5991 */
5992 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04005993 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005994
5995 /**
5996 * @hide
5997 */
5998 protected CharSequence mSummaryText = null;
5999
6000 /**
6001 * @hide
6002 */
6003 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006004
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006005 protected Builder mBuilder;
6006
Chris Wrend6297db2012-05-03 16:20:13 -04006007 /**
6008 * Overrides ContentTitle in the big form of the template.
6009 * This defaults to the value passed to setContentTitle().
6010 */
6011 protected void internalSetBigContentTitle(CharSequence title) {
6012 mBigContentTitle = title;
6013 }
6014
6015 /**
6016 * Set the first line of text after the detail section in the big form of the template.
6017 */
6018 protected void internalSetSummaryText(CharSequence cs) {
6019 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006020 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006021 }
6022
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006023 public void setBuilder(Builder builder) {
6024 if (mBuilder != builder) {
6025 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006026 if (mBuilder != null) {
6027 mBuilder.setStyle(this);
6028 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006029 }
6030 }
6031
Chris Wrend6297db2012-05-03 16:20:13 -04006032 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006033 if (mBuilder == null) {
6034 throw new IllegalArgumentException("Style requires a valid Builder object");
6035 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006036 }
Chris Wrend6297db2012-05-03 16:20:13 -04006037
6038 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006039 return getStandardView(layoutId, null);
6040 }
6041
6042 /**
6043 * Get the standard view for this style.
6044 *
6045 * @param layoutId The layout id to use
6046 * @param result The result where template bind information is saved.
6047 * @return A remoteView for this style.
6048 * @hide
6049 */
6050 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006051 checkBuilder();
6052
Christoph Studer4600f9b2014-07-22 22:44:43 +02006053 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006054 CharSequence oldBuilderContentTitle =
6055 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006056 if (mBigContentTitle != null) {
6057 mBuilder.setContentTitle(mBigContentTitle);
6058 }
6059
Selim Cinek384804b2018-04-18 14:31:07 +08006060 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006061
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006062 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006063
Chris Wrend6297db2012-05-03 16:20:13 -04006064 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6065 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006066 } else {
6067 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006068 }
6069
Chris Wrend6297db2012-05-03 16:20:13 -04006070 return contentView;
6071 }
6072
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006073 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006074 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006075 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006076 *
6077 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006078 * @hide
6079 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006080 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006081 return null;
6082 }
6083
6084 /**
6085 * Construct a Style-specific RemoteViews for the final big notification layout.
6086 * @hide
6087 */
6088 public RemoteViews makeBigContentView() {
6089 return null;
6090 }
6091
6092 /**
6093 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006094 *
6095 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006096 * @hide
6097 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006098 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006099 return null;
6100 }
6101
6102 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006103 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006104 * @hide
6105 */
6106 public void addExtras(Bundle extras) {
6107 if (mSummaryTextSet) {
6108 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6109 }
6110 if (mBigContentTitle != null) {
6111 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6112 }
Chris Wren91ad5632013-06-05 15:05:57 -04006113 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006114 }
6115
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006116 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006117 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006118 * @hide
6119 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006120 protected void restoreFromExtras(Bundle extras) {
6121 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6122 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6123 mSummaryTextSet = true;
6124 }
6125 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6126 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6127 }
6128 }
6129
6130
6131 /**
6132 * @hide
6133 */
6134 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006135 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006136 return wip;
6137 }
6138
Daniel Sandler0ec46202015-06-24 01:27:05 -04006139 /**
6140 * @hide
6141 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006142 public void purgeResources() {}
6143
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006144 /**
6145 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6146 * attached to.
6147 *
6148 * @return the fully constructed Notification.
6149 */
6150 public Notification build() {
6151 checkBuilder();
6152 return mBuilder.build();
6153 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006154
6155 /**
6156 * @hide
6157 * @return true if the style positions the progress bar on the second line; false if the
6158 * style hides the progress bar
6159 */
6160 protected boolean hasProgress() {
6161 return true;
6162 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006163
6164 /**
6165 * @hide
6166 * @return Whether we should put the summary be put into the notification header
6167 */
6168 public boolean hasSummaryInHeader() {
6169 return true;
6170 }
Selim Cinek593610c2016-02-16 18:42:57 -08006171
6172 /**
6173 * @hide
6174 * @return Whether custom content views are displayed inline in the style
6175 */
6176 public boolean displayCustomViewInline() {
6177 return false;
6178 }
Selim Cinekd0426622017-07-11 13:19:59 +02006179
6180 /**
6181 * Reduces the image sizes contained in this style.
6182 *
6183 * @hide
6184 */
6185 public void reduceImageSizes(Context context) {
6186 }
Selim Cinek90343862018-02-01 11:07:11 -08006187
6188 /**
6189 * Validate that this style was properly composed. This is called at build time.
6190 * @hide
6191 */
6192 public void validate(Context context) {
6193 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006194
6195 /**
6196 * @hide
6197 */
6198 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006199
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006200 /**
6201 * @return the the text that should be displayed in the statusBar when heads-upped.
6202 * If {@code null} is returned, the default implementation will be used.
6203 *
6204 * @hide
6205 */
6206 public CharSequence getHeadsUpStatusBarText() {
6207 return null;
6208 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006209 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006210
6211 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006212 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006213 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006214 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006215 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006216 * Notification notif = new Notification.Builder(mContext)
6217 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6218 * .setContentText(subject)
6219 * .setSmallIcon(R.drawable.new_post)
6220 * .setLargeIcon(aBitmap)
6221 * .setStyle(new Notification.BigPictureStyle()
6222 * .bigPicture(aBigBitmap))
6223 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006224 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006225 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006226 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006227 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006228 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006229 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006230 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006231 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006232
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006233 public BigPictureStyle() {
6234 }
6235
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006236 /**
6237 * @deprecated use {@code BigPictureStyle()}.
6238 */
6239 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006240 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006241 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006242 }
6243
Chris Wrend6297db2012-05-03 16:20:13 -04006244 /**
6245 * Overrides ContentTitle in the big form of the template.
6246 * This defaults to the value passed to setContentTitle().
6247 */
6248 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006249 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006250 return this;
6251 }
6252
6253 /**
6254 * Set the first line of text after the detail section in the big form of the template.
6255 */
6256 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006257 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006258 return this;
6259 }
6260
Chris Wren0bd664d2012-08-01 13:56:56 -04006261 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006262 * @hide
6263 */
6264 public Bitmap getBigPicture() {
6265 return mPicture;
6266 }
6267
6268 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006269 * Provide the bitmap to be used as the payload for the BigPicture notification.
6270 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006271 public BigPictureStyle bigPicture(Bitmap b) {
6272 mPicture = b;
6273 return this;
6274 }
6275
Chris Wren3745a3d2012-05-22 15:11:52 -04006276 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006277 * Override the large icon when the big notification is shown.
6278 */
6279 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006280 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6281 }
6282
6283 /**
6284 * Override the large icon when the big notification is shown.
6285 */
6286 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006287 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006288 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006289 return this;
6290 }
6291
Riley Andrews0394a0c2015-11-03 23:36:52 -08006292 /** @hide */
6293 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6294
Daniel Sandler0ec46202015-06-24 01:27:05 -04006295 /**
6296 * @hide
6297 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006298 @Override
6299 public void purgeResources() {
6300 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006301 if (mPicture != null &&
6302 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006303 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006304 mPicture = mPicture.createAshmemBitmap();
6305 }
6306 if (mBigLargeIcon != null) {
6307 mBigLargeIcon.convertToAshmem();
6308 }
6309 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006310
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006311 /**
6312 * @hide
6313 */
Selim Cinekd0426622017-07-11 13:19:59 +02006314 @Override
6315 public void reduceImageSizes(Context context) {
6316 super.reduceImageSizes(context);
6317 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006318 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006319 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006320 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6321 ? R.dimen.notification_big_picture_max_height_low_ram
6322 : R.dimen.notification_big_picture_max_height);
6323 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6324 ? R.dimen.notification_big_picture_max_width_low_ram
6325 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006326 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6327 }
6328 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006329 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6330 ? R.dimen.notification_right_icon_size_low_ram
6331 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006332 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6333 }
6334 }
6335
6336 /**
6337 * @hide
6338 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006339 public RemoteViews makeBigContentView() {
6340 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006341 // This covers the following cases:
6342 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006343 // mN.mLargeIcon
6344 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006345 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006346 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006347 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006348 oldLargeIcon = mBuilder.mN.mLargeIcon;
6349 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006350 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6351 // replacement if the other one is null. Because we're restoring these legacy icons
6352 // for old listeners, this is in general non-null.
6353 largeIconLegacy = mBuilder.mN.largeIcon;
6354 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006355 }
6356
Selim Cinek384804b2018-04-18 14:31:07 +08006357 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6358 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006359 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006360 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6361 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006362 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006363 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006364 }
Selim Cinek279fa862016-06-14 10:57:25 -07006365 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006366
Christoph Studer5c510ee2014-12-15 16:32:27 +01006367 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006368 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006369 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006370 }
6371
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006372 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006373 return contentView;
6374 }
6375
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006376 /**
6377 * @hide
6378 */
6379 public void addExtras(Bundle extras) {
6380 super.addExtras(extras);
6381
6382 if (mBigLargeIconSet) {
6383 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6384 }
6385 extras.putParcelable(EXTRA_PICTURE, mPicture);
6386 }
6387
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006388 /**
6389 * @hide
6390 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006391 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006392 protected void restoreFromExtras(Bundle extras) {
6393 super.restoreFromExtras(extras);
6394
6395 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006396 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006397 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006398 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006399 mPicture = extras.getParcelable(EXTRA_PICTURE);
6400 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006401
6402 /**
6403 * @hide
6404 */
6405 @Override
6406 public boolean hasSummaryInHeader() {
6407 return false;
6408 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006409
6410 /**
6411 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006412 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6413 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006414 */
6415 @Override
6416 public boolean areNotificationsVisiblyDifferent(Style other) {
6417 if (other == null || getClass() != other.getClass()) {
6418 return true;
6419 }
6420 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006421 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6422 }
6423
6424 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6425 if (a == b) {
6426 return false;
6427 }
6428 if (a == null || b == null) {
6429 return true;
6430 }
6431 return a.getWidth() != b.getWidth()
6432 || a.getHeight() != b.getHeight()
6433 || a.getConfig() != b.getConfig()
6434 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006435 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006436 }
6437
6438 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006439 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006440 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006441 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006442 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006443 * Notification notif = new Notification.Builder(mContext)
6444 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6445 * .setContentText(subject)
6446 * .setSmallIcon(R.drawable.new_mail)
6447 * .setLargeIcon(aBitmap)
6448 * .setStyle(new Notification.BigTextStyle()
6449 * .bigText(aVeryLongString))
6450 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006451 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006452 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006453 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006454 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006455 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006456
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006457 private CharSequence mBigText;
6458
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006459 public BigTextStyle() {
6460 }
6461
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006462 /**
6463 * @deprecated use {@code BigTextStyle()}.
6464 */
6465 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006466 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006467 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006468 }
6469
Chris Wrend6297db2012-05-03 16:20:13 -04006470 /**
6471 * Overrides ContentTitle in the big form of the template.
6472 * This defaults to the value passed to setContentTitle().
6473 */
6474 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006475 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006476 return this;
6477 }
6478
6479 /**
6480 * Set the first line of text after the detail section in the big form of the template.
6481 */
6482 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006483 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006484 return this;
6485 }
6486
Chris Wren0bd664d2012-08-01 13:56:56 -04006487 /**
6488 * Provide the longer text to be displayed in the big form of the
6489 * template in place of the content text.
6490 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006491 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006492 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006493 return this;
6494 }
6495
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006496 /**
6497 * @hide
6498 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006499 public CharSequence getBigText() {
6500 return mBigText;
6501 }
6502
6503 /**
6504 * @hide
6505 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006506 public void addExtras(Bundle extras) {
6507 super.addExtras(extras);
6508
Christoph Studer4600f9b2014-07-22 22:44:43 +02006509 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6510 }
6511
6512 /**
6513 * @hide
6514 */
6515 @Override
6516 protected void restoreFromExtras(Bundle extras) {
6517 super.restoreFromExtras(extras);
6518
6519 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006520 }
6521
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006522 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006523 * @param increasedHeight true if this layout be created with an increased height.
6524 *
6525 * @hide
6526 */
6527 @Override
6528 public RemoteViews makeContentView(boolean increasedHeight) {
6529 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006530 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006531 mBuilder.mActions = new ArrayList<>();
6532 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006533 mBuilder.mActions = mBuilder.mOriginalActions;
6534 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006535 return remoteViews;
6536 }
6537 return super.makeContentView(increasedHeight);
6538 }
6539
6540 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006541 * @hide
6542 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006543 @Override
6544 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6545 if (increasedHeight && mBuilder.mActions.size() > 0) {
6546 return makeBigContentView();
6547 }
6548 return super.makeHeadsUpContentView(increasedHeight);
6549 }
6550
6551 /**
6552 * @hide
6553 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006554 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006555
6556 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006557 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006558 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006559
Selim Cinek384804b2018-04-18 14:31:07 +08006560 TemplateBindResult result = new TemplateBindResult();
6561 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006562 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006563
Selim Cinek75998782016-04-26 10:39:17 -07006564 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006565
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006566 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006567 if (TextUtils.isEmpty(bigTextText)) {
6568 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6569 // experience
6570 bigTextText = mBuilder.processLegacyText(text);
6571 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006572 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006573
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006574 return contentView;
6575 }
6576
Julia Reynolds7217dc92018-03-07 12:12:09 -05006577 /**
6578 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006579 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006580 */
6581 @Override
6582 public boolean areNotificationsVisiblyDifferent(Style other) {
6583 if (other == null || getClass() != other.getClass()) {
6584 return true;
6585 }
6586 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006587 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006588 }
6589
Adrian Roosb1f427c2016-05-26 12:27:15 -07006590 static void applyBigTextContentView(Builder builder,
6591 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006592 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006593 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006594 contentView.setViewVisibility(R.id.big_text,
6595 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006596 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006597 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006598 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006599
6600 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006601 * Helper class for generating large-format notifications that include multiple back-and-forth
6602 * messages of varying types between any number of people.
6603 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006604 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006605 * If the platform does not provide large-format notifications, this method has no effect. The
6606 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006607 *
6608 * <p>
6609 * If the app is targeting Android P and above, it is required to use the {@link Person}
6610 * class in order to get an optimal rendering of the notification and its avatars. For
6611 * conversations involving multiple people, the app should also make sure that it marks the
6612 * conversation as a group with {@link #setGroupConversation(boolean)}.
6613 *
6614 * <p>
6615 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6616 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006617 * <pre class="prettyprint">
6618 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006619 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6620 * MessagingStyle style = new MessagingStyle(user)
6621 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6622 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6623 * .setGroupConversation(hasMultiplePeople());
6624 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006625 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006626 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006627 * .setContentText(subject)
6628 * .setSmallIcon(R.drawable.new_message)
6629 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006630 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006631 * .build();
6632 * </pre>
6633 */
6634 public static class MessagingStyle extends Style {
6635
6636 /**
6637 * The maximum number of messages that will be retained in the Notification itself (the
6638 * number displayed is up to the platform).
6639 */
6640 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6641
Selim Cinekcb8b9852017-12-15 18:01:52 -08006642 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006643 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006644 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006645 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006646 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006647
6648 MessagingStyle() {
6649 }
6650
6651 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006652 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6653 * user before the posting app reposts the notification with those messages after they've
6654 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006655 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006656 *
6657 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006658 */
Alex Hillsfd590442016-10-07 09:52:44 -04006659 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006660 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006661 }
6662
6663 /**
6664 * @param user Required - The person displayed for any messages that are sent by the
6665 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6666 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006667 * by this user. The user also needs to have a valid name associated with it, which will
6668 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006669 */
6670 public MessagingStyle(@NonNull Person user) {
6671 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006672 }
6673
6674 /**
6675 * Validate that this style was properly composed. This is called at build time.
6676 * @hide
6677 */
6678 @Override
6679 public void validate(Context context) {
6680 super.validate(context);
6681 if (context.getApplicationInfo().targetSdkVersion
6682 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6683 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006684 }
6685 }
6686
6687 /**
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006688 * @return the the text that should be displayed in the statusBar when heads upped.
6689 * If {@code null} is returned, the default implementation will be used.
6690 *
6691 * @hide
6692 */
6693 @Override
6694 public CharSequence getHeadsUpStatusBarText() {
6695 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6696 ? super.mBigContentTitle
6697 : mConversationTitle;
6698 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6699 return conversationTitle;
6700 }
6701 return null;
6702 }
6703
6704 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006705 * @return the user to be displayed for any replies sent by the user
6706 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006707 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006708 public Person getUser() {
6709 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006710 }
6711
6712 /**
6713 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006714 *
6715 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006716 */
6717 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006718 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006719 }
6720
6721 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006722 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6723 *
Kodlee Yin14656422017-12-22 17:00:46 -08006724 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6725 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6726 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6727 * {@code true} and passing {@code null} will make it return {@code false}. In
6728 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6729 * to set group conversation status.
6730 *
6731 * @param conversationTitle Title displayed for this conversation
6732 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006733 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006734 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 mConversationTitle = conversationTitle;
6736 return this;
6737 }
6738
6739 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006740 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006741 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006742 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006743 public CharSequence getConversationTitle() {
6744 return mConversationTitle;
6745 }
6746
6747 /**
6748 * Adds a message for display by this notification. Convenience call for a simple
6749 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6750 * @param text A {@link CharSequence} to be displayed as the message content
6751 * @param timestamp Time at which the message arrived
6752 * @param sender A {@link CharSequence} to be used for displaying the name of the
6753 * sender. Should be <code>null</code> for messages by the current user, in which case
6754 * the platform will insert {@link #getUserDisplayName()}.
6755 * Should be unique amongst all individuals in the conversation, and should be
6756 * consistent during re-posts of the notification.
6757 *
6758 * @see Message#Message(CharSequence, long, CharSequence)
6759 *
6760 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006761 *
6762 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006763 */
6764 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006765 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006766 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006767 }
6768
6769 /**
6770 * Adds a message for display by this notification. Convenience call for a simple
6771 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6772 * @param text A {@link CharSequence} to be displayed as the message content
6773 * @param timestamp Time at which the message arrived
6774 * @param sender The {@link Person} who sent the message.
6775 * Should be <code>null</code> for messages by the current user, in which case
6776 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6777 *
6778 * @see Message#Message(CharSequence, long, CharSequence)
6779 *
6780 * @return this object for method chaining
6781 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006782 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6783 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006784 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006785 }
6786
6787 /**
6788 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006789 *
6790 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6791 * the newest last.
6792 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006793 * @param message The {@link Message} to be displayed
6794 * @return this object for method chaining
6795 */
6796 public MessagingStyle addMessage(Message message) {
6797 mMessages.add(message);
6798 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6799 mMessages.remove(0);
6800 }
6801 return this;
6802 }
6803
6804 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006805 * Adds a {@link Message} for historic context in this notification.
6806 *
6807 * <p>Messages should be added as historic if they are not the main subject of the
6808 * notification but may give context to a conversation. The system may choose to present
6809 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6810 *
6811 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6812 * the newest last.
6813 *
6814 * @param message The historic {@link Message} to be added
6815 * @return this object for method chaining
6816 */
6817 public MessagingStyle addHistoricMessage(Message message) {
6818 mHistoricMessages.add(message);
6819 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6820 mHistoricMessages.remove(0);
6821 }
6822 return this;
6823 }
6824
6825 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006826 * Gets the list of {@code Message} objects that represent the notification
6827 */
6828 public List<Message> getMessages() {
6829 return mMessages;
6830 }
6831
6832 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006833 * Gets the list of historic {@code Message}s in the notification.
6834 */
6835 public List<Message> getHistoricMessages() {
6836 return mHistoricMessages;
6837 }
6838
6839 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006840 * Sets whether this conversation notification represents a group. If the app is targeting
6841 * Android P, this is required if the app wants to display the largeIcon set with
6842 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006843 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006844 * @param isGroupConversation {@code true} if the conversation represents a group,
6845 * {@code false} otherwise.
6846 * @return this object for method chaining
6847 */
6848 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6849 mIsGroupConversation = isGroupConversation;
6850 return this;
6851 }
6852
6853 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006854 * Returns {@code true} if this notification represents a group conversation, otherwise
6855 * {@code false}.
6856 *
6857 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6858 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6859 * not the conversation title is set; returning {@code true} if the conversation title is
6860 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6861 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6862 * named, non-group conversations.
6863 *
6864 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006865 */
6866 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006867 // When target SDK version is < P, a non-null conversation title dictates if this is
6868 // as group conversation.
6869 if (mBuilder != null
6870 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6871 < Build.VERSION_CODES.P) {
6872 return mConversationTitle != null;
6873 }
6874
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006875 return mIsGroupConversation;
6876 }
6877
6878 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006879 * @hide
6880 */
6881 @Override
6882 public void addExtras(Bundle extras) {
6883 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006884 if (mUser != null) {
6885 // For legacy usages
6886 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6887 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006888 }
6889 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006890 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006891 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006892 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6893 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006894 }
Adrian Roos437cd562017-01-18 15:47:03 -08006895 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6896 Message.getBundleArrayForMessages(mHistoricMessages));
6897 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006898
6899 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006900 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006901 }
6902
6903 private void fixTitleAndTextExtras(Bundle extras) {
6904 Message m = findLatestIncomingMessage();
6905 CharSequence text = (m == null) ? null : m.mText;
6906 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006907 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6908 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006909 CharSequence title;
6910 if (!TextUtils.isEmpty(mConversationTitle)) {
6911 if (!TextUtils.isEmpty(sender)) {
6912 BidiFormatter bidi = BidiFormatter.getInstance();
6913 title = mBuilder.mContext.getString(
6914 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006915 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006916 } else {
6917 title = mConversationTitle;
6918 }
6919 } else {
6920 title = sender;
6921 }
6922
6923 if (title != null) {
6924 extras.putCharSequence(EXTRA_TITLE, title);
6925 }
6926 if (text != null) {
6927 extras.putCharSequence(EXTRA_TEXT, text);
6928 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006929 }
6930
6931 /**
6932 * @hide
6933 */
6934 @Override
6935 protected void restoreFromExtras(Bundle extras) {
6936 super.restoreFromExtras(extras);
6937
Selim Cinekcb8b9852017-12-15 18:01:52 -08006938 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6939 if (mUser == null) {
6940 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07006941 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08006942 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006943 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006944 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006945 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006946 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006947 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006948 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006949 }
6950
6951 /**
6952 * @hide
6953 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006954 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006955 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006956 mBuilder.mOriginalActions = mBuilder.mActions;
6957 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006958 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006959 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006960 mBuilder.mActions = mBuilder.mOriginalActions;
6961 mBuilder.mOriginalActions = null;
6962 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006963 }
6964
Julia Reynolds7217dc92018-03-07 12:12:09 -05006965 /**
6966 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006967 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006968 */
6969 @Override
6970 public boolean areNotificationsVisiblyDifferent(Style other) {
6971 if (other == null || getClass() != other.getClass()) {
6972 return true;
6973 }
6974 MessagingStyle newS = (MessagingStyle) other;
6975 List<MessagingStyle.Message> oldMs = getMessages();
6976 List<MessagingStyle.Message> newMs = newS.getMessages();
6977
Dan Sandler7d67bd42018-05-15 14:06:38 -04006978 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006979 newMs = new ArrayList<>();
6980 }
6981
6982 int n = oldMs.size();
6983 if (n != newMs.size()) {
6984 return true;
6985 }
6986 for (int i = 0; i < n; i++) {
6987 MessagingStyle.Message oldM = oldMs.get(i);
6988 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04006989 if (!Objects.equals(
6990 String.valueOf(oldM.getText()),
6991 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006992 return true;
6993 }
6994 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
6995 return true;
6996 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04006997 String oldSender = String.valueOf(oldM.getSenderPerson() == null
6998 ? oldM.getSender()
6999 : oldM.getSenderPerson().getName());
7000 String newSender = String.valueOf(newM.getSenderPerson() == null
7001 ? newM.getSender()
7002 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007003 if (!Objects.equals(oldSender, newSender)) {
7004 return true;
7005 }
7006
7007 String oldKey = oldM.getSenderPerson() == null
7008 ? null : oldM.getSenderPerson().getKey();
7009 String newKey = newM.getSenderPerson() == null
7010 ? null : newM.getSenderPerson().getKey();
7011 if (!Objects.equals(oldKey, newKey)) {
7012 return true;
7013 }
7014 // Other fields (like timestamp) intentionally excluded
7015 }
7016 return false;
7017 }
7018
Adrian Roosc1a80b02016-04-05 14:54:55 -07007019 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007020 return findLatestIncomingMessage(mMessages);
7021 }
7022
7023 /**
7024 * @hide
7025 */
7026 @Nullable
7027 public static Message findLatestIncomingMessage(
7028 List<Message> messages) {
7029 for (int i = messages.size() - 1; i >= 0; i--) {
7030 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007031 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007032 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007033 return m;
7034 }
7035 }
Selim Cinek88188f22017-09-19 16:46:56 -07007036 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007037 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007038 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007039 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007040 return null;
7041 }
7042
7043 /**
7044 * @hide
7045 */
7046 @Override
7047 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007048 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007049 }
7050
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007051 /**
7052 * Create a messaging layout.
7053 *
7054 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7055 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007056 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007057 * @return the created remoteView.
7058 */
Selim Cinekafeed292017-12-12 17:32:44 -08007059 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007060 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007061 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007062 ? super.mBigContentTitle
7063 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007064 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7065 >= Build.VERSION_CODES.P;
7066 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007067 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007068 Icon avatarReplacement = null;
7069 if (!atLeastP) {
7070 isOneToOne = TextUtils.isEmpty(conversationTitle);
7071 avatarReplacement = mBuilder.mN.mLargeIcon;
7072 if (hasOnlyWhiteSpaceSenders()) {
7073 isOneToOne = true;
7074 nameReplacement = conversationTitle;
7075 conversationTitle = null;
7076 }
7077 } else {
7078 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007079 }
Selim Cinek384804b2018-04-18 14:31:07 +08007080 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007081 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007082 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007083 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007084 .hideLargeIcon(hideRightIcons || isOneToOne)
7085 .hideReplyIcon(hideRightIcons)
7086 .headerTextSecondary(conversationTitle),
7087 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007088 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007089 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007090 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7091 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007092 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007093 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7094 : mBuilder.resolveContrastColor());
7095 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7096 mBuilder.getPrimaryTextColor());
7097 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7098 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007099 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7100 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007101 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7102 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007103 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7104 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007105 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7106 isOneToOne);
7107 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7108 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007109 return contentView;
7110 }
7111
Selim Cinekf7409db2017-10-24 16:17:14 -07007112 private boolean hasOnlyWhiteSpaceSenders() {
7113 for (int i = 0; i < mMessages.size(); i++) {
7114 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007115 Person sender = m.getSenderPerson();
7116 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007117 return false;
7118 }
7119 }
7120 return true;
7121 }
7122
7123 private boolean isWhiteSpace(CharSequence sender) {
7124 if (TextUtils.isEmpty(sender)) {
7125 return true;
7126 }
7127 if (sender.toString().matches("^\\s*$")) {
7128 return true;
7129 }
7130 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7131 // For the presentation that we had.
7132 for (int i = 0; i < sender.length(); i++) {
7133 char c = sender.charAt(i);
7134 if (c != '\u200B') {
7135 return false;
7136 }
7137 }
7138 return true;
7139 }
7140
Selim Cinek88188f22017-09-19 16:46:56 -07007141 private CharSequence createConversationTitleFromMessages() {
7142 ArraySet<CharSequence> names = new ArraySet<>();
7143 for (int i = 0; i < mMessages.size(); i++) {
7144 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007145 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007146 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007147 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007148 }
7149 }
7150 SpannableStringBuilder title = new SpannableStringBuilder();
7151 int size = names.size();
7152 for (int i = 0; i < size; i++) {
7153 CharSequence name = names.valueAt(i);
7154 if (!TextUtils.isEmpty(title)) {
7155 title.append(", ");
7156 }
7157 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7158 }
7159 return title;
7160 }
7161
Adrian Roosdedd1df2016-04-26 16:38:47 -07007162 /**
7163 * @hide
7164 */
7165 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007166 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007167 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007168 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007169 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7170 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007171 }
7172
Adrian Roosc1a80b02016-04-05 14:54:55 -07007173 private static TextAppearanceSpan makeFontColorSpan(int color) {
7174 return new TextAppearanceSpan(null, 0, 0,
7175 ColorStateList.valueOf(color), null);
7176 }
7177
Alex Hillsd9b04d92016-04-11 16:38:16 -04007178 public static final class Message {
7179
7180 static final String KEY_TEXT = "text";
7181 static final String KEY_TIMESTAMP = "time";
7182 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007183 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007184 static final String KEY_DATA_MIME_TYPE = "type";
7185 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007186 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007187 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007188
7189 private final CharSequence mText;
7190 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007191 @Nullable
7192 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007193 /** True if this message was generated from the extra
7194 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7195 */
7196 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007197
Shane Brennan5a871862017-03-11 13:14:17 -08007198 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007199 private String mDataMimeType;
7200 private Uri mDataUri;
7201
7202 /**
7203 * Constructor
7204 * @param text A {@link CharSequence} to be displayed as the message content
7205 * @param timestamp Time at which the message arrived
7206 * @param sender A {@link CharSequence} to be used for displaying the name of the
7207 * sender. Should be <code>null</code> for messages by the current user, in which case
7208 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7209 * Should be unique amongst all individuals in the conversation, and should be
7210 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007211 *
7212 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007213 */
7214 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007215 this(text, timestamp, sender == null ? null
7216 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007217 }
7218
7219 /**
7220 * Constructor
7221 * @param text A {@link CharSequence} to be displayed as the message content
7222 * @param timestamp Time at which the message arrived
7223 * @param sender The {@link Person} who sent the message.
7224 * Should be <code>null</code> for messages by the current user, in which case
7225 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7226 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007227 * The person provided should contain an Icon, set with
7228 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7229 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7230 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7231 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007232 * </p>
7233 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007234 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007235 this(text, timestamp, sender, false /* remoteHistory */);
7236 }
7237
7238 /**
7239 * Constructor
7240 * @param text A {@link CharSequence} to be displayed as the message content
7241 * @param timestamp Time at which the message arrived
7242 * @param sender The {@link Person} who sent the message.
7243 * Should be <code>null</code> for messages by the current user, in which case
7244 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7245 * @param remoteInputHistory True if the messages was generated from the extra
7246 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7247 * <p>
7248 * The person provided should contain an Icon, set with
7249 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7250 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7251 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7252 * to differentiate between the different users.
7253 * </p>
7254 * @hide
7255 */
7256 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7257 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007258 mText = text;
7259 mTimestamp = timestamp;
7260 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007261 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007262 }
7263
7264 /**
7265 * Sets a binary blob of data and an associated MIME type for a message. In the case
7266 * where the platform doesn't support the MIME type, the original text provided in the
7267 * constructor will be used.
7268 * @param dataMimeType The MIME type of the content. See
7269 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7270 * types on Android and Android Wear.
7271 * @param dataUri The uri containing the content whose type is given by the MIME type.
7272 * <p class="note">
7273 * <ol>
7274 * <li>Notification Listeners including the System UI need permission to access the
7275 * data the Uri points to. The recommended ways to do this are:</li>
7276 * <li>Store the data in your own ContentProvider, making sure that other apps have
7277 * the correct permission to access your provider. The preferred mechanism for
7278 * providing access is to use per-URI permissions which are temporary and only
7279 * grant access to the receiving application. An easy way to create a
7280 * ContentProvider like this is to use the FileProvider helper class.</li>
7281 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7282 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7283 * also store non-media types (see MediaStore.Files for more info). Files can be
7284 * inserted into the MediaStore using scanFile() after which a content:// style
7285 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7286 * Note that once added to the system MediaStore the content is accessible to any
7287 * app on the device.</li>
7288 * </ol>
7289 * @return this object for method chaining
7290 */
7291 public Message setData(String dataMimeType, Uri dataUri) {
7292 mDataMimeType = dataMimeType;
7293 mDataUri = dataUri;
7294 return this;
7295 }
7296
Alex Hillsfc737de2016-03-23 17:33:02 -04007297 /**
7298 * Get the text to be used for this message, or the fallback text if a type and content
7299 * Uri have been set
7300 */
7301 public CharSequence getText() {
7302 return mText;
7303 }
7304
7305 /**
7306 * Get the time at which this message arrived
7307 */
7308 public long getTimestamp() {
7309 return mTimestamp;
7310 }
7311
7312 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007313 * Get the extras Bundle for this message.
7314 */
7315 public Bundle getExtras() {
7316 return mExtras;
7317 }
7318
7319 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007320 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007321 *
7322 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007323 */
7324 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007325 return mSender == null ? null : mSender.getName();
7326 }
7327
7328 /**
7329 * Get the sender associated with this message.
7330 */
7331 @Nullable
7332 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007333 return mSender;
7334 }
7335
7336 /**
7337 * Get the MIME type of the data pointed to by the Uri
7338 */
7339 public String getDataMimeType() {
7340 return mDataMimeType;
7341 }
7342
7343 /**
7344 * Get the the Uri pointing to the content of the message. Can be null, in which case
7345 * {@see #getText()} is used.
7346 */
7347 public Uri getDataUri() {
7348 return mDataUri;
7349 }
7350
Kenny Guya0f6de82018-04-06 16:20:16 +01007351 /**
7352 * @return True if the message was generated from
7353 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7354 * @hide
7355 */
7356 public boolean isRemoteInputHistory() {
7357 return mRemoteInputHistory;
7358 }
7359
Alex Hillsd9b04d92016-04-11 16:38:16 -04007360 private Bundle toBundle() {
7361 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007362 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007363 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007364 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007365 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007366 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007367 // Legacy listeners need this
7368 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7369 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007370 }
7371 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007372 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007373 }
7374 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007375 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007376 }
Shane Brennan5a871862017-03-11 13:14:17 -08007377 if (mExtras != null) {
7378 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7379 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007380 if (mRemoteInputHistory) {
7381 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7382 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007383 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007384 }
7385
Alex Hillsd9b04d92016-04-11 16:38:16 -04007386 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7387 Bundle[] bundles = new Bundle[messages.size()];
7388 final int N = messages.size();
7389 for (int i = 0; i < N; i++) {
7390 bundles[i] = messages.get(i).toBundle();
7391 }
7392 return bundles;
7393 }
7394
Selim Cinek88188f22017-09-19 16:46:56 -07007395 /**
7396 * @return A list of messages read from the bundles.
7397 *
7398 * @hide
7399 */
7400 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7401 if (bundles == null) {
7402 return new ArrayList<>();
7403 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007404 List<Message> messages = new ArrayList<>(bundles.length);
7405 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007406 if (bundles[i] instanceof Bundle) {
7407 Message message = getMessageFromBundle((Bundle)bundles[i]);
7408 if (message != null) {
7409 messages.add(message);
7410 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007411 }
7412 }
7413 return messages;
7414 }
7415
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007416 /**
7417 * @return The message that is stored in the bundle or null if the message couldn't be
7418 * resolved.
7419 *
7420 * @hide
7421 */
7422 @Nullable
7423 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007424 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007425 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007426 return null;
7427 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007428
7429 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7430 if (senderPerson == null) {
7431 // Legacy apps that use compat don't actually provide the sender objects
7432 // We need to fix the compat version to provide people / use
7433 // the native api instead
7434 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7435 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007436 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007437 }
7438 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007439 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007440 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007441 senderPerson,
7442 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007443 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7444 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007445 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7446 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007447 }
Shane Brennan5a871862017-03-11 13:14:17 -08007448 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7449 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7450 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007451 return message;
7452 }
7453 } catch (ClassCastException e) {
7454 return null;
7455 }
7456 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007457 }
7458 }
7459
7460 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007461 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007462 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007463 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007464 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007465 * Notification notif = new Notification.Builder(mContext)
7466 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7467 * .setContentText(subject)
7468 * .setSmallIcon(R.drawable.new_mail)
7469 * .setLargeIcon(aBitmap)
7470 * .setStyle(new Notification.InboxStyle()
7471 * .addLine(str1)
7472 * .addLine(str2)
7473 * .setContentTitle(&quot;&quot;)
7474 * .setSummaryText(&quot;+3 more&quot;))
7475 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007476 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007477 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007478 * @see Notification#bigContentView
7479 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007480 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007481
7482 /**
7483 * The number of lines of remote input history allowed until we start reducing lines.
7484 */
7485 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007486 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7487
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007488 public InboxStyle() {
7489 }
7490
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007491 /**
7492 * @deprecated use {@code InboxStyle()}.
7493 */
7494 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007495 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007496 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007497 }
7498
Chris Wrend6297db2012-05-03 16:20:13 -04007499 /**
7500 * Overrides ContentTitle in the big form of the template.
7501 * This defaults to the value passed to setContentTitle().
7502 */
7503 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007504 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007505 return this;
7506 }
7507
7508 /**
7509 * Set the first line of text after the detail section in the big form of the template.
7510 */
7511 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007512 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007513 return this;
7514 }
7515
Chris Wren0bd664d2012-08-01 13:56:56 -04007516 /**
7517 * Append a line to the digest section of the Inbox notification.
7518 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007519 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007520 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007521 return this;
7522 }
7523
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007524 /**
7525 * @hide
7526 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007527 public ArrayList<CharSequence> getLines() {
7528 return mTexts;
7529 }
7530
7531 /**
7532 * @hide
7533 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007534 public void addExtras(Bundle extras) {
7535 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007536
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007537 CharSequence[] a = new CharSequence[mTexts.size()];
7538 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7539 }
7540
Christoph Studer4600f9b2014-07-22 22:44:43 +02007541 /**
7542 * @hide
7543 */
7544 @Override
7545 protected void restoreFromExtras(Bundle extras) {
7546 super.restoreFromExtras(extras);
7547
7548 mTexts.clear();
7549 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7550 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7551 }
7552 }
7553
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007554 /**
7555 * @hide
7556 */
7557 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007558 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007559 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007560 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7561 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007562
Selim Cinek384804b2018-04-18 14:31:07 +08007563 TemplateBindResult result = new TemplateBindResult();
7564 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007565
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007566 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007567
Chris Wrend6297db2012-05-03 16:20:13 -04007568 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 -04007569 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007570
Chris Wren4ed80d52012-05-17 09:30:03 -04007571 // Make sure all rows are gone in case we reuse a view.
7572 for (int rowId : rowIds) {
7573 contentView.setViewVisibility(rowId, View.GONE);
7574 }
7575
Daniel Sandler879c5e02012-04-17 16:46:51 -04007576 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007577 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7578 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007579 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007580 int onlyViewId = 0;
7581 int maxRows = rowIds.length;
7582 if (mBuilder.mActions.size() > 0) {
7583 maxRows--;
7584 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007585 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7586 EXTRA_REMOTE_INPUT_HISTORY);
7587 if (remoteInputHistory != null
7588 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7589 // Let's remove some messages to make room for the remote input history.
7590 // 1 is always able to fit, but let's remove them if they are 2 or 3
7591 int numRemoteInputs = Math.min(remoteInputHistory.length,
7592 MAX_REMOTE_INPUT_HISTORY_LINES);
7593 int totalNumRows = mTexts.size() + numRemoteInputs
7594 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7595 if (totalNumRows > maxRows) {
7596 int overflow = totalNumRows - maxRows;
7597 if (mTexts.size() > maxRows) {
7598 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7599 // few messages, even with the remote input
7600 maxRows -= overflow;
7601 } else {
7602 // otherwise we drop the first messages
7603 i = overflow;
7604 }
7605 }
7606 }
Selim Cinek07c80172016-04-21 16:40:47 -07007607 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007608 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007609 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007610 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007611 contentView.setTextViewText(rowIds[i],
7612 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007613 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007614 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007615 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007616 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007617 if (first) {
7618 onlyViewId = rowIds[i];
7619 } else {
7620 onlyViewId = 0;
7621 }
Selim Cinek247fa012016-02-18 09:50:48 -08007622 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007623 }
7624 i++;
7625 }
Selim Cinek07c80172016-04-21 16:40:47 -07007626 if (onlyViewId != 0) {
7627 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7628 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7629 R.dimen.notification_text_margin_top);
7630 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7631 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007632
Daniel Sandler879c5e02012-04-17 16:46:51 -04007633 return contentView;
7634 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007635
Julia Reynolds7217dc92018-03-07 12:12:09 -05007636 /**
7637 * @hide
7638 */
7639 @Override
7640 public boolean areNotificationsVisiblyDifferent(Style other) {
7641 if (other == null || getClass() != other.getClass()) {
7642 return true;
7643 }
7644 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007645
7646 final ArrayList<CharSequence> myLines = getLines();
7647 final ArrayList<CharSequence> newLines = newS.getLines();
7648 final int n = myLines.size();
7649 if (n != newLines.size()) {
7650 return true;
7651 }
7652
7653 for (int i = 0; i < n; i++) {
7654 if (!Objects.equals(
7655 String.valueOf(myLines.get(i)),
7656 String.valueOf(newLines.get(i)))) {
7657 return true;
7658 }
7659 }
7660 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007661 }
7662
Selim Cinek384804b2018-04-18 14:31:07 +08007663 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007664 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007665 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007666 if (first) {
7667 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7668 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7669 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007670 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007671 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007672 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007673 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007674 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007675 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007676 }
Dan Sandler842dd772014-05-15 09:36:47 -04007677
7678 /**
7679 * Notification style for media playback notifications.
7680 *
7681 * In the expanded form, {@link Notification#bigContentView}, up to 5
7682 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007683 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007684 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7685 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7686 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007687 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007688 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7689 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007690 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007691 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007692 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007693 * Notifications created with MediaStyle will have their category set to
7694 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7695 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007696 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007697 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7698 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007699 * the System UI can identify this as a notification representing an active media session
7700 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7701 *
Selim Cinek99104832017-01-25 14:47:33 -08007702 * <p>
7703 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7704 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7705 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7706 * <p>
7707 *
Dan Sandler842dd772014-05-15 09:36:47 -04007708 * To use this style with your Notification, feed it to
7709 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7710 * <pre class="prettyprint">
7711 * Notification noti = new Notification.Builder()
7712 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007713 * .setContentTitle(&quot;Track title&quot;)
7714 * .setContentText(&quot;Artist - Album&quot;)
7715 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007716 * .setStyle(<b>new Notification.MediaStyle()</b>
7717 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007718 * .build();
7719 * </pre>
7720 *
7721 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007722 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007723 */
7724 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007725 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007726 static final int MAX_MEDIA_BUTTONS = 5;
7727
7728 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007729 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007730
7731 public MediaStyle() {
7732 }
7733
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007734 /**
7735 * @deprecated use {@code MediaStyle()}.
7736 */
7737 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007738 public MediaStyle(Builder builder) {
7739 setBuilder(builder);
7740 }
7741
7742 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007743 * 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 -04007744 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007745 *
7746 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007747 */
7748 public MediaStyle setShowActionsInCompactView(int...actions) {
7749 mActionsToShowInCompact = actions;
7750 return this;
7751 }
7752
7753 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007754 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7755 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007756 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007757 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007758 mToken = token;
7759 return this;
7760 }
7761
Christoph Studer4600f9b2014-07-22 22:44:43 +02007762 /**
7763 * @hide
7764 */
Dan Sandler842dd772014-05-15 09:36:47 -04007765 @Override
7766 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007767 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007768 if (wip.category == null) {
7769 wip.category = Notification.CATEGORY_TRANSPORT;
7770 }
Dan Sandler842dd772014-05-15 09:36:47 -04007771 return wip;
7772 }
7773
Christoph Studer4600f9b2014-07-22 22:44:43 +02007774 /**
7775 * @hide
7776 */
7777 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007778 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007779 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007780 }
7781
7782 /**
7783 * @hide
7784 */
7785 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007786 public RemoteViews makeBigContentView() {
7787 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007788 }
7789
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007790 /**
7791 * @hide
7792 */
7793 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007794 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007795 RemoteViews expanded = makeMediaBigContentView();
7796 return expanded != null ? expanded : makeMediaContentView();
7797 }
7798
Dan Sandler842dd772014-05-15 09:36:47 -04007799 /** @hide */
7800 @Override
7801 public void addExtras(Bundle extras) {
7802 super.addExtras(extras);
7803
7804 if (mToken != null) {
7805 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7806 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007807 if (mActionsToShowInCompact != null) {
7808 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7809 }
Dan Sandler842dd772014-05-15 09:36:47 -04007810 }
7811
Christoph Studer4600f9b2014-07-22 22:44:43 +02007812 /**
7813 * @hide
7814 */
7815 @Override
7816 protected void restoreFromExtras(Bundle extras) {
7817 super.restoreFromExtras(extras);
7818
7819 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7820 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7821 }
7822 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7823 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7824 }
7825 }
7826
Julia Reynolds7217dc92018-03-07 12:12:09 -05007827 /**
7828 * @hide
7829 */
7830 @Override
7831 public boolean areNotificationsVisiblyDifferent(Style other) {
7832 if (other == null || getClass() != other.getClass()) {
7833 return true;
7834 }
7835 // All fields to compare are on the Notification object
7836 return false;
7837 }
7838
Selim Cinek5bf069a2015-11-10 19:14:27 -05007839 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007840 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007841 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007842 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007843 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007844
7845 // If the action buttons should not be tinted, then just use the default
7846 // notification color. Otherwise, just use the passed-in color.
7847 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7848 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007849 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Anthony Chenad4d1582017-04-10 16:07:58 -07007850 Notification.COLOR_DEFAULT);
7851
Sunny Goyal5b153922017-09-21 21:00:36 -07007852 button.setDrawableTint(R.id.action0, false, tintColor,
7853 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007854 if (!tombstone) {
7855 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7856 }
7857 button.setContentDescription(R.id.action0, action.title);
7858 return button;
7859 }
7860
7861 private RemoteViews makeMediaContentView() {
7862 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007863 R.layout.notification_template_material_media, false, /* hasProgress */
7864 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007865
7866 final int numActions = mBuilder.mActions.size();
7867 final int N = mActionsToShowInCompact == null
7868 ? 0
7869 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7870 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007871 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007872 for (int i = 0; i < N; i++) {
7873 if (i >= numActions) {
7874 throw new IllegalArgumentException(String.format(
7875 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7876 i, numActions - 1));
7877 }
7878
7879 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinekc7f5a822018-03-20 19:32:06 -07007880 final RemoteViews button = generateMediaActionButton(action, getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007881 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007882 }
7883 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007884 handleImage(view);
7885 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007886 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007887 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007888 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007889 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007890 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007891 return view;
7892 }
7893
Selim Cinekc7f5a822018-03-20 19:32:06 -07007894 private int getActionColor() {
7895 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7896 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007897 }
7898
Dan Sandler842dd772014-05-15 09:36:47 -04007899 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007900 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007901 // Dont add an expanded view if there is no more content to be revealed
7902 int actionsInCompact = mActionsToShowInCompact == null
7903 ? 0
7904 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007905 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007906 return null;
7907 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007908 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007909 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007910
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007911 if (actionCount > 0) {
7912 big.removeAllViews(com.android.internal.R.id.media_actions);
7913 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007914 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07007915 getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007916 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007917 }
7918 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007919 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007920 return big;
7921 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007922
Selim Cinek5bf069a2015-11-10 19:14:27 -05007923 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007924 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007925 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7926 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007927 }
7928 }
7929
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007930 /**
7931 * @hide
7932 */
7933 @Override
7934 protected boolean hasProgress() {
7935 return false;
7936 }
Dan Sandler842dd772014-05-15 09:36:47 -04007937 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007938
Selim Cinek593610c2016-02-16 18:42:57 -08007939 /**
7940 * Notification style for custom views that are decorated by the system
7941 *
7942 * <p>Instead of providing a notification that is completely custom, a developer can set this
7943 * style and still obtain system decorations like the notification header with the expand
7944 * affordance and actions.
7945 *
7946 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7947 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7948 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7949 * corresponding custom views to display.
7950 *
7951 * To use this style with your Notification, feed it to
7952 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7953 * <pre class="prettyprint">
7954 * Notification noti = new Notification.Builder()
7955 * .setSmallIcon(R.drawable.ic_stat_player)
7956 * .setLargeIcon(albumArtBitmap))
7957 * .setCustomContentView(contentView);
7958 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7959 * .build();
7960 * </pre>
7961 */
7962 public static class DecoratedCustomViewStyle extends Style {
7963
7964 public DecoratedCustomViewStyle() {
7965 }
7966
Selim Cinek593610c2016-02-16 18:42:57 -08007967 /**
7968 * @hide
7969 */
7970 public boolean displayCustomViewInline() {
7971 return true;
7972 }
7973
7974 /**
7975 * @hide
7976 */
7977 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007978 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007979 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7980 }
7981
7982 /**
7983 * @hide
7984 */
7985 @Override
7986 public RemoteViews makeBigContentView() {
7987 return makeDecoratedBigContentView();
7988 }
7989
7990 /**
7991 * @hide
7992 */
7993 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007994 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007995 return makeDecoratedHeadsUpContentView();
7996 }
7997
Selim Cinek593610c2016-02-16 18:42:57 -08007998 private RemoteViews makeDecoratedHeadsUpContentView() {
7999 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8000 ? mBuilder.mN.contentView
8001 : mBuilder.mN.headsUpContentView;
8002 if (mBuilder.mActions.size() == 0) {
8003 return makeStandardTemplateWithCustomContent(headsUpContentView);
8004 }
Selim Cinek384804b2018-04-18 14:31:07 +08008005 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008006 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008007 mBuilder.getBigBaseLayoutResource(), result);
8008 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008009 return remoteViews;
8010 }
8011
Selim Cinek593610c2016-02-16 18:42:57 -08008012 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008013 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008014 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008015 mBuilder.getBaseLayoutResource(), result);
8016 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008017 return remoteViews;
8018 }
8019
Selim Cinek593610c2016-02-16 18:42:57 -08008020 private RemoteViews makeDecoratedBigContentView() {
8021 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8022 ? mBuilder.mN.contentView
8023 : mBuilder.mN.bigContentView;
8024 if (mBuilder.mActions.size() == 0) {
8025 return makeStandardTemplateWithCustomContent(bigContentView);
8026 }
Selim Cinek384804b2018-04-18 14:31:07 +08008027 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008028 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008029 mBuilder.getBigBaseLayoutResource(), result);
8030 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008031 return remoteViews;
8032 }
Selim Cinek247fa012016-02-18 09:50:48 -08008033
8034 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008035 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008036 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008037 // Need to clone customContent before adding, because otherwise it can no longer be
8038 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008039 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008040 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8041 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008042 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008043 }
Selim Cinek247fa012016-02-18 09:50:48 -08008044 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008045 Resources resources = mBuilder.mContext.getResources();
8046 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008047 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008048 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008049 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008050
8051 /**
8052 * @hide
8053 */
8054 @Override
8055 public boolean areNotificationsVisiblyDifferent(Style other) {
8056 if (other == null || getClass() != other.getClass()) {
8057 return true;
8058 }
8059 // Comparison done for all custom RemoteViews, independent of style
8060 return false;
8061 }
Selim Cinek593610c2016-02-16 18:42:57 -08008062 }
8063
Selim Cinek03eb3b72016-02-18 10:39:45 -08008064 /**
8065 * Notification style for media custom views that are decorated by the system
8066 *
8067 * <p>Instead of providing a media notification that is completely custom, a developer can set
8068 * this style and still obtain system decorations like the notification header with the expand
8069 * affordance and actions.
8070 *
8071 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8072 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8073 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8074 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008075 * <p>
8076 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8077 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8078 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008079 * To use this style with your Notification, feed it to
8080 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8081 * <pre class="prettyprint">
8082 * Notification noti = new Notification.Builder()
8083 * .setSmallIcon(R.drawable.ic_stat_player)
8084 * .setLargeIcon(albumArtBitmap))
8085 * .setCustomContentView(contentView);
8086 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8087 * .setMediaSession(mySession))
8088 * .build();
8089 * </pre>
8090 *
8091 * @see android.app.Notification.DecoratedCustomViewStyle
8092 * @see android.app.Notification.MediaStyle
8093 */
8094 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8095
8096 public DecoratedMediaCustomViewStyle() {
8097 }
8098
Selim Cinek03eb3b72016-02-18 10:39:45 -08008099 /**
8100 * @hide
8101 */
8102 public boolean displayCustomViewInline() {
8103 return true;
8104 }
8105
8106 /**
8107 * @hide
8108 */
8109 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008110 public RemoteViews makeContentView(boolean increasedHeight) {
8111 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008112 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8113 mBuilder.mN.contentView);
8114 }
8115
8116 /**
8117 * @hide
8118 */
8119 @Override
8120 public RemoteViews makeBigContentView() {
8121 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8122 ? mBuilder.mN.bigContentView
8123 : mBuilder.mN.contentView;
8124 return makeBigContentViewWithCustomContent(customRemoteView);
8125 }
8126
8127 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8128 RemoteViews remoteViews = super.makeBigContentView();
8129 if (remoteViews != null) {
8130 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8131 customRemoteView);
8132 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008133 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008134 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8135 customRemoteView);
8136 } else {
8137 return null;
8138 }
8139 }
8140
8141 /**
8142 * @hide
8143 */
8144 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008145 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008146 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8147 ? mBuilder.mN.headsUpContentView
8148 : mBuilder.mN.contentView;
8149 return makeBigContentViewWithCustomContent(customRemoteView);
8150 }
8151
Julia Reynolds7217dc92018-03-07 12:12:09 -05008152 /**
8153 * @hide
8154 */
8155 @Override
8156 public boolean areNotificationsVisiblyDifferent(Style other) {
8157 if (other == null || getClass() != other.getClass()) {
8158 return true;
8159 }
8160 // Comparison done for all custom RemoteViews, independent of style
8161 return false;
8162 }
8163
Selim Cinek03eb3b72016-02-18 10:39:45 -08008164 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8165 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008166 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008167 // Need to clone customContent before adding, because otherwise it can no longer be
8168 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008169 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008170 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008171 remoteViews.removeAllViews(id);
8172 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008173 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008174 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008175 return remoteViews;
8176 }
8177 }
8178
Christoph Studer4600f9b2014-07-22 22:44:43 +02008179 // When adding a new Style subclass here, don't forget to update
8180 // Builder.getNotificationStyleClass.
8181
Griff Hazen61a9e862014-05-22 16:05:19 -07008182 /**
8183 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8184 * metadata or change options on a notification builder.
8185 */
8186 public interface Extender {
8187 /**
8188 * Apply this extender to a notification builder.
8189 * @param builder the builder to be modified.
8190 * @return the build object for chaining.
8191 */
8192 public Builder extend(Builder builder);
8193 }
8194
8195 /**
8196 * Helper class to add wearable extensions to notifications.
8197 * <p class="note"> See
8198 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8199 * for Android Wear</a> for more information on how to use this class.
8200 * <p>
8201 * To create a notification with wearable extensions:
8202 * <ol>
8203 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8204 * properties.
8205 * <li>Create a {@link android.app.Notification.WearableExtender}.
8206 * <li>Set wearable-specific properties using the
8207 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8208 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8209 * notification.
8210 * <li>Post the notification to the notification system with the
8211 * {@code NotificationManager.notify(...)} methods.
8212 * </ol>
8213 *
8214 * <pre class="prettyprint">
8215 * Notification notif = new Notification.Builder(mContext)
8216 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8217 * .setContentText(subject)
8218 * .setSmallIcon(R.drawable.new_mail)
8219 * .extend(new Notification.WearableExtender()
8220 * .setContentIcon(R.drawable.new_mail))
8221 * .build();
8222 * NotificationManager notificationManger =
8223 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8224 * notificationManger.notify(0, notif);</pre>
8225 *
8226 * <p>Wearable extensions can be accessed on an existing notification by using the
8227 * {@code WearableExtender(Notification)} constructor,
8228 * and then using the {@code get} methods to access values.
8229 *
8230 * <pre class="prettyprint">
8231 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8232 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008233 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008234 */
8235 public static final class WearableExtender implements Extender {
8236 /**
8237 * Sentinel value for an action index that is unset.
8238 */
8239 public static final int UNSET_ACTION_INDEX = -1;
8240
8241 /**
8242 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8243 * default sizing.
8244 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008245 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008246 * on their content.
8247 */
8248 public static final int SIZE_DEFAULT = 0;
8249
8250 /**
8251 * Size value for use with {@link #setCustomSizePreset} to show this notification
8252 * with an extra small size.
8253 * <p>This value is only applicable for custom display notifications created using
8254 * {@link #setDisplayIntent}.
8255 */
8256 public static final int SIZE_XSMALL = 1;
8257
8258 /**
8259 * Size value for use with {@link #setCustomSizePreset} to show this notification
8260 * with a small size.
8261 * <p>This value is only applicable for custom display notifications created using
8262 * {@link #setDisplayIntent}.
8263 */
8264 public static final int SIZE_SMALL = 2;
8265
8266 /**
8267 * Size value for use with {@link #setCustomSizePreset} to show this notification
8268 * with a medium size.
8269 * <p>This value is only applicable for custom display notifications created using
8270 * {@link #setDisplayIntent}.
8271 */
8272 public static final int SIZE_MEDIUM = 3;
8273
8274 /**
8275 * Size value for use with {@link #setCustomSizePreset} to show this notification
8276 * with a large size.
8277 * <p>This value is only applicable for custom display notifications created using
8278 * {@link #setDisplayIntent}.
8279 */
8280 public static final int SIZE_LARGE = 4;
8281
Griff Hazend5f11f92014-05-27 15:40:09 -07008282 /**
8283 * Size value for use with {@link #setCustomSizePreset} to show this notification
8284 * full screen.
8285 * <p>This value is only applicable for custom display notifications created using
8286 * {@link #setDisplayIntent}.
8287 */
8288 public static final int SIZE_FULL_SCREEN = 5;
8289
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008290 /**
8291 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8292 * short amount of time when this notification is displayed on the screen. This
8293 * is the default value.
8294 */
8295 public static final int SCREEN_TIMEOUT_SHORT = 0;
8296
8297 /**
8298 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8299 * for a longer amount of time when this notification is displayed on the screen.
8300 */
8301 public static final int SCREEN_TIMEOUT_LONG = -1;
8302
Griff Hazen61a9e862014-05-22 16:05:19 -07008303 /** Notification extra which contains wearable extensions */
8304 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8305
Pete Gastaf6781d2014-10-07 15:17:05 -04008306 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008307 private static final String KEY_ACTIONS = "actions";
8308 private static final String KEY_FLAGS = "flags";
8309 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8310 private static final String KEY_PAGES = "pages";
8311 private static final String KEY_BACKGROUND = "background";
8312 private static final String KEY_CONTENT_ICON = "contentIcon";
8313 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8314 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8315 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8316 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8317 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008318 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008319 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008320 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008321
8322 // Flags bitwise-ored to mFlags
8323 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8324 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8325 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8326 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008327 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008328 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008329 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008330
8331 // Default value for flags integer
8332 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8333
8334 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8335 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8336
8337 private ArrayList<Action> mActions = new ArrayList<Action>();
8338 private int mFlags = DEFAULT_FLAGS;
8339 private PendingIntent mDisplayIntent;
8340 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8341 private Bitmap mBackground;
8342 private int mContentIcon;
8343 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8344 private int mContentActionIndex = UNSET_ACTION_INDEX;
8345 private int mCustomSizePreset = SIZE_DEFAULT;
8346 private int mCustomContentHeight;
8347 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008348 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008349 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008350 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008351
8352 /**
8353 * Create a {@link android.app.Notification.WearableExtender} with default
8354 * options.
8355 */
8356 public WearableExtender() {
8357 }
8358
8359 public WearableExtender(Notification notif) {
8360 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8361 if (wearableBundle != null) {
8362 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8363 if (actions != null) {
8364 mActions.addAll(actions);
8365 }
8366
8367 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8368 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8369
8370 Notification[] pages = getNotificationArrayFromBundle(
8371 wearableBundle, KEY_PAGES);
8372 if (pages != null) {
8373 Collections.addAll(mPages, pages);
8374 }
8375
8376 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8377 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8378 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8379 DEFAULT_CONTENT_ICON_GRAVITY);
8380 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8381 UNSET_ACTION_INDEX);
8382 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8383 SIZE_DEFAULT);
8384 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8385 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008386 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008387 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008388 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008389 }
8390 }
8391
8392 /**
8393 * Apply wearable extensions to a notification that is being built. This is typically
8394 * called by the {@link android.app.Notification.Builder#extend} method of
8395 * {@link android.app.Notification.Builder}.
8396 */
8397 @Override
8398 public Notification.Builder extend(Notification.Builder builder) {
8399 Bundle wearableBundle = new Bundle();
8400
8401 if (!mActions.isEmpty()) {
8402 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8403 }
8404 if (mFlags != DEFAULT_FLAGS) {
8405 wearableBundle.putInt(KEY_FLAGS, mFlags);
8406 }
8407 if (mDisplayIntent != null) {
8408 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8409 }
8410 if (!mPages.isEmpty()) {
8411 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8412 new Notification[mPages.size()]));
8413 }
8414 if (mBackground != null) {
8415 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8416 }
8417 if (mContentIcon != 0) {
8418 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8419 }
8420 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8421 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8422 }
8423 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8424 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8425 mContentActionIndex);
8426 }
8427 if (mCustomSizePreset != SIZE_DEFAULT) {
8428 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8429 }
8430 if (mCustomContentHeight != 0) {
8431 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8432 }
8433 if (mGravity != DEFAULT_GRAVITY) {
8434 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8435 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008436 if (mHintScreenTimeout != 0) {
8437 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8438 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008439 if (mDismissalId != null) {
8440 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8441 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008442 if (mBridgeTag != null) {
8443 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8444 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008445
8446 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8447 return builder;
8448 }
8449
8450 @Override
8451 public WearableExtender clone() {
8452 WearableExtender that = new WearableExtender();
8453 that.mActions = new ArrayList<Action>(this.mActions);
8454 that.mFlags = this.mFlags;
8455 that.mDisplayIntent = this.mDisplayIntent;
8456 that.mPages = new ArrayList<Notification>(this.mPages);
8457 that.mBackground = this.mBackground;
8458 that.mContentIcon = this.mContentIcon;
8459 that.mContentIconGravity = this.mContentIconGravity;
8460 that.mContentActionIndex = this.mContentActionIndex;
8461 that.mCustomSizePreset = this.mCustomSizePreset;
8462 that.mCustomContentHeight = this.mCustomContentHeight;
8463 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008464 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008465 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008466 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008467 return that;
8468 }
8469
8470 /**
8471 * Add a wearable action to this notification.
8472 *
8473 * <p>When wearable actions are added using this method, the set of actions that
8474 * show on a wearable device splits from devices that only show actions added
8475 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8476 * of which actions display on different devices.
8477 *
8478 * @param action the action to add to this notification
8479 * @return this object for method chaining
8480 * @see android.app.Notification.Action
8481 */
8482 public WearableExtender addAction(Action action) {
8483 mActions.add(action);
8484 return this;
8485 }
8486
8487 /**
8488 * Adds wearable actions to this notification.
8489 *
8490 * <p>When wearable actions are added using this method, the set of actions that
8491 * show on a wearable device splits from devices that only show actions added
8492 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8493 * of which actions display on different devices.
8494 *
8495 * @param actions the actions to add to this notification
8496 * @return this object for method chaining
8497 * @see android.app.Notification.Action
8498 */
8499 public WearableExtender addActions(List<Action> actions) {
8500 mActions.addAll(actions);
8501 return this;
8502 }
8503
8504 /**
8505 * Clear all wearable actions present on this builder.
8506 * @return this object for method chaining.
8507 * @see #addAction
8508 */
8509 public WearableExtender clearActions() {
8510 mActions.clear();
8511 return this;
8512 }
8513
8514 /**
8515 * Get the wearable actions present on this notification.
8516 */
8517 public List<Action> getActions() {
8518 return mActions;
8519 }
8520
8521 /**
8522 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008523 * this notification. The {@link PendingIntent} provided should be for an activity.
8524 *
8525 * <pre class="prettyprint">
8526 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8527 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8528 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8529 * Notification notif = new Notification.Builder(context)
8530 * .extend(new Notification.WearableExtender()
8531 * .setDisplayIntent(displayPendingIntent)
8532 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8533 * .build();</pre>
8534 *
8535 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008536 * should have an empty task affinity. It is also recommended to use the device
8537 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008538 *
8539 * <p>Example AndroidManifest.xml entry:
8540 * <pre class="prettyprint">
8541 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8542 * android:exported=&quot;true&quot;
8543 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008544 * android:taskAffinity=&quot;&quot;
8545 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008546 *
8547 * @param intent the {@link PendingIntent} for an activity
8548 * @return this object for method chaining
8549 * @see android.app.Notification.WearableExtender#getDisplayIntent
8550 */
8551 public WearableExtender setDisplayIntent(PendingIntent intent) {
8552 mDisplayIntent = intent;
8553 return this;
8554 }
8555
8556 /**
8557 * Get the intent to launch inside of an activity view when displaying this
8558 * notification. This {@code PendingIntent} should be for an activity.
8559 */
8560 public PendingIntent getDisplayIntent() {
8561 return mDisplayIntent;
8562 }
8563
8564 /**
8565 * Add an additional page of content to display with this notification. The current
8566 * notification forms the first page, and pages added using this function form
8567 * subsequent pages. This field can be used to separate a notification into multiple
8568 * sections.
8569 *
8570 * @param page the notification to add as another page
8571 * @return this object for method chaining
8572 * @see android.app.Notification.WearableExtender#getPages
8573 */
8574 public WearableExtender addPage(Notification page) {
8575 mPages.add(page);
8576 return this;
8577 }
8578
8579 /**
8580 * Add additional pages of content to display with this notification. The current
8581 * notification forms the first page, and pages added using this function form
8582 * subsequent pages. This field can be used to separate a notification into multiple
8583 * sections.
8584 *
8585 * @param pages a list of notifications
8586 * @return this object for method chaining
8587 * @see android.app.Notification.WearableExtender#getPages
8588 */
8589 public WearableExtender addPages(List<Notification> pages) {
8590 mPages.addAll(pages);
8591 return this;
8592 }
8593
8594 /**
8595 * Clear all additional pages present on this builder.
8596 * @return this object for method chaining.
8597 * @see #addPage
8598 */
8599 public WearableExtender clearPages() {
8600 mPages.clear();
8601 return this;
8602 }
8603
8604 /**
8605 * Get the array of additional pages of content for displaying this notification. The
8606 * current notification forms the first page, and elements within this array form
8607 * subsequent pages. This field can be used to separate a notification into multiple
8608 * sections.
8609 * @return the pages for this notification
8610 */
8611 public List<Notification> getPages() {
8612 return mPages;
8613 }
8614
8615 /**
8616 * Set a background image to be displayed behind the notification content.
8617 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8618 * will work with any notification style.
8619 *
8620 * @param background the background bitmap
8621 * @return this object for method chaining
8622 * @see android.app.Notification.WearableExtender#getBackground
8623 */
8624 public WearableExtender setBackground(Bitmap background) {
8625 mBackground = background;
8626 return this;
8627 }
8628
8629 /**
8630 * Get a background image to be displayed behind the notification content.
8631 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8632 * will work with any notification style.
8633 *
8634 * @return the background image
8635 * @see android.app.Notification.WearableExtender#setBackground
8636 */
8637 public Bitmap getBackground() {
8638 return mBackground;
8639 }
8640
8641 /**
8642 * Set an icon that goes with the content of this notification.
8643 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008644 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008645 public WearableExtender setContentIcon(int icon) {
8646 mContentIcon = icon;
8647 return this;
8648 }
8649
8650 /**
8651 * Get an icon that goes with the content of this notification.
8652 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008653 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008654 public int getContentIcon() {
8655 return mContentIcon;
8656 }
8657
8658 /**
8659 * Set the gravity that the content icon should have within the notification display.
8660 * Supported values include {@link android.view.Gravity#START} and
8661 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8662 * @see #setContentIcon
8663 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008664 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008665 public WearableExtender setContentIconGravity(int contentIconGravity) {
8666 mContentIconGravity = contentIconGravity;
8667 return this;
8668 }
8669
8670 /**
8671 * Get the gravity that the content icon should have within the notification display.
8672 * Supported values include {@link android.view.Gravity#START} and
8673 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8674 * @see #getContentIcon
8675 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008676 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008677 public int getContentIconGravity() {
8678 return mContentIconGravity;
8679 }
8680
8681 /**
8682 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008683 * this notification. This action will no longer display separately from the
8684 * notification's content.
8685 *
Griff Hazenca48d352014-05-28 22:37:13 -07008686 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008687 * set, although the list of available actions comes from the main notification and not
8688 * from the child page's notification.
8689 *
8690 * @param actionIndex The index of the action to hoist onto the current notification page.
8691 * If wearable actions were added to the main notification, this index
8692 * will apply to that list, otherwise it will apply to the regular
8693 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008694 */
8695 public WearableExtender setContentAction(int actionIndex) {
8696 mContentActionIndex = actionIndex;
8697 return this;
8698 }
8699
8700 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008701 * Get the index of the notification action, if any, that was specified as being clickable
8702 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008703 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008704 *
Griff Hazenca48d352014-05-28 22:37:13 -07008705 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008706 * set, although the list of available actions comes from the main notification and not
8707 * from the child page's notification.
8708 *
8709 * <p>If wearable specific actions were added to the main notification, this index will
8710 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008711 *
8712 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008713 */
8714 public int getContentAction() {
8715 return mContentActionIndex;
8716 }
8717
8718 /**
8719 * Set the gravity that this notification should have within the available viewport space.
8720 * Supported values include {@link android.view.Gravity#TOP},
8721 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8722 * The default value is {@link android.view.Gravity#BOTTOM}.
8723 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008724 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008725 public WearableExtender setGravity(int gravity) {
8726 mGravity = gravity;
8727 return this;
8728 }
8729
8730 /**
8731 * Get the gravity that this notification should have within the available viewport space.
8732 * Supported values include {@link android.view.Gravity#TOP},
8733 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8734 * The default value is {@link android.view.Gravity#BOTTOM}.
8735 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008736 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008737 public int getGravity() {
8738 return mGravity;
8739 }
8740
8741 /**
8742 * Set the custom size preset for the display of this notification out of the available
8743 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8744 * {@link #SIZE_LARGE}.
8745 * <p>Some custom size presets are only applicable for custom display notifications created
8746 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8747 * documentation for the preset in question. See also
8748 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8749 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008750 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008751 public WearableExtender setCustomSizePreset(int sizePreset) {
8752 mCustomSizePreset = sizePreset;
8753 return this;
8754 }
8755
8756 /**
8757 * Get the custom size preset for the display of this notification out of the available
8758 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8759 * {@link #SIZE_LARGE}.
8760 * <p>Some custom size presets are only applicable for custom display notifications created
8761 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8762 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8763 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008764 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008765 public int getCustomSizePreset() {
8766 return mCustomSizePreset;
8767 }
8768
8769 /**
8770 * Set the custom height in pixels for the display of this notification's content.
8771 * <p>This option is only available for custom display notifications created
8772 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8773 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8774 * {@link #getCustomContentHeight}.
8775 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008776 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008777 public WearableExtender setCustomContentHeight(int height) {
8778 mCustomContentHeight = height;
8779 return this;
8780 }
8781
8782 /**
8783 * Get the custom height in pixels for the display of this notification's content.
8784 * <p>This option is only available for custom display notifications created
8785 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8786 * {@link #setCustomContentHeight}.
8787 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008788 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008789 public int getCustomContentHeight() {
8790 return mCustomContentHeight;
8791 }
8792
8793 /**
8794 * Set whether the scrolling position for the contents of this notification should start
8795 * at the bottom of the contents instead of the top when the contents are too long to
8796 * display within the screen. Default is false (start scroll at the top).
8797 */
8798 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8799 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8800 return this;
8801 }
8802
8803 /**
8804 * Get whether the scrolling position for the contents of this notification should start
8805 * at the bottom of the contents instead of the top when the contents are too long to
8806 * display within the screen. Default is false (start scroll at the top).
8807 */
8808 public boolean getStartScrollBottom() {
8809 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8810 }
8811
8812 /**
8813 * Set whether the content intent is available when the wearable device is not connected
8814 * to a companion device. The user can still trigger this intent when the wearable device
8815 * is offline, but a visual hint will indicate that the content intent may not be available.
8816 * Defaults to true.
8817 */
8818 public WearableExtender setContentIntentAvailableOffline(
8819 boolean contentIntentAvailableOffline) {
8820 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8821 return this;
8822 }
8823
8824 /**
8825 * Get whether the content intent is available when the wearable device is not connected
8826 * to a companion device. The user can still trigger this intent when the wearable device
8827 * is offline, but a visual hint will indicate that the content intent may not be available.
8828 * Defaults to true.
8829 */
8830 public boolean getContentIntentAvailableOffline() {
8831 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8832 }
8833
8834 /**
8835 * Set a hint that this notification's icon should not be displayed.
8836 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8837 * @return this object for method chaining
8838 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008839 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008840 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8841 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8842 return this;
8843 }
8844
8845 /**
8846 * Get a hint that this notification's icon should not be displayed.
8847 * @return {@code true} if this icon should not be displayed, false otherwise.
8848 * The default value is {@code false} if this was never set.
8849 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008850 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008851 public boolean getHintHideIcon() {
8852 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8853 }
8854
8855 /**
8856 * Set a visual hint that only the background image of this notification should be
8857 * displayed, and other semantic content should be hidden. This hint is only applicable
8858 * to sub-pages added using {@link #addPage}.
8859 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008860 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008861 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8862 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8863 return this;
8864 }
8865
8866 /**
8867 * Get a visual hint that only the background image of this notification should be
8868 * displayed, and other semantic content should be hidden. This hint is only applicable
8869 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8870 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008871 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008872 public boolean getHintShowBackgroundOnly() {
8873 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8874 }
8875
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008876 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008877 * Set a hint that this notification's background should not be clipped if possible,
8878 * and should instead be resized to fully display on the screen, retaining the aspect
8879 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008880 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8881 * @return this object for method chaining
8882 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008883 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008884 public WearableExtender setHintAvoidBackgroundClipping(
8885 boolean hintAvoidBackgroundClipping) {
8886 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8887 return this;
8888 }
8889
8890 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008891 * Get a hint that this notification's background should not be clipped if possible,
8892 * and should instead be resized to fully display on the screen, retaining the aspect
8893 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008894 * @return {@code true} if it's ok if the background is clipped on the screen, false
8895 * otherwise. The default value is {@code false} if this was never set.
8896 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008897 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008898 public boolean getHintAvoidBackgroundClipping() {
8899 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8900 }
8901
8902 /**
8903 * Set a hint that the screen should remain on for at least this duration when
8904 * this notification is displayed on the screen.
8905 * @param timeout The requested screen timeout in milliseconds. Can also be either
8906 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8907 * @return this object for method chaining
8908 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008909 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008910 public WearableExtender setHintScreenTimeout(int timeout) {
8911 mHintScreenTimeout = timeout;
8912 return this;
8913 }
8914
8915 /**
8916 * Get the duration, in milliseconds, that the screen should remain on for
8917 * when this notification is displayed.
8918 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8919 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8920 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008921 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008922 public int getHintScreenTimeout() {
8923 return mHintScreenTimeout;
8924 }
8925
Alex Hills9ab3a232016-04-05 14:54:56 -04008926 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008927 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8928 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8929 * qr codes, as well as other simple black-and-white tickets.
8930 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8931 * @return this object for method chaining
8932 */
8933 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8934 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8935 return this;
8936 }
8937
8938 /**
8939 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8940 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8941 * qr codes, as well as other simple black-and-white tickets.
8942 * @return {@code true} if it should be displayed in ambient, false otherwise
8943 * otherwise. The default value is {@code false} if this was never set.
8944 */
8945 public boolean getHintAmbientBigPicture() {
8946 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8947 }
8948
8949 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008950 * Set a hint that this notification's content intent will launch an {@link Activity}
8951 * directly, telling the platform that it can generate the appropriate transitions.
8952 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8953 * an activity and transitions should be generated, false otherwise.
8954 * @return this object for method chaining
8955 */
8956 public WearableExtender setHintContentIntentLaunchesActivity(
8957 boolean hintContentIntentLaunchesActivity) {
8958 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8959 return this;
8960 }
8961
8962 /**
8963 * Get a hint that this notification's content intent will launch an {@link Activity}
8964 * directly, telling the platform that it can generate the appropriate transitions
8965 * @return {@code true} if the content intent will launch an activity and transitions should
8966 * be generated, false otherwise. The default value is {@code false} if this was never set.
8967 */
8968 public boolean getHintContentIntentLaunchesActivity() {
8969 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8970 }
8971
Nadia Benbernou948627e2016-04-14 14:41:08 -04008972 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008973 * Sets the dismissal id for this notification. If a notification is posted with a
8974 * dismissal id, then when that notification is canceled, notifications on other wearables
8975 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008976 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008977 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008978 * @param dismissalId the dismissal id of the notification.
8979 * @return this object for method chaining
8980 */
8981 public WearableExtender setDismissalId(String dismissalId) {
8982 mDismissalId = dismissalId;
8983 return this;
8984 }
8985
8986 /**
8987 * Returns the dismissal id of the notification.
8988 * @return the dismissal id of the notification or null if it has not been set.
8989 */
8990 public String getDismissalId() {
8991 return mDismissalId;
8992 }
8993
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008994 /**
8995 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8996 * posted from a phone to provide finer-grained control on what notifications are bridged
8997 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8998 * Features to Notifications</a> for more information.
8999 * @param bridgeTag the bridge tag of the notification.
9000 * @return this object for method chaining
9001 */
9002 public WearableExtender setBridgeTag(String bridgeTag) {
9003 mBridgeTag = bridgeTag;
9004 return this;
9005 }
9006
9007 /**
9008 * Returns the bridge tag of the notification.
9009 * @return the bridge tag or null if not present.
9010 */
9011 public String getBridgeTag() {
9012 return mBridgeTag;
9013 }
9014
Griff Hazen61a9e862014-05-22 16:05:19 -07009015 private void setFlag(int mask, boolean value) {
9016 if (value) {
9017 mFlags |= mask;
9018 } else {
9019 mFlags &= ~mask;
9020 }
9021 }
9022 }
9023
9024 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009025 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9026 * with car extensions:
9027 *
9028 * <ol>
9029 * <li>Create an {@link Notification.Builder}, setting any desired
9030 * properties.
9031 * <li>Create a {@link CarExtender}.
9032 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9033 * {@link CarExtender}.
9034 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9035 * to apply the extensions to a notification.
9036 * </ol>
9037 *
9038 * <pre class="prettyprint">
9039 * Notification notification = new Notification.Builder(context)
9040 * ...
9041 * .extend(new CarExtender()
9042 * .set*(...))
9043 * .build();
9044 * </pre>
9045 *
9046 * <p>Car extensions can be accessed on an existing notification by using the
9047 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9048 * to access values.
9049 */
9050 public static final class CarExtender implements Extender {
9051 private static final String TAG = "CarExtender";
9052
9053 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9054 private static final String EXTRA_LARGE_ICON = "large_icon";
9055 private static final String EXTRA_CONVERSATION = "car_conversation";
9056 private static final String EXTRA_COLOR = "app_color";
9057
9058 private Bitmap mLargeIcon;
9059 private UnreadConversation mUnreadConversation;
9060 private int mColor = Notification.COLOR_DEFAULT;
9061
9062 /**
9063 * Create a {@link CarExtender} with default options.
9064 */
9065 public CarExtender() {
9066 }
9067
9068 /**
9069 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9070 *
9071 * @param notif The notification from which to copy options.
9072 */
9073 public CarExtender(Notification notif) {
9074 Bundle carBundle = notif.extras == null ?
9075 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9076 if (carBundle != null) {
9077 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9078 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9079
9080 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9081 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9082 }
9083 }
9084
9085 /**
9086 * Apply car extensions to a notification that is being built. This is typically called by
9087 * the {@link Notification.Builder#extend(Notification.Extender)}
9088 * method of {@link Notification.Builder}.
9089 */
9090 @Override
9091 public Notification.Builder extend(Notification.Builder builder) {
9092 Bundle carExtensions = new Bundle();
9093
9094 if (mLargeIcon != null) {
9095 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9096 }
9097 if (mColor != Notification.COLOR_DEFAULT) {
9098 carExtensions.putInt(EXTRA_COLOR, mColor);
9099 }
9100
9101 if (mUnreadConversation != null) {
9102 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9103 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9104 }
9105
9106 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9107 return builder;
9108 }
9109
9110 /**
9111 * Sets the accent color to use when Android Auto presents the notification.
9112 *
9113 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9114 * to accent the displayed notification. However, not all colors are acceptable in an
9115 * automotive setting. This method can be used to override the color provided in the
9116 * notification in such a situation.
9117 */
Tor Norbye80756e32015-03-02 09:39:27 -08009118 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009119 mColor = color;
9120 return this;
9121 }
9122
9123 /**
9124 * Gets the accent color.
9125 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009126 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009127 */
Tor Norbye80756e32015-03-02 09:39:27 -08009128 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009129 public int getColor() {
9130 return mColor;
9131 }
9132
9133 /**
9134 * Sets the large icon of the car notification.
9135 *
9136 * If no large icon is set in the extender, Android Auto will display the icon
9137 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9138 *
9139 * @param largeIcon The large icon to use in the car notification.
9140 * @return This object for method chaining.
9141 */
9142 public CarExtender setLargeIcon(Bitmap largeIcon) {
9143 mLargeIcon = largeIcon;
9144 return this;
9145 }
9146
9147 /**
9148 * Gets the large icon used in this car notification, or null if no icon has been set.
9149 *
9150 * @return The large icon for the car notification.
9151 * @see CarExtender#setLargeIcon
9152 */
9153 public Bitmap getLargeIcon() {
9154 return mLargeIcon;
9155 }
9156
9157 /**
9158 * Sets the unread conversation in a message notification.
9159 *
9160 * @param unreadConversation The unread part of the conversation this notification conveys.
9161 * @return This object for method chaining.
9162 */
9163 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9164 mUnreadConversation = unreadConversation;
9165 return this;
9166 }
9167
9168 /**
9169 * Returns the unread conversation conveyed by this notification.
9170 * @see #setUnreadConversation(UnreadConversation)
9171 */
9172 public UnreadConversation getUnreadConversation() {
9173 return mUnreadConversation;
9174 }
9175
9176 /**
9177 * A class which holds the unread messages from a conversation.
9178 */
9179 public static class UnreadConversation {
9180 private static final String KEY_AUTHOR = "author";
9181 private static final String KEY_TEXT = "text";
9182 private static final String KEY_MESSAGES = "messages";
9183 private static final String KEY_REMOTE_INPUT = "remote_input";
9184 private static final String KEY_ON_REPLY = "on_reply";
9185 private static final String KEY_ON_READ = "on_read";
9186 private static final String KEY_PARTICIPANTS = "participants";
9187 private static final String KEY_TIMESTAMP = "timestamp";
9188
9189 private final String[] mMessages;
9190 private final RemoteInput mRemoteInput;
9191 private final PendingIntent mReplyPendingIntent;
9192 private final PendingIntent mReadPendingIntent;
9193 private final String[] mParticipants;
9194 private final long mLatestTimestamp;
9195
9196 UnreadConversation(String[] messages, RemoteInput remoteInput,
9197 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9198 String[] participants, long latestTimestamp) {
9199 mMessages = messages;
9200 mRemoteInput = remoteInput;
9201 mReadPendingIntent = readPendingIntent;
9202 mReplyPendingIntent = replyPendingIntent;
9203 mParticipants = participants;
9204 mLatestTimestamp = latestTimestamp;
9205 }
9206
9207 /**
9208 * Gets the list of messages conveyed by this notification.
9209 */
9210 public String[] getMessages() {
9211 return mMessages;
9212 }
9213
9214 /**
9215 * Gets the remote input that will be used to convey the response to a message list, or
9216 * null if no such remote input exists.
9217 */
9218 public RemoteInput getRemoteInput() {
9219 return mRemoteInput;
9220 }
9221
9222 /**
9223 * Gets the pending intent that will be triggered when the user replies to this
9224 * notification.
9225 */
9226 public PendingIntent getReplyPendingIntent() {
9227 return mReplyPendingIntent;
9228 }
9229
9230 /**
9231 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9232 * in this object's message list.
9233 */
9234 public PendingIntent getReadPendingIntent() {
9235 return mReadPendingIntent;
9236 }
9237
9238 /**
9239 * Gets the participants in the conversation.
9240 */
9241 public String[] getParticipants() {
9242 return mParticipants;
9243 }
9244
9245 /**
9246 * Gets the firs participant in the conversation.
9247 */
9248 public String getParticipant() {
9249 return mParticipants.length > 0 ? mParticipants[0] : null;
9250 }
9251
9252 /**
9253 * Gets the timestamp of the conversation.
9254 */
9255 public long getLatestTimestamp() {
9256 return mLatestTimestamp;
9257 }
9258
9259 Bundle getBundleForUnreadConversation() {
9260 Bundle b = new Bundle();
9261 String author = null;
9262 if (mParticipants != null && mParticipants.length > 1) {
9263 author = mParticipants[0];
9264 }
9265 Parcelable[] messages = new Parcelable[mMessages.length];
9266 for (int i = 0; i < messages.length; i++) {
9267 Bundle m = new Bundle();
9268 m.putString(KEY_TEXT, mMessages[i]);
9269 m.putString(KEY_AUTHOR, author);
9270 messages[i] = m;
9271 }
9272 b.putParcelableArray(KEY_MESSAGES, messages);
9273 if (mRemoteInput != null) {
9274 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9275 }
9276 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9277 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9278 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9279 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9280 return b;
9281 }
9282
9283 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9284 if (b == null) {
9285 return null;
9286 }
9287 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9288 String[] messages = null;
9289 if (parcelableMessages != null) {
9290 String[] tmp = new String[parcelableMessages.length];
9291 boolean success = true;
9292 for (int i = 0; i < tmp.length; i++) {
9293 if (!(parcelableMessages[i] instanceof Bundle)) {
9294 success = false;
9295 break;
9296 }
9297 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9298 if (tmp[i] == null) {
9299 success = false;
9300 break;
9301 }
9302 }
9303 if (success) {
9304 messages = tmp;
9305 } else {
9306 return null;
9307 }
9308 }
9309
9310 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9311 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9312
9313 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9314
9315 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9316 if (participants == null || participants.length != 1) {
9317 return null;
9318 }
9319
9320 return new UnreadConversation(messages,
9321 remoteInput,
9322 onReply,
9323 onRead,
9324 participants, b.getLong(KEY_TIMESTAMP));
9325 }
9326 };
9327
9328 /**
9329 * Builder class for {@link CarExtender.UnreadConversation} objects.
9330 */
9331 public static class Builder {
9332 private final List<String> mMessages = new ArrayList<String>();
9333 private final String mParticipant;
9334 private RemoteInput mRemoteInput;
9335 private PendingIntent mReadPendingIntent;
9336 private PendingIntent mReplyPendingIntent;
9337 private long mLatestTimestamp;
9338
9339 /**
9340 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9341 *
9342 * @param name The name of the other participant in the conversation.
9343 */
9344 public Builder(String name) {
9345 mParticipant = name;
9346 }
9347
9348 /**
9349 * Appends a new unread message to the list of messages for this conversation.
9350 *
9351 * The messages should be added from oldest to newest.
9352 *
9353 * @param message The text of the new unread message.
9354 * @return This object for method chaining.
9355 */
9356 public Builder addMessage(String message) {
9357 mMessages.add(message);
9358 return this;
9359 }
9360
9361 /**
9362 * Sets the pending intent and remote input which will convey the reply to this
9363 * notification.
9364 *
9365 * @param pendingIntent The pending intent which will be triggered on a reply.
9366 * @param remoteInput The remote input parcelable which will carry the reply.
9367 * @return This object for method chaining.
9368 *
9369 * @see CarExtender.UnreadConversation#getRemoteInput
9370 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9371 */
9372 public Builder setReplyAction(
9373 PendingIntent pendingIntent, RemoteInput remoteInput) {
9374 mRemoteInput = remoteInput;
9375 mReplyPendingIntent = pendingIntent;
9376
9377 return this;
9378 }
9379
9380 /**
9381 * Sets the pending intent that will be sent once the messages in this notification
9382 * are read.
9383 *
9384 * @param pendingIntent The pending intent to use.
9385 * @return This object for method chaining.
9386 */
9387 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9388 mReadPendingIntent = pendingIntent;
9389 return this;
9390 }
9391
9392 /**
9393 * Sets the timestamp of the most recent message in an unread conversation.
9394 *
9395 * If a messaging notification has been posted by your application and has not
9396 * yet been cancelled, posting a later notification with the same id and tag
9397 * but without a newer timestamp may result in Android Auto not displaying a
9398 * heads up notification for the later notification.
9399 *
9400 * @param timestamp The timestamp of the most recent message in the conversation.
9401 * @return This object for method chaining.
9402 */
9403 public Builder setLatestTimestamp(long timestamp) {
9404 mLatestTimestamp = timestamp;
9405 return this;
9406 }
9407
9408 /**
9409 * Builds a new unread conversation object.
9410 *
9411 * @return The new unread conversation object.
9412 */
9413 public UnreadConversation build() {
9414 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9415 String[] participants = { mParticipant };
9416 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9417 mReadPendingIntent, participants, mLatestTimestamp);
9418 }
9419 }
9420 }
9421
9422 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009423 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9424 * with a TV extension:
9425 *
9426 * <ol>
9427 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9428 * <li>Create a {@link TvExtender}.
9429 * <li>Set TV-specific properties using the {@code set} methods of
9430 * {@link TvExtender}.
9431 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9432 * to apply the extension to a notification.
9433 * </ol>
9434 *
9435 * <pre class="prettyprint">
9436 * Notification notification = new Notification.Builder(context)
9437 * ...
9438 * .extend(new TvExtender()
9439 * .set*(...))
9440 * .build();
9441 * </pre>
9442 *
9443 * <p>TV extensions can be accessed on an existing notification by using the
9444 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9445 * to access values.
9446 *
9447 * @hide
9448 */
9449 @SystemApi
9450 public static final class TvExtender implements Extender {
9451 private static final String TAG = "TvExtender";
9452
9453 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9454 private static final String EXTRA_FLAGS = "flags";
9455 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9456 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009457 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009458 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009459
9460 // Flags bitwise-ored to mFlags
9461 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9462
9463 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009464 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009465 private PendingIntent mContentIntent;
9466 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009467 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009468
9469 /**
9470 * Create a {@link TvExtender} with default options.
9471 */
9472 public TvExtender() {
9473 mFlags = FLAG_AVAILABLE_ON_TV;
9474 }
9475
9476 /**
9477 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9478 *
9479 * @param notif The notification from which to copy options.
9480 */
9481 public TvExtender(Notification notif) {
9482 Bundle bundle = notif.extras == null ?
9483 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9484 if (bundle != null) {
9485 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009486 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009487 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009488 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9489 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9490 }
9491 }
9492
9493 /**
9494 * Apply a TV extension to a notification that is being built. This is typically called by
9495 * the {@link Notification.Builder#extend(Notification.Extender)}
9496 * method of {@link Notification.Builder}.
9497 */
9498 @Override
9499 public Notification.Builder extend(Notification.Builder builder) {
9500 Bundle bundle = new Bundle();
9501
9502 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009503 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009504 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009505 if (mContentIntent != null) {
9506 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9507 }
9508
9509 if (mDeleteIntent != null) {
9510 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9511 }
9512
9513 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9514 return builder;
9515 }
9516
9517 /**
9518 * Returns true if this notification should be shown on TV. This method return true
9519 * if the notification was extended with a TvExtender.
9520 */
9521 public boolean isAvailableOnTv() {
9522 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9523 }
9524
9525 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009526 * Specifies the channel the notification should be delivered on when shown on TV.
9527 * It can be different from the channel that the notification is delivered to when
9528 * posting on a non-TV device.
9529 */
9530 public TvExtender setChannel(String channelId) {
9531 mChannelId = channelId;
9532 return this;
9533 }
9534
9535 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009536 * Specifies the channel the notification should be delivered on when shown on TV.
9537 * It can be different from the channel that the notification is delivered to when
9538 * posting on a non-TV device.
9539 */
9540 public TvExtender setChannelId(String channelId) {
9541 mChannelId = channelId;
9542 return this;
9543 }
9544
Jeff Sharkey000ce802017-04-29 13:13:27 -06009545 /** @removed */
9546 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009547 public String getChannel() {
9548 return mChannelId;
9549 }
9550
9551 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009552 * Returns the id of the channel this notification posts to on TV.
9553 */
9554 public String getChannelId() {
9555 return mChannelId;
9556 }
9557
9558 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009559 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9560 * If provided, it is used instead of the content intent specified
9561 * at the level of Notification.
9562 */
9563 public TvExtender setContentIntent(PendingIntent intent) {
9564 mContentIntent = intent;
9565 return this;
9566 }
9567
9568 /**
9569 * Returns the TV-specific content intent. If this method returns null, the
9570 * main content intent on the notification should be used.
9571 *
9572 * @see {@link Notification#contentIntent}
9573 */
9574 public PendingIntent getContentIntent() {
9575 return mContentIntent;
9576 }
9577
9578 /**
9579 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9580 * by the user on TV. If provided, it is used instead of the delete intent specified
9581 * at the level of Notification.
9582 */
9583 public TvExtender setDeleteIntent(PendingIntent intent) {
9584 mDeleteIntent = intent;
9585 return this;
9586 }
9587
9588 /**
9589 * Returns the TV-specific delete intent. If this method returns null, the
9590 * main delete intent on the notification should be used.
9591 *
9592 * @see {@link Notification#deleteIntent}
9593 */
9594 public PendingIntent getDeleteIntent() {
9595 return mDeleteIntent;
9596 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009597
9598 /**
9599 * Specifies whether this notification should suppress showing a message over top of apps
9600 * outside of the launcher.
9601 */
9602 public TvExtender setSuppressShowOverApps(boolean suppress) {
9603 mSuppressShowOverApps = suppress;
9604 return this;
9605 }
9606
9607 /**
9608 * Returns true if this notification should not show messages over top of apps
9609 * outside of the launcher.
9610 */
9611 public boolean getSuppressShowOverApps() {
9612 return mSuppressShowOverApps;
9613 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009614 }
9615
9616 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009617 * Get an array of Notification objects from a parcelable array bundle field.
9618 * Update the bundle to have a typed array so fetches in the future don't need
9619 * to do an array copy.
9620 */
9621 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9622 Parcelable[] array = bundle.getParcelableArray(key);
9623 if (array instanceof Notification[] || array == null) {
9624 return (Notification[]) array;
9625 }
9626 Notification[] typedArray = Arrays.copyOf(array, array.length,
9627 Notification[].class);
9628 bundle.putParcelableArray(key, typedArray);
9629 return typedArray;
9630 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009631
9632 private static class BuilderRemoteViews extends RemoteViews {
9633 public BuilderRemoteViews(Parcel parcel) {
9634 super(parcel);
9635 }
9636
Kenny Guy77320062014-08-27 21:37:15 +01009637 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9638 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009639 }
9640
9641 @Override
9642 public BuilderRemoteViews clone() {
9643 Parcel p = Parcel.obtain();
9644 writeToParcel(p, 0);
9645 p.setDataPosition(0);
9646 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9647 p.recycle();
9648 return brv;
9649 }
9650 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009651
Selim Cinek384804b2018-04-18 14:31:07 +08009652 /**
9653 * A result object where information about the template that was created is saved.
9654 */
9655 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009656 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009657
9658 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009659 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009660 * with the right actions.
9661 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009662 public int getIconMarginEnd() {
9663 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009664 }
9665
Selim Cinek1c72fa02018-04-23 18:00:54 +08009666 public void setIconMarginEnd(int iconMarginEnd) {
9667 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009668 }
9669 }
9670
Adrian Roos70d7aa32017-01-11 15:39:06 -08009671 private static class StandardTemplateParams {
9672 boolean hasProgress = true;
9673 boolean ambient = false;
9674 CharSequence title;
9675 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009676 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009677 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009678 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009679 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009680
9681 final StandardTemplateParams reset() {
9682 hasProgress = true;
9683 ambient = false;
9684 title = null;
9685 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009686 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009687 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009688 return this;
9689 }
9690
9691 final StandardTemplateParams hasProgress(boolean hasProgress) {
9692 this.hasProgress = hasProgress;
9693 return this;
9694 }
9695
9696 final StandardTemplateParams title(CharSequence title) {
9697 this.title = title;
9698 return this;
9699 }
9700
9701 final StandardTemplateParams text(CharSequence text) {
9702 this.text = text;
9703 return this;
9704 }
9705
Selim Cinekafeed292017-12-12 17:32:44 -08009706 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9707 this.headerTextSecondary = text;
9708 return this;
9709 }
9710
Selim Cinek384804b2018-04-18 14:31:07 +08009711 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9712 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009713 return this;
9714 }
9715
Selim Cinek384804b2018-04-18 14:31:07 +08009716 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9717 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009718 return this;
9719 }
9720
Adrian Roos70d7aa32017-01-11 15:39:06 -08009721 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009722 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009723 this.ambient = ambient;
9724 return this;
9725 }
9726
9727 final StandardTemplateParams fillTextsFrom(Builder b) {
9728 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009729 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9730
9731 // Big text notifications should contain their content when viewed in ambient mode.
9732 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9733 if (!ambient || TextUtils.isEmpty(text)) {
9734 text = extras.getCharSequence(EXTRA_TEXT);
9735 }
9736 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009737 return this;
9738 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009739
9740 /**
9741 * Set the maximum lines of remote input history lines allowed.
9742 * @param maxRemoteInputHistory The number of lines.
9743 * @return The builder for method chaining.
9744 */
9745 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9746 this.maxRemoteInputHistory = maxRemoteInputHistory;
9747 return this;
9748 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750}