blob: fce74496d9c4ca4ffed2505bcae94fb0c588bf7b [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
Mady Mellor8a529e22019-03-25 17:37:55 -070019import static android.annotation.Dimension.DP;
Mady Mellor9848a6c2019-03-19 15:29:05 -070020
Lucas Dupina291d192018-06-07 13:59:42 -070021import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070022
Tor Norbye80756e32015-03-02 09:39:27 -080023import android.annotation.ColorInt;
Mady Mellor7af771a2019-03-07 15:04:54 -080024import android.annotation.DimenRes;
Mady Mellor8a529e22019-03-25 17:37:55 -070025import android.annotation.Dimension;
Tor Norbye7b9c9122013-05-30 16:48:33 -070026import android.annotation.DrawableRes;
Gus Prevas9cc96602018-10-11 11:24:23 -040027import android.annotation.IdRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070028import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040029import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070030import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060031import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040032import android.annotation.SdkConstant;
33import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040034import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010035import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.Context;
37import android.content.Intent;
Felipe Leme90205ef2019-03-05 09:59:52 -080038import android.content.LocusId;
Kenny Guy77320062014-08-27 21:37:15 +010039import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040040import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020041import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050042import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020043import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070044import android.content.res.Configuration;
45import android.content.res.Resources;
Gus Prevas9cc96602018-10-11 11:24:23 -040046import android.content.res.TypedArray;
Joe Onoratoef1e7762010-09-17 18:38:38 -040047import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010048import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070049import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010050import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010051import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040052import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040053import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070054import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080055import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070056import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040058import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020059import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050060import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070061import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Parcel;
63import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040064import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080065import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070066import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070067import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080068import android.text.SpannableStringBuilder;
69import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080071import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080072import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070073import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080074import android.text.style.RelativeSizeSpan;
75import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070076import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040077import android.util.Log;
Tony Mak638430e2018-10-08 19:19:10 +010078import android.util.Pair;
Dan Sandler50128532015-12-08 15:42:41 -050079import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070080import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070081import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080082import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080083import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070084import android.view.ViewGroup;
Felipe Leme90205ef2019-03-05 09:59:52 -080085import android.view.contentcapture.ContentCaptureContext;
Jeff Sharkey1c400132011-08-05 14:50:13 -070086import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.widget.RemoteViews;
88
Griff Hazen959591e2014-05-15 22:26:18 -070089import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070090import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070091import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070092import com.android.internal.util.ContrastColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070093
Tor Norbyed9273d62013-05-30 15:59:53 -070094import java.lang.annotation.Retention;
95import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020096import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070098import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070099import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -0700100import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -0500101import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -0500102import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -0600103import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -0800104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105/**
106 * A class that represents how a persistent notification is to be presented to
107 * the user using the {@link android.app.NotificationManager}.
108 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800109 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
110 * easier to construct Notifications.</p>
111 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700112 * <div class="special reference">
113 * <h3>Developer Guides</h3>
114 * <p>For a guide to creating notifications, read the
115 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
116 * developer guide.</p>
117 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 */
119public class Notification implements Parcelable
120{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400121 private static final String TAG = "Notification";
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400124 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400126 */
127 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
128 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
129 = "android.intent.category.NOTIFICATION_PREFERENCES";
130
131 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500132 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
133 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400134 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500135 */
136 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
137
138 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400139 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400140 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
141 * what settings should be shown in the target app.
142 */
143 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
144
145 /**
146 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400147 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
148 * that can be used to narrow down what settings should be shown in the target app.
149 */
150 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
151
152 /**
153 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
154 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
155 * that can be used to narrow down what settings should be shown in the target app.
156 */
157 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Use all default values (where applicable).
161 */
162 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /**
165 * Use the default notification sound. This will ignore any given
166 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 *
Chris Wren47c20a12014-06-18 17:27:29 -0400168 * <p>
169 * A notification that is noisy is more likely to be presented as a heads-up notification.
170 * </p>
171 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 */
174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final int DEFAULT_SOUND = 1;
176
177 /**
178 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700180 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181 *
Chris Wren47c20a12014-06-18 17:27:29 -0400182 * <p>
183 * A notification that vibrates is more likely to be presented as a heads-up notification.
184 * </p>
185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187 */
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
192 * Use the default notification lights. This will ignore the
193 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
194 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500197 */
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200202 * Maximum length of CharSequences accepted by Builder and friends.
203 *
204 * <p>
205 * Avoids spamming the system with overly large strings such as full e-mails.
206 */
207 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
208
209 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800210 * Maximum entries of reply text that are accepted by Builder and friends.
211 */
212 private static final int MAX_REPLY_HISTORY = 5;
213
Tony Mak09db2ea2018-06-27 18:12:48 +0100214 /**
Gustav Sennton1463d832018-11-06 16:12:48 +0000215 * Maximum number of (generic) action buttons in a notification (contextual action buttons are
216 * handled separately).
Tony Mak09db2ea2018-06-27 18:12:48 +0100217 * @hide
218 */
219 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800220
221 /**
222 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
223 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
224 *
225 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
226 * sends messages.</p>
227 */
228 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
229
Adrian Roose458aa82015-12-08 16:17:19 -0800230 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500231 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500233 * Default value: {@link System#currentTimeMillis() Now}.
234 *
235 * Choose a timestamp that will be most relevant to the user. For most finite events, this
236 * corresponds to the time the event happened (or will happen, in the case of events that have
237 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800238 * timestamped according to when the activity began.
239 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500240 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800241 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500242 * <ul>
243 * <li>Notification of a new chat message should be stamped when the message was received.</li>
244 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
245 * <li>Notification of a completed file download should be stamped when the download finished.</li>
246 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
247 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
248 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800249 * </ul>
250 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700251 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
252 * anymore by default and must be opted into by using
253 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 */
255 public long when;
256
257 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700258 * The creation time of the notification
259 */
260 private long creationTime;
261
262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400264 *
265 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 */
Dan Sandler86647982015-05-13 23:41:13 -0400267 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700268 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public int icon;
270
271 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800272 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
273 * leave it at its default value of 0.
274 *
275 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700276 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800277 */
278 public int iconLevel;
279
280 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500281 * The number of events that this notification represents. For example, in a new mail
282 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800283 *
Julia Reynolds30331982017-04-27 10:12:50 -0400284 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500285 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
286 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 */
Julia Reynolds30331982017-04-27 10:12:50 -0400288 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 /**
291 * The intent to execute when the expanded status entry is clicked. If
292 * this is an activity, it must include the
293 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800294 * that you take care of task management as described in the
295 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800296 * Stack</a> document. In particular, make sure to read the notification section
297 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
298 * Notifications</a> for the correct ways to launch an application from a
299 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
301 public PendingIntent contentIntent;
302
303 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500304 * The intent to execute when the notification is explicitly dismissed by the user, either with
305 * the "Clear All" button or by swiping it away individually.
306 *
307 * This probably shouldn't be launching an activity since several of those will be sent
308 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 */
310 public PendingIntent deleteIntent;
311
312 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700313 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800314 *
Chris Wren47c20a12014-06-18 17:27:29 -0400315 * <p>
316 * The system UI may choose to display a heads-up notification, instead of
317 * launching this intent, while the user is using the device.
318 * </p>
319 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800320 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400321 */
322 public PendingIntent fullScreenIntent;
323
324 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400325 * Text that summarizes this notification for accessibility services.
326 *
327 * As of the L release, this text is no longer shown on screen, but it is still useful to
328 * accessibility services (where it serves as an audible announcement of the notification's
329 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400330 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800331 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 */
333 public CharSequence tickerText;
334
335 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400336 * Formerly, a view showing the {@link #tickerText}.
337 *
338 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400339 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400340 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800341 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400342
343 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 * The view that will represent this notification in the notification list (which is pulled
345 * down from the status bar).
346 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500347 * As of N, this field may be null. The notification view is determined by the inputs
348 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400349 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400351 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public RemoteViews contentView;
353
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400354 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400355 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400356 * opportunity to show more detail. The system UI may choose to show this
357 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400358 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500359 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400360 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
361 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400362 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400363 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400364 public RemoteViews bigContentView;
365
Chris Wren8fd39ec2014-02-27 17:43:26 -0500366
367 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400368 * A medium-format version of {@link #contentView}, providing the Notification an
369 * opportunity to add action buttons to contentView. At its discretion, the system UI may
370 * choose to show this as a heads-up notification, which will pop up so the user can see
371 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400372 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500373 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400374 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
375 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500376 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400377 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500378 public RemoteViews headsUpContentView;
379
Julia Reynoldsfc640012018-02-21 12:25:27 -0500380 private boolean mUsesStandardHeader;
381
382 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
383 static {
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
385 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
386 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
387 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
388 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
389 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
390 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
391 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500392 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500393 }
394
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400395 /**
Dan Sandler86647982015-05-13 23:41:13 -0400396 * A large bitmap to be shown in the notification content area.
397 *
398 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 */
Dan Sandler86647982015-05-13 23:41:13 -0400400 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800401 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
403 /**
404 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500405 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400407 * A notification that is noisy is more likely to be presented as a heads-up notification.
408 * </p>
409 *
410 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500411 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500413 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500415 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 public Uri sound;
417
418 /**
419 * Use this constant as the value for audioStreamType to request that
420 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700421 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400422 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500423 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700425 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 public static final int STREAM_DEFAULT = -1;
427
428 /**
429 * The audio stream type to use when playing the sound.
430 * Should be one of the STREAM_ constants from
431 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400432 *
433 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700435 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 public int audioStreamType = STREAM_DEFAULT;
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400439 * The default value of {@link #audioAttributes}.
440 */
441 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
442 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
443 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
444 .build();
445
446 /**
447 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 *
449 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400450 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500451 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400452 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
453
454 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500455 * The pattern with which to vibrate.
456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * <p>
458 * To vibrate the default pattern, see {@link #defaults}.
459 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500460 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500462 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500464 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public long[] vibrate;
466
467 /**
468 * The color of the led. The hardware will do its best approximation.
469 *
470 * @see #FLAG_SHOW_LIGHTS
471 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500472 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
Tor Norbye80756e32015-03-02 09:39:27 -0800474 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500475 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 public int ledARGB;
477
478 /**
479 * The number of milliseconds for the LED to be on while it's flashing.
480 * The hardware will do its best approximation.
481 *
482 * @see #FLAG_SHOW_LIGHTS
483 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500484 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500486 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public int ledOnMS;
488
489 /**
490 * The number of milliseconds for the LED to be off while it's flashing.
491 * The hardware will do its best approximation.
492 *
493 * @see #FLAG_SHOW_LIGHTS
494 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500495 *
496 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500498 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public int ledOffMS;
500
501 /**
502 * Specifies which values should be taken from the defaults.
503 * <p>
504 * To set, OR the desired from {@link #DEFAULT_SOUND},
505 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
506 * values, use {@link #DEFAULT_ALL}.
507 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500508 *
509 * @deprecated use {@link NotificationChannel#getSound()} and
510 * {@link NotificationChannel#shouldShowLights()} and
511 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500513 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 public int defaults;
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /**
517 * Bit to be bitwise-ored into the {@link #flags} field that should be
518 * set if you want the LED on for this notification.
519 * <ul>
520 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
521 * or 0 for both ledOnMS and ledOffMS.</li>
522 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
523 * <li>To flash the LED, pass the number of milliseconds that it should
524 * be on and off to ledOnMS and ledOffMS.</li>
525 * </ul>
526 * <p>
527 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800528 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 * because they will be set to values that work on any given hardware.
530 * <p>
531 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500532 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500533 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500535 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
537
538 /**
539 * Bit to be bitwise-ored into the {@link #flags} field that should be
540 * set if this notification is in reference to something that is ongoing,
541 * like a phone call. It should not be set if this notification is in
542 * reference to something that happened at a particular point in time,
543 * like a missed phone call.
544 */
545 public static final int FLAG_ONGOING_EVENT = 0x00000002;
546
547 /**
548 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700549 * the audio will be repeated until the notification is
550 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 */
552 public static final int FLAG_INSISTENT = 0x00000004;
553
554 /**
555 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700556 * set if you would only like the sound, vibrate and ticker to be played
557 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 */
559 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
560
561 /**
562 * Bit to be bitwise-ored into the {@link #flags} field that should be
563 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500564 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 */
566 public static final int FLAG_AUTO_CANCEL = 0x00000010;
567
568 /**
569 * Bit to be bitwise-ored into the {@link #flags} field that should be
570 * set if the notification should not be canceled when the user clicks
571 * the Clear all button.
572 */
573 public static final int FLAG_NO_CLEAR = 0x00000020;
574
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700575 /**
576 * Bit to be bitwise-ored into the {@link #flags} field that should be
577 * set if this notification represents a currently running service. This
578 * will normally be set for you by {@link Service#startForeground}.
579 */
580 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
581
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400582 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800584 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500585 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400586 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700587 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500588 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400589
Griff Hazendfcb0802014-02-11 12:00:00 -0800590 /**
591 * Bit to be bitswise-ored into the {@link #flags} field that should be
592 * set if this notification is relevant to the current device only
593 * and it is not recommended that it bridge to other devices.
594 */
595 public static final int FLAG_LOCAL_ONLY = 0x00000100;
596
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700597 /**
598 * Bit to be bitswise-ored into the {@link #flags} field that should be
599 * set if this notification is the group summary for a group of notifications.
600 * Grouped notifications may display in a cluster or stack on devices which
601 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
602 */
603 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
604
Julia Reynoldse46bb372016-03-17 11:05:58 -0400605 /**
606 * Bit to be bitswise-ored into the {@link #flags} field that should be
607 * set if this notification is the group summary for an auto-group of notifications.
608 *
609 * @hide
610 */
611 @SystemApi
612 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
613
Julia Reynolds4db59552017-06-30 13:34:01 -0400614 /**
615 * @hide
616 */
617 public static final int FLAG_CAN_COLORIZE = 0x00000800;
618
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700619 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400620 * Bit to be bitswised-ored into the {@link #flags} field that should be
621 * set by the system if this notification is showing as a bubble.
Mady Mellor65dcaa92019-04-03 12:21:44 -0700622 *
Julia Tuttle28fb1a92019-05-03 18:07:45 -0400623 * Applications cannot set this flag directly; they should instead call
624 * {@link Notification.Builder#setBubbleMetadata(BubbleMetadata)} to
625 * request that a notification be displayed as a bubble, and then check
626 * this flag to see whether that request was honored by the system.
Mady Mellor7eb18ef2019-03-27 14:03:46 -0700627 */
628 public static final int FLAG_BUBBLE = 0x00001000;
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public int flags;
631
Tor Norbyed9273d62013-05-30 15:59:53 -0700632 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700633 @IntDef(prefix = { "PRIORITY_" }, value = {
634 PRIORITY_DEFAULT,
635 PRIORITY_LOW,
636 PRIORITY_MIN,
637 PRIORITY_HIGH,
638 PRIORITY_MAX
639 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700640 @Retention(RetentionPolicy.SOURCE)
641 public @interface Priority {}
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 * Default notification {@link #priority}. If your application does not prioritize its own
645 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500646 *
647 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500649 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 public static final int PRIORITY_DEFAULT = 0;
651
652 /**
653 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
654 * items smaller, or at a different position in the list, compared with your app's
655 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500656 *
657 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500659 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 public static final int PRIORITY_LOW = -1;
661
662 /**
663 * Lowest {@link #priority}; these items might not be shown to the user except under special
664 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500665 *
666 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500668 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 public static final int PRIORITY_MIN = -2;
670
671 /**
672 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
673 * show these items larger, or at a different position in notification lists, compared with
674 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500675 *
676 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500677 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500678 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500679 public static final int PRIORITY_HIGH = 1;
680
681 /**
682 * Highest {@link #priority}, for your application's most important items that require the
683 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500684 *
685 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500686 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500687 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500688 public static final int PRIORITY_MAX = 2;
689
690 /**
691 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800692 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693 * Priority is an indication of how much of the user's valuable attention should be consumed by
694 * this notification. Low-priority notifications may be hidden from the user in certain
695 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500696 * system will make a determination about how to interpret this priority when presenting
697 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400698 *
699 * <p>
700 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
701 * as a heads-up notification.
702 * </p>
703 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500704 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500705 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700706 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500707 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500708 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800709
Dan Sandler26e81cf2014-05-06 10:01:27 -0400710 /**
711 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
712 * to be applied by the standard Style templates when presenting this notification.
713 *
714 * The current template design constructs a colorful header image by overlaying the
715 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
716 * ignored.
717 */
Tor Norbye80756e32015-03-02 09:39:27 -0800718 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400719 public int color = COLOR_DEFAULT;
720
721 /**
722 * Special value of {@link #color} telling the system not to decorate this notification with
723 * any special color but instead use default colors when presenting this notification.
724 */
Tor Norbye80756e32015-03-02 09:39:27 -0800725 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400726 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600727
728 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800729 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800730 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800731 */
732 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800733 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800734
735 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700736 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
737 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600738 * lockscreen).
739 *
740 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
741 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
742 * shown in all situations, but the contents are only available if the device is unlocked for
743 * the appropriate user.
744 *
745 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
746 * can be read even in an "insecure" context (that is, above a secure lockscreen).
747 * To modify the public version of this notification—for example, to redact some portions—see
748 * {@link Builder#setPublicVersion(Notification)}.
749 *
750 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
751 * and ticker until the user has bypassed the lockscreen.
752 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600753 public @Visibility int visibility;
754
755 /** @hide */
756 @IntDef(prefix = { "VISIBILITY_" }, value = {
757 VISIBILITY_PUBLIC,
758 VISIBILITY_PRIVATE,
759 VISIBILITY_SECRET,
760 })
761 @Retention(RetentionPolicy.SOURCE)
762 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600763
Griff Hazenfc3922d2014-08-20 11:56:44 -0700764 /**
765 * Notification visibility: Show this notification in its entirety on all lockscreens.
766 *
767 * {@see #visibility}
768 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600769 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700770
771 /**
772 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
773 * private information on secure lockscreens.
774 *
775 * {@see #visibility}
776 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600777 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700778
779 /**
780 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
781 *
782 * {@see #visibility}
783 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600784 public static final int VISIBILITY_SECRET = -1;
785
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700792 * Notification category: map turn-by-turn navigation.
793 */
794 public static final String CATEGORY_NAVIGATION = "navigation";
795
796 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400797 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500798 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400799 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800
801 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400802 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500803 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500805
806 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400807 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400809 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500810
811 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400812 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500813 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400814 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500815
816 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400817 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500818 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400819 public static final String CATEGORY_ALARM = "alarm";
820
821 /**
822 * Notification category: progress of a long-running background operation.
823 */
824 public static final String CATEGORY_PROGRESS = "progress";
825
826 /**
827 * Notification category: social network or sharing update.
828 */
829 public static final String CATEGORY_SOCIAL = "social";
830
831 /**
832 * Notification category: error in background operation or authentication status.
833 */
834 public static final String CATEGORY_ERROR = "err";
835
836 /**
837 * Notification category: media transport control for playback.
838 */
839 public static final String CATEGORY_TRANSPORT = "transport";
840
841 /**
842 * Notification category: system or device status update. Reserved for system use.
843 */
844 public static final String CATEGORY_SYSTEM = "sys";
845
846 /**
847 * Notification category: indication of running background service.
848 */
849 public static final String CATEGORY_SERVICE = "service";
850
851 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400852 * Notification category: a specific, timely recommendation for a single thing.
853 * For example, a news app might want to recommend a news story it believes the user will
854 * want to read next.
855 */
856 public static final String CATEGORY_RECOMMENDATION = "recommendation";
857
858 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400859 * Notification category: ongoing information about device or contextual status.
860 */
861 public static final String CATEGORY_STATUS = "status";
862
863 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400864 * Notification category: user-scheduled reminder.
865 */
866 public static final String CATEGORY_REMINDER = "reminder";
867
868 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700869 * Notification category: extreme car emergencies.
870 * @hide
871 */
872 @SystemApi
873 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
874
875 /**
876 * Notification category: car warnings.
877 * @hide
878 */
879 @SystemApi
880 public static final String CATEGORY_CAR_WARNING = "car_warning";
881
882 /**
883 * Notification category: general car system information.
884 * @hide
885 */
886 @SystemApi
887 public static final String CATEGORY_CAR_INFORMATION = "car_information";
888
889 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400890 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
891 * that best describes this Notification. May be used by the system for ranking and filtering.
892 */
893 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500894
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100895 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700896 private String mGroupKey;
897
898 /**
899 * Get the key used to group this notification into a cluster or stack
900 * with other notifications on devices which support such rendering.
901 */
902 public String getGroup() {
903 return mGroupKey;
904 }
905
906 private String mSortKey;
907
908 /**
909 * Get a sort key that orders this notification among other notifications from the
910 * same package. This can be useful if an external sort was already applied and an app
911 * would like to preserve this. Notifications will be sorted lexicographically using this
912 * value, although providing different priorities in addition to providing sort key may
913 * cause this value to be ignored.
914 *
915 * <p>This sort key can also be used to order members of a notification group. See
916 * {@link Builder#setGroup}.
917 *
918 * @see String#compareTo(String)
919 */
920 public String getSortKey() {
921 return mSortKey;
922 }
923
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500924 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400925 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400926 * <p>
927 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
928 * APIs, and are intended to be used by
929 * {@link android.service.notification.NotificationListenerService} implementations to extract
930 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500931 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400932 public Bundle extras = new Bundle();
933
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700935 * All pending intents in the notification as the system needs to be able to access them but
936 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700937 * custom parcelable objects.
938 *
939 * @hide
940 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100941 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700942 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700943
944 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700945 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
946 * pending intents inside of it, so only those will get the behavior.
947 *
948 * @hide
949 */
Adrian Roosfb921842017-10-26 14:49:56 +0200950 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700951
952 /**
953 * Must be set by a process to start associating tokens with Notification objects
954 * coming in to it. This is set by NotificationManagerService.
955 *
956 * @hide
957 */
958 static public IBinder processWhitelistToken;
959
960 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400961 * {@link #extras} key: this is the title of the notification,
962 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
963 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500964 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400965
966 /**
967 * {@link #extras} key: this is the title of the notification when shown in expanded form,
968 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
969 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400970 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400971
972 /**
973 * {@link #extras} key: this is the main text payload, as supplied to
974 * {@link Builder#setContentText(CharSequence)}.
975 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500976 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400977
978 /**
979 * {@link #extras} key: this is a third line of text, as supplied to
980 * {@link Builder#setSubText(CharSequence)}.
981 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400982 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983
984 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800985 * {@link #extras} key: this is the remote input history, as supplied to
986 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700987 *
988 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
989 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
990 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
991 * notifications once the other party has responded).
992 *
993 * The extra with this key is of type CharSequence[] and contains the most recent entry at
994 * the 0 index, the second most recent at the 1 index, etc.
995 *
996 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800997 */
998 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
999
1000 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01001001 * {@link #extras} key: boolean as supplied to
1002 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
1003 *
1004 * If set to true, then the view displaying the remote input history from
1005 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
1006 *
1007 * @see Builder#setShowRemoteInputSpinner(boolean)
1008 * @hide
1009 */
1010 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
1011
1012 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01001013 * {@link #extras} key: boolean as supplied to
1014 * {@link Builder#setHideSmartReplies(boolean)}.
1015 *
1016 * If set to true, then any smart reply buttons will be hidden.
1017 *
1018 * @see Builder#setHideSmartReplies(boolean)
1019 * @hide
1020 */
1021 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1022
1023 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001024 * {@link #extras} key: this is a small piece of additional text as supplied to
1025 * {@link Builder#setContentInfo(CharSequence)}.
1026 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001027 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001028
1029 /**
1030 * {@link #extras} key: this is a line of summary information intended to be shown
1031 * alongside expanded notifications, as supplied to (e.g.)
1032 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1033 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001034 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001035
1036 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001037 * {@link #extras} key: this is the longer text shown in the big form of a
1038 * {@link BigTextStyle} notification, as supplied to
1039 * {@link BigTextStyle#bigText(CharSequence)}.
1040 */
1041 public static final String EXTRA_BIG_TEXT = "android.bigText";
1042
1043 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1045 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001046 *
1047 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001049 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001050 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001051
1052 /**
1053 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1054 * notification payload, as
1055 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001056 *
1057 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001058 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001059 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001060 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001061
1062 /**
1063 * {@link #extras} key: this is a bitmap to be used instead of the one from
1064 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1065 * shown in its expanded form, as supplied to
1066 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1067 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001068 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001069
1070 /**
1071 * {@link #extras} key: this is the progress value supplied to
1072 * {@link Builder#setProgress(int, int, boolean)}.
1073 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001074 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001075
1076 /**
1077 * {@link #extras} key: this is the maximum value supplied to
1078 * {@link Builder#setProgress(int, int, boolean)}.
1079 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001080 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001081
1082 /**
1083 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1084 * {@link Builder#setProgress(int, int, boolean)}.
1085 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001086 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001087
1088 /**
1089 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1090 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1091 * {@link Builder#setUsesChronometer(boolean)}.
1092 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001093 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001094
1095 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001096 * {@link #extras} key: whether the chronometer set on the notification should count down
1097 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001098 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001099 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001100 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001101
1102 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001103 * {@link #extras} key: whether {@link #when} should be shown,
1104 * as supplied to {@link Builder#setShowWhen(boolean)}.
1105 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001106 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001107
1108 /**
1109 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1110 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1111 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001112 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001113
1114 /**
1115 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1116 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1117 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001118 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001119
1120 /**
1121 * {@link #extras} key: A string representing the name of the specific
1122 * {@link android.app.Notification.Style} used to create this notification.
1123 */
Chris Wren91ad5632013-06-05 15:05:57 -04001124 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001125
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001126 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001127 * {@link #extras} key: A String array containing the people that this notification relates to,
1128 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001129 *
1130 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001131 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001132 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001133
1134 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001135 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1136 * this notification relates to.
1137 */
1138 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1139
1140 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001141 * Allow certain system-generated notifications to appear before the device is provisioned.
1142 * Only available to notifications coming from the android package.
1143 * @hide
1144 */
Maurice Lam96c10032017-03-29 15:42:38 -07001145 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001146 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001147 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1148
1149 /**
Robin Leed107af62018-04-27 13:55:56 +02001150 * {@link #extras} key:
1151 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1152 * pointing to an image that can be displayed in the background when the notification is
1153 * selected. Used on television platforms. The URI must point to an image stream suitable for
1154 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001155 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001156 */
1157 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1158
1159 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001160 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001161 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001162 * {@link android.app.Notification.MediaStyle} notification.
1163 */
1164 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1165
1166 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001167 * {@link #extras} key: the indices of actions to be shown in the compact view,
1168 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1169 */
1170 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1171
Christoph Studer943aa672014-08-03 20:31:16 +02001172 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001173 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1174 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001175 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1176 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001177 *
1178 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001179 */
1180 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1181
1182 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001183 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1184 * direct replies
1185 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1186 * {@link Person}
1187 */
1188 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1189
1190 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001191 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001192 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001193 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001194 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001195
1196 /**
1197 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1198 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001199 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1200 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001201 */
1202 public static final String EXTRA_MESSAGES = "android.messages";
1203
1204 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001205 * {@link #extras} key: an array of
1206 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1207 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1208 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1209 * array of bundles.
1210 */
1211 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1212
1213 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001214 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1215 * represents a group conversation.
1216 */
1217 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1218
1219 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001220 * {@link #extras} key: whether the notification should be colorized as
Gustav Senntondf4c2442019-03-18 11:41:11 +00001221 * supplied to {@link Builder#setColorized(boolean)}.
Selim Cinek7b9605b2017-01-19 17:36:00 -08001222 */
1223 public static final String EXTRA_COLORIZED = "android.colorized";
1224
1225 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001226 * @hide
1227 */
1228 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1229
Selim Cinek247fa012016-02-18 09:50:48 -08001230 /**
1231 * @hide
1232 */
1233 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1234
Shane Brennan472a3b32016-12-12 15:28:10 -08001235 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001236 * @hide
1237 */
1238 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1239
1240 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001241 * {@link #extras} key: the audio contents of this notification.
1242 *
1243 * This is for use when rendering the notification on an audio-focused interface;
1244 * the audio contents are a complete sound sample that contains the contents/body of the
1245 * notification. This may be used in substitute of a Text-to-Speech reading of the
1246 * notification. For example if the notification represents a voice message this should point
1247 * to the audio of that message.
1248 *
1249 * The data stored under this key should be a String representation of a Uri that contains the
1250 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1251 *
1252 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1253 * has a field for holding data URI. That field can be used for audio.
1254 * See {@code Message#setData}.
1255 *
1256 * Example usage:
1257 * <pre>
1258 * {@code
1259 * Notification.Builder myBuilder = (build your Notification as normal);
1260 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1261 * }
1262 * </pre>
1263 */
1264 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1265
Dan Sandler80eaa592016-04-14 23:34:54 -04001266 /** @hide */
1267 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001268 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001269 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1270
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001271 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001272 * This is set on the notifications shown by system_server about apps running foreground
1273 * services. It indicates that the notification should be shown
1274 * only if any of the given apps do not already have a properly tagged
1275 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1276 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001277 * @hide
1278 */
1279 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1280
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001281 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001282 private Icon mSmallIcon;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001283 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001284 private Icon mLargeIcon;
1285
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001286 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001287 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001288 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001289
Julia Reynolds13d898c2017-02-02 12:22:05 -05001290 private String mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08001291 private LocusId mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001292 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001293
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001294 private BubbleMetadata mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001295
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001296 /** @hide */
1297 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1298 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1299 })
1300 @Retention(RetentionPolicy.SOURCE)
1301 public @interface GroupAlertBehavior {}
1302
1303 /**
1304 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1305 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1306 * notification will not be muted when it is in a group.
1307 */
1308 public static final int GROUP_ALERT_ALL = 0;
1309
1310 /**
1311 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1312 * notification in a group should be silenced (no sound or vibration) even if they are posted
1313 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001314 * mute this notification if this notification is a group child. This must be applied to all
1315 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001316 *
1317 * <p> For example, you might want to use this constant if you post a number of children
1318 * notifications at once (say, after a periodic sync), and only need to notify the user
1319 * audibly once.
1320 */
1321 public static final int GROUP_ALERT_SUMMARY = 1;
1322
1323 /**
1324 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1325 * notification in a group should be silenced (no sound or vibration) even if they are
1326 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1327 * to mute this notification if this notification is a group summary.
1328 *
1329 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001330 * in your group have content and the summary is only used to visually group notifications
1331 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001332 */
1333 public static final int GROUP_ALERT_CHILDREN = 2;
1334
Julia Reynolds2f431e22017-06-07 14:12:09 +00001335 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001336
Julia Reynolds13d898c2017-02-02 12:22:05 -05001337 /**
1338 * If this notification is being shown as a badge, always show as a number.
1339 */
1340 public static final int BADGE_ICON_NONE = 0;
1341
1342 /**
1343 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1344 * represent this notification.
1345 */
1346 public static final int BADGE_ICON_SMALL = 1;
1347
1348 /**
1349 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1350 * represent this notification.
1351 */
1352 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001353 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001354
Chris Wren51c75102013-07-16 20:49:17 -04001355 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00001356 * Determines whether the platform can generate contextual actions for a notification.
1357 */
1358 private boolean mAllowSystemGeneratedContextualActions = true;
1359
1360 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001361 * Structure to encapsulate a named action that can be shown as part of this notification.
1362 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1363 * selected by the user.
1364 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001365 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1366 * or {@link Notification.Builder#addAction(Notification.Action)}
1367 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001368 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001369 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001370 /**
1371 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1372 * {@link RemoteInput}s.
1373 *
1374 * This is intended for {@link RemoteInput}s that only accept data, meaning
1375 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
Gustav Senntondf4c2442019-03-18 11:41:11 +00001376 * is null or empty, and {@link RemoteInput#getAllowedDataTypes} is non-null and not
Shane Brennan472a3b32016-12-12 15:28:10 -08001377 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1378 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1379 *
1380 * You can test if a RemoteInput matches these constraints using
1381 * {@link RemoteInput#isDataOnly}.
1382 */
1383 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1384
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001385 /**
1386 * {@link }: No semantic action defined.
1387 */
1388 public static final int SEMANTIC_ACTION_NONE = 0;
1389
1390 /**
1391 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1392 * may be appropriate.
1393 */
1394 public static final int SEMANTIC_ACTION_REPLY = 1;
1395
1396 /**
1397 * {@code SemanticAction}: Mark content as read.
1398 */
1399 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1400
1401 /**
1402 * {@code SemanticAction}: Mark content as unread.
1403 */
1404 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1405
1406 /**
1407 * {@code SemanticAction}: Delete the content associated with the notification. This
1408 * could mean deleting an email, message, etc.
1409 */
1410 public static final int SEMANTIC_ACTION_DELETE = 4;
1411
1412 /**
1413 * {@code SemanticAction}: Archive the content associated with the notification. This
1414 * could mean archiving an email, message, etc.
1415 */
1416 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1417
1418 /**
1419 * {@code SemanticAction}: Mute the content associated with the notification. This could
1420 * mean silencing a conversation or currently playing media.
1421 */
1422 public static final int SEMANTIC_ACTION_MUTE = 6;
1423
1424 /**
1425 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1426 * mean un-silencing a conversation or currently playing media.
1427 */
1428 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1429
1430 /**
1431 * {@code SemanticAction}: Mark content with a thumbs up.
1432 */
1433 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1434
1435 /**
1436 * {@code SemanticAction}: Mark content with a thumbs down.
1437 */
1438 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1439
Kodlee Yincda5b092018-02-15 15:34:53 -08001440 /**
1441 * {@code SemanticAction}: Call a contact, group, etc.
1442 */
1443 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001444
Griff Hazen959591e2014-05-15 22:26:18 -07001445 private final Bundle mExtras;
Mathew Inwood8c854f82018-09-14 12:35:36 +01001446 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001447 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001448 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001449 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001450 private final @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001451 private final boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001452
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001453 /**
1454 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001455 *
1456 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001457 */
Dan Sandler86647982015-05-13 23:41:13 -04001458 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001459 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001460
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001461 /**
1462 * Title of the action.
1463 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001464 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001465
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001466 /**
1467 * Intent to send when the user invokes this action. May be null, in which case the action
1468 * may be rendered in a disabled presentation by the system UI.
1469 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001470 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001471
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001472 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001473 if (in.readInt() != 0) {
1474 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001475 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1476 icon = mIcon.getResId();
1477 }
Dan Sandler86647982015-05-13 23:41:13 -04001478 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001479 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1480 if (in.readInt() == 1) {
1481 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1482 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001483 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001484 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001485 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001486 mSemanticAction = in.readInt();
Gustav Sennton005d7a02019-01-04 13:41:32 +00001487 mIsContextual = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001488 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001489
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001490 /**
Dan Sandler86647982015-05-13 23:41:13 -04001491 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001492 */
Dan Sandler86647982015-05-13 23:41:13 -04001493 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001494 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001495 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001496 SEMANTIC_ACTION_NONE, false /* isContextual */);
Griff Hazen959591e2014-05-15 22:26:18 -07001497 }
1498
Adrian Roos7af53622016-10-12 13:44:05 -07001499 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001500 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001501 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001502 @SemanticAction int semanticAction, boolean isContextual) {
Dan Sandler86647982015-05-13 23:41:13 -04001503 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001504 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1505 this.icon = icon.getResId();
1506 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001507 this.title = title;
1508 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001509 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001510 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001511 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001512 this.mSemanticAction = semanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001513 this.mIsContextual = isContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001514 }
1515
1516 /**
Dan Sandler86647982015-05-13 23:41:13 -04001517 * Return an icon representing the action.
1518 */
1519 public Icon getIcon() {
1520 if (mIcon == null && icon != 0) {
1521 // you snuck an icon in here without using the builder; let's try to keep it
1522 mIcon = Icon.createWithResource("", icon);
1523 }
1524 return mIcon;
1525 }
1526
1527 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001528 * Get additional metadata carried around with this Action.
1529 */
1530 public Bundle getExtras() {
1531 return mExtras;
1532 }
1533
1534 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001535 * Return whether the platform should automatically generate possible replies for this
1536 * {@link Action}
1537 */
1538 public boolean getAllowGeneratedReplies() {
1539 return mAllowGeneratedReplies;
1540 }
1541
1542 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001543 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001544 * May return null if no remote inputs were added. Only returns inputs which accept
1545 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001546 */
1547 public RemoteInput[] getRemoteInputs() {
1548 return mRemoteInputs;
1549 }
1550
1551 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001552 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1553 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1554 * (eg. reply, mark as read, delete, etc).
1555 */
1556 public @SemanticAction int getSemanticAction() {
1557 return mSemanticAction;
1558 }
1559
1560 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001561 * Returns whether this is a contextual Action, i.e. whether the action is dependent on the
1562 * notification message body. An example of a contextual action could be an action opening a
1563 * map application with an address shown in the notification.
1564 */
1565 public boolean isContextual() {
1566 return mIsContextual;
1567 }
1568
1569 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001570 * Get the list of inputs to be collected from the user that ONLY accept data when this
1571 * action is sent. These remote inputs are guaranteed to return true on a call to
1572 * {@link RemoteInput#isDataOnly}.
1573 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001574 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001575 *
1576 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1577 * of non-textual RemoteInputs do not access these remote inputs.
1578 */
1579 public RemoteInput[] getDataOnlyRemoteInputs() {
1580 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1581 }
1582
1583 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001584 * Builder class for {@link Action} objects.
1585 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001586 public static final class Builder {
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001587 @Nullable private final Icon mIcon;
1588 @Nullable private final CharSequence mTitle;
1589 @Nullable private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001590 private boolean mAllowGeneratedReplies = true;
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001591 @NonNull private final Bundle mExtras;
1592 @Nullable private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001593 private @SemanticAction int mSemanticAction;
Gustav Sennton005d7a02019-01-04 13:41:32 +00001594 private boolean mIsContextual;
Griff Hazen959591e2014-05-15 22:26:18 -07001595
1596 /**
1597 * Construct a new builder for {@link Action} object.
1598 * @param icon icon to show for this action
1599 * @param title the title of the action
1600 * @param intent the {@link PendingIntent} to fire when users trigger this action
1601 */
Dan Sandler86647982015-05-13 23:41:13 -04001602 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001603 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001604 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001605 }
1606
1607 /**
1608 * Construct a new builder for {@link Action} object.
1609 * @param icon icon to show for this action
1610 * @param title the title of the action
1611 * @param intent the {@link PendingIntent} to fire when users trigger this action
1612 */
1613 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001614 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001615 }
1616
1617 /**
1618 * Construct a new builder for {@link Action} object using the fields from an
1619 * {@link Action}.
1620 * @param action the action to read fields from.
1621 */
1622 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001623 this(action.getIcon(), action.title, action.actionIntent,
1624 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001625 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001626 }
1627
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001628 private Builder(@Nullable Icon icon, @Nullable CharSequence title,
1629 @Nullable PendingIntent intent, @NonNull Bundle extras,
1630 @Nullable RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1631 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001632 mIcon = icon;
1633 mTitle = title;
1634 mIntent = intent;
1635 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001636 if (remoteInputs != null) {
1637 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1638 Collections.addAll(mRemoteInputs, remoteInputs);
1639 }
Adrian Roos7af53622016-10-12 13:44:05 -07001640 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001641 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001642 }
1643
1644 /**
1645 * Merge additional metadata into this builder.
1646 *
1647 * <p>Values within the Bundle will replace existing extras values in this Builder.
1648 *
1649 * @see Notification.Action#extras
1650 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001651 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001652 public Builder addExtras(Bundle extras) {
1653 if (extras != null) {
1654 mExtras.putAll(extras);
1655 }
1656 return this;
1657 }
1658
1659 /**
1660 * Get the metadata Bundle used by this Builder.
1661 *
1662 * <p>The returned Bundle is shared with this Builder.
1663 */
Gustav Senntone1fc87b2019-03-18 14:31:28 +00001664 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001665 public Bundle getExtras() {
1666 return mExtras;
1667 }
1668
1669 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001670 * Add an input to be collected from the user when this action is sent.
1671 * Response values can be retrieved from the fired intent by using the
1672 * {@link RemoteInput#getResultsFromIntent} function.
1673 * @param remoteInput a {@link RemoteInput} to add to the action
1674 * @return this object for method chaining
1675 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001676 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001677 public Builder addRemoteInput(RemoteInput remoteInput) {
1678 if (mRemoteInputs == null) {
1679 mRemoteInputs = new ArrayList<RemoteInput>();
1680 }
1681 mRemoteInputs.add(remoteInput);
1682 return this;
1683 }
1684
1685 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001686 * Set whether the platform should automatically generate possible replies to add to
1687 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1688 * {@link RemoteInput}, this has no effect.
1689 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1690 * otherwise
1691 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001692 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001693 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001694 @NonNull
Alex Hills42b0c4d2016-04-26 13:35:36 -04001695 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1696 mAllowGeneratedReplies = allowGeneratedReplies;
1697 return this;
1698 }
1699
1700 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001701 * Sets the {@code SemanticAction} for this {@link Action}. A
1702 * {@code SemanticAction} denotes what an {@link Action}'s
1703 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1704 * @param semanticAction a SemanticAction defined within {@link Action} with
1705 * {@code SEMANTIC_ACTION_} prefixes
1706 * @return this object for method chaining
1707 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001708 @NonNull
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001709 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1710 mSemanticAction = semanticAction;
1711 return this;
1712 }
1713
1714 /**
Gustav Sennton005d7a02019-01-04 13:41:32 +00001715 * Sets whether this {@link Action} is a contextual action, i.e. whether the action is
1716 * dependent on the notification message body. An example of a contextual action could
1717 * be an action opening a map application with an address shown in the notification.
1718 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001719 @NonNull
Gustav Sennton005d7a02019-01-04 13:41:32 +00001720 public Builder setContextual(boolean isContextual) {
1721 mIsContextual = isContextual;
1722 return this;
1723 }
1724
1725 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001726 * Apply an extender to this action builder. Extenders may be used to add
1727 * metadata or change options on this builder.
1728 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001729 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07001730 public Builder extend(Extender extender) {
1731 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001732 return this;
1733 }
1734
1735 /**
Gustav Senntonc98b1632018-11-23 12:26:15 +00001736 * Throws an NPE if we are building a contextual action missing one of the fields
1737 * necessary to display the action.
1738 */
1739 private void checkContextualActionNullFields() {
Gustav Sennton005d7a02019-01-04 13:41:32 +00001740 if (!mIsContextual) return;
Gustav Senntonc98b1632018-11-23 12:26:15 +00001741
1742 if (mIcon == null) {
1743 throw new NullPointerException("Contextual Actions must contain a valid icon");
1744 }
1745
1746 if (mIntent == null) {
1747 throw new NullPointerException(
1748 "Contextual Actions must contain a valid PendingIntent");
1749 }
1750 }
1751
1752 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001753 * Combine all of the options that have been set and return a new {@link Action}
1754 * object.
1755 * @return the built action
1756 */
Gustav Sennton44a23132019-02-28 18:02:52 +00001757 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07001758 public Action build() {
Gustav Senntonc98b1632018-11-23 12:26:15 +00001759 checkContextualActionNullFields();
1760
Shane Brennan472a3b32016-12-12 15:28:10 -08001761 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1762 RemoteInput[] previousDataInputs =
1763 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001764 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001765 for (RemoteInput input : previousDataInputs) {
1766 dataOnlyInputs.add(input);
1767 }
1768 }
1769 List<RemoteInput> textInputs = new ArrayList<>();
1770 if (mRemoteInputs != null) {
1771 for (RemoteInput input : mRemoteInputs) {
1772 if (input.isDataOnly()) {
1773 dataOnlyInputs.add(input);
1774 } else {
1775 textInputs.add(input);
1776 }
1777 }
1778 }
1779 if (!dataOnlyInputs.isEmpty()) {
1780 RemoteInput[] dataInputsArr =
1781 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1782 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1783 }
1784 RemoteInput[] textInputsArr = textInputs.isEmpty()
1785 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1786 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Gustav Sennton005d7a02019-01-04 13:41:32 +00001787 mAllowGeneratedReplies, mSemanticAction, mIsContextual);
Griff Hazen959591e2014-05-15 22:26:18 -07001788 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001789 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001790
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001791 @Override
1792 public Action clone() {
1793 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001794 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001795 title,
1796 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001797 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001798 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001799 getAllowGeneratedReplies(),
Gustav Sennton005d7a02019-01-04 13:41:32 +00001800 getSemanticAction(),
1801 isContextual());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001802 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001803
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001804 @Override
1805 public int describeContents() {
1806 return 0;
1807 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001808
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001809 @Override
1810 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001811 final Icon ic = getIcon();
1812 if (ic != null) {
1813 out.writeInt(1);
1814 ic.writeToParcel(out, 0);
1815 } else {
1816 out.writeInt(0);
1817 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001818 TextUtils.writeToParcel(title, out, flags);
1819 if (actionIntent != null) {
1820 out.writeInt(1);
1821 actionIntent.writeToParcel(out, flags);
1822 } else {
1823 out.writeInt(0);
1824 }
Griff Hazen959591e2014-05-15 22:26:18 -07001825 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001826 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001827 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001828 out.writeInt(mSemanticAction);
Gustav Sennton005d7a02019-01-04 13:41:32 +00001829 out.writeInt(mIsContextual ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001830 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001831
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07001832 public static final @android.annotation.NonNull Parcelable.Creator<Action> CREATOR =
Griff Hazen959591e2014-05-15 22:26:18 -07001833 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001834 public Action createFromParcel(Parcel in) {
1835 return new Action(in);
1836 }
1837 public Action[] newArray(int size) {
1838 return new Action[size];
1839 }
1840 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001841
1842 /**
1843 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1844 * metadata or change options on an action builder.
1845 */
1846 public interface Extender {
1847 /**
1848 * Apply this extender to a notification action builder.
1849 * @param builder the builder to be modified.
1850 * @return the build object for chaining.
1851 */
1852 public Builder extend(Builder builder);
1853 }
1854
1855 /**
1856 * Wearable extender for notification actions. To add extensions to an action,
1857 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1858 * the {@code WearableExtender()} constructor and apply it to a
1859 * {@link android.app.Notification.Action.Builder} using
1860 * {@link android.app.Notification.Action.Builder#extend}.
1861 *
1862 * <pre class="prettyprint">
1863 * Notification.Action action = new Notification.Action.Builder(
1864 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001865 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001866 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001867 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001868 */
1869 public static final class WearableExtender implements Extender {
1870 /** Notification action extra which contains wearable extensions */
1871 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1872
Pete Gastaf6781d2014-10-07 15:17:05 -04001873 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001874 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001875 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1876 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1877 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001878
1879 // Flags bitwise-ored to mFlags
1880 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001881 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001882 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001883
1884 // Default value for flags integer
1885 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1886
1887 private int mFlags = DEFAULT_FLAGS;
1888
Pete Gastaf6781d2014-10-07 15:17:05 -04001889 private CharSequence mInProgressLabel;
1890 private CharSequence mConfirmLabel;
1891 private CharSequence mCancelLabel;
1892
Griff Hazen61a9e862014-05-22 16:05:19 -07001893 /**
1894 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1895 * options.
1896 */
1897 public WearableExtender() {
1898 }
1899
1900 /**
1901 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1902 * wearable options present in an existing notification action.
1903 * @param action the notification action to inspect.
1904 */
1905 public WearableExtender(Action action) {
1906 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1907 if (wearableBundle != null) {
1908 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001909 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1910 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1911 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001912 }
1913 }
1914
1915 /**
1916 * Apply wearable extensions to a notification action that is being built. This is
1917 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1918 * method of {@link android.app.Notification.Action.Builder}.
1919 */
1920 @Override
1921 public Action.Builder extend(Action.Builder builder) {
1922 Bundle wearableBundle = new Bundle();
1923
1924 if (mFlags != DEFAULT_FLAGS) {
1925 wearableBundle.putInt(KEY_FLAGS, mFlags);
1926 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001927 if (mInProgressLabel != null) {
1928 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1929 }
1930 if (mConfirmLabel != null) {
1931 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1932 }
1933 if (mCancelLabel != null) {
1934 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1935 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001936
1937 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1938 return builder;
1939 }
1940
1941 @Override
1942 public WearableExtender clone() {
1943 WearableExtender that = new WearableExtender();
1944 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001945 that.mInProgressLabel = this.mInProgressLabel;
1946 that.mConfirmLabel = this.mConfirmLabel;
1947 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001948 return that;
1949 }
1950
1951 /**
1952 * Set whether this action is available when the wearable device is not connected to
1953 * a companion device. The user can still trigger this action when the wearable device is
1954 * offline, but a visual hint will indicate that the action may not be available.
1955 * Defaults to true.
1956 */
1957 public WearableExtender setAvailableOffline(boolean availableOffline) {
1958 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1959 return this;
1960 }
1961
1962 /**
1963 * Get whether this action is available when the wearable device is not connected to
1964 * a companion device. The user can still trigger this action when the wearable device is
1965 * offline, but a visual hint will indicate that the action may not be available.
1966 * Defaults to true.
1967 */
1968 public boolean isAvailableOffline() {
1969 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1970 }
1971
1972 private void setFlag(int mask, boolean value) {
1973 if (value) {
1974 mFlags |= mask;
1975 } else {
1976 mFlags &= ~mask;
1977 }
1978 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001979
1980 /**
1981 * Set a label to display while the wearable is preparing to automatically execute the
1982 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1983 *
1984 * @param label the label to display while the action is being prepared to execute
1985 * @return this object for method chaining
1986 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001987 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001988 public WearableExtender setInProgressLabel(CharSequence label) {
1989 mInProgressLabel = label;
1990 return this;
1991 }
1992
1993 /**
1994 * Get the label to display while the wearable is preparing to automatically execute
1995 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1996 *
1997 * @return the label to display while the action is being prepared to execute
1998 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001999 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002000 public CharSequence getInProgressLabel() {
2001 return mInProgressLabel;
2002 }
2003
2004 /**
2005 * Set a label to display to confirm that the action should be executed.
2006 * This is usually an imperative verb like "Send".
2007 *
2008 * @param label the label to confirm the action should be executed
2009 * @return this object for method chaining
2010 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002011 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002012 public WearableExtender setConfirmLabel(CharSequence label) {
2013 mConfirmLabel = label;
2014 return this;
2015 }
2016
2017 /**
2018 * Get the label to display to confirm that the action should be executed.
2019 * This is usually an imperative verb like "Send".
2020 *
2021 * @return the label to confirm the action should be executed
2022 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002023 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002024 public CharSequence getConfirmLabel() {
2025 return mConfirmLabel;
2026 }
2027
2028 /**
2029 * Set a label to display to cancel the action.
2030 * This is usually an imperative verb, like "Cancel".
2031 *
2032 * @param label the label to display to cancel the action
2033 * @return this object for method chaining
2034 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002035 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002036 public WearableExtender setCancelLabel(CharSequence label) {
2037 mCancelLabel = label;
2038 return this;
2039 }
2040
2041 /**
2042 * Get the label to display to cancel the action.
2043 * This is usually an imperative verb like "Cancel".
2044 *
2045 * @return the label to display to cancel the action
2046 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05002047 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04002048 public CharSequence getCancelLabel() {
2049 return mCancelLabel;
2050 }
Alex Hills9ab3a232016-04-05 14:54:56 -04002051
2052 /**
2053 * Set a hint that this Action will launch an {@link Activity} directly, telling the
2054 * platform that it can generate the appropriate transitions.
2055 * @param hintLaunchesActivity {@code true} if the content intent will launch
2056 * an activity and transitions should be generated, false otherwise.
2057 * @return this object for method chaining
2058 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002059 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04002060 boolean hintLaunchesActivity) {
2061 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
2062 return this;
2063 }
2064
2065 /**
2066 * Get a hint that this Action will launch an {@link Activity} directly, telling the
2067 * platform that it can generate the appropriate transitions
2068 * @return {@code true} if the content intent will launch an activity and transitions
2069 * should be generated, false otherwise. The default value is {@code false} if this was
2070 * never set.
2071 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04002072 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04002073 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
2074 }
Alex Hills9f087612016-06-07 09:08:59 -04002075
2076 /**
2077 * Set a hint that this Action should be displayed inline.
2078 *
2079 * @param hintDisplayInline {@code true} if action should be displayed inline, false
2080 * otherwise
2081 * @return this object for method chaining
2082 */
2083 public WearableExtender setHintDisplayActionInline(
2084 boolean hintDisplayInline) {
2085 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2086 return this;
2087 }
2088
2089 /**
2090 * Get a hint that this Action should be displayed inline.
2091 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002092 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002093 * otherwise. The default value is {@code false} if this was never set.
2094 */
2095 public boolean getHintDisplayActionInline() {
2096 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2097 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002098 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002099
2100 /**
2101 * Provides meaning to an {@link Action} that hints at what the associated
2102 * {@link PendingIntent} will do. For example, an {@link Action} with a
2103 * {@link PendingIntent} that replies to a text message notification may have the
2104 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2105 *
2106 * @hide
2107 */
2108 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2109 SEMANTIC_ACTION_NONE,
2110 SEMANTIC_ACTION_REPLY,
2111 SEMANTIC_ACTION_MARK_AS_READ,
2112 SEMANTIC_ACTION_MARK_AS_UNREAD,
2113 SEMANTIC_ACTION_DELETE,
2114 SEMANTIC_ACTION_ARCHIVE,
2115 SEMANTIC_ACTION_MUTE,
2116 SEMANTIC_ACTION_UNMUTE,
2117 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002118 SEMANTIC_ACTION_THUMBS_DOWN,
Gustav Sennton005d7a02019-01-04 13:41:32 +00002119 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002120 })
2121 @Retention(RetentionPolicy.SOURCE)
2122 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002123 }
2124
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002125 /**
2126 * Array of all {@link Action} structures attached to this notification by
2127 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2128 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2129 * interface for invoking actions.
2130 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002131 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002132
2133 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002134 * Replacement version of this notification whose content will be shown
2135 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2136 * and {@link #VISIBILITY_PUBLIC}.
2137 */
2138 public Notification publicVersion;
2139
2140 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002142 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 */
2144 public Notification()
2145 {
2146 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002147 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002148 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
2150
2151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * @hide
2153 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002154 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 public Notification(Context context, int icon, CharSequence tickerText, long when,
2156 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2157 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002158 new Builder(context)
2159 .setWhen(when)
2160 .setSmallIcon(icon)
2161 .setTicker(tickerText)
2162 .setContentTitle(contentTitle)
2163 .setContentText(contentText)
2164 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2165 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
2167
2168 /**
2169 * Constructs a Notification object with the information needed to
2170 * have a status bar icon without the standard expanded view.
2171 *
2172 * @param icon The resource id of the icon to put in the status bar.
2173 * @param tickerText The text that flows by in the status bar when the notification first
2174 * activates.
2175 * @param when The time to show in the time field. In the System.currentTimeMillis
2176 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002177 *
2178 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002180 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 public Notification(int icon, CharSequence tickerText, long when)
2182 {
2183 this.icon = icon;
2184 this.tickerText = tickerText;
2185 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002186 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188
2189 /**
2190 * Unflatten the notification from a parcel.
2191 */
Svet Ganovddb94882016-06-23 19:55:24 -07002192 @SuppressWarnings("unchecked")
2193 public Notification(Parcel parcel) {
2194 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2195 // intents in extras are always written as the last entry.
2196 readFromParcelImpl(parcel);
2197 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002198 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002199 }
2200
2201 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 {
2203 int version = parcel.readInt();
2204
Adrian Roosfb921842017-10-26 14:49:56 +02002205 mWhitelistToken = parcel.readStrongBinder();
2206 if (mWhitelistToken == null) {
2207 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002208 }
2209 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002210 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002213 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002214 if (parcel.readInt() != 0) {
2215 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002216 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2217 icon = mSmallIcon.getResId();
2218 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 number = parcel.readInt();
2221 if (parcel.readInt() != 0) {
2222 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2223 }
2224 if (parcel.readInt() != 0) {
2225 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2226 }
2227 if (parcel.readInt() != 0) {
2228 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2229 }
2230 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002231 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002232 }
2233 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2235 }
Joe Onorato561d3852010-11-20 18:09:34 -08002236 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002237 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 defaults = parcel.readInt();
2240 flags = parcel.readInt();
2241 if (parcel.readInt() != 0) {
2242 sound = Uri.CREATOR.createFromParcel(parcel);
2243 }
2244
2245 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002246 if (parcel.readInt() != 0) {
2247 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 vibrate = parcel.createLongArray();
2250 ledARGB = parcel.readInt();
2251 ledOnMS = parcel.readInt();
2252 ledOffMS = parcel.readInt();
2253 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002254
2255 if (parcel.readInt() != 0) {
2256 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2257 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002258
2259 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002260
John Spurlockfd7f1e02014-03-18 16:41:57 -04002261 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002262
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002263 mGroupKey = parcel.readString();
2264
2265 mSortKey = parcel.readString();
2266
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002267 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002268 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002269
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002270 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2271
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002272 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002273 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2274 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002275
Chris Wren8fd39ec2014-02-27 17:43:26 -05002276 if (parcel.readInt() != 0) {
2277 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2278 }
2279
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002280 visibility = parcel.readInt();
2281
2282 if (parcel.readInt() != 0) {
2283 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2284 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002285
2286 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002287
2288 if (parcel.readInt() != 0) {
2289 mChannelId = parcel.readString();
2290 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002291 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002292
2293 if (parcel.readInt() != 0) {
2294 mShortcutId = parcel.readString();
2295 }
2296
Felipe Leme90205ef2019-03-05 09:59:52 -08002297 if (parcel.readInt() != 0) {
2298 mLocusId = LocusId.CREATOR.createFromParcel(parcel);
2299 }
2300
Julia Reynolds13d898c2017-02-02 12:22:05 -05002301 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002302
2303 if (parcel.readInt() != 0) {
2304 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2305 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002306
2307 mGroupAlertBehavior = parcel.readInt();
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002308 if (parcel.readInt() != 0) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002309 mBubbleMetadata = BubbleMetadata.CREATOR.createFromParcel(parcel);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002310 }
Gustav Sennton761884c2018-11-19 17:40:19 +00002311
2312 mAllowSystemGeneratedContextualActions = parcel.readBoolean();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314
Andy Stadler110988c2010-12-03 14:29:16 -08002315 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002316 public Notification clone() {
2317 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002318 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002319 return that;
2320 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002321
Daniel Sandler1a497d32013-04-18 14:52:45 -04002322 /**
2323 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2324 * of this into that.
2325 * @hide
2326 */
2327 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002328 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002329 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002330 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002331 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002332 that.number = this.number;
2333
2334 // PendingIntents are global, so there's no reason (or way) to clone them.
2335 that.contentIntent = this.contentIntent;
2336 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002337 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002338
2339 if (this.tickerText != null) {
2340 that.tickerText = this.tickerText.toString();
2341 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002342 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002344 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002345 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002346 that.contentView = this.contentView.clone();
2347 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002348 if (heavy && this.mLargeIcon != null) {
2349 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002350 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002351 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002352 that.sound = this.sound; // android.net.Uri is immutable
2353 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002354 if (this.audioAttributes != null) {
2355 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2356 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002357
2358 final long[] vibrate = this.vibrate;
2359 if (vibrate != null) {
2360 final int N = vibrate.length;
2361 final long[] vib = that.vibrate = new long[N];
2362 System.arraycopy(vibrate, 0, vib, 0, N);
2363 }
2364
2365 that.ledARGB = this.ledARGB;
2366 that.ledOnMS = this.ledOnMS;
2367 that.ledOffMS = this.ledOffMS;
2368 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002369
Joe Onorato18e69df2010-05-17 22:26:12 -07002370 that.flags = this.flags;
2371
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002372 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002373
John Spurlockfd7f1e02014-03-18 16:41:57 -04002374 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002375
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002376 that.mGroupKey = this.mGroupKey;
2377
2378 that.mSortKey = this.mSortKey;
2379
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002381 try {
2382 that.extras = new Bundle(this.extras);
2383 // will unparcel
2384 that.extras.size();
2385 } catch (BadParcelableException e) {
2386 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2387 that.extras = null;
2388 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 }
2390
Felipe Lemedd85da62016-06-28 11:29:54 -07002391 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2392 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002393 }
2394
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002395 if (this.actions != null) {
2396 that.actions = new Action[this.actions.length];
2397 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002398 if ( this.actions[i] != null) {
2399 that.actions[i] = this.actions[i].clone();
2400 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002401 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002402 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002403
Daniel Sandler1a497d32013-04-18 14:52:45 -04002404 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002405 that.bigContentView = this.bigContentView.clone();
2406 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002407
Chris Wren8fd39ec2014-02-27 17:43:26 -05002408 if (heavy && this.headsUpContentView != null) {
2409 that.headsUpContentView = this.headsUpContentView.clone();
2410 }
2411
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002412 that.visibility = this.visibility;
2413
2414 if (this.publicVersion != null) {
2415 that.publicVersion = new Notification();
2416 this.publicVersion.cloneInto(that.publicVersion, heavy);
2417 }
2418
Dan Sandler26e81cf2014-05-06 10:01:27 -04002419 that.color = this.color;
2420
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002421 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002422 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002423 that.mShortcutId = this.mShortcutId;
Felipe Leme90205ef2019-03-05 09:59:52 -08002424 that.mLocusId = this.mLocusId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002425 that.mBadgeIcon = this.mBadgeIcon;
2426 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002427 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002428 that.mBubbleMetadata = this.mBubbleMetadata;
Gustav Sennton761884c2018-11-19 17:40:19 +00002429 that.mAllowSystemGeneratedContextualActions = this.mAllowSystemGeneratedContextualActions;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002430
Daniel Sandler1a497d32013-04-18 14:52:45 -04002431 if (!heavy) {
2432 that.lightenPayload(); // will clean out extras
2433 }
2434 }
2435
2436 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002437 * Note all {@link Uri} that are referenced internally, with the expectation
2438 * that Uri permission grants will need to be issued to ensure the recipient
2439 * of this object is able to render its contents.
2440 *
2441 * @hide
2442 */
2443 public void visitUris(@NonNull Consumer<Uri> visitor) {
2444 visitor.accept(sound);
2445
2446 if (tickerView != null) tickerView.visitUris(visitor);
2447 if (contentView != null) contentView.visitUris(visitor);
2448 if (bigContentView != null) bigContentView.visitUris(visitor);
2449 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2450
2451 if (extras != null) {
2452 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002453 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2454 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2455 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002456 }
2457
2458 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2459 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2460 if (!ArrayUtils.isEmpty(messages)) {
2461 for (MessagingStyle.Message message : MessagingStyle.Message
2462 .getMessagesFromBundleArray(messages)) {
2463 visitor.accept(message.getDataUri());
2464 }
2465 }
2466
2467 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2468 if (!ArrayUtils.isEmpty(historic)) {
2469 for (MessagingStyle.Message message : MessagingStyle.Message
2470 .getMessagesFromBundleArray(historic)) {
2471 visitor.accept(message.getDataUri());
2472 }
2473 }
2474 }
2475 }
2476
2477 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002478 * Removes heavyweight parts of the Notification object for archival or for sending to
2479 * listeners when the full contents are not necessary.
2480 * @hide
2481 */
2482 public final void lightenPayload() {
2483 tickerView = null;
2484 contentView = null;
2485 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002486 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002487 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002488 if (extras != null && !extras.isEmpty()) {
2489 final Set<String> keyset = extras.keySet();
2490 final int N = keyset.size();
2491 final String[] keys = keyset.toArray(new String[N]);
2492 for (int i=0; i<N; i++) {
2493 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002494 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2495 continue;
2496 }
Dan Sandler50128532015-12-08 15:42:41 -05002497 final Object obj = extras.get(key);
2498 if (obj != null &&
2499 ( obj instanceof Parcelable
2500 || obj instanceof Parcelable[]
2501 || obj instanceof SparseArray
2502 || obj instanceof ArrayList)) {
2503 extras.remove(key);
2504 }
2505 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002506 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002507 }
2508
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002509 /**
2510 * Make sure this CharSequence is safe to put into a bundle, which basically
2511 * means it had better not be some custom Parcelable implementation.
2512 * @hide
2513 */
2514 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002515 if (cs == null) return cs;
2516 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2517 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2518 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002519 if (cs instanceof Parcelable) {
2520 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2521 + " instance is a custom Parcelable and not allowed in Notification");
2522 return cs.toString();
2523 }
Selim Cinek60a54252016-02-26 17:03:25 -08002524 return removeTextSizeSpans(cs);
2525 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002526
Selim Cinek60a54252016-02-26 17:03:25 -08002527 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2528 if (charSequence instanceof Spanned) {
2529 Spanned ss = (Spanned) charSequence;
2530 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2531 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2532 for (Object span : spans) {
2533 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002534 if (resultSpan instanceof CharacterStyle) {
2535 resultSpan = ((CharacterStyle) span).getUnderlying();
2536 }
2537 if (resultSpan instanceof TextAppearanceSpan) {
2538 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002539 resultSpan = new TextAppearanceSpan(
2540 originalSpan.getFamily(),
2541 originalSpan.getTextStyle(),
2542 -1,
2543 originalSpan.getTextColor(),
2544 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002545 } else if (resultSpan instanceof RelativeSizeSpan
2546 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002547 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002548 } else {
2549 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002550 }
2551 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2552 ss.getSpanFlags(span));
2553 }
2554 return builder;
2555 }
2556 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002557 }
2558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 public int describeContents() {
2560 return 0;
2561 }
2562
2563 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002564 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 */
Svet Ganovddb94882016-06-23 19:55:24 -07002566 public void writeToParcel(Parcel parcel, int flags) {
2567 // We need to mark all pending intents getting into the notification
2568 // system as being put there to later allow the notification ranker
2569 // to launch them and by doing so add the app to the battery saver white
2570 // list for a short period of time. The problem is that the system
2571 // cannot look into the extras as there may be parcelables there that
2572 // the platform does not know how to handle. To go around that we have
2573 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002574 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002575 if (collectPendingIntents) {
2576 PendingIntent.setOnMarshaledListener(
2577 (PendingIntent intent, Parcel out, int outFlags) -> {
2578 if (parcel == out) {
Hyangseok Chae1d9b8052019-11-08 08:01:39 +09002579 synchronized (this) {
2580 if (allPendingIntents == null) {
2581 allPendingIntents = new ArraySet<>();
2582 }
2583 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002584 }
Svet Ganovddb94882016-06-23 19:55:24 -07002585 }
2586 });
2587 }
2588 try {
2589 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002590 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002591 writeToParcelImpl(parcel, flags);
Hyangseok Chae1d9b8052019-11-08 08:01:39 +09002592 synchronized (this) {
2593 // Must be written last!
2594 parcel.writeArraySet(allPendingIntents);
2595 }
Svet Ganovddb94882016-06-23 19:55:24 -07002596 } finally {
2597 if (collectPendingIntents) {
2598 PendingIntent.setOnMarshaledListener(null);
2599 }
2600 }
2601 }
2602
2603 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 parcel.writeInt(1);
2605
Adrian Roosfb921842017-10-26 14:49:56 +02002606 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002608 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002609 if (mSmallIcon == null && icon != 0) {
2610 // you snuck an icon in here without using the builder; let's try to keep it
2611 mSmallIcon = Icon.createWithResource("", icon);
2612 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002613 if (mSmallIcon != null) {
2614 parcel.writeInt(1);
2615 mSmallIcon.writeToParcel(parcel, 0);
2616 } else {
2617 parcel.writeInt(0);
2618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 parcel.writeInt(number);
2620 if (contentIntent != null) {
2621 parcel.writeInt(1);
2622 contentIntent.writeToParcel(parcel, 0);
2623 } else {
2624 parcel.writeInt(0);
2625 }
2626 if (deleteIntent != null) {
2627 parcel.writeInt(1);
2628 deleteIntent.writeToParcel(parcel, 0);
2629 } else {
2630 parcel.writeInt(0);
2631 }
2632 if (tickerText != null) {
2633 parcel.writeInt(1);
2634 TextUtils.writeToParcel(tickerText, parcel, flags);
2635 } else {
2636 parcel.writeInt(0);
2637 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002638 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002639 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002640 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002641 } else {
2642 parcel.writeInt(0);
2643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 if (contentView != null) {
2645 parcel.writeInt(1);
2646 contentView.writeToParcel(parcel, 0);
2647 } else {
2648 parcel.writeInt(0);
2649 }
Selim Cinek279fa862016-06-14 10:57:25 -07002650 if (mLargeIcon == null && largeIcon != null) {
2651 // you snuck an icon in here without using the builder; let's try to keep it
2652 mLargeIcon = Icon.createWithBitmap(largeIcon);
2653 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002654 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002655 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002656 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002657 } else {
2658 parcel.writeInt(0);
2659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660
2661 parcel.writeInt(defaults);
2662 parcel.writeInt(this.flags);
2663
2664 if (sound != null) {
2665 parcel.writeInt(1);
2666 sound.writeToParcel(parcel, 0);
2667 } else {
2668 parcel.writeInt(0);
2669 }
2670 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002671
2672 if (audioAttributes != null) {
2673 parcel.writeInt(1);
2674 audioAttributes.writeToParcel(parcel, 0);
2675 } else {
2676 parcel.writeInt(0);
2677 }
2678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 parcel.writeLongArray(vibrate);
2680 parcel.writeInt(ledARGB);
2681 parcel.writeInt(ledOnMS);
2682 parcel.writeInt(ledOffMS);
2683 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002684
2685 if (fullScreenIntent != null) {
2686 parcel.writeInt(1);
2687 fullScreenIntent.writeToParcel(parcel, 0);
2688 } else {
2689 parcel.writeInt(0);
2690 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002691
2692 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002693
John Spurlockfd7f1e02014-03-18 16:41:57 -04002694 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002695
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002696 parcel.writeString(mGroupKey);
2697
2698 parcel.writeString(mSortKey);
2699
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002700 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002701
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002702 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002703
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002704 if (bigContentView != null) {
2705 parcel.writeInt(1);
2706 bigContentView.writeToParcel(parcel, 0);
2707 } else {
2708 parcel.writeInt(0);
2709 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002710
Chris Wren8fd39ec2014-02-27 17:43:26 -05002711 if (headsUpContentView != null) {
2712 parcel.writeInt(1);
2713 headsUpContentView.writeToParcel(parcel, 0);
2714 } else {
2715 parcel.writeInt(0);
2716 }
2717
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002718 parcel.writeInt(visibility);
2719
2720 if (publicVersion != null) {
2721 parcel.writeInt(1);
2722 publicVersion.writeToParcel(parcel, 0);
2723 } else {
2724 parcel.writeInt(0);
2725 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002726
2727 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002728
2729 if (mChannelId != null) {
2730 parcel.writeInt(1);
2731 parcel.writeString(mChannelId);
2732 } else {
2733 parcel.writeInt(0);
2734 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002735 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002736
2737 if (mShortcutId != null) {
2738 parcel.writeInt(1);
2739 parcel.writeString(mShortcutId);
2740 } else {
2741 parcel.writeInt(0);
2742 }
2743
Felipe Leme90205ef2019-03-05 09:59:52 -08002744 if (mLocusId != null) {
2745 parcel.writeInt(1);
2746 mLocusId.writeToParcel(parcel, 0);
2747 } else {
2748 parcel.writeInt(0);
2749 }
2750
Julia Reynolds13d898c2017-02-02 12:22:05 -05002751 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002752
2753 if (mSettingsText != null) {
2754 parcel.writeInt(1);
2755 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2756 } else {
2757 parcel.writeInt(0);
2758 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002759
2760 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002761
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002762 if (mBubbleMetadata != null) {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002763 parcel.writeInt(1);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002764 mBubbleMetadata.writeToParcel(parcel, 0);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04002765 } else {
2766 parcel.writeInt(0);
2767 }
2768
Gustav Sennton761884c2018-11-19 17:40:19 +00002769 parcel.writeBoolean(mAllowSystemGeneratedContextualActions);
2770
Julia Reynoldsfc640012018-02-21 12:25:27 -05002771 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
2773
2774 /**
2775 * Parcelable.Creator that instantiates Notification objects
2776 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002777 public static final @android.annotation.NonNull Parcelable.Creator<Notification> CREATOR
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 = new Parcelable.Creator<Notification>()
2779 {
2780 public Notification createFromParcel(Parcel parcel)
2781 {
2782 return new Notification(parcel);
2783 }
2784
2785 public Notification[] newArray(int size)
2786 {
2787 return new Notification[size];
2788 }
2789 };
2790
2791 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002792 * @hide
2793 */
2794 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2795 Notification.Action[] firstAs = first.actions;
2796 Notification.Action[] secondAs = second.actions;
2797 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2798 return true;
2799 }
2800 if (firstAs != null && secondAs != null) {
2801 if (firstAs.length != secondAs.length) {
2802 return true;
2803 }
2804 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002805 if (!Objects.equals(String.valueOf(firstAs[i].title),
2806 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002807 return true;
2808 }
2809 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2810 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2811 if (firstRs == null) {
2812 firstRs = new RemoteInput[0];
2813 }
2814 if (secondRs == null) {
2815 secondRs = new RemoteInput[0];
2816 }
2817 if (firstRs.length != secondRs.length) {
2818 return true;
2819 }
2820 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002821 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2822 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002823 return true;
2824 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002825 }
2826 }
2827 }
2828 return false;
2829 }
2830
2831 /**
2832 * @hide
2833 */
2834 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2835 if (first.getStyle() == null) {
2836 return second.getStyle() != null;
2837 }
2838 if (second.getStyle() == null) {
2839 return true;
2840 }
2841 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2842 }
2843
2844 /**
2845 * @hide
2846 */
2847 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002848 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2849 return true;
2850 }
2851
2852 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2853 return true;
2854 }
2855 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2856 return true;
2857 }
2858 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2859 return true;
2860 }
2861
2862 return false;
2863 }
2864
2865 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2866 if (first == null && second == null) {
2867 return false;
2868 }
2869 if (first == null && second != null || first != null && second == null) {
2870 return true;
2871 }
2872
2873 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2874 return true;
2875 }
2876
2877 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2878 return true;
2879 }
2880
2881 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002882 }
2883
2884 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002885 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2886 * <p>
2887 * For backwards compatibility {@code extras} holds some references to "real" member data such
2888 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2889 * fine as long as the object stays in one process.
2890 * <p>
2891 * However, once the notification goes into a parcel each reference gets marshalled separately,
2892 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2893 */
2894 private void fixDuplicateExtras() {
2895 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002896 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2897 }
2898 }
2899
2900 /**
2901 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2902 * separate object, replace it with the field's version to avoid holding duplicate copies.
2903 */
2904 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2905 if (original != null && extras.getParcelable(extraName) != null) {
2906 extras.putParcelable(extraName, original);
2907 }
2908 }
2909
2910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2912 * layout.
2913 *
2914 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2915 * in the view.</p>
2916 * @param context The context for your application / activity.
2917 * @param contentTitle The title that goes in the expanded entry.
2918 * @param contentText The text that goes in the expanded entry.
2919 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2920 * If this is an activity, it must include the
2921 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002922 * that you take care of task management as described in the
2923 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2924 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002925 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002926 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002927 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002929 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 public void setLatestEventInfo(Context context,
2931 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002932 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2933 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2934 new Throwable());
2935 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002936
Selim Cinek4ac6f602016-06-13 15:47:03 -07002937 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2938 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2939 }
2940
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002941 // ensure that any information already set directly is preserved
2942 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002943
2944 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002946 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
2948 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002949 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002951 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002952
2953 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955
Julia Reynoldsda303542015-11-23 14:00:20 -05002956 /**
2957 * @hide
2958 */
2959 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002960 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002961 }
2962
2963 /**
2964 * @hide
2965 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002966 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002967 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002968 }
2969
Yi Jin6b514142017-10-30 14:54:12 -07002970 /**
2971 * @hide
2972 */
2973 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2974 long token = proto.start(fieldId);
2975 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2976 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2977 proto.write(NotificationProto.FLAGS, this.flags);
2978 proto.write(NotificationProto.COLOR, this.color);
2979 proto.write(NotificationProto.CATEGORY, this.category);
2980 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2981 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2982 if (this.actions != null) {
2983 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2984 }
2985 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2986 proto.write(NotificationProto.VISIBILITY, this.visibility);
2987 }
2988 if (publicVersion != null) {
2989 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2990 }
2991 proto.end(token);
2992 }
2993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 @Override
2995 public String toString() {
2996 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002997 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002998 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002999 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003000 sb.append(priority);
3001 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08003002 if (contentView != null) {
3003 sb.append(contentView.getPackage());
3004 sb.append("/0x");
3005 sb.append(Integer.toHexString(contentView.getLayoutId()));
3006 } else {
3007 sb.append("null");
3008 }
3009 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003010 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
3011 sb.append("default");
3012 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 int N = this.vibrate.length-1;
3014 sb.append("[");
3015 for (int i=0; i<N; i++) {
3016 sb.append(this.vibrate[i]);
3017 sb.append(',');
3018 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02003019 if (N != -1) {
3020 sb.append(this.vibrate[N]);
3021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 } else {
3024 sb.append("null");
3025 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003026 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003027 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05003029 } else if (this.sound != null) {
3030 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 } else {
3032 sb.append("null");
3033 }
Chris Wren365b6d32015-07-16 10:39:26 -04003034 if (this.tickerText != null) {
3035 sb.append(" tick");
3036 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003037 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003039 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04003040 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04003041 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003042 if (this.category != null) {
3043 sb.append(" category=");
3044 sb.append(this.category);
3045 }
3046 if (this.mGroupKey != null) {
3047 sb.append(" groupKey=");
3048 sb.append(this.mGroupKey);
3049 }
3050 if (this.mSortKey != null) {
3051 sb.append(" sortKey=");
3052 sb.append(this.mSortKey);
3053 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003054 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04003055 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003056 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04003057 }
3058 sb.append(" vis=");
3059 sb.append(visibilityToString(this.visibility));
3060 if (this.publicVersion != null) {
3061 sb.append(" publicVersion=");
3062 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003063 }
Felipe Leme90205ef2019-03-05 09:59:52 -08003064 if (this.mLocusId != null) {
3065 sb.append(" locusId=");
3066 sb.append(this.mLocusId); // LocusId.toString() is PII safe.
3067 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003068 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 return sb.toString();
3070 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003071
Dan Sandler1b718782014-07-18 12:43:45 -04003072 /**
3073 * {@hide}
3074 */
3075 public static String visibilityToString(int vis) {
3076 switch (vis) {
3077 case VISIBILITY_PRIVATE:
3078 return "PRIVATE";
3079 case VISIBILITY_PUBLIC:
3080 return "PUBLIC";
3081 case VISIBILITY_SECRET:
3082 return "SECRET";
3083 default:
3084 return "UNKNOWN(" + String.valueOf(vis) + ")";
3085 }
3086 }
3087
Joe Onoratocb109a02011-01-18 17:57:41 -08003088 /**
John Spurlock1d881a12015-03-18 19:21:54 -04003089 * {@hide}
3090 */
3091 public static String priorityToString(@Priority int pri) {
3092 switch (pri) {
3093 case PRIORITY_MIN:
3094 return "MIN";
3095 case PRIORITY_LOW:
3096 return "LOW";
3097 case PRIORITY_DEFAULT:
3098 return "DEFAULT";
3099 case PRIORITY_HIGH:
3100 return "HIGH";
3101 case PRIORITY_MAX:
3102 return "MAX";
3103 default:
3104 return "UNKNOWN(" + String.valueOf(pri) + ")";
3105 }
3106 }
3107
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04003108 /**
3109 * @hide
3110 */
3111 public boolean hasCompletedProgress() {
3112 // not a progress notification; can't be complete
3113 if (!extras.containsKey(EXTRA_PROGRESS)
3114 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
3115 return false;
3116 }
3117 // many apps use max 0 for 'indeterminate'; not complete
3118 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
3119 return false;
3120 }
3121 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3122 }
3123
Jeff Sharkey000ce802017-04-29 13:13:27 -06003124 /** @removed */
3125 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003126 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003127 return mChannelId;
3128 }
3129
3130 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003131 * Returns the id of the channel this notification posts to.
3132 */
3133 public String getChannelId() {
3134 return mChannelId;
3135 }
3136
Jeff Sharkey000ce802017-04-29 13:13:27 -06003137 /** @removed */
3138 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003139 public long getTimeout() {
3140 return mTimeout;
3141 }
3142
3143 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003144 * Returns the duration from posting after which this notification should be canceled by the
3145 * system, if it's not canceled already.
3146 */
3147 public long getTimeoutAfter() {
3148 return mTimeout;
3149 }
3150
3151 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003152 * Returns what icon should be shown for this notification if it is being displayed in a
3153 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3154 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3155 */
3156 public int getBadgeIconType() {
3157 return mBadgeIcon;
3158 }
3159
3160 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003161 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003162 *
3163 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3164 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003165 */
3166 public String getShortcutId() {
3167 return mShortcutId;
3168 }
3169
Felipe Leme90205ef2019-03-05 09:59:52 -08003170 /**
3171 * Gets the {@link LocusId} associated with this notification.
3172 *
3173 * <p>Used by the device's intelligence services to correlate objects (such as
3174 * {@link ShortcutInfo} and {@link ContentCaptureContext}) that are correlated.
3175 */
3176 @Nullable
3177 public LocusId getLocusId() {
3178 return mLocusId;
3179 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04003180
3181 /**
3182 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3183 */
3184 public CharSequence getSettingsText() {
3185 return mSettingsText;
3186 }
3187
Julia Reynolds13d898c2017-02-02 12:22:05 -05003188 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003189 * Returns which type of notifications in a group are responsible for audibly alerting the
3190 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3191 * {@link #GROUP_ALERT_SUMMARY}.
3192 */
3193 public @GroupAlertBehavior int getGroupAlertBehavior() {
3194 return mGroupAlertBehavior;
3195 }
3196
3197 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003198 * Returns the bubble metadata that will be used to display app content in a floating window
3199 * over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003200 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003201 @Nullable
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003202 public BubbleMetadata getBubbleMetadata() {
3203 return mBubbleMetadata;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003204 }
3205
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003206 /**
Mady Mellor53162c12019-10-22 17:12:59 -07003207 * Sets the {@link BubbleMetadata} for this notification.
3208 * @hide
3209 */
3210 public void setBubbleMetadata(BubbleMetadata data) {
3211 mBubbleMetadata = data;
3212 }
3213
3214 /**
Gustav Sennton79ebf4b2018-12-19 10:31:51 +00003215 * Returns whether the platform is allowed (by the app developer) to generate contextual actions
3216 * for this notification.
3217 */
Gustav Sennton761884c2018-11-19 17:40:19 +00003218 public boolean getAllowSystemGeneratedContextualActions() {
3219 return mAllowSystemGeneratedContextualActions;
3220 }
3221
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003222 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003223 * The small icon representing this notification in the status bar and content view.
3224 *
3225 * @return the small icon representing this notification.
3226 *
3227 * @see Builder#getSmallIcon()
3228 * @see Builder#setSmallIcon(Icon)
3229 */
3230 public Icon getSmallIcon() {
3231 return mSmallIcon;
3232 }
3233
3234 /**
3235 * Used when notifying to clean up legacy small icons.
3236 * @hide
3237 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003238 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003239 public void setSmallIcon(Icon icon) {
3240 mSmallIcon = icon;
3241 }
3242
3243 /**
3244 * The large icon shown in this notification's content view.
3245 * @see Builder#getLargeIcon()
3246 * @see Builder#setLargeIcon(Icon)
3247 */
3248 public Icon getLargeIcon() {
3249 return mLargeIcon;
3250 }
3251
3252 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003253 * @hide
3254 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003255 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003256 public boolean isGroupSummary() {
3257 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3258 }
3259
3260 /**
3261 * @hide
3262 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003263 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003264 public boolean isGroupChild() {
3265 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3266 }
3267
3268 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003269 * @hide
3270 */
3271 public boolean suppressAlertingDueToGrouping() {
3272 if (isGroupSummary()
3273 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3274 return true;
3275 } else if (isGroupChild()
3276 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3277 return true;
3278 }
3279 return false;
3280 }
3281
Tony Mak638430e2018-10-08 19:19:10 +01003282
3283 /**
3284 * Finds and returns a remote input and its corresponding action.
3285 *
3286 * @param requiresFreeform requires the remoteinput to allow freeform or not.
3287 * @return the result pair, {@code null} if no result is found.
Tony Mak638430e2018-10-08 19:19:10 +01003288 */
3289 @Nullable
3290 public Pair<RemoteInput, Action> findRemoteInputActionPair(boolean requiresFreeform) {
3291 if (actions == null) {
3292 return null;
3293 }
3294 for (Notification.Action action : actions) {
3295 if (action.getRemoteInputs() == null) {
3296 continue;
3297 }
3298 RemoteInput resultRemoteInput = null;
3299 for (RemoteInput remoteInput : action.getRemoteInputs()) {
3300 if (remoteInput.getAllowFreeFormInput() || !requiresFreeform) {
3301 resultRemoteInput = remoteInput;
3302 }
3303 }
3304 if (resultRemoteInput != null) {
3305 return Pair.create(resultRemoteInput, action);
3306 }
3307 }
3308 return null;
3309 }
3310
Julia Reynolds30203152017-05-26 13:36:31 -04003311 /**
Julia Reynolds5cb8d5a2019-09-03 11:46:31 -04003312 * Returns the actions that are contextual (that is, suggested because of the content of the
3313 * notification) out of the actions in this notification.
Gustav Senntoneab53682018-11-01 16:30:23 +00003314 */
Julia Reynoldsa10437f42019-08-16 13:43:12 -04003315 public @NonNull List<Notification.Action> getContextualActions() {
Gustav Senntoneab53682018-11-01 16:30:23 +00003316 if (actions == null) return Collections.emptyList();
3317
3318 List<Notification.Action> contextualActions = new ArrayList<>();
3319 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00003320 if (action.isContextual()) {
Gustav Senntoneab53682018-11-01 16:30:23 +00003321 contextualActions.add(action);
3322 }
3323 }
3324 return contextualActions;
3325 }
3326
3327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003328 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003329 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003330 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003331 * content views using the platform's notification layout template. If your app supports
3332 * versions of Android as old as API level 4, you can instead use
3333 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3334 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3335 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003336 *
Scott Main183bf112012-08-13 19:12:13 -07003337 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003338 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003339 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003340 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003341 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3342 * .setContentText(subject)
3343 * .setSmallIcon(R.drawable.new_mail)
3344 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003345 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003346 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003347 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003348 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003349 /**
3350 * @hide
3351 */
3352 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3353 "android.rebuild.contentViewActionCount";
3354 /**
3355 * @hide
3356 */
3357 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3358 = "android.rebuild.bigViewActionCount";
3359 /**
3360 * @hide
3361 */
3362 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3363 = "android.rebuild.hudViewActionCount";
3364
Selim Cinek6743c0b2017-01-18 18:24:01 -08003365 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3366 SystemProperties.getBoolean("notifications.only_title", true);
3367
Selim Cinek389edcd2017-05-11 19:16:44 -07003368 /**
3369 * The lightness difference that has to be added to the primary text color to obtain the
3370 * secondary text color when the background is light.
3371 */
3372 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3373
3374 /**
3375 * The lightness difference that has to be added to the primary text color to obtain the
3376 * secondary text color when the background is dark.
3377 * A bit less then the above value, since it looks better on dark backgrounds.
3378 */
3379 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3380
Joe Onorato46439ce2010-11-19 13:56:21 -08003381 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003382 private Notification mN;
3383 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003384 private Style mStyle;
Mathew Inwood61e8ae62018-08-14 14:17:44 +01003385 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003386 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003387 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003388 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003389 private boolean mIsLegacy;
3390 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003391
3392 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003393 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3394 */
3395 private int mCachedContrastColor = COLOR_INVALID;
3396 private int mCachedContrastColorIsFor = COLOR_INVALID;
Selim Cinekc3fec682019-06-06 18:11:07 -07003397
Selim Cinek4717d862018-04-19 09:19:15 +08003398 /**
3399 * A neutral color color that can be used for icons.
3400 */
3401 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003402
3403 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003404 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3405 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3406 */
3407 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003408 private int mTextColorsAreForBackground = COLOR_INVALID;
3409 private int mPrimaryTextColor = COLOR_INVALID;
3410 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003411 private int mBackgroundColor = COLOR_INVALID;
3412 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003413 /**
3414 * A temporary location where actions are stored. If != null the view originally has action
3415 * but doesn't have any for this inflation.
3416 */
3417 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003418 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003419
Anthony Chenad4d1582017-04-10 16:07:58 -07003420 private boolean mTintActionButtons;
3421 private boolean mInNightMode;
3422
Adrian Roos70d7aa32017-01-11 15:39:06 -08003423 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003424 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003425 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003426 * @param context
3427 * A {@link Context} that will be used by the Builder to construct the
3428 * RemoteViews. The Context will not be held past the lifetime of this Builder
3429 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003430 * @param channelId
3431 * The constructed Notification will be posted on this
3432 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3433 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003434 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003435 public Builder(Context context, String channelId) {
3436 this(context, (Notification) null);
3437 mN.mChannelId = channelId;
3438 }
3439
3440 /**
3441 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3442 * instead. All posted Notifications must specify a NotificationChannel Id.
3443 */
3444 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003445 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003446 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003447 }
3448
Joe Onoratocb109a02011-01-18 17:57:41 -08003449 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003450 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003451 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003452 public Builder(Context context, Notification toAdopt) {
3453 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003454 Resources res = mContext.getResources();
3455 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3456
3457 if (res.getBoolean(R.bool.config_enableNightMode)) {
3458 Configuration currentConfig = res.getConfiguration();
3459 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3460 == Configuration.UI_MODE_NIGHT_YES;
3461 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003462
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 if (toAdopt == null) {
3464 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003465 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3466 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3467 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003468 mN.priority = PRIORITY_DEFAULT;
3469 mN.visibility = VISIBILITY_PRIVATE;
3470 } else {
3471 mN = toAdopt;
3472 if (mN.actions != null) {
3473 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003474 }
3475
Selim Cineke7238dd2017-12-14 17:48:32 -08003476 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3477 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3478 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003479 }
3480
Selim Cinek4ac6f602016-06-13 15:47:03 -07003481 if (mN.getSmallIcon() == null && mN.icon != 0) {
3482 setSmallIcon(mN.icon);
3483 }
3484
3485 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3486 setLargeIcon(mN.largeIcon);
3487 }
3488
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003489 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3490 if (!TextUtils.isEmpty(templateClass)) {
3491 final Class<? extends Style> styleClass
3492 = getNotificationStyleClass(templateClass);
3493 if (styleClass == null) {
3494 Log.d(TAG, "Unknown style class: " + templateClass);
3495 } else {
3496 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003497 final Constructor<? extends Style> ctor =
3498 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003499 ctor.setAccessible(true);
3500 final Style style = ctor.newInstance();
3501 style.restoreFromExtras(mN.extras);
3502
3503 if (style != null) {
3504 setStyle(style);
3505 }
3506 } catch (Throwable t) {
3507 Log.e(TAG, "Could not create Style", t);
3508 }
3509 }
3510 }
3511
3512 }
3513 }
3514
Lucas Dupina291d192018-06-07 13:59:42 -07003515 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003516 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003517 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003518 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003520 }
3521
3522 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003523 * If this notification is duplicative of a Launcher shortcut, sets the
3524 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3525 * the shortcut.
3526 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003527 * This field will be ignored by Launchers that don't support badging, don't show
3528 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003529 *
3530 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3531 * supersedes
3532 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003533 @NonNull
Julia Reynolds13d898c2017-02-02 12:22:05 -05003534 public Builder setShortcutId(String shortcutId) {
3535 mN.mShortcutId = shortcutId;
3536 return this;
3537 }
3538
3539 /**
Felipe Leme90205ef2019-03-05 09:59:52 -08003540 * Sets the {@link LocusId} associated with this notification.
3541 *
3542 * <p>This method should be called when the {@link LocusId} is used in other places (such
3543 * as {@link ShortcutInfo} and {@link ContentCaptureContext}) so the device's intelligence
3544 * services can correlate them.
3545 */
3546 @NonNull
3547 public Builder setLocusId(@Nullable LocusId locusId) {
3548 mN.mLocusId = locusId;
3549 return this;
3550 }
3551
3552 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003553 * Sets which icon to display as a badge for this notification.
3554 *
3555 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3556 * {@link #BADGE_ICON_LARGE}.
3557 *
3558 * Note: This value might be ignored, for launchers that don't support badge icons.
3559 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003560 @NonNull
Julia Reynolds612beb22017-03-30 10:48:30 -04003561 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003562 mN.mBadgeIcon = icon;
3563 return this;
3564 }
3565
3566 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003567 * Sets the group alert behavior for this notification. Use this method to mute this
3568 * notification if alerts for this notification's group should be handled by a different
3569 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003570 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3571 * mute. For example, if you want only the summary of your group to make noise, all
3572 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003573 *
3574 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3575 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003576 @NonNull
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003577 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3578 mN.mGroupAlertBehavior = groupAlertBehavior;
3579 return this;
3580 }
3581
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003582 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003583 * Sets the {@link BubbleMetadata} that will be used to display app content in a floating
3584 * window over the existing foreground activity.
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003585 *
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003586 * <p>This data will be ignored unless the notification is posted to a channel that
3587 * allows {@link NotificationChannel#canBubble() bubbles}.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003588 *
Mady Mellor65dcaa92019-04-03 12:21:44 -07003589 * <p>Notifications allowed to bubble that have valid bubble metadata will display in
3590 * collapsed state outside of the notification shade on unlocked devices. When a user
3591 * interacts with the collapsed state, the bubble intent will be invoked and displayed.</p>
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003592 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003593 @NonNull
Mady Mellorcf8f1b22019-03-07 14:08:21 -08003594 public Builder setBubbleMetadata(@Nullable BubbleMetadata data) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08003595 mN.mBubbleMetadata = data;
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04003596 return this;
3597 }
3598
Jeff Sharkey000ce802017-04-29 13:13:27 -06003599 /** @removed */
3600 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003601 public Builder setChannel(String channelId) {
3602 mN.mChannelId = channelId;
3603 return this;
3604 }
3605
3606 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003607 * Specifies the channel the notification should be delivered on.
3608 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003609 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003610 public Builder setChannelId(String channelId) {
3611 mN.mChannelId = channelId;
3612 return this;
3613 }
3614
Jeff Sharkey000ce802017-04-29 13:13:27 -06003615 /** @removed */
3616 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003617 public Builder setTimeout(long durationMs) {
3618 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003619 return this;
3620 }
3621
3622 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003623 * Specifies a duration in milliseconds after which this notification should be canceled,
3624 * if it is not already canceled.
3625 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003626 @NonNull
Julia Reynoldsbad42972017-04-25 13:52:49 -04003627 public Builder setTimeoutAfter(long durationMs) {
3628 mN.mTimeout = durationMs;
3629 return this;
3630 }
3631
3632 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003633 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003634 *
3635 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3636 * shown anymore by default and must be opted into by using
3637 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003638 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003639 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003640 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003641 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003642 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003643 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003644 return this;
3645 }
3646
Joe Onoratocb109a02011-01-18 17:57:41 -08003647 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003648 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003649 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003650 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3651 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003652 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003653 @NonNull
Daniel Sandler0c890492012-09-12 17:23:10 -07003654 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003655 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003656 return this;
3657 }
3658
3659 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003660 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003661 *
3662 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003663 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003664 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003665 * Useful when showing an elapsed time (like an ongoing phone call).
3666 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003667 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003668 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003669 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003670 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003671 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003672 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003673 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003674 @NonNull
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003675 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003676 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003677 return this;
3678 }
3679
3680 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003681 * Sets the Chronometer to count down instead of counting up.
3682 *
3683 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3684 * If it isn't set the chronometer will count up.
3685 *
3686 * @see #setUsesChronometer(boolean)
3687 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003688 @NonNull
Adrian Roos96b7e202016-05-17 13:50:38 -07003689 public Builder setChronometerCountDown(boolean countDown) {
3690 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003691 return this;
3692 }
3693
3694 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003695 * Set the small icon resource, which will be used to represent the notification in the
3696 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003697 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698
3699 * The platform template for the expanded view will draw this icon in the left, unless a
3700 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3701 * icon will be moved to the right-hand side.
3702 *
3703
3704 * @param icon
3705 * A resource ID in the application's package of the drawable to use.
3706 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003707 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003708 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003709 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003710 return setSmallIcon(icon != 0
3711 ? Icon.createWithResource(mContext, icon)
3712 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003713 }
3714
Joe Onoratocb109a02011-01-18 17:57:41 -08003715 /**
3716 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3717 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3718 * LevelListDrawable}.
3719 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003720 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003721 * @param level The level to use for the icon.
3722 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003723 * @see Notification#icon
3724 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003725 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003726 @NonNull
Tor Norbye7b9c9122013-05-30 16:48:33 -07003727 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003728 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003729 return setSmallIcon(icon);
3730 }
3731
3732 /**
3733 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003734 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003735 * {@link #setLargeIcon(Bitmap) large icon}).
3736 *
3737 * @param icon An Icon object to use.
3738 * @see Notification#icon
3739 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003740 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04003741 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003742 mN.setSmallIcon(icon);
3743 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3744 mN.icon = icon.getResId();
3745 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003746 return this;
3747 }
3748
Joe Onoratocb109a02011-01-18 17:57:41 -08003749 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003750 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003751 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003752 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003753 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003754 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003755 return this;
3756 }
3757
Joe Onoratocb109a02011-01-18 17:57:41 -08003758 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003759 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003760 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003761 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003762 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003764 return this;
3765 }
3766
Joe Onoratocb109a02011-01-18 17:57:41 -08003767 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003768 * This provides some additional information that is displayed in the notification. No
3769 * guarantees are given where exactly it is displayed.
3770 *
3771 * <p>This information should only be provided if it provides an essential
3772 * benefit to the understanding of the notification. The more text you provide the
3773 * less readable it becomes. For example, an email client should only provide the account
3774 * name here if more than one email account has been added.</p>
3775 *
3776 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3777 * notification header area.
3778 *
3779 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3780 * this will be shown in the third line of text in the platform notification template.
3781 * You should not be using {@link #setProgress(int, int, boolean)} at the
3782 * same time on those versions; they occupy the same place.
3783 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003784 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003785 @NonNull
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003786 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003787 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003788 return this;
3789 }
3790
3791 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003792 * Provides text that will appear as a link to your application's settings.
3793 *
3794 * <p>This text does not appear within notification {@link Style templates} but may
3795 * appear when the user uses an affordance to learn more about the notification.
3796 * Additionally, this text will not appear unless you provide a valid link target by
3797 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3798 *
3799 * <p>This text is meant to be concise description about what the user can customize
3800 * when they click on this link. The recommended maximum length is 40 characters.
3801 * @param text
3802 * @return
3803 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003804 @NonNull
Julia Reynolds3aedded2017-03-31 14:42:09 -04003805 public Builder setSettingsText(CharSequence text) {
3806 mN.mSettingsText = safeCharSequence(text);
3807 return this;
3808 }
3809
3810 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003811 * Set the remote input history.
3812 *
3813 * This should be set to the most recent inputs that have been sent
3814 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3815 * longer relevant (e.g. for chat notifications once the other party has responded).
3816 *
3817 * The most recent input must be stored at the 0 index, the second most recent at the
3818 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3819 * and how much of each individual input is shown.
3820 *
3821 * <p>Note: The reply text will only be shown on notifications that have least one action
3822 * with a {@code RemoteInput}.</p>
3823 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003824 @NonNull
Adrian Roose458aa82015-12-08 16:17:19 -08003825 public Builder setRemoteInputHistory(CharSequence[] text) {
3826 if (text == null) {
3827 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3828 } else {
3829 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3830 CharSequence[] safe = new CharSequence[N];
3831 for (int i = 0; i < N; i++) {
3832 safe[i] = safeCharSequence(text[i]);
3833 }
3834 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3835 }
3836 return this;
3837 }
3838
3839 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003840 * Sets whether remote history entries view should have a spinner.
3841 * @hide
3842 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003843 @NonNull
Kenny Guya0f6de82018-04-06 16:20:16 +01003844 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3845 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3846 return this;
3847 }
3848
3849 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003850 * Sets whether smart reply buttons should be hidden.
3851 * @hide
3852 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003853 @NonNull
Kenny Guy8cc15d22018-05-09 09:50:55 +01003854 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3855 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3856 return this;
3857 }
3858
3859 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003860 * Sets the number of items this notification represents. May be displayed as a badge count
3861 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003862 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003863 @NonNull
Joe Onorato8595a3d2010-11-19 18:12:07 -08003864 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003865 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003866 return this;
3867 }
3868
Joe Onoratocb109a02011-01-18 17:57:41 -08003869 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003870 * A small piece of additional information pertaining to this notification.
3871 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003872 * The platform template will draw this on the last line of the notification, at the far
3873 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003874 *
3875 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3876 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3877 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003878 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003879 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003880 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003881 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003882 return this;
3883 }
3884
Joe Onoratocb109a02011-01-18 17:57:41 -08003885 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003886 * Set the progress this notification represents.
3887 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003888 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003889 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003890 @NonNull
Jeff Sharkey1c400132011-08-05 14:50:13 -07003891 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 mN.extras.putInt(EXTRA_PROGRESS, progress);
3893 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3894 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003895 return this;
3896 }
3897
3898 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003899 * Supply a custom RemoteViews to use instead of the platform template.
3900 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003902 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003903 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003904 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003905 return setCustomContentView(views);
3906 }
3907
3908 /**
3909 * Supply custom RemoteViews to use instead of the platform template.
3910 *
3911 * This will override the layout that would otherwise be constructed by this Builder
3912 * object.
3913 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003914 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003915 public Builder setCustomContentView(RemoteViews contentView) {
3916 mN.contentView = contentView;
3917 return this;
3918 }
3919
3920 /**
3921 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3922 *
3923 * This will override the expanded layout that would otherwise be constructed by this
3924 * Builder object.
3925 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003926 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003927 public Builder setCustomBigContentView(RemoteViews contentView) {
3928 mN.bigContentView = contentView;
3929 return this;
3930 }
3931
3932 /**
3933 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3934 *
3935 * This will override the heads-up layout that would otherwise be constructed by this
3936 * Builder object.
3937 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003938 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003939 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3940 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003941 return this;
3942 }
3943
Joe Onoratocb109a02011-01-18 17:57:41 -08003944 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003945 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3946 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003947 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3948 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3949 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003950 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003951 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003952 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003953 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003955 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003956 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003957 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003958 return this;
3959 }
3960
Joe Onoratocb109a02011-01-18 17:57:41 -08003961 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003962 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3963 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003964 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003965 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003966 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003967 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003969 return this;
3970 }
3971
Joe Onoratocb109a02011-01-18 17:57:41 -08003972 /**
3973 * An intent to launch instead of posting the notification to the status bar.
3974 * Only for use with extremely high-priority notifications demanding the user's
3975 * <strong>immediate</strong> attention, such as an incoming phone call or
3976 * alarm clock that the user has explicitly set to a particular time.
3977 * If this facility is used for something else, please give the user an option
3978 * to turn it off and use a normal notification, as this can be extremely
3979 * disruptive.
3980 *
Chris Wren47c20a12014-06-18 17:27:29 -04003981 * <p>
3982 * The system UI may choose to display a heads-up notification, instead of
3983 * launching this intent, while the user is using the device.
3984 * </p>
Julia Reynolds47fd15f2018-11-28 10:16:00 -05003985 * <p>Apps targeting {@link Build.VERSION_CODES#Q} and above will have to request
3986 * a permission ({@link android.Manifest.permission#USE_FULL_SCREEN_INTENT}) in order to
3987 * use full screen intents.</p>
Chris Wren47c20a12014-06-18 17:27:29 -04003988 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003989 * @param intent The pending intent to launch.
3990 * @param highPriority Passing true will cause this notification to be sent
3991 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003992 *
3993 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003994 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00003995 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08003996 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003997 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003998 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3999 return this;
4000 }
4001
Joe Onoratocb109a02011-01-18 17:57:41 -08004002 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004003 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004004 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004005 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08004006 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004007 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004008 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004009 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08004010 return this;
4011 }
4012
Joe Onoratocb109a02011-01-18 17:57:41 -08004013 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004014 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004015 *
Joe Onoratocb109a02011-01-18 17:57:41 -08004016 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04004017 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004018 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004019 setTicker(tickerText);
4020 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08004021 return this;
4022 }
4023
Joe Onoratocb109a02011-01-18 17:57:41 -08004024 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04004025 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004026 *
4027 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04004028 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4029 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04004030 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004031 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004032 public Builder setLargeIcon(Bitmap b) {
4033 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4034 }
4035
4036 /**
4037 * Add a large icon to the notification content view.
4038 *
4039 * In the platform template, this image will be shown on the left of the notification view
4040 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
4041 * badge atop the large icon).
4042 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004043 @NonNull
Dan Sandlerd63f9322015-05-06 15:18:49 -04004044 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004045 mN.mLargeIcon = icon;
4046 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08004047 return this;
4048 }
4049
Joe Onoratocb109a02011-01-18 17:57:41 -08004050 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004051 * Set the sound to play.
4052 *
John Spurlockc0650f022014-07-19 13:22:39 -04004053 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
4054 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004055 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004056 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004057 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004058 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08004059 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004060 mN.sound = sound;
4061 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08004062 return this;
4063 }
4064
Joe Onoratocb109a02011-01-18 17:57:41 -08004065 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004066 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08004067 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004068 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
4069 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004070 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08004071 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07004072 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004073 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08004074 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004075 mN.sound = sound;
4076 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08004077 return this;
4078 }
4079
Joe Onoratocb109a02011-01-18 17:57:41 -08004080 /**
John Spurlockc0650f022014-07-19 13:22:39 -04004081 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
4082 * use during playback.
4083 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004084 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04004085 * @see Notification#sound
4086 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004087 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04004088 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004089 mN.sound = sound;
4090 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04004091 return this;
4092 }
4093
4094 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004095 * Set the vibration pattern to use.
4096 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004097 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
4098 * <code>pattern</code> parameter.
4099 *
Chris Wren47c20a12014-06-18 17:27:29 -04004100 * <p>
4101 * A notification that vibrates is more likely to be presented as a heads-up notification.
4102 * </p>
4103 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004104 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004105 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08004106 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004107 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004108 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004109 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08004110 return this;
4111 }
4112
Joe Onoratocb109a02011-01-18 17:57:41 -08004113 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004114 * Set the desired color for the indicator LED on the device, as well as the
4115 * blink duty cycle (specified in milliseconds).
4116 *
4117
4118 * Not all devices will honor all (or even any) of these values.
4119 *
Julia Reynolds529e3322017-02-06 08:33:01 -05004120 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004121 * @see Notification#ledARGB
4122 * @see Notification#ledOnMS
4123 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08004124 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004125 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08004126 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004127 mN.ledARGB = argb;
4128 mN.ledOnMS = onMs;
4129 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004130 if (onMs != 0 || offMs != 0) {
4131 mN.flags |= FLAG_SHOW_LIGHTS;
4132 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004133 return this;
4134 }
4135
Joe Onoratocb109a02011-01-18 17:57:41 -08004136 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004137 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08004138 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004139
4140 * Ongoing notifications cannot be dismissed by the user, so your application or service
4141 * must take care of canceling them.
4142 *
4143
4144 * They are typically used to indicate a background task that the user is actively engaged
4145 * with (e.g., playing music) or is pending in some way and therefore occupying the device
4146 * (e.g., a file download, sync operation, active network connection).
4147 *
4148
4149 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08004150 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004151 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004152 public Builder setOngoing(boolean ongoing) {
4153 setFlag(FLAG_ONGOING_EVENT, ongoing);
4154 return this;
4155 }
4156
Joe Onoratocb109a02011-01-18 17:57:41 -08004157 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08004158 * Set whether this notification should be colorized. When set, the color set with
4159 * {@link #setColor(int)} will be used as the background color of this notification.
4160 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08004161 * This should only be used for high priority ongoing tasks like navigation, an ongoing
4162 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08004163 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07004164 * For most styles, the coloring will only be applied if the notification is for a
4165 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08004166 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07004167 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08004168 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07004169 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08004170 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08004171 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004172 @NonNull
Selim Cinek7b9605b2017-01-19 17:36:00 -08004173 public Builder setColorized(boolean colorize) {
4174 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
4175 return this;
4176 }
4177
4178 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08004179 * Set this flag if you would only like the sound, vibrate
4180 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004181 *
4182 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08004183 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004184 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004185 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
4186 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
4187 return this;
4188 }
4189
Joe Onoratocb109a02011-01-18 17:57:41 -08004190 /**
Julia Reynolds04499532016-09-13 14:04:53 -04004191 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004192 *
4193 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08004194 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004195 @NonNull
Joe Onorato46439ce2010-11-19 13:56:21 -08004196 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08004197 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08004198 return this;
4199 }
4200
Joe Onoratocb109a02011-01-18 17:57:41 -08004201 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08004202 * Set whether or not this notification should not bridge to other devices.
4203 *
4204 * <p>Some notifications can be bridged to other devices for remote display.
4205 * This hint can be set to recommend this notification not be bridged.
4206 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004207 @NonNull
Griff Hazendfcb0802014-02-11 12:00:00 -08004208 public Builder setLocalOnly(boolean localOnly) {
4209 setFlag(FLAG_LOCAL_ONLY, localOnly);
4210 return this;
4211 }
4212
4213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004214 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08004215 * <p>
4216 * The value should be one or more of the following fields combined with
4217 * bitwise-or:
4218 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
4219 * <p>
4220 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004221 *
4222 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05004223 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004224 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08004225 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004226 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08004227 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004228 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08004229 return this;
4230 }
4231
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004232 /**
4233 * Set the priority of this notification.
4234 *
4235 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004236 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004237 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05004238 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07004239 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004240 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004241 return this;
4242 }
Joe Malin8d40d042012-11-05 11:36:40 -08004243
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004244 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04004245 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08004246 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04004247 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004248 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004249 @NonNull
John Spurlockfd7f1e02014-03-18 16:41:57 -04004250 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004251 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004252 return this;
4253 }
4254
4255 /**
Chris Wrendde75302014-03-26 17:24:15 -04004256 * Add a person that is relevant to this notification.
4257 *
Chris Wrene6c48932014-09-29 17:19:27 -04004258 * <P>
4259 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04004260 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4261 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4262 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04004263 * </P>
4264 *
4265 * <P>
4266 * The person should be specified by the {@code String} representation of a
4267 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4268 * </P>
4269 *
4270 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4271 * URIs. The path part of these URIs must exist in the contacts database, in the
4272 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4273 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004274 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4275 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004276 * </P>
4277 *
4278 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004279 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004280 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004281 */
Chris Wrene6c48932014-09-29 17:19:27 -04004282 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004283 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004284 return this;
4285 }
4286
4287 /**
4288 * Add a person that is relevant to this notification.
4289 *
4290 * <P>
4291 * Depending on user preferences, this annotation may allow the notification to pass
4292 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4293 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4294 * appear more prominently in the user interface.
4295 * </P>
4296 *
4297 * <P>
4298 * A person should usually contain a uri in order to benefit from the ranking boost.
4299 * However, even if no uri is provided, it's beneficial to provide other people in the
4300 * notification, such that listeners and voice only devices can announce and handle them
4301 * properly.
4302 * </P>
4303 *
4304 * @param person the person to add.
4305 * @see Notification#EXTRA_PEOPLE_LIST
4306 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004307 @NonNull
Selim Cineke7238dd2017-12-14 17:48:32 -08004308 public Builder addPerson(Person person) {
4309 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004310 return this;
4311 }
4312
4313 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004314 * Set this notification to be part of a group of notifications sharing the same key.
4315 * Grouped notifications may display in a cluster or stack on devices which
4316 * support such rendering.
4317 *
4318 * <p>To make this notification the summary for its group, also call
4319 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4320 * {@link #setSortKey}.
4321 * @param groupKey The group key of the group.
4322 * @return this object for method chaining
4323 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004324 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004325 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004326 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004327 return this;
4328 }
4329
4330 /**
4331 * Set this notification to be the group summary for a group of notifications.
4332 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004333 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4334 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004335 * @param isGroupSummary Whether this notification should be a group summary.
4336 * @return this object for method chaining
4337 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004338 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004339 public Builder setGroupSummary(boolean isGroupSummary) {
4340 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4341 return this;
4342 }
4343
4344 /**
4345 * Set a sort key that orders this notification among other notifications from the
4346 * same package. This can be useful if an external sort was already applied and an app
4347 * would like to preserve this. Notifications will be sorted lexicographically using this
4348 * value, although providing different priorities in addition to providing sort key may
4349 * cause this value to be ignored.
4350 *
4351 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004352 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004353 *
4354 * @see String#compareTo(String)
4355 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004356 @NonNull
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004357 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004358 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004359 return this;
4360 }
4361
4362 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004363 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004364 *
Griff Hazen720042b2014-02-24 15:46:56 -08004365 * <p>Values within the Bundle will replace existing extras values in this Builder.
4366 *
4367 * @see Notification#extras
4368 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004369 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004370 public Builder addExtras(Bundle extras) {
4371 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004372 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004373 }
4374 return this;
4375 }
4376
4377 /**
4378 * Set metadata for this notification.
4379 *
4380 * <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 -04004381 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004382 * called.
4383 *
Griff Hazen720042b2014-02-24 15:46:56 -08004384 * <p>Replaces any existing extras values with those from the provided Bundle.
4385 * Use {@link #addExtras} to merge in metadata instead.
4386 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004387 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004388 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004389 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004390 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004391 if (extras != null) {
4392 mUserExtras = extras;
4393 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004394 return this;
4395 }
4396
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004397 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004398 * Get the current metadata Bundle used by this notification Builder.
4399 *
4400 * <p>The returned Bundle is shared with this Builder.
4401 *
4402 * <p>The current contents of this Bundle are copied into the Notification each time
4403 * {@link #build()} is called.
4404 *
4405 * @see Notification#extras
4406 */
4407 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 return mUserExtras;
4409 }
4410
4411 private Bundle getAllExtras() {
4412 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4413 saveExtras.putAll(mN.extras);
4414 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004415 }
4416
4417 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004418 * Add an action to this notification. Actions are typically displayed by
4419 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004420 * <p>
4421 * Every action must have an icon (32dp square and matching the
4422 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4423 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4424 * <p>
4425 * A notification in its expanded form can display up to 3 actions, from left to right in
4426 * the order they were added. Actions will not be displayed when the notification is
4427 * collapsed, however, so be sure that any essential functions may be accessed by the user
4428 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004429 *
4430 * @param icon Resource ID of a drawable that represents the action.
4431 * @param title Text describing the action.
4432 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004433 *
4434 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004435 */
Dan Sandler86647982015-05-13 23:41:13 -04004436 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004437 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004438 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004439 return this;
4440 }
4441
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004442 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004443 * Add an action to this notification. Actions are typically displayed by
4444 * the system as a button adjacent to the notification content.
4445 * <p>
4446 * Every action must have an icon (32dp square and matching the
4447 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4448 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4449 * <p>
4450 * A notification in its expanded form can display up to 3 actions, from left to right in
4451 * the order they were added. Actions will not be displayed when the notification is
4452 * collapsed, however, so be sure that any essential functions may be accessed by the user
4453 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4454 *
4455 * @param action The action to add.
4456 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004457 @NonNull
Griff Hazen959591e2014-05-15 22:26:18 -07004458 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004459 if (action != null) {
4460 mActions.add(action);
4461 }
Griff Hazen959591e2014-05-15 22:26:18 -07004462 return this;
4463 }
4464
4465 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004466 * Alter the complete list of actions attached to this notification.
4467 * @see #addAction(Action).
4468 *
4469 * @param actions
4470 * @return
4471 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004472 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004473 public Builder setActions(Action... actions) {
4474 mActions.clear();
4475 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004476 if (actions[i] != null) {
4477 mActions.add(actions[i]);
4478 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004479 }
4480 return this;
4481 }
4482
4483 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004484 * Add a rich notification style to be applied at build time.
4485 *
4486 * @param style Object responsible for modifying the notification style.
4487 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004488 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004489 public Builder setStyle(Style style) {
4490 if (mStyle != style) {
4491 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004492 if (mStyle != null) {
4493 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004494 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4495 } else {
4496 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004497 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004498 }
4499 return this;
4500 }
4501
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004502 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004503 * Returns the style set by {@link #setStyle(Style)}.
4504 */
4505 public Style getStyle() {
4506 return mStyle;
4507 }
4508
4509 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004510 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004511 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004512 * @return The same Builder.
4513 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004514 @NonNull
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004515 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004516 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004517 return this;
4518 }
4519
4520 /**
4521 * Supply a replacement Notification whose contents should be shown in insecure contexts
4522 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4523 * @param n A replacement notification, presumably with some or all info redacted.
4524 * @return The same Builder.
4525 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004526 @NonNull
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004527 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004528 if (n != null) {
4529 mN.publicVersion = new Notification();
4530 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4531 } else {
4532 mN.publicVersion = null;
4533 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004534 return this;
4535 }
4536
Griff Hazenb720abe2014-05-20 13:15:30 -07004537 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004538 * Apply an extender to this notification builder. Extenders may be used to add
4539 * metadata or change options on this builder.
4540 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004541 @NonNull
Griff Hazen61a9e862014-05-22 16:05:19 -07004542 public Builder extend(Extender extender) {
4543 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004544 return this;
4545 }
4546
Dan Sandler4e787062015-06-17 15:09:48 -04004547 /**
4548 * @hide
4549 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004550 @NonNull
Julia Reynoldse46bb372016-03-17 11:05:58 -04004551 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004552 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004553 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004554 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004555 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004556 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004557 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004558 }
4559
Dan Sandler26e81cf2014-05-06 10:01:27 -04004560 /**
4561 * Sets {@link Notification#color}.
4562 *
4563 * @param argb The accent color to use
4564 *
4565 * @return The same Builder.
4566 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00004567 @NonNull
Tor Norbye80756e32015-03-02 09:39:27 -08004568 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004569 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004570 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004571 return this;
4572 }
4573
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004574 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004575 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4576 // This user can never be a badged profile,
4577 // and also includes USER_ALL system notifications.
4578 return null;
4579 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004580 // Note: This assumes that the current user can read the profile badge of the
4581 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004582 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004583 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004584 }
4585
4586 private Bitmap getProfileBadge() {
4587 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004588 if (badge == null) {
4589 return null;
4590 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004591 final int size = mContext.getResources().getDimensionPixelSize(
4592 R.dimen.notification_badge_size);
4593 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004594 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004595 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004596 badge.draw(canvas);
4597 return bitmap;
4598 }
4599
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004600 private void bindProfileBadge(RemoteViews contentView, StandardTemplateParams p) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004601 Bitmap profileBadge = getProfileBadge();
4602
Kenny Guy98193ea2014-07-24 19:54:37 +01004603 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004604 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4605 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004606 if (isColorized(p)) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004607 contentView.setDrawableTint(R.id.profile_badge, false,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004608 getPrimaryTextColor(p), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004609 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004610 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004611 }
4612
Gus Prevasbae5ba32018-11-28 15:45:13 -05004613 private void bindAlertedIcon(RemoteViews contentView, StandardTemplateParams p) {
4614 contentView.setDrawableTint(
4615 R.id.alerted_icon,
4616 false /* targetBackground */,
4617 getNeutralColor(p),
4618 PorterDuff.Mode.SRC_ATOP);
4619 }
4620
Julia Reynoldsfc640012018-02-21 12:25:27 -05004621 /**
4622 * @hide
4623 */
4624 public boolean usesStandardHeader() {
4625 if (mN.mUsesStandardHeader) {
4626 return true;
4627 }
4628 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4629 if (mN.contentView == null && mN.bigContentView == null) {
4630 return true;
4631 }
4632 }
4633 boolean contentViewUsesHeader = mN.contentView == null
4634 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4635 boolean bigContentViewUsesHeader = mN.bigContentView == null
4636 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4637 return contentViewUsesHeader && bigContentViewUsesHeader;
4638 }
4639
Christoph Studerfe718432014-09-01 18:21:18 +02004640 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004641 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004642 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004643 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004644 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004645 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004646 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004647 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004648 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004649 }
4650
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004651 /**
4652 * Resets the notification header to its original state
4653 */
4654 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004655 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4656 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004657 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004658 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004659 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004660 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004661 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004662 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4663 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004664 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004665 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004666 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004667 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004668 contentView.setImageViewIcon(R.id.profile_badge, null);
4669 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Gus Prevasa3226492018-10-23 11:10:09 -04004670 contentView.setViewVisibility(R.id.alerted_icon, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004671 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004672 }
4673
Selim Cinek384804b2018-04-18 14:31:07 +08004674 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4675 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4676 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004677 }
4678
Selim Cinek384804b2018-04-18 14:31:07 +08004679 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4680 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004681 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004682
Christoph Studerfe718432014-09-01 18:21:18 +02004683 resetStandardTemplate(contentView);
4684
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004685 final Bundle ex = mN.extras;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004686 updateBackgroundColor(contentView, p);
4687 bindNotificationHeader(contentView, p);
Selim Cinek384804b2018-04-18 14:31:07 +08004688 bindLargeIconAndReply(contentView, p, result);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004689 boolean showProgress = handleProgressBar(contentView, ex, p);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004690 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004691 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004692 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004693 setTextViewColorPrimary(contentView, R.id.title, p);
Selim Cinek954cc232016-05-20 13:29:23 -07004694 contentView.setViewLayoutWidth(R.id.title, showProgress
4695 ? ViewGroup.LayoutParams.WRAP_CONTENT
4696 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004697 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004698 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004699 int textId = showProgress ? com.android.internal.R.id.text_line_1
4700 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004701 contentView.setTextViewText(textId, processTextSpans(p.text));
Lucas Dupin00be88f2019-01-03 17:50:52 -08004702 setTextViewColorSecondary(contentView, textId, p);
Selim Cinek41598732016-01-11 16:58:37 -08004703 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004704 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004705
Selim Cinek279fa862016-06-14 10:57:25 -07004706 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004707
Selim Cinek29603462015-11-17 19:04:39 -08004708 return contentView;
4709 }
4710
Selim Cinek48f66b72017-08-18 16:17:51 -07004711 private CharSequence processTextSpans(CharSequence text) {
Lucas Dupind3c99322018-09-27 10:22:29 -07004712 if (hasForegroundColor() || mInNightMode) {
Lucas Dupina291d192018-06-07 13:59:42 -07004713 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004714 }
4715 return text;
4716 }
4717
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004718 private void setTextViewColorPrimary(RemoteViews contentView, int id,
4719 StandardTemplateParams p) {
4720 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004721 contentView.setTextColor(id, mPrimaryTextColor);
4722 }
4723
Selim Cinek48f66b72017-08-18 16:17:51 -07004724 private boolean hasForegroundColor() {
4725 return mForegroundColor != COLOR_INVALID;
4726 }
4727
Selim Cinek389edcd2017-05-11 19:16:44 -07004728 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004729 * Return the primary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004730 * @hide
4731 */
4732 @VisibleForTesting
4733 public int getPrimaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004734 return getPrimaryTextColor(mParams);
4735 }
4736
4737 /**
4738 * @param p the template params to inflate this with
4739 * @return the primary text color
4740 * @hide
4741 */
4742 @VisibleForTesting
4743 public int getPrimaryTextColor(StandardTemplateParams p) {
4744 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004745 return mPrimaryTextColor;
4746 }
4747
Selim Cinek389edcd2017-05-11 19:16:44 -07004748 /**
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004749 * Return the secondary text color using the existing template params
Selim Cinek389edcd2017-05-11 19:16:44 -07004750 * @hide
4751 */
4752 @VisibleForTesting
4753 public int getSecondaryTextColor() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004754 return getSecondaryTextColor(mParams);
4755 }
4756
4757 /**
4758 * @param p the template params to inflate this with
4759 * @return the secondary text color
4760 * @hide
4761 */
4762 @VisibleForTesting
4763 public int getSecondaryTextColor(StandardTemplateParams p) {
4764 ensureColors(p);
Selim Cinek389edcd2017-05-11 19:16:44 -07004765 return mSecondaryTextColor;
4766 }
4767
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004768 private void setTextViewColorSecondary(RemoteViews contentView, int id,
4769 StandardTemplateParams p) {
4770 ensureColors(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004771 contentView.setTextColor(id, mSecondaryTextColor);
4772 }
4773
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004774 private void ensureColors(StandardTemplateParams p) {
4775 int backgroundColor = getBackgroundColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004776 if (mPrimaryTextColor == COLOR_INVALID
4777 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004778 || mTextColorsAreForBackground != backgroundColor) {
4779 mTextColorsAreForBackground = backgroundColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004780 if (!hasForegroundColor() || !isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004781 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004782 backgroundColor, mInNightMode);
Lucas Dupina291d192018-06-07 13:59:42 -07004783 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07004784 backgroundColor, mInNightMode);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004785 if (backgroundColor != COLOR_DEFAULT && isColorized(p)) {
Lucas Dupina291d192018-06-07 13:59:42 -07004786 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004787 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004788 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004789 mSecondaryTextColor, backgroundColor, 4.5);
4790 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004791 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004792 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4793 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4794 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004795 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004796 // We only respect the given colors if worst case Black or White still has
4797 // contrast
4798 boolean backgroundLight = backLum > textLum
4799 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4800 || backLum <= textLum
4801 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004802 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004803 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004804 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004805 mForegroundColor,
4806 backgroundColor,
4807 true /* findFG */,
4808 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004809 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004810 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004811 } else {
4812 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004813 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004814 mForegroundColor,
4815 backgroundColor,
4816 true /* findFG */,
4817 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004818 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004819 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004820 }
4821 } else {
4822 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004823 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004824 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4825 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004826 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004827 backgroundColor) < 4.5f) {
4828 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004829 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004830 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004831 mSecondaryTextColor,
4832 backgroundColor,
4833 true /* findFG */,
4834 4.5f);
4835 } else {
4836 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004837 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004838 mSecondaryTextColor,
4839 backgroundColor,
4840 true /* findFG */,
4841 4.5f);
4842 }
Lucas Dupina291d192018-06-07 13:59:42 -07004843 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004844 mSecondaryTextColor, backgroundLight
4845 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4846 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004847 }
4848 }
4849 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004850 }
4851 }
4852
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004853 private void updateBackgroundColor(RemoteViews contentView,
4854 StandardTemplateParams p) {
4855 if (isColorized(p)) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004856 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004857 getBackgroundColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004858 } else {
4859 // Clear it!
4860 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4861 0);
4862 }
4863 }
4864
Selim Cinek860b6da2015-12-16 19:02:19 -08004865 /**
4866 * @param remoteView the remote view to update the minheight in
4867 * @param hasMinHeight does it have a mimHeight
4868 * @hide
4869 */
4870 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4871 int minHeight = 0;
4872 if (hasMinHeight) {
4873 // we need to set the minHeight of the notification
4874 minHeight = mContext.getResources().getDimensionPixelSize(
4875 com.android.internal.R.dimen.notification_min_content_height);
4876 }
4877 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4878 }
4879
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004880 private boolean handleProgressBar(RemoteViews contentView, Bundle ex,
4881 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004882 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4883 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4884 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004885 if (p.hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004886 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004887 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004888 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004889 contentView.setProgressBackgroundTintList(
4890 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4891 R.color.notification_progress_background_color)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004892 if (getRawColor(p) != COLOR_DEFAULT) {
Beth Thibodeau38275c32019-05-08 16:36:59 -04004893 int color = isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p);
4894 ColorStateList colorStateList = ColorStateList.valueOf(color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004895 contentView.setProgressTintList(R.id.progress, colorStateList);
4896 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004897 }
Selim Cinek29603462015-11-17 19:04:39 -08004898 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004899 } else {
4900 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004901 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004902 }
Joe Onorato561d3852010-11-20 18:09:34 -08004903 }
4904
Selim Cinek384804b2018-04-18 14:31:07 +08004905 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4906 TemplateBindResult result) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004907 boolean largeIconShown = bindLargeIcon(contentView, p);
4908 boolean replyIconShown = bindReplyIcon(contentView, p);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004909 boolean iconContainerVisible = largeIconShown || replyIconShown;
Selim Cinek384804b2018-04-18 14:31:07 +08004910 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004911 iconContainerVisible ? View.VISIBLE : View.GONE);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004912 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4913 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4914 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4915 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004916 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004917 result.setIconMarginEnd(marginEnd);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08004918 result.setRightIconContainerVisible(iconContainerVisible);
Selim Cinek384804b2018-04-18 14:31:07 +08004919 }
4920 }
4921
Selim Cinek1c72fa02018-04-23 18:00:54 +08004922 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4923 int marginEnd = 0;
4924 int contentMargin = mContext.getResources().getDimensionPixelSize(
4925 R.dimen.notification_content_margin_end);
4926 int iconSize = mContext.getResources().getDimensionPixelSize(
4927 R.dimen.notification_right_icon_size);
4928 if (replyIconShown) {
4929 // The size of the reply icon
4930 marginEnd += iconSize;
4931
4932 int replyInset = mContext.getResources().getDimensionPixelSize(
4933 R.dimen.notification_reply_inset);
4934 // We're subtracting the inset of the reply icon to make sure it's
4935 // aligned nicely on the right, and remove it from the following padding
4936 marginEnd -= replyInset * 2;
4937 }
4938 if (largeIconShown) {
4939 // adding size of the right icon
4940 marginEnd += iconSize;
4941
4942 if (replyIconShown) {
4943 // We also add some padding to the reply icon if it's around
4944 marginEnd += contentMargin;
4945 }
4946 }
4947 if (replyIconShown || largeIconShown) {
4948 // The padding to the content
4949 marginEnd += contentMargin;
4950 }
4951 return marginEnd;
4952 }
4953
Selim Cinek384804b2018-04-18 14:31:07 +08004954 /**
4955 * Bind the large icon.
4956 * @return if the largeIcon is visible
4957 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004958 private boolean bindLargeIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07004959 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4960 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4961 }
Lucas Dupin00be88f2019-01-03 17:50:52 -08004962 boolean showLargeIcon = mN.mLargeIcon != null && !p.hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07004963 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004964 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4965 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004966 processLargeLegacyIcon(mN.mLargeIcon, contentView, p);
Selim Cinek88188f22017-09-19 16:46:56 -07004967 }
Selim Cinek384804b2018-04-18 14:31:07 +08004968 return showLargeIcon;
4969 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004970
Selim Cinek384804b2018-04-18 14:31:07 +08004971 /**
4972 * Bind the reply icon.
4973 * @return if the reply icon is visible
4974 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004975 private boolean bindReplyIcon(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08004976 boolean actionVisible = !p.hideReplyIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08004977 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004978 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004979 action = findReplyAction();
4980 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004981 }
Selim Cinek384804b2018-04-18 14:31:07 +08004982 if (actionVisible) {
4983 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4984 contentView.setDrawableTint(R.id.reply_icon_action,
4985 false /* targetBackground */,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08004986 getNeutralColor(p),
Selim Cinek384804b2018-04-18 14:31:07 +08004987 PorterDuff.Mode.SRC_ATOP);
4988 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4989 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4990 } else {
4991 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4992 }
4993 contentView.setViewVisibility(R.id.reply_icon_action,
4994 actionVisible ? View.VISIBLE : View.GONE);
4995 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004996 }
4997
4998 private Action findReplyAction() {
4999 ArrayList<Action> actions = mActions;
5000 if (mOriginalActions != null) {
5001 actions = mOriginalActions;
5002 }
5003 int numActions = actions.size();
5004 for (int i = 0; i < numActions; i++) {
5005 Action action = actions.get(i);
5006 if (hasValidRemoteInput(action)) {
5007 return action;
5008 }
5009 }
5010 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005011 }
5012
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005013 private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
5014 bindSmallIcon(contentView, p);
5015 bindHeaderAppName(contentView, p);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005016 bindHeaderText(contentView, p);
5017 bindHeaderTextSecondary(contentView, p);
5018 bindHeaderChronometerAndTime(contentView, p);
5019 bindProfileBadge(contentView, p);
5020 bindAlertedIcon(contentView, p);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005021 bindActivePermissions(contentView, p);
5022 bindExpandButton(contentView, p);
Julia Reynoldsfc640012018-02-21 12:25:27 -05005023 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005024 }
5025
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005026 private void bindActivePermissions(RemoteViews contentView, StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005027 int color = getNeutralColor(p);
Julia Reynoldsb887b232018-04-10 16:38:08 -04005028 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
5029 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
5030 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
5031 }
5032
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005033 private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
5034 int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
Sunny Goyal5b153922017-09-21 21:00:36 -07005035 contentView.setDrawableTint(R.id.expand_button, false, color,
5036 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005037 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08005038 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005039 }
5040
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005041 private void bindHeaderChronometerAndTime(RemoteViews contentView,
5042 StandardTemplateParams p) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005043 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08005044 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005045 setTextViewColorSecondary(contentView, R.id.time_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005046 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
5047 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
5048 contentView.setLong(R.id.chronometer, "setBase",
5049 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
5050 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07005051 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07005052 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005053 setTextViewColorSecondary(contentView, R.id.chronometer, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005054 } else {
5055 contentView.setViewVisibility(R.id.time, View.VISIBLE);
5056 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005057 setTextViewColorSecondary(contentView, R.id.time, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005058 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005059 } else {
5060 // We still want a time to be set but gone, such that we can show and hide it
5061 // on demand in case it's a child notification without anything in the header
5062 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005063 }
5064 }
5065
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005066 private void bindHeaderText(RemoteViews contentView, StandardTemplateParams p) {
5067 CharSequence summaryText = p.summaryText;
5068 if (summaryText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05005069 && mStyle.hasSummaryInHeader()) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005070 summaryText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05005071 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005072 if (summaryText == null
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005073 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
5074 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005075 summaryText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005076 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005077 if (summaryText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005078 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07005079 contentView.setTextViewText(R.id.header_text, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005080 processLegacyText(summaryText)));
5081 setTextViewColorSecondary(contentView, R.id.header_text, p);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07005082 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
5083 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005084 setTextViewColorSecondary(contentView, R.id.header_text_divider, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005085 }
5086 }
5087
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005088 private void bindHeaderTextSecondary(RemoteViews contentView, StandardTemplateParams p) {
5089 if (!TextUtils.isEmpty(p.headerTextSecondary)) {
Selim Cinekafeed292017-12-12 17:32:44 -08005090 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005091 processLegacyText(p.headerTextSecondary)));
5092 setTextViewColorSecondary(contentView, R.id.header_text_secondary, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005093 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
5094 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005095 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider, p);
Selim Cinekafeed292017-12-12 17:32:44 -08005096 }
5097 }
5098
Adrian Rooseba05822016-04-22 17:09:27 -07005099 /**
5100 * @hide
5101 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005102 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07005103 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005104 CharSequence name = null;
5105 final PackageManager pm = mContext.getPackageManager();
5106 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
5107 // only system packages which lump together a bunch of unrelated stuff
5108 // may substitute a different name to make the purpose of the
5109 // notification more clear. the correct package label should always
5110 // be accessible via SystemUI.
5111 final String pkg = mContext.getPackageName();
5112 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
5113 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
5114 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
5115 name = subName;
5116 } else {
5117 Log.w(TAG, "warning: pkg "
5118 + pkg + " attempting to substitute app name '" + subName
5119 + "' without holding perm "
5120 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
5121 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005122 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04005123 if (TextUtils.isEmpty(name)) {
5124 name = pm.getApplicationLabel(mContext.getApplicationInfo());
5125 }
5126 if (TextUtils.isEmpty(name)) {
5127 // still nothing?
5128 return null;
5129 }
5130
5131 return String.valueOf(name);
5132 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005133 private void bindHeaderAppName(RemoteViews contentView, StandardTemplateParams p) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04005134 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005135 if (isColorized(p)) {
5136 setTextViewColorPrimary(contentView, R.id.app_name_text, p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005137 } else {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005138 contentView.setTextColor(R.id.app_name_text, getSecondaryTextColor(p));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005139 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005140 }
5141
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005142 private boolean isColorized(StandardTemplateParams p) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005143 return p.allowColorization && mN.isColorized();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005144 }
5145
5146 private void bindSmallIcon(RemoteViews contentView, StandardTemplateParams p) {
Selim Cinek279fa862016-06-14 10:57:25 -07005147 if (mN.mSmallIcon == null && mN.icon != 0) {
5148 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
5149 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005150 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07005151 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005152 processSmallIconColor(mN.mSmallIcon, contentView, p);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005153 }
5154
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005155 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005156 * @return true if the built notification will show the time or the chronometer; false
5157 * otherwise
5158 */
5159 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07005160 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005161 }
5162
Christoph Studerfe718432014-09-01 18:21:18 +02005163 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005164 // actions_container is only reset when there are no actions to avoid focus issues with
5165 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02005166 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02005167 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08005168
5169 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
5170 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01005171 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
5172 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08005173
5174 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
5175 big.setTextViewText(R.id.notification_material_reply_text_2, null);
5176 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
5177 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07005178
Selim Cineked64a142018-02-06 18:06:01 -08005179 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
5180 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02005181 }
5182
Selim Cinek384804b2018-04-18 14:31:07 +08005183 private RemoteViews applyStandardTemplateWithActions(int layoutId,
5184 TemplateBindResult result) {
5185 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
5186 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07005187 }
5188
Gustav Sennton1463d832018-11-06 16:12:48 +00005189 private static List<Notification.Action> filterOutContextualActions(
5190 List<Notification.Action> actions) {
5191 List<Notification.Action> nonContextualActions = new ArrayList<>();
5192 for (Notification.Action action : actions) {
Gustav Sennton005d7a02019-01-04 13:41:32 +00005193 if (!action.isContextual()) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005194 nonContextualActions.add(action);
5195 }
5196 }
5197 return nonContextualActions;
5198 }
5199
Adrian Roos70d7aa32017-01-11 15:39:06 -08005200 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08005201 StandardTemplateParams p, TemplateBindResult result) {
5202 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005203
Christoph Studerfe718432014-09-01 18:21:18 +02005204 resetStandardTemplateWithActions(big);
5205
Adrian Roose458aa82015-12-08 16:17:19 -08005206 boolean validRemoteInput = false;
5207
Gustav Sennton1463d832018-11-06 16:12:48 +00005208 // In the UI contextual actions appear separately from the standard actions, so we
5209 // filter them out here.
5210 List<Notification.Action> nonContextualActions = filterOutContextualActions(mActions);
5211
5212 int N = nonContextualActions.size();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005213 boolean emphazisedMode = mN.fullScreenIntent != null;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005214 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005215 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08005216 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005217 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08005218 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04005219 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005220 for (int i=0; i<N; i++) {
Gustav Sennton1463d832018-11-06 16:12:48 +00005221 Action action = nonContextualActions.get(i);
5222
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005223 boolean actionHasValidInput = hasValidRemoteInput(action);
5224 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08005225
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005226 final RemoteViews button = generateActionButton(action, emphazisedMode, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005227 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08005228 // Clear the drawable
5229 button.setInt(R.id.action0, "setBackgroundResource", 0);
5230 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005231 big.addView(R.id.actions, button);
5232 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07005233 } else {
5234 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005235 }
Adrian Roose458aa82015-12-08 16:17:19 -08005236
5237 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005238 if (validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07005239 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
5240 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01005241 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08005242 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01005243 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
5244 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005245 big.setTextViewText(R.id.notification_material_reply_text_1,
5246 processTextSpans(replyText[0]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005247 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1, p);
Kenny Guya0f6de82018-04-06 16:20:16 +01005248 big.setViewVisibility(R.id.notification_material_reply_progress,
5249 showSpinner ? View.VISIBLE : View.GONE);
5250 big.setProgressIndeterminateTintList(
5251 R.id.notification_material_reply_progress,
5252 ColorStateList.valueOf(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005253 isColorized(p) ? getPrimaryTextColor(p) : resolveContrastColor(p)));
Adrian Roose458aa82015-12-08 16:17:19 -08005254
Selim Cinekbee4e072018-05-21 22:06:43 -07005255 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
5256 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08005257 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005258 big.setTextViewText(R.id.notification_material_reply_text_2,
5259 processTextSpans(replyText[1]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005260 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005261
Selim Cinekbee4e072018-05-21 22:06:43 -07005262 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
5263 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08005264 big.setViewVisibility(
5265 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07005266 big.setTextViewText(R.id.notification_material_reply_text_3,
5267 processTextSpans(replyText[2]));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005268 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3, p);
Adrian Roose458aa82015-12-08 16:17:19 -08005269 }
5270 }
5271 }
5272
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005273 return big;
5274 }
5275
Adrian Roose458aa82015-12-08 16:17:19 -08005276 private boolean hasValidRemoteInput(Action action) {
5277 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
5278 // Weird actions
5279 return false;
5280 }
5281
5282 RemoteInput[] remoteInputs = action.getRemoteInputs();
5283 if (remoteInputs == null) {
5284 return false;
5285 }
5286
5287 for (RemoteInput r : remoteInputs) {
5288 CharSequence[] choices = r.getChoices();
5289 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
5290 return true;
5291 }
5292 }
5293 return false;
5294 }
5295
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005296 /**
5297 * Construct a RemoteViews for the final 1U notification layout. In order:
5298 * 1. Custom contentView from the caller
5299 * 2. Style's proposed content view
5300 * 3. Standard template view
5301 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005302 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005303 return createContentView(false /* increasedheight */ );
5304 }
5305
5306 /**
5307 * Construct a RemoteViews for the smaller content view.
5308 *
5309 * @param increasedHeight true if this layout be created with an increased height. Some
5310 * styles may support showing more then just that basic 1U size
5311 * and the system may decide to render important notifications
5312 * slightly bigger even when collapsed.
5313 *
5314 * @hide
5315 */
5316 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005317 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005318 return mN.contentView;
5319 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005320 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005321 if (styleView != null) {
5322 return styleView;
5323 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005324 }
Selim Cinek384804b2018-04-18 14:31:07 +08005325 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005326 }
5327
Selim Cineka7679b62017-05-10 16:33:25 -07005328 private boolean useExistingRemoteView() {
5329 return mStyle == null || (!mStyle.displayCustomViewInline()
5330 && !mRebuildStyledRemoteViews);
5331 }
5332
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005333 /**
5334 * Construct a RemoteViews for the final big notification layout.
5335 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005336 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005337 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005338 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005339 return mN.bigContentView;
5340 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005341 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005342 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005343 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005344 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5345 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005346 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005347 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005348 return result;
5349 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005350
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005351 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005352 * Construct a RemoteViews for the final notification header only. This will not be
5353 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005354 *
5355 * @hide
5356 */
Lucas Dupin00be88f2019-01-03 17:50:52 -08005357 public RemoteViews makeNotificationHeader() {
5358 return makeNotificationHeader(mParams.reset().fillTextsFrom(this));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005359 }
5360
5361 /**
5362 * Construct a RemoteViews for the final notification header only. This will not be
5363 * colorized.
5364 *
5365 * @param p the template params to inflate this with
5366 */
5367 private RemoteViews makeNotificationHeader(StandardTemplateParams p) {
5368 // Headers on their own are never colorized
5369 p.disallowColorization();
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005370 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Lucas Dupin00be88f2019-01-03 17:50:52 -08005371 R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005372 resetNotificationHeader(header);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005373 bindNotificationHeader(header, p);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005374 return header;
5375 }
5376
Adrian Roos487374f2017-01-11 15:48:14 -08005377 /**
5378 * Construct a RemoteViews for the ambient version of the notification.
5379 *
5380 * @hide
5381 */
5382 public RemoteViews makeAmbientNotification() {
Selim Cinek0e069942019-01-24 16:09:41 -08005383 RemoteViews headsUpContentView = createHeadsUpContentView(false /* increasedHeight */);
5384 if (headsUpContentView != null) {
5385 return headsUpContentView;
5386 }
5387 return createContentView();
Adrian Roos487374f2017-01-11 15:48:14 -08005388 }
5389
Selim Cinek29603462015-11-17 19:04:39 -08005390 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005391 if (result != null) {
5392 result.setViewVisibility(R.id.text_line_1, View.GONE);
5393 }
Selim Cinek29603462015-11-17 19:04:39 -08005394 }
5395
Selim Cinek6743c0b2017-01-18 18:24:01 -08005396 /**
5397 * Adapt the Notification header if this view is used as an expanded view.
5398 *
5399 * @hide
5400 */
5401 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005402 if (result != null) {
5403 result.setBoolean(R.id.notification_header, "setExpanded", true);
5404 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005405 }
5406
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005407 /**
5408 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005409 *
5410 * @param increasedHeight true if this layout be created with an increased height. Some
5411 * styles may support showing more then just that basic 1U size
5412 * and the system may decide to render important notifications
5413 * slightly bigger even when collapsed.
5414 *
5415 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005416 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005417 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005418 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005419 return mN.headsUpContentView;
5420 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005421 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5422 if (styleView != null) {
5423 return styleView;
5424 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005425 } else if (mActions.size() == 0) {
5426 return null;
5427 }
5428
Selim Cinekbee4e072018-05-21 22:06:43 -07005429 // We only want at most a single remote input history to be shown here, otherwise
5430 // the content would become squished.
5431 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5432 .setMaxRemoteInputHistory(1);
5433 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5434 p,
5435 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005436 }
5437
Selim Cinek624c02db2015-12-14 21:00:02 -08005438 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005439 * Construct a RemoteViews for the final heads-up notification layout.
5440 */
5441 public RemoteViews createHeadsUpContentView() {
5442 return createHeadsUpContentView(false /* useIncreasedHeight */);
5443 }
5444
5445 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005446 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5447 *
Selim Cinekabcc2012019-07-23 18:44:07 -07005448 * @param isLowPriority is this notification low priority
Selim Cinek624c02db2015-12-14 21:00:02 -08005449 * @hide
5450 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005451 @UnsupportedAppUsage
Selim Cinekabcc2012019-07-23 18:44:07 -07005452 public RemoteViews makePublicContentView(boolean isLowPriority) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005453 if (mN.publicVersion != null) {
5454 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Selim Cinekc3fec682019-06-06 18:11:07 -07005455 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005456 }
5457 Bundle savedBundle = mN.extras;
5458 Style style = mStyle;
5459 mStyle = null;
5460 Icon largeIcon = mN.mLargeIcon;
5461 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005462 Bitmap largeIconLegacy = mN.largeIcon;
5463 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005464 ArrayList<Action> actions = mActions;
5465 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005466 Bundle publicExtras = new Bundle();
5467 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5468 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5469 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5470 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005471 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5472 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005473 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5474 if (appName != null) {
5475 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5476 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005477 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005478 RemoteViews view;
Selim Cinekabcc2012019-07-23 18:44:07 -07005479 StandardTemplateParams params = mParams.reset().fillTextsFrom(this);
5480 if (isLowPriority) {
5481 params.forceDefaultColor();
5482 }
5483 view = makeNotificationHeader(params);
Lucas Dupin00be88f2019-01-03 17:50:52 -08005484 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
Selim Cinek624c02db2015-12-14 21:00:02 -08005485 mN.extras = savedBundle;
5486 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005487 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005488 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005489 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005490 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005491 }
5492
Selim Cinek6743c0b2017-01-18 18:24:01 -08005493 /**
5494 * Construct a content view for the display when low - priority
5495 *
5496 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5497 * a new subtext is created consisting of the content of the
5498 * notification.
5499 * @hide
5500 */
5501 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005502 StandardTemplateParams p = mParams.reset()
5503 .forceDefaultColor()
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005504 .fillTextsFrom(this);
5505 if (!useRegularSubtext || TextUtils.isEmpty(mParams.summaryText)) {
5506 p.summaryText(createSummaryText());
Selim Cinek6743c0b2017-01-18 18:24:01 -08005507 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005508 RemoteViews header = makeNotificationHeader(p);
Selim Cinek1b554392017-02-28 17:22:49 -08005509 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005510 return header;
5511 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005512
Selim Cinek6743c0b2017-01-18 18:24:01 -08005513 private CharSequence createSummaryText() {
5514 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5515 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5516 return titleText;
5517 }
5518 SpannableStringBuilder summary = new SpannableStringBuilder();
5519 if (titleText == null) {
5520 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5521 }
5522 BidiFormatter bidi = BidiFormatter.getInstance();
5523 if (titleText != null) {
5524 summary.append(bidi.unicodeWrap(titleText));
5525 }
5526 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5527 if (titleText != null && contentText != null) {
5528 summary.append(bidi.unicodeWrap(mContext.getText(
5529 R.string.notification_header_divider_symbol_with_spaces)));
5530 }
5531 if (contentText != null) {
5532 summary.append(bidi.unicodeWrap(contentText));
5533 }
5534 return summary;
5535 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005536
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005537 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005538 StandardTemplateParams p) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005539 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005540 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005541 emphazisedMode ? getEmphasizedActionLayoutResource()
5542 : tombstone ? getActionTombstoneLayoutResource()
5543 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005544 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005545 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005546 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005547 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005548 if (action.mRemoteInputs != null) {
5549 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5550 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005551 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005552 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005553 CharSequence title = action.title;
5554 ColorStateList[] outResultColor = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005555 int background = resolveBackgroundColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005556 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005557 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005558 } else {
5559 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005560 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005561 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005562 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005563 setTextViewColorPrimary(button, R.id.action0, p);
Selim Cinek396caca2018-04-10 17:46:46 -07005564 int rippleColor;
5565 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5566 if (hasColorOverride) {
5567 // There's a span spanning the full text, let's take it and use it as the
5568 // background color
5569 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005570 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07005571 background, mInNightMode);
Selim Cinek396caca2018-04-10 17:46:46 -07005572 button.setTextColor(R.id.action0, textColor);
5573 rippleColor = textColor;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005574 } else if (getRawColor(p) != COLOR_DEFAULT && !isColorized(p)
Lucas Dupinf15c5e52019-04-01 19:00:12 -07005575 && mTintActionButtons && !mInNightMode) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005576 rippleColor = resolveContrastColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07005577 button.setTextColor(R.id.action0, rippleColor);
5578 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005579 rippleColor = getPrimaryTextColor(p);
Selim Cinek981962e2016-07-20 20:41:58 -07005580 }
Selim Cinek396caca2018-04-10 17:46:46 -07005581 // We only want about 20% alpha for the ripple
5582 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5583 button.setColorStateList(R.id.action0, "setRippleColor",
5584 ColorStateList.valueOf(rippleColor));
5585 button.setColorStateList(R.id.action0, "setButtonBackground",
5586 ColorStateList.valueOf(background));
5587 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005588 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005589 button.setTextViewText(R.id.action0, processTextSpans(
5590 processLegacyText(action.title)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005591 if (isColorized(p)) {
5592 setTextViewColorPrimary(button, R.id.action0, p);
5593 } else if (getRawColor(p) != COLOR_DEFAULT && mTintActionButtons) {
Lucas Dupin00be88f2019-01-03 17:50:52 -08005594 button.setTextColor(R.id.action0, resolveContrastColor(p));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005595 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005596 }
Tony Mak7d4b3a52018-11-27 17:29:36 +00005597 button.setIntTag(R.id.action0, R.id.notification_action_index_tag,
5598 mActions.indexOf(action));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005599 return button;
5600 }
5601
Joe Onoratocb109a02011-01-18 17:57:41 -08005602 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005603 * Ensures contrast on color spans against a background color. also returns the color of the
5604 * text if a span was found that spans over the whole text.
5605 *
5606 * @param charSequence the charSequence on which the spans are
5607 * @param background the background color to ensure the contrast against
5608 * @param outResultColor an array in which a color will be returned as the first element if
5609 * there exists a full length color span.
5610 * @return the contrasted charSequence
5611 */
5612 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5613 ColorStateList[] outResultColor) {
5614 if (charSequence instanceof Spanned) {
5615 Spanned ss = (Spanned) charSequence;
5616 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5617 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5618 for (Object span : spans) {
5619 Object resultSpan = span;
5620 int spanStart = ss.getSpanStart(span);
5621 int spanEnd = ss.getSpanEnd(span);
5622 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5623 if (resultSpan instanceof CharacterStyle) {
5624 resultSpan = ((CharacterStyle) span).getUnderlying();
5625 }
5626 if (resultSpan instanceof TextAppearanceSpan) {
5627 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5628 ColorStateList textColor = originalSpan.getTextColor();
5629 if (textColor != null) {
5630 int[] colors = textColor.getColors();
5631 int[] newColors = new int[colors.length];
5632 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005633 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005634 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005635 }
5636 textColor = new ColorStateList(textColor.getStates().clone(),
5637 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005638 if (fullLength) {
5639 outResultColor[0] = textColor;
5640 // Let's drop the color from the span
5641 textColor = null;
5642 }
Selim Cinek981962e2016-07-20 20:41:58 -07005643 resultSpan = new TextAppearanceSpan(
5644 originalSpan.getFamily(),
5645 originalSpan.getTextStyle(),
5646 originalSpan.getTextSize(),
5647 textColor,
5648 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005649 }
5650 } else if (resultSpan instanceof ForegroundColorSpan) {
5651 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5652 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005653 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005654 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005655 if (fullLength) {
5656 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005657 resultSpan = null;
5658 } else {
5659 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005660 }
5661 } else {
5662 resultSpan = span;
5663 }
Selim Cinek396caca2018-04-10 17:46:46 -07005664 if (resultSpan != null) {
5665 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5666 }
Selim Cinek981962e2016-07-20 20:41:58 -07005667 }
5668 return builder;
5669 }
5670 return charSequence;
5671 }
5672
5673 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005674 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005675 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005676 */
5677 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005678 if (!mIsLegacyInitialized) {
5679 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5680 < Build.VERSION_CODES.LOLLIPOP;
5681 mIsLegacyInitialized = true;
5682 }
5683 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005684 }
5685
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005686 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005687 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
Lucas Dupin00be88f2019-01-03 17:50:52 -08005688 if (isAlreadyLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005689 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005690 } else {
5691 return charSequence;
5692 }
5693 }
5694
Dan Sandler26e81cf2014-05-06 10:01:27 -04005695 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005696 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005697 */
Adrian Roos487374f2017-01-11 15:48:14 -08005698 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005699 StandardTemplateParams p) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005700 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005701 int color;
Lucas Dupin00be88f2019-01-03 17:50:52 -08005702 if (isColorized(p)) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005703 color = getPrimaryTextColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005704 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005705 color = resolveContrastColor(p);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005706 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005707 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005708 contentView.setDrawableTint(R.id.icon, false, color,
5709 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005710
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005711 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005712 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005713 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005714 }
5715
Dan Sandler26e81cf2014-05-06 10:01:27 -04005716 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005717 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005718 * if it's grayscale).
5719 */
5720 // TODO: also check bounds, transparency, that sort of thing.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005721 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView,
5722 StandardTemplateParams p) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005723 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005724 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005725 // resolve color will fall back to the default when legacy
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005726 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(p),
Sunny Goyal5b153922017-09-21 21:00:36 -07005727 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005728 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005729 }
5730
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005731 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005732 if (mN.color != COLOR_DEFAULT) {
5733 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005734 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005735 }
5736
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005737 int resolveContrastColor(StandardTemplateParams p) {
5738 int rawColor = getRawColor(p);
5739 if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08005740 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005741 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005742
Selim Cinekac5f0272017-05-02 16:05:41 -07005743 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005744 int background = mContext.getColor(
5745 com.android.internal.R.color.notification_material_background_color);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005746 if (rawColor == COLOR_DEFAULT) {
5747 ensureColors(p);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005748 color = ContrastColorUtil.resolveDefaultColor(mContext, background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005749 } else {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005750 color = ContrastColorUtil.resolveContrastColor(mContext, rawColor,
Anthony Chenad4d1582017-04-10 16:07:58 -07005751 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005752 }
5753 if (Color.alpha(color) < 255) {
5754 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005755 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005756 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005757 mCachedContrastColorIsFor = rawColor;
Selim Cinekac5f0272017-05-02 16:05:41 -07005758 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005759 }
5760
Selim Cinek10bbc0e2018-11-21 15:14:10 -08005761 /**
5762 * Return the raw color of this Notification, which doesn't necessarily satisfy contrast.
5763 *
5764 * @see #resolveContrastColor(StandardTemplateParams) for the contrasted color
5765 * @param p the template params to inflate this with
5766 */
5767 private int getRawColor(StandardTemplateParams p) {
5768 if (p.forceDefaultColor) {
5769 return COLOR_DEFAULT;
5770 }
5771 return mN.color;
5772 }
5773
Selim Cinek4717d862018-04-19 09:19:15 +08005774 int resolveNeutralColor() {
5775 if (mNeutralColor != COLOR_INVALID) {
5776 return mNeutralColor;
5777 }
5778 int background = mContext.getColor(
5779 com.android.internal.R.color.notification_material_background_color);
Lucas Dupinf03e7522018-06-25 16:21:13 -07005780 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background,
5781 mInNightMode);
Selim Cinek4717d862018-04-19 09:19:15 +08005782 if (Color.alpha(mNeutralColor) < 255) {
5783 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005784 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005785 }
5786 return mNeutralColor;
5787 }
5788
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005789 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005790 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005791 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005792 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005793 @NonNull
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005794 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005795 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005796 mN.actions = new Action[mActions.size()];
5797 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005798 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005799 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005800 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005801 }
Selim Cinek247fa012016-02-18 09:50:48 -08005802 if (mN.bigContentView != null || mN.contentView != null
5803 || mN.headsUpContentView != null) {
5804 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5805 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005806 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005807 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005808
Julia Reynolds3b848122016-02-26 10:45:32 -05005809 /**
5810 * Creates a Builder from an existing notification so further changes can be made.
5811 * @param context The context for your application / activity.
5812 * @param n The notification to create a Builder from.
5813 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005814 @NonNull
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005815 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005816 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005817 ApplicationInfo applicationInfo = n.extras.getParcelable(
5818 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005819 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005820 if (applicationInfo != null) {
5821 try {
5822 builderContext = context.createApplicationContext(applicationInfo,
5823 Context.CONTEXT_RESTRICTED);
5824 } catch (NameNotFoundException e) {
5825 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5826 builderContext = context; // try with our context
5827 }
5828 } else {
5829 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005830 }
5831
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005832 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005833 }
5834
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005835 /**
Gustav Sennton761884c2018-11-19 17:40:19 +00005836 * Determines whether the platform can generate contextual actions for a notification.
5837 * By default this is true.
5838 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005839 @NonNull
Gustav Sennton761884c2018-11-19 17:40:19 +00005840 public Builder setAllowSystemGeneratedContextualActions(boolean allowed) {
5841 mN.mAllowSystemGeneratedContextualActions = allowed;
5842 return this;
5843 }
5844
5845 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005846 * @deprecated Use {@link #build()} instead.
5847 */
5848 @Deprecated
5849 public Notification getNotification() {
5850 return build();
5851 }
5852
5853 /**
5854 * Combine all of the options that have been set and return a new {@link Notification}
5855 * object.
5856 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005857 @NonNull
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005858 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005859 // first, add any extras from the calling code
5860 if (mUserExtras != null) {
5861 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005862 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005863
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005864 mN.creationTime = System.currentTimeMillis();
5865
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005866 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005867 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005868
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005869 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005870
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005871 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005872 mStyle.reduceImageSizes(mContext);
5873 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005874 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005875 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005876 }
Selim Cinekd0426622017-07-11 13:19:59 +02005877 mN.reduceImageSizes(mContext);
5878
Adrian Roos5081c0d2016-02-26 16:04:19 -08005879 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005880 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005881 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005882 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005883 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5884 mN.contentView.getSequenceNumber());
5885 }
5886 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005887 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005888 if (mN.bigContentView != null) {
5889 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5890 mN.bigContentView.getSequenceNumber());
5891 }
5892 }
5893 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005894 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005895 if (mN.headsUpContentView != null) {
5896 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5897 mN.headsUpContentView.getSequenceNumber());
5898 }
5899 }
5900 }
5901
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005902 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5903 mN.flags |= FLAG_SHOW_LIGHTS;
5904 }
5905
Adrian Roosfb921842017-10-26 14:49:56 +02005906 mN.allPendingIntents = null;
5907
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005908 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005909 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005910
5911 /**
5912 * Apply this Builder to an existing {@link Notification} object.
5913 *
5914 * @hide
5915 */
Gustav Sennton625bcab2019-02-28 18:17:38 +00005916 @NonNull
5917 public Notification buildInto(@NonNull Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005918 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005919 return n;
5920 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005921
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005922 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005923 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005924 * change. Also removes extenders on low ram devices, as
5925 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005926 *
5927 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5928 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005929 * @hide
5930 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005931 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5932 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005933 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005934
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005935 // Only strip views for known Styles because we won't know how to
5936 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005937 if (!isLowRam
5938 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005939 && getNotificationStyleClass(templateClass) == null) {
5940 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005941 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005942
5943 // Only strip unmodified BuilderRemoteViews.
5944 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005945 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005946 n.contentView.getSequenceNumber();
5947 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005948 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005949 n.bigContentView.getSequenceNumber();
5950 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005951 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005952 n.headsUpContentView.getSequenceNumber();
5953
5954 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005955 if (!isLowRam
5956 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005957 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005958 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005959
5960 Notification clone = n.clone();
5961 if (stripContentView) {
5962 clone.contentView = null;
5963 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5964 }
5965 if (stripBigContentView) {
5966 clone.bigContentView = null;
5967 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5968 }
5969 if (stripHeadsUpContentView) {
5970 clone.headsUpContentView = null;
5971 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5972 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005973 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005974 String[] allowedServices = context.getResources().getStringArray(
5975 R.array.config_allowedManagedServicesOnLowRamDevices);
5976 if (allowedServices.length == 0) {
5977 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5978 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5979 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5980 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005981 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005982 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005983 }
5984
Mathew Inwood61e8ae62018-08-14 14:17:44 +01005985 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005986 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005987 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005988 }
5989
5990 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005991 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005992 }
5993
5994 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005995 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005996 }
5997
5998 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005999 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006000 }
6001
6002 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006003 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006004 }
6005
Adrian Roosc1a80b02016-04-05 14:54:55 -07006006 private int getMessagingLayoutResource() {
6007 return R.layout.notification_template_material_messaging;
6008 }
6009
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006010 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006011 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006012 }
6013
Selim Cinek06e9e1f2016-07-08 17:14:16 -07006014 private int getEmphasizedActionLayoutResource() {
6015 return R.layout.notification_material_action_emphasized;
6016 }
6017
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006018 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07006019 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006020 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006021
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006022 private int getBackgroundColor(StandardTemplateParams p) {
6023 if (isColorized(p)) {
6024 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : getRawColor(p);
Selim Cinek7b9605b2017-01-19 17:36:00 -08006025 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07006026 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006027 }
6028 }
6029
Selim Cinek396caca2018-04-10 17:46:46 -07006030 /**
Selim Cinek4717d862018-04-19 09:19:15 +08006031 * Gets a neutral color that can be used for icons or similar that should not stand out.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006032 * @param p the template params to inflate this with
Selim Cinek4717d862018-04-19 09:19:15 +08006033 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006034 private int getNeutralColor(StandardTemplateParams p) {
6035 if (isColorized(p)) {
6036 return getSecondaryTextColor(p);
Selim Cinek4717d862018-04-19 09:19:15 +08006037 } else {
6038 return resolveNeutralColor();
6039 }
6040 }
6041
6042 /**
Selim Cinek396caca2018-04-10 17:46:46 -07006043 * Same as getBackgroundColor but also resolved the default color to the background.
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006044 * @param p the template params to inflate this with
Selim Cinek396caca2018-04-10 17:46:46 -07006045 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006046 private int resolveBackgroundColor(StandardTemplateParams p) {
6047 int backgroundColor = getBackgroundColor(p);
Selim Cinek396caca2018-04-10 17:46:46 -07006048 if (backgroundColor == COLOR_DEFAULT) {
6049 backgroundColor = mContext.getColor(
6050 com.android.internal.R.color.notification_material_background_color);
6051 }
6052 return backgroundColor;
6053 }
6054
Anthony Chenad4d1582017-04-10 16:07:58 -07006055 private boolean shouldTintActionButtons() {
6056 return mTintActionButtons;
6057 }
6058
Selim Cinek99104832017-01-25 14:47:33 -08006059 private boolean textColorsNeedInversion() {
6060 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
6061 return false;
6062 }
6063 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
6064 return targetSdkVersion > Build.VERSION_CODES.M
6065 && targetSdkVersion < Build.VERSION_CODES.O;
6066 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006067
6068 /**
6069 * Set a color palette to be used as the background and textColors
6070 *
6071 * @param backgroundColor the color to be used as the background
6072 * @param foregroundColor the color to be used as the foreground
6073 *
6074 * @hide
6075 */
6076 public void setColorPalette(int backgroundColor, int foregroundColor) {
6077 mBackgroundColor = backgroundColor;
6078 mForegroundColor = foregroundColor;
6079 mTextColorsAreForBackground = COLOR_INVALID;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006080 ensureColors(mParams.reset().fillTextsFrom(this));
Selim Cinek5fb73f82017-04-20 16:55:38 -07006081 }
Selim Cinekac5f0272017-05-02 16:05:41 -07006082
6083 /**
Selim Cineka7679b62017-05-10 16:33:25 -07006084 * Forces all styled remoteViews to be built from scratch and not use any cached
6085 * RemoteViews.
6086 * This is needed for legacy apps that are baking in their remoteviews into the
6087 * notification.
6088 *
6089 * @hide
6090 */
6091 public void setRebuildStyledRemoteViews(boolean rebuild) {
6092 mRebuildStyledRemoteViews = rebuild;
6093 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006094
6095 /**
6096 * Get the text that should be displayed in the statusBar when heads upped. This is
6097 * usually just the app name, but may be different depending on the style.
6098 *
6099 * @param publicMode If true, return a text that is safe to display in public.
6100 *
6101 * @hide
6102 */
6103 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
6104 if (mStyle != null && !publicMode) {
6105 CharSequence text = mStyle.getHeadsUpStatusBarText();
6106 if (!TextUtils.isEmpty(text)) {
6107 return text;
6108 }
6109 }
6110 return loadHeaderAppName();
6111 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08006112 }
6113
6114 /**
Selim Cinekd0426622017-07-11 13:19:59 +02006115 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
6116 * remote views.
6117 *
6118 * @hide
6119 */
6120 void reduceImageSizes(Context context) {
6121 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
6122 return;
6123 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006124 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006125 if (mLargeIcon != null || largeIcon != null) {
6126 Resources resources = context.getResources();
6127 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07006128 int maxWidth = resources.getDimensionPixelSize(isLowRam
6129 ? R.dimen.notification_right_icon_size_low_ram
6130 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006131 int maxHeight = maxWidth;
6132 if (MediaStyle.class.equals(style)
6133 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006134 maxHeight = resources.getDimensionPixelSize(isLowRam
6135 ? R.dimen.notification_media_image_max_height_low_ram
6136 : R.dimen.notification_media_image_max_height);
6137 maxWidth = resources.getDimensionPixelSize(isLowRam
6138 ? R.dimen.notification_media_image_max_width_low_ram
6139 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006140 }
6141 if (mLargeIcon != null) {
6142 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
6143 }
6144 if (largeIcon != null) {
6145 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
6146 }
6147 }
Selim Cineka8cb1262017-08-15 16:53:44 -07006148 reduceImageSizesForRemoteView(contentView, context, isLowRam);
6149 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
6150 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02006151 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
6152 }
6153
Selim Cineka8cb1262017-08-15 16:53:44 -07006154 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
6155 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02006156 if (remoteView != null) {
6157 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006158 int maxWidth = resources.getDimensionPixelSize(isLowRam
6159 ? R.dimen.notification_custom_view_max_image_width_low_ram
6160 : R.dimen.notification_custom_view_max_image_width);
6161 int maxHeight = resources.getDimensionPixelSize(isLowRam
6162 ? R.dimen.notification_custom_view_max_image_height_low_ram
6163 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02006164 remoteView.reduceImageSizes(maxWidth, maxHeight);
6165 }
6166 }
6167
6168 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006169 * @return whether this notification is a foreground service notification
Gus Prevasb0c1a462018-11-05 11:06:15 -05006170 * @hide
Selim Cinek7b9605b2017-01-19 17:36:00 -08006171 */
Gus Prevasb0c1a462018-11-05 11:06:15 -05006172 public boolean isForegroundService() {
Selim Cinek22714f12017-04-13 16:23:53 -07006173 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08006174 }
6175
6176 /**
Selim Cinek99104832017-01-25 14:47:33 -08006177 * @return whether this notification has a media session attached
6178 * @hide
6179 */
6180 public boolean hasMediaSession() {
6181 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
6182 }
6183
6184 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006185 * @return the style class of this notification
6186 * @hide
6187 */
6188 public Class<? extends Notification.Style> getNotificationStyle() {
6189 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
6190
6191 if (!TextUtils.isEmpty(templateClass)) {
6192 return Notification.getNotificationStyleClass(templateClass);
6193 }
6194 return null;
6195 }
6196
6197 /**
Selim Cinek22714f12017-04-13 16:23:53 -07006198 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08006199 *
6200 * @hide
6201 */
6202 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07006203 if (isColorizedMedia()) {
6204 return true;
6205 }
Julia Reynolds4db59552017-06-30 13:34:01 -04006206 return extras.getBoolean(EXTRA_COLORIZED)
6207 && (hasColorizedPermission() || isForegroundService());
6208 }
6209
6210 /**
6211 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
6212 * permission. The permission is checked when a notification is enqueued.
6213 */
6214 private boolean hasColorizedPermission() {
6215 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07006216 }
6217
6218 /**
6219 * @return true if this notification is colorized and it is a media notification
6220 *
6221 * @hide
6222 */
6223 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08006224 Class<? extends Style> style = getNotificationStyle();
6225 if (MediaStyle.class.equals(style)) {
6226 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
6227 if ((colorized == null || colorized) && hasMediaSession()) {
6228 return true;
6229 }
6230 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6231 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
6232 return true;
6233 }
6234 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07006235 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006236 }
6237
Selim Cinek0847acd2017-04-24 19:48:29 -07006238
6239 /**
6240 * @return true if this is a media notification
6241 *
6242 * @hide
6243 */
6244 public boolean isMediaNotification() {
6245 Class<? extends Style> style = getNotificationStyle();
6246 if (MediaStyle.class.equals(style)) {
6247 return true;
6248 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
6249 return true;
6250 }
6251 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006252 }
6253
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006254 /**
Julia Tuttle28fb1a92019-05-03 18:07:45 -04006255 * @return true if this notification is showing as a bubble
Mady Mellor7eb18ef2019-03-27 14:03:46 -07006256 *
6257 * @hide
6258 */
6259 public boolean isBubbleNotification() {
6260 return (flags & Notification.FLAG_BUBBLE) != 0;
6261 }
6262
Selim Cinek279fa862016-06-14 10:57:25 -07006263 private boolean hasLargeIcon() {
6264 return mLargeIcon != null || largeIcon != null;
6265 }
6266
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006267 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006268 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006269 * @hide
6270 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07006271 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07006272 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
6273 }
6274
6275 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07006276 * @return true if the notification will show a chronometer; false otherwise
6277 * @hide
6278 */
6279 public boolean showsChronometer() {
6280 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
6281 }
6282
6283 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04006284 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05006285 */
6286 @SystemApi
6287 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
6288 Class<? extends Style>[] classes = new Class[] {
6289 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
6290 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
6291 MessagingStyle.class };
6292 for (Class<? extends Style> innerClass : classes) {
6293 if (templateClass.equals(innerClass.getName())) {
6294 return innerClass;
6295 }
6296 }
6297 return null;
6298 }
6299
6300 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006301 * An object that can apply a rich notification style to a {@link Notification.Builder}
6302 * object.
6303 */
Griff Hazendfcb0802014-02-11 12:00:00 -08006304 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07006305
6306 /**
6307 * The number of items allowed simulatanously in the remote input history.
6308 * @hide
6309 */
6310 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04006311 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006312
6313 /**
6314 * @hide
6315 */
6316 protected CharSequence mSummaryText = null;
6317
6318 /**
6319 * @hide
6320 */
6321 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006322
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006323 protected Builder mBuilder;
6324
Chris Wrend6297db2012-05-03 16:20:13 -04006325 /**
6326 * Overrides ContentTitle in the big form of the template.
6327 * This defaults to the value passed to setContentTitle().
6328 */
6329 protected void internalSetBigContentTitle(CharSequence title) {
6330 mBigContentTitle = title;
6331 }
6332
6333 /**
6334 * Set the first line of text after the detail section in the big form of the template.
6335 */
6336 protected void internalSetSummaryText(CharSequence cs) {
6337 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006338 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006339 }
6340
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006341 public void setBuilder(Builder builder) {
6342 if (mBuilder != builder) {
6343 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006344 if (mBuilder != null) {
6345 mBuilder.setStyle(this);
6346 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006347 }
6348 }
6349
Chris Wrend6297db2012-05-03 16:20:13 -04006350 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006351 if (mBuilder == null) {
6352 throw new IllegalArgumentException("Style requires a valid Builder object");
6353 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006354 }
Chris Wrend6297db2012-05-03 16:20:13 -04006355
6356 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006357 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
6358 return getStandardView(layoutId, p, null);
Selim Cinek384804b2018-04-18 14:31:07 +08006359 }
6360
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006361
Selim Cinek384804b2018-04-18 14:31:07 +08006362 /**
6363 * Get the standard view for this style.
6364 *
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006365 * @param layoutId The layout id to use.
6366 * @param p the params for this inflation.
Selim Cinek384804b2018-04-18 14:31:07 +08006367 * @param result The result where template bind information is saved.
6368 * @return A remoteView for this style.
6369 * @hide
6370 */
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006371 protected RemoteViews getStandardView(int layoutId, StandardTemplateParams p,
6372 TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006373 checkBuilder();
6374
6375 if (mBigContentTitle != null) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006376 p.title = mBigContentTitle;
Chris Wrend6297db2012-05-03 16:20:13 -04006377 }
6378
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006379 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, p,
6380 result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006381
Chris Wrend6297db2012-05-03 16:20:13 -04006382 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6383 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006384 } else {
6385 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006386 }
6387
Chris Wrend6297db2012-05-03 16:20:13 -04006388 return contentView;
6389 }
6390
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006391 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006392 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006393 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006394 *
6395 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006396 * @hide
6397 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006398 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006399 return null;
6400 }
6401
6402 /**
6403 * Construct a Style-specific RemoteViews for the final big notification layout.
6404 * @hide
6405 */
6406 public RemoteViews makeBigContentView() {
6407 return null;
6408 }
6409
6410 /**
6411 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006412 *
6413 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006414 * @hide
6415 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006416 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006417 return null;
6418 }
6419
6420 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006421 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006422 * @hide
6423 */
6424 public void addExtras(Bundle extras) {
6425 if (mSummaryTextSet) {
6426 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6427 }
6428 if (mBigContentTitle != null) {
6429 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6430 }
Chris Wren91ad5632013-06-05 15:05:57 -04006431 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006432 }
6433
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006434 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006435 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006436 * @hide
6437 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006438 protected void restoreFromExtras(Bundle extras) {
6439 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6440 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6441 mSummaryTextSet = true;
6442 }
6443 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6444 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6445 }
6446 }
6447
6448
6449 /**
6450 * @hide
6451 */
6452 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006453 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006454 return wip;
6455 }
6456
Daniel Sandler0ec46202015-06-24 01:27:05 -04006457 /**
6458 * @hide
6459 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006460 public void purgeResources() {}
6461
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006462 /**
6463 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6464 * attached to.
6465 *
6466 * @return the fully constructed Notification.
6467 */
6468 public Notification build() {
6469 checkBuilder();
6470 return mBuilder.build();
6471 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006472
6473 /**
6474 * @hide
6475 * @return true if the style positions the progress bar on the second line; false if the
6476 * style hides the progress bar
6477 */
6478 protected boolean hasProgress() {
6479 return true;
6480 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006481
6482 /**
6483 * @hide
6484 * @return Whether we should put the summary be put into the notification header
6485 */
6486 public boolean hasSummaryInHeader() {
6487 return true;
6488 }
Selim Cinek593610c2016-02-16 18:42:57 -08006489
6490 /**
6491 * @hide
6492 * @return Whether custom content views are displayed inline in the style
6493 */
6494 public boolean displayCustomViewInline() {
6495 return false;
6496 }
Selim Cinekd0426622017-07-11 13:19:59 +02006497
6498 /**
6499 * Reduces the image sizes contained in this style.
6500 *
6501 * @hide
6502 */
6503 public void reduceImageSizes(Context context) {
6504 }
Selim Cinek90343862018-02-01 11:07:11 -08006505
6506 /**
6507 * Validate that this style was properly composed. This is called at build time.
6508 * @hide
6509 */
6510 public void validate(Context context) {
6511 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006512
6513 /**
6514 * @hide
6515 */
6516 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006517
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006518 /**
koprivaa1a78482018-10-09 10:09:23 -07006519 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006520 * If {@code null} is returned, the default implementation will be used.
6521 *
6522 * @hide
6523 */
6524 public CharSequence getHeadsUpStatusBarText() {
6525 return null;
6526 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006527 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006528
6529 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006530 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006531 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006532 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006533 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006534 * Notification notif = new Notification.Builder(mContext)
6535 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6536 * .setContentText(subject)
6537 * .setSmallIcon(R.drawable.new_post)
6538 * .setLargeIcon(aBitmap)
6539 * .setStyle(new Notification.BigPictureStyle()
6540 * .bigPicture(aBigBitmap))
6541 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006542 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006543 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006544 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006545 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006546 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006547 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006548 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006549 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006550
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006551 public BigPictureStyle() {
6552 }
6553
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006554 /**
6555 * @deprecated use {@code BigPictureStyle()}.
6556 */
6557 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006558 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006559 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006560 }
6561
Chris Wrend6297db2012-05-03 16:20:13 -04006562 /**
6563 * Overrides ContentTitle in the big form of the template.
6564 * This defaults to the value passed to setContentTitle().
6565 */
6566 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006567 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006568 return this;
6569 }
6570
6571 /**
6572 * Set the first line of text after the detail section in the big form of the template.
6573 */
6574 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006575 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006576 return this;
6577 }
6578
Chris Wren0bd664d2012-08-01 13:56:56 -04006579 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006580 * @hide
6581 */
6582 public Bitmap getBigPicture() {
6583 return mPicture;
6584 }
6585
6586 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006587 * Provide the bitmap to be used as the payload for the BigPicture notification.
6588 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006589 public BigPictureStyle bigPicture(Bitmap b) {
6590 mPicture = b;
6591 return this;
6592 }
6593
Chris Wren3745a3d2012-05-22 15:11:52 -04006594 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006595 * Override the large icon when the big notification is shown.
6596 */
6597 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006598 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6599 }
6600
6601 /**
6602 * Override the large icon when the big notification is shown.
6603 */
6604 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006605 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006606 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006607 return this;
6608 }
6609
Riley Andrews0394a0c2015-11-03 23:36:52 -08006610 /** @hide */
6611 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6612
Daniel Sandler0ec46202015-06-24 01:27:05 -04006613 /**
6614 * @hide
6615 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006616 @Override
6617 public void purgeResources() {
6618 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006619 if (mPicture != null &&
6620 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006621 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006622 mPicture = mPicture.createAshmemBitmap();
6623 }
6624 if (mBigLargeIcon != null) {
6625 mBigLargeIcon.convertToAshmem();
6626 }
6627 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006628
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006629 /**
6630 * @hide
6631 */
Selim Cinekd0426622017-07-11 13:19:59 +02006632 @Override
6633 public void reduceImageSizes(Context context) {
6634 super.reduceImageSizes(context);
6635 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006636 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006637 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006638 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6639 ? R.dimen.notification_big_picture_max_height_low_ram
6640 : R.dimen.notification_big_picture_max_height);
6641 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6642 ? R.dimen.notification_big_picture_max_width_low_ram
6643 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006644 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6645 }
6646 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006647 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6648 ? R.dimen.notification_right_icon_size_low_ram
6649 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006650 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6651 }
6652 }
6653
6654 /**
6655 * @hide
6656 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006657 public RemoteViews makeBigContentView() {
6658 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006659 // This covers the following cases:
6660 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006661 // mN.mLargeIcon
6662 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006663 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006664 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006665 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006666 oldLargeIcon = mBuilder.mN.mLargeIcon;
6667 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006668 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6669 // replacement if the other one is null. Because we're restoring these legacy icons
6670 // for old listeners, this is in general non-null.
6671 largeIconLegacy = mBuilder.mN.largeIcon;
6672 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006673 }
6674
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006675 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder);
Selim Cinek384804b2018-04-18 14:31:07 +08006676 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006677 p, null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006678 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006679 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6680 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006681 mBuilder.setTextViewColorSecondary(contentView, R.id.text, p);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006682 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006683 }
Selim Cinek279fa862016-06-14 10:57:25 -07006684 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006685
Christoph Studer5c510ee2014-12-15 16:32:27 +01006686 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006687 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006688 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006689 }
6690
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006691 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006692 return contentView;
6693 }
6694
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006695 /**
6696 * @hide
6697 */
6698 public void addExtras(Bundle extras) {
6699 super.addExtras(extras);
6700
6701 if (mBigLargeIconSet) {
6702 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6703 }
6704 extras.putParcelable(EXTRA_PICTURE, mPicture);
6705 }
6706
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006707 /**
6708 * @hide
6709 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006710 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006711 protected void restoreFromExtras(Bundle extras) {
6712 super.restoreFromExtras(extras);
6713
6714 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006715 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006716 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006717 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006718 mPicture = extras.getParcelable(EXTRA_PICTURE);
6719 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006720
6721 /**
6722 * @hide
6723 */
6724 @Override
6725 public boolean hasSummaryInHeader() {
6726 return false;
6727 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006728
6729 /**
6730 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006731 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6732 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006733 */
6734 @Override
6735 public boolean areNotificationsVisiblyDifferent(Style other) {
6736 if (other == null || getClass() != other.getClass()) {
6737 return true;
6738 }
6739 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006740 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6741 }
6742
6743 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6744 if (a == b) {
6745 return false;
6746 }
6747 if (a == null || b == null) {
6748 return true;
6749 }
6750 return a.getWidth() != b.getWidth()
6751 || a.getHeight() != b.getHeight()
6752 || a.getConfig() != b.getConfig()
6753 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006754 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006755 }
6756
6757 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006758 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006759 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006760 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006761 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006762 * Notification notif = new Notification.Builder(mContext)
6763 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6764 * .setContentText(subject)
6765 * .setSmallIcon(R.drawable.new_mail)
6766 * .setLargeIcon(aBitmap)
6767 * .setStyle(new Notification.BigTextStyle()
6768 * .bigText(aVeryLongString))
6769 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006770 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006771 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006772 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006773 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006774 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006775
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006776 private CharSequence mBigText;
6777
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006778 public BigTextStyle() {
6779 }
6780
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006781 /**
6782 * @deprecated use {@code BigTextStyle()}.
6783 */
6784 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006785 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006786 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006787 }
6788
Chris Wrend6297db2012-05-03 16:20:13 -04006789 /**
6790 * Overrides ContentTitle in the big form of the template.
6791 * This defaults to the value passed to setContentTitle().
6792 */
6793 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006794 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006795 return this;
6796 }
6797
6798 /**
6799 * Set the first line of text after the detail section in the big form of the template.
6800 */
6801 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006802 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006803 return this;
6804 }
6805
Chris Wren0bd664d2012-08-01 13:56:56 -04006806 /**
6807 * Provide the longer text to be displayed in the big form of the
6808 * template in place of the content text.
6809 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006810 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006811 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006812 return this;
6813 }
6814
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006815 /**
6816 * @hide
6817 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006818 public CharSequence getBigText() {
6819 return mBigText;
6820 }
6821
6822 /**
6823 * @hide
6824 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006825 public void addExtras(Bundle extras) {
6826 super.addExtras(extras);
6827
Christoph Studer4600f9b2014-07-22 22:44:43 +02006828 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6829 }
6830
6831 /**
6832 * @hide
6833 */
6834 @Override
6835 protected void restoreFromExtras(Bundle extras) {
6836 super.restoreFromExtras(extras);
6837
6838 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006839 }
6840
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006841 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006842 * @param increasedHeight true if this layout be created with an increased height.
6843 *
6844 * @hide
6845 */
6846 @Override
6847 public RemoteViews makeContentView(boolean increasedHeight) {
6848 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006849 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006850 mBuilder.mActions = new ArrayList<>();
6851 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006852 mBuilder.mActions = mBuilder.mOriginalActions;
6853 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006854 return remoteViews;
6855 }
6856 return super.makeContentView(increasedHeight);
6857 }
6858
6859 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006860 * @hide
6861 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006862 @Override
6863 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6864 if (increasedHeight && mBuilder.mActions.size() > 0) {
6865 return makeBigContentView();
6866 }
6867 return super.makeHeadsUpContentView(increasedHeight);
6868 }
6869
6870 /**
6871 * @hide
6872 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006873 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006874 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08006875 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006876 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), p,
6877 result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006878 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006879
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006880 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006881 if (TextUtils.isEmpty(bigTextText)) {
6882 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6883 // experience
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006884 bigTextText = mBuilder.processLegacyText(
6885 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT));
Selim Cinek75998782016-04-26 10:39:17 -07006886 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08006887 contentView.setTextViewText(R.id.big_text, mBuilder.processTextSpans(bigTextText));
6888 mBuilder.setTextViewColorSecondary(contentView, R.id.big_text, p);
6889 contentView.setViewVisibility(R.id.big_text,
6890 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek6e41b0e2018-12-06 18:10:20 -08006891 contentView.setBoolean(R.id.big_text, "setHasImage",
6892 result.isRightIconContainerVisible());
Selim Cinek4fb12d32015-11-19 18:10:48 -08006893
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006894 return contentView;
6895 }
6896
Julia Reynolds7217dc92018-03-07 12:12:09 -05006897 /**
6898 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006899 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006900 */
6901 @Override
6902 public boolean areNotificationsVisiblyDifferent(Style other) {
6903 if (other == null || getClass() != other.getClass()) {
6904 return true;
6905 }
6906 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006907 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006908 }
6909
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006910 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006911
6912 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006913 * Helper class for generating large-format notifications that include multiple back-and-forth
6914 * messages of varying types between any number of people.
6915 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006916 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006917 * If the platform does not provide large-format notifications, this method has no effect. The
6918 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006919 *
6920 * <p>
6921 * If the app is targeting Android P and above, it is required to use the {@link Person}
6922 * class in order to get an optimal rendering of the notification and its avatars. For
6923 * conversations involving multiple people, the app should also make sure that it marks the
6924 * conversation as a group with {@link #setGroupConversation(boolean)}.
6925 *
6926 * <p>
6927 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6928 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006929 * <pre class="prettyprint">
6930 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006931 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6932 * MessagingStyle style = new MessagingStyle(user)
6933 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6934 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6935 * .setGroupConversation(hasMultiplePeople());
6936 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006937 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006938 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006939 * .setContentText(subject)
6940 * .setSmallIcon(R.drawable.new_message)
6941 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006942 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006943 * .build();
6944 * </pre>
6945 */
6946 public static class MessagingStyle extends Style {
6947
6948 /**
6949 * The maximum number of messages that will be retained in the Notification itself (the
6950 * number displayed is up to the platform).
6951 */
6952 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6953
Selim Cinekcb8b9852017-12-15 18:01:52 -08006954 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006955 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006956 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006957 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006958 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006959
6960 MessagingStyle() {
6961 }
6962
6963 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006964 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6965 * user before the posting app reposts the notification with those messages after they've
6966 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006967 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006968 *
6969 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006970 */
Alex Hillsfd590442016-10-07 09:52:44 -04006971 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006972 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006973 }
6974
6975 /**
6976 * @param user Required - The person displayed for any messages that are sent by the
6977 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6978 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006979 * by this user. The user also needs to have a valid name associated with it, which will
6980 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006981 */
6982 public MessagingStyle(@NonNull Person user) {
6983 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006984 }
6985
6986 /**
6987 * Validate that this style was properly composed. This is called at build time.
6988 * @hide
6989 */
6990 @Override
6991 public void validate(Context context) {
6992 super.validate(context);
6993 if (context.getApplicationInfo().targetSdkVersion
6994 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6995 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006996 }
6997 }
6998
6999 /**
koprivaa1a78482018-10-09 10:09:23 -07007000 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08007001 * If {@code null} is returned, the default implementation will be used.
7002 *
7003 * @hide
7004 */
7005 @Override
7006 public CharSequence getHeadsUpStatusBarText() {
7007 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
7008 ? super.mBigContentTitle
7009 : mConversationTitle;
7010 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
7011 return conversationTitle;
7012 }
7013 return null;
7014 }
7015
7016 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08007017 * @return the user to be displayed for any replies sent by the user
7018 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007019 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08007020 public Person getUser() {
7021 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04007022 }
7023
7024 /**
7025 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08007026 *
7027 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04007028 */
7029 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007030 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04007031 }
7032
7033 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007034 * Sets the title to be displayed on this conversation. May be set to {@code null}.
7035 *
Kodlee Yin14656422017-12-22 17:00:46 -08007036 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
7037 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
7038 * conversation title to a non-null value will make {@link #isGroupConversation()} return
7039 * {@code true} and passing {@code null} will make it return {@code false}. In
7040 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
7041 * to set group conversation status.
7042 *
7043 * @param conversationTitle Title displayed for this conversation
7044 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04007045 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007046 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007047 mConversationTitle = conversationTitle;
7048 return this;
7049 }
7050
7051 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007052 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04007053 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007054 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04007055 public CharSequence getConversationTitle() {
7056 return mConversationTitle;
7057 }
7058
7059 /**
7060 * Adds a message for display by this notification. Convenience call for a simple
7061 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7062 * @param text A {@link CharSequence} to be displayed as the message content
7063 * @param timestamp Time at which the message arrived
7064 * @param sender A {@link CharSequence} to be used for displaying the name of the
7065 * sender. Should be <code>null</code> for messages by the current user, in which case
7066 * the platform will insert {@link #getUserDisplayName()}.
7067 * Should be unique amongst all individuals in the conversation, and should be
7068 * consistent during re-posts of the notification.
7069 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007070 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04007071 *
7072 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08007073 *
7074 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007075 */
7076 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007077 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07007078 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007079 }
7080
7081 /**
7082 * Adds a message for display by this notification. Convenience call for a simple
7083 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
7084 * @param text A {@link CharSequence} to be displayed as the message content
7085 * @param timestamp Time at which the message arrived
7086 * @param sender The {@link Person} who sent the message.
7087 * Should be <code>null</code> for messages by the current user, in which case
7088 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7089 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07007090 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08007091 *
7092 * @return this object for method chaining
7093 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007094 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
7095 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08007096 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04007097 }
7098
7099 /**
7100 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08007101 *
7102 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7103 * the newest last.
7104 *
Alex Hillsfc737de2016-03-23 17:33:02 -04007105 * @param message The {@link Message} to be displayed
7106 * @return this object for method chaining
7107 */
7108 public MessagingStyle addMessage(Message message) {
7109 mMessages.add(message);
7110 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7111 mMessages.remove(0);
7112 }
7113 return this;
7114 }
7115
7116 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007117 * Adds a {@link Message} for historic context in this notification.
7118 *
7119 * <p>Messages should be added as historic if they are not the main subject of the
7120 * notification but may give context to a conversation. The system may choose to present
7121 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
7122 *
7123 * <p>The messages should be added in chronologic order, i.e. the oldest first,
7124 * the newest last.
7125 *
7126 * @param message The historic {@link Message} to be added
7127 * @return this object for method chaining
7128 */
7129 public MessagingStyle addHistoricMessage(Message message) {
7130 mHistoricMessages.add(message);
7131 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
7132 mHistoricMessages.remove(0);
7133 }
7134 return this;
7135 }
7136
7137 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007138 * Gets the list of {@code Message} objects that represent the notification
7139 */
7140 public List<Message> getMessages() {
7141 return mMessages;
7142 }
7143
7144 /**
Adrian Roos437cd562017-01-18 15:47:03 -08007145 * Gets the list of historic {@code Message}s in the notification.
7146 */
7147 public List<Message> getHistoricMessages() {
7148 return mHistoricMessages;
7149 }
7150
7151 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07007152 * Sets whether this conversation notification represents a group. If the app is targeting
7153 * Android P, this is required if the app wants to display the largeIcon set with
7154 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08007155 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007156 * @param isGroupConversation {@code true} if the conversation represents a group,
7157 * {@code false} otherwise.
7158 * @return this object for method chaining
7159 */
7160 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
7161 mIsGroupConversation = isGroupConversation;
7162 return this;
7163 }
7164
7165 /**
Kodlee Yin14656422017-12-22 17:00:46 -08007166 * Returns {@code true} if this notification represents a group conversation, otherwise
7167 * {@code false}.
7168 *
7169 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
7170 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
7171 * not the conversation title is set; returning {@code true} if the conversation title is
7172 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
7173 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
7174 * named, non-group conversations.
7175 *
7176 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007177 */
7178 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08007179 // When target SDK version is < P, a non-null conversation title dictates if this is
7180 // as group conversation.
7181 if (mBuilder != null
7182 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
7183 < Build.VERSION_CODES.P) {
7184 return mConversationTitle != null;
7185 }
7186
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007187 return mIsGroupConversation;
7188 }
7189
7190 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007191 * @hide
7192 */
7193 @Override
7194 public void addExtras(Bundle extras) {
7195 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007196 if (mUser != null) {
7197 // For legacy usages
7198 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
7199 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04007200 }
7201 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007202 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04007203 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007204 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
7205 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04007206 }
Adrian Roos437cd562017-01-18 15:47:03 -08007207 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
7208 Message.getBundleArrayForMessages(mHistoricMessages));
7209 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007210
7211 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007212 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007213 }
7214
7215 private void fixTitleAndTextExtras(Bundle extras) {
7216 Message m = findLatestIncomingMessage();
7217 CharSequence text = (m == null) ? null : m.mText;
7218 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08007219 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
7220 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007221 CharSequence title;
7222 if (!TextUtils.isEmpty(mConversationTitle)) {
7223 if (!TextUtils.isEmpty(sender)) {
7224 BidiFormatter bidi = BidiFormatter.getInstance();
7225 title = mBuilder.mContext.getString(
7226 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08007227 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007228 } else {
7229 title = mConversationTitle;
7230 }
7231 } else {
7232 title = sender;
7233 }
7234
7235 if (title != null) {
7236 extras.putCharSequence(EXTRA_TITLE, title);
7237 }
7238 if (text != null) {
7239 extras.putCharSequence(EXTRA_TEXT, text);
7240 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007241 }
7242
7243 /**
7244 * @hide
7245 */
7246 @Override
7247 protected void restoreFromExtras(Bundle extras) {
7248 super.restoreFromExtras(extras);
7249
Selim Cinekcb8b9852017-12-15 18:01:52 -08007250 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
7251 if (mUser == null) {
7252 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07007253 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007254 }
Adrian Roos96b7e202016-05-17 13:50:38 -07007255 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08007256 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007257 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08007258 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07007259 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08007260 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04007261 }
7262
7263 /**
7264 * @hide
7265 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07007266 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007267 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07007268 mBuilder.mOriginalActions = mBuilder.mActions;
7269 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007270 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007271 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007272 mBuilder.mActions = mBuilder.mOriginalActions;
7273 mBuilder.mOriginalActions = null;
7274 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07007275 }
7276
Julia Reynolds7217dc92018-03-07 12:12:09 -05007277 /**
7278 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04007279 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05007280 */
7281 @Override
7282 public boolean areNotificationsVisiblyDifferent(Style other) {
7283 if (other == null || getClass() != other.getClass()) {
7284 return true;
7285 }
7286 MessagingStyle newS = (MessagingStyle) other;
7287 List<MessagingStyle.Message> oldMs = getMessages();
7288 List<MessagingStyle.Message> newMs = newS.getMessages();
7289
Dan Sandler7d67bd42018-05-15 14:06:38 -04007290 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007291 newMs = new ArrayList<>();
7292 }
7293
7294 int n = oldMs.size();
7295 if (n != newMs.size()) {
7296 return true;
7297 }
7298 for (int i = 0; i < n; i++) {
7299 MessagingStyle.Message oldM = oldMs.get(i);
7300 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04007301 if (!Objects.equals(
7302 String.valueOf(oldM.getText()),
7303 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05007304 return true;
7305 }
7306 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
7307 return true;
7308 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04007309 String oldSender = String.valueOf(oldM.getSenderPerson() == null
7310 ? oldM.getSender()
7311 : oldM.getSenderPerson().getName());
7312 String newSender = String.valueOf(newM.getSenderPerson() == null
7313 ? newM.getSender()
7314 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007315 if (!Objects.equals(oldSender, newSender)) {
7316 return true;
7317 }
7318
7319 String oldKey = oldM.getSenderPerson() == null
7320 ? null : oldM.getSenderPerson().getKey();
7321 String newKey = newM.getSenderPerson() == null
7322 ? null : newM.getSenderPerson().getKey();
7323 if (!Objects.equals(oldKey, newKey)) {
7324 return true;
7325 }
7326 // Other fields (like timestamp) intentionally excluded
7327 }
7328 return false;
7329 }
7330
Adrian Roosc1a80b02016-04-05 14:54:55 -07007331 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007332 return findLatestIncomingMessage(mMessages);
7333 }
7334
7335 /**
7336 * @hide
7337 */
7338 @Nullable
7339 public static Message findLatestIncomingMessage(
7340 List<Message> messages) {
7341 for (int i = messages.size() - 1; i >= 0; i--) {
7342 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007343 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007344 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007345 return m;
7346 }
7347 }
Selim Cinek88188f22017-09-19 16:46:56 -07007348 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007349 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007350 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007351 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007352 return null;
7353 }
7354
7355 /**
7356 * @hide
7357 */
7358 @Override
7359 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007360 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007361 }
7362
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007363 /**
7364 * Create a messaging layout.
7365 *
7366 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7367 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007368 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007369 * @return the created remoteView.
7370 */
Selim Cinekafeed292017-12-12 17:32:44 -08007371 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007372 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007373 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007374 ? super.mBigContentTitle
7375 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007376 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7377 >= Build.VERSION_CODES.P;
7378 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007379 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007380 Icon avatarReplacement = null;
7381 if (!atLeastP) {
7382 isOneToOne = TextUtils.isEmpty(conversationTitle);
7383 avatarReplacement = mBuilder.mN.mLargeIcon;
7384 if (hasOnlyWhiteSpaceSenders()) {
7385 isOneToOne = true;
7386 nameReplacement = conversationTitle;
7387 conversationTitle = null;
7388 }
7389 } else {
7390 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007391 }
Selim Cinek384804b2018-04-18 14:31:07 +08007392 TemplateBindResult bindResult = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007393 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).title(
7394 conversationTitle).text(null)
7395 .hideLargeIcon(hideRightIcons || isOneToOne)
7396 .hideReplyIcon(hideRightIcons)
7397 .headerTextSecondary(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07007398 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007399 mBuilder.getMessagingLayoutResource(),
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007400 p,
Selim Cinek384804b2018-04-18 14:31:07 +08007401 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007402 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007403 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007404 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7405 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007406 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007407 mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
7408 : mBuilder.resolveContrastColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007409 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007410 mBuilder.getPrimaryTextColor(p));
Kenny Guy14d035c2018-05-02 19:10:36 +01007411 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007412 mBuilder.getSecondaryTextColor(p));
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007413 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7414 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007415 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7416 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007417 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7418 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007419 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7420 isOneToOne);
7421 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7422 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007423 return contentView;
7424 }
7425
Selim Cinekf7409db2017-10-24 16:17:14 -07007426 private boolean hasOnlyWhiteSpaceSenders() {
7427 for (int i = 0; i < mMessages.size(); i++) {
7428 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007429 Person sender = m.getSenderPerson();
7430 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007431 return false;
7432 }
7433 }
7434 return true;
7435 }
7436
7437 private boolean isWhiteSpace(CharSequence sender) {
7438 if (TextUtils.isEmpty(sender)) {
7439 return true;
7440 }
7441 if (sender.toString().matches("^\\s*$")) {
7442 return true;
7443 }
7444 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7445 // For the presentation that we had.
7446 for (int i = 0; i < sender.length(); i++) {
7447 char c = sender.charAt(i);
7448 if (c != '\u200B') {
7449 return false;
7450 }
7451 }
7452 return true;
7453 }
7454
Selim Cinek88188f22017-09-19 16:46:56 -07007455 private CharSequence createConversationTitleFromMessages() {
7456 ArraySet<CharSequence> names = new ArraySet<>();
7457 for (int i = 0; i < mMessages.size(); i++) {
7458 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007459 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007460 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007461 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007462 }
7463 }
7464 SpannableStringBuilder title = new SpannableStringBuilder();
7465 int size = names.size();
7466 for (int i = 0; i < size; i++) {
7467 CharSequence name = names.valueAt(i);
7468 if (!TextUtils.isEmpty(title)) {
7469 title.append(", ");
7470 }
7471 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7472 }
7473 return title;
7474 }
7475
Adrian Roosdedd1df2016-04-26 16:38:47 -07007476 /**
7477 * @hide
7478 */
7479 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007480 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007481 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007482 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007483 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7484 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007485 }
7486
Adrian Roosc1a80b02016-04-05 14:54:55 -07007487 private static TextAppearanceSpan makeFontColorSpan(int color) {
7488 return new TextAppearanceSpan(null, 0, 0,
7489 ColorStateList.valueOf(color), null);
7490 }
7491
Alex Hillsd9b04d92016-04-11 16:38:16 -04007492 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007493 /** @hide */
7494 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007495 static final String KEY_TIMESTAMP = "time";
7496 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007497 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007498 static final String KEY_DATA_MIME_TYPE = "type";
7499 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007500 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007501 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007502
7503 private final CharSequence mText;
7504 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007505 @Nullable
7506 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007507 /** True if this message was generated from the extra
7508 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7509 */
7510 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007511
Shane Brennan5a871862017-03-11 13:14:17 -08007512 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007513 private String mDataMimeType;
7514 private Uri mDataUri;
7515
7516 /**
7517 * Constructor
7518 * @param text A {@link CharSequence} to be displayed as the message content
7519 * @param timestamp Time at which the message arrived
7520 * @param sender A {@link CharSequence} to be used for displaying the name of the
7521 * sender. Should be <code>null</code> for messages by the current user, in which case
7522 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7523 * Should be unique amongst all individuals in the conversation, and should be
7524 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007525 *
7526 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007527 */
7528 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007529 this(text, timestamp, sender == null ? null
7530 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007531 }
7532
7533 /**
7534 * Constructor
7535 * @param text A {@link CharSequence} to be displayed as the message content
7536 * @param timestamp Time at which the message arrived
7537 * @param sender The {@link Person} who sent the message.
7538 * Should be <code>null</code> for messages by the current user, in which case
7539 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7540 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007541 * The person provided should contain an Icon, set with
7542 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7543 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7544 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7545 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007546 * </p>
7547 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007548 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007549 this(text, timestamp, sender, false /* remoteHistory */);
7550 }
7551
7552 /**
7553 * Constructor
7554 * @param text A {@link CharSequence} to be displayed as the message content
7555 * @param timestamp Time at which the message arrived
7556 * @param sender The {@link Person} who sent the message.
7557 * Should be <code>null</code> for messages by the current user, in which case
7558 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7559 * @param remoteInputHistory True if the messages was generated from the extra
7560 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7561 * <p>
7562 * The person provided should contain an Icon, set with
7563 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7564 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7565 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7566 * to differentiate between the different users.
7567 * </p>
7568 * @hide
7569 */
7570 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7571 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007572 mText = text;
7573 mTimestamp = timestamp;
7574 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007575 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007576 }
7577
7578 /**
7579 * Sets a binary blob of data and an associated MIME type for a message. In the case
7580 * where the platform doesn't support the MIME type, the original text provided in the
7581 * constructor will be used.
7582 * @param dataMimeType The MIME type of the content. See
7583 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7584 * types on Android and Android Wear.
7585 * @param dataUri The uri containing the content whose type is given by the MIME type.
7586 * <p class="note">
7587 * <ol>
7588 * <li>Notification Listeners including the System UI need permission to access the
7589 * data the Uri points to. The recommended ways to do this are:</li>
7590 * <li>Store the data in your own ContentProvider, making sure that other apps have
7591 * the correct permission to access your provider. The preferred mechanism for
7592 * providing access is to use per-URI permissions which are temporary and only
7593 * grant access to the receiving application. An easy way to create a
7594 * ContentProvider like this is to use the FileProvider helper class.</li>
7595 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7596 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7597 * also store non-media types (see MediaStore.Files for more info). Files can be
7598 * inserted into the MediaStore using scanFile() after which a content:// style
7599 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7600 * Note that once added to the system MediaStore the content is accessible to any
7601 * app on the device.</li>
7602 * </ol>
7603 * @return this object for method chaining
7604 */
7605 public Message setData(String dataMimeType, Uri dataUri) {
7606 mDataMimeType = dataMimeType;
7607 mDataUri = dataUri;
7608 return this;
7609 }
7610
Alex Hillsfc737de2016-03-23 17:33:02 -04007611 /**
7612 * Get the text to be used for this message, or the fallback text if a type and content
7613 * Uri have been set
7614 */
7615 public CharSequence getText() {
7616 return mText;
7617 }
7618
7619 /**
7620 * Get the time at which this message arrived
7621 */
7622 public long getTimestamp() {
7623 return mTimestamp;
7624 }
7625
7626 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007627 * Get the extras Bundle for this message.
7628 */
7629 public Bundle getExtras() {
7630 return mExtras;
7631 }
7632
7633 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007634 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007635 *
7636 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007637 */
7638 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007639 return mSender == null ? null : mSender.getName();
7640 }
7641
7642 /**
7643 * Get the sender associated with this message.
7644 */
7645 @Nullable
7646 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007647 return mSender;
7648 }
7649
7650 /**
7651 * Get the MIME type of the data pointed to by the Uri
7652 */
7653 public String getDataMimeType() {
7654 return mDataMimeType;
7655 }
7656
7657 /**
koprivaa1a78482018-10-09 10:09:23 -07007658 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007659 * {@see #getText()} is used.
7660 */
7661 public Uri getDataUri() {
7662 return mDataUri;
7663 }
7664
Kenny Guya0f6de82018-04-06 16:20:16 +01007665 /**
7666 * @return True if the message was generated from
7667 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7668 * @hide
7669 */
7670 public boolean isRemoteInputHistory() {
7671 return mRemoteInputHistory;
7672 }
7673
Beverlye98937a2018-11-15 10:18:57 -05007674 /**
7675 * @hide
7676 */
7677 @VisibleForTesting
7678 public Bundle toBundle() {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007679 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007680 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007681 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007682 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007683 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007684 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007685 // Legacy listeners need this
7686 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7687 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007688 }
7689 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007690 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007691 }
7692 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007693 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007694 }
Shane Brennan5a871862017-03-11 13:14:17 -08007695 if (mExtras != null) {
7696 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7697 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007698 if (mRemoteInputHistory) {
7699 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7700 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007701 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007702 }
7703
Alex Hillsd9b04d92016-04-11 16:38:16 -04007704 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7705 Bundle[] bundles = new Bundle[messages.size()];
7706 final int N = messages.size();
7707 for (int i = 0; i < N; i++) {
7708 bundles[i] = messages.get(i).toBundle();
7709 }
7710 return bundles;
7711 }
7712
Selim Cinek88188f22017-09-19 16:46:56 -07007713 /**
Julia Reynolds5cb8d5a2019-09-03 11:46:31 -04007714 * Returns a list of messages read from the given bundle list, e.g.
7715 * {@link #EXTRA_MESSAGES} or {@link #EXTRA_HISTORIC_MESSAGES}.
Selim Cinek88188f22017-09-19 16:46:56 -07007716 */
Julia Reynolds5cb8d5a2019-09-03 11:46:31 -04007717 @NonNull
7718 public static List<Message> getMessagesFromBundleArray(@Nullable Parcelable[] bundles) {
Selim Cinek88188f22017-09-19 16:46:56 -07007719 if (bundles == null) {
7720 return new ArrayList<>();
7721 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007722 List<Message> messages = new ArrayList<>(bundles.length);
7723 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007724 if (bundles[i] instanceof Bundle) {
7725 Message message = getMessageFromBundle((Bundle)bundles[i]);
7726 if (message != null) {
7727 messages.add(message);
7728 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007729 }
7730 }
7731 return messages;
7732 }
7733
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007734 /**
Julia Reynolds5cb8d5a2019-09-03 11:46:31 -04007735 * Returns the message that is stored in the bundle (e.g. one of the values in the lists
7736 * in {@link #EXTRA_MESSAGES} or {@link #EXTRA_HISTORIC_MESSAGES}) or null if the
7737 * message couldn't be resolved.
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007738 * @hide
7739 */
7740 @Nullable
Julia Reynoldsa10437f42019-08-16 13:43:12 -04007741 public static Message getMessageFromBundle(@NonNull Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007742 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007743 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007744 return null;
7745 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007746
7747 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7748 if (senderPerson == null) {
7749 // Legacy apps that use compat don't actually provide the sender objects
7750 // We need to fix the compat version to provide people / use
7751 // the native api instead
7752 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7753 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007754 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007755 }
7756 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007757 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007758 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007759 senderPerson,
7760 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007761 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7762 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007763 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7764 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007765 }
Shane Brennan5a871862017-03-11 13:14:17 -08007766 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7767 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7768 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007769 return message;
7770 }
7771 } catch (ClassCastException e) {
7772 return null;
7773 }
7774 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007775 }
7776 }
7777
7778 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007779 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007780 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007781 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007782 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007783 * Notification notif = new Notification.Builder(mContext)
7784 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7785 * .setContentText(subject)
7786 * .setSmallIcon(R.drawable.new_mail)
7787 * .setLargeIcon(aBitmap)
7788 * .setStyle(new Notification.InboxStyle()
7789 * .addLine(str1)
7790 * .addLine(str2)
7791 * .setContentTitle(&quot;&quot;)
7792 * .setSummaryText(&quot;+3 more&quot;))
7793 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007794 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007795 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007796 * @see Notification#bigContentView
7797 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007798 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007799
7800 /**
7801 * The number of lines of remote input history allowed until we start reducing lines.
7802 */
7803 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007804 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7805
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007806 public InboxStyle() {
7807 }
7808
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007809 /**
7810 * @deprecated use {@code InboxStyle()}.
7811 */
7812 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007813 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007814 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007815 }
7816
Chris Wrend6297db2012-05-03 16:20:13 -04007817 /**
7818 * Overrides ContentTitle in the big form of the template.
7819 * This defaults to the value passed to setContentTitle().
7820 */
7821 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007822 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007823 return this;
7824 }
7825
7826 /**
7827 * Set the first line of text after the detail section in the big form of the template.
7828 */
7829 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007830 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007831 return this;
7832 }
7833
Chris Wren0bd664d2012-08-01 13:56:56 -04007834 /**
7835 * Append a line to the digest section of the Inbox notification.
7836 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007837 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007838 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007839 return this;
7840 }
7841
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007842 /**
7843 * @hide
7844 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007845 public ArrayList<CharSequence> getLines() {
7846 return mTexts;
7847 }
7848
7849 /**
7850 * @hide
7851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007852 public void addExtras(Bundle extras) {
7853 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007854
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007855 CharSequence[] a = new CharSequence[mTexts.size()];
7856 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7857 }
7858
Christoph Studer4600f9b2014-07-22 22:44:43 +02007859 /**
7860 * @hide
7861 */
7862 @Override
7863 protected void restoreFromExtras(Bundle extras) {
7864 super.restoreFromExtras(extras);
7865
7866 mTexts.clear();
7867 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7868 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7869 }
7870 }
7871
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007872 /**
7873 * @hide
7874 */
7875 public RemoteViews makeBigContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007876 StandardTemplateParams p = mBuilder.mParams.reset().fillTextsFrom(mBuilder).text(null);
Selim Cinek384804b2018-04-18 14:31:07 +08007877 TemplateBindResult result = new TemplateBindResult();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007878 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), p, result);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007879
Chris Wrend6297db2012-05-03 16:20:13 -04007880 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 -04007881 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007882
Chris Wren4ed80d52012-05-17 09:30:03 -04007883 // Make sure all rows are gone in case we reuse a view.
7884 for (int rowId : rowIds) {
7885 contentView.setViewVisibility(rowId, View.GONE);
7886 }
7887
Daniel Sandler879c5e02012-04-17 16:46:51 -04007888 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007889 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7890 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007891 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007892 int onlyViewId = 0;
7893 int maxRows = rowIds.length;
7894 if (mBuilder.mActions.size() > 0) {
7895 maxRows--;
7896 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007897 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7898 EXTRA_REMOTE_INPUT_HISTORY);
7899 if (remoteInputHistory != null
7900 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7901 // Let's remove some messages to make room for the remote input history.
7902 // 1 is always able to fit, but let's remove them if they are 2 or 3
7903 int numRemoteInputs = Math.min(remoteInputHistory.length,
7904 MAX_REMOTE_INPUT_HISTORY_LINES);
7905 int totalNumRows = mTexts.size() + numRemoteInputs
7906 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7907 if (totalNumRows > maxRows) {
7908 int overflow = totalNumRows - maxRows;
7909 if (mTexts.size() > maxRows) {
7910 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7911 // few messages, even with the remote input
7912 maxRows -= overflow;
7913 } else {
7914 // otherwise we drop the first messages
7915 i = overflow;
7916 }
7917 }
7918 }
Selim Cinek07c80172016-04-21 16:40:47 -07007919 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007920 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007921 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007922 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007923 contentView.setTextViewText(rowIds[i],
7924 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek10bbc0e2018-11-21 15:14:10 -08007925 mBuilder.setTextViewColorSecondary(contentView, rowIds[i], p);
Selim Cinek07c80172016-04-21 16:40:47 -07007926 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007927 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007928 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007929 if (first) {
7930 onlyViewId = rowIds[i];
7931 } else {
7932 onlyViewId = 0;
7933 }
Selim Cinek247fa012016-02-18 09:50:48 -08007934 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007935 }
7936 i++;
7937 }
Selim Cinek07c80172016-04-21 16:40:47 -07007938 if (onlyViewId != 0) {
7939 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7940 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7941 R.dimen.notification_text_margin_top);
7942 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7943 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007944
Daniel Sandler879c5e02012-04-17 16:46:51 -04007945 return contentView;
7946 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007947
Julia Reynolds7217dc92018-03-07 12:12:09 -05007948 /**
7949 * @hide
7950 */
7951 @Override
7952 public boolean areNotificationsVisiblyDifferent(Style other) {
7953 if (other == null || getClass() != other.getClass()) {
7954 return true;
7955 }
7956 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007957
7958 final ArrayList<CharSequence> myLines = getLines();
7959 final ArrayList<CharSequence> newLines = newS.getLines();
7960 final int n = myLines.size();
7961 if (n != newLines.size()) {
7962 return true;
7963 }
7964
7965 for (int i = 0; i < n; i++) {
7966 if (!Objects.equals(
7967 String.valueOf(myLines.get(i)),
7968 String.valueOf(newLines.get(i)))) {
7969 return true;
7970 }
7971 }
7972 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007973 }
7974
Selim Cinek384804b2018-04-18 14:31:07 +08007975 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007976 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007977 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007978 if (first) {
7979 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7980 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7981 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007982 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007983 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007984 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007985 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007986 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007987 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007988 }
Dan Sandler842dd772014-05-15 09:36:47 -04007989
7990 /**
7991 * Notification style for media playback notifications.
7992 *
7993 * In the expanded form, {@link Notification#bigContentView}, up to 5
7994 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007995 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007996 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7997 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7998 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007999 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04008000 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
8001 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01008002 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04008003 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08008004 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008005 * Notifications created with MediaStyle will have their category set to
8006 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
8007 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08008008 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07008009 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
8010 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04008011 * the System UI can identify this as a notification representing an active media session
8012 * and respond accordingly (by showing album artwork in the lockscreen, for example).
8013 *
Selim Cinek99104832017-01-25 14:47:33 -08008014 * <p>
8015 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
8016 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
8017 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
8018 * <p>
8019 *
Dan Sandler842dd772014-05-15 09:36:47 -04008020 * To use this style with your Notification, feed it to
8021 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8022 * <pre class="prettyprint">
8023 * Notification noti = new Notification.Builder()
8024 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01008025 * .setContentTitle(&quot;Track title&quot;)
8026 * .setContentText(&quot;Artist - Album&quot;)
8027 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008028 * .setStyle(<b>new Notification.MediaStyle()</b>
8029 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04008030 * .build();
8031 * </pre>
8032 *
8033 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08008034 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04008035 */
8036 public static class MediaStyle extends Style {
Gus Prevas9cc96602018-10-11 11:24:23 -04008037 // Changing max media buttons requires also changing templates
8038 // (notification_template_material_media and notification_template_material_big_media).
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008039 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04008040 static final int MAX_MEDIA_BUTTONS = 5;
Gus Prevas9cc96602018-10-11 11:24:23 -04008041 @IdRes private static final int[] MEDIA_BUTTON_IDS = {
8042 R.id.action0,
8043 R.id.action1,
8044 R.id.action2,
8045 R.id.action3,
8046 R.id.action4,
8047 };
Dan Sandler842dd772014-05-15 09:36:47 -04008048
8049 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07008050 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04008051
8052 public MediaStyle() {
8053 }
8054
Adrian Roosf5faf9d2016-05-23 13:56:15 -07008055 /**
8056 * @deprecated use {@code MediaStyle()}.
8057 */
8058 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04008059 public MediaStyle(Builder builder) {
8060 setBuilder(builder);
8061 }
8062
8063 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008064 * 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 -04008065 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008066 *
8067 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04008068 */
8069 public MediaStyle setShowActionsInCompactView(int...actions) {
8070 mActionsToShowInCompact = actions;
8071 return this;
8072 }
8073
8074 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07008075 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
8076 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04008077 */
Jeff Browndba34ba2014-06-24 20:46:03 -07008078 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04008079 mToken = token;
8080 return this;
8081 }
8082
Christoph Studer4600f9b2014-07-22 22:44:43 +02008083 /**
8084 * @hide
8085 */
Dan Sandler842dd772014-05-15 09:36:47 -04008086 @Override
Mathew Inwood61e8ae62018-08-14 14:17:44 +01008087 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04008088 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02008089 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01008090 if (wip.category == null) {
8091 wip.category = Notification.CATEGORY_TRANSPORT;
8092 }
Dan Sandler842dd772014-05-15 09:36:47 -04008093 return wip;
8094 }
8095
Christoph Studer4600f9b2014-07-22 22:44:43 +02008096 /**
8097 * @hide
8098 */
8099 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008100 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008101 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008102 }
8103
8104 /**
8105 * @hide
8106 */
8107 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008108 public RemoteViews makeBigContentView() {
8109 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02008110 }
8111
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008112 /**
8113 * @hide
8114 */
8115 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008116 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek16f334e2019-07-18 16:00:55 -07008117 return makeMediaContentView();
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008118 }
8119
Dan Sandler842dd772014-05-15 09:36:47 -04008120 /** @hide */
8121 @Override
8122 public void addExtras(Bundle extras) {
8123 super.addExtras(extras);
8124
8125 if (mToken != null) {
8126 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
8127 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01008128 if (mActionsToShowInCompact != null) {
8129 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
8130 }
Dan Sandler842dd772014-05-15 09:36:47 -04008131 }
8132
Christoph Studer4600f9b2014-07-22 22:44:43 +02008133 /**
8134 * @hide
8135 */
8136 @Override
8137 protected void restoreFromExtras(Bundle extras) {
8138 super.restoreFromExtras(extras);
8139
8140 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
8141 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
8142 }
8143 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
8144 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
8145 }
8146 }
8147
Julia Reynolds7217dc92018-03-07 12:12:09 -05008148 /**
8149 * @hide
8150 */
8151 @Override
8152 public boolean areNotificationsVisiblyDifferent(Style other) {
8153 if (other == null || getClass() != other.getClass()) {
8154 return true;
8155 }
8156 // All fields to compare are on the Notification object
8157 return false;
8158 }
8159
Gus Prevas9cc96602018-10-11 11:24:23 -04008160 private void bindMediaActionButton(RemoteViews container, @IdRes int buttonId,
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008161 Action action, StandardTemplateParams p) {
Dan Sandler842dd772014-05-15 09:36:47 -04008162 final boolean tombstone = (action.actionIntent == null);
Gus Prevas9cc96602018-10-11 11:24:23 -04008163 container.setViewVisibility(buttonId, View.VISIBLE);
Beth Thibodeau750ec582019-09-18 15:14:09 -04008164 if (buttonId != R.id.media_seamless) {
8165 container.setImageViewIcon(buttonId, action.getIcon());
8166 }
Anthony Chenad4d1582017-04-10 16:07:58 -07008167
8168 // If the action buttons should not be tinted, then just use the default
8169 // notification color. Otherwise, just use the passed-in color.
Gus Prevas9cc96602018-10-11 11:24:23 -04008170 Resources resources = mBuilder.mContext.getResources();
8171 Configuration currentConfig = resources.getConfiguration();
Lucas Dupinf03e7522018-06-25 16:21:13 -07008172 boolean inNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
8173 == Configuration.UI_MODE_NIGHT_YES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008174 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized(p)
8175 ? getActionColor(p)
Lucas Dupina291d192018-06-07 13:59:42 -07008176 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Lucas Dupinf03e7522018-06-25 16:21:13 -07008177 Notification.COLOR_DEFAULT, inNightMode);
Anthony Chenad4d1582017-04-10 16:07:58 -07008178
Gus Prevas9cc96602018-10-11 11:24:23 -04008179 container.setDrawableTint(buttonId, false, tintColor,
Sunny Goyal5b153922017-09-21 21:00:36 -07008180 PorterDuff.Mode.SRC_ATOP);
Gus Prevas9cc96602018-10-11 11:24:23 -04008181
8182 final TypedArray typedArray = mBuilder.mContext.obtainStyledAttributes(
8183 new int[]{ android.R.attr.colorControlHighlight });
8184 int rippleAlpha = Color.alpha(typedArray.getColor(0, 0));
8185 typedArray.recycle();
8186 int rippleColor = Color.argb(rippleAlpha, Color.red(tintColor), Color.green(tintColor),
8187 Color.blue(tintColor));
8188 container.setRippleDrawableColor(buttonId, ColorStateList.valueOf(rippleColor));
8189
Dan Sandler842dd772014-05-15 09:36:47 -04008190 if (!tombstone) {
Gus Prevas9cc96602018-10-11 11:24:23 -04008191 container.setOnClickPendingIntent(buttonId, action.actionIntent);
Dan Sandler842dd772014-05-15 09:36:47 -04008192 }
Gus Prevas9cc96602018-10-11 11:24:23 -04008193 container.setContentDescription(buttonId, action.title);
Dan Sandler842dd772014-05-15 09:36:47 -04008194 }
8195
8196 private RemoteViews makeMediaContentView() {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008197 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8198 mBuilder);
Dan Sandler842dd772014-05-15 09:36:47 -04008199 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008200 R.layout.notification_template_material_media, p,
Selim Cinek384804b2018-04-18 14:31:07 +08008201 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008202
8203 final int numActions = mBuilder.mActions.size();
Gus Prevas9cc96602018-10-11 11:24:23 -04008204 final int numActionsToShow = mActionsToShowInCompact == null
Dan Sandler842dd772014-05-15 09:36:47 -04008205 ? 0
8206 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Gus Prevas9cc96602018-10-11 11:24:23 -04008207 if (numActionsToShow > numActions) {
8208 throw new IllegalArgumentException(String.format(
8209 "setShowActionsInCompactView: action %d out of bounds (max %d)",
8210 numActions, numActions - 1));
8211 }
8212 for (int i = 0; i < MAX_MEDIA_BUTTONS_IN_COMPACT; i++) {
8213 if (i < numActionsToShow) {
Dan Sandler842dd772014-05-15 09:36:47 -04008214 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008215 bindMediaActionButton(view, MEDIA_BUTTON_IDS[i], action, p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008216 } else {
8217 view.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008218 }
8219 }
Beth Thibodeau750ec582019-09-18 15:14:09 -04008220 bindMediaActionButton(view, R.id.media_seamless, new Action(
8221 R.drawable.ic_media_seamless, mBuilder.mContext.getString(
8222 com.android.internal.R.string.ext_media_seamless_action), null), p);
8223 view.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinekfdc738f2016-01-27 20:04:27 -08008224 handleImage(view);
8225 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008226 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07008227 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08008228 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08008229 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008230 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04008231 return view;
8232 }
8233
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008234 private int getActionColor(StandardTemplateParams p) {
8235 return mBuilder.isColorized(p) ? mBuilder.getPrimaryTextColor(p)
8236 : mBuilder.resolveContrastColor(p);
Selim Cinek99104832017-01-25 14:47:33 -08008237 }
8238
Dan Sandler842dd772014-05-15 09:36:47 -04008239 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008240 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008241 // Dont add an expanded view if there is no more content to be revealed
8242 int actionsInCompact = mActionsToShowInCompact == null
8243 ? 0
8244 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07008245 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08008246 return null;
8247 }
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008248 StandardTemplateParams p = mBuilder.mParams.reset().hasProgress(false).fillTextsFrom(
8249 mBuilder);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008250 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008251 R.layout.notification_template_material_big_media, p , null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04008252
Gus Prevas9cc96602018-10-11 11:24:23 -04008253 for (int i = 0; i < MAX_MEDIA_BUTTONS; i++) {
8254 if (i < actionCount) {
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008255 bindMediaActionButton(big, MEDIA_BUTTON_IDS[i], mBuilder.mActions.get(i), p);
Gus Prevas9cc96602018-10-11 11:24:23 -04008256 } else {
8257 big.setViewVisibility(MEDIA_BUTTON_IDS[i], View.GONE);
Dan Sandler842dd772014-05-15 09:36:47 -04008258 }
8259 }
linanson2bcd4032019-01-14 15:22:04 +08008260 bindMediaActionButton(big, R.id.media_seamless, new Action(R.drawable.ic_media_seamless,
8261 mBuilder.mContext.getString(
8262 com.android.internal.R.string.ext_media_seamless_action), null), p);
8263 big.setViewVisibility(R.id.media_seamless, View.GONE);
Selim Cinek5bf069a2015-11-10 19:14:27 -05008264 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04008265 return big;
8266 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008267
Selim Cinek5bf069a2015-11-10 19:14:27 -05008268 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07008269 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07008270 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
8271 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008272 }
8273 }
8274
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02008275 /**
8276 * @hide
8277 */
8278 @Override
8279 protected boolean hasProgress() {
8280 return false;
8281 }
Dan Sandler842dd772014-05-15 09:36:47 -04008282 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008283
Selim Cinek593610c2016-02-16 18:42:57 -08008284 /**
8285 * Notification style for custom views that are decorated by the system
8286 *
8287 * <p>Instead of providing a notification that is completely custom, a developer can set this
8288 * style and still obtain system decorations like the notification header with the expand
8289 * affordance and actions.
8290 *
8291 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8292 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8293 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8294 * corresponding custom views to display.
8295 *
8296 * To use this style with your Notification, feed it to
8297 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8298 * <pre class="prettyprint">
8299 * Notification noti = new Notification.Builder()
8300 * .setSmallIcon(R.drawable.ic_stat_player)
8301 * .setLargeIcon(albumArtBitmap))
8302 * .setCustomContentView(contentView);
8303 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
8304 * .build();
8305 * </pre>
8306 */
8307 public static class DecoratedCustomViewStyle extends Style {
8308
8309 public DecoratedCustomViewStyle() {
8310 }
8311
Selim Cinek593610c2016-02-16 18:42:57 -08008312 /**
8313 * @hide
8314 */
8315 public boolean displayCustomViewInline() {
8316 return true;
8317 }
8318
8319 /**
8320 * @hide
8321 */
8322 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008323 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008324 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
8325 }
8326
8327 /**
8328 * @hide
8329 */
8330 @Override
8331 public RemoteViews makeBigContentView() {
8332 return makeDecoratedBigContentView();
8333 }
8334
8335 /**
8336 * @hide
8337 */
8338 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008339 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08008340 return makeDecoratedHeadsUpContentView();
8341 }
8342
Selim Cinek593610c2016-02-16 18:42:57 -08008343 private RemoteViews makeDecoratedHeadsUpContentView() {
8344 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8345 ? mBuilder.mN.contentView
8346 : mBuilder.mN.headsUpContentView;
8347 if (mBuilder.mActions.size() == 0) {
8348 return makeStandardTemplateWithCustomContent(headsUpContentView);
8349 }
Selim Cinek384804b2018-04-18 14:31:07 +08008350 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008351 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008352 mBuilder.getBigBaseLayoutResource(), result);
8353 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008354 return remoteViews;
8355 }
8356
Selim Cinek593610c2016-02-16 18:42:57 -08008357 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008358 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008359 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008360 mBuilder.getBaseLayoutResource(), result);
8361 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008362 return remoteViews;
8363 }
8364
Selim Cinek593610c2016-02-16 18:42:57 -08008365 private RemoteViews makeDecoratedBigContentView() {
8366 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8367 ? mBuilder.mN.contentView
8368 : mBuilder.mN.bigContentView;
8369 if (mBuilder.mActions.size() == 0) {
8370 return makeStandardTemplateWithCustomContent(bigContentView);
8371 }
Selim Cinek384804b2018-04-18 14:31:07 +08008372 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008373 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008374 mBuilder.getBigBaseLayoutResource(), result);
8375 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008376 return remoteViews;
8377 }
Selim Cinek247fa012016-02-18 09:50:48 -08008378
8379 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008380 RemoteViews customContent, TemplateBindResult result) {
Selim Cinek6fe4a102019-01-25 14:38:24 -08008381 int childIndex = -1;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008382 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008383 // Need to clone customContent before adding, because otherwise it can no longer be
8384 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008385 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008386 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8387 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008388 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Selim Cinek6fe4a102019-01-25 14:38:24 -08008389 childIndex = 0;
Adrian Roos5081c0d2016-02-26 16:04:19 -08008390 }
Selim Cinek6fe4a102019-01-25 14:38:24 -08008391 remoteViews.setIntTag(R.id.notification_main_column,
8392 com.android.internal.R.id.notification_custom_view_index_tag,
8393 childIndex);
Selim Cinek247fa012016-02-18 09:50:48 -08008394 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008395 Resources resources = mBuilder.mContext.getResources();
8396 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008397 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008398 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008399 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008400
8401 /**
8402 * @hide
8403 */
8404 @Override
8405 public boolean areNotificationsVisiblyDifferent(Style other) {
8406 if (other == null || getClass() != other.getClass()) {
8407 return true;
8408 }
8409 // Comparison done for all custom RemoteViews, independent of style
8410 return false;
8411 }
Selim Cinek593610c2016-02-16 18:42:57 -08008412 }
8413
Selim Cinek03eb3b72016-02-18 10:39:45 -08008414 /**
8415 * Notification style for media custom views that are decorated by the system
8416 *
8417 * <p>Instead of providing a media notification that is completely custom, a developer can set
8418 * this style and still obtain system decorations like the notification header with the expand
8419 * affordance and actions.
8420 *
8421 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8422 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8423 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8424 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008425 * <p>
8426 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8427 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8428 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008429 * To use this style with your Notification, feed it to
8430 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8431 * <pre class="prettyprint">
8432 * Notification noti = new Notification.Builder()
8433 * .setSmallIcon(R.drawable.ic_stat_player)
8434 * .setLargeIcon(albumArtBitmap))
8435 * .setCustomContentView(contentView);
8436 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8437 * .setMediaSession(mySession))
8438 * .build();
8439 * </pre>
8440 *
8441 * @see android.app.Notification.DecoratedCustomViewStyle
8442 * @see android.app.Notification.MediaStyle
8443 */
8444 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8445
8446 public DecoratedMediaCustomViewStyle() {
8447 }
8448
Selim Cinek03eb3b72016-02-18 10:39:45 -08008449 /**
8450 * @hide
8451 */
8452 public boolean displayCustomViewInline() {
8453 return true;
8454 }
8455
8456 /**
8457 * @hide
8458 */
8459 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008460 public RemoteViews makeContentView(boolean increasedHeight) {
8461 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008462 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8463 mBuilder.mN.contentView);
8464 }
8465
8466 /**
8467 * @hide
8468 */
8469 @Override
8470 public RemoteViews makeBigContentView() {
8471 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8472 ? mBuilder.mN.bigContentView
8473 : mBuilder.mN.contentView;
8474 return makeBigContentViewWithCustomContent(customRemoteView);
8475 }
8476
8477 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8478 RemoteViews remoteViews = super.makeBigContentView();
8479 if (remoteViews != null) {
8480 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8481 customRemoteView);
8482 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008483 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008484 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8485 customRemoteView);
8486 } else {
8487 return null;
8488 }
8489 }
8490
8491 /**
8492 * @hide
8493 */
8494 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008495 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008496 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8497 ? mBuilder.mN.headsUpContentView
8498 : mBuilder.mN.contentView;
8499 return makeBigContentViewWithCustomContent(customRemoteView);
8500 }
8501
Julia Reynolds7217dc92018-03-07 12:12:09 -05008502 /**
8503 * @hide
8504 */
8505 @Override
8506 public boolean areNotificationsVisiblyDifferent(Style other) {
8507 if (other == null || getClass() != other.getClass()) {
8508 return true;
8509 }
8510 // Comparison done for all custom RemoteViews, independent of style
8511 return false;
8512 }
8513
Selim Cinek03eb3b72016-02-18 10:39:45 -08008514 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8515 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008516 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008517 // Need to clone customContent before adding, because otherwise it can no longer be
8518 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008519 customContent = customContent.clone();
Selim Cinek10bbc0e2018-11-21 15:14:10 -08008520 customContent.overrideTextColors(mBuilder.getPrimaryTextColor(mBuilder.mParams));
Selim Cinekf91017e2016-03-14 12:25:09 -07008521 remoteViews.removeAllViews(id);
8522 remoteViews.addView(id, customContent);
Sunny Goyalc12d31c2018-11-12 16:29:18 -08008523 remoteViews.addFlags(RemoteViews.FLAG_REAPPLY_DISALLOWED);
Adrian Roos5081c0d2016-02-26 16:04:19 -08008524 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008525 return remoteViews;
8526 }
8527 }
8528
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008529 /**
8530 * Encapsulates the information needed to display a notification as a bubble.
8531 *
8532 * <p>A bubble is used to display app content in a floating window over the existing
8533 * foreground activity. A bubble has a collapsed state represented by an icon,
8534 * {@link BubbleMetadata.Builder#setIcon(Icon)} and an expanded state which is populated
8535 * via {@link BubbleMetadata.Builder#setIntent(PendingIntent)}.</p>
8536 *
8537 * <b>Notifications with a valid and allowed bubble will display in collapsed state
8538 * outside of the notification shade on unlocked devices. When a user interacts with the
8539 * collapsed bubble, the bubble intent will be invoked and displayed.</b>
8540 *
8541 * @see Notification.Builder#setBubbleMetadata(BubbleMetadata)
8542 */
8543 public static final class BubbleMetadata implements Parcelable {
8544
8545 private PendingIntent mPendingIntent;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008546 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008547 private Icon mIcon;
8548 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008549 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008550 private int mFlags;
8551
8552 /**
8553 * If set and the app creating the bubble is in the foreground, the bubble will be posted
8554 * in its expanded state, with the contents of {@link #getIntent()} in a floating window.
8555 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008556 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8557 * The app is considered foreground if it is visible and on the screen, note that
8558 * a foreground service does not qualify.
8559 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008560 *
8561 * <p>Generally this flag should only be set if the user has performed an action to request
8562 * or create a bubble.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008563 *
8564 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008565 */
Mady Mellora7731962019-06-17 17:57:02 -07008566 public static final int FLAG_AUTO_EXPAND_BUBBLE = 0x00000001;
Mady Mellor0157ff22019-01-24 15:31:48 -08008567
8568 /**
Mady Mellor8a1f0252019-04-01 11:31:34 -07008569 * If set and the app posting the bubble is in the foreground, the bubble will
8570 * be posted <b>without</b> the associated notification in the notification shade.
Mady Mellor0157ff22019-01-24 15:31:48 -08008571 *
Mady Mellor1f4703a2019-07-12 13:23:51 -07008572 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
8573 * The app is considered foreground if it is visible and on the screen, note that
8574 * a foreground service does not qualify.
8575 * </p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008576 *
8577 * <p>Generally this flag should only be set if the user has performed an action to request
Mady Mellor8a1f0252019-04-01 11:31:34 -07008578 * or create a bubble, or if the user has seen the content in the notification and the
8579 * notification is no longer relevant.</p>
Mady Mellora7731962019-06-17 17:57:02 -07008580 *
8581 * @hide
Mady Mellor0157ff22019-01-24 15:31:48 -08008582 */
Mady Mellora7731962019-06-17 17:57:02 -07008583 public static final int FLAG_SUPPRESS_NOTIFICATION = 0x00000002;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008584
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008585 private BubbleMetadata(PendingIntent expandIntent, PendingIntent deleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008586 Icon icon, int height, @DimenRes int heightResId) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008587 mPendingIntent = expandIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008588 mIcon = icon;
8589 mDesiredHeight = height;
Mady Mellor7af771a2019-03-07 15:04:54 -08008590 mDesiredHeightResId = heightResId;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008591 mDeleteIntent = deleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008592 }
8593
8594 private BubbleMetadata(Parcel in) {
8595 mPendingIntent = PendingIntent.CREATOR.createFromParcel(in);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008596 mIcon = Icon.CREATOR.createFromParcel(in);
8597 mDesiredHeight = in.readInt();
Mady Mellor0157ff22019-01-24 15:31:48 -08008598 mFlags = in.readInt();
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008599 if (in.readInt() != 0) {
8600 mDeleteIntent = PendingIntent.CREATOR.createFromParcel(in);
8601 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008602 mDesiredHeightResId = in.readInt();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008603 }
8604
8605 /**
8606 * @return the pending intent used to populate the floating window for this bubble.
8607 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008608 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008609 public PendingIntent getIntent() {
8610 return mPendingIntent;
8611 }
8612
8613 /**
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008614 * @return the pending intent to send when the bubble is dismissed by a user, if one exists.
8615 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008616 @Nullable
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008617 public PendingIntent getDeleteIntent() {
8618 return mDeleteIntent;
8619 }
8620
8621 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008622 * @return the icon that will be displayed for this bubble when it is collapsed.
8623 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008624 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008625 public Icon getIcon() {
8626 return mIcon;
8627 }
8628
8629 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008630 * @return the ideal height, in DPs, for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008631 * {@link #getIntent()} for this bubble. A value of 0 indicates a desired height has not
8632 * been set.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008633 */
Mady Mellor8a529e22019-03-25 17:37:55 -07008634 @Dimension(unit = DP)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008635 public int getDesiredHeight() {
8636 return mDesiredHeight;
8637 }
8638
Mady Mellor0157ff22019-01-24 15:31:48 -08008639 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008640 * @return the resId of ideal height for the floating window that app content defined by
Mady Mellor8a529e22019-03-25 17:37:55 -07008641 * {@link #getIntent()} for this bubble. A value of 0 indicates a res value has not
8642 * been provided for the desired height.
Mady Mellor7af771a2019-03-07 15:04:54 -08008643 */
8644 @DimenRes
8645 public int getDesiredHeightResId() {
8646 return mDesiredHeightResId;
8647 }
8648
8649 /**
Mady Mellor0157ff22019-01-24 15:31:48 -08008650 * @return whether this bubble should auto expand when it is posted.
8651 *
8652 * @see BubbleMetadata.Builder#setAutoExpandBubble(boolean)
8653 */
8654 public boolean getAutoExpandBubble() {
8655 return (mFlags & FLAG_AUTO_EXPAND_BUBBLE) != 0;
8656 }
8657
8658 /**
Mady Mellorc529d6d2019-04-16 14:22:52 -07008659 * @return whether this bubble should suppress the notification when it is posted.
8660 *
8661 * @see BubbleMetadata.Builder#setSuppressNotification(boolean)
8662 */
8663 public boolean isNotificationSuppressed() {
Mady Mellor8a1f0252019-04-01 11:31:34 -07008664 return (mFlags & FLAG_SUPPRESS_NOTIFICATION) != 0;
Mady Mellor0157ff22019-01-24 15:31:48 -08008665 }
8666
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07008667 public static final @android.annotation.NonNull Parcelable.Creator<BubbleMetadata> CREATOR =
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008668 new Parcelable.Creator<BubbleMetadata>() {
8669
8670 @Override
8671 public BubbleMetadata createFromParcel(Parcel source) {
8672 return new BubbleMetadata(source);
8673 }
8674
8675 @Override
8676 public BubbleMetadata[] newArray(int size) {
8677 return new BubbleMetadata[size];
8678 }
8679 };
8680
8681 @Override
8682 public int describeContents() {
8683 return 0;
8684 }
8685
8686 @Override
8687 public void writeToParcel(Parcel out, int flags) {
8688 mPendingIntent.writeToParcel(out, 0);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008689 mIcon.writeToParcel(out, 0);
8690 out.writeInt(mDesiredHeight);
Mady Mellor0157ff22019-01-24 15:31:48 -08008691 out.writeInt(mFlags);
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008692 out.writeInt(mDeleteIntent != null ? 1 : 0);
8693 if (mDeleteIntent != null) {
8694 mDeleteIntent.writeToParcel(out, 0);
8695 }
Mady Mellor7af771a2019-03-07 15:04:54 -08008696 out.writeInt(mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008697 }
8698
Mady Mellora7731962019-06-17 17:57:02 -07008699 /**
8700 * @hide
8701 */
8702 public void setFlags(int flags) {
Mady Mellor0157ff22019-01-24 15:31:48 -08008703 mFlags = flags;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008704 }
8705
8706 /**
Mady Mellora7731962019-06-17 17:57:02 -07008707 * @hide
8708 */
8709 public int getFlags() {
8710 return mFlags;
8711 }
8712
8713 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008714 * Builder to construct a {@link BubbleMetadata} object.
8715 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008716 public static final class Builder {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008717
8718 private PendingIntent mPendingIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008719 private Icon mIcon;
8720 private int mDesiredHeight;
Mady Mellor7af771a2019-03-07 15:04:54 -08008721 @DimenRes private int mDesiredHeightResId;
Mady Mellor0157ff22019-01-24 15:31:48 -08008722 private int mFlags;
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008723 private PendingIntent mDeleteIntent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008724
8725 /**
8726 * Constructs a new builder object.
8727 */
8728 public Builder() {
8729 }
8730
8731 /**
8732 * Sets the intent that will be used when the bubble is expanded. This will display the
8733 * app content in a floating window over the existing foreground activity.
Julia Tuttled9f48352019-04-15 14:06:54 -04008734 *
8735 * <p>An intent is required.</p>
8736 *
8737 * @throws IllegalArgumentException if intent is null
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008738 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008739 @NonNull
8740 public BubbleMetadata.Builder setIntent(@NonNull PendingIntent intent) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008741 if (intent == null) {
8742 throw new IllegalArgumentException("Bubble requires non-null pending intent");
8743 }
8744 mPendingIntent = intent;
8745 return this;
8746 }
8747
8748 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008749 * Sets the icon that will represent the bubble when it is collapsed.
8750 *
8751 * <p>An icon is required and should be representative of the content within the bubble.
8752 * If your app produces multiple bubbles, the image should be unique for each of them.
8753 * </p>
Mady Mellor9848a6c2019-03-19 15:29:05 -07008754 *
Mady Mellor05e860b2019-10-30 22:48:15 -07008755 * <p>The shape of a bubble icon is adaptive and will match the device theme.
Mady Mellor9848a6c2019-03-19 15:29:05 -07008756 *
Mady Mellor05e860b2019-10-30 22:48:15 -07008757 * Ideally your icon should be constructed via
8758 * {@link Icon#createWithAdaptiveBitmap(Bitmap)}, otherwise, the icon will be shrunk
8759 * and placed on an adaptive shape.
Mady Mellor9848a6c2019-03-19 15:29:05 -07008760 * </p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008761 *
Mady Mellor05e860b2019-10-30 22:48:15 -07008762 * @throws IllegalArgumentException if icon is null.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008763 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008764 @NonNull
8765 public BubbleMetadata.Builder setIcon(@NonNull Icon icon) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008766 if (icon == null) {
8767 throw new IllegalArgumentException("Bubbles require non-null icon");
8768 }
8769 mIcon = icon;
8770 return this;
8771 }
8772
8773 /**
Mady Mellor7af771a2019-03-07 15:04:54 -08008774 * Sets the desired height in DPs for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008775 * {@link #setIntent(PendingIntent)}.
8776 *
8777 * <p>This height may not be respected if there is not enough space on the screen or if
8778 * the provided height is too small to be useful.</p>
8779 *
8780 * <p>If {@link #setDesiredHeightResId(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008781 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008782 * be used instead.</p>
8783 *
8784 * <p>A desired height (in DPs or via resID) is optional.</p>
8785 *
8786 * @see #setDesiredHeightResId(int)
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008787 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008788 @NonNull
Mady Mellor8a529e22019-03-25 17:37:55 -07008789 public BubbleMetadata.Builder setDesiredHeight(@Dimension(unit = DP) int height) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008790 mDesiredHeight = Math.max(height, 0);
Mady Mellor7af771a2019-03-07 15:04:54 -08008791 mDesiredHeightResId = 0;
8792 return this;
8793 }
8794
8795
8796 /**
8797 * Sets the desired height via resId for the app content defined by
Julia Tuttled9f48352019-04-15 14:06:54 -04008798 * {@link #setIntent(PendingIntent)}.
8799 *
8800 * <p>This height may not be respected if there is not enough space on the screen or if
8801 * the provided height is too small to be useful.</p>
8802 *
8803 * <p>If {@link #setDesiredHeight(int)} was previously called on this builder, the
Mady Mellor7af771a2019-03-07 15:04:54 -08008804 * previous value set will be cleared after calling this method, and this value will
Julia Tuttled9f48352019-04-15 14:06:54 -04008805 * be used instead.</p>
8806 *
8807 * <p>A desired height (in DPs or via resID) is optional.</p>
8808 *
8809 * @see #setDesiredHeight(int)
Mady Mellor7af771a2019-03-07 15:04:54 -08008810 */
8811 @NonNull
8812 public BubbleMetadata.Builder setDesiredHeightResId(@DimenRes int heightResId) {
8813 mDesiredHeightResId = heightResId;
8814 mDesiredHeight = 0;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008815 return this;
8816 }
8817
8818 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008819 * Sets whether the bubble will be posted in its expanded state (with the contents of
8820 * {@link #getIntent()} in a floating window).
Mady Mellor0157ff22019-01-24 15:31:48 -08008821 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008822 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008823 * The app is considered foreground if it is visible and on the screen, note that
8824 * a foreground service does not qualify.
Mady Mellor0157ff22019-01-24 15:31:48 -08008825 * </p>
8826 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008827 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor0157ff22019-01-24 15:31:48 -08008828 * request or create a bubble.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008829 *
8830 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor0157ff22019-01-24 15:31:48 -08008831 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008832 @NonNull
Mady Mellor0157ff22019-01-24 15:31:48 -08008833 public BubbleMetadata.Builder setAutoExpandBubble(boolean shouldExpand) {
8834 setFlag(FLAG_AUTO_EXPAND_BUBBLE, shouldExpand);
8835 return this;
8836 }
8837
8838 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008839 * Sets whether the bubble will be posted <b>without</b> the associated notification in
8840 * the notification shade.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008841 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008842 * <p>This flag has no effect if the app posting the bubble is not in the foreground.
Mady Mellor1f4703a2019-07-12 13:23:51 -07008843 * The app is considered foreground if it is visible and on the screen, note that
8844 * a foreground service does not qualify.
Mady Mellor8a1f0252019-04-01 11:31:34 -07008845 * </p>
8846 *
Julia Tuttled9f48352019-04-15 14:06:54 -04008847 * <p>Generally, this flag should only be set if the user has performed an action to
Mady Mellor8a1f0252019-04-01 11:31:34 -07008848 * request or create a bubble, or if the user has seen the content in the notification
8849 * and the notification is no longer relevant.</p>
Julia Tuttled9f48352019-04-15 14:06:54 -04008850 *
8851 * <p>Setting this flag is optional; it defaults to false.</p>
Mady Mellor8a1f0252019-04-01 11:31:34 -07008852 */
8853 @NonNull
Julia Tuttle4f1bf202019-05-06 13:40:05 -04008854 public BubbleMetadata.Builder setSuppressNotification(boolean shouldSuppressNotif) {
8855 setFlag(FLAG_SUPPRESS_NOTIFICATION, shouldSuppressNotif);
Mady Mellor0157ff22019-01-24 15:31:48 -08008856 return this;
8857 }
8858
8859 /**
Julia Tuttled9f48352019-04-15 14:06:54 -04008860 * Sets an intent to send when this bubble is explicitly removed by the user.
8861 *
8862 * <p>Setting a delete intent is optional.</p>
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008863 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008864 @NonNull
8865 public BubbleMetadata.Builder setDeleteIntent(@Nullable PendingIntent deleteIntent) {
Mady Mellor1ab4aa82019-02-19 16:41:55 -08008866 mDeleteIntent = deleteIntent;
8867 return this;
8868 }
8869
8870 /**
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008871 * Creates the {@link BubbleMetadata} defined by this builder.
Julia Tuttled9f48352019-04-15 14:06:54 -04008872 *
8873 * @throws IllegalStateException if {@link #setIntent(PendingIntent)} and/or
8874 * {@link #setIcon(Icon)} have not been called on this
8875 * builder.
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008876 */
Mady Mellorcf8f1b22019-03-07 14:08:21 -08008877 @NonNull
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008878 public BubbleMetadata build() {
8879 if (mPendingIntent == null) {
8880 throw new IllegalStateException("Must supply pending intent to bubble");
8881 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008882 if (mIcon == null) {
8883 throw new IllegalStateException("Must supply an icon for the bubble");
8884 }
Mady Mellor138f4c92019-02-20 16:12:17 -08008885 BubbleMetadata data = new BubbleMetadata(mPendingIntent, mDeleteIntent,
Mady Mellor7af771a2019-03-07 15:04:54 -08008886 mIcon, mDesiredHeight, mDesiredHeightResId);
Mady Mellor0157ff22019-01-24 15:31:48 -08008887 data.setFlags(mFlags);
8888 return data;
8889 }
8890
8891 /**
8892 * @hide
8893 */
8894 public BubbleMetadata.Builder setFlag(int mask, boolean value) {
8895 if (value) {
8896 mFlags |= mask;
8897 } else {
8898 mFlags &= ~mask;
8899 }
8900 return this;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08008901 }
8902 }
8903 }
8904
8905
Christoph Studer4600f9b2014-07-22 22:44:43 +02008906 // When adding a new Style subclass here, don't forget to update
8907 // Builder.getNotificationStyleClass.
8908
Griff Hazen61a9e862014-05-22 16:05:19 -07008909 /**
8910 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8911 * metadata or change options on a notification builder.
8912 */
8913 public interface Extender {
8914 /**
8915 * Apply this extender to a notification builder.
8916 * @param builder the builder to be modified.
8917 * @return the build object for chaining.
8918 */
8919 public Builder extend(Builder builder);
8920 }
8921
8922 /**
8923 * Helper class to add wearable extensions to notifications.
8924 * <p class="note"> See
8925 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8926 * for Android Wear</a> for more information on how to use this class.
8927 * <p>
8928 * To create a notification with wearable extensions:
8929 * <ol>
8930 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8931 * properties.
8932 * <li>Create a {@link android.app.Notification.WearableExtender}.
8933 * <li>Set wearable-specific properties using the
8934 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8935 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8936 * notification.
8937 * <li>Post the notification to the notification system with the
8938 * {@code NotificationManager.notify(...)} methods.
8939 * </ol>
8940 *
8941 * <pre class="prettyprint">
8942 * Notification notif = new Notification.Builder(mContext)
8943 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8944 * .setContentText(subject)
8945 * .setSmallIcon(R.drawable.new_mail)
8946 * .extend(new Notification.WearableExtender()
8947 * .setContentIcon(R.drawable.new_mail))
8948 * .build();
8949 * NotificationManager notificationManger =
8950 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8951 * notificationManger.notify(0, notif);</pre>
8952 *
8953 * <p>Wearable extensions can be accessed on an existing notification by using the
8954 * {@code WearableExtender(Notification)} constructor,
8955 * and then using the {@code get} methods to access values.
8956 *
8957 * <pre class="prettyprint">
8958 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8959 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008960 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008961 */
8962 public static final class WearableExtender implements Extender {
8963 /**
8964 * Sentinel value for an action index that is unset.
8965 */
8966 public static final int UNSET_ACTION_INDEX = -1;
8967
8968 /**
8969 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8970 * default sizing.
8971 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008972 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008973 * on their content.
Gus Prevasd7363752018-09-18 14:35:15 -04008974 *
8975 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008976 */
Gus Prevasd7363752018-09-18 14:35:15 -04008977 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008978 public static final int SIZE_DEFAULT = 0;
8979
8980 /**
8981 * Size value for use with {@link #setCustomSizePreset} to show this notification
8982 * with an extra small size.
8983 * <p>This value is only applicable for custom display notifications created using
8984 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008985 *
8986 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008987 */
Gus Prevasd7363752018-09-18 14:35:15 -04008988 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008989 public static final int SIZE_XSMALL = 1;
8990
8991 /**
8992 * Size value for use with {@link #setCustomSizePreset} to show this notification
8993 * with a small size.
8994 * <p>This value is only applicable for custom display notifications created using
8995 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04008996 *
8997 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07008998 */
Gus Prevasd7363752018-09-18 14:35:15 -04008999 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009000 public static final int SIZE_SMALL = 2;
9001
9002 /**
9003 * Size value for use with {@link #setCustomSizePreset} to show this notification
9004 * with a medium size.
9005 * <p>This value is only applicable for custom display notifications created using
9006 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009007 *
9008 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009009 */
Gus Prevasd7363752018-09-18 14:35:15 -04009010 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009011 public static final int SIZE_MEDIUM = 3;
9012
9013 /**
9014 * Size value for use with {@link #setCustomSizePreset} to show this notification
9015 * with a large size.
9016 * <p>This value is only applicable for custom display notifications created using
9017 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009018 *
9019 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009020 */
Gus Prevasd7363752018-09-18 14:35:15 -04009021 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009022 public static final int SIZE_LARGE = 4;
9023
Griff Hazend5f11f92014-05-27 15:40:09 -07009024 /**
9025 * Size value for use with {@link #setCustomSizePreset} to show this notification
9026 * full screen.
9027 * <p>This value is only applicable for custom display notifications created using
9028 * {@link #setDisplayIntent}.
Gus Prevasd7363752018-09-18 14:35:15 -04009029 *
9030 * @deprecated Display intents are no longer supported.
Griff Hazend5f11f92014-05-27 15:40:09 -07009031 */
Gus Prevasd7363752018-09-18 14:35:15 -04009032 @Deprecated
Griff Hazend5f11f92014-05-27 15:40:09 -07009033 public static final int SIZE_FULL_SCREEN = 5;
9034
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009035 /**
9036 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
9037 * short amount of time when this notification is displayed on the screen. This
9038 * is the default value.
Gus Prevasd7363752018-09-18 14:35:15 -04009039 *
9040 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009041 */
Gus Prevasd7363752018-09-18 14:35:15 -04009042 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009043 public static final int SCREEN_TIMEOUT_SHORT = 0;
9044
9045 /**
9046 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
9047 * for a longer amount of time when this notification is displayed on the screen.
Gus Prevasd7363752018-09-18 14:35:15 -04009048 *
9049 * @deprecated This feature is no longer supported.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009050 */
Gus Prevasd7363752018-09-18 14:35:15 -04009051 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009052 public static final int SCREEN_TIMEOUT_LONG = -1;
9053
Griff Hazen61a9e862014-05-22 16:05:19 -07009054 /** Notification extra which contains wearable extensions */
9055 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
9056
Pete Gastaf6781d2014-10-07 15:17:05 -04009057 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07009058 private static final String KEY_ACTIONS = "actions";
9059 private static final String KEY_FLAGS = "flags";
9060 private static final String KEY_DISPLAY_INTENT = "displayIntent";
9061 private static final String KEY_PAGES = "pages";
9062 private static final String KEY_BACKGROUND = "background";
9063 private static final String KEY_CONTENT_ICON = "contentIcon";
9064 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
9065 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
9066 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
9067 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
9068 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009069 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04009070 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009071 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07009072
9073 // Flags bitwise-ored to mFlags
9074 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
9075 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
9076 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
9077 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009078 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04009079 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04009080 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07009081
9082 // Default value for flags integer
9083 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
9084
9085 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
9086 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
9087
9088 private ArrayList<Action> mActions = new ArrayList<Action>();
9089 private int mFlags = DEFAULT_FLAGS;
9090 private PendingIntent mDisplayIntent;
9091 private ArrayList<Notification> mPages = new ArrayList<Notification>();
9092 private Bitmap mBackground;
9093 private int mContentIcon;
9094 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
9095 private int mContentActionIndex = UNSET_ACTION_INDEX;
9096 private int mCustomSizePreset = SIZE_DEFAULT;
9097 private int mCustomContentHeight;
9098 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009099 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009100 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009101 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009102
9103 /**
9104 * Create a {@link android.app.Notification.WearableExtender} with default
9105 * options.
9106 */
9107 public WearableExtender() {
9108 }
9109
9110 public WearableExtender(Notification notif) {
9111 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
9112 if (wearableBundle != null) {
9113 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
9114 if (actions != null) {
9115 mActions.addAll(actions);
9116 }
9117
9118 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
9119 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
9120
9121 Notification[] pages = getNotificationArrayFromBundle(
9122 wearableBundle, KEY_PAGES);
9123 if (pages != null) {
9124 Collections.addAll(mPages, pages);
9125 }
9126
9127 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
9128 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
9129 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
9130 DEFAULT_CONTENT_ICON_GRAVITY);
9131 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
9132 UNSET_ACTION_INDEX);
9133 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
9134 SIZE_DEFAULT);
9135 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
9136 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009137 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04009138 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009139 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07009140 }
9141 }
9142
9143 /**
9144 * Apply wearable extensions to a notification that is being built. This is typically
9145 * called by the {@link android.app.Notification.Builder#extend} method of
9146 * {@link android.app.Notification.Builder}.
9147 */
9148 @Override
9149 public Notification.Builder extend(Notification.Builder builder) {
9150 Bundle wearableBundle = new Bundle();
9151
9152 if (!mActions.isEmpty()) {
9153 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
9154 }
9155 if (mFlags != DEFAULT_FLAGS) {
9156 wearableBundle.putInt(KEY_FLAGS, mFlags);
9157 }
9158 if (mDisplayIntent != null) {
9159 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
9160 }
9161 if (!mPages.isEmpty()) {
9162 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
9163 new Notification[mPages.size()]));
9164 }
9165 if (mBackground != null) {
9166 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
9167 }
9168 if (mContentIcon != 0) {
9169 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
9170 }
9171 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
9172 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
9173 }
9174 if (mContentActionIndex != UNSET_ACTION_INDEX) {
9175 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
9176 mContentActionIndex);
9177 }
9178 if (mCustomSizePreset != SIZE_DEFAULT) {
9179 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
9180 }
9181 if (mCustomContentHeight != 0) {
9182 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
9183 }
9184 if (mGravity != DEFAULT_GRAVITY) {
9185 wearableBundle.putInt(KEY_GRAVITY, mGravity);
9186 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009187 if (mHintScreenTimeout != 0) {
9188 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
9189 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04009190 if (mDismissalId != null) {
9191 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
9192 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009193 if (mBridgeTag != null) {
9194 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
9195 }
Griff Hazen61a9e862014-05-22 16:05:19 -07009196
9197 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
9198 return builder;
9199 }
9200
9201 @Override
9202 public WearableExtender clone() {
9203 WearableExtender that = new WearableExtender();
9204 that.mActions = new ArrayList<Action>(this.mActions);
9205 that.mFlags = this.mFlags;
9206 that.mDisplayIntent = this.mDisplayIntent;
9207 that.mPages = new ArrayList<Notification>(this.mPages);
9208 that.mBackground = this.mBackground;
9209 that.mContentIcon = this.mContentIcon;
9210 that.mContentIconGravity = this.mContentIconGravity;
9211 that.mContentActionIndex = this.mContentActionIndex;
9212 that.mCustomSizePreset = this.mCustomSizePreset;
9213 that.mCustomContentHeight = this.mCustomContentHeight;
9214 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009215 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04009216 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009217 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07009218 return that;
9219 }
9220
9221 /**
9222 * Add a wearable action to this notification.
9223 *
9224 * <p>When wearable actions are added using this method, the set of actions that
9225 * show on a wearable device splits from devices that only show actions added
9226 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9227 * of which actions display on different devices.
9228 *
9229 * @param action the action to add to this notification
9230 * @return this object for method chaining
9231 * @see android.app.Notification.Action
9232 */
9233 public WearableExtender addAction(Action action) {
9234 mActions.add(action);
9235 return this;
9236 }
9237
9238 /**
9239 * Adds wearable actions to this notification.
9240 *
9241 * <p>When wearable actions are added using this method, the set of actions that
9242 * show on a wearable device splits from devices that only show actions added
9243 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
9244 * of which actions display on different devices.
9245 *
9246 * @param actions the actions to add to this notification
9247 * @return this object for method chaining
9248 * @see android.app.Notification.Action
9249 */
9250 public WearableExtender addActions(List<Action> actions) {
9251 mActions.addAll(actions);
9252 return this;
9253 }
9254
9255 /**
9256 * Clear all wearable actions present on this builder.
9257 * @return this object for method chaining.
9258 * @see #addAction
9259 */
9260 public WearableExtender clearActions() {
9261 mActions.clear();
9262 return this;
9263 }
9264
9265 /**
9266 * Get the wearable actions present on this notification.
9267 */
9268 public List<Action> getActions() {
9269 return mActions;
9270 }
9271
9272 /**
9273 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07009274 * this notification. The {@link PendingIntent} provided should be for an activity.
9275 *
9276 * <pre class="prettyprint">
9277 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
9278 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
9279 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
9280 * Notification notif = new Notification.Builder(context)
9281 * .extend(new Notification.WearableExtender()
9282 * .setDisplayIntent(displayPendingIntent)
9283 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
9284 * .build();</pre>
9285 *
9286 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07009287 * should have an empty task affinity. It is also recommended to use the device
9288 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07009289 *
9290 * <p>Example AndroidManifest.xml entry:
9291 * <pre class="prettyprint">
9292 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
9293 * android:exported=&quot;true&quot;
9294 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07009295 * android:taskAffinity=&quot;&quot;
9296 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07009297 *
9298 * @param intent the {@link PendingIntent} for an activity
9299 * @return this object for method chaining
9300 * @see android.app.Notification.WearableExtender#getDisplayIntent
Gus Prevasd7363752018-09-18 14:35:15 -04009301 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009302 */
Gus Prevasd7363752018-09-18 14:35:15 -04009303 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009304 public WearableExtender setDisplayIntent(PendingIntent intent) {
9305 mDisplayIntent = intent;
9306 return this;
9307 }
9308
9309 /**
9310 * Get the intent to launch inside of an activity view when displaying this
9311 * notification. This {@code PendingIntent} should be for an activity.
Gus Prevasd7363752018-09-18 14:35:15 -04009312 *
9313 * @deprecated Display intents are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009314 */
Gus Prevasd7363752018-09-18 14:35:15 -04009315 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009316 public PendingIntent getDisplayIntent() {
9317 return mDisplayIntent;
9318 }
9319
9320 /**
9321 * Add an additional page of content to display with this notification. The current
9322 * notification forms the first page, and pages added using this function form
9323 * subsequent pages. This field can be used to separate a notification into multiple
9324 * sections.
9325 *
9326 * @param page the notification to add as another page
9327 * @return this object for method chaining
9328 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009329 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009330 */
Gus Prevasd7363752018-09-18 14:35:15 -04009331 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009332 public WearableExtender addPage(Notification page) {
9333 mPages.add(page);
9334 return this;
9335 }
9336
9337 /**
9338 * Add additional pages of content to display with this notification. The current
9339 * notification forms the first page, and pages added using this function form
9340 * subsequent pages. This field can be used to separate a notification into multiple
9341 * sections.
9342 *
9343 * @param pages a list of notifications
9344 * @return this object for method chaining
9345 * @see android.app.Notification.WearableExtender#getPages
Gus Prevasd7363752018-09-18 14:35:15 -04009346 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009347 */
Gus Prevasd7363752018-09-18 14:35:15 -04009348 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009349 public WearableExtender addPages(List<Notification> pages) {
9350 mPages.addAll(pages);
9351 return this;
9352 }
9353
9354 /**
9355 * Clear all additional pages present on this builder.
9356 * @return this object for method chaining.
9357 * @see #addPage
Gus Prevasd7363752018-09-18 14:35:15 -04009358 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009359 */
Gus Prevasd7363752018-09-18 14:35:15 -04009360 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009361 public WearableExtender clearPages() {
9362 mPages.clear();
9363 return this;
9364 }
9365
9366 /**
9367 * Get the array of additional pages of content for displaying this notification. The
9368 * current notification forms the first page, and elements within this array form
9369 * subsequent pages. This field can be used to separate a notification into multiple
9370 * sections.
9371 * @return the pages for this notification
Gus Prevasd7363752018-09-18 14:35:15 -04009372 * @deprecated Multiple content pages are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009373 */
Gus Prevasd7363752018-09-18 14:35:15 -04009374 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009375 public List<Notification> getPages() {
9376 return mPages;
9377 }
9378
9379 /**
9380 * Set a background image to be displayed behind the notification content.
9381 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9382 * will work with any notification style.
9383 *
9384 * @param background the background bitmap
9385 * @return this object for method chaining
9386 * @see android.app.Notification.WearableExtender#getBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009387 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009388 */
Gus Prevasd7363752018-09-18 14:35:15 -04009389 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009390 public WearableExtender setBackground(Bitmap background) {
9391 mBackground = background;
9392 return this;
9393 }
9394
9395 /**
9396 * Get a background image to be displayed behind the notification content.
9397 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
9398 * will work with any notification style.
9399 *
9400 * @return the background image
9401 * @see android.app.Notification.WearableExtender#setBackground
Gus Prevasd7363752018-09-18 14:35:15 -04009402 * @deprecated Background images are no longer supported.
Griff Hazen61a9e862014-05-22 16:05:19 -07009403 */
Gus Prevasd7363752018-09-18 14:35:15 -04009404 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009405 public Bitmap getBackground() {
9406 return mBackground;
9407 }
9408
9409 /**
9410 * Set an icon that goes with the content of this notification.
9411 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009412 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009413 public WearableExtender setContentIcon(int icon) {
9414 mContentIcon = icon;
9415 return this;
9416 }
9417
9418 /**
9419 * Get an icon that goes with the content of this notification.
9420 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009421 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009422 public int getContentIcon() {
9423 return mContentIcon;
9424 }
9425
9426 /**
9427 * Set the gravity that the content icon should have within the notification display.
9428 * Supported values include {@link android.view.Gravity#START} and
9429 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9430 * @see #setContentIcon
9431 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009432 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009433 public WearableExtender setContentIconGravity(int contentIconGravity) {
9434 mContentIconGravity = contentIconGravity;
9435 return this;
9436 }
9437
9438 /**
9439 * Get the gravity that the content icon should have within the notification display.
9440 * Supported values include {@link android.view.Gravity#START} and
9441 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
9442 * @see #getContentIcon
9443 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009444 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009445 public int getContentIconGravity() {
9446 return mContentIconGravity;
9447 }
9448
9449 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009450 * Set an action from this notification's actions as the primary action. If the action has a
9451 * {@link RemoteInput} associated with it, shortcuts to the options for that input are shown
9452 * directly on the notification.
Griff Hazen14f57992014-05-26 09:07:14 -07009453 *
Gus Prevasd7363752018-09-18 14:35:15 -04009454 * @param actionIndex The index of the primary action.
Griff Hazen14f57992014-05-26 09:07:14 -07009455 * If wearable actions were added to the main notification, this index
9456 * will apply to that list, otherwise it will apply to the regular
9457 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07009458 */
9459 public WearableExtender setContentAction(int actionIndex) {
9460 mContentActionIndex = actionIndex;
9461 return this;
9462 }
9463
9464 /**
Gus Prevasd7363752018-09-18 14:35:15 -04009465 * Get the index of the notification action, if any, that was specified as the primary
9466 * action.
Griff Hazen14f57992014-05-26 09:07:14 -07009467 *
9468 * <p>If wearable specific actions were added to the main notification, this index will
9469 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07009470 *
9471 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07009472 */
9473 public int getContentAction() {
9474 return mContentActionIndex;
9475 }
9476
9477 /**
9478 * Set the gravity that this notification should have within the available viewport space.
9479 * Supported values include {@link android.view.Gravity#TOP},
9480 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9481 * The default value is {@link android.view.Gravity#BOTTOM}.
9482 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009483 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009484 public WearableExtender setGravity(int gravity) {
9485 mGravity = gravity;
9486 return this;
9487 }
9488
9489 /**
9490 * Get the gravity that this notification should have within the available viewport space.
9491 * Supported values include {@link android.view.Gravity#TOP},
9492 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
9493 * The default value is {@link android.view.Gravity#BOTTOM}.
9494 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009495 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009496 public int getGravity() {
9497 return mGravity;
9498 }
9499
9500 /**
9501 * Set the custom size preset for the display of this notification out of the available
9502 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9503 * {@link #SIZE_LARGE}.
9504 * <p>Some custom size presets are only applicable for custom display notifications created
9505 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
9506 * documentation for the preset in question. See also
9507 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
9508 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009509 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009510 public WearableExtender setCustomSizePreset(int sizePreset) {
9511 mCustomSizePreset = sizePreset;
9512 return this;
9513 }
9514
9515 /**
9516 * Get the custom size preset for the display of this notification out of the available
9517 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
9518 * {@link #SIZE_LARGE}.
9519 * <p>Some custom size presets are only applicable for custom display notifications created
9520 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
9521 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
9522 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009523 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009524 public int getCustomSizePreset() {
9525 return mCustomSizePreset;
9526 }
9527
9528 /**
9529 * Set the custom height in pixels for the display of this notification's content.
9530 * <p>This option is only available for custom display notifications created
9531 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
9532 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
9533 * {@link #getCustomContentHeight}.
9534 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009535 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009536 public WearableExtender setCustomContentHeight(int height) {
9537 mCustomContentHeight = height;
9538 return this;
9539 }
9540
9541 /**
9542 * Get the custom height in pixels for the display of this notification's content.
9543 * <p>This option is only available for custom display notifications created
9544 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
9545 * {@link #setCustomContentHeight}.
9546 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009547 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009548 public int getCustomContentHeight() {
9549 return mCustomContentHeight;
9550 }
9551
9552 /**
9553 * Set whether the scrolling position for the contents of this notification should start
9554 * at the bottom of the contents instead of the top when the contents are too long to
9555 * display within the screen. Default is false (start scroll at the top).
9556 */
9557 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
9558 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
9559 return this;
9560 }
9561
9562 /**
9563 * Get whether the scrolling position for the contents of this notification should start
9564 * at the bottom of the contents instead of the top when the contents are too long to
9565 * display within the screen. Default is false (start scroll at the top).
9566 */
9567 public boolean getStartScrollBottom() {
9568 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
9569 }
9570
9571 /**
9572 * Set whether the content intent is available when the wearable device is not connected
9573 * to a companion device. The user can still trigger this intent when the wearable device
9574 * is offline, but a visual hint will indicate that the content intent may not be available.
9575 * Defaults to true.
9576 */
9577 public WearableExtender setContentIntentAvailableOffline(
9578 boolean contentIntentAvailableOffline) {
9579 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
9580 return this;
9581 }
9582
9583 /**
9584 * Get whether the content intent is available when the wearable device is not connected
9585 * to a companion device. The user can still trigger this intent when the wearable device
9586 * is offline, but a visual hint will indicate that the content intent may not be available.
9587 * Defaults to true.
9588 */
9589 public boolean getContentIntentAvailableOffline() {
9590 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
9591 }
9592
9593 /**
9594 * Set a hint that this notification's icon should not be displayed.
9595 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
9596 * @return this object for method chaining
9597 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009598 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009599 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
9600 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
9601 return this;
9602 }
9603
9604 /**
9605 * Get a hint that this notification's icon should not be displayed.
9606 * @return {@code true} if this icon should not be displayed, false otherwise.
9607 * The default value is {@code false} if this was never set.
9608 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009609 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009610 public boolean getHintHideIcon() {
9611 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
9612 }
9613
9614 /**
9615 * Set a visual hint that only the background image of this notification should be
9616 * displayed, and other semantic content should be hidden. This hint is only applicable
9617 * to sub-pages added using {@link #addPage}.
9618 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009619 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009620 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
9621 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
9622 return this;
9623 }
9624
9625 /**
9626 * Get a visual hint that only the background image of this notification should be
9627 * displayed, and other semantic content should be hidden. This hint is only applicable
9628 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
9629 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009630 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07009631 public boolean getHintShowBackgroundOnly() {
9632 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
9633 }
9634
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009635 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009636 * Set a hint that this notification's background should not be clipped if possible,
9637 * and should instead be resized to fully display on the screen, retaining the aspect
9638 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009639 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
9640 * @return this object for method chaining
9641 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009642 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009643 public WearableExtender setHintAvoidBackgroundClipping(
9644 boolean hintAvoidBackgroundClipping) {
9645 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
9646 return this;
9647 }
9648
9649 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08009650 * Get a hint that this notification's background should not be clipped if possible,
9651 * and should instead be resized to fully display on the screen, retaining the aspect
9652 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009653 * @return {@code true} if it's ok if the background is clipped on the screen, false
9654 * otherwise. The default value is {@code false} if this was never set.
9655 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009656 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009657 public boolean getHintAvoidBackgroundClipping() {
9658 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
9659 }
9660
9661 /**
9662 * Set a hint that the screen should remain on for at least this duration when
9663 * this notification is displayed on the screen.
9664 * @param timeout The requested screen timeout in milliseconds. Can also be either
9665 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9666 * @return this object for method chaining
9667 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009668 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009669 public WearableExtender setHintScreenTimeout(int timeout) {
9670 mHintScreenTimeout = timeout;
9671 return this;
9672 }
9673
9674 /**
9675 * Get the duration, in milliseconds, that the screen should remain on for
9676 * when this notification is displayed.
9677 * @return the duration in milliseconds if > 0, or either one of the sentinel values
9678 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
9679 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05009680 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07009681 public int getHintScreenTimeout() {
9682 return mHintScreenTimeout;
9683 }
9684
Alex Hills9ab3a232016-04-05 14:54:56 -04009685 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04009686 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
9687 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9688 * qr codes, as well as other simple black-and-white tickets.
9689 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
9690 * @return this object for method chaining
Gus Prevasd7363752018-09-18 14:35:15 -04009691 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009692 */
Gus Prevasd7363752018-09-18 14:35:15 -04009693 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009694 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
9695 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
9696 return this;
9697 }
9698
9699 /**
9700 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
9701 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
9702 * qr codes, as well as other simple black-and-white tickets.
9703 * @return {@code true} if it should be displayed in ambient, false otherwise
9704 * otherwise. The default value is {@code false} if this was never set.
Gus Prevasd7363752018-09-18 14:35:15 -04009705 * @deprecated This feature is no longer supported.
Alex Hills4bcb06b2016-04-05 14:26:25 -04009706 */
Gus Prevasd7363752018-09-18 14:35:15 -04009707 @Deprecated
Alex Hills4bcb06b2016-04-05 14:26:25 -04009708 public boolean getHintAmbientBigPicture() {
9709 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
9710 }
9711
9712 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04009713 * Set a hint that this notification's content intent will launch an {@link Activity}
9714 * directly, telling the platform that it can generate the appropriate transitions.
9715 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
9716 * an activity and transitions should be generated, false otherwise.
9717 * @return this object for method chaining
9718 */
9719 public WearableExtender setHintContentIntentLaunchesActivity(
9720 boolean hintContentIntentLaunchesActivity) {
9721 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
9722 return this;
9723 }
9724
9725 /**
9726 * Get a hint that this notification's content intent will launch an {@link Activity}
9727 * directly, telling the platform that it can generate the appropriate transitions
9728 * @return {@code true} if the content intent will launch an activity and transitions should
9729 * be generated, false otherwise. The default value is {@code false} if this was never set.
9730 */
9731 public boolean getHintContentIntentLaunchesActivity() {
9732 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
9733 }
9734
Nadia Benbernou948627e2016-04-14 14:41:08 -04009735 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009736 * Sets the dismissal id for this notification. If a notification is posted with a
9737 * dismissal id, then when that notification is canceled, notifications on other wearables
9738 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04009739 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009740 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04009741 * @param dismissalId the dismissal id of the notification.
9742 * @return this object for method chaining
9743 */
9744 public WearableExtender setDismissalId(String dismissalId) {
9745 mDismissalId = dismissalId;
9746 return this;
9747 }
9748
9749 /**
9750 * Returns the dismissal id of the notification.
9751 * @return the dismissal id of the notification or null if it has not been set.
9752 */
9753 public String getDismissalId() {
9754 return mDismissalId;
9755 }
9756
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04009757 /**
9758 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
9759 * posted from a phone to provide finer-grained control on what notifications are bridged
9760 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
9761 * Features to Notifications</a> for more information.
9762 * @param bridgeTag the bridge tag of the notification.
9763 * @return this object for method chaining
9764 */
9765 public WearableExtender setBridgeTag(String bridgeTag) {
9766 mBridgeTag = bridgeTag;
9767 return this;
9768 }
9769
9770 /**
9771 * Returns the bridge tag of the notification.
9772 * @return the bridge tag or null if not present.
9773 */
9774 public String getBridgeTag() {
9775 return mBridgeTag;
9776 }
9777
Griff Hazen61a9e862014-05-22 16:05:19 -07009778 private void setFlag(int mask, boolean value) {
9779 if (value) {
9780 mFlags |= mask;
9781 } else {
9782 mFlags &= ~mask;
9783 }
9784 }
9785 }
9786
9787 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009788 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9789 * with car extensions:
9790 *
9791 * <ol>
9792 * <li>Create an {@link Notification.Builder}, setting any desired
9793 * properties.
9794 * <li>Create a {@link CarExtender}.
9795 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9796 * {@link CarExtender}.
9797 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9798 * to apply the extensions to a notification.
9799 * </ol>
9800 *
9801 * <pre class="prettyprint">
9802 * Notification notification = new Notification.Builder(context)
9803 * ...
9804 * .extend(new CarExtender()
9805 * .set*(...))
9806 * .build();
9807 * </pre>
9808 *
9809 * <p>Car extensions can be accessed on an existing notification by using the
9810 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9811 * to access values.
9812 */
9813 public static final class CarExtender implements Extender {
9814 private static final String TAG = "CarExtender";
9815
9816 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9817 private static final String EXTRA_LARGE_ICON = "large_icon";
9818 private static final String EXTRA_CONVERSATION = "car_conversation";
9819 private static final String EXTRA_COLOR = "app_color";
9820
9821 private Bitmap mLargeIcon;
9822 private UnreadConversation mUnreadConversation;
9823 private int mColor = Notification.COLOR_DEFAULT;
9824
9825 /**
9826 * Create a {@link CarExtender} with default options.
9827 */
9828 public CarExtender() {
9829 }
9830
9831 /**
9832 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9833 *
9834 * @param notif The notification from which to copy options.
9835 */
9836 public CarExtender(Notification notif) {
9837 Bundle carBundle = notif.extras == null ?
9838 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9839 if (carBundle != null) {
9840 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9841 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9842
9843 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9844 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9845 }
9846 }
9847
9848 /**
9849 * Apply car extensions to a notification that is being built. This is typically called by
9850 * the {@link Notification.Builder#extend(Notification.Extender)}
9851 * method of {@link Notification.Builder}.
9852 */
9853 @Override
9854 public Notification.Builder extend(Notification.Builder builder) {
9855 Bundle carExtensions = new Bundle();
9856
9857 if (mLargeIcon != null) {
9858 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9859 }
9860 if (mColor != Notification.COLOR_DEFAULT) {
9861 carExtensions.putInt(EXTRA_COLOR, mColor);
9862 }
9863
9864 if (mUnreadConversation != null) {
9865 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9866 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9867 }
9868
9869 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9870 return builder;
9871 }
9872
9873 /**
9874 * Sets the accent color to use when Android Auto presents the notification.
9875 *
9876 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9877 * to accent the displayed notification. However, not all colors are acceptable in an
9878 * automotive setting. This method can be used to override the color provided in the
9879 * notification in such a situation.
9880 */
Tor Norbye80756e32015-03-02 09:39:27 -08009881 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009882 mColor = color;
9883 return this;
9884 }
9885
9886 /**
9887 * Gets the accent color.
9888 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009889 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009890 */
Tor Norbye80756e32015-03-02 09:39:27 -08009891 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009892 public int getColor() {
9893 return mColor;
9894 }
9895
9896 /**
9897 * Sets the large icon of the car notification.
9898 *
9899 * If no large icon is set in the extender, Android Auto will display the icon
9900 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9901 *
9902 * @param largeIcon The large icon to use in the car notification.
9903 * @return This object for method chaining.
9904 */
9905 public CarExtender setLargeIcon(Bitmap largeIcon) {
9906 mLargeIcon = largeIcon;
9907 return this;
9908 }
9909
9910 /**
9911 * Gets the large icon used in this car notification, or null if no icon has been set.
9912 *
9913 * @return The large icon for the car notification.
9914 * @see CarExtender#setLargeIcon
9915 */
9916 public Bitmap getLargeIcon() {
9917 return mLargeIcon;
9918 }
9919
9920 /**
9921 * Sets the unread conversation in a message notification.
9922 *
9923 * @param unreadConversation The unread part of the conversation this notification conveys.
9924 * @return This object for method chaining.
9925 */
9926 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9927 mUnreadConversation = unreadConversation;
9928 return this;
9929 }
9930
9931 /**
9932 * Returns the unread conversation conveyed by this notification.
9933 * @see #setUnreadConversation(UnreadConversation)
9934 */
9935 public UnreadConversation getUnreadConversation() {
9936 return mUnreadConversation;
9937 }
9938
9939 /**
9940 * A class which holds the unread messages from a conversation.
9941 */
9942 public static class UnreadConversation {
9943 private static final String KEY_AUTHOR = "author";
9944 private static final String KEY_TEXT = "text";
9945 private static final String KEY_MESSAGES = "messages";
9946 private static final String KEY_REMOTE_INPUT = "remote_input";
9947 private static final String KEY_ON_REPLY = "on_reply";
9948 private static final String KEY_ON_READ = "on_read";
9949 private static final String KEY_PARTICIPANTS = "participants";
9950 private static final String KEY_TIMESTAMP = "timestamp";
9951
9952 private final String[] mMessages;
9953 private final RemoteInput mRemoteInput;
9954 private final PendingIntent mReplyPendingIntent;
9955 private final PendingIntent mReadPendingIntent;
9956 private final String[] mParticipants;
9957 private final long mLatestTimestamp;
9958
9959 UnreadConversation(String[] messages, RemoteInput remoteInput,
9960 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9961 String[] participants, long latestTimestamp) {
9962 mMessages = messages;
9963 mRemoteInput = remoteInput;
9964 mReadPendingIntent = readPendingIntent;
9965 mReplyPendingIntent = replyPendingIntent;
9966 mParticipants = participants;
9967 mLatestTimestamp = latestTimestamp;
9968 }
9969
9970 /**
9971 * Gets the list of messages conveyed by this notification.
9972 */
9973 public String[] getMessages() {
9974 return mMessages;
9975 }
9976
9977 /**
9978 * Gets the remote input that will be used to convey the response to a message list, or
9979 * null if no such remote input exists.
9980 */
9981 public RemoteInput getRemoteInput() {
9982 return mRemoteInput;
9983 }
9984
9985 /**
9986 * Gets the pending intent that will be triggered when the user replies to this
9987 * notification.
9988 */
9989 public PendingIntent getReplyPendingIntent() {
9990 return mReplyPendingIntent;
9991 }
9992
9993 /**
9994 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9995 * in this object's message list.
9996 */
9997 public PendingIntent getReadPendingIntent() {
9998 return mReadPendingIntent;
9999 }
10000
10001 /**
10002 * Gets the participants in the conversation.
10003 */
10004 public String[] getParticipants() {
10005 return mParticipants;
10006 }
10007
10008 /**
10009 * Gets the firs participant in the conversation.
10010 */
10011 public String getParticipant() {
10012 return mParticipants.length > 0 ? mParticipants[0] : null;
10013 }
10014
10015 /**
10016 * Gets the timestamp of the conversation.
10017 */
10018 public long getLatestTimestamp() {
10019 return mLatestTimestamp;
10020 }
10021
10022 Bundle getBundleForUnreadConversation() {
10023 Bundle b = new Bundle();
10024 String author = null;
10025 if (mParticipants != null && mParticipants.length > 1) {
10026 author = mParticipants[0];
10027 }
10028 Parcelable[] messages = new Parcelable[mMessages.length];
10029 for (int i = 0; i < messages.length; i++) {
10030 Bundle m = new Bundle();
10031 m.putString(KEY_TEXT, mMessages[i]);
10032 m.putString(KEY_AUTHOR, author);
10033 messages[i] = m;
10034 }
10035 b.putParcelableArray(KEY_MESSAGES, messages);
10036 if (mRemoteInput != null) {
10037 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
10038 }
10039 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
10040 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
10041 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
10042 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
10043 return b;
10044 }
10045
10046 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
10047 if (b == null) {
10048 return null;
10049 }
10050 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
10051 String[] messages = null;
10052 if (parcelableMessages != null) {
10053 String[] tmp = new String[parcelableMessages.length];
10054 boolean success = true;
10055 for (int i = 0; i < tmp.length; i++) {
10056 if (!(parcelableMessages[i] instanceof Bundle)) {
10057 success = false;
10058 break;
10059 }
10060 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
10061 if (tmp[i] == null) {
10062 success = false;
10063 break;
10064 }
10065 }
10066 if (success) {
10067 messages = tmp;
10068 } else {
10069 return null;
10070 }
10071 }
10072
10073 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
10074 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
10075
10076 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
10077
10078 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
10079 if (participants == null || participants.length != 1) {
10080 return null;
10081 }
10082
10083 return new UnreadConversation(messages,
10084 remoteInput,
10085 onReply,
10086 onRead,
10087 participants, b.getLong(KEY_TIMESTAMP));
10088 }
10089 };
10090
10091 /**
10092 * Builder class for {@link CarExtender.UnreadConversation} objects.
10093 */
10094 public static class Builder {
10095 private final List<String> mMessages = new ArrayList<String>();
10096 private final String mParticipant;
10097 private RemoteInput mRemoteInput;
10098 private PendingIntent mReadPendingIntent;
10099 private PendingIntent mReplyPendingIntent;
10100 private long mLatestTimestamp;
10101
10102 /**
10103 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
10104 *
10105 * @param name The name of the other participant in the conversation.
10106 */
10107 public Builder(String name) {
10108 mParticipant = name;
10109 }
10110
10111 /**
10112 * Appends a new unread message to the list of messages for this conversation.
10113 *
10114 * The messages should be added from oldest to newest.
10115 *
10116 * @param message The text of the new unread message.
10117 * @return This object for method chaining.
10118 */
10119 public Builder addMessage(String message) {
10120 mMessages.add(message);
10121 return this;
10122 }
10123
10124 /**
10125 * Sets the pending intent and remote input which will convey the reply to this
10126 * notification.
10127 *
10128 * @param pendingIntent The pending intent which will be triggered on a reply.
10129 * @param remoteInput The remote input parcelable which will carry the reply.
10130 * @return This object for method chaining.
10131 *
10132 * @see CarExtender.UnreadConversation#getRemoteInput
10133 * @see CarExtender.UnreadConversation#getReplyPendingIntent
10134 */
10135 public Builder setReplyAction(
10136 PendingIntent pendingIntent, RemoteInput remoteInput) {
10137 mRemoteInput = remoteInput;
10138 mReplyPendingIntent = pendingIntent;
10139
10140 return this;
10141 }
10142
10143 /**
10144 * Sets the pending intent that will be sent once the messages in this notification
10145 * are read.
10146 *
10147 * @param pendingIntent The pending intent to use.
10148 * @return This object for method chaining.
10149 */
10150 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
10151 mReadPendingIntent = pendingIntent;
10152 return this;
10153 }
10154
10155 /**
10156 * Sets the timestamp of the most recent message in an unread conversation.
10157 *
10158 * If a messaging notification has been posted by your application and has not
10159 * yet been cancelled, posting a later notification with the same id and tag
10160 * but without a newer timestamp may result in Android Auto not displaying a
10161 * heads up notification for the later notification.
10162 *
10163 * @param timestamp The timestamp of the most recent message in the conversation.
10164 * @return This object for method chaining.
10165 */
10166 public Builder setLatestTimestamp(long timestamp) {
10167 mLatestTimestamp = timestamp;
10168 return this;
10169 }
10170
10171 /**
10172 * Builds a new unread conversation object.
10173 *
10174 * @return The new unread conversation object.
10175 */
10176 public UnreadConversation build() {
10177 String[] messages = mMessages.toArray(new String[mMessages.size()]);
10178 String[] participants = { mParticipant };
10179 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
10180 mReadPendingIntent, participants, mLatestTimestamp);
10181 }
10182 }
10183 }
10184
10185 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010186 * <p>Helper class to add Android TV extensions to notifications. To create a notification
10187 * with a TV extension:
10188 *
10189 * <ol>
10190 * <li>Create an {@link Notification.Builder}, setting any desired properties.
10191 * <li>Create a {@link TvExtender}.
10192 * <li>Set TV-specific properties using the {@code set} methods of
10193 * {@link TvExtender}.
10194 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
10195 * to apply the extension to a notification.
10196 * </ol>
10197 *
10198 * <pre class="prettyprint">
10199 * Notification notification = new Notification.Builder(context)
10200 * ...
10201 * .extend(new TvExtender()
10202 * .set*(...))
10203 * .build();
10204 * </pre>
10205 *
10206 * <p>TV extensions can be accessed on an existing notification by using the
10207 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
10208 * to access values.
10209 *
10210 * @hide
10211 */
10212 @SystemApi
10213 public static final class TvExtender implements Extender {
10214 private static final String TAG = "TvExtender";
10215
10216 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
10217 private static final String EXTRA_FLAGS = "flags";
10218 private static final String EXTRA_CONTENT_INTENT = "content_intent";
10219 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010220 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -080010221 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010222
10223 // Flags bitwise-ored to mFlags
10224 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
10225
10226 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010227 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010228 private PendingIntent mContentIntent;
10229 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -080010230 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010231
10232 /**
10233 * Create a {@link TvExtender} with default options.
10234 */
10235 public TvExtender() {
10236 mFlags = FLAG_AVAILABLE_ON_TV;
10237 }
10238
10239 /**
10240 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
10241 *
10242 * @param notif The notification from which to copy options.
10243 */
10244 public TvExtender(Notification notif) {
10245 Bundle bundle = notif.extras == null ?
10246 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
10247 if (bundle != null) {
10248 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010249 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010250 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010251 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
10252 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
10253 }
10254 }
10255
10256 /**
10257 * Apply a TV extension to a notification that is being built. This is typically called by
10258 * the {@link Notification.Builder#extend(Notification.Extender)}
10259 * method of {@link Notification.Builder}.
10260 */
10261 @Override
10262 public Notification.Builder extend(Notification.Builder builder) {
10263 Bundle bundle = new Bundle();
10264
10265 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010266 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -080010267 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010268 if (mContentIntent != null) {
10269 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
10270 }
10271
10272 if (mDeleteIntent != null) {
10273 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
10274 }
10275
10276 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
10277 return builder;
10278 }
10279
10280 /**
10281 * Returns true if this notification should be shown on TV. This method return true
10282 * if the notification was extended with a TvExtender.
10283 */
10284 public boolean isAvailableOnTv() {
10285 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
10286 }
10287
10288 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010289 * Specifies the channel the notification should be delivered on when shown on TV.
10290 * It can be different from the channel that the notification is delivered to when
10291 * posting on a non-TV device.
10292 */
10293 public TvExtender setChannel(String channelId) {
10294 mChannelId = channelId;
10295 return this;
10296 }
10297
10298 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010299 * Specifies the channel the notification should be delivered on when shown on TV.
10300 * It can be different from the channel that the notification is delivered to when
10301 * posting on a non-TV device.
10302 */
10303 public TvExtender setChannelId(String channelId) {
10304 mChannelId = channelId;
10305 return this;
10306 }
10307
Jeff Sharkey000ce802017-04-29 13:13:27 -060010308 /** @removed */
10309 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -080010310 public String getChannel() {
10311 return mChannelId;
10312 }
10313
10314 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -040010315 * Returns the id of the channel this notification posts to on TV.
10316 */
10317 public String getChannelId() {
10318 return mChannelId;
10319 }
10320
10321 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010322 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
10323 * If provided, it is used instead of the content intent specified
10324 * at the level of Notification.
10325 */
10326 public TvExtender setContentIntent(PendingIntent intent) {
10327 mContentIntent = intent;
10328 return this;
10329 }
10330
10331 /**
10332 * Returns the TV-specific content intent. If this method returns null, the
10333 * main content intent on the notification should be used.
10334 *
10335 * @see {@link Notification#contentIntent}
10336 */
10337 public PendingIntent getContentIntent() {
10338 return mContentIntent;
10339 }
10340
10341 /**
10342 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
10343 * by the user on TV. If provided, it is used instead of the delete intent specified
10344 * at the level of Notification.
10345 */
10346 public TvExtender setDeleteIntent(PendingIntent intent) {
10347 mDeleteIntent = intent;
10348 return this;
10349 }
10350
10351 /**
10352 * Returns the TV-specific delete intent. If this method returns null, the
10353 * main delete intent on the notification should be used.
10354 *
10355 * @see {@link Notification#deleteIntent}
10356 */
10357 public PendingIntent getDeleteIntent() {
10358 return mDeleteIntent;
10359 }
Rhiannon Malia1a083932018-01-24 15:02:30 -080010360
10361 /**
10362 * Specifies whether this notification should suppress showing a message over top of apps
10363 * outside of the launcher.
10364 */
10365 public TvExtender setSuppressShowOverApps(boolean suppress) {
10366 mSuppressShowOverApps = suppress;
10367 return this;
10368 }
10369
10370 /**
10371 * Returns true if this notification should not show messages over top of apps
10372 * outside of the launcher.
10373 */
10374 public boolean getSuppressShowOverApps() {
10375 return mSuppressShowOverApps;
10376 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -080010377 }
10378
10379 /**
Griff Hazen61a9e862014-05-22 16:05:19 -070010380 * Get an array of Notification objects from a parcelable array bundle field.
10381 * Update the bundle to have a typed array so fetches in the future don't need
10382 * to do an array copy.
10383 */
10384 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
10385 Parcelable[] array = bundle.getParcelableArray(key);
10386 if (array instanceof Notification[] || array == null) {
10387 return (Notification[]) array;
10388 }
10389 Notification[] typedArray = Arrays.copyOf(array, array.length,
10390 Notification[].class);
10391 bundle.putParcelableArray(key, typedArray);
10392 return typedArray;
10393 }
Christoph Studer4600f9b2014-07-22 22:44:43 +020010394
10395 private static class BuilderRemoteViews extends RemoteViews {
10396 public BuilderRemoteViews(Parcel parcel) {
10397 super(parcel);
10398 }
10399
Kenny Guy77320062014-08-27 21:37:15 +010010400 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
10401 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +020010402 }
10403
10404 @Override
10405 public BuilderRemoteViews clone() {
10406 Parcel p = Parcel.obtain();
10407 writeToParcel(p, 0);
10408 p.setDataPosition(0);
10409 BuilderRemoteViews brv = new BuilderRemoteViews(p);
10410 p.recycle();
10411 return brv;
10412 }
10413 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010414
Selim Cinek384804b2018-04-18 14:31:07 +080010415 /**
10416 * A result object where information about the template that was created is saved.
10417 */
10418 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +080010419 int mIconMarginEnd;
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010420 boolean mRightIconContainerVisible;
Selim Cinek384804b2018-04-18 14:31:07 +080010421
10422 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +080010423 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +080010424 * with the right actions.
10425 */
Selim Cinek1c72fa02018-04-23 18:00:54 +080010426 public int getIconMarginEnd() {
10427 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010428 }
10429
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010430 /**
10431 * Is the icon container visible on the right size because of the reply button or the
10432 * right icon.
10433 */
10434 public boolean isRightIconContainerVisible() {
10435 return mRightIconContainerVisible;
10436 }
10437
Selim Cinek1c72fa02018-04-23 18:00:54 +080010438 public void setIconMarginEnd(int iconMarginEnd) {
10439 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +080010440 }
Selim Cinek6e41b0e2018-12-06 18:10:20 -080010441
10442 public void setRightIconContainerVisible(boolean iconContainerVisible) {
10443 mRightIconContainerVisible = iconContainerVisible;
10444 }
Selim Cinek384804b2018-04-18 14:31:07 +080010445 }
10446
Adrian Roos70d7aa32017-01-11 15:39:06 -080010447 private static class StandardTemplateParams {
10448 boolean hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010449 CharSequence title;
10450 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -080010451 CharSequence headerTextSecondary;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010452 CharSequence summaryText;
Selim Cinekbee4e072018-05-21 22:06:43 -070010453 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -070010454 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +080010455 boolean hideReplyIcon;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010456 boolean allowColorization = true;
10457 boolean forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010458
10459 final StandardTemplateParams reset() {
10460 hasProgress = true;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010461 title = null;
10462 text = null;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010463 summaryText = null;
Selim Cinekafeed292017-12-12 17:32:44 -080010464 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -070010465 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010466 allowColorization = true;
10467 forceDefaultColor = false;
Adrian Roos70d7aa32017-01-11 15:39:06 -080010468 return this;
10469 }
10470
10471 final StandardTemplateParams hasProgress(boolean hasProgress) {
10472 this.hasProgress = hasProgress;
10473 return this;
10474 }
10475
10476 final StandardTemplateParams title(CharSequence title) {
10477 this.title = title;
10478 return this;
10479 }
10480
10481 final StandardTemplateParams text(CharSequence text) {
10482 this.text = text;
10483 return this;
10484 }
10485
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010486 final StandardTemplateParams summaryText(CharSequence text) {
10487 this.summaryText = text;
10488 return this;
10489 }
10490
Selim Cinekafeed292017-12-12 17:32:44 -080010491 final StandardTemplateParams headerTextSecondary(CharSequence text) {
10492 this.headerTextSecondary = text;
10493 return this;
10494 }
10495
Selim Cinek384804b2018-04-18 14:31:07 +080010496 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
10497 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010498 return this;
10499 }
10500
Selim Cinek384804b2018-04-18 14:31:07 +080010501 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
10502 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -070010503 return this;
10504 }
10505
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010506 final StandardTemplateParams disallowColorization() {
10507 this.allowColorization = false;
10508 return this;
10509 }
10510
10511 final StandardTemplateParams forceDefaultColor() {
10512 this.forceDefaultColor = true;
10513 return this;
10514 }
10515
Adrian Roos70d7aa32017-01-11 15:39:06 -080010516 final StandardTemplateParams fillTextsFrom(Builder b) {
10517 Bundle extras = b.mN.extras;
Lucas Dupin00be88f2019-01-03 17:50:52 -080010518 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
Lucas Dupin7b4d8852019-09-23 17:07:35 -070010519 this.text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
Selim Cinek10bbc0e2018-11-21 15:14:10 -080010520 this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT);
Adrian Roos70d7aa32017-01-11 15:39:06 -080010521 return this;
10522 }
Selim Cinekbee4e072018-05-21 22:06:43 -070010523
10524 /**
10525 * Set the maximum lines of remote input history lines allowed.
10526 * @param maxRemoteInputHistory The number of lines.
10527 * @return The builder for method chaining.
10528 */
10529 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
10530 this.maxRemoteInputHistory = maxRemoteInputHistory;
10531 return this;
10532 }
Adrian Roos70d7aa32017-01-11 15:39:06 -080010533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534}