blob: 97349fe76a9734981fa4801e45fee79b18acac83 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010030import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.Context;
32import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010033import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040034import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020035import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050036import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020037import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070038import android.content.res.Configuration;
39import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040040import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010041import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070042import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010043import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010044import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040045import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040046import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070047import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080048import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070049import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040051import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020052import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050053import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070054import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.Parcel;
56import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040057import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080058import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070059import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070060import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080061import android.text.SpannableStringBuilder;
62import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080064import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080065import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070066import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080067import android.text.style.RelativeSizeSpan;
68import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070069import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040070import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050071import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070072import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070073import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080074import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080075import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070076import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070077import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.RemoteViews;
79
Griff Hazen959591e2014-05-15 22:26:18 -070080import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070081import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070082import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070083import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080084import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070085
Tor Norbyed9273d62013-05-30 15:59:53 -070086import java.lang.annotation.Retention;
87import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020088import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050089import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070090import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070091import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070092import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050093import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050094import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060095import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097/**
98 * A class that represents how a persistent notification is to be presented to
99 * the user using the {@link android.app.NotificationManager}.
100 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800101 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
102 * easier to construct Notifications.</p>
103 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700104 * <div class="special reference">
105 * <h3>Developer Guides</h3>
106 * <p>For a guide to creating notifications, read the
107 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
108 * developer guide.</p>
109 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 */
111public class Notification implements Parcelable
112{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400113 private static final String TAG = "Notification";
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400116 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400117 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400118 */
119 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
120 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
121 = "android.intent.category.NOTIFICATION_PREFERENCES";
122
123 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
125 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400126 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500127 */
128 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
129
130 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400131 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400132 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
133 * what settings should be shown in the target app.
134 */
135 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
136
137 /**
138 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400139 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
140 * that can be used to narrow down what settings should be shown in the target app.
141 */
142 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
143
144 /**
145 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
146 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
147 * that can be used to narrow down what settings should be shown in the target app.
148 */
149 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
150
151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 * Use all default values (where applicable).
153 */
154 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 /**
157 * Use the default notification sound. This will ignore any given
158 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500159 *
Chris Wren47c20a12014-06-18 17:27:29 -0400160 * <p>
161 * A notification that is noisy is more likely to be presented as a heads-up notification.
162 * </p>
163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 */
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 public static final int DEFAULT_SOUND = 1;
168
169 /**
170 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700172 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500173 *
Chris Wren47c20a12014-06-18 17:27:29 -0400174 * <p>
175 * A notification that vibrates is more likely to be presented as a heads-up notification.
176 * </p>
177 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179 */
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /**
184 * Use the default notification lights. This will ignore the
185 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
186 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 */
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200194 * Maximum length of CharSequences accepted by Builder and friends.
195 *
196 * <p>
197 * Avoids spamming the system with overly large strings such as full e-mails.
198 */
199 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
200
201 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800202 * Maximum entries of reply text that are accepted by Builder and friends.
203 */
204 private static final int MAX_REPLY_HISTORY = 5;
205
Selim Cinekde4de0e2018-01-24 16:21:07 -0800206
207 /**
208 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
209 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
210 *
211 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
212 * sends messages.</p>
213 */
214 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
215
Adrian Roose458aa82015-12-08 16:17:19 -0800216 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800218 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500219 * Default value: {@link System#currentTimeMillis() Now}.
220 *
221 * Choose a timestamp that will be most relevant to the user. For most finite events, this
222 * corresponds to the time the event happened (or will happen, in the case of events that have
223 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800224 * timestamped according to when the activity began.
225 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500228 * <ul>
229 * <li>Notification of a new chat message should be stamped when the message was received.</li>
230 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
231 * <li>Notification of a completed file download should be stamped when the download finished.</li>
232 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
233 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
234 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800235 * </ul>
236 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700237 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
238 * anymore by default and must be opted into by using
239 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public long when;
242
243 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700244 * The creation time of the notification
245 */
246 private long creationTime;
247
248 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400250 *
251 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
Dan Sandler86647982015-05-13 23:41:13 -0400253 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700254 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public int icon;
256
257 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800258 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
259 * leave it at its default value of 0.
260 *
261 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700262 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800263 */
264 public int iconLevel;
265
266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500267 * The number of events that this notification represents. For example, in a new mail
268 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800269 *
Julia Reynolds30331982017-04-27 10:12:50 -0400270 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500271 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
272 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 */
Julia Reynolds30331982017-04-27 10:12:50 -0400274 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 /**
277 * The intent to execute when the expanded status entry is clicked. If
278 * this is an activity, it must include the
279 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800280 * that you take care of task management as described in the
281 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800282 * Stack</a> document. In particular, make sure to read the notification section
283 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
284 * Notifications</a> for the correct ways to launch an application from a
285 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 */
287 public PendingIntent contentIntent;
288
289 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500290 * The intent to execute when the notification is explicitly dismissed by the user, either with
291 * the "Clear All" button or by swiping it away individually.
292 *
293 * This probably shouldn't be launching an activity since several of those will be sent
294 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public PendingIntent deleteIntent;
297
298 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700299 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800300 *
Chris Wren47c20a12014-06-18 17:27:29 -0400301 * <p>
302 * The system UI may choose to display a heads-up notification, instead of
303 * launching this intent, while the user is using the device.
304 * </p>
305 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800306 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400307 */
308 public PendingIntent fullScreenIntent;
309
310 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400311 * Text that summarizes this notification for accessibility services.
312 *
313 * As of the L release, this text is no longer shown on screen, but it is still useful to
314 * accessibility services (where it serves as an audible announcement of the notification's
315 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400316 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800317 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 */
319 public CharSequence tickerText;
320
321 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400322 * Formerly, a view showing the {@link #tickerText}.
323 *
324 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400325 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400326 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800327 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400328
329 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400330 * The view that will represent this notification in the notification list (which is pulled
331 * down from the status bar).
332 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500333 * As of N, this field may be null. The notification view is determined by the inputs
334 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 public RemoteViews contentView;
339
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400340 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400341 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400342 * opportunity to show more detail. The system UI may choose to show this
343 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500345 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
347 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400348 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400349 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400350 public RemoteViews bigContentView;
351
Chris Wren8fd39ec2014-02-27 17:43:26 -0500352
353 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400354 * A medium-format version of {@link #contentView}, providing the Notification an
355 * opportunity to add action buttons to contentView. At its discretion, the system UI may
356 * choose to show this as a heads-up notification, which will pop up so the user can see
357 * it without leaving their current activity.
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 heads-up 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#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500362 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400363 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500364 public RemoteViews headsUpContentView;
365
Julia Reynoldsfc640012018-02-21 12:25:27 -0500366 private boolean mUsesStandardHeader;
367
368 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
369 static {
370 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
371 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
372 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
373 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
378 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
381 }
382
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400383 /**
Dan Sandler86647982015-05-13 23:41:13 -0400384 * A large bitmap to be shown in the notification content area.
385 *
386 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 */
Dan Sandler86647982015-05-13 23:41:13 -0400388 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800389 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390
391 /**
392 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500393 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400395 * A notification that is noisy is more likely to be presented as a heads-up notification.
396 * </p>
397 *
398 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500399 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500401 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500403 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 public Uri sound;
405
406 /**
407 * Use this constant as the value for audioStreamType to request that
408 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700409 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400410 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500411 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700413 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 public static final int STREAM_DEFAULT = -1;
415
416 /**
417 * The audio stream type to use when playing the sound.
418 * Should be one of the STREAM_ constants from
419 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400420 *
421 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public int audioStreamType = STREAM_DEFAULT;
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400427 * The default value of {@link #audioAttributes}.
428 */
429 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
430 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
431 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
432 .build();
433
434 /**
435 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500436 *
437 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400438 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500439 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400440 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
441
442 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500443 * The pattern with which to vibrate.
444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 * <p>
446 * To vibrate the default pattern, see {@link #defaults}.
447 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500450 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500452 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public long[] vibrate;
454
455 /**
456 * The color of the led. The hardware will do its best approximation.
457 *
458 * @see #FLAG_SHOW_LIGHTS
459 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500460 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
Tor Norbye80756e32015-03-02 09:39:27 -0800462 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 public int ledARGB;
465
466 /**
467 * The number of milliseconds for the LED to be on while it's flashing.
468 * 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 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500474 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 public int ledOnMS;
476
477 /**
478 * The number of milliseconds for the LED to be off while it's flashing.
479 * The hardware will do its best approximation.
480 *
481 * @see #FLAG_SHOW_LIGHTS
482 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 *
484 * @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 ledOffMS;
488
489 /**
490 * Specifies which values should be taken from the defaults.
491 * <p>
492 * To set, OR the desired from {@link #DEFAULT_SOUND},
493 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
494 * values, use {@link #DEFAULT_ALL}.
495 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500496 *
497 * @deprecated use {@link NotificationChannel#getSound()} and
498 * {@link NotificationChannel#shouldShowLights()} and
499 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500501 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 public int defaults;
503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 /**
505 * Bit to be bitwise-ored into the {@link #flags} field that should be
506 * set if you want the LED on for this notification.
507 * <ul>
508 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
509 * or 0 for both ledOnMS and ledOffMS.</li>
510 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
511 * <li>To flash the LED, pass the number of milliseconds that it should
512 * be on and off to ledOnMS and ledOffMS.</li>
513 * </ul>
514 * <p>
515 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800516 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 * because they will be set to values that work on any given hardware.
518 * <p>
519 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500520 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500521 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500523 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
525
526 /**
527 * Bit to be bitwise-ored into the {@link #flags} field that should be
528 * set if this notification is in reference to something that is ongoing,
529 * like a phone call. It should not be set if this notification is in
530 * reference to something that happened at a particular point in time,
531 * like a missed phone call.
532 */
533 public static final int FLAG_ONGOING_EVENT = 0x00000002;
534
535 /**
536 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700537 * the audio will be repeated until the notification is
538 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 */
540 public static final int FLAG_INSISTENT = 0x00000004;
541
542 /**
543 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700544 * set if you would only like the sound, vibrate and ticker to be played
545 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 */
547 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
548
549 /**
550 * Bit to be bitwise-ored into the {@link #flags} field that should be
551 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500552 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 */
554 public static final int FLAG_AUTO_CANCEL = 0x00000010;
555
556 /**
557 * Bit to be bitwise-ored into the {@link #flags} field that should be
558 * set if the notification should not be canceled when the user clicks
559 * the Clear all button.
560 */
561 public static final int FLAG_NO_CLEAR = 0x00000020;
562
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700563 /**
564 * Bit to be bitwise-ored into the {@link #flags} field that should be
565 * set if this notification represents a currently running service. This
566 * will normally be set for you by {@link Service#startForeground}.
567 */
568 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
569
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400570 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800572 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500573 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400574 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700575 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500576 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400577
Griff Hazendfcb0802014-02-11 12:00:00 -0800578 /**
579 * Bit to be bitswise-ored into the {@link #flags} field that should be
580 * set if this notification is relevant to the current device only
581 * and it is not recommended that it bridge to other devices.
582 */
583 public static final int FLAG_LOCAL_ONLY = 0x00000100;
584
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700585 /**
586 * Bit to be bitswise-ored into the {@link #flags} field that should be
587 * set if this notification is the group summary for a group of notifications.
588 * Grouped notifications may display in a cluster or stack on devices which
589 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
590 */
591 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
592
Julia Reynoldse46bb372016-03-17 11:05:58 -0400593 /**
594 * Bit to be bitswise-ored into the {@link #flags} field that should be
595 * set if this notification is the group summary for an auto-group of notifications.
596 *
597 * @hide
598 */
599 @SystemApi
600 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
601
Julia Reynolds4db59552017-06-30 13:34:01 -0400602 /**
603 * @hide
604 */
605 public static final int FLAG_CAN_COLORIZE = 0x00000800;
606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public int flags;
608
Tor Norbyed9273d62013-05-30 15:59:53 -0700609 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700610 @IntDef(prefix = { "PRIORITY_" }, value = {
611 PRIORITY_DEFAULT,
612 PRIORITY_LOW,
613 PRIORITY_MIN,
614 PRIORITY_HIGH,
615 PRIORITY_MAX
616 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700617 @Retention(RetentionPolicy.SOURCE)
618 public @interface Priority {}
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621 * Default notification {@link #priority}. If your application does not prioritize its own
622 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500623 *
624 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500626 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627 public static final int PRIORITY_DEFAULT = 0;
628
629 /**
630 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
631 * items smaller, or at a different position in the list, compared with your app's
632 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 *
634 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500636 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500637 public static final int PRIORITY_LOW = -1;
638
639 /**
640 * Lowest {@link #priority}; these items might not be shown to the user except under special
641 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500642 *
643 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500645 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 public static final int PRIORITY_MIN = -2;
647
648 /**
649 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
650 * show these items larger, or at a different position in notification lists, compared with
651 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500652 *
653 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500655 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 public static final int PRIORITY_HIGH = 1;
657
658 /**
659 * Highest {@link #priority}, for your application's most important items that require the
660 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 *
662 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public static final int PRIORITY_MAX = 2;
666
667 /**
668 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800669 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 * Priority is an indication of how much of the user's valuable attention should be consumed by
671 * this notification. Low-priority notifications may be hidden from the user in certain
672 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500673 * system will make a determination about how to interpret this priority when presenting
674 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400675 *
676 * <p>
677 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
678 * as a heads-up notification.
679 * </p>
680 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500681 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500682 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700683 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500684 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500685 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800686
Dan Sandler26e81cf2014-05-06 10:01:27 -0400687 /**
688 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
689 * to be applied by the standard Style templates when presenting this notification.
690 *
691 * The current template design constructs a colorful header image by overlaying the
692 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
693 * ignored.
694 */
Tor Norbye80756e32015-03-02 09:39:27 -0800695 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400696 public int color = COLOR_DEFAULT;
697
698 /**
699 * Special value of {@link #color} telling the system not to decorate this notification with
700 * any special color but instead use default colors when presenting this notification.
701 */
Tor Norbye80756e32015-03-02 09:39:27 -0800702 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400703 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600704
705 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800706 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800707 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800708 */
709 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800710 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800711
712 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700713 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
714 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600715 * lockscreen).
716 *
717 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
718 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
719 * shown in all situations, but the contents are only available if the device is unlocked for
720 * the appropriate user.
721 *
722 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
723 * can be read even in an "insecure" context (that is, above a secure lockscreen).
724 * To modify the public version of this notification—for example, to redact some portions—see
725 * {@link Builder#setPublicVersion(Notification)}.
726 *
727 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
728 * and ticker until the user has bypassed the lockscreen.
729 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600730 public @Visibility int visibility;
731
732 /** @hide */
733 @IntDef(prefix = { "VISIBILITY_" }, value = {
734 VISIBILITY_PUBLIC,
735 VISIBILITY_PRIVATE,
736 VISIBILITY_SECRET,
737 })
738 @Retention(RetentionPolicy.SOURCE)
739 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600740
Griff Hazenfc3922d2014-08-20 11:56:44 -0700741 /**
742 * Notification visibility: Show this notification in its entirety on all lockscreens.
743 *
744 * {@see #visibility}
745 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600746 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700747
748 /**
749 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
750 * private information on secure lockscreens.
751 *
752 * {@see #visibility}
753 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600754 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700755
756 /**
757 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
758 *
759 * {@see #visibility}
760 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600761 public static final int VISIBILITY_SECRET = -1;
762
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500763 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400764 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500765 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400766 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500767
768 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700769 * Notification category: map turn-by-turn navigation.
770 */
771 public static final String CATEGORY_NAVIGATION = "navigation";
772
773 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400776 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500777
778 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400779 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500780 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400781 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500782
783 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400786 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500787
788 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400791 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500792
793 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500795 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400796 public static final String CATEGORY_ALARM = "alarm";
797
798 /**
799 * Notification category: progress of a long-running background operation.
800 */
801 public static final String CATEGORY_PROGRESS = "progress";
802
803 /**
804 * Notification category: social network or sharing update.
805 */
806 public static final String CATEGORY_SOCIAL = "social";
807
808 /**
809 * Notification category: error in background operation or authentication status.
810 */
811 public static final String CATEGORY_ERROR = "err";
812
813 /**
814 * Notification category: media transport control for playback.
815 */
816 public static final String CATEGORY_TRANSPORT = "transport";
817
818 /**
819 * Notification category: system or device status update. Reserved for system use.
820 */
821 public static final String CATEGORY_SYSTEM = "sys";
822
823 /**
824 * Notification category: indication of running background service.
825 */
826 public static final String CATEGORY_SERVICE = "service";
827
828 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400829 * Notification category: a specific, timely recommendation for a single thing.
830 * For example, a news app might want to recommend a news story it believes the user will
831 * want to read next.
832 */
833 public static final String CATEGORY_RECOMMENDATION = "recommendation";
834
835 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400836 * Notification category: ongoing information about device or contextual status.
837 */
838 public static final String CATEGORY_STATUS = "status";
839
840 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400841 * Notification category: user-scheduled reminder.
842 */
843 public static final String CATEGORY_REMINDER = "reminder";
844
845 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700846 * Notification category: extreme car emergencies.
847 * @hide
848 */
849 @SystemApi
850 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
851
852 /**
853 * Notification category: car warnings.
854 * @hide
855 */
856 @SystemApi
857 public static final String CATEGORY_CAR_WARNING = "car_warning";
858
859 /**
860 * Notification category: general car system information.
861 * @hide
862 */
863 @SystemApi
864 public static final String CATEGORY_CAR_INFORMATION = "car_information";
865
866 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400867 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
868 * that best describes this Notification. May be used by the system for ranking and filtering.
869 */
870 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500871
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100872 @UnsupportedAppUsage
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700873 private String mGroupKey;
874
875 /**
876 * Get the key used to group this notification into a cluster or stack
877 * with other notifications on devices which support such rendering.
878 */
879 public String getGroup() {
880 return mGroupKey;
881 }
882
883 private String mSortKey;
884
885 /**
886 * Get a sort key that orders this notification among other notifications from the
887 * same package. This can be useful if an external sort was already applied and an app
888 * would like to preserve this. Notifications will be sorted lexicographically using this
889 * value, although providing different priorities in addition to providing sort key may
890 * cause this value to be ignored.
891 *
892 * <p>This sort key can also be used to order members of a notification group. See
893 * {@link Builder#setGroup}.
894 *
895 * @see String#compareTo(String)
896 */
897 public String getSortKey() {
898 return mSortKey;
899 }
900
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500901 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400902 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400903 * <p>
904 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
905 * APIs, and are intended to be used by
906 * {@link android.service.notification.NotificationListenerService} implementations to extract
907 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500908 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400909 public Bundle extras = new Bundle();
910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700912 * All pending intents in the notification as the system needs to be able to access them but
913 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700914 * custom parcelable objects.
915 *
916 * @hide
917 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100918 @UnsupportedAppUsage
Felipe Lemedd85da62016-06-28 11:29:54 -0700919 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700920
921 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700922 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
923 * pending intents inside of it, so only those will get the behavior.
924 *
925 * @hide
926 */
Adrian Roosfb921842017-10-26 14:49:56 +0200927 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700928
929 /**
930 * Must be set by a process to start associating tokens with Notification objects
931 * coming in to it. This is set by NotificationManagerService.
932 *
933 * @hide
934 */
935 static public IBinder processWhitelistToken;
936
937 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400938 * {@link #extras} key: this is the title of the notification,
939 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
940 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500941 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400942
943 /**
944 * {@link #extras} key: this is the title of the notification when shown in expanded form,
945 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
946 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400947 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948
949 /**
950 * {@link #extras} key: this is the main text payload, as supplied to
951 * {@link Builder#setContentText(CharSequence)}.
952 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500953 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954
955 /**
956 * {@link #extras} key: this is a third line of text, as supplied to
957 * {@link Builder#setSubText(CharSequence)}.
958 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400959 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400960
961 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800962 * {@link #extras} key: this is the remote input history, as supplied to
963 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700964 *
965 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
966 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
967 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
968 * notifications once the other party has responded).
969 *
970 * The extra with this key is of type CharSequence[] and contains the most recent entry at
971 * the 0 index, the second most recent at the 1 index, etc.
972 *
973 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800974 */
975 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
976
977 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100978 * {@link #extras} key: boolean as supplied to
979 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
980 *
981 * If set to true, then the view displaying the remote input history from
982 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
983 *
984 * @see Builder#setShowRemoteInputSpinner(boolean)
985 * @hide
986 */
987 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
988
989 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100990 * {@link #extras} key: boolean as supplied to
991 * {@link Builder#setHideSmartReplies(boolean)}.
992 *
993 * If set to true, then any smart reply buttons will be hidden.
994 *
995 * @see Builder#setHideSmartReplies(boolean)
996 * @hide
997 */
998 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
999
1000 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001 * {@link #extras} key: this is a small piece of additional text as supplied to
1002 * {@link Builder#setContentInfo(CharSequence)}.
1003 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001004 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001005
1006 /**
1007 * {@link #extras} key: this is a line of summary information intended to be shown
1008 * alongside expanded notifications, as supplied to (e.g.)
1009 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1010 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001011 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001012
1013 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001014 * {@link #extras} key: this is the longer text shown in the big form of a
1015 * {@link BigTextStyle} notification, as supplied to
1016 * {@link BigTextStyle#bigText(CharSequence)}.
1017 */
1018 public static final String EXTRA_BIG_TEXT = "android.bigText";
1019
1020 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001021 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1022 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001023 *
1024 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001026 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001027 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001028
1029 /**
1030 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1031 * notification payload, as
1032 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001033 *
1034 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001035 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001036 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001037 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001038
1039 /**
1040 * {@link #extras} key: this is a bitmap to be used instead of the one from
1041 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1042 * shown in its expanded form, as supplied to
1043 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1044 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001045 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046
1047 /**
1048 * {@link #extras} key: this is the progress value supplied to
1049 * {@link Builder#setProgress(int, int, boolean)}.
1050 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001051 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052
1053 /**
1054 * {@link #extras} key: this is the maximum value supplied to
1055 * {@link Builder#setProgress(int, int, boolean)}.
1056 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001057 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001058
1059 /**
1060 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1061 * {@link Builder#setProgress(int, int, boolean)}.
1062 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001063 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001064
1065 /**
1066 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1067 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1068 * {@link Builder#setUsesChronometer(boolean)}.
1069 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001070 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001071
1072 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001073 * {@link #extras} key: whether the chronometer set on the notification should count down
1074 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001075 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001076 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001077 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001078
1079 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001080 * {@link #extras} key: whether {@link #when} should be shown,
1081 * as supplied to {@link Builder#setShowWhen(boolean)}.
1082 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001083 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001084
1085 /**
1086 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1087 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1088 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001089 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001090
1091 /**
1092 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1093 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1094 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001095 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001096
1097 /**
1098 * {@link #extras} key: A string representing the name of the specific
1099 * {@link android.app.Notification.Style} used to create this notification.
1100 */
Chris Wren91ad5632013-06-05 15:05:57 -04001101 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001102
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001103 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001104 * {@link #extras} key: A String array containing the people that this notification relates to,
1105 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001106 *
1107 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001108 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001109 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001110
1111 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001112 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1113 * this notification relates to.
1114 */
1115 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1116
1117 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001118 * Allow certain system-generated notifications to appear before the device is provisioned.
1119 * Only available to notifications coming from the android package.
1120 * @hide
1121 */
Maurice Lam96c10032017-03-29 15:42:38 -07001122 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001123 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001124 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1125
1126 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001127 * {@link #extras} key: A
1128 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001129 * in the background when the notification is selected. Used on television platforms.
1130 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001131 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001132 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001133 */
1134 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1135
1136 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001137 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001138 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001139 * {@link android.app.Notification.MediaStyle} notification.
1140 */
1141 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1142
1143 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001144 * {@link #extras} key: the indices of actions to be shown in the compact view,
1145 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1146 */
1147 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1148
Christoph Studer943aa672014-08-03 20:31:16 +02001149 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001150 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1151 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001152 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1153 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001154 *
1155 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001156 */
1157 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1158
1159 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001160 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1161 * direct replies
1162 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1163 * {@link Person}
1164 */
1165 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1166
1167 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001168 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001169 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001170 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001171 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001172
1173 /**
1174 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1175 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001176 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1177 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001178 */
1179 public static final String EXTRA_MESSAGES = "android.messages";
1180
1181 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001182 * {@link #extras} key: an array of
1183 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1184 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1185 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1186 * array of bundles.
1187 */
1188 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1189
1190 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001191 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1192 * represents a group conversation.
1193 */
1194 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1195
1196 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001197 * {@link #extras} key: whether the notification should be colorized as
1198 * supplied to {@link Builder#setColorized(boolean)}}.
1199 */
1200 public static final String EXTRA_COLORIZED = "android.colorized";
1201
1202 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001203 * @hide
1204 */
1205 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1206
Selim Cinek247fa012016-02-18 09:50:48 -08001207 /**
1208 * @hide
1209 */
1210 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1211
Shane Brennan472a3b32016-12-12 15:28:10 -08001212 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001213 * @hide
1214 */
1215 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1216
1217 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001218 * {@link #extras} key: the audio contents of this notification.
1219 *
1220 * This is for use when rendering the notification on an audio-focused interface;
1221 * the audio contents are a complete sound sample that contains the contents/body of the
1222 * notification. This may be used in substitute of a Text-to-Speech reading of the
1223 * notification. For example if the notification represents a voice message this should point
1224 * to the audio of that message.
1225 *
1226 * The data stored under this key should be a String representation of a Uri that contains the
1227 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1228 *
1229 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1230 * has a field for holding data URI. That field can be used for audio.
1231 * See {@code Message#setData}.
1232 *
1233 * Example usage:
1234 * <pre>
1235 * {@code
1236 * Notification.Builder myBuilder = (build your Notification as normal);
1237 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1238 * }
1239 * </pre>
1240 */
1241 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1242
Dan Sandler80eaa592016-04-14 23:34:54 -04001243 /** @hide */
1244 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001245 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001246 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1247
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001248 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001249 * This is set on the notifications shown by system_server about apps running foreground
1250 * services. It indicates that the notification should be shown
1251 * only if any of the given apps do not already have a properly tagged
1252 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1253 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001254 * @hide
1255 */
1256 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1257
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001258 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001259 private Icon mSmallIcon;
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001260 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04001261 private Icon mLargeIcon;
1262
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001263 @UnsupportedAppUsage
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001264 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001265 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001266
Julia Reynolds13d898c2017-02-02 12:22:05 -05001267 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001268 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001269
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001270 /** @hide */
1271 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1272 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1273 })
1274 @Retention(RetentionPolicy.SOURCE)
1275 public @interface GroupAlertBehavior {}
1276
1277 /**
1278 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1279 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1280 * notification will not be muted when it is in a group.
1281 */
1282 public static final int GROUP_ALERT_ALL = 0;
1283
1284 /**
1285 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1286 * notification in a group should be silenced (no sound or vibration) even if they are posted
1287 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001288 * mute this notification if this notification is a group child. This must be applied to all
1289 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001290 *
1291 * <p> For example, you might want to use this constant if you post a number of children
1292 * notifications at once (say, after a periodic sync), and only need to notify the user
1293 * audibly once.
1294 */
1295 public static final int GROUP_ALERT_SUMMARY = 1;
1296
1297 /**
1298 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1299 * notification in a group should be silenced (no sound or vibration) even if they are
1300 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1301 * to mute this notification if this notification is a group summary.
1302 *
1303 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001304 * in your group have content and the summary is only used to visually group notifications
1305 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001306 */
1307 public static final int GROUP_ALERT_CHILDREN = 2;
1308
Julia Reynolds2f431e22017-06-07 14:12:09 +00001309 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001310
Julia Reynolds13d898c2017-02-02 12:22:05 -05001311 /**
1312 * If this notification is being shown as a badge, always show as a number.
1313 */
1314 public static final int BADGE_ICON_NONE = 0;
1315
1316 /**
1317 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1318 * represent this notification.
1319 */
1320 public static final int BADGE_ICON_SMALL = 1;
1321
1322 /**
1323 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1324 * represent this notification.
1325 */
1326 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001327 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001328
Chris Wren51c75102013-07-16 20:49:17 -04001329 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001330 * Structure to encapsulate a named action that can be shown as part of this notification.
1331 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1332 * selected by the user.
1333 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001334 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1335 * or {@link Notification.Builder#addAction(Notification.Action)}
1336 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001337 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001338 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001339 /**
1340 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1341 * {@link RemoteInput}s.
1342 *
1343 * This is intended for {@link RemoteInput}s that only accept data, meaning
1344 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1345 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1346 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1347 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1348 *
1349 * You can test if a RemoteInput matches these constraints using
1350 * {@link RemoteInput#isDataOnly}.
1351 */
1352 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1353
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001354 /**
1355 * {@link }: No semantic action defined.
1356 */
1357 public static final int SEMANTIC_ACTION_NONE = 0;
1358
1359 /**
1360 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1361 * may be appropriate.
1362 */
1363 public static final int SEMANTIC_ACTION_REPLY = 1;
1364
1365 /**
1366 * {@code SemanticAction}: Mark content as read.
1367 */
1368 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1369
1370 /**
1371 * {@code SemanticAction}: Mark content as unread.
1372 */
1373 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1374
1375 /**
1376 * {@code SemanticAction}: Delete the content associated with the notification. This
1377 * could mean deleting an email, message, etc.
1378 */
1379 public static final int SEMANTIC_ACTION_DELETE = 4;
1380
1381 /**
1382 * {@code SemanticAction}: Archive the content associated with the notification. This
1383 * could mean archiving an email, message, etc.
1384 */
1385 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1386
1387 /**
1388 * {@code SemanticAction}: Mute the content associated with the notification. This could
1389 * mean silencing a conversation or currently playing media.
1390 */
1391 public static final int SEMANTIC_ACTION_MUTE = 6;
1392
1393 /**
1394 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1395 * mean un-silencing a conversation or currently playing media.
1396 */
1397 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1398
1399 /**
1400 * {@code SemanticAction}: Mark content with a thumbs up.
1401 */
1402 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1403
1404 /**
1405 * {@code SemanticAction}: Mark content with a thumbs down.
1406 */
1407 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1408
Kodlee Yincda5b092018-02-15 15:34:53 -08001409 /**
1410 * {@code SemanticAction}: Call a contact, group, etc.
1411 */
1412 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001413
Griff Hazen959591e2014-05-15 22:26:18 -07001414 private final Bundle mExtras;
Mathew Inwood45d2c252018-09-14 12:35:36 +01001415 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dan Sandler86647982015-05-13 23:41:13 -04001416 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001417 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001418 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001419 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001420
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001421 /**
1422 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001423 *
1424 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001425 */
Dan Sandler86647982015-05-13 23:41:13 -04001426 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001427 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001428
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001429 /**
1430 * Title of the action.
1431 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001432 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001433
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001434 /**
1435 * Intent to send when the user invokes this action. May be null, in which case the action
1436 * may be rendered in a disabled presentation by the system UI.
1437 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001438 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001439
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001440 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001441 if (in.readInt() != 0) {
1442 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001443 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1444 icon = mIcon.getResId();
1445 }
Dan Sandler86647982015-05-13 23:41:13 -04001446 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001447 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1448 if (in.readInt() == 1) {
1449 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1450 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001451 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001452 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001453 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001454 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001455 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001456
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001457 /**
Dan Sandler86647982015-05-13 23:41:13 -04001458 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001459 */
Dan Sandler86647982015-05-13 23:41:13 -04001460 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001461 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001462 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1463 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001464 }
1465
Adrian Roos7af53622016-10-12 13:44:05 -07001466 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001467 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001468 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1469 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001470 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001471 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1472 this.icon = icon.getResId();
1473 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001474 this.title = title;
1475 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001476 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001477 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001478 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001479 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001480 }
1481
1482 /**
Dan Sandler86647982015-05-13 23:41:13 -04001483 * Return an icon representing the action.
1484 */
1485 public Icon getIcon() {
1486 if (mIcon == null && icon != 0) {
1487 // you snuck an icon in here without using the builder; let's try to keep it
1488 mIcon = Icon.createWithResource("", icon);
1489 }
1490 return mIcon;
1491 }
1492
1493 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001494 * Get additional metadata carried around with this Action.
1495 */
1496 public Bundle getExtras() {
1497 return mExtras;
1498 }
1499
1500 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001501 * Return whether the platform should automatically generate possible replies for this
1502 * {@link Action}
1503 */
1504 public boolean getAllowGeneratedReplies() {
1505 return mAllowGeneratedReplies;
1506 }
1507
1508 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001509 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001510 * May return null if no remote inputs were added. Only returns inputs which accept
1511 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001512 */
1513 public RemoteInput[] getRemoteInputs() {
1514 return mRemoteInputs;
1515 }
1516
1517 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001518 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1519 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1520 * (eg. reply, mark as read, delete, etc).
1521 */
1522 public @SemanticAction int getSemanticAction() {
1523 return mSemanticAction;
1524 }
1525
1526 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001527 * Get the list of inputs to be collected from the user that ONLY accept data when this
1528 * action is sent. These remote inputs are guaranteed to return true on a call to
1529 * {@link RemoteInput#isDataOnly}.
1530 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001531 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001532 *
1533 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1534 * of non-textual RemoteInputs do not access these remote inputs.
1535 */
1536 public RemoteInput[] getDataOnlyRemoteInputs() {
1537 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1538 }
1539
1540 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001541 * Builder class for {@link Action} objects.
1542 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001543 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001544 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001545 private final CharSequence mTitle;
1546 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001547 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001548 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001549 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001550 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001551
1552 /**
1553 * Construct a new builder for {@link Action} object.
1554 * @param icon icon to show for this action
1555 * @param title the title of the action
1556 * @param intent the {@link PendingIntent} to fire when users trigger this action
1557 */
Dan Sandler86647982015-05-13 23:41:13 -04001558 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001559 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001560 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001561 }
1562
1563 /**
1564 * Construct a new builder for {@link Action} object.
1565 * @param icon icon to show for this action
1566 * @param title the title of the action
1567 * @param intent the {@link PendingIntent} to fire when users trigger this action
1568 */
1569 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001570 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001571 }
1572
1573 /**
1574 * Construct a new builder for {@link Action} object using the fields from an
1575 * {@link Action}.
1576 * @param action the action to read fields from.
1577 */
1578 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001579 this(action.getIcon(), action.title, action.actionIntent,
1580 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001581 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001582 }
1583
Dan Sandler86647982015-05-13 23:41:13 -04001584 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001585 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1586 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001587 mIcon = icon;
1588 mTitle = title;
1589 mIntent = intent;
1590 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001591 if (remoteInputs != null) {
1592 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1593 Collections.addAll(mRemoteInputs, remoteInputs);
1594 }
Adrian Roos7af53622016-10-12 13:44:05 -07001595 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001596 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001597 }
1598
1599 /**
1600 * Merge additional metadata into this builder.
1601 *
1602 * <p>Values within the Bundle will replace existing extras values in this Builder.
1603 *
1604 * @see Notification.Action#extras
1605 */
1606 public Builder addExtras(Bundle extras) {
1607 if (extras != null) {
1608 mExtras.putAll(extras);
1609 }
1610 return this;
1611 }
1612
1613 /**
1614 * Get the metadata Bundle used by this Builder.
1615 *
1616 * <p>The returned Bundle is shared with this Builder.
1617 */
1618 public Bundle getExtras() {
1619 return mExtras;
1620 }
1621
1622 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001623 * Add an input to be collected from the user when this action is sent.
1624 * Response values can be retrieved from the fired intent by using the
1625 * {@link RemoteInput#getResultsFromIntent} function.
1626 * @param remoteInput a {@link RemoteInput} to add to the action
1627 * @return this object for method chaining
1628 */
1629 public Builder addRemoteInput(RemoteInput remoteInput) {
1630 if (mRemoteInputs == null) {
1631 mRemoteInputs = new ArrayList<RemoteInput>();
1632 }
1633 mRemoteInputs.add(remoteInput);
1634 return this;
1635 }
1636
1637 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001638 * Set whether the platform should automatically generate possible replies to add to
1639 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1640 * {@link RemoteInput}, this has no effect.
1641 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1642 * otherwise
1643 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001644 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001645 */
1646 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1647 mAllowGeneratedReplies = allowGeneratedReplies;
1648 return this;
1649 }
1650
1651 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001652 * Sets the {@code SemanticAction} for this {@link Action}. A
1653 * {@code SemanticAction} denotes what an {@link Action}'s
1654 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1655 * @param semanticAction a SemanticAction defined within {@link Action} with
1656 * {@code SEMANTIC_ACTION_} prefixes
1657 * @return this object for method chaining
1658 */
1659 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1660 mSemanticAction = semanticAction;
1661 return this;
1662 }
1663
1664 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001665 * Apply an extender to this action builder. Extenders may be used to add
1666 * metadata or change options on this builder.
1667 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001668 public Builder extend(Extender extender) {
1669 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001670 return this;
1671 }
1672
1673 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001674 * Combine all of the options that have been set and return a new {@link Action}
1675 * object.
1676 * @return the built action
1677 */
1678 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001679 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1680 RemoteInput[] previousDataInputs =
1681 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001682 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001683 for (RemoteInput input : previousDataInputs) {
1684 dataOnlyInputs.add(input);
1685 }
1686 }
1687 List<RemoteInput> textInputs = new ArrayList<>();
1688 if (mRemoteInputs != null) {
1689 for (RemoteInput input : mRemoteInputs) {
1690 if (input.isDataOnly()) {
1691 dataOnlyInputs.add(input);
1692 } else {
1693 textInputs.add(input);
1694 }
1695 }
1696 }
1697 if (!dataOnlyInputs.isEmpty()) {
1698 RemoteInput[] dataInputsArr =
1699 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1700 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1701 }
1702 RemoteInput[] textInputsArr = textInputs.isEmpty()
1703 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1704 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001705 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001706 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001707 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001708
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001709 @Override
1710 public Action clone() {
1711 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001712 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001713 title,
1714 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001715 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001716 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001717 getAllowGeneratedReplies(),
1718 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001720
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001721 @Override
1722 public int describeContents() {
1723 return 0;
1724 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001725
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001726 @Override
1727 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001728 final Icon ic = getIcon();
1729 if (ic != null) {
1730 out.writeInt(1);
1731 ic.writeToParcel(out, 0);
1732 } else {
1733 out.writeInt(0);
1734 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001735 TextUtils.writeToParcel(title, out, flags);
1736 if (actionIntent != null) {
1737 out.writeInt(1);
1738 actionIntent.writeToParcel(out, flags);
1739 } else {
1740 out.writeInt(0);
1741 }
Griff Hazen959591e2014-05-15 22:26:18 -07001742 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001743 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001744 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001745 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001746 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001747
Griff Hazen959591e2014-05-15 22:26:18 -07001748 public static final Parcelable.Creator<Action> CREATOR =
1749 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001750 public Action createFromParcel(Parcel in) {
1751 return new Action(in);
1752 }
1753 public Action[] newArray(int size) {
1754 return new Action[size];
1755 }
1756 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001757
1758 /**
1759 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1760 * metadata or change options on an action builder.
1761 */
1762 public interface Extender {
1763 /**
1764 * Apply this extender to a notification action builder.
1765 * @param builder the builder to be modified.
1766 * @return the build object for chaining.
1767 */
1768 public Builder extend(Builder builder);
1769 }
1770
1771 /**
1772 * Wearable extender for notification actions. To add extensions to an action,
1773 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1774 * the {@code WearableExtender()} constructor and apply it to a
1775 * {@link android.app.Notification.Action.Builder} using
1776 * {@link android.app.Notification.Action.Builder#extend}.
1777 *
1778 * <pre class="prettyprint">
1779 * Notification.Action action = new Notification.Action.Builder(
1780 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001781 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001782 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001783 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001784 */
1785 public static final class WearableExtender implements Extender {
1786 /** Notification action extra which contains wearable extensions */
1787 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1788
Pete Gastaf6781d2014-10-07 15:17:05 -04001789 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001790 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001791 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1792 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1793 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001794
1795 // Flags bitwise-ored to mFlags
1796 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001797 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001798 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001799
1800 // Default value for flags integer
1801 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1802
1803 private int mFlags = DEFAULT_FLAGS;
1804
Pete Gastaf6781d2014-10-07 15:17:05 -04001805 private CharSequence mInProgressLabel;
1806 private CharSequence mConfirmLabel;
1807 private CharSequence mCancelLabel;
1808
Griff Hazen61a9e862014-05-22 16:05:19 -07001809 /**
1810 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1811 * options.
1812 */
1813 public WearableExtender() {
1814 }
1815
1816 /**
1817 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1818 * wearable options present in an existing notification action.
1819 * @param action the notification action to inspect.
1820 */
1821 public WearableExtender(Action action) {
1822 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1823 if (wearableBundle != null) {
1824 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001825 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1826 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1827 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001828 }
1829 }
1830
1831 /**
1832 * Apply wearable extensions to a notification action that is being built. This is
1833 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1834 * method of {@link android.app.Notification.Action.Builder}.
1835 */
1836 @Override
1837 public Action.Builder extend(Action.Builder builder) {
1838 Bundle wearableBundle = new Bundle();
1839
1840 if (mFlags != DEFAULT_FLAGS) {
1841 wearableBundle.putInt(KEY_FLAGS, mFlags);
1842 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001843 if (mInProgressLabel != null) {
1844 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1845 }
1846 if (mConfirmLabel != null) {
1847 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1848 }
1849 if (mCancelLabel != null) {
1850 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1851 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001852
1853 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1854 return builder;
1855 }
1856
1857 @Override
1858 public WearableExtender clone() {
1859 WearableExtender that = new WearableExtender();
1860 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001861 that.mInProgressLabel = this.mInProgressLabel;
1862 that.mConfirmLabel = this.mConfirmLabel;
1863 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001864 return that;
1865 }
1866
1867 /**
1868 * Set whether this action is available when the wearable device is not connected to
1869 * a companion device. The user can still trigger this action when the wearable device is
1870 * offline, but a visual hint will indicate that the action may not be available.
1871 * Defaults to true.
1872 */
1873 public WearableExtender setAvailableOffline(boolean availableOffline) {
1874 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1875 return this;
1876 }
1877
1878 /**
1879 * Get whether this action is available when the wearable device is not connected to
1880 * a companion device. The user can still trigger this action when the wearable device is
1881 * offline, but a visual hint will indicate that the action may not be available.
1882 * Defaults to true.
1883 */
1884 public boolean isAvailableOffline() {
1885 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1886 }
1887
1888 private void setFlag(int mask, boolean value) {
1889 if (value) {
1890 mFlags |= mask;
1891 } else {
1892 mFlags &= ~mask;
1893 }
1894 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001895
1896 /**
1897 * Set a label to display while the wearable is preparing to automatically execute the
1898 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1899 *
1900 * @param label the label to display while the action is being prepared to execute
1901 * @return this object for method chaining
1902 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001903 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001904 public WearableExtender setInProgressLabel(CharSequence label) {
1905 mInProgressLabel = label;
1906 return this;
1907 }
1908
1909 /**
1910 * Get the label to display while the wearable is preparing to automatically execute
1911 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1912 *
1913 * @return the label to display while the action is being prepared to execute
1914 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001915 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001916 public CharSequence getInProgressLabel() {
1917 return mInProgressLabel;
1918 }
1919
1920 /**
1921 * Set a label to display to confirm that the action should be executed.
1922 * This is usually an imperative verb like "Send".
1923 *
1924 * @param label the label to confirm the action should be executed
1925 * @return this object for method chaining
1926 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001927 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001928 public WearableExtender setConfirmLabel(CharSequence label) {
1929 mConfirmLabel = label;
1930 return this;
1931 }
1932
1933 /**
1934 * Get the label to display to confirm that the action should be executed.
1935 * This is usually an imperative verb like "Send".
1936 *
1937 * @return the label to confirm the action should be executed
1938 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001939 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001940 public CharSequence getConfirmLabel() {
1941 return mConfirmLabel;
1942 }
1943
1944 /**
1945 * Set a label to display to cancel the action.
1946 * This is usually an imperative verb, like "Cancel".
1947 *
1948 * @param label the label to display to cancel the action
1949 * @return this object for method chaining
1950 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001951 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001952 public WearableExtender setCancelLabel(CharSequence label) {
1953 mCancelLabel = label;
1954 return this;
1955 }
1956
1957 /**
1958 * Get the label to display to cancel the action.
1959 * This is usually an imperative verb like "Cancel".
1960 *
1961 * @return the label to display to cancel the action
1962 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001963 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001964 public CharSequence getCancelLabel() {
1965 return mCancelLabel;
1966 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001967
1968 /**
1969 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1970 * platform that it can generate the appropriate transitions.
1971 * @param hintLaunchesActivity {@code true} if the content intent will launch
1972 * an activity and transitions should be generated, false otherwise.
1973 * @return this object for method chaining
1974 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001975 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001976 boolean hintLaunchesActivity) {
1977 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1978 return this;
1979 }
1980
1981 /**
1982 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1983 * platform that it can generate the appropriate transitions
1984 * @return {@code true} if the content intent will launch an activity and transitions
1985 * should be generated, false otherwise. The default value is {@code false} if this was
1986 * never set.
1987 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001988 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001989 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1990 }
Alex Hills9f087612016-06-07 09:08:59 -04001991
1992 /**
1993 * Set a hint that this Action should be displayed inline.
1994 *
1995 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1996 * otherwise
1997 * @return this object for method chaining
1998 */
1999 public WearableExtender setHintDisplayActionInline(
2000 boolean hintDisplayInline) {
2001 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2002 return this;
2003 }
2004
2005 /**
2006 * Get a hint that this Action should be displayed inline.
2007 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002008 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002009 * otherwise. The default value is {@code false} if this was never set.
2010 */
2011 public boolean getHintDisplayActionInline() {
2012 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2013 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002014 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002015
2016 /**
2017 * Provides meaning to an {@link Action} that hints at what the associated
2018 * {@link PendingIntent} will do. For example, an {@link Action} with a
2019 * {@link PendingIntent} that replies to a text message notification may have the
2020 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2021 *
2022 * @hide
2023 */
2024 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2025 SEMANTIC_ACTION_NONE,
2026 SEMANTIC_ACTION_REPLY,
2027 SEMANTIC_ACTION_MARK_AS_READ,
2028 SEMANTIC_ACTION_MARK_AS_UNREAD,
2029 SEMANTIC_ACTION_DELETE,
2030 SEMANTIC_ACTION_ARCHIVE,
2031 SEMANTIC_ACTION_MUTE,
2032 SEMANTIC_ACTION_UNMUTE,
2033 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002034 SEMANTIC_ACTION_THUMBS_DOWN,
2035 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002036 })
2037 @Retention(RetentionPolicy.SOURCE)
2038 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002039 }
2040
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002041 /**
2042 * Array of all {@link Action} structures attached to this notification by
2043 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2044 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2045 * interface for invoking actions.
2046 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002047 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002048
2049 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002050 * Replacement version of this notification whose content will be shown
2051 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2052 * and {@link #VISIBILITY_PUBLIC}.
2053 */
2054 public Notification publicVersion;
2055
2056 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002057 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002058 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 */
2060 public Notification()
2061 {
2062 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002063 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002064 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066
2067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 * @hide
2069 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01002070 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 public Notification(Context context, int icon, CharSequence tickerText, long when,
2072 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2073 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002074 new Builder(context)
2075 .setWhen(when)
2076 .setSmallIcon(icon)
2077 .setTicker(tickerText)
2078 .setContentTitle(contentTitle)
2079 .setContentText(contentText)
2080 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2081 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083
2084 /**
2085 * Constructs a Notification object with the information needed to
2086 * have a status bar icon without the standard expanded view.
2087 *
2088 * @param icon The resource id of the icon to put in the status bar.
2089 * @param tickerText The text that flows by in the status bar when the notification first
2090 * activates.
2091 * @param when The time to show in the time field. In the System.currentTimeMillis
2092 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002093 *
2094 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002096 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 public Notification(int icon, CharSequence tickerText, long when)
2098 {
2099 this.icon = icon;
2100 this.tickerText = tickerText;
2101 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002102 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
2104
2105 /**
2106 * Unflatten the notification from a parcel.
2107 */
Svet Ganovddb94882016-06-23 19:55:24 -07002108 @SuppressWarnings("unchecked")
2109 public Notification(Parcel parcel) {
2110 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2111 // intents in extras are always written as the last entry.
2112 readFromParcelImpl(parcel);
2113 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002114 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002115 }
2116
2117 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 {
2119 int version = parcel.readInt();
2120
Adrian Roosfb921842017-10-26 14:49:56 +02002121 mWhitelistToken = parcel.readStrongBinder();
2122 if (mWhitelistToken == null) {
2123 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002124 }
2125 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002126 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002129 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002130 if (parcel.readInt() != 0) {
2131 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002132 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2133 icon = mSmallIcon.getResId();
2134 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 number = parcel.readInt();
2137 if (parcel.readInt() != 0) {
2138 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2139 }
2140 if (parcel.readInt() != 0) {
2141 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2142 }
2143 if (parcel.readInt() != 0) {
2144 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2145 }
2146 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002147 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002148 }
2149 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2151 }
Joe Onorato561d3852010-11-20 18:09:34 -08002152 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002153 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 defaults = parcel.readInt();
2156 flags = parcel.readInt();
2157 if (parcel.readInt() != 0) {
2158 sound = Uri.CREATOR.createFromParcel(parcel);
2159 }
2160
2161 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002162 if (parcel.readInt() != 0) {
2163 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 vibrate = parcel.createLongArray();
2166 ledARGB = parcel.readInt();
2167 ledOnMS = parcel.readInt();
2168 ledOffMS = parcel.readInt();
2169 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002170
2171 if (parcel.readInt() != 0) {
2172 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2173 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002174
2175 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002176
John Spurlockfd7f1e02014-03-18 16:41:57 -04002177 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002178
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002179 mGroupKey = parcel.readString();
2180
2181 mSortKey = parcel.readString();
2182
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002183 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002184 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002185
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002186 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2187
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002188 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002189 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2190 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002191
Chris Wren8fd39ec2014-02-27 17:43:26 -05002192 if (parcel.readInt() != 0) {
2193 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2194 }
2195
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002196 visibility = parcel.readInt();
2197
2198 if (parcel.readInt() != 0) {
2199 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2200 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002201
2202 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002203
2204 if (parcel.readInt() != 0) {
2205 mChannelId = parcel.readString();
2206 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002207 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002208
2209 if (parcel.readInt() != 0) {
2210 mShortcutId = parcel.readString();
2211 }
2212
2213 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002214
2215 if (parcel.readInt() != 0) {
2216 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2217 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002218
2219 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
2221
Andy Stadler110988c2010-12-03 14:29:16 -08002222 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002223 public Notification clone() {
2224 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002225 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002226 return that;
2227 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002228
Daniel Sandler1a497d32013-04-18 14:52:45 -04002229 /**
2230 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2231 * of this into that.
2232 * @hide
2233 */
2234 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002235 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002236 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002237 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002238 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002239 that.number = this.number;
2240
2241 // PendingIntents are global, so there's no reason (or way) to clone them.
2242 that.contentIntent = this.contentIntent;
2243 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002244 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002245
2246 if (this.tickerText != null) {
2247 that.tickerText = this.tickerText.toString();
2248 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002249 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002250 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002251 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002252 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002253 that.contentView = this.contentView.clone();
2254 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002255 if (heavy && this.mLargeIcon != null) {
2256 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002257 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002258 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002259 that.sound = this.sound; // android.net.Uri is immutable
2260 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002261 if (this.audioAttributes != null) {
2262 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2263 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002264
2265 final long[] vibrate = this.vibrate;
2266 if (vibrate != null) {
2267 final int N = vibrate.length;
2268 final long[] vib = that.vibrate = new long[N];
2269 System.arraycopy(vibrate, 0, vib, 0, N);
2270 }
2271
2272 that.ledARGB = this.ledARGB;
2273 that.ledOnMS = this.ledOnMS;
2274 that.ledOffMS = this.ledOffMS;
2275 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276
Joe Onorato18e69df2010-05-17 22:26:12 -07002277 that.flags = this.flags;
2278
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002280
John Spurlockfd7f1e02014-03-18 16:41:57 -04002281 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002282
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002283 that.mGroupKey = this.mGroupKey;
2284
2285 that.mSortKey = this.mSortKey;
2286
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002287 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002288 try {
2289 that.extras = new Bundle(this.extras);
2290 // will unparcel
2291 that.extras.size();
2292 } catch (BadParcelableException e) {
2293 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2294 that.extras = null;
2295 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002296 }
2297
Felipe Lemedd85da62016-06-28 11:29:54 -07002298 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2299 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002300 }
2301
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002302 if (this.actions != null) {
2303 that.actions = new Action[this.actions.length];
2304 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002305 if ( this.actions[i] != null) {
2306 that.actions[i] = this.actions[i].clone();
2307 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002308 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002309 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002310
Daniel Sandler1a497d32013-04-18 14:52:45 -04002311 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002312 that.bigContentView = this.bigContentView.clone();
2313 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002314
Chris Wren8fd39ec2014-02-27 17:43:26 -05002315 if (heavy && this.headsUpContentView != null) {
2316 that.headsUpContentView = this.headsUpContentView.clone();
2317 }
2318
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002319 that.visibility = this.visibility;
2320
2321 if (this.publicVersion != null) {
2322 that.publicVersion = new Notification();
2323 this.publicVersion.cloneInto(that.publicVersion, heavy);
2324 }
2325
Dan Sandler26e81cf2014-05-06 10:01:27 -04002326 that.color = this.color;
2327
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002328 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002329 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002330 that.mShortcutId = this.mShortcutId;
2331 that.mBadgeIcon = this.mBadgeIcon;
2332 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002333 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002334
Daniel Sandler1a497d32013-04-18 14:52:45 -04002335 if (!heavy) {
2336 that.lightenPayload(); // will clean out extras
2337 }
2338 }
2339
2340 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002341 * Note all {@link Uri} that are referenced internally, with the expectation
2342 * that Uri permission grants will need to be issued to ensure the recipient
2343 * of this object is able to render its contents.
2344 *
2345 * @hide
2346 */
2347 public void visitUris(@NonNull Consumer<Uri> visitor) {
2348 visitor.accept(sound);
2349
2350 if (tickerView != null) tickerView.visitUris(visitor);
2351 if (contentView != null) contentView.visitUris(visitor);
2352 if (bigContentView != null) bigContentView.visitUris(visitor);
2353 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2354
2355 if (extras != null) {
2356 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
2357 visitor.accept(extras.getParcelable(EXTRA_BACKGROUND_IMAGE_URI));
2358 }
2359
2360 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2361 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2362 if (!ArrayUtils.isEmpty(messages)) {
2363 for (MessagingStyle.Message message : MessagingStyle.Message
2364 .getMessagesFromBundleArray(messages)) {
2365 visitor.accept(message.getDataUri());
2366 }
2367 }
2368
2369 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2370 if (!ArrayUtils.isEmpty(historic)) {
2371 for (MessagingStyle.Message message : MessagingStyle.Message
2372 .getMessagesFromBundleArray(historic)) {
2373 visitor.accept(message.getDataUri());
2374 }
2375 }
2376 }
2377 }
2378
2379 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002380 * Removes heavyweight parts of the Notification object for archival or for sending to
2381 * listeners when the full contents are not necessary.
2382 * @hide
2383 */
2384 public final void lightenPayload() {
2385 tickerView = null;
2386 contentView = null;
2387 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002388 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002389 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002390 if (extras != null && !extras.isEmpty()) {
2391 final Set<String> keyset = extras.keySet();
2392 final int N = keyset.size();
2393 final String[] keys = keyset.toArray(new String[N]);
2394 for (int i=0; i<N; i++) {
2395 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002396 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2397 continue;
2398 }
Dan Sandler50128532015-12-08 15:42:41 -05002399 final Object obj = extras.get(key);
2400 if (obj != null &&
2401 ( obj instanceof Parcelable
2402 || obj instanceof Parcelable[]
2403 || obj instanceof SparseArray
2404 || obj instanceof ArrayList)) {
2405 extras.remove(key);
2406 }
2407 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002408 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002409 }
2410
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002411 /**
2412 * Make sure this CharSequence is safe to put into a bundle, which basically
2413 * means it had better not be some custom Parcelable implementation.
2414 * @hide
2415 */
2416 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002417 if (cs == null) return cs;
2418 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2419 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2420 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002421 if (cs instanceof Parcelable) {
2422 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2423 + " instance is a custom Parcelable and not allowed in Notification");
2424 return cs.toString();
2425 }
Selim Cinek60a54252016-02-26 17:03:25 -08002426 return removeTextSizeSpans(cs);
2427 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002428
Selim Cinek60a54252016-02-26 17:03:25 -08002429 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2430 if (charSequence instanceof Spanned) {
2431 Spanned ss = (Spanned) charSequence;
2432 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2433 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2434 for (Object span : spans) {
2435 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002436 if (resultSpan instanceof CharacterStyle) {
2437 resultSpan = ((CharacterStyle) span).getUnderlying();
2438 }
2439 if (resultSpan instanceof TextAppearanceSpan) {
2440 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002441 resultSpan = new TextAppearanceSpan(
2442 originalSpan.getFamily(),
2443 originalSpan.getTextStyle(),
2444 -1,
2445 originalSpan.getTextColor(),
2446 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002447 } else if (resultSpan instanceof RelativeSizeSpan
2448 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002449 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002450 } else {
2451 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002452 }
2453 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2454 ss.getSpanFlags(span));
2455 }
2456 return builder;
2457 }
2458 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002459 }
2460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 public int describeContents() {
2462 return 0;
2463 }
2464
2465 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002466 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 */
Svet Ganovddb94882016-06-23 19:55:24 -07002468 public void writeToParcel(Parcel parcel, int flags) {
2469 // We need to mark all pending intents getting into the notification
2470 // system as being put there to later allow the notification ranker
2471 // to launch them and by doing so add the app to the battery saver white
2472 // list for a short period of time. The problem is that the system
2473 // cannot look into the extras as there may be parcelables there that
2474 // the platform does not know how to handle. To go around that we have
2475 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002476 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002477 if (collectPendingIntents) {
2478 PendingIntent.setOnMarshaledListener(
2479 (PendingIntent intent, Parcel out, int outFlags) -> {
2480 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002481 if (allPendingIntents == null) {
2482 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002483 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002484 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002485 }
2486 });
2487 }
2488 try {
2489 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002490 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002491 writeToParcelImpl(parcel, flags);
2492 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002493 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002494 } finally {
2495 if (collectPendingIntents) {
2496 PendingIntent.setOnMarshaledListener(null);
2497 }
2498 }
2499 }
2500
2501 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 parcel.writeInt(1);
2503
Adrian Roosfb921842017-10-26 14:49:56 +02002504 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002506 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002507 if (mSmallIcon == null && icon != 0) {
2508 // you snuck an icon in here without using the builder; let's try to keep it
2509 mSmallIcon = Icon.createWithResource("", icon);
2510 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002511 if (mSmallIcon != null) {
2512 parcel.writeInt(1);
2513 mSmallIcon.writeToParcel(parcel, 0);
2514 } else {
2515 parcel.writeInt(0);
2516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 parcel.writeInt(number);
2518 if (contentIntent != null) {
2519 parcel.writeInt(1);
2520 contentIntent.writeToParcel(parcel, 0);
2521 } else {
2522 parcel.writeInt(0);
2523 }
2524 if (deleteIntent != null) {
2525 parcel.writeInt(1);
2526 deleteIntent.writeToParcel(parcel, 0);
2527 } else {
2528 parcel.writeInt(0);
2529 }
2530 if (tickerText != null) {
2531 parcel.writeInt(1);
2532 TextUtils.writeToParcel(tickerText, parcel, flags);
2533 } else {
2534 parcel.writeInt(0);
2535 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002536 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002537 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002538 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002539 } else {
2540 parcel.writeInt(0);
2541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 if (contentView != null) {
2543 parcel.writeInt(1);
2544 contentView.writeToParcel(parcel, 0);
2545 } else {
2546 parcel.writeInt(0);
2547 }
Selim Cinek279fa862016-06-14 10:57:25 -07002548 if (mLargeIcon == null && largeIcon != null) {
2549 // you snuck an icon in here without using the builder; let's try to keep it
2550 mLargeIcon = Icon.createWithBitmap(largeIcon);
2551 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002552 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002553 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002554 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002555 } else {
2556 parcel.writeInt(0);
2557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558
2559 parcel.writeInt(defaults);
2560 parcel.writeInt(this.flags);
2561
2562 if (sound != null) {
2563 parcel.writeInt(1);
2564 sound.writeToParcel(parcel, 0);
2565 } else {
2566 parcel.writeInt(0);
2567 }
2568 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002569
2570 if (audioAttributes != null) {
2571 parcel.writeInt(1);
2572 audioAttributes.writeToParcel(parcel, 0);
2573 } else {
2574 parcel.writeInt(0);
2575 }
2576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 parcel.writeLongArray(vibrate);
2578 parcel.writeInt(ledARGB);
2579 parcel.writeInt(ledOnMS);
2580 parcel.writeInt(ledOffMS);
2581 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002582
2583 if (fullScreenIntent != null) {
2584 parcel.writeInt(1);
2585 fullScreenIntent.writeToParcel(parcel, 0);
2586 } else {
2587 parcel.writeInt(0);
2588 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002589
2590 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002591
John Spurlockfd7f1e02014-03-18 16:41:57 -04002592 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002593
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002594 parcel.writeString(mGroupKey);
2595
2596 parcel.writeString(mSortKey);
2597
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002598 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002599
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002600 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002601
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002602 if (bigContentView != null) {
2603 parcel.writeInt(1);
2604 bigContentView.writeToParcel(parcel, 0);
2605 } else {
2606 parcel.writeInt(0);
2607 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002608
Chris Wren8fd39ec2014-02-27 17:43:26 -05002609 if (headsUpContentView != null) {
2610 parcel.writeInt(1);
2611 headsUpContentView.writeToParcel(parcel, 0);
2612 } else {
2613 parcel.writeInt(0);
2614 }
2615
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002616 parcel.writeInt(visibility);
2617
2618 if (publicVersion != null) {
2619 parcel.writeInt(1);
2620 publicVersion.writeToParcel(parcel, 0);
2621 } else {
2622 parcel.writeInt(0);
2623 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002624
2625 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002626
2627 if (mChannelId != null) {
2628 parcel.writeInt(1);
2629 parcel.writeString(mChannelId);
2630 } else {
2631 parcel.writeInt(0);
2632 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002633 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002634
2635 if (mShortcutId != null) {
2636 parcel.writeInt(1);
2637 parcel.writeString(mShortcutId);
2638 } else {
2639 parcel.writeInt(0);
2640 }
2641
2642 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002643
2644 if (mSettingsText != null) {
2645 parcel.writeInt(1);
2646 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2647 } else {
2648 parcel.writeInt(0);
2649 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002650
2651 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002652
2653 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
2655
2656 /**
2657 * Parcelable.Creator that instantiates Notification objects
2658 */
2659 public static final Parcelable.Creator<Notification> CREATOR
2660 = new Parcelable.Creator<Notification>()
2661 {
2662 public Notification createFromParcel(Parcel parcel)
2663 {
2664 return new Notification(parcel);
2665 }
2666
2667 public Notification[] newArray(int size)
2668 {
2669 return new Notification[size];
2670 }
2671 };
2672
2673 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002674 * @hide
2675 */
2676 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2677 Notification.Action[] firstAs = first.actions;
2678 Notification.Action[] secondAs = second.actions;
2679 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2680 return true;
2681 }
2682 if (firstAs != null && secondAs != null) {
2683 if (firstAs.length != secondAs.length) {
2684 return true;
2685 }
2686 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002687 if (!Objects.equals(String.valueOf(firstAs[i].title),
2688 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002689 return true;
2690 }
2691 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2692 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2693 if (firstRs == null) {
2694 firstRs = new RemoteInput[0];
2695 }
2696 if (secondRs == null) {
2697 secondRs = new RemoteInput[0];
2698 }
2699 if (firstRs.length != secondRs.length) {
2700 return true;
2701 }
2702 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002703 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2704 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002705 return true;
2706 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002707 }
2708 }
2709 }
2710 return false;
2711 }
2712
2713 /**
2714 * @hide
2715 */
2716 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2717 if (first.getStyle() == null) {
2718 return second.getStyle() != null;
2719 }
2720 if (second.getStyle() == null) {
2721 return true;
2722 }
2723 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2724 }
2725
2726 /**
2727 * @hide
2728 */
2729 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002730 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2731 return true;
2732 }
2733
2734 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2735 return true;
2736 }
2737 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2738 return true;
2739 }
2740 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2741 return true;
2742 }
2743
2744 return false;
2745 }
2746
2747 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2748 if (first == null && second == null) {
2749 return false;
2750 }
2751 if (first == null && second != null || first != null && second == null) {
2752 return true;
2753 }
2754
2755 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2756 return true;
2757 }
2758
2759 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2760 return true;
2761 }
2762
2763 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002764 }
2765
2766 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002767 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2768 * <p>
2769 * For backwards compatibility {@code extras} holds some references to "real" member data such
2770 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2771 * fine as long as the object stays in one process.
2772 * <p>
2773 * However, once the notification goes into a parcel each reference gets marshalled separately,
2774 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2775 */
2776 private void fixDuplicateExtras() {
2777 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002778 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2779 }
2780 }
2781
2782 /**
2783 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2784 * separate object, replace it with the field's version to avoid holding duplicate copies.
2785 */
2786 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2787 if (original != null && extras.getParcelable(extraName) != null) {
2788 extras.putParcelable(extraName, original);
2789 }
2790 }
2791
2792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2794 * layout.
2795 *
2796 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2797 * in the view.</p>
2798 * @param context The context for your application / activity.
2799 * @param contentTitle The title that goes in the expanded entry.
2800 * @param contentText The text that goes in the expanded entry.
2801 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2802 * If this is an activity, it must include the
2803 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002804 * that you take care of task management as described in the
2805 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2806 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002807 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002808 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002809 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002811 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 public void setLatestEventInfo(Context context,
2813 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002814 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2815 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2816 new Throwable());
2817 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002818
Selim Cinek4ac6f602016-06-13 15:47:03 -07002819 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2820 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2821 }
2822
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002823 // ensure that any information already set directly is preserved
2824 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002825
2826 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002828 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
2830 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002831 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002833 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002834
2835 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 }
2837
Julia Reynoldsda303542015-11-23 14:00:20 -05002838 /**
2839 * @hide
2840 */
2841 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002842 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002843 }
2844
2845 /**
2846 * @hide
2847 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002848 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002849 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002850 }
2851
Yi Jin6b514142017-10-30 14:54:12 -07002852 /**
2853 * @hide
2854 */
2855 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2856 long token = proto.start(fieldId);
2857 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2858 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2859 proto.write(NotificationProto.FLAGS, this.flags);
2860 proto.write(NotificationProto.COLOR, this.color);
2861 proto.write(NotificationProto.CATEGORY, this.category);
2862 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2863 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2864 if (this.actions != null) {
2865 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2866 }
2867 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2868 proto.write(NotificationProto.VISIBILITY, this.visibility);
2869 }
2870 if (publicVersion != null) {
2871 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2872 }
2873 proto.end(token);
2874 }
2875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 @Override
2877 public String toString() {
2878 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002879 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002880 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002881 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002882 sb.append(priority);
2883 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002884 if (contentView != null) {
2885 sb.append(contentView.getPackage());
2886 sb.append("/0x");
2887 sb.append(Integer.toHexString(contentView.getLayoutId()));
2888 } else {
2889 sb.append("null");
2890 }
2891 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002892 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2893 sb.append("default");
2894 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 int N = this.vibrate.length-1;
2896 sb.append("[");
2897 for (int i=0; i<N; i++) {
2898 sb.append(this.vibrate[i]);
2899 sb.append(',');
2900 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002901 if (N != -1) {
2902 sb.append(this.vibrate[N]);
2903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 } else {
2906 sb.append("null");
2907 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002908 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002909 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002911 } else if (this.sound != null) {
2912 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } else {
2914 sb.append("null");
2915 }
Chris Wren365b6d32015-07-16 10:39:26 -04002916 if (this.tickerText != null) {
2917 sb.append(" tick");
2918 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002919 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002921 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002922 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002923 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002924 if (this.category != null) {
2925 sb.append(" category=");
2926 sb.append(this.category);
2927 }
2928 if (this.mGroupKey != null) {
2929 sb.append(" groupKey=");
2930 sb.append(this.mGroupKey);
2931 }
2932 if (this.mSortKey != null) {
2933 sb.append(" sortKey=");
2934 sb.append(this.mSortKey);
2935 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002936 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002937 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002938 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002939 }
2940 sb.append(" vis=");
2941 sb.append(visibilityToString(this.visibility));
2942 if (this.publicVersion != null) {
2943 sb.append(" publicVersion=");
2944 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002945 }
2946 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 return sb.toString();
2948 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002949
Dan Sandler1b718782014-07-18 12:43:45 -04002950 /**
2951 * {@hide}
2952 */
2953 public static String visibilityToString(int vis) {
2954 switch (vis) {
2955 case VISIBILITY_PRIVATE:
2956 return "PRIVATE";
2957 case VISIBILITY_PUBLIC:
2958 return "PUBLIC";
2959 case VISIBILITY_SECRET:
2960 return "SECRET";
2961 default:
2962 return "UNKNOWN(" + String.valueOf(vis) + ")";
2963 }
2964 }
2965
Joe Onoratocb109a02011-01-18 17:57:41 -08002966 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002967 * {@hide}
2968 */
2969 public static String priorityToString(@Priority int pri) {
2970 switch (pri) {
2971 case PRIORITY_MIN:
2972 return "MIN";
2973 case PRIORITY_LOW:
2974 return "LOW";
2975 case PRIORITY_DEFAULT:
2976 return "DEFAULT";
2977 case PRIORITY_HIGH:
2978 return "HIGH";
2979 case PRIORITY_MAX:
2980 return "MAX";
2981 default:
2982 return "UNKNOWN(" + String.valueOf(pri) + ")";
2983 }
2984 }
2985
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002986 /**
2987 * @hide
2988 */
2989 public boolean hasCompletedProgress() {
2990 // not a progress notification; can't be complete
2991 if (!extras.containsKey(EXTRA_PROGRESS)
2992 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2993 return false;
2994 }
2995 // many apps use max 0 for 'indeterminate'; not complete
2996 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2997 return false;
2998 }
2999 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
3000 }
3001
Jeff Sharkey000ce802017-04-29 13:13:27 -06003002 /** @removed */
3003 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003004 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003005 return mChannelId;
3006 }
3007
3008 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003009 * Returns the id of the channel this notification posts to.
3010 */
3011 public String getChannelId() {
3012 return mChannelId;
3013 }
3014
Jeff Sharkey000ce802017-04-29 13:13:27 -06003015 /** @removed */
3016 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003017 public long getTimeout() {
3018 return mTimeout;
3019 }
3020
3021 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003022 * Returns the duration from posting after which this notification should be canceled by the
3023 * system, if it's not canceled already.
3024 */
3025 public long getTimeoutAfter() {
3026 return mTimeout;
3027 }
3028
3029 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003030 * Returns what icon should be shown for this notification if it is being displayed in a
3031 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3032 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3033 */
3034 public int getBadgeIconType() {
3035 return mBadgeIcon;
3036 }
3037
3038 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003039 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003040 *
3041 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3042 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003043 */
3044 public String getShortcutId() {
3045 return mShortcutId;
3046 }
3047
Julia Reynolds3aedded2017-03-31 14:42:09 -04003048
3049 /**
3050 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3051 */
3052 public CharSequence getSettingsText() {
3053 return mSettingsText;
3054 }
3055
Julia Reynolds13d898c2017-02-02 12:22:05 -05003056 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003057 * Returns which type of notifications in a group are responsible for audibly alerting the
3058 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3059 * {@link #GROUP_ALERT_SUMMARY}.
3060 */
3061 public @GroupAlertBehavior int getGroupAlertBehavior() {
3062 return mGroupAlertBehavior;
3063 }
3064
3065 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003066 * The small icon representing this notification in the status bar and content view.
3067 *
3068 * @return the small icon representing this notification.
3069 *
3070 * @see Builder#getSmallIcon()
3071 * @see Builder#setSmallIcon(Icon)
3072 */
3073 public Icon getSmallIcon() {
3074 return mSmallIcon;
3075 }
3076
3077 /**
3078 * Used when notifying to clean up legacy small icons.
3079 * @hide
3080 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01003081 @UnsupportedAppUsage
Dan Sandlerd63f9322015-05-06 15:18:49 -04003082 public void setSmallIcon(Icon icon) {
3083 mSmallIcon = icon;
3084 }
3085
3086 /**
3087 * The large icon shown in this notification's content view.
3088 * @see Builder#getLargeIcon()
3089 * @see Builder#setLargeIcon(Icon)
3090 */
3091 public Icon getLargeIcon() {
3092 return mLargeIcon;
3093 }
3094
3095 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003096 * @hide
3097 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01003098 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003099 public boolean isGroupSummary() {
3100 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3101 }
3102
3103 /**
3104 * @hide
3105 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01003106 @UnsupportedAppUsage
Christoph Studerc8db24b2014-07-25 17:50:30 +02003107 public boolean isGroupChild() {
3108 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3109 }
3110
3111 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003112 * @hide
3113 */
3114 public boolean suppressAlertingDueToGrouping() {
3115 if (isGroupSummary()
3116 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3117 return true;
3118 } else if (isGroupChild()
3119 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3120 return true;
3121 }
3122 return false;
3123 }
3124
3125 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003126 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003127 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003128 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003129 * content views using the platform's notification layout template. If your app supports
3130 * versions of Android as old as API level 4, you can instead use
3131 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3132 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3133 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003134 *
Scott Main183bf112012-08-13 19:12:13 -07003135 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003136 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003137 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003138 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003139 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3140 * .setContentText(subject)
3141 * .setSmallIcon(R.drawable.new_mail)
3142 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003143 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003144 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003145 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003146 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003147 /**
3148 * @hide
3149 */
3150 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3151 "android.rebuild.contentViewActionCount";
3152 /**
3153 * @hide
3154 */
3155 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3156 = "android.rebuild.bigViewActionCount";
3157 /**
3158 * @hide
3159 */
3160 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3161 = "android.rebuild.hudViewActionCount";
3162
Daniel Sandler602ad1c2012-06-12 16:06:27 -04003163 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04003164
Selim Cinek6743c0b2017-01-18 18:24:01 -08003165 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3166 SystemProperties.getBoolean("notifications.only_title", true);
3167
Selim Cinek389edcd2017-05-11 19:16:44 -07003168 /**
3169 * The lightness difference that has to be added to the primary text color to obtain the
3170 * secondary text color when the background is light.
3171 */
3172 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3173
3174 /**
3175 * The lightness difference that has to be added to the primary text color to obtain the
3176 * secondary text color when the background is dark.
3177 * A bit less then the above value, since it looks better on dark backgrounds.
3178 */
3179 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3180
Joe Onorato46439ce2010-11-19 13:56:21 -08003181 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003182 private Notification mN;
3183 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003184 private Style mStyle;
Mathew Inwood4fb17d12018-08-14 14:25:44 +01003185 @UnsupportedAppUsage
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003186 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003187 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003188 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003189 private boolean mIsLegacy;
3190 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003191
3192 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003193 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3194 */
3195 private int mCachedContrastColor = COLOR_INVALID;
3196 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003197 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003198 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003199 */
3200 private int mCachedAmbientColor = COLOR_INVALID;
3201 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003202 /**
3203 * A neutral color color that can be used for icons.
3204 */
3205 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003206
3207 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003208 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3209 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3210 */
3211 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003212 private int mTextColorsAreForBackground = COLOR_INVALID;
3213 private int mPrimaryTextColor = COLOR_INVALID;
3214 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003215 private int mBackgroundColor = COLOR_INVALID;
3216 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003217 /**
3218 * A temporary location where actions are stored. If != null the view originally has action
3219 * but doesn't have any for this inflation.
3220 */
3221 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003222 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003223
Anthony Chenad4d1582017-04-10 16:07:58 -07003224 private boolean mTintActionButtons;
3225 private boolean mInNightMode;
3226
Adrian Roos70d7aa32017-01-11 15:39:06 -08003227 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003228 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003229 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003230 * @param context
3231 * A {@link Context} that will be used by the Builder to construct the
3232 * RemoteViews. The Context will not be held past the lifetime of this Builder
3233 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003234 * @param channelId
3235 * The constructed Notification will be posted on this
3236 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3237 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003238 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003239 public Builder(Context context, String channelId) {
3240 this(context, (Notification) null);
3241 mN.mChannelId = channelId;
3242 }
3243
3244 /**
3245 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3246 * instead. All posted Notifications must specify a NotificationChannel Id.
3247 */
3248 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003249 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003250 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003251 }
3252
Joe Onoratocb109a02011-01-18 17:57:41 -08003253 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003254 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003255 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003256 public Builder(Context context, Notification toAdopt) {
3257 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003258 Resources res = mContext.getResources();
3259 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3260
3261 if (res.getBoolean(R.bool.config_enableNightMode)) {
3262 Configuration currentConfig = res.getConfiguration();
3263 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3264 == Configuration.UI_MODE_NIGHT_YES;
3265 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003266
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003267 if (toAdopt == null) {
3268 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003269 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3270 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3271 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003272 mN.priority = PRIORITY_DEFAULT;
3273 mN.visibility = VISIBILITY_PRIVATE;
3274 } else {
3275 mN = toAdopt;
3276 if (mN.actions != null) {
3277 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003278 }
3279
Selim Cineke7238dd2017-12-14 17:48:32 -08003280 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3281 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3282 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003283 }
3284
Selim Cinek4ac6f602016-06-13 15:47:03 -07003285 if (mN.getSmallIcon() == null && mN.icon != 0) {
3286 setSmallIcon(mN.icon);
3287 }
3288
3289 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3290 setLargeIcon(mN.largeIcon);
3291 }
3292
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003293 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3294 if (!TextUtils.isEmpty(templateClass)) {
3295 final Class<? extends Style> styleClass
3296 = getNotificationStyleClass(templateClass);
3297 if (styleClass == null) {
3298 Log.d(TAG, "Unknown style class: " + templateClass);
3299 } else {
3300 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003301 final Constructor<? extends Style> ctor =
3302 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003303 ctor.setAccessible(true);
3304 final Style style = ctor.newInstance();
3305 style.restoreFromExtras(mN.extras);
3306
3307 if (style != null) {
3308 setStyle(style);
3309 }
3310 } catch (Throwable t) {
3311 Log.e(TAG, "Could not create Style", t);
3312 }
3313 }
3314 }
3315
3316 }
3317 }
3318
3319 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003320 if (mColorUtil == null) {
3321 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003322 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003323 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003324 }
3325
3326 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003327 * If this notification is duplicative of a Launcher shortcut, sets the
3328 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3329 * the shortcut.
3330 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003331 * This field will be ignored by Launchers that don't support badging, don't show
3332 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003333 *
3334 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3335 * supersedes
3336 */
3337 public Builder setShortcutId(String shortcutId) {
3338 mN.mShortcutId = shortcutId;
3339 return this;
3340 }
3341
3342 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003343 * Sets which icon to display as a badge for this notification.
3344 *
3345 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3346 * {@link #BADGE_ICON_LARGE}.
3347 *
3348 * Note: This value might be ignored, for launchers that don't support badge icons.
3349 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003350 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003351 mN.mBadgeIcon = icon;
3352 return this;
3353 }
3354
3355 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003356 * Sets the group alert behavior for this notification. Use this method to mute this
3357 * notification if alerts for this notification's group should be handled by a different
3358 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003359 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3360 * mute. For example, if you want only the summary of your group to make noise, all
3361 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003362 *
3363 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3364 */
3365 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3366 mN.mGroupAlertBehavior = groupAlertBehavior;
3367 return this;
3368 }
3369
Jeff Sharkey000ce802017-04-29 13:13:27 -06003370 /** @removed */
3371 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003372 public Builder setChannel(String channelId) {
3373 mN.mChannelId = channelId;
3374 return this;
3375 }
3376
3377 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003378 * Specifies the channel the notification should be delivered on.
3379 */
3380 public Builder setChannelId(String channelId) {
3381 mN.mChannelId = channelId;
3382 return this;
3383 }
3384
Jeff Sharkey000ce802017-04-29 13:13:27 -06003385 /** @removed */
3386 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003387 public Builder setTimeout(long durationMs) {
3388 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003389 return this;
3390 }
3391
3392 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003393 * Specifies a duration in milliseconds after which this notification should be canceled,
3394 * if it is not already canceled.
3395 */
3396 public Builder setTimeoutAfter(long durationMs) {
3397 mN.mTimeout = durationMs;
3398 return this;
3399 }
3400
3401 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003402 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003403 *
3404 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3405 * shown anymore by default and must be opted into by using
3406 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003407 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003408 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003409 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003410 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003411 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003412 return this;
3413 }
3414
Joe Onoratocb109a02011-01-18 17:57:41 -08003415 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003416 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003417 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003418 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3419 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003420 */
3421 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003422 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003423 return this;
3424 }
3425
3426 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003427 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003428 *
3429 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003430 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003431 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003432 * Useful when showing an elapsed time (like an ongoing phone call).
3433 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003434 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003435 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003436 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003437 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003438 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003439 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003440 */
3441 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003442 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003443 return this;
3444 }
3445
3446 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003447 * Sets the Chronometer to count down instead of counting up.
3448 *
3449 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3450 * If it isn't set the chronometer will count up.
3451 *
3452 * @see #setUsesChronometer(boolean)
3453 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003454 public Builder setChronometerCountDown(boolean countDown) {
3455 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003456 return this;
3457 }
3458
3459 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003460 * Set the small icon resource, which will be used to represent the notification in the
3461 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003462 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003463
3464 * The platform template for the expanded view will draw this icon in the left, unless a
3465 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3466 * icon will be moved to the right-hand side.
3467 *
3468
3469 * @param icon
3470 * A resource ID in the application's package of the drawable to use.
3471 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003473 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003474 return setSmallIcon(icon != 0
3475 ? Icon.createWithResource(mContext, icon)
3476 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003477 }
3478
Joe Onoratocb109a02011-01-18 17:57:41 -08003479 /**
3480 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3481 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3482 * LevelListDrawable}.
3483 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003484 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003485 * @param level The level to use for the icon.
3486 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003487 * @see Notification#icon
3488 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003489 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003490 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003491 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003492 return setSmallIcon(icon);
3493 }
3494
3495 /**
3496 * Set the small icon, which will be used to represent the notification in the
koprivadebd4ee2018-09-13 10:59:46 -07003497 * status bar and content view (unless overridden there by a
Dan Sandlerd63f9322015-05-06 15:18:49 -04003498 * {@link #setLargeIcon(Bitmap) large icon}).
3499 *
3500 * @param icon An Icon object to use.
3501 * @see Notification#icon
3502 */
3503 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003504 mN.setSmallIcon(icon);
3505 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3506 mN.icon = icon.getResId();
3507 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 return this;
3509 }
3510
Joe Onoratocb109a02011-01-18 17:57:41 -08003511 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003512 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003513 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003514 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003515 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003516 return this;
3517 }
3518
Joe Onoratocb109a02011-01-18 17:57:41 -08003519 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003520 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003521 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003522 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003523 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003524 return this;
3525 }
3526
Joe Onoratocb109a02011-01-18 17:57:41 -08003527 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003528 * This provides some additional information that is displayed in the notification. No
3529 * guarantees are given where exactly it is displayed.
3530 *
3531 * <p>This information should only be provided if it provides an essential
3532 * benefit to the understanding of the notification. The more text you provide the
3533 * less readable it becomes. For example, an email client should only provide the account
3534 * name here if more than one email account has been added.</p>
3535 *
3536 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3537 * notification header area.
3538 *
3539 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3540 * this will be shown in the third line of text in the platform notification template.
3541 * You should not be using {@link #setProgress(int, int, boolean)} at the
3542 * same time on those versions; they occupy the same place.
3543 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003544 */
3545 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003546 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003547 return this;
3548 }
3549
3550 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003551 * Provides text that will appear as a link to your application's settings.
3552 *
3553 * <p>This text does not appear within notification {@link Style templates} but may
3554 * appear when the user uses an affordance to learn more about the notification.
3555 * Additionally, this text will not appear unless you provide a valid link target by
3556 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3557 *
3558 * <p>This text is meant to be concise description about what the user can customize
3559 * when they click on this link. The recommended maximum length is 40 characters.
3560 * @param text
3561 * @return
3562 */
3563 public Builder setSettingsText(CharSequence text) {
3564 mN.mSettingsText = safeCharSequence(text);
3565 return this;
3566 }
3567
3568 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003569 * Set the remote input history.
3570 *
3571 * This should be set to the most recent inputs that have been sent
3572 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3573 * longer relevant (e.g. for chat notifications once the other party has responded).
3574 *
3575 * The most recent input must be stored at the 0 index, the second most recent at the
3576 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3577 * and how much of each individual input is shown.
3578 *
3579 * <p>Note: The reply text will only be shown on notifications that have least one action
3580 * with a {@code RemoteInput}.</p>
3581 */
3582 public Builder setRemoteInputHistory(CharSequence[] text) {
3583 if (text == null) {
3584 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3585 } else {
3586 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3587 CharSequence[] safe = new CharSequence[N];
3588 for (int i = 0; i < N; i++) {
3589 safe[i] = safeCharSequence(text[i]);
3590 }
3591 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3592 }
3593 return this;
3594 }
3595
3596 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003597 * Sets whether remote history entries view should have a spinner.
3598 * @hide
3599 */
3600 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3601 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3602 return this;
3603 }
3604
3605 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003606 * Sets whether smart reply buttons should be hidden.
3607 * @hide
3608 */
3609 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3610 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3611 return this;
3612 }
3613
3614 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003615 * Sets the number of items this notification represents. May be displayed as a badge count
3616 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003617 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003618 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003619 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003620 return this;
3621 }
3622
Joe Onoratocb109a02011-01-18 17:57:41 -08003623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003624 * A small piece of additional information pertaining to this notification.
3625 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003626 * The platform template will draw this on the last line of the notification, at the far
3627 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003628 *
3629 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3630 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3631 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003633 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003634 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003636 return this;
3637 }
3638
Joe Onoratocb109a02011-01-18 17:57:41 -08003639 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003640 * Set the progress this notification represents.
3641 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003642 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003643 */
3644 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003645 mN.extras.putInt(EXTRA_PROGRESS, progress);
3646 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3647 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003648 return this;
3649 }
3650
3651 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003652 * Supply a custom RemoteViews to use instead of the platform template.
3653 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003654 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003655 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003656 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003657 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003658 return setCustomContentView(views);
3659 }
3660
3661 /**
3662 * Supply custom RemoteViews to use instead of the platform template.
3663 *
3664 * This will override the layout that would otherwise be constructed by this Builder
3665 * object.
3666 */
3667 public Builder setCustomContentView(RemoteViews contentView) {
3668 mN.contentView = contentView;
3669 return this;
3670 }
3671
3672 /**
3673 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3674 *
3675 * This will override the expanded layout that would otherwise be constructed by this
3676 * Builder object.
3677 */
3678 public Builder setCustomBigContentView(RemoteViews contentView) {
3679 mN.bigContentView = contentView;
3680 return this;
3681 }
3682
3683 /**
3684 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3685 *
3686 * This will override the heads-up layout that would otherwise be constructed by this
3687 * Builder object.
3688 */
3689 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3690 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003691 return this;
3692 }
3693
Joe Onoratocb109a02011-01-18 17:57:41 -08003694 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003695 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3696 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003697 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3698 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3699 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003700 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003701 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003702 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003703 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003704 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003705 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003706 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003707 return this;
3708 }
3709
Joe Onoratocb109a02011-01-18 17:57:41 -08003710 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003711 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3712 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003713 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003714 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003715 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003716 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003717 return this;
3718 }
3719
Joe Onoratocb109a02011-01-18 17:57:41 -08003720 /**
3721 * An intent to launch instead of posting the notification to the status bar.
3722 * Only for use with extremely high-priority notifications demanding the user's
3723 * <strong>immediate</strong> attention, such as an incoming phone call or
3724 * alarm clock that the user has explicitly set to a particular time.
3725 * If this facility is used for something else, please give the user an option
3726 * to turn it off and use a normal notification, as this can be extremely
3727 * disruptive.
3728 *
Chris Wren47c20a12014-06-18 17:27:29 -04003729 * <p>
3730 * The system UI may choose to display a heads-up notification, instead of
3731 * launching this intent, while the user is using the device.
3732 * </p>
3733 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003734 * @param intent The pending intent to launch.
3735 * @param highPriority Passing true will cause this notification to be sent
3736 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003737 *
3738 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003739 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003740 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003742 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3743 return this;
3744 }
3745
Joe Onoratocb109a02011-01-18 17:57:41 -08003746 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003747 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003748 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003749 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003750 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003751 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003752 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003753 return this;
3754 }
3755
Joe Onoratocb109a02011-01-18 17:57:41 -08003756 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003757 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003758 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003759 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003760 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003761 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003762 setTicker(tickerText);
3763 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003764 return this;
3765 }
3766
Joe Onoratocb109a02011-01-18 17:57:41 -08003767 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003768 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003769 *
3770 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003771 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3772 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003773 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003774 public Builder setLargeIcon(Bitmap b) {
3775 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3776 }
3777
3778 /**
3779 * Add a large icon to the notification content view.
3780 *
3781 * In the platform template, this image will be shown on the left of the notification view
3782 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3783 * badge atop the large icon).
3784 */
3785 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003786 mN.mLargeIcon = icon;
3787 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003788 return this;
3789 }
3790
Joe Onoratocb109a02011-01-18 17:57:41 -08003791 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003792 * Set the sound to play.
3793 *
John Spurlockc0650f022014-07-19 13:22:39 -04003794 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3795 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003796 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003797 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003798 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003799 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003800 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003801 mN.sound = sound;
3802 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003803 return this;
3804 }
3805
Joe Onoratocb109a02011-01-18 17:57:41 -08003806 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003807 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003808 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003809 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3810 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003811 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003812 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003813 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003814 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003815 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003816 mN.sound = sound;
3817 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003818 return this;
3819 }
3820
Joe Onoratocb109a02011-01-18 17:57:41 -08003821 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003822 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3823 * use during playback.
3824 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003825 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003826 * @see Notification#sound
3827 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003828 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003829 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003830 mN.sound = sound;
3831 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003832 return this;
3833 }
3834
3835 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003836 * Set the vibration pattern to use.
3837 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003838 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3839 * <code>pattern</code> parameter.
3840 *
Chris Wren47c20a12014-06-18 17:27:29 -04003841 * <p>
3842 * A notification that vibrates is more likely to be presented as a heads-up notification.
3843 * </p>
3844 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003845 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003846 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003848 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003849 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003850 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003851 return this;
3852 }
3853
Joe Onoratocb109a02011-01-18 17:57:41 -08003854 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003855 * Set the desired color for the indicator LED on the device, as well as the
3856 * blink duty cycle (specified in milliseconds).
3857 *
3858
3859 * Not all devices will honor all (or even any) of these values.
3860 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003861 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003862 * @see Notification#ledARGB
3863 * @see Notification#ledOnMS
3864 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003865 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003866 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003867 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003868 mN.ledARGB = argb;
3869 mN.ledOnMS = onMs;
3870 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003871 if (onMs != 0 || offMs != 0) {
3872 mN.flags |= FLAG_SHOW_LIGHTS;
3873 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003874 return this;
3875 }
3876
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003878 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003879 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880
3881 * Ongoing notifications cannot be dismissed by the user, so your application or service
3882 * must take care of canceling them.
3883 *
3884
3885 * They are typically used to indicate a background task that the user is actively engaged
3886 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3887 * (e.g., a file download, sync operation, active network connection).
3888 *
3889
3890 * @see Notification#FLAG_ONGOING_EVENT
3891 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003892 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003893 public Builder setOngoing(boolean ongoing) {
3894 setFlag(FLAG_ONGOING_EVENT, ongoing);
3895 return this;
3896 }
3897
Joe Onoratocb109a02011-01-18 17:57:41 -08003898 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003899 * Set whether this notification should be colorized. When set, the color set with
3900 * {@link #setColor(int)} will be used as the background color of this notification.
3901 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003902 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3903 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003904 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003905 * For most styles, the coloring will only be applied if the notification is for a
3906 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003907 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003908 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003909 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003910 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003911 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003912 */
3913 public Builder setColorized(boolean colorize) {
3914 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3915 return this;
3916 }
3917
3918 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003919 * Set this flag if you would only like the sound, vibrate
3920 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003921 *
3922 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003923 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003924 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3925 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3926 return this;
3927 }
3928
Joe Onoratocb109a02011-01-18 17:57:41 -08003929 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003930 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003931 *
3932 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003933 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003934 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003935 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003936 return this;
3937 }
3938
Joe Onoratocb109a02011-01-18 17:57:41 -08003939 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003940 * Set whether or not this notification should not bridge to other devices.
3941 *
3942 * <p>Some notifications can be bridged to other devices for remote display.
3943 * This hint can be set to recommend this notification not be bridged.
3944 */
3945 public Builder setLocalOnly(boolean localOnly) {
3946 setFlag(FLAG_LOCAL_ONLY, localOnly);
3947 return this;
3948 }
3949
3950 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003951 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003952 * <p>
3953 * The value should be one or more of the following fields combined with
3954 * bitwise-or:
3955 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3956 * <p>
3957 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003958 *
3959 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003960 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003961 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003962 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003963 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003964 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003965 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003966 return this;
3967 }
3968
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003969 /**
3970 * Set the priority of this notification.
3971 *
3972 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003973 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003974 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003975 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003976 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003977 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003978 return this;
3979 }
Joe Malin8d40d042012-11-05 11:36:40 -08003980
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003981 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003982 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003983 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003984 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003985 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003986 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003987 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003988 return this;
3989 }
3990
3991 /**
Chris Wrendde75302014-03-26 17:24:15 -04003992 * Add a person that is relevant to this notification.
3993 *
Chris Wrene6c48932014-09-29 17:19:27 -04003994 * <P>
3995 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003996 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3997 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3998 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003999 * </P>
4000 *
4001 * <P>
4002 * The person should be specified by the {@code String} representation of a
4003 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
4004 * </P>
4005 *
4006 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
4007 * URIs. The path part of these URIs must exist in the contacts database, in the
4008 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4009 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004010 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4011 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004012 * </P>
4013 *
4014 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004015 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004016 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004017 */
Chris Wrene6c48932014-09-29 17:19:27 -04004018 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004019 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004020 return this;
4021 }
4022
4023 /**
4024 * Add a person that is relevant to this notification.
4025 *
4026 * <P>
4027 * Depending on user preferences, this annotation may allow the notification to pass
4028 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4029 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4030 * appear more prominently in the user interface.
4031 * </P>
4032 *
4033 * <P>
4034 * A person should usually contain a uri in order to benefit from the ranking boost.
4035 * However, even if no uri is provided, it's beneficial to provide other people in the
4036 * notification, such that listeners and voice only devices can announce and handle them
4037 * properly.
4038 * </P>
4039 *
4040 * @param person the person to add.
4041 * @see Notification#EXTRA_PEOPLE_LIST
4042 */
4043 public Builder addPerson(Person person) {
4044 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004045 return this;
4046 }
4047
4048 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004049 * Set this notification to be part of a group of notifications sharing the same key.
4050 * Grouped notifications may display in a cluster or stack on devices which
4051 * support such rendering.
4052 *
4053 * <p>To make this notification the summary for its group, also call
4054 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4055 * {@link #setSortKey}.
4056 * @param groupKey The group key of the group.
4057 * @return this object for method chaining
4058 */
4059 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004060 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004061 return this;
4062 }
4063
4064 /**
4065 * Set this notification to be the group summary for a group of notifications.
4066 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004067 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4068 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004069 * @param isGroupSummary Whether this notification should be a group summary.
4070 * @return this object for method chaining
4071 */
4072 public Builder setGroupSummary(boolean isGroupSummary) {
4073 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4074 return this;
4075 }
4076
4077 /**
4078 * Set a sort key that orders this notification among other notifications from the
4079 * same package. This can be useful if an external sort was already applied and an app
4080 * would like to preserve this. Notifications will be sorted lexicographically using this
4081 * value, although providing different priorities in addition to providing sort key may
4082 * cause this value to be ignored.
4083 *
4084 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004085 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004086 *
4087 * @see String#compareTo(String)
4088 */
4089 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004090 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004091 return this;
4092 }
4093
4094 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004095 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004096 *
Griff Hazen720042b2014-02-24 15:46:56 -08004097 * <p>Values within the Bundle will replace existing extras values in this Builder.
4098 *
4099 * @see Notification#extras
4100 */
Griff Hazen959591e2014-05-15 22:26:18 -07004101 public Builder addExtras(Bundle extras) {
4102 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004103 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004104 }
4105 return this;
4106 }
4107
4108 /**
4109 * Set metadata for this notification.
4110 *
4111 * <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 -04004112 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004113 * called.
4114 *
Griff Hazen720042b2014-02-24 15:46:56 -08004115 * <p>Replaces any existing extras values with those from the provided Bundle.
4116 * Use {@link #addExtras} to merge in metadata instead.
4117 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004118 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004119 */
Griff Hazen959591e2014-05-15 22:26:18 -07004120 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004121 if (extras != null) {
4122 mUserExtras = extras;
4123 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004124 return this;
4125 }
4126
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004127 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004128 * Get the current metadata Bundle used by this notification Builder.
4129 *
4130 * <p>The returned Bundle is shared with this Builder.
4131 *
4132 * <p>The current contents of this Bundle are copied into the Notification each time
4133 * {@link #build()} is called.
4134 *
4135 * @see Notification#extras
4136 */
4137 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004138 return mUserExtras;
4139 }
4140
4141 private Bundle getAllExtras() {
4142 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4143 saveExtras.putAll(mN.extras);
4144 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004145 }
4146
4147 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004148 * Add an action to this notification. Actions are typically displayed by
4149 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004150 * <p>
4151 * Every action must have an icon (32dp square and matching the
4152 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4153 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4154 * <p>
4155 * A notification in its expanded form can display up to 3 actions, from left to right in
4156 * the order they were added. Actions will not be displayed when the notification is
4157 * collapsed, however, so be sure that any essential functions may be accessed by the user
4158 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004159 *
4160 * @param icon Resource ID of a drawable that represents the action.
4161 * @param title Text describing the action.
4162 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004163 *
4164 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004165 */
Dan Sandler86647982015-05-13 23:41:13 -04004166 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004167 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004168 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004169 return this;
4170 }
4171
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004172 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004173 * Add an action to this notification. Actions are typically displayed by
4174 * the system as a button adjacent to the notification content.
4175 * <p>
4176 * Every action must have an icon (32dp square and matching the
4177 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4178 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4179 * <p>
4180 * A notification in its expanded form can display up to 3 actions, from left to right in
4181 * the order they were added. Actions will not be displayed when the notification is
4182 * collapsed, however, so be sure that any essential functions may be accessed by the user
4183 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4184 *
4185 * @param action The action to add.
4186 */
4187 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004188 if (action != null) {
4189 mActions.add(action);
4190 }
Griff Hazen959591e2014-05-15 22:26:18 -07004191 return this;
4192 }
4193
4194 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004195 * Alter the complete list of actions attached to this notification.
4196 * @see #addAction(Action).
4197 *
4198 * @param actions
4199 * @return
4200 */
4201 public Builder setActions(Action... actions) {
4202 mActions.clear();
4203 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004204 if (actions[i] != null) {
4205 mActions.add(actions[i]);
4206 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004207 }
4208 return this;
4209 }
4210
4211 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004212 * Add a rich notification style to be applied at build time.
4213 *
4214 * @param style Object responsible for modifying the notification style.
4215 */
4216 public Builder setStyle(Style style) {
4217 if (mStyle != style) {
4218 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004219 if (mStyle != null) {
4220 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004221 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4222 } else {
4223 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004224 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004225 }
4226 return this;
4227 }
4228
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004229 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004230 * Returns the style set by {@link #setStyle(Style)}.
4231 */
4232 public Style getStyle() {
4233 return mStyle;
4234 }
4235
4236 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004237 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004238 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004239 * @return The same Builder.
4240 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004241 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004242 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004243 return this;
4244 }
4245
4246 /**
4247 * Supply a replacement Notification whose contents should be shown in insecure contexts
4248 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4249 * @param n A replacement notification, presumably with some or all info redacted.
4250 * @return The same Builder.
4251 */
4252 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004253 if (n != null) {
4254 mN.publicVersion = new Notification();
4255 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4256 } else {
4257 mN.publicVersion = null;
4258 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004259 return this;
4260 }
4261
Griff Hazenb720abe2014-05-20 13:15:30 -07004262 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004263 * Apply an extender to this notification builder. Extenders may be used to add
4264 * metadata or change options on this builder.
4265 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004266 public Builder extend(Extender extender) {
4267 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004268 return this;
4269 }
4270
Dan Sandler4e787062015-06-17 15:09:48 -04004271 /**
4272 * @hide
4273 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004274 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004275 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004276 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004277 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004278 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004279 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004280 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004281 }
4282
Dan Sandler26e81cf2014-05-06 10:01:27 -04004283 /**
4284 * Sets {@link Notification#color}.
4285 *
4286 * @param argb The accent color to use
4287 *
4288 * @return The same Builder.
4289 */
Tor Norbye80756e32015-03-02 09:39:27 -08004290 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004291 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004292 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004293 return this;
4294 }
4295
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004296 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004297 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4298 // This user can never be a badged profile,
4299 // and also includes USER_ALL system notifications.
4300 return null;
4301 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004302 // Note: This assumes that the current user can read the profile badge of the
4303 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004304 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004305 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004306 }
4307
4308 private Bitmap getProfileBadge() {
4309 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004310 if (badge == null) {
4311 return null;
4312 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004313 final int size = mContext.getResources().getDimensionPixelSize(
4314 R.dimen.notification_badge_size);
4315 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004316 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004317 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004318 badge.draw(canvas);
4319 return bitmap;
4320 }
4321
Selim Cinekc848c3a2016-01-13 15:27:30 -08004322 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004323 Bitmap profileBadge = getProfileBadge();
4324
Kenny Guy98193ea2014-07-24 19:54:37 +01004325 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004326 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4327 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004328 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004329 contentView.setDrawableTint(R.id.profile_badge, false,
4330 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004331 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004332 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004333 }
4334
Julia Reynoldsfc640012018-02-21 12:25:27 -05004335 /**
4336 * @hide
4337 */
4338 public boolean usesStandardHeader() {
4339 if (mN.mUsesStandardHeader) {
4340 return true;
4341 }
4342 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4343 if (mN.contentView == null && mN.bigContentView == null) {
4344 return true;
4345 }
4346 }
4347 boolean contentViewUsesHeader = mN.contentView == null
4348 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4349 boolean bigContentViewUsesHeader = mN.bigContentView == null
4350 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4351 return contentViewUsesHeader && bigContentViewUsesHeader;
4352 }
4353
Christoph Studerfe718432014-09-01 18:21:18 +02004354 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004355 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004356 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004357 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004358 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004359 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004360 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004361 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004362 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004363 }
4364
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004365 /**
4366 * Resets the notification header to its original state
4367 */
4368 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004369 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4370 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004371 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004372 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004373 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004374 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004375 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004376 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4377 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004378 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004379 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004380 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004381 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004382 contentView.setImageViewIcon(R.id.profile_badge, null);
4383 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004384 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004385 }
4386
Selim Cinek384804b2018-04-18 14:31:07 +08004387 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4388 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4389 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004390 }
4391
4392 /**
4393 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004394 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004395 */
Selim Cinek384804b2018-04-18 14:31:07 +08004396 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4397 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004398 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004399 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004400 }
4401
Selim Cinek384804b2018-04-18 14:31:07 +08004402 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4403 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004404 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004405
Christoph Studerfe718432014-09-01 18:21:18 +02004406 resetStandardTemplate(contentView);
4407
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004408 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004409 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004410 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004411 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004412 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4413 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004414 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004415 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004416 if (!p.ambient) {
4417 setTextViewColorPrimary(contentView, R.id.title);
4418 }
Selim Cinek954cc232016-05-20 13:29:23 -07004419 contentView.setViewLayoutWidth(R.id.title, showProgress
4420 ? ViewGroup.LayoutParams.WRAP_CONTENT
4421 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004422 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004423 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004424 int textId = showProgress ? com.android.internal.R.id.text_line_1
4425 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004426 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004427 if (!p.ambient) {
4428 setTextViewColorSecondary(contentView, textId);
4429 }
Selim Cinek41598732016-01-11 16:58:37 -08004430 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004431 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004432
Selim Cinek279fa862016-06-14 10:57:25 -07004433 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004434
Selim Cinek29603462015-11-17 19:04:39 -08004435 return contentView;
4436 }
4437
Selim Cinek48f66b72017-08-18 16:17:51 -07004438 private CharSequence processTextSpans(CharSequence text) {
4439 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004440 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004441 }
4442 return text;
4443 }
4444
Selim Cinek7b9605b2017-01-19 17:36:00 -08004445 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4446 ensureColors();
4447 contentView.setTextColor(id, mPrimaryTextColor);
4448 }
4449
Selim Cinek48f66b72017-08-18 16:17:51 -07004450 private boolean hasForegroundColor() {
4451 return mForegroundColor != COLOR_INVALID;
4452 }
4453
Selim Cinek389edcd2017-05-11 19:16:44 -07004454 /**
4455 * @return the primary text color
4456 * @hide
4457 */
4458 @VisibleForTesting
4459 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004460 ensureColors();
4461 return mPrimaryTextColor;
4462 }
4463
Selim Cinek389edcd2017-05-11 19:16:44 -07004464 /**
4465 * @return the secondary text color
4466 * @hide
4467 */
4468 @VisibleForTesting
4469 public int getSecondaryTextColor() {
4470 ensureColors();
4471 return mSecondaryTextColor;
4472 }
4473
Selim Cinek7b9605b2017-01-19 17:36:00 -08004474 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4475 ensureColors();
4476 contentView.setTextColor(id, mSecondaryTextColor);
4477 }
4478
4479 private void ensureColors() {
4480 int backgroundColor = getBackgroundColor();
4481 if (mPrimaryTextColor == COLOR_INVALID
4482 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004483 || mTextColorsAreForBackground != backgroundColor) {
4484 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004485 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004486 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4487 backgroundColor);
4488 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4489 backgroundColor);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004490 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Selim Cinekac5f0272017-05-02 16:05:41 -07004491 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4492 mPrimaryTextColor, backgroundColor, 4.5);
4493 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4494 mSecondaryTextColor, backgroundColor, 4.5);
4495 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004496 } else {
4497 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4498 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4499 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4500 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004501 // We only respect the given colors if worst case Black or White still has
4502 // contrast
4503 boolean backgroundLight = backLum > textLum
4504 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4505 || backLum <= textLum
4506 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004507 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004508 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004509 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4510 mForegroundColor,
4511 backgroundColor,
4512 true /* findFG */,
4513 4.5f);
4514 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004515 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004516 } else {
4517 mSecondaryTextColor =
4518 NotificationColorUtil.findContrastColorAgainstDark(
4519 mForegroundColor,
4520 backgroundColor,
4521 true /* findFG */,
4522 4.5f);
4523 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004524 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004525 }
4526 } else {
4527 mPrimaryTextColor = mForegroundColor;
4528 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004529 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4530 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004531 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4532 backgroundColor) < 4.5f) {
4533 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004534 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004535 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4536 mSecondaryTextColor,
4537 backgroundColor,
4538 true /* findFG */,
4539 4.5f);
4540 } else {
4541 mSecondaryTextColor
4542 = NotificationColorUtil.findContrastColorAgainstDark(
4543 mSecondaryTextColor,
4544 backgroundColor,
4545 true /* findFG */,
4546 4.5f);
4547 }
4548 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004549 mSecondaryTextColor, backgroundLight
4550 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4551 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004552 }
4553 }
4554 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004555 }
4556 }
4557
4558 private void updateBackgroundColor(RemoteViews contentView) {
4559 if (isColorized()) {
4560 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4561 getBackgroundColor());
4562 } else {
4563 // Clear it!
4564 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4565 0);
4566 }
4567 }
4568
Selim Cinek860b6da2015-12-16 19:02:19 -08004569 /**
4570 * @param remoteView the remote view to update the minheight in
4571 * @param hasMinHeight does it have a mimHeight
4572 * @hide
4573 */
4574 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4575 int minHeight = 0;
4576 if (hasMinHeight) {
4577 // we need to set the minHeight of the notification
4578 minHeight = mContext.getResources().getDimensionPixelSize(
4579 com.android.internal.R.dimen.notification_min_content_height);
4580 }
4581 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4582 }
4583
Selim Cinek29603462015-11-17 19:04:39 -08004584 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004585 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4586 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4587 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4588 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004589 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004590 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004591 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004592 contentView.setProgressBackgroundTintList(
4593 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4594 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004595 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004596 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004597 contentView.setProgressTintList(R.id.progress, colorStateList);
4598 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004599 }
Selim Cinek29603462015-11-17 19:04:39 -08004600 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004601 } else {
4602 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004603 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004604 }
Joe Onorato561d3852010-11-20 18:09:34 -08004605 }
4606
Selim Cinek384804b2018-04-18 14:31:07 +08004607 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4608 TemplateBindResult result) {
4609 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004610 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004611 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004612 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4613 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4614 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4615 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4616 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004617 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004618 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004619 }
4620 }
4621
Selim Cinek1c72fa02018-04-23 18:00:54 +08004622 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4623 int marginEnd = 0;
4624 int contentMargin = mContext.getResources().getDimensionPixelSize(
4625 R.dimen.notification_content_margin_end);
4626 int iconSize = mContext.getResources().getDimensionPixelSize(
4627 R.dimen.notification_right_icon_size);
4628 if (replyIconShown) {
4629 // The size of the reply icon
4630 marginEnd += iconSize;
4631
4632 int replyInset = mContext.getResources().getDimensionPixelSize(
4633 R.dimen.notification_reply_inset);
4634 // We're subtracting the inset of the reply icon to make sure it's
4635 // aligned nicely on the right, and remove it from the following padding
4636 marginEnd -= replyInset * 2;
4637 }
4638 if (largeIconShown) {
4639 // adding size of the right icon
4640 marginEnd += iconSize;
4641
4642 if (replyIconShown) {
4643 // We also add some padding to the reply icon if it's around
4644 marginEnd += contentMargin;
4645 }
4646 }
4647 if (replyIconShown || largeIconShown) {
4648 // The padding to the content
4649 marginEnd += contentMargin;
4650 }
4651 return marginEnd;
4652 }
4653
Selim Cinek384804b2018-04-18 14:31:07 +08004654 /**
4655 * Bind the large icon.
4656 * @return if the largeIcon is visible
4657 */
4658 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004659 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4660 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4661 }
Selim Cinek88188f22017-09-19 16:46:56 -07004662 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4663 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004664 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4665 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4666 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004667 }
Selim Cinek384804b2018-04-18 14:31:07 +08004668 return showLargeIcon;
4669 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004670
Selim Cinek384804b2018-04-18 14:31:07 +08004671 /**
4672 * Bind the reply icon.
4673 * @return if the reply icon is visible
4674 */
4675 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4676 boolean actionVisible = !hideReplyIcon;
4677 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004678 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004679 action = findReplyAction();
4680 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004681 }
Selim Cinek384804b2018-04-18 14:31:07 +08004682 if (actionVisible) {
4683 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4684 contentView.setDrawableTint(R.id.reply_icon_action,
4685 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004686 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004687 PorterDuff.Mode.SRC_ATOP);
4688 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4689 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4690 } else {
4691 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4692 }
4693 contentView.setViewVisibility(R.id.reply_icon_action,
4694 actionVisible ? View.VISIBLE : View.GONE);
4695 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004696 }
4697
4698 private Action findReplyAction() {
4699 ArrayList<Action> actions = mActions;
4700 if (mOriginalActions != null) {
4701 actions = mOriginalActions;
4702 }
4703 int numActions = actions.size();
4704 for (int i = 0; i < numActions; i++) {
4705 Action action = actions.get(i);
4706 if (hasValidRemoteInput(action)) {
4707 return action;
4708 }
4709 }
4710 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004711 }
4712
Selim Cinekafeed292017-12-12 17:32:44 -08004713 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4714 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004715 bindSmallIcon(contentView, ambient);
4716 bindHeaderAppName(contentView, ambient);
4717 if (!ambient) {
4718 // Ambient view does not have these
4719 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004720 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004721 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004722 bindProfileBadge(contentView);
4723 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004724 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004725 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004726 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004727 }
4728
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004729 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004730 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004731 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4732 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4733 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4734 }
4735
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004736 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004737 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004738 contentView.setDrawableTint(R.id.expand_button, false, color,
4739 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004740 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004741 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004742 }
4743
4744 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4745 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004746 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004747 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004748 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4749 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4750 contentView.setLong(R.id.chronometer, "setBase",
4751 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4752 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004753 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004754 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004755 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004756 } else {
4757 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4758 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004759 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004760 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004761 } else {
4762 // We still want a time to be set but gone, such that we can show and hide it
4763 // on demand in case it's a child notification without anything in the header
4764 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004765 }
4766 }
4767
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004768 private void bindHeaderText(RemoteViews contentView) {
4769 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4770 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004771 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004772 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004773 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004774 if (headerText == null
4775 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4776 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4777 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4778 }
4779 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004780 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004781 contentView.setTextViewText(R.id.header_text, processTextSpans(
4782 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004783 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004784 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4785 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004786 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004787 }
4788 }
4789
Selim Cinekafeed292017-12-12 17:32:44 -08004790 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4791 if (!TextUtils.isEmpty(secondaryText)) {
4792 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4793 processLegacyText(secondaryText)));
4794 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4795 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4796 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4797 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4798 }
4799 }
4800
Adrian Rooseba05822016-04-22 17:09:27 -07004801 /**
4802 * @hide
4803 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01004804 @UnsupportedAppUsage
Adrian Rooseba05822016-04-22 17:09:27 -07004805 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004806 CharSequence name = null;
4807 final PackageManager pm = mContext.getPackageManager();
4808 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4809 // only system packages which lump together a bunch of unrelated stuff
4810 // may substitute a different name to make the purpose of the
4811 // notification more clear. the correct package label should always
4812 // be accessible via SystemUI.
4813 final String pkg = mContext.getPackageName();
4814 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4815 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4816 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4817 name = subName;
4818 } else {
4819 Log.w(TAG, "warning: pkg "
4820 + pkg + " attempting to substitute app name '" + subName
4821 + "' without holding perm "
4822 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4823 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004824 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004825 if (TextUtils.isEmpty(name)) {
4826 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4827 }
4828 if (TextUtils.isEmpty(name)) {
4829 // still nothing?
4830 return null;
4831 }
4832
4833 return String.valueOf(name);
4834 }
Adrian Roos487374f2017-01-11 15:48:14 -08004835 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004836 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004837 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004838 setTextViewColorPrimary(contentView, R.id.app_name_text);
4839 } else {
4840 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004841 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004842 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004843 }
4844
Adrian Roos487374f2017-01-11 15:48:14 -08004845 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004846 if (mN.mSmallIcon == null && mN.icon != 0) {
4847 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4848 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004849 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004850 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004851 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004852 }
4853
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004854 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004855 * @return true if the built notification will show the time or the chronometer; false
4856 * otherwise
4857 */
4858 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004859 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004860 }
4861
Christoph Studerfe718432014-09-01 18:21:18 +02004862 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004863 // actions_container is only reset when there are no actions to avoid focus issues with
4864 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004865 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004866 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004867
4868 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4869 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004870 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4871 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004872
4873 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4874 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4875 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4876 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004877
Selim Cineked64a142018-02-06 18:06:01 -08004878 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4879 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004880 }
4881
Selim Cinek384804b2018-04-18 14:31:07 +08004882 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4883 TemplateBindResult result) {
4884 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4885 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004886 }
4887
Adrian Roos70d7aa32017-01-11 15:39:06 -08004888 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004889 StandardTemplateParams p, TemplateBindResult result) {
4890 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004891
Christoph Studerfe718432014-09-01 18:21:18 +02004892 resetStandardTemplateWithActions(big);
4893
Adrian Roose458aa82015-12-08 16:17:19 -08004894 boolean validRemoteInput = false;
4895
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004896 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004897 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004898 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004899 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004900 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004901 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004902 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004903 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004904 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004905 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004906 boolean actionHasValidInput = hasValidRemoteInput(action);
4907 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004908
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004909 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004910 p.ambient);
4911 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004912 // Clear the drawable
4913 button.setInt(R.id.action0, "setBackgroundResource", 0);
4914 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004915 big.addView(R.id.actions, button);
4916 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004917 } else {
4918 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004919 }
Adrian Roose458aa82015-12-08 16:17:19 -08004920
4921 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004922 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07004923 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
4924 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01004925 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08004926 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01004927 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
4928 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004929 big.setTextViewText(R.id.notification_material_reply_text_1,
4930 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004931 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01004932 big.setViewVisibility(R.id.notification_material_reply_progress,
4933 showSpinner ? View.VISIBLE : View.GONE);
4934 big.setProgressIndeterminateTintList(
4935 R.id.notification_material_reply_progress,
4936 ColorStateList.valueOf(
4937 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08004938
Selim Cinekbee4e072018-05-21 22:06:43 -07004939 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
4940 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08004941 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004942 big.setTextViewText(R.id.notification_material_reply_text_2,
4943 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004944 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004945
Selim Cinekbee4e072018-05-21 22:06:43 -07004946 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
4947 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08004948 big.setViewVisibility(
4949 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004950 big.setTextViewText(R.id.notification_material_reply_text_3,
4951 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004952 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004953 }
4954 }
4955 }
4956
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004957 return big;
4958 }
4959
Adrian Roose458aa82015-12-08 16:17:19 -08004960 private boolean hasValidRemoteInput(Action action) {
4961 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4962 // Weird actions
4963 return false;
4964 }
4965
4966 RemoteInput[] remoteInputs = action.getRemoteInputs();
4967 if (remoteInputs == null) {
4968 return false;
4969 }
4970
4971 for (RemoteInput r : remoteInputs) {
4972 CharSequence[] choices = r.getChoices();
4973 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4974 return true;
4975 }
4976 }
4977 return false;
4978 }
4979
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004980 /**
4981 * Construct a RemoteViews for the final 1U notification layout. In order:
4982 * 1. Custom contentView from the caller
4983 * 2. Style's proposed content view
4984 * 3. Standard template view
4985 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004986 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004987 return createContentView(false /* increasedheight */ );
4988 }
4989
4990 /**
4991 * Construct a RemoteViews for the smaller content view.
4992 *
4993 * @param increasedHeight true if this layout be created with an increased height. Some
4994 * styles may support showing more then just that basic 1U size
4995 * and the system may decide to render important notifications
4996 * slightly bigger even when collapsed.
4997 *
4998 * @hide
4999 */
5000 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005001 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005002 return mN.contentView;
5003 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08005004 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005005 if (styleView != null) {
5006 return styleView;
5007 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005008 }
Selim Cinek384804b2018-04-18 14:31:07 +08005009 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005010 }
5011
Selim Cineka7679b62017-05-10 16:33:25 -07005012 private boolean useExistingRemoteView() {
5013 return mStyle == null || (!mStyle.displayCustomViewInline()
5014 && !mRebuildStyledRemoteViews);
5015 }
5016
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005017 /**
5018 * Construct a RemoteViews for the final big notification layout.
5019 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005020 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005021 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005022 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005023 return mN.bigContentView;
5024 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005025 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005026 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005027 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005028 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5029 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005030 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005031 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005032 return result;
5033 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005034
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005035 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005036 * Construct a RemoteViews for the final notification header only. This will not be
5037 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005038 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005039 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005040 * @hide
5041 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005042 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005043 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5044 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005045 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005046 ambient ? R.layout.notification_template_ambient_header
5047 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005048 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005049 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005050 if (colorized != null) {
5051 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5052 } else {
5053 mN.extras.remove(EXTRA_COLORIZED);
5054 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005055 return header;
5056 }
5057
Adrian Roos487374f2017-01-11 15:48:14 -08005058 /**
5059 * Construct a RemoteViews for the ambient version of the notification.
5060 *
5061 * @hide
5062 */
5063 public RemoteViews makeAmbientNotification() {
5064 RemoteViews ambient = applyStandardTemplateWithActions(
5065 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005066 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5067 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005068 return ambient;
5069 }
5070
Selim Cinek29603462015-11-17 19:04:39 -08005071 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005072 if (result != null) {
5073 result.setViewVisibility(R.id.text_line_1, View.GONE);
5074 }
Selim Cinek29603462015-11-17 19:04:39 -08005075 }
5076
Selim Cinek6743c0b2017-01-18 18:24:01 -08005077 /**
5078 * Adapt the Notification header if this view is used as an expanded view.
5079 *
5080 * @hide
5081 */
5082 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005083 if (result != null) {
5084 result.setBoolean(R.id.notification_header, "setExpanded", true);
5085 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005086 }
5087
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005088 /**
5089 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005090 *
5091 * @param increasedHeight true if this layout be created with an increased height. Some
5092 * styles may support showing more then just that basic 1U size
5093 * and the system may decide to render important notifications
5094 * slightly bigger even when collapsed.
5095 *
5096 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005097 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005098 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005099 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005100 return mN.headsUpContentView;
5101 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005102 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5103 if (styleView != null) {
5104 return styleView;
5105 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005106 } else if (mActions.size() == 0) {
5107 return null;
5108 }
5109
Selim Cinekbee4e072018-05-21 22:06:43 -07005110 // We only want at most a single remote input history to be shown here, otherwise
5111 // the content would become squished.
5112 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5113 .setMaxRemoteInputHistory(1);
5114 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5115 p,
5116 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005117 }
5118
Selim Cinek624c02db2015-12-14 21:00:02 -08005119 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005120 * Construct a RemoteViews for the final heads-up notification layout.
5121 */
5122 public RemoteViews createHeadsUpContentView() {
5123 return createHeadsUpContentView(false /* useIncreasedHeight */);
5124 }
5125
5126 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005127 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5128 *
5129 * @hide
5130 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005131 @UnsupportedAppUsage
Selim Cinek624c02db2015-12-14 21:00:02 -08005132 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005133 return makePublicView(false /* ambient */);
5134 }
5135
5136 /**
5137 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5138 *
5139 * @hide
5140 */
5141 public RemoteViews makePublicAmbientNotification() {
5142 return makePublicView(true /* ambient */);
5143 }
5144
5145 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005146 if (mN.publicVersion != null) {
5147 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005148 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005149 }
5150 Bundle savedBundle = mN.extras;
5151 Style style = mStyle;
5152 mStyle = null;
5153 Icon largeIcon = mN.mLargeIcon;
5154 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005155 Bitmap largeIconLegacy = mN.largeIcon;
5156 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005157 ArrayList<Action> actions = mActions;
5158 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005159 Bundle publicExtras = new Bundle();
5160 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5161 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5162 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5163 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005164 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5165 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005166 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5167 if (appName != null) {
5168 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5169 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005170 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005171 RemoteViews view;
5172 if (ambient) {
5173 publicExtras.putCharSequence(EXTRA_TITLE,
5174 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5175 view = makeAmbientNotification();
5176 } else{
5177 view = makeNotificationHeader(false /* ambient */);
5178 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5179 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005180 mN.extras = savedBundle;
5181 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005182 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005183 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005184 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005185 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005186 }
5187
Selim Cinek6743c0b2017-01-18 18:24:01 -08005188 /**
5189 * Construct a content view for the display when low - priority
5190 *
5191 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5192 * a new subtext is created consisting of the content of the
5193 * notification.
5194 * @hide
5195 */
5196 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5197 int color = mN.color;
5198 mN.color = COLOR_DEFAULT;
5199 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5200 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5201 CharSequence newSummary = createSummaryText();
5202 if (!TextUtils.isEmpty(newSummary)) {
5203 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5204 }
5205 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005206
Adrian Roos6f6e1592017-05-02 16:22:53 -07005207 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005208 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005209 if (summary != null) {
5210 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5211 } else {
5212 mN.extras.remove(EXTRA_SUB_TEXT);
5213 }
5214 mN.color = color;
5215 return header;
5216 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005217
Selim Cinek6743c0b2017-01-18 18:24:01 -08005218 private CharSequence createSummaryText() {
5219 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5220 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5221 return titleText;
5222 }
5223 SpannableStringBuilder summary = new SpannableStringBuilder();
5224 if (titleText == null) {
5225 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5226 }
5227 BidiFormatter bidi = BidiFormatter.getInstance();
5228 if (titleText != null) {
5229 summary.append(bidi.unicodeWrap(titleText));
5230 }
5231 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5232 if (titleText != null && contentText != null) {
5233 summary.append(bidi.unicodeWrap(mContext.getText(
5234 R.string.notification_header_divider_symbol_with_spaces)));
5235 }
5236 if (contentText != null) {
5237 summary.append(bidi.unicodeWrap(contentText));
5238 }
5239 return summary;
5240 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005241
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005242 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005243 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005244 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005245 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005246 emphazisedMode ? getEmphasizedActionLayoutResource()
5247 : tombstone ? getActionTombstoneLayoutResource()
5248 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005249 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005250 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005251 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005252 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005253 if (action.mRemoteInputs != null) {
5254 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5255 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005256 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005257 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005258 CharSequence title = action.title;
5259 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005260 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005261 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07005262 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005263 } else {
5264 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005265 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005266 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005267 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005268 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005269 int rippleColor;
5270 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5271 if (hasColorOverride) {
5272 // There's a span spanning the full text, let's take it and use it as the
5273 // background color
5274 background = outResultColor[0].getDefaultColor();
5275 int textColor = NotificationColorUtil.resolvePrimaryColor(mContext,
5276 background);
5277 button.setTextColor(R.id.action0, textColor);
5278 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005279 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005280 rippleColor = resolveContrastColor();
5281 button.setTextColor(R.id.action0, rippleColor);
5282 } else {
5283 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005284 }
Selim Cinek396caca2018-04-10 17:46:46 -07005285 // We only want about 20% alpha for the ripple
5286 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5287 button.setColorStateList(R.id.action0, "setRippleColor",
5288 ColorStateList.valueOf(rippleColor));
5289 button.setColorStateList(R.id.action0, "setButtonBackground",
5290 ColorStateList.valueOf(background));
5291 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005292 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005293 button.setTextViewText(R.id.action0, processTextSpans(
5294 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005295 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005296 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005297 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005298 button.setTextColor(R.id.action0,
5299 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005300 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005301 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005302 return button;
5303 }
5304
Joe Onoratocb109a02011-01-18 17:57:41 -08005305 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005306 * Ensures contrast on color spans against a background color. also returns the color of the
5307 * text if a span was found that spans over the whole text.
5308 *
5309 * @param charSequence the charSequence on which the spans are
5310 * @param background the background color to ensure the contrast against
5311 * @param outResultColor an array in which a color will be returned as the first element if
5312 * there exists a full length color span.
5313 * @return the contrasted charSequence
5314 */
5315 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5316 ColorStateList[] outResultColor) {
5317 if (charSequence instanceof Spanned) {
5318 Spanned ss = (Spanned) charSequence;
5319 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5320 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5321 for (Object span : spans) {
5322 Object resultSpan = span;
5323 int spanStart = ss.getSpanStart(span);
5324 int spanEnd = ss.getSpanEnd(span);
5325 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5326 if (resultSpan instanceof CharacterStyle) {
5327 resultSpan = ((CharacterStyle) span).getUnderlying();
5328 }
5329 if (resultSpan instanceof TextAppearanceSpan) {
5330 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5331 ColorStateList textColor = originalSpan.getTextColor();
5332 if (textColor != null) {
5333 int[] colors = textColor.getColors();
5334 int[] newColors = new int[colors.length];
5335 for (int i = 0; i < newColors.length; i++) {
5336 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005337 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005338 }
5339 textColor = new ColorStateList(textColor.getStates().clone(),
5340 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005341 if (fullLength) {
5342 outResultColor[0] = textColor;
5343 // Let's drop the color from the span
5344 textColor = null;
5345 }
Selim Cinek981962e2016-07-20 20:41:58 -07005346 resultSpan = new TextAppearanceSpan(
5347 originalSpan.getFamily(),
5348 originalSpan.getTextStyle(),
5349 originalSpan.getTextSize(),
5350 textColor,
5351 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005352 }
5353 } else if (resultSpan instanceof ForegroundColorSpan) {
5354 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5355 int foregroundColor = originalSpan.getForegroundColor();
5356 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005357 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005358 if (fullLength) {
5359 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005360 resultSpan = null;
5361 } else {
5362 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005363 }
5364 } else {
5365 resultSpan = span;
5366 }
Selim Cinek396caca2018-04-10 17:46:46 -07005367 if (resultSpan != null) {
5368 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5369 }
Selim Cinek981962e2016-07-20 20:41:58 -07005370 }
5371 return builder;
5372 }
5373 return charSequence;
5374 }
5375
5376 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005377 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005378 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005379 */
5380 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005381 if (!mIsLegacyInitialized) {
5382 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5383 < Build.VERSION_CODES.LOLLIPOP;
5384 mIsLegacyInitialized = true;
5385 }
5386 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005387 }
5388
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005389 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005390 return processLegacyText(charSequence, false /* ambient */);
5391 }
5392
5393 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5394 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5395 boolean wantLightText = ambient;
5396 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005397 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005398 } else {
5399 return charSequence;
5400 }
5401 }
5402
Dan Sandler26e81cf2014-05-06 10:01:27 -04005403 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005404 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005405 */
Adrian Roos487374f2017-01-11 15:48:14 -08005406 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5407 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005408 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005409 int color;
5410 if (ambient) {
5411 color = resolveAmbientColor();
5412 } else if (isColorized()) {
5413 color = getPrimaryTextColor();
5414 } else {
5415 color = resolveContrastColor();
5416 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005417 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005418 contentView.setDrawableTint(R.id.icon, false, color,
5419 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005420
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005421 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005422 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005423 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005424 }
5425
Dan Sandler26e81cf2014-05-06 10:01:27 -04005426 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005427 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005428 * if it's grayscale).
5429 */
5430 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005431 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5432 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005433 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005434 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005435 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5436 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005437 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005438 }
5439
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005440 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005441 if (mN.color != COLOR_DEFAULT) {
5442 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005443 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005444 }
5445
Adrian Roos4ff3b122016-02-01 12:26:13 -08005446 int resolveContrastColor() {
5447 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5448 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005449 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005450
Selim Cinekac5f0272017-05-02 16:05:41 -07005451 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005452 int background = mContext.getColor(
5453 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005454 if (mN.color == COLOR_DEFAULT) {
5455 ensureColors();
Selim Cinekc7f5a822018-03-20 19:32:06 -07005456 color = NotificationColorUtil.resolveDefaultColor(mContext, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005457 } else {
5458 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005459 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005460 }
5461 if (Color.alpha(color) < 255) {
5462 // alpha doesn't go well for color filters, so let's blend it manually
5463 color = NotificationColorUtil.compositeColors(color, background);
5464 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005465 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005466 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005467 }
5468
Selim Cinek4717d862018-04-19 09:19:15 +08005469 int resolveNeutralColor() {
5470 if (mNeutralColor != COLOR_INVALID) {
5471 return mNeutralColor;
5472 }
5473 int background = mContext.getColor(
5474 com.android.internal.R.color.notification_material_background_color);
5475 mNeutralColor = NotificationColorUtil.resolveDefaultColor(mContext, background);
5476 if (Color.alpha(mNeutralColor) < 255) {
5477 // alpha doesn't go well for color filters, so let's blend it manually
5478 mNeutralColor = NotificationColorUtil.compositeColors(mNeutralColor, background);
5479 }
5480 return mNeutralColor;
5481 }
5482
Adrian Roos487374f2017-01-11 15:48:14 -08005483 int resolveAmbientColor() {
5484 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5485 return mCachedAmbientColor;
5486 }
5487 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5488
5489 mCachedAmbientColorIsFor = mN.color;
5490 return mCachedAmbientColor = contrasted;
5491 }
5492
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005493 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005494 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005495 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005496 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005497 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005498 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005499 mN.actions = new Action[mActions.size()];
5500 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005501 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005502 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005503 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005504 }
Selim Cinek247fa012016-02-18 09:50:48 -08005505 if (mN.bigContentView != null || mN.contentView != null
5506 || mN.headsUpContentView != null) {
5507 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5508 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005509 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005510 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005511
Julia Reynolds3b848122016-02-26 10:45:32 -05005512 /**
5513 * Creates a Builder from an existing notification so further changes can be made.
5514 * @param context The context for your application / activity.
5515 * @param n The notification to create a Builder from.
5516 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005517 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005518 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005519 ApplicationInfo applicationInfo = n.extras.getParcelable(
5520 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005521 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005522 if (applicationInfo != null) {
5523 try {
5524 builderContext = context.createApplicationContext(applicationInfo,
5525 Context.CONTEXT_RESTRICTED);
5526 } catch (NameNotFoundException e) {
5527 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5528 builderContext = context; // try with our context
5529 }
5530 } else {
5531 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005532 }
5533
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005534 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005535 }
5536
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005537 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005538 * @deprecated Use {@link #build()} instead.
5539 */
5540 @Deprecated
5541 public Notification getNotification() {
5542 return build();
5543 }
5544
5545 /**
5546 * Combine all of the options that have been set and return a new {@link Notification}
5547 * object.
5548 */
5549 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005550 // first, add any extras from the calling code
5551 if (mUserExtras != null) {
5552 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005553 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005554
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005555 mN.creationTime = System.currentTimeMillis();
5556
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005557 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005558 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005559
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005560 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005561
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005562 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005563 mStyle.reduceImageSizes(mContext);
5564 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005565 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005566 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005567 }
Selim Cinekd0426622017-07-11 13:19:59 +02005568 mN.reduceImageSizes(mContext);
5569
Adrian Roos5081c0d2016-02-26 16:04:19 -08005570 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005571 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005572 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005573 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005574 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5575 mN.contentView.getSequenceNumber());
5576 }
5577 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005578 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005579 if (mN.bigContentView != null) {
5580 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5581 mN.bigContentView.getSequenceNumber());
5582 }
5583 }
5584 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005585 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005586 if (mN.headsUpContentView != null) {
5587 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5588 mN.headsUpContentView.getSequenceNumber());
5589 }
5590 }
5591 }
5592
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005593 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5594 mN.flags |= FLAG_SHOW_LIGHTS;
5595 }
5596
Adrian Roosfb921842017-10-26 14:49:56 +02005597 mN.allPendingIntents = null;
5598
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005599 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005600 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005601
5602 /**
5603 * Apply this Builder to an existing {@link Notification} object.
5604 *
5605 * @hide
5606 */
5607 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005608 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005609 return n;
5610 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005611
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005612 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005613 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005614 * change. Also removes extenders on low ram devices, as
5615 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005616 *
5617 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5618 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005619 * @hide
5620 */
Kristian Monsen30f59b22018-04-09 10:27:16 +02005621 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5622 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005623 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005624
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005625 // Only strip views for known Styles because we won't know how to
5626 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005627 if (!isLowRam
5628 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005629 && getNotificationStyleClass(templateClass) == null) {
5630 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005631 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005632
5633 // Only strip unmodified BuilderRemoteViews.
5634 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005635 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005636 n.contentView.getSequenceNumber();
5637 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005638 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005639 n.bigContentView.getSequenceNumber();
5640 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005641 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005642 n.headsUpContentView.getSequenceNumber();
5643
5644 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005645 if (!isLowRam
5646 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005647 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005648 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005649
5650 Notification clone = n.clone();
5651 if (stripContentView) {
5652 clone.contentView = null;
5653 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5654 }
5655 if (stripBigContentView) {
5656 clone.bigContentView = null;
5657 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5658 }
5659 if (stripHeadsUpContentView) {
5660 clone.headsUpContentView = null;
5661 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5662 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005663 if (isLowRam) {
Kristian Monsen30f59b22018-04-09 10:27:16 +02005664 String[] allowedServices = context.getResources().getStringArray(
5665 R.array.config_allowedManagedServicesOnLowRamDevices);
5666 if (allowedServices.length == 0) {
5667 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5668 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5669 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5670 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005671 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005672 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005673 }
5674
Mathew Inwood4fb17d12018-08-14 14:25:44 +01005675 @UnsupportedAppUsage
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005676 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005677 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005678 }
5679
5680 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005681 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005682 }
5683
5684 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005685 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005686 }
5687
5688 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005689 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005690 }
5691
5692 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005693 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005694 }
5695
Adrian Roosc1a80b02016-04-05 14:54:55 -07005696 private int getMessagingLayoutResource() {
5697 return R.layout.notification_template_material_messaging;
5698 }
5699
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005700 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005701 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005702 }
5703
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005704 private int getEmphasizedActionLayoutResource() {
5705 return R.layout.notification_material_action_emphasized;
5706 }
5707
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005708 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005709 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005710 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005711
5712 private int getBackgroundColor() {
5713 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005714 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005715 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005716 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005717 }
5718 }
5719
Selim Cinek396caca2018-04-10 17:46:46 -07005720 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005721 * Gets a neutral color that can be used for icons or similar that should not stand out.
5722 */
5723 private int getNeutralColor() {
5724 if (isColorized()) {
5725 return getSecondaryTextColor();
5726 } else {
5727 return resolveNeutralColor();
5728 }
5729 }
5730
5731 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005732 * Same as getBackgroundColor but also resolved the default color to the background.
5733 */
5734 private int resolveBackgroundColor() {
5735 int backgroundColor = getBackgroundColor();
5736 if (backgroundColor == COLOR_DEFAULT) {
5737 backgroundColor = mContext.getColor(
5738 com.android.internal.R.color.notification_material_background_color);
5739 }
5740 return backgroundColor;
5741 }
5742
Selim Cinek7b9605b2017-01-19 17:36:00 -08005743 private boolean isColorized() {
5744 return mN.isColorized();
5745 }
Selim Cinek99104832017-01-25 14:47:33 -08005746
Anthony Chenad4d1582017-04-10 16:07:58 -07005747 private boolean shouldTintActionButtons() {
5748 return mTintActionButtons;
5749 }
5750
Selim Cinek99104832017-01-25 14:47:33 -08005751 private boolean textColorsNeedInversion() {
5752 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5753 return false;
5754 }
5755 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5756 return targetSdkVersion > Build.VERSION_CODES.M
5757 && targetSdkVersion < Build.VERSION_CODES.O;
5758 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005759
5760 /**
5761 * Set a color palette to be used as the background and textColors
5762 *
5763 * @param backgroundColor the color to be used as the background
5764 * @param foregroundColor the color to be used as the foreground
5765 *
5766 * @hide
5767 */
5768 public void setColorPalette(int backgroundColor, int foregroundColor) {
5769 mBackgroundColor = backgroundColor;
5770 mForegroundColor = foregroundColor;
5771 mTextColorsAreForBackground = COLOR_INVALID;
5772 ensureColors();
5773 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005774
5775 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005776 * Forces all styled remoteViews to be built from scratch and not use any cached
5777 * RemoteViews.
5778 * This is needed for legacy apps that are baking in their remoteviews into the
5779 * notification.
5780 *
5781 * @hide
5782 */
5783 public void setRebuildStyledRemoteViews(boolean rebuild) {
5784 mRebuildStyledRemoteViews = rebuild;
5785 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005786
5787 /**
5788 * Get the text that should be displayed in the statusBar when heads upped. This is
5789 * usually just the app name, but may be different depending on the style.
5790 *
5791 * @param publicMode If true, return a text that is safe to display in public.
5792 *
5793 * @hide
5794 */
5795 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5796 if (mStyle != null && !publicMode) {
5797 CharSequence text = mStyle.getHeadsUpStatusBarText();
5798 if (!TextUtils.isEmpty(text)) {
5799 return text;
5800 }
5801 }
5802 return loadHeaderAppName();
5803 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005804 }
5805
5806 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005807 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5808 * remote views.
5809 *
5810 * @hide
5811 */
5812 void reduceImageSizes(Context context) {
5813 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5814 return;
5815 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005816 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005817 if (mLargeIcon != null || largeIcon != null) {
5818 Resources resources = context.getResources();
5819 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005820 int maxWidth = resources.getDimensionPixelSize(isLowRam
5821 ? R.dimen.notification_right_icon_size_low_ram
5822 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005823 int maxHeight = maxWidth;
5824 if (MediaStyle.class.equals(style)
5825 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005826 maxHeight = resources.getDimensionPixelSize(isLowRam
5827 ? R.dimen.notification_media_image_max_height_low_ram
5828 : R.dimen.notification_media_image_max_height);
5829 maxWidth = resources.getDimensionPixelSize(isLowRam
5830 ? R.dimen.notification_media_image_max_width_low_ram
5831 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005832 }
5833 if (mLargeIcon != null) {
5834 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5835 }
5836 if (largeIcon != null) {
5837 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5838 }
5839 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005840 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5841 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5842 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005843 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5844 }
5845
Selim Cineka8cb1262017-08-15 16:53:44 -07005846 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5847 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005848 if (remoteView != null) {
5849 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005850 int maxWidth = resources.getDimensionPixelSize(isLowRam
5851 ? R.dimen.notification_custom_view_max_image_width_low_ram
5852 : R.dimen.notification_custom_view_max_image_width);
5853 int maxHeight = resources.getDimensionPixelSize(isLowRam
5854 ? R.dimen.notification_custom_view_max_image_height_low_ram
5855 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005856 remoteView.reduceImageSizes(maxWidth, maxHeight);
5857 }
5858 }
5859
5860 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005861 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005862 */
Selim Cinek22714f12017-04-13 16:23:53 -07005863 private boolean isForegroundService() {
5864 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005865 }
5866
5867 /**
Selim Cinek99104832017-01-25 14:47:33 -08005868 * @return whether this notification has a media session attached
5869 * @hide
5870 */
5871 public boolean hasMediaSession() {
5872 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5873 }
5874
5875 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005876 * @return the style class of this notification
5877 * @hide
5878 */
5879 public Class<? extends Notification.Style> getNotificationStyle() {
5880 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5881
5882 if (!TextUtils.isEmpty(templateClass)) {
5883 return Notification.getNotificationStyleClass(templateClass);
5884 }
5885 return null;
5886 }
5887
5888 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005889 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005890 *
5891 * @hide
5892 */
5893 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005894 if (isColorizedMedia()) {
5895 return true;
5896 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005897 return extras.getBoolean(EXTRA_COLORIZED)
5898 && (hasColorizedPermission() || isForegroundService());
5899 }
5900
5901 /**
5902 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5903 * permission. The permission is checked when a notification is enqueued.
5904 */
5905 private boolean hasColorizedPermission() {
5906 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005907 }
5908
5909 /**
5910 * @return true if this notification is colorized and it is a media notification
5911 *
5912 * @hide
5913 */
5914 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005915 Class<? extends Style> style = getNotificationStyle();
5916 if (MediaStyle.class.equals(style)) {
5917 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5918 if ((colorized == null || colorized) && hasMediaSession()) {
5919 return true;
5920 }
5921 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5922 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5923 return true;
5924 }
5925 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005926 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005927 }
5928
Selim Cinek0847acd2017-04-24 19:48:29 -07005929
5930 /**
5931 * @return true if this is a media notification
5932 *
5933 * @hide
5934 */
5935 public boolean isMediaNotification() {
5936 Class<? extends Style> style = getNotificationStyle();
5937 if (MediaStyle.class.equals(style)) {
5938 return true;
5939 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5940 return true;
5941 }
5942 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005943 }
5944
Selim Cinek279fa862016-06-14 10:57:25 -07005945 private boolean hasLargeIcon() {
5946 return mLargeIcon != null || largeIcon != null;
5947 }
5948
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005949 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005950 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005951 * @hide
5952 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005953 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005954 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5955 }
5956
5957 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005958 * @return true if the notification will show a chronometer; false otherwise
5959 * @hide
5960 */
5961 public boolean showsChronometer() {
5962 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5963 }
5964
5965 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005966 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005967 */
5968 @SystemApi
5969 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5970 Class<? extends Style>[] classes = new Class[] {
5971 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5972 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5973 MessagingStyle.class };
5974 for (Class<? extends Style> innerClass : classes) {
5975 if (templateClass.equals(innerClass.getName())) {
5976 return innerClass;
5977 }
5978 }
5979 return null;
5980 }
5981
5982 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005983 * An object that can apply a rich notification style to a {@link Notification.Builder}
5984 * object.
5985 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005986 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07005987
5988 /**
5989 * The number of items allowed simulatanously in the remote input history.
5990 * @hide
5991 */
5992 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04005993 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005994
5995 /**
5996 * @hide
5997 */
5998 protected CharSequence mSummaryText = null;
5999
6000 /**
6001 * @hide
6002 */
6003 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04006004
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006005 protected Builder mBuilder;
6006
Chris Wrend6297db2012-05-03 16:20:13 -04006007 /**
6008 * Overrides ContentTitle in the big form of the template.
6009 * This defaults to the value passed to setContentTitle().
6010 */
6011 protected void internalSetBigContentTitle(CharSequence title) {
6012 mBigContentTitle = title;
6013 }
6014
6015 /**
6016 * Set the first line of text after the detail section in the big form of the template.
6017 */
6018 protected void internalSetSummaryText(CharSequence cs) {
6019 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006020 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006021 }
6022
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006023 public void setBuilder(Builder builder) {
6024 if (mBuilder != builder) {
6025 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006026 if (mBuilder != null) {
6027 mBuilder.setStyle(this);
6028 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006029 }
6030 }
6031
Chris Wrend6297db2012-05-03 16:20:13 -04006032 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006033 if (mBuilder == null) {
6034 throw new IllegalArgumentException("Style requires a valid Builder object");
6035 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006036 }
Chris Wrend6297db2012-05-03 16:20:13 -04006037
6038 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006039 return getStandardView(layoutId, null);
6040 }
6041
6042 /**
6043 * Get the standard view for this style.
6044 *
6045 * @param layoutId The layout id to use
6046 * @param result The result where template bind information is saved.
6047 * @return A remoteView for this style.
6048 * @hide
6049 */
6050 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006051 checkBuilder();
6052
Christoph Studer4600f9b2014-07-22 22:44:43 +02006053 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006054 CharSequence oldBuilderContentTitle =
6055 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006056 if (mBigContentTitle != null) {
6057 mBuilder.setContentTitle(mBigContentTitle);
6058 }
6059
Selim Cinek384804b2018-04-18 14:31:07 +08006060 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006061
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006062 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006063
Chris Wrend6297db2012-05-03 16:20:13 -04006064 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6065 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006066 } else {
6067 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006068 }
6069
Chris Wrend6297db2012-05-03 16:20:13 -04006070 return contentView;
6071 }
6072
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006073 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006074 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006075 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006076 *
6077 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006078 * @hide
6079 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006080 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006081 return null;
6082 }
6083
6084 /**
6085 * Construct a Style-specific RemoteViews for the final big notification layout.
6086 * @hide
6087 */
6088 public RemoteViews makeBigContentView() {
6089 return null;
6090 }
6091
6092 /**
6093 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006094 *
6095 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006096 * @hide
6097 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006098 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006099 return null;
6100 }
6101
6102 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006103 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006104 * @hide
6105 */
6106 public void addExtras(Bundle extras) {
6107 if (mSummaryTextSet) {
6108 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6109 }
6110 if (mBigContentTitle != null) {
6111 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6112 }
Chris Wren91ad5632013-06-05 15:05:57 -04006113 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006114 }
6115
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006116 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006117 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006118 * @hide
6119 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006120 protected void restoreFromExtras(Bundle extras) {
6121 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6122 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6123 mSummaryTextSet = true;
6124 }
6125 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6126 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6127 }
6128 }
6129
6130
6131 /**
6132 * @hide
6133 */
6134 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006135 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006136 return wip;
6137 }
6138
Daniel Sandler0ec46202015-06-24 01:27:05 -04006139 /**
6140 * @hide
6141 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006142 public void purgeResources() {}
6143
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006144 /**
6145 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6146 * attached to.
6147 *
6148 * @return the fully constructed Notification.
6149 */
6150 public Notification build() {
6151 checkBuilder();
6152 return mBuilder.build();
6153 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006154
6155 /**
6156 * @hide
6157 * @return true if the style positions the progress bar on the second line; false if the
6158 * style hides the progress bar
6159 */
6160 protected boolean hasProgress() {
6161 return true;
6162 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006163
6164 /**
6165 * @hide
6166 * @return Whether we should put the summary be put into the notification header
6167 */
6168 public boolean hasSummaryInHeader() {
6169 return true;
6170 }
Selim Cinek593610c2016-02-16 18:42:57 -08006171
6172 /**
6173 * @hide
6174 * @return Whether custom content views are displayed inline in the style
6175 */
6176 public boolean displayCustomViewInline() {
6177 return false;
6178 }
Selim Cinekd0426622017-07-11 13:19:59 +02006179
6180 /**
6181 * Reduces the image sizes contained in this style.
6182 *
6183 * @hide
6184 */
6185 public void reduceImageSizes(Context context) {
6186 }
Selim Cinek90343862018-02-01 11:07:11 -08006187
6188 /**
6189 * Validate that this style was properly composed. This is called at build time.
6190 * @hide
6191 */
6192 public void validate(Context context) {
6193 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006194
6195 /**
6196 * @hide
6197 */
6198 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006199
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006200 /**
koprivaa1a78482018-10-09 10:09:23 -07006201 * @return the text that should be displayed in the statusBar when heads-upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006202 * If {@code null} is returned, the default implementation will be used.
6203 *
6204 * @hide
6205 */
6206 public CharSequence getHeadsUpStatusBarText() {
6207 return null;
6208 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006209 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006210
6211 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006212 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006213 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006214 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006215 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006216 * Notification notif = new Notification.Builder(mContext)
6217 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6218 * .setContentText(subject)
6219 * .setSmallIcon(R.drawable.new_post)
6220 * .setLargeIcon(aBitmap)
6221 * .setStyle(new Notification.BigPictureStyle()
6222 * .bigPicture(aBigBitmap))
6223 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006224 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006225 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006226 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006227 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006228 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006229 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006230 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006231 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006232
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006233 public BigPictureStyle() {
6234 }
6235
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006236 /**
6237 * @deprecated use {@code BigPictureStyle()}.
6238 */
6239 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006240 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006241 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006242 }
6243
Chris Wrend6297db2012-05-03 16:20:13 -04006244 /**
6245 * Overrides ContentTitle in the big form of the template.
6246 * This defaults to the value passed to setContentTitle().
6247 */
6248 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006249 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006250 return this;
6251 }
6252
6253 /**
6254 * Set the first line of text after the detail section in the big form of the template.
6255 */
6256 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006257 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006258 return this;
6259 }
6260
Chris Wren0bd664d2012-08-01 13:56:56 -04006261 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006262 * @hide
6263 */
6264 public Bitmap getBigPicture() {
6265 return mPicture;
6266 }
6267
6268 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006269 * Provide the bitmap to be used as the payload for the BigPicture notification.
6270 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006271 public BigPictureStyle bigPicture(Bitmap b) {
6272 mPicture = b;
6273 return this;
6274 }
6275
Chris Wren3745a3d2012-05-22 15:11:52 -04006276 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006277 * Override the large icon when the big notification is shown.
6278 */
6279 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006280 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6281 }
6282
6283 /**
6284 * Override the large icon when the big notification is shown.
6285 */
6286 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006287 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006288 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006289 return this;
6290 }
6291
Riley Andrews0394a0c2015-11-03 23:36:52 -08006292 /** @hide */
6293 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6294
Daniel Sandler0ec46202015-06-24 01:27:05 -04006295 /**
6296 * @hide
6297 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006298 @Override
6299 public void purgeResources() {
6300 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006301 if (mPicture != null &&
6302 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006303 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006304 mPicture = mPicture.createAshmemBitmap();
6305 }
6306 if (mBigLargeIcon != null) {
6307 mBigLargeIcon.convertToAshmem();
6308 }
6309 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006310
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006311 /**
6312 * @hide
6313 */
Selim Cinekd0426622017-07-11 13:19:59 +02006314 @Override
6315 public void reduceImageSizes(Context context) {
6316 super.reduceImageSizes(context);
6317 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006318 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006319 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006320 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6321 ? R.dimen.notification_big_picture_max_height_low_ram
6322 : R.dimen.notification_big_picture_max_height);
6323 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6324 ? R.dimen.notification_big_picture_max_width_low_ram
6325 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006326 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6327 }
6328 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006329 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6330 ? R.dimen.notification_right_icon_size_low_ram
6331 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006332 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6333 }
6334 }
6335
6336 /**
6337 * @hide
6338 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006339 public RemoteViews makeBigContentView() {
6340 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006341 // This covers the following cases:
6342 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006343 // mN.mLargeIcon
6344 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006345 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006346 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006347 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006348 oldLargeIcon = mBuilder.mN.mLargeIcon;
6349 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006350 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6351 // replacement if the other one is null. Because we're restoring these legacy icons
6352 // for old listeners, this is in general non-null.
6353 largeIconLegacy = mBuilder.mN.largeIcon;
6354 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006355 }
6356
Selim Cinek384804b2018-04-18 14:31:07 +08006357 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6358 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006359 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006360 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6361 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006362 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006363 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006364 }
Selim Cinek279fa862016-06-14 10:57:25 -07006365 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006366
Christoph Studer5c510ee2014-12-15 16:32:27 +01006367 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006368 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006369 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006370 }
6371
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006372 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006373 return contentView;
6374 }
6375
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006376 /**
6377 * @hide
6378 */
6379 public void addExtras(Bundle extras) {
6380 super.addExtras(extras);
6381
6382 if (mBigLargeIconSet) {
6383 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6384 }
6385 extras.putParcelable(EXTRA_PICTURE, mPicture);
6386 }
6387
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006388 /**
6389 * @hide
6390 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006391 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006392 protected void restoreFromExtras(Bundle extras) {
6393 super.restoreFromExtras(extras);
6394
6395 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006396 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006397 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006398 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006399 mPicture = extras.getParcelable(EXTRA_PICTURE);
6400 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006401
6402 /**
6403 * @hide
6404 */
6405 @Override
6406 public boolean hasSummaryInHeader() {
6407 return false;
6408 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006409
6410 /**
6411 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006412 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6413 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006414 */
6415 @Override
6416 public boolean areNotificationsVisiblyDifferent(Style other) {
6417 if (other == null || getClass() != other.getClass()) {
6418 return true;
6419 }
6420 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006421 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6422 }
6423
6424 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6425 if (a == b) {
6426 return false;
6427 }
6428 if (a == null || b == null) {
6429 return true;
6430 }
6431 return a.getWidth() != b.getWidth()
6432 || a.getHeight() != b.getHeight()
6433 || a.getConfig() != b.getConfig()
6434 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006435 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006436 }
6437
6438 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006439 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006440 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006441 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006442 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006443 * Notification notif = new Notification.Builder(mContext)
6444 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6445 * .setContentText(subject)
6446 * .setSmallIcon(R.drawable.new_mail)
6447 * .setLargeIcon(aBitmap)
6448 * .setStyle(new Notification.BigTextStyle()
6449 * .bigText(aVeryLongString))
6450 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006451 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006452 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006453 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006454 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006455 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006456
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006457 private CharSequence mBigText;
6458
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006459 public BigTextStyle() {
6460 }
6461
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006462 /**
6463 * @deprecated use {@code BigTextStyle()}.
6464 */
6465 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006466 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006467 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006468 }
6469
Chris Wrend6297db2012-05-03 16:20:13 -04006470 /**
6471 * Overrides ContentTitle in the big form of the template.
6472 * This defaults to the value passed to setContentTitle().
6473 */
6474 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006475 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006476 return this;
6477 }
6478
6479 /**
6480 * Set the first line of text after the detail section in the big form of the template.
6481 */
6482 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006483 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006484 return this;
6485 }
6486
Chris Wren0bd664d2012-08-01 13:56:56 -04006487 /**
6488 * Provide the longer text to be displayed in the big form of the
6489 * template in place of the content text.
6490 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006491 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006492 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006493 return this;
6494 }
6495
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006496 /**
6497 * @hide
6498 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006499 public CharSequence getBigText() {
6500 return mBigText;
6501 }
6502
6503 /**
6504 * @hide
6505 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006506 public void addExtras(Bundle extras) {
6507 super.addExtras(extras);
6508
Christoph Studer4600f9b2014-07-22 22:44:43 +02006509 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6510 }
6511
6512 /**
6513 * @hide
6514 */
6515 @Override
6516 protected void restoreFromExtras(Bundle extras) {
6517 super.restoreFromExtras(extras);
6518
6519 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006520 }
6521
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006522 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006523 * @param increasedHeight true if this layout be created with an increased height.
6524 *
6525 * @hide
6526 */
6527 @Override
6528 public RemoteViews makeContentView(boolean increasedHeight) {
6529 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006530 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006531 mBuilder.mActions = new ArrayList<>();
6532 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006533 mBuilder.mActions = mBuilder.mOriginalActions;
6534 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006535 return remoteViews;
6536 }
6537 return super.makeContentView(increasedHeight);
6538 }
6539
6540 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006541 * @hide
6542 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006543 @Override
6544 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6545 if (increasedHeight && mBuilder.mActions.size() > 0) {
6546 return makeBigContentView();
6547 }
6548 return super.makeHeadsUpContentView(increasedHeight);
6549 }
6550
6551 /**
6552 * @hide
6553 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006554 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006555
6556 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006557 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006558 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006559
Selim Cinek384804b2018-04-18 14:31:07 +08006560 TemplateBindResult result = new TemplateBindResult();
6561 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006562 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006563
Selim Cinek75998782016-04-26 10:39:17 -07006564 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006565
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006566 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006567 if (TextUtils.isEmpty(bigTextText)) {
6568 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6569 // experience
6570 bigTextText = mBuilder.processLegacyText(text);
6571 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006572 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006573
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006574 return contentView;
6575 }
6576
Julia Reynolds7217dc92018-03-07 12:12:09 -05006577 /**
6578 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006579 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006580 */
6581 @Override
6582 public boolean areNotificationsVisiblyDifferent(Style other) {
6583 if (other == null || getClass() != other.getClass()) {
6584 return true;
6585 }
6586 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006587 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006588 }
6589
Adrian Roosb1f427c2016-05-26 12:27:15 -07006590 static void applyBigTextContentView(Builder builder,
6591 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006592 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006593 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006594 contentView.setViewVisibility(R.id.big_text,
6595 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006596 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006597 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006598 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006599
6600 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006601 * Helper class for generating large-format notifications that include multiple back-and-forth
6602 * messages of varying types between any number of people.
6603 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006604 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006605 * If the platform does not provide large-format notifications, this method has no effect. The
6606 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006607 *
6608 * <p>
6609 * If the app is targeting Android P and above, it is required to use the {@link Person}
6610 * class in order to get an optimal rendering of the notification and its avatars. For
6611 * conversations involving multiple people, the app should also make sure that it marks the
6612 * conversation as a group with {@link #setGroupConversation(boolean)}.
6613 *
6614 * <p>
6615 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6616 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006617 * <pre class="prettyprint">
6618 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006619 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6620 * MessagingStyle style = new MessagingStyle(user)
6621 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6622 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6623 * .setGroupConversation(hasMultiplePeople());
6624 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006625 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006626 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006627 * .setContentText(subject)
6628 * .setSmallIcon(R.drawable.new_message)
6629 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006630 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006631 * .build();
6632 * </pre>
6633 */
6634 public static class MessagingStyle extends Style {
6635
6636 /**
6637 * The maximum number of messages that will be retained in the Notification itself (the
6638 * number displayed is up to the platform).
6639 */
6640 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6641
Selim Cinekcb8b9852017-12-15 18:01:52 -08006642 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006643 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006644 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006645 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006646 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006647
6648 MessagingStyle() {
6649 }
6650
6651 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006652 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6653 * user before the posting app reposts the notification with those messages after they've
6654 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006655 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006656 *
6657 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006658 */
Alex Hillsfd590442016-10-07 09:52:44 -04006659 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006660 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006661 }
6662
6663 /**
6664 * @param user Required - The person displayed for any messages that are sent by the
6665 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6666 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006667 * by this user. The user also needs to have a valid name associated with it, which will
6668 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006669 */
6670 public MessagingStyle(@NonNull Person user) {
6671 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006672 }
6673
6674 /**
6675 * Validate that this style was properly composed. This is called at build time.
6676 * @hide
6677 */
6678 @Override
6679 public void validate(Context context) {
6680 super.validate(context);
6681 if (context.getApplicationInfo().targetSdkVersion
6682 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6683 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006684 }
6685 }
6686
6687 /**
koprivaa1a78482018-10-09 10:09:23 -07006688 * @return the text that should be displayed in the statusBar when heads upped.
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006689 * If {@code null} is returned, the default implementation will be used.
6690 *
6691 * @hide
6692 */
6693 @Override
6694 public CharSequence getHeadsUpStatusBarText() {
6695 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6696 ? super.mBigContentTitle
6697 : mConversationTitle;
6698 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6699 return conversationTitle;
6700 }
6701 return null;
6702 }
6703
6704 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006705 * @return the user to be displayed for any replies sent by the user
6706 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006707 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006708 public Person getUser() {
6709 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006710 }
6711
6712 /**
6713 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006714 *
6715 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006716 */
6717 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006718 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006719 }
6720
6721 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006722 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6723 *
Kodlee Yin14656422017-12-22 17:00:46 -08006724 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6725 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6726 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6727 * {@code true} and passing {@code null} will make it return {@code false}. In
6728 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6729 * to set group conversation status.
6730 *
6731 * @param conversationTitle Title displayed for this conversation
6732 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006733 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006734 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 mConversationTitle = conversationTitle;
6736 return this;
6737 }
6738
6739 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006740 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006741 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006742 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006743 public CharSequence getConversationTitle() {
6744 return mConversationTitle;
6745 }
6746
6747 /**
6748 * Adds a message for display by this notification. Convenience call for a simple
6749 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6750 * @param text A {@link CharSequence} to be displayed as the message content
6751 * @param timestamp Time at which the message arrived
6752 * @param sender A {@link CharSequence} to be used for displaying the name of the
6753 * sender. Should be <code>null</code> for messages by the current user, in which case
6754 * the platform will insert {@link #getUserDisplayName()}.
6755 * Should be unique amongst all individuals in the conversation, and should be
6756 * consistent during re-posts of the notification.
6757 *
6758 * @see Message#Message(CharSequence, long, CharSequence)
6759 *
6760 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006761 *
6762 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006763 */
6764 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006765 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006766 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006767 }
6768
6769 /**
6770 * Adds a message for display by this notification. Convenience call for a simple
6771 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6772 * @param text A {@link CharSequence} to be displayed as the message content
6773 * @param timestamp Time at which the message arrived
6774 * @param sender The {@link Person} who sent the message.
6775 * Should be <code>null</code> for messages by the current user, in which case
6776 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6777 *
6778 * @see Message#Message(CharSequence, long, CharSequence)
6779 *
6780 * @return this object for method chaining
6781 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006782 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6783 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006784 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006785 }
6786
6787 /**
6788 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006789 *
6790 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6791 * the newest last.
6792 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006793 * @param message The {@link Message} to be displayed
6794 * @return this object for method chaining
6795 */
6796 public MessagingStyle addMessage(Message message) {
6797 mMessages.add(message);
6798 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6799 mMessages.remove(0);
6800 }
6801 return this;
6802 }
6803
6804 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006805 * Adds a {@link Message} for historic context in this notification.
6806 *
6807 * <p>Messages should be added as historic if they are not the main subject of the
6808 * notification but may give context to a conversation. The system may choose to present
6809 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6810 *
6811 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6812 * the newest last.
6813 *
6814 * @param message The historic {@link Message} to be added
6815 * @return this object for method chaining
6816 */
6817 public MessagingStyle addHistoricMessage(Message message) {
6818 mHistoricMessages.add(message);
6819 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6820 mHistoricMessages.remove(0);
6821 }
6822 return this;
6823 }
6824
6825 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006826 * Gets the list of {@code Message} objects that represent the notification
6827 */
6828 public List<Message> getMessages() {
6829 return mMessages;
6830 }
6831
6832 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006833 * Gets the list of historic {@code Message}s in the notification.
6834 */
6835 public List<Message> getHistoricMessages() {
6836 return mHistoricMessages;
6837 }
6838
6839 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006840 * Sets whether this conversation notification represents a group. If the app is targeting
6841 * Android P, this is required if the app wants to display the largeIcon set with
6842 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006843 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006844 * @param isGroupConversation {@code true} if the conversation represents a group,
6845 * {@code false} otherwise.
6846 * @return this object for method chaining
6847 */
6848 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6849 mIsGroupConversation = isGroupConversation;
6850 return this;
6851 }
6852
6853 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006854 * Returns {@code true} if this notification represents a group conversation, otherwise
6855 * {@code false}.
6856 *
6857 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6858 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6859 * not the conversation title is set; returning {@code true} if the conversation title is
6860 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6861 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6862 * named, non-group conversations.
6863 *
6864 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006865 */
6866 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006867 // When target SDK version is < P, a non-null conversation title dictates if this is
6868 // as group conversation.
6869 if (mBuilder != null
6870 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6871 < Build.VERSION_CODES.P) {
6872 return mConversationTitle != null;
6873 }
6874
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006875 return mIsGroupConversation;
6876 }
6877
6878 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006879 * @hide
6880 */
6881 @Override
6882 public void addExtras(Bundle extras) {
6883 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006884 if (mUser != null) {
6885 // For legacy usages
6886 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6887 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006888 }
6889 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006890 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006891 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006892 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6893 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006894 }
Adrian Roos437cd562017-01-18 15:47:03 -08006895 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6896 Message.getBundleArrayForMessages(mHistoricMessages));
6897 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006898
6899 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006900 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006901 }
6902
6903 private void fixTitleAndTextExtras(Bundle extras) {
6904 Message m = findLatestIncomingMessage();
6905 CharSequence text = (m == null) ? null : m.mText;
6906 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006907 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6908 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006909 CharSequence title;
6910 if (!TextUtils.isEmpty(mConversationTitle)) {
6911 if (!TextUtils.isEmpty(sender)) {
6912 BidiFormatter bidi = BidiFormatter.getInstance();
6913 title = mBuilder.mContext.getString(
6914 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006915 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006916 } else {
6917 title = mConversationTitle;
6918 }
6919 } else {
6920 title = sender;
6921 }
6922
6923 if (title != null) {
6924 extras.putCharSequence(EXTRA_TITLE, title);
6925 }
6926 if (text != null) {
6927 extras.putCharSequence(EXTRA_TEXT, text);
6928 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006929 }
6930
6931 /**
6932 * @hide
6933 */
6934 @Override
6935 protected void restoreFromExtras(Bundle extras) {
6936 super.restoreFromExtras(extras);
6937
Selim Cinekcb8b9852017-12-15 18:01:52 -08006938 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6939 if (mUser == null) {
6940 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07006941 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08006942 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006943 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006944 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006945 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006946 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006947 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006948 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006949 }
6950
6951 /**
6952 * @hide
6953 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006954 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006955 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006956 mBuilder.mOriginalActions = mBuilder.mActions;
6957 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006958 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006959 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006960 mBuilder.mActions = mBuilder.mOriginalActions;
6961 mBuilder.mOriginalActions = null;
6962 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006963 }
6964
Julia Reynolds7217dc92018-03-07 12:12:09 -05006965 /**
6966 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006967 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006968 */
6969 @Override
6970 public boolean areNotificationsVisiblyDifferent(Style other) {
6971 if (other == null || getClass() != other.getClass()) {
6972 return true;
6973 }
6974 MessagingStyle newS = (MessagingStyle) other;
6975 List<MessagingStyle.Message> oldMs = getMessages();
6976 List<MessagingStyle.Message> newMs = newS.getMessages();
6977
Dan Sandler7d67bd42018-05-15 14:06:38 -04006978 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006979 newMs = new ArrayList<>();
6980 }
6981
6982 int n = oldMs.size();
6983 if (n != newMs.size()) {
6984 return true;
6985 }
6986 for (int i = 0; i < n; i++) {
6987 MessagingStyle.Message oldM = oldMs.get(i);
6988 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04006989 if (!Objects.equals(
6990 String.valueOf(oldM.getText()),
6991 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006992 return true;
6993 }
6994 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
6995 return true;
6996 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04006997 String oldSender = String.valueOf(oldM.getSenderPerson() == null
6998 ? oldM.getSender()
6999 : oldM.getSenderPerson().getName());
7000 String newSender = String.valueOf(newM.getSenderPerson() == null
7001 ? newM.getSender()
7002 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05007003 if (!Objects.equals(oldSender, newSender)) {
7004 return true;
7005 }
7006
7007 String oldKey = oldM.getSenderPerson() == null
7008 ? null : oldM.getSenderPerson().getKey();
7009 String newKey = newM.getSenderPerson() == null
7010 ? null : newM.getSenderPerson().getKey();
7011 if (!Objects.equals(oldKey, newKey)) {
7012 return true;
7013 }
7014 // Other fields (like timestamp) intentionally excluded
7015 }
7016 return false;
7017 }
7018
Adrian Roosc1a80b02016-04-05 14:54:55 -07007019 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007020 return findLatestIncomingMessage(mMessages);
7021 }
7022
7023 /**
7024 * @hide
7025 */
7026 @Nullable
7027 public static Message findLatestIncomingMessage(
7028 List<Message> messages) {
7029 for (int i = messages.size() - 1; i >= 0; i--) {
7030 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007031 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007032 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007033 return m;
7034 }
7035 }
Selim Cinek88188f22017-09-19 16:46:56 -07007036 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007037 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007038 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007039 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007040 return null;
7041 }
7042
7043 /**
7044 * @hide
7045 */
7046 @Override
7047 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007048 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007049 }
7050
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007051 /**
7052 * Create a messaging layout.
7053 *
7054 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7055 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007056 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007057 * @return the created remoteView.
7058 */
Selim Cinekafeed292017-12-12 17:32:44 -08007059 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007060 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007061 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007062 ? super.mBigContentTitle
7063 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007064 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7065 >= Build.VERSION_CODES.P;
7066 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007067 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007068 Icon avatarReplacement = null;
7069 if (!atLeastP) {
7070 isOneToOne = TextUtils.isEmpty(conversationTitle);
7071 avatarReplacement = mBuilder.mN.mLargeIcon;
7072 if (hasOnlyWhiteSpaceSenders()) {
7073 isOneToOne = true;
7074 nameReplacement = conversationTitle;
7075 conversationTitle = null;
7076 }
7077 } else {
7078 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007079 }
Selim Cinek384804b2018-04-18 14:31:07 +08007080 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007081 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007082 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007083 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007084 .hideLargeIcon(hideRightIcons || isOneToOne)
7085 .hideReplyIcon(hideRightIcons)
7086 .headerTextSecondary(conversationTitle),
7087 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007088 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007089 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007090 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7091 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007092 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007093 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7094 : mBuilder.resolveContrastColor());
7095 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7096 mBuilder.getPrimaryTextColor());
7097 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7098 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007099 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7100 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007101 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7102 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007103 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7104 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007105 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7106 isOneToOne);
7107 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7108 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007109 return contentView;
7110 }
7111
Selim Cinekf7409db2017-10-24 16:17:14 -07007112 private boolean hasOnlyWhiteSpaceSenders() {
7113 for (int i = 0; i < mMessages.size(); i++) {
7114 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007115 Person sender = m.getSenderPerson();
7116 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007117 return false;
7118 }
7119 }
7120 return true;
7121 }
7122
7123 private boolean isWhiteSpace(CharSequence sender) {
7124 if (TextUtils.isEmpty(sender)) {
7125 return true;
7126 }
7127 if (sender.toString().matches("^\\s*$")) {
7128 return true;
7129 }
7130 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7131 // For the presentation that we had.
7132 for (int i = 0; i < sender.length(); i++) {
7133 char c = sender.charAt(i);
7134 if (c != '\u200B') {
7135 return false;
7136 }
7137 }
7138 return true;
7139 }
7140
Selim Cinek88188f22017-09-19 16:46:56 -07007141 private CharSequence createConversationTitleFromMessages() {
7142 ArraySet<CharSequence> names = new ArraySet<>();
7143 for (int i = 0; i < mMessages.size(); i++) {
7144 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007145 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007146 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007147 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007148 }
7149 }
7150 SpannableStringBuilder title = new SpannableStringBuilder();
7151 int size = names.size();
7152 for (int i = 0; i < size; i++) {
7153 CharSequence name = names.valueAt(i);
7154 if (!TextUtils.isEmpty(title)) {
7155 title.append(", ");
7156 }
7157 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7158 }
7159 return title;
7160 }
7161
Adrian Roosdedd1df2016-04-26 16:38:47 -07007162 /**
7163 * @hide
7164 */
7165 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007166 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007167 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007168 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007169 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7170 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007171 }
7172
Adrian Roosc1a80b02016-04-05 14:54:55 -07007173 private static TextAppearanceSpan makeFontColorSpan(int color) {
7174 return new TextAppearanceSpan(null, 0, 0,
7175 ColorStateList.valueOf(color), null);
7176 }
7177
Alex Hillsd9b04d92016-04-11 16:38:16 -04007178 public static final class Message {
7179
7180 static final String KEY_TEXT = "text";
7181 static final String KEY_TIMESTAMP = "time";
7182 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007183 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007184 static final String KEY_DATA_MIME_TYPE = "type";
7185 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007186 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007187 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007188
7189 private final CharSequence mText;
7190 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007191 @Nullable
7192 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007193 /** True if this message was generated from the extra
7194 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7195 */
7196 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007197
Shane Brennan5a871862017-03-11 13:14:17 -08007198 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007199 private String mDataMimeType;
7200 private Uri mDataUri;
7201
7202 /**
7203 * Constructor
7204 * @param text A {@link CharSequence} to be displayed as the message content
7205 * @param timestamp Time at which the message arrived
7206 * @param sender A {@link CharSequence} to be used for displaying the name of the
7207 * sender. Should be <code>null</code> for messages by the current user, in which case
7208 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7209 * Should be unique amongst all individuals in the conversation, and should be
7210 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007211 *
7212 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007213 */
7214 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007215 this(text, timestamp, sender == null ? null
7216 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007217 }
7218
7219 /**
7220 * Constructor
7221 * @param text A {@link CharSequence} to be displayed as the message content
7222 * @param timestamp Time at which the message arrived
7223 * @param sender The {@link Person} who sent the message.
7224 * Should be <code>null</code> for messages by the current user, in which case
7225 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7226 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007227 * The person provided should contain an Icon, set with
7228 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7229 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7230 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7231 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007232 * </p>
7233 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007234 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007235 this(text, timestamp, sender, false /* remoteHistory */);
7236 }
7237
7238 /**
7239 * Constructor
7240 * @param text A {@link CharSequence} to be displayed as the message content
7241 * @param timestamp Time at which the message arrived
7242 * @param sender The {@link Person} who sent the message.
7243 * Should be <code>null</code> for messages by the current user, in which case
7244 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7245 * @param remoteInputHistory True if the messages was generated from the extra
7246 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7247 * <p>
7248 * The person provided should contain an Icon, set with
7249 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7250 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7251 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7252 * to differentiate between the different users.
7253 * </p>
7254 * @hide
7255 */
7256 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7257 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007258 mText = text;
7259 mTimestamp = timestamp;
7260 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007261 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007262 }
7263
7264 /**
7265 * Sets a binary blob of data and an associated MIME type for a message. In the case
7266 * where the platform doesn't support the MIME type, the original text provided in the
7267 * constructor will be used.
7268 * @param dataMimeType The MIME type of the content. See
7269 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7270 * types on Android and Android Wear.
7271 * @param dataUri The uri containing the content whose type is given by the MIME type.
7272 * <p class="note">
7273 * <ol>
7274 * <li>Notification Listeners including the System UI need permission to access the
7275 * data the Uri points to. The recommended ways to do this are:</li>
7276 * <li>Store the data in your own ContentProvider, making sure that other apps have
7277 * the correct permission to access your provider. The preferred mechanism for
7278 * providing access is to use per-URI permissions which are temporary and only
7279 * grant access to the receiving application. An easy way to create a
7280 * ContentProvider like this is to use the FileProvider helper class.</li>
7281 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7282 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7283 * also store non-media types (see MediaStore.Files for more info). Files can be
7284 * inserted into the MediaStore using scanFile() after which a content:// style
7285 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7286 * Note that once added to the system MediaStore the content is accessible to any
7287 * app on the device.</li>
7288 * </ol>
7289 * @return this object for method chaining
7290 */
7291 public Message setData(String dataMimeType, Uri dataUri) {
7292 mDataMimeType = dataMimeType;
7293 mDataUri = dataUri;
7294 return this;
7295 }
7296
Alex Hillsfc737de2016-03-23 17:33:02 -04007297 /**
7298 * Get the text to be used for this message, or the fallback text if a type and content
7299 * Uri have been set
7300 */
7301 public CharSequence getText() {
7302 return mText;
7303 }
7304
7305 /**
7306 * Get the time at which this message arrived
7307 */
7308 public long getTimestamp() {
7309 return mTimestamp;
7310 }
7311
7312 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007313 * Get the extras Bundle for this message.
7314 */
7315 public Bundle getExtras() {
7316 return mExtras;
7317 }
7318
7319 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007320 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007321 *
7322 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007323 */
7324 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007325 return mSender == null ? null : mSender.getName();
7326 }
7327
7328 /**
7329 * Get the sender associated with this message.
7330 */
7331 @Nullable
7332 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007333 return mSender;
7334 }
7335
7336 /**
7337 * Get the MIME type of the data pointed to by the Uri
7338 */
7339 public String getDataMimeType() {
7340 return mDataMimeType;
7341 }
7342
7343 /**
koprivaa1a78482018-10-09 10:09:23 -07007344 * Get the Uri pointing to the content of the message. Can be null, in which case
Alex Hillsfc737de2016-03-23 17:33:02 -04007345 * {@see #getText()} is used.
7346 */
7347 public Uri getDataUri() {
7348 return mDataUri;
7349 }
7350
Kenny Guya0f6de82018-04-06 16:20:16 +01007351 /**
7352 * @return True if the message was generated from
7353 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7354 * @hide
7355 */
7356 public boolean isRemoteInputHistory() {
7357 return mRemoteInputHistory;
7358 }
7359
Alex Hillsd9b04d92016-04-11 16:38:16 -04007360 private Bundle toBundle() {
7361 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007362 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007363 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007364 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007365 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007366 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007367 // Legacy listeners need this
7368 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7369 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007370 }
7371 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007372 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007373 }
7374 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007375 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007376 }
Shane Brennan5a871862017-03-11 13:14:17 -08007377 if (mExtras != null) {
7378 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7379 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007380 if (mRemoteInputHistory) {
7381 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7382 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007383 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007384 }
7385
Alex Hillsd9b04d92016-04-11 16:38:16 -04007386 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7387 Bundle[] bundles = new Bundle[messages.size()];
7388 final int N = messages.size();
7389 for (int i = 0; i < N; i++) {
7390 bundles[i] = messages.get(i).toBundle();
7391 }
7392 return bundles;
7393 }
7394
Selim Cinek88188f22017-09-19 16:46:56 -07007395 /**
7396 * @return A list of messages read from the bundles.
7397 *
7398 * @hide
7399 */
7400 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7401 if (bundles == null) {
7402 return new ArrayList<>();
7403 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007404 List<Message> messages = new ArrayList<>(bundles.length);
7405 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007406 if (bundles[i] instanceof Bundle) {
7407 Message message = getMessageFromBundle((Bundle)bundles[i]);
7408 if (message != null) {
7409 messages.add(message);
7410 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007411 }
7412 }
7413 return messages;
7414 }
7415
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007416 /**
7417 * @return The message that is stored in the bundle or null if the message couldn't be
7418 * resolved.
7419 *
7420 * @hide
7421 */
7422 @Nullable
7423 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007424 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007425 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007426 return null;
7427 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007428
7429 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7430 if (senderPerson == null) {
7431 // Legacy apps that use compat don't actually provide the sender objects
7432 // We need to fix the compat version to provide people / use
7433 // the native api instead
7434 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7435 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007436 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007437 }
7438 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007439 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007440 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007441 senderPerson,
7442 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007443 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7444 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007445 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7446 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007447 }
Shane Brennan5a871862017-03-11 13:14:17 -08007448 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7449 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7450 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007451 return message;
7452 }
7453 } catch (ClassCastException e) {
7454 return null;
7455 }
7456 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007457 }
7458 }
7459
7460 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007461 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007462 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007463 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007464 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007465 * Notification notif = new Notification.Builder(mContext)
7466 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7467 * .setContentText(subject)
7468 * .setSmallIcon(R.drawable.new_mail)
7469 * .setLargeIcon(aBitmap)
7470 * .setStyle(new Notification.InboxStyle()
7471 * .addLine(str1)
7472 * .addLine(str2)
7473 * .setContentTitle(&quot;&quot;)
7474 * .setSummaryText(&quot;+3 more&quot;))
7475 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007476 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007477 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007478 * @see Notification#bigContentView
7479 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007480 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007481
7482 /**
7483 * The number of lines of remote input history allowed until we start reducing lines.
7484 */
7485 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007486 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7487
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007488 public InboxStyle() {
7489 }
7490
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007491 /**
7492 * @deprecated use {@code InboxStyle()}.
7493 */
7494 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007495 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007496 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007497 }
7498
Chris Wrend6297db2012-05-03 16:20:13 -04007499 /**
7500 * Overrides ContentTitle in the big form of the template.
7501 * This defaults to the value passed to setContentTitle().
7502 */
7503 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007504 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007505 return this;
7506 }
7507
7508 /**
7509 * Set the first line of text after the detail section in the big form of the template.
7510 */
7511 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007512 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007513 return this;
7514 }
7515
Chris Wren0bd664d2012-08-01 13:56:56 -04007516 /**
7517 * Append a line to the digest section of the Inbox notification.
7518 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007519 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007520 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007521 return this;
7522 }
7523
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007524 /**
7525 * @hide
7526 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007527 public ArrayList<CharSequence> getLines() {
7528 return mTexts;
7529 }
7530
7531 /**
7532 * @hide
7533 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007534 public void addExtras(Bundle extras) {
7535 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007536
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007537 CharSequence[] a = new CharSequence[mTexts.size()];
7538 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7539 }
7540
Christoph Studer4600f9b2014-07-22 22:44:43 +02007541 /**
7542 * @hide
7543 */
7544 @Override
7545 protected void restoreFromExtras(Bundle extras) {
7546 super.restoreFromExtras(extras);
7547
7548 mTexts.clear();
7549 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7550 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7551 }
7552 }
7553
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007554 /**
7555 * @hide
7556 */
7557 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007558 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007559 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007560 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7561 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007562
Selim Cinek384804b2018-04-18 14:31:07 +08007563 TemplateBindResult result = new TemplateBindResult();
7564 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007565
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007566 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007567
Chris Wrend6297db2012-05-03 16:20:13 -04007568 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
Chris Wren29bb6d92012-05-17 18:09:42 -04007569 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007570
Chris Wren4ed80d52012-05-17 09:30:03 -04007571 // Make sure all rows are gone in case we reuse a view.
7572 for (int rowId : rowIds) {
7573 contentView.setViewVisibility(rowId, View.GONE);
7574 }
7575
Daniel Sandler879c5e02012-04-17 16:46:51 -04007576 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007577 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7578 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007579 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007580 int onlyViewId = 0;
7581 int maxRows = rowIds.length;
7582 if (mBuilder.mActions.size() > 0) {
7583 maxRows--;
7584 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007585 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7586 EXTRA_REMOTE_INPUT_HISTORY);
7587 if (remoteInputHistory != null
7588 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7589 // Let's remove some messages to make room for the remote input history.
7590 // 1 is always able to fit, but let's remove them if they are 2 or 3
7591 int numRemoteInputs = Math.min(remoteInputHistory.length,
7592 MAX_REMOTE_INPUT_HISTORY_LINES);
7593 int totalNumRows = mTexts.size() + numRemoteInputs
7594 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7595 if (totalNumRows > maxRows) {
7596 int overflow = totalNumRows - maxRows;
7597 if (mTexts.size() > maxRows) {
7598 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7599 // few messages, even with the remote input
7600 maxRows -= overflow;
7601 } else {
7602 // otherwise we drop the first messages
7603 i = overflow;
7604 }
7605 }
7606 }
Selim Cinek07c80172016-04-21 16:40:47 -07007607 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007608 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007609 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007610 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007611 contentView.setTextViewText(rowIds[i],
7612 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007613 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007614 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007615 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007616 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007617 if (first) {
7618 onlyViewId = rowIds[i];
7619 } else {
7620 onlyViewId = 0;
7621 }
Selim Cinek247fa012016-02-18 09:50:48 -08007622 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007623 }
7624 i++;
7625 }
Selim Cinek07c80172016-04-21 16:40:47 -07007626 if (onlyViewId != 0) {
7627 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7628 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7629 R.dimen.notification_text_margin_top);
7630 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7631 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007632
Daniel Sandler879c5e02012-04-17 16:46:51 -04007633 return contentView;
7634 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007635
Julia Reynolds7217dc92018-03-07 12:12:09 -05007636 /**
7637 * @hide
7638 */
7639 @Override
7640 public boolean areNotificationsVisiblyDifferent(Style other) {
7641 if (other == null || getClass() != other.getClass()) {
7642 return true;
7643 }
7644 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007645
7646 final ArrayList<CharSequence> myLines = getLines();
7647 final ArrayList<CharSequence> newLines = newS.getLines();
7648 final int n = myLines.size();
7649 if (n != newLines.size()) {
7650 return true;
7651 }
7652
7653 for (int i = 0; i < n; i++) {
7654 if (!Objects.equals(
7655 String.valueOf(myLines.get(i)),
7656 String.valueOf(newLines.get(i)))) {
7657 return true;
7658 }
7659 }
7660 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007661 }
7662
Selim Cinek384804b2018-04-18 14:31:07 +08007663 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007664 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007665 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007666 if (first) {
7667 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7668 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7669 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007670 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007671 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007672 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007673 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007674 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007675 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007676 }
Dan Sandler842dd772014-05-15 09:36:47 -04007677
7678 /**
7679 * Notification style for media playback notifications.
7680 *
7681 * In the expanded form, {@link Notification#bigContentView}, up to 5
7682 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007683 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007684 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7685 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7686 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007687 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007688 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7689 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007690 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007691 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007692 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007693 * Notifications created with MediaStyle will have their category set to
7694 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7695 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007696 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007697 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7698 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007699 * the System UI can identify this as a notification representing an active media session
7700 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7701 *
Selim Cinek99104832017-01-25 14:47:33 -08007702 * <p>
7703 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7704 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7705 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7706 * <p>
7707 *
Dan Sandler842dd772014-05-15 09:36:47 -04007708 * To use this style with your Notification, feed it to
7709 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7710 * <pre class="prettyprint">
7711 * Notification noti = new Notification.Builder()
7712 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007713 * .setContentTitle(&quot;Track title&quot;)
7714 * .setContentText(&quot;Artist - Album&quot;)
7715 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007716 * .setStyle(<b>new Notification.MediaStyle()</b>
7717 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007718 * .build();
7719 * </pre>
7720 *
7721 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007722 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007723 */
7724 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007725 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007726 static final int MAX_MEDIA_BUTTONS = 5;
7727
7728 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007729 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007730
7731 public MediaStyle() {
7732 }
7733
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007734 /**
7735 * @deprecated use {@code MediaStyle()}.
7736 */
7737 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007738 public MediaStyle(Builder builder) {
7739 setBuilder(builder);
7740 }
7741
7742 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007743 * Request up to 3 actions (by index in the order of addition) to be shown in the compact
Dan Sandler842dd772014-05-15 09:36:47 -04007744 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007745 *
7746 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007747 */
7748 public MediaStyle setShowActionsInCompactView(int...actions) {
7749 mActionsToShowInCompact = actions;
7750 return this;
7751 }
7752
7753 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007754 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7755 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007756 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007757 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007758 mToken = token;
7759 return this;
7760 }
7761
Christoph Studer4600f9b2014-07-22 22:44:43 +02007762 /**
7763 * @hide
7764 */
Dan Sandler842dd772014-05-15 09:36:47 -04007765 @Override
Mathew Inwood4fb17d12018-08-14 14:25:44 +01007766 @UnsupportedAppUsage
Dan Sandler842dd772014-05-15 09:36:47 -04007767 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007768 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007769 if (wip.category == null) {
7770 wip.category = Notification.CATEGORY_TRANSPORT;
7771 }
Dan Sandler842dd772014-05-15 09:36:47 -04007772 return wip;
7773 }
7774
Christoph Studer4600f9b2014-07-22 22:44:43 +02007775 /**
7776 * @hide
7777 */
7778 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007779 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007780 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007781 }
7782
7783 /**
7784 * @hide
7785 */
7786 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007787 public RemoteViews makeBigContentView() {
7788 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007789 }
7790
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007791 /**
7792 * @hide
7793 */
7794 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007795 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007796 RemoteViews expanded = makeMediaBigContentView();
7797 return expanded != null ? expanded : makeMediaContentView();
7798 }
7799
Dan Sandler842dd772014-05-15 09:36:47 -04007800 /** @hide */
7801 @Override
7802 public void addExtras(Bundle extras) {
7803 super.addExtras(extras);
7804
7805 if (mToken != null) {
7806 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7807 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007808 if (mActionsToShowInCompact != null) {
7809 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7810 }
Dan Sandler842dd772014-05-15 09:36:47 -04007811 }
7812
Christoph Studer4600f9b2014-07-22 22:44:43 +02007813 /**
7814 * @hide
7815 */
7816 @Override
7817 protected void restoreFromExtras(Bundle extras) {
7818 super.restoreFromExtras(extras);
7819
7820 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7821 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7822 }
7823 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7824 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7825 }
7826 }
7827
Julia Reynolds7217dc92018-03-07 12:12:09 -05007828 /**
7829 * @hide
7830 */
7831 @Override
7832 public boolean areNotificationsVisiblyDifferent(Style other) {
7833 if (other == null || getClass() != other.getClass()) {
7834 return true;
7835 }
7836 // All fields to compare are on the Notification object
7837 return false;
7838 }
7839
Selim Cinek5bf069a2015-11-10 19:14:27 -05007840 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007841 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007842 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007843 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007844 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007845
7846 // If the action buttons should not be tinted, then just use the default
7847 // notification color. Otherwise, just use the passed-in color.
7848 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7849 ? color
7850 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7851 Notification.COLOR_DEFAULT);
7852
Sunny Goyal5b153922017-09-21 21:00:36 -07007853 button.setDrawableTint(R.id.action0, false, tintColor,
7854 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007855 if (!tombstone) {
7856 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7857 }
7858 button.setContentDescription(R.id.action0, action.title);
7859 return button;
7860 }
7861
7862 private RemoteViews makeMediaContentView() {
7863 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007864 R.layout.notification_template_material_media, false, /* hasProgress */
7865 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007866
7867 final int numActions = mBuilder.mActions.size();
7868 final int N = mActionsToShowInCompact == null
7869 ? 0
7870 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7871 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007872 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007873 for (int i = 0; i < N; i++) {
7874 if (i >= numActions) {
7875 throw new IllegalArgumentException(String.format(
7876 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7877 i, numActions - 1));
7878 }
7879
7880 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinekc7f5a822018-03-20 19:32:06 -07007881 final RemoteViews button = generateMediaActionButton(action, getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007882 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007883 }
7884 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007885 handleImage(view);
7886 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007887 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007888 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007889 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007890 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007891 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007892 return view;
7893 }
7894
Selim Cinekc7f5a822018-03-20 19:32:06 -07007895 private int getActionColor() {
7896 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7897 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007898 }
7899
Dan Sandler842dd772014-05-15 09:36:47 -04007900 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007901 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007902 // Dont add an expanded view if there is no more content to be revealed
7903 int actionsInCompact = mActionsToShowInCompact == null
7904 ? 0
7905 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007906 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007907 return null;
7908 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007909 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007910 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007911
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007912 if (actionCount > 0) {
7913 big.removeAllViews(com.android.internal.R.id.media_actions);
7914 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007915 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07007916 getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007917 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007918 }
7919 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007920 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007921 return big;
7922 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007923
Selim Cinek5bf069a2015-11-10 19:14:27 -05007924 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007925 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007926 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7927 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007928 }
7929 }
7930
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007931 /**
7932 * @hide
7933 */
7934 @Override
7935 protected boolean hasProgress() {
7936 return false;
7937 }
Dan Sandler842dd772014-05-15 09:36:47 -04007938 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007939
Selim Cinek593610c2016-02-16 18:42:57 -08007940 /**
7941 * Notification style for custom views that are decorated by the system
7942 *
7943 * <p>Instead of providing a notification that is completely custom, a developer can set this
7944 * style and still obtain system decorations like the notification header with the expand
7945 * affordance and actions.
7946 *
7947 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7948 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7949 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7950 * corresponding custom views to display.
7951 *
7952 * To use this style with your Notification, feed it to
7953 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7954 * <pre class="prettyprint">
7955 * Notification noti = new Notification.Builder()
7956 * .setSmallIcon(R.drawable.ic_stat_player)
7957 * .setLargeIcon(albumArtBitmap))
7958 * .setCustomContentView(contentView);
7959 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7960 * .build();
7961 * </pre>
7962 */
7963 public static class DecoratedCustomViewStyle extends Style {
7964
7965 public DecoratedCustomViewStyle() {
7966 }
7967
Selim Cinek593610c2016-02-16 18:42:57 -08007968 /**
7969 * @hide
7970 */
7971 public boolean displayCustomViewInline() {
7972 return true;
7973 }
7974
7975 /**
7976 * @hide
7977 */
7978 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007979 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007980 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7981 }
7982
7983 /**
7984 * @hide
7985 */
7986 @Override
7987 public RemoteViews makeBigContentView() {
7988 return makeDecoratedBigContentView();
7989 }
7990
7991 /**
7992 * @hide
7993 */
7994 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007995 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007996 return makeDecoratedHeadsUpContentView();
7997 }
7998
Selim Cinek593610c2016-02-16 18:42:57 -08007999 private RemoteViews makeDecoratedHeadsUpContentView() {
8000 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
8001 ? mBuilder.mN.contentView
8002 : mBuilder.mN.headsUpContentView;
8003 if (mBuilder.mActions.size() == 0) {
8004 return makeStandardTemplateWithCustomContent(headsUpContentView);
8005 }
Selim Cinek384804b2018-04-18 14:31:07 +08008006 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008007 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008008 mBuilder.getBigBaseLayoutResource(), result);
8009 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008010 return remoteViews;
8011 }
8012
Selim Cinek593610c2016-02-16 18:42:57 -08008013 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008014 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008015 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008016 mBuilder.getBaseLayoutResource(), result);
8017 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008018 return remoteViews;
8019 }
8020
Selim Cinek593610c2016-02-16 18:42:57 -08008021 private RemoteViews makeDecoratedBigContentView() {
8022 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8023 ? mBuilder.mN.contentView
8024 : mBuilder.mN.bigContentView;
8025 if (mBuilder.mActions.size() == 0) {
8026 return makeStandardTemplateWithCustomContent(bigContentView);
8027 }
Selim Cinek384804b2018-04-18 14:31:07 +08008028 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008029 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008030 mBuilder.getBigBaseLayoutResource(), result);
8031 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008032 return remoteViews;
8033 }
Selim Cinek247fa012016-02-18 09:50:48 -08008034
8035 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008036 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008037 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008038 // Need to clone customContent before adding, because otherwise it can no longer be
8039 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008040 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008041 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8042 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008043 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008044 }
Selim Cinek247fa012016-02-18 09:50:48 -08008045 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008046 Resources resources = mBuilder.mContext.getResources();
8047 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008048 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008049 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008050 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008051
8052 /**
8053 * @hide
8054 */
8055 @Override
8056 public boolean areNotificationsVisiblyDifferent(Style other) {
8057 if (other == null || getClass() != other.getClass()) {
8058 return true;
8059 }
8060 // Comparison done for all custom RemoteViews, independent of style
8061 return false;
8062 }
Selim Cinek593610c2016-02-16 18:42:57 -08008063 }
8064
Selim Cinek03eb3b72016-02-18 10:39:45 -08008065 /**
8066 * Notification style for media custom views that are decorated by the system
8067 *
8068 * <p>Instead of providing a media notification that is completely custom, a developer can set
8069 * this style and still obtain system decorations like the notification header with the expand
8070 * affordance and actions.
8071 *
8072 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8073 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8074 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8075 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008076 * <p>
8077 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8078 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8079 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008080 * To use this style with your Notification, feed it to
8081 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8082 * <pre class="prettyprint">
8083 * Notification noti = new Notification.Builder()
8084 * .setSmallIcon(R.drawable.ic_stat_player)
8085 * .setLargeIcon(albumArtBitmap))
8086 * .setCustomContentView(contentView);
8087 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8088 * .setMediaSession(mySession))
8089 * .build();
8090 * </pre>
8091 *
8092 * @see android.app.Notification.DecoratedCustomViewStyle
8093 * @see android.app.Notification.MediaStyle
8094 */
8095 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8096
8097 public DecoratedMediaCustomViewStyle() {
8098 }
8099
Selim Cinek03eb3b72016-02-18 10:39:45 -08008100 /**
8101 * @hide
8102 */
8103 public boolean displayCustomViewInline() {
8104 return true;
8105 }
8106
8107 /**
8108 * @hide
8109 */
8110 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008111 public RemoteViews makeContentView(boolean increasedHeight) {
8112 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008113 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8114 mBuilder.mN.contentView);
8115 }
8116
8117 /**
8118 * @hide
8119 */
8120 @Override
8121 public RemoteViews makeBigContentView() {
8122 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8123 ? mBuilder.mN.bigContentView
8124 : mBuilder.mN.contentView;
8125 return makeBigContentViewWithCustomContent(customRemoteView);
8126 }
8127
8128 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8129 RemoteViews remoteViews = super.makeBigContentView();
8130 if (remoteViews != null) {
8131 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8132 customRemoteView);
8133 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008134 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008135 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8136 customRemoteView);
8137 } else {
8138 return null;
8139 }
8140 }
8141
8142 /**
8143 * @hide
8144 */
8145 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008146 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008147 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8148 ? mBuilder.mN.headsUpContentView
8149 : mBuilder.mN.contentView;
8150 return makeBigContentViewWithCustomContent(customRemoteView);
8151 }
8152
Julia Reynolds7217dc92018-03-07 12:12:09 -05008153 /**
8154 * @hide
8155 */
8156 @Override
8157 public boolean areNotificationsVisiblyDifferent(Style other) {
8158 if (other == null || getClass() != other.getClass()) {
8159 return true;
8160 }
8161 // Comparison done for all custom RemoteViews, independent of style
8162 return false;
8163 }
8164
Selim Cinek03eb3b72016-02-18 10:39:45 -08008165 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8166 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008167 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008168 // Need to clone customContent before adding, because otherwise it can no longer be
8169 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008170 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008171 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008172 remoteViews.removeAllViews(id);
8173 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008174 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008175 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008176 return remoteViews;
8177 }
8178 }
8179
Christoph Studer4600f9b2014-07-22 22:44:43 +02008180 // When adding a new Style subclass here, don't forget to update
8181 // Builder.getNotificationStyleClass.
8182
Griff Hazen61a9e862014-05-22 16:05:19 -07008183 /**
8184 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8185 * metadata or change options on a notification builder.
8186 */
8187 public interface Extender {
8188 /**
8189 * Apply this extender to a notification builder.
8190 * @param builder the builder to be modified.
8191 * @return the build object for chaining.
8192 */
8193 public Builder extend(Builder builder);
8194 }
8195
8196 /**
8197 * Helper class to add wearable extensions to notifications.
8198 * <p class="note"> See
8199 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8200 * for Android Wear</a> for more information on how to use this class.
8201 * <p>
8202 * To create a notification with wearable extensions:
8203 * <ol>
8204 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8205 * properties.
8206 * <li>Create a {@link android.app.Notification.WearableExtender}.
8207 * <li>Set wearable-specific properties using the
8208 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8209 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8210 * notification.
8211 * <li>Post the notification to the notification system with the
8212 * {@code NotificationManager.notify(...)} methods.
8213 * </ol>
8214 *
8215 * <pre class="prettyprint">
8216 * Notification notif = new Notification.Builder(mContext)
8217 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8218 * .setContentText(subject)
8219 * .setSmallIcon(R.drawable.new_mail)
8220 * .extend(new Notification.WearableExtender()
8221 * .setContentIcon(R.drawable.new_mail))
8222 * .build();
8223 * NotificationManager notificationManger =
8224 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8225 * notificationManger.notify(0, notif);</pre>
8226 *
8227 * <p>Wearable extensions can be accessed on an existing notification by using the
8228 * {@code WearableExtender(Notification)} constructor,
8229 * and then using the {@code get} methods to access values.
8230 *
8231 * <pre class="prettyprint">
8232 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8233 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008234 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008235 */
8236 public static final class WearableExtender implements Extender {
8237 /**
8238 * Sentinel value for an action index that is unset.
8239 */
8240 public static final int UNSET_ACTION_INDEX = -1;
8241
8242 /**
8243 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8244 * default sizing.
8245 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008246 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008247 * on their content.
8248 */
8249 public static final int SIZE_DEFAULT = 0;
8250
8251 /**
8252 * Size value for use with {@link #setCustomSizePreset} to show this notification
8253 * with an extra small size.
8254 * <p>This value is only applicable for custom display notifications created using
8255 * {@link #setDisplayIntent}.
8256 */
8257 public static final int SIZE_XSMALL = 1;
8258
8259 /**
8260 * Size value for use with {@link #setCustomSizePreset} to show this notification
8261 * with a small size.
8262 * <p>This value is only applicable for custom display notifications created using
8263 * {@link #setDisplayIntent}.
8264 */
8265 public static final int SIZE_SMALL = 2;
8266
8267 /**
8268 * Size value for use with {@link #setCustomSizePreset} to show this notification
8269 * with a medium size.
8270 * <p>This value is only applicable for custom display notifications created using
8271 * {@link #setDisplayIntent}.
8272 */
8273 public static final int SIZE_MEDIUM = 3;
8274
8275 /**
8276 * Size value for use with {@link #setCustomSizePreset} to show this notification
8277 * with a large size.
8278 * <p>This value is only applicable for custom display notifications created using
8279 * {@link #setDisplayIntent}.
8280 */
8281 public static final int SIZE_LARGE = 4;
8282
Griff Hazend5f11f92014-05-27 15:40:09 -07008283 /**
8284 * Size value for use with {@link #setCustomSizePreset} to show this notification
8285 * full screen.
8286 * <p>This value is only applicable for custom display notifications created using
8287 * {@link #setDisplayIntent}.
8288 */
8289 public static final int SIZE_FULL_SCREEN = 5;
8290
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008291 /**
8292 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8293 * short amount of time when this notification is displayed on the screen. This
8294 * is the default value.
8295 */
8296 public static final int SCREEN_TIMEOUT_SHORT = 0;
8297
8298 /**
8299 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8300 * for a longer amount of time when this notification is displayed on the screen.
8301 */
8302 public static final int SCREEN_TIMEOUT_LONG = -1;
8303
Griff Hazen61a9e862014-05-22 16:05:19 -07008304 /** Notification extra which contains wearable extensions */
8305 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8306
Pete Gastaf6781d2014-10-07 15:17:05 -04008307 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008308 private static final String KEY_ACTIONS = "actions";
8309 private static final String KEY_FLAGS = "flags";
8310 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8311 private static final String KEY_PAGES = "pages";
8312 private static final String KEY_BACKGROUND = "background";
8313 private static final String KEY_CONTENT_ICON = "contentIcon";
8314 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8315 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8316 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8317 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8318 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008319 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008320 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008321 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008322
8323 // Flags bitwise-ored to mFlags
8324 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8325 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8326 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8327 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008328 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008329 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008330 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008331
8332 // Default value for flags integer
8333 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8334
8335 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8336 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8337
8338 private ArrayList<Action> mActions = new ArrayList<Action>();
8339 private int mFlags = DEFAULT_FLAGS;
8340 private PendingIntent mDisplayIntent;
8341 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8342 private Bitmap mBackground;
8343 private int mContentIcon;
8344 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8345 private int mContentActionIndex = UNSET_ACTION_INDEX;
8346 private int mCustomSizePreset = SIZE_DEFAULT;
8347 private int mCustomContentHeight;
8348 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008349 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008350 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008351 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008352
8353 /**
8354 * Create a {@link android.app.Notification.WearableExtender} with default
8355 * options.
8356 */
8357 public WearableExtender() {
8358 }
8359
8360 public WearableExtender(Notification notif) {
8361 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8362 if (wearableBundle != null) {
8363 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8364 if (actions != null) {
8365 mActions.addAll(actions);
8366 }
8367
8368 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8369 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8370
8371 Notification[] pages = getNotificationArrayFromBundle(
8372 wearableBundle, KEY_PAGES);
8373 if (pages != null) {
8374 Collections.addAll(mPages, pages);
8375 }
8376
8377 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8378 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8379 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8380 DEFAULT_CONTENT_ICON_GRAVITY);
8381 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8382 UNSET_ACTION_INDEX);
8383 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8384 SIZE_DEFAULT);
8385 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8386 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008387 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008388 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008389 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008390 }
8391 }
8392
8393 /**
8394 * Apply wearable extensions to a notification that is being built. This is typically
8395 * called by the {@link android.app.Notification.Builder#extend} method of
8396 * {@link android.app.Notification.Builder}.
8397 */
8398 @Override
8399 public Notification.Builder extend(Notification.Builder builder) {
8400 Bundle wearableBundle = new Bundle();
8401
8402 if (!mActions.isEmpty()) {
8403 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8404 }
8405 if (mFlags != DEFAULT_FLAGS) {
8406 wearableBundle.putInt(KEY_FLAGS, mFlags);
8407 }
8408 if (mDisplayIntent != null) {
8409 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8410 }
8411 if (!mPages.isEmpty()) {
8412 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8413 new Notification[mPages.size()]));
8414 }
8415 if (mBackground != null) {
8416 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8417 }
8418 if (mContentIcon != 0) {
8419 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8420 }
8421 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8422 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8423 }
8424 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8425 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8426 mContentActionIndex);
8427 }
8428 if (mCustomSizePreset != SIZE_DEFAULT) {
8429 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8430 }
8431 if (mCustomContentHeight != 0) {
8432 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8433 }
8434 if (mGravity != DEFAULT_GRAVITY) {
8435 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8436 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008437 if (mHintScreenTimeout != 0) {
8438 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8439 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008440 if (mDismissalId != null) {
8441 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8442 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008443 if (mBridgeTag != null) {
8444 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8445 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008446
8447 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8448 return builder;
8449 }
8450
8451 @Override
8452 public WearableExtender clone() {
8453 WearableExtender that = new WearableExtender();
8454 that.mActions = new ArrayList<Action>(this.mActions);
8455 that.mFlags = this.mFlags;
8456 that.mDisplayIntent = this.mDisplayIntent;
8457 that.mPages = new ArrayList<Notification>(this.mPages);
8458 that.mBackground = this.mBackground;
8459 that.mContentIcon = this.mContentIcon;
8460 that.mContentIconGravity = this.mContentIconGravity;
8461 that.mContentActionIndex = this.mContentActionIndex;
8462 that.mCustomSizePreset = this.mCustomSizePreset;
8463 that.mCustomContentHeight = this.mCustomContentHeight;
8464 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008465 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008466 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008467 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008468 return that;
8469 }
8470
8471 /**
8472 * Add a wearable action to this notification.
8473 *
8474 * <p>When wearable actions are added using this method, the set of actions that
8475 * show on a wearable device splits from devices that only show actions added
8476 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8477 * of which actions display on different devices.
8478 *
8479 * @param action the action to add to this notification
8480 * @return this object for method chaining
8481 * @see android.app.Notification.Action
8482 */
8483 public WearableExtender addAction(Action action) {
8484 mActions.add(action);
8485 return this;
8486 }
8487
8488 /**
8489 * Adds wearable actions to this notification.
8490 *
8491 * <p>When wearable actions are added using this method, the set of actions that
8492 * show on a wearable device splits from devices that only show actions added
8493 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8494 * of which actions display on different devices.
8495 *
8496 * @param actions the actions to add to this notification
8497 * @return this object for method chaining
8498 * @see android.app.Notification.Action
8499 */
8500 public WearableExtender addActions(List<Action> actions) {
8501 mActions.addAll(actions);
8502 return this;
8503 }
8504
8505 /**
8506 * Clear all wearable actions present on this builder.
8507 * @return this object for method chaining.
8508 * @see #addAction
8509 */
8510 public WearableExtender clearActions() {
8511 mActions.clear();
8512 return this;
8513 }
8514
8515 /**
8516 * Get the wearable actions present on this notification.
8517 */
8518 public List<Action> getActions() {
8519 return mActions;
8520 }
8521
8522 /**
8523 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008524 * this notification. The {@link PendingIntent} provided should be for an activity.
8525 *
8526 * <pre class="prettyprint">
8527 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8528 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8529 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8530 * Notification notif = new Notification.Builder(context)
8531 * .extend(new Notification.WearableExtender()
8532 * .setDisplayIntent(displayPendingIntent)
8533 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8534 * .build();</pre>
8535 *
8536 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008537 * should have an empty task affinity. It is also recommended to use the device
8538 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008539 *
8540 * <p>Example AndroidManifest.xml entry:
8541 * <pre class="prettyprint">
8542 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8543 * android:exported=&quot;true&quot;
8544 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008545 * android:taskAffinity=&quot;&quot;
8546 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008547 *
8548 * @param intent the {@link PendingIntent} for an activity
8549 * @return this object for method chaining
8550 * @see android.app.Notification.WearableExtender#getDisplayIntent
8551 */
8552 public WearableExtender setDisplayIntent(PendingIntent intent) {
8553 mDisplayIntent = intent;
8554 return this;
8555 }
8556
8557 /**
8558 * Get the intent to launch inside of an activity view when displaying this
8559 * notification. This {@code PendingIntent} should be for an activity.
8560 */
8561 public PendingIntent getDisplayIntent() {
8562 return mDisplayIntent;
8563 }
8564
8565 /**
8566 * Add an additional page of content to display with this notification. The current
8567 * notification forms the first page, and pages added using this function form
8568 * subsequent pages. This field can be used to separate a notification into multiple
8569 * sections.
8570 *
8571 * @param page the notification to add as another page
8572 * @return this object for method chaining
8573 * @see android.app.Notification.WearableExtender#getPages
8574 */
8575 public WearableExtender addPage(Notification page) {
8576 mPages.add(page);
8577 return this;
8578 }
8579
8580 /**
8581 * Add additional pages of content to display with this notification. The current
8582 * notification forms the first page, and pages added using this function form
8583 * subsequent pages. This field can be used to separate a notification into multiple
8584 * sections.
8585 *
8586 * @param pages a list of notifications
8587 * @return this object for method chaining
8588 * @see android.app.Notification.WearableExtender#getPages
8589 */
8590 public WearableExtender addPages(List<Notification> pages) {
8591 mPages.addAll(pages);
8592 return this;
8593 }
8594
8595 /**
8596 * Clear all additional pages present on this builder.
8597 * @return this object for method chaining.
8598 * @see #addPage
8599 */
8600 public WearableExtender clearPages() {
8601 mPages.clear();
8602 return this;
8603 }
8604
8605 /**
8606 * Get the array of additional pages of content for displaying this notification. The
8607 * current notification forms the first page, and elements within this array form
8608 * subsequent pages. This field can be used to separate a notification into multiple
8609 * sections.
8610 * @return the pages for this notification
8611 */
8612 public List<Notification> getPages() {
8613 return mPages;
8614 }
8615
8616 /**
8617 * Set a background image to be displayed behind the notification content.
8618 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8619 * will work with any notification style.
8620 *
8621 * @param background the background bitmap
8622 * @return this object for method chaining
8623 * @see android.app.Notification.WearableExtender#getBackground
8624 */
8625 public WearableExtender setBackground(Bitmap background) {
8626 mBackground = background;
8627 return this;
8628 }
8629
8630 /**
8631 * Get a background image to be displayed behind the notification content.
8632 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8633 * will work with any notification style.
8634 *
8635 * @return the background image
8636 * @see android.app.Notification.WearableExtender#setBackground
8637 */
8638 public Bitmap getBackground() {
8639 return mBackground;
8640 }
8641
8642 /**
8643 * Set an icon that goes with the content of this notification.
8644 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008645 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008646 public WearableExtender setContentIcon(int icon) {
8647 mContentIcon = icon;
8648 return this;
8649 }
8650
8651 /**
8652 * Get an icon that goes with the content of this notification.
8653 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008654 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008655 public int getContentIcon() {
8656 return mContentIcon;
8657 }
8658
8659 /**
8660 * Set the gravity that the content icon should have within the notification display.
8661 * Supported values include {@link android.view.Gravity#START} and
8662 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8663 * @see #setContentIcon
8664 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008665 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008666 public WearableExtender setContentIconGravity(int contentIconGravity) {
8667 mContentIconGravity = contentIconGravity;
8668 return this;
8669 }
8670
8671 /**
8672 * Get the gravity that the content icon should have within the notification display.
8673 * Supported values include {@link android.view.Gravity#START} and
8674 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8675 * @see #getContentIcon
8676 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008677 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008678 public int getContentIconGravity() {
8679 return mContentIconGravity;
8680 }
8681
8682 /**
8683 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008684 * this notification. This action will no longer display separately from the
8685 * notification's content.
8686 *
Griff Hazenca48d352014-05-28 22:37:13 -07008687 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008688 * set, although the list of available actions comes from the main notification and not
8689 * from the child page's notification.
8690 *
8691 * @param actionIndex The index of the action to hoist onto the current notification page.
8692 * If wearable actions were added to the main notification, this index
8693 * will apply to that list, otherwise it will apply to the regular
8694 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008695 */
8696 public WearableExtender setContentAction(int actionIndex) {
8697 mContentActionIndex = actionIndex;
8698 return this;
8699 }
8700
8701 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008702 * Get the index of the notification action, if any, that was specified as being clickable
8703 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008704 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008705 *
Griff Hazenca48d352014-05-28 22:37:13 -07008706 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008707 * set, although the list of available actions comes from the main notification and not
8708 * from the child page's notification.
8709 *
8710 * <p>If wearable specific actions were added to the main notification, this index will
8711 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008712 *
8713 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008714 */
8715 public int getContentAction() {
8716 return mContentActionIndex;
8717 }
8718
8719 /**
8720 * Set the gravity that this notification should have within the available viewport space.
8721 * Supported values include {@link android.view.Gravity#TOP},
8722 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8723 * The default value is {@link android.view.Gravity#BOTTOM}.
8724 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008725 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008726 public WearableExtender setGravity(int gravity) {
8727 mGravity = gravity;
8728 return this;
8729 }
8730
8731 /**
8732 * Get the gravity that this notification should have within the available viewport space.
8733 * Supported values include {@link android.view.Gravity#TOP},
8734 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8735 * The default value is {@link android.view.Gravity#BOTTOM}.
8736 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008737 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008738 public int getGravity() {
8739 return mGravity;
8740 }
8741
8742 /**
8743 * Set the custom size preset for the display of this notification out of the available
8744 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8745 * {@link #SIZE_LARGE}.
8746 * <p>Some custom size presets are only applicable for custom display notifications created
8747 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8748 * documentation for the preset in question. See also
8749 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8750 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008751 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008752 public WearableExtender setCustomSizePreset(int sizePreset) {
8753 mCustomSizePreset = sizePreset;
8754 return this;
8755 }
8756
8757 /**
8758 * Get the custom size preset for the display of this notification out of the available
8759 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8760 * {@link #SIZE_LARGE}.
8761 * <p>Some custom size presets are only applicable for custom display notifications created
8762 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8763 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8764 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008765 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008766 public int getCustomSizePreset() {
8767 return mCustomSizePreset;
8768 }
8769
8770 /**
8771 * Set the custom height in pixels for the display of this notification's content.
8772 * <p>This option is only available for custom display notifications created
8773 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8774 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8775 * {@link #getCustomContentHeight}.
8776 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008777 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008778 public WearableExtender setCustomContentHeight(int height) {
8779 mCustomContentHeight = height;
8780 return this;
8781 }
8782
8783 /**
8784 * Get the custom height in pixels for the display of this notification's content.
8785 * <p>This option is only available for custom display notifications created
8786 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8787 * {@link #setCustomContentHeight}.
8788 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008789 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008790 public int getCustomContentHeight() {
8791 return mCustomContentHeight;
8792 }
8793
8794 /**
8795 * Set whether the scrolling position for the contents of this notification should start
8796 * at the bottom of the contents instead of the top when the contents are too long to
8797 * display within the screen. Default is false (start scroll at the top).
8798 */
8799 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8800 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8801 return this;
8802 }
8803
8804 /**
8805 * Get whether the scrolling position for the contents of this notification should start
8806 * at the bottom of the contents instead of the top when the contents are too long to
8807 * display within the screen. Default is false (start scroll at the top).
8808 */
8809 public boolean getStartScrollBottom() {
8810 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8811 }
8812
8813 /**
8814 * Set whether the content intent is available when the wearable device is not connected
8815 * to a companion device. The user can still trigger this intent when the wearable device
8816 * is offline, but a visual hint will indicate that the content intent may not be available.
8817 * Defaults to true.
8818 */
8819 public WearableExtender setContentIntentAvailableOffline(
8820 boolean contentIntentAvailableOffline) {
8821 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8822 return this;
8823 }
8824
8825 /**
8826 * Get whether the content intent is available when the wearable device is not connected
8827 * to a companion device. The user can still trigger this intent when the wearable device
8828 * is offline, but a visual hint will indicate that the content intent may not be available.
8829 * Defaults to true.
8830 */
8831 public boolean getContentIntentAvailableOffline() {
8832 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8833 }
8834
8835 /**
8836 * Set a hint that this notification's icon should not be displayed.
8837 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8838 * @return this object for method chaining
8839 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008840 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008841 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8842 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8843 return this;
8844 }
8845
8846 /**
8847 * Get a hint that this notification's icon should not be displayed.
8848 * @return {@code true} if this icon should not be displayed, false otherwise.
8849 * The default value is {@code false} if this was never set.
8850 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008851 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008852 public boolean getHintHideIcon() {
8853 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8854 }
8855
8856 /**
8857 * Set a visual hint that only the background image of this notification should be
8858 * displayed, and other semantic content should be hidden. This hint is only applicable
8859 * to sub-pages added using {@link #addPage}.
8860 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008861 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008862 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8863 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8864 return this;
8865 }
8866
8867 /**
8868 * Get a visual hint that only the background image of this notification should be
8869 * displayed, and other semantic content should be hidden. This hint is only applicable
8870 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8871 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008872 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008873 public boolean getHintShowBackgroundOnly() {
8874 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8875 }
8876
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008877 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008878 * Set a hint that this notification's background should not be clipped if possible,
8879 * and should instead be resized to fully display on the screen, retaining the aspect
8880 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008881 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8882 * @return this object for method chaining
8883 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008884 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008885 public WearableExtender setHintAvoidBackgroundClipping(
8886 boolean hintAvoidBackgroundClipping) {
8887 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8888 return this;
8889 }
8890
8891 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008892 * Get a hint that this notification's background should not be clipped if possible,
8893 * and should instead be resized to fully display on the screen, retaining the aspect
8894 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008895 * @return {@code true} if it's ok if the background is clipped on the screen, false
8896 * otherwise. The default value is {@code false} if this was never set.
8897 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008898 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008899 public boolean getHintAvoidBackgroundClipping() {
8900 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8901 }
8902
8903 /**
8904 * Set a hint that the screen should remain on for at least this duration when
8905 * this notification is displayed on the screen.
8906 * @param timeout The requested screen timeout in milliseconds. Can also be either
8907 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8908 * @return this object for method chaining
8909 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008910 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008911 public WearableExtender setHintScreenTimeout(int timeout) {
8912 mHintScreenTimeout = timeout;
8913 return this;
8914 }
8915
8916 /**
8917 * Get the duration, in milliseconds, that the screen should remain on for
8918 * when this notification is displayed.
8919 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8920 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8921 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008922 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008923 public int getHintScreenTimeout() {
8924 return mHintScreenTimeout;
8925 }
8926
Alex Hills9ab3a232016-04-05 14:54:56 -04008927 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008928 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8929 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8930 * qr codes, as well as other simple black-and-white tickets.
8931 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8932 * @return this object for method chaining
8933 */
8934 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8935 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8936 return this;
8937 }
8938
8939 /**
8940 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8941 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8942 * qr codes, as well as other simple black-and-white tickets.
8943 * @return {@code true} if it should be displayed in ambient, false otherwise
8944 * otherwise. The default value is {@code false} if this was never set.
8945 */
8946 public boolean getHintAmbientBigPicture() {
8947 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8948 }
8949
8950 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008951 * Set a hint that this notification's content intent will launch an {@link Activity}
8952 * directly, telling the platform that it can generate the appropriate transitions.
8953 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8954 * an activity and transitions should be generated, false otherwise.
8955 * @return this object for method chaining
8956 */
8957 public WearableExtender setHintContentIntentLaunchesActivity(
8958 boolean hintContentIntentLaunchesActivity) {
8959 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8960 return this;
8961 }
8962
8963 /**
8964 * Get a hint that this notification's content intent will launch an {@link Activity}
8965 * directly, telling the platform that it can generate the appropriate transitions
8966 * @return {@code true} if the content intent will launch an activity and transitions should
8967 * be generated, false otherwise. The default value is {@code false} if this was never set.
8968 */
8969 public boolean getHintContentIntentLaunchesActivity() {
8970 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8971 }
8972
Nadia Benbernou948627e2016-04-14 14:41:08 -04008973 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008974 * Sets the dismissal id for this notification. If a notification is posted with a
8975 * dismissal id, then when that notification is canceled, notifications on other wearables
8976 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008977 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008978 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008979 * @param dismissalId the dismissal id of the notification.
8980 * @return this object for method chaining
8981 */
8982 public WearableExtender setDismissalId(String dismissalId) {
8983 mDismissalId = dismissalId;
8984 return this;
8985 }
8986
8987 /**
8988 * Returns the dismissal id of the notification.
8989 * @return the dismissal id of the notification or null if it has not been set.
8990 */
8991 public String getDismissalId() {
8992 return mDismissalId;
8993 }
8994
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008995 /**
8996 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8997 * posted from a phone to provide finer-grained control on what notifications are bridged
8998 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8999 * Features to Notifications</a> for more information.
9000 * @param bridgeTag the bridge tag of the notification.
9001 * @return this object for method chaining
9002 */
9003 public WearableExtender setBridgeTag(String bridgeTag) {
9004 mBridgeTag = bridgeTag;
9005 return this;
9006 }
9007
9008 /**
9009 * Returns the bridge tag of the notification.
9010 * @return the bridge tag or null if not present.
9011 */
9012 public String getBridgeTag() {
9013 return mBridgeTag;
9014 }
9015
Griff Hazen61a9e862014-05-22 16:05:19 -07009016 private void setFlag(int mask, boolean value) {
9017 if (value) {
9018 mFlags |= mask;
9019 } else {
9020 mFlags &= ~mask;
9021 }
9022 }
9023 }
9024
9025 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009026 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9027 * with car extensions:
9028 *
9029 * <ol>
9030 * <li>Create an {@link Notification.Builder}, setting any desired
9031 * properties.
9032 * <li>Create a {@link CarExtender}.
9033 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9034 * {@link CarExtender}.
9035 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9036 * to apply the extensions to a notification.
9037 * </ol>
9038 *
9039 * <pre class="prettyprint">
9040 * Notification notification = new Notification.Builder(context)
9041 * ...
9042 * .extend(new CarExtender()
9043 * .set*(...))
9044 * .build();
9045 * </pre>
9046 *
9047 * <p>Car extensions can be accessed on an existing notification by using the
9048 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9049 * to access values.
9050 */
9051 public static final class CarExtender implements Extender {
9052 private static final String TAG = "CarExtender";
9053
9054 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9055 private static final String EXTRA_LARGE_ICON = "large_icon";
9056 private static final String EXTRA_CONVERSATION = "car_conversation";
9057 private static final String EXTRA_COLOR = "app_color";
9058
9059 private Bitmap mLargeIcon;
9060 private UnreadConversation mUnreadConversation;
9061 private int mColor = Notification.COLOR_DEFAULT;
9062
9063 /**
9064 * Create a {@link CarExtender} with default options.
9065 */
9066 public CarExtender() {
9067 }
9068
9069 /**
9070 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9071 *
9072 * @param notif The notification from which to copy options.
9073 */
9074 public CarExtender(Notification notif) {
9075 Bundle carBundle = notif.extras == null ?
9076 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9077 if (carBundle != null) {
9078 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9079 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9080
9081 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9082 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9083 }
9084 }
9085
9086 /**
9087 * Apply car extensions to a notification that is being built. This is typically called by
9088 * the {@link Notification.Builder#extend(Notification.Extender)}
9089 * method of {@link Notification.Builder}.
9090 */
9091 @Override
9092 public Notification.Builder extend(Notification.Builder builder) {
9093 Bundle carExtensions = new Bundle();
9094
9095 if (mLargeIcon != null) {
9096 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9097 }
9098 if (mColor != Notification.COLOR_DEFAULT) {
9099 carExtensions.putInt(EXTRA_COLOR, mColor);
9100 }
9101
9102 if (mUnreadConversation != null) {
9103 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9104 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9105 }
9106
9107 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9108 return builder;
9109 }
9110
9111 /**
9112 * Sets the accent color to use when Android Auto presents the notification.
9113 *
9114 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9115 * to accent the displayed notification. However, not all colors are acceptable in an
9116 * automotive setting. This method can be used to override the color provided in the
9117 * notification in such a situation.
9118 */
Tor Norbye80756e32015-03-02 09:39:27 -08009119 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009120 mColor = color;
9121 return this;
9122 }
9123
9124 /**
9125 * Gets the accent color.
9126 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009127 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009128 */
Tor Norbye80756e32015-03-02 09:39:27 -08009129 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009130 public int getColor() {
9131 return mColor;
9132 }
9133
9134 /**
9135 * Sets the large icon of the car notification.
9136 *
9137 * If no large icon is set in the extender, Android Auto will display the icon
9138 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9139 *
9140 * @param largeIcon The large icon to use in the car notification.
9141 * @return This object for method chaining.
9142 */
9143 public CarExtender setLargeIcon(Bitmap largeIcon) {
9144 mLargeIcon = largeIcon;
9145 return this;
9146 }
9147
9148 /**
9149 * Gets the large icon used in this car notification, or null if no icon has been set.
9150 *
9151 * @return The large icon for the car notification.
9152 * @see CarExtender#setLargeIcon
9153 */
9154 public Bitmap getLargeIcon() {
9155 return mLargeIcon;
9156 }
9157
9158 /**
9159 * Sets the unread conversation in a message notification.
9160 *
9161 * @param unreadConversation The unread part of the conversation this notification conveys.
9162 * @return This object for method chaining.
9163 */
9164 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9165 mUnreadConversation = unreadConversation;
9166 return this;
9167 }
9168
9169 /**
9170 * Returns the unread conversation conveyed by this notification.
9171 * @see #setUnreadConversation(UnreadConversation)
9172 */
9173 public UnreadConversation getUnreadConversation() {
9174 return mUnreadConversation;
9175 }
9176
9177 /**
9178 * A class which holds the unread messages from a conversation.
9179 */
9180 public static class UnreadConversation {
9181 private static final String KEY_AUTHOR = "author";
9182 private static final String KEY_TEXT = "text";
9183 private static final String KEY_MESSAGES = "messages";
9184 private static final String KEY_REMOTE_INPUT = "remote_input";
9185 private static final String KEY_ON_REPLY = "on_reply";
9186 private static final String KEY_ON_READ = "on_read";
9187 private static final String KEY_PARTICIPANTS = "participants";
9188 private static final String KEY_TIMESTAMP = "timestamp";
9189
9190 private final String[] mMessages;
9191 private final RemoteInput mRemoteInput;
9192 private final PendingIntent mReplyPendingIntent;
9193 private final PendingIntent mReadPendingIntent;
9194 private final String[] mParticipants;
9195 private final long mLatestTimestamp;
9196
9197 UnreadConversation(String[] messages, RemoteInput remoteInput,
9198 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9199 String[] participants, long latestTimestamp) {
9200 mMessages = messages;
9201 mRemoteInput = remoteInput;
9202 mReadPendingIntent = readPendingIntent;
9203 mReplyPendingIntent = replyPendingIntent;
9204 mParticipants = participants;
9205 mLatestTimestamp = latestTimestamp;
9206 }
9207
9208 /**
9209 * Gets the list of messages conveyed by this notification.
9210 */
9211 public String[] getMessages() {
9212 return mMessages;
9213 }
9214
9215 /**
9216 * Gets the remote input that will be used to convey the response to a message list, or
9217 * null if no such remote input exists.
9218 */
9219 public RemoteInput getRemoteInput() {
9220 return mRemoteInput;
9221 }
9222
9223 /**
9224 * Gets the pending intent that will be triggered when the user replies to this
9225 * notification.
9226 */
9227 public PendingIntent getReplyPendingIntent() {
9228 return mReplyPendingIntent;
9229 }
9230
9231 /**
9232 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9233 * in this object's message list.
9234 */
9235 public PendingIntent getReadPendingIntent() {
9236 return mReadPendingIntent;
9237 }
9238
9239 /**
9240 * Gets the participants in the conversation.
9241 */
9242 public String[] getParticipants() {
9243 return mParticipants;
9244 }
9245
9246 /**
9247 * Gets the firs participant in the conversation.
9248 */
9249 public String getParticipant() {
9250 return mParticipants.length > 0 ? mParticipants[0] : null;
9251 }
9252
9253 /**
9254 * Gets the timestamp of the conversation.
9255 */
9256 public long getLatestTimestamp() {
9257 return mLatestTimestamp;
9258 }
9259
9260 Bundle getBundleForUnreadConversation() {
9261 Bundle b = new Bundle();
9262 String author = null;
9263 if (mParticipants != null && mParticipants.length > 1) {
9264 author = mParticipants[0];
9265 }
9266 Parcelable[] messages = new Parcelable[mMessages.length];
9267 for (int i = 0; i < messages.length; i++) {
9268 Bundle m = new Bundle();
9269 m.putString(KEY_TEXT, mMessages[i]);
9270 m.putString(KEY_AUTHOR, author);
9271 messages[i] = m;
9272 }
9273 b.putParcelableArray(KEY_MESSAGES, messages);
9274 if (mRemoteInput != null) {
9275 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9276 }
9277 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9278 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9279 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9280 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9281 return b;
9282 }
9283
9284 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9285 if (b == null) {
9286 return null;
9287 }
9288 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9289 String[] messages = null;
9290 if (parcelableMessages != null) {
9291 String[] tmp = new String[parcelableMessages.length];
9292 boolean success = true;
9293 for (int i = 0; i < tmp.length; i++) {
9294 if (!(parcelableMessages[i] instanceof Bundle)) {
9295 success = false;
9296 break;
9297 }
9298 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9299 if (tmp[i] == null) {
9300 success = false;
9301 break;
9302 }
9303 }
9304 if (success) {
9305 messages = tmp;
9306 } else {
9307 return null;
9308 }
9309 }
9310
9311 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9312 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9313
9314 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9315
9316 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9317 if (participants == null || participants.length != 1) {
9318 return null;
9319 }
9320
9321 return new UnreadConversation(messages,
9322 remoteInput,
9323 onReply,
9324 onRead,
9325 participants, b.getLong(KEY_TIMESTAMP));
9326 }
9327 };
9328
9329 /**
9330 * Builder class for {@link CarExtender.UnreadConversation} objects.
9331 */
9332 public static class Builder {
9333 private final List<String> mMessages = new ArrayList<String>();
9334 private final String mParticipant;
9335 private RemoteInput mRemoteInput;
9336 private PendingIntent mReadPendingIntent;
9337 private PendingIntent mReplyPendingIntent;
9338 private long mLatestTimestamp;
9339
9340 /**
9341 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9342 *
9343 * @param name The name of the other participant in the conversation.
9344 */
9345 public Builder(String name) {
9346 mParticipant = name;
9347 }
9348
9349 /**
9350 * Appends a new unread message to the list of messages for this conversation.
9351 *
9352 * The messages should be added from oldest to newest.
9353 *
9354 * @param message The text of the new unread message.
9355 * @return This object for method chaining.
9356 */
9357 public Builder addMessage(String message) {
9358 mMessages.add(message);
9359 return this;
9360 }
9361
9362 /**
9363 * Sets the pending intent and remote input which will convey the reply to this
9364 * notification.
9365 *
9366 * @param pendingIntent The pending intent which will be triggered on a reply.
9367 * @param remoteInput The remote input parcelable which will carry the reply.
9368 * @return This object for method chaining.
9369 *
9370 * @see CarExtender.UnreadConversation#getRemoteInput
9371 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9372 */
9373 public Builder setReplyAction(
9374 PendingIntent pendingIntent, RemoteInput remoteInput) {
9375 mRemoteInput = remoteInput;
9376 mReplyPendingIntent = pendingIntent;
9377
9378 return this;
9379 }
9380
9381 /**
9382 * Sets the pending intent that will be sent once the messages in this notification
9383 * are read.
9384 *
9385 * @param pendingIntent The pending intent to use.
9386 * @return This object for method chaining.
9387 */
9388 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9389 mReadPendingIntent = pendingIntent;
9390 return this;
9391 }
9392
9393 /**
9394 * Sets the timestamp of the most recent message in an unread conversation.
9395 *
9396 * If a messaging notification has been posted by your application and has not
9397 * yet been cancelled, posting a later notification with the same id and tag
9398 * but without a newer timestamp may result in Android Auto not displaying a
9399 * heads up notification for the later notification.
9400 *
9401 * @param timestamp The timestamp of the most recent message in the conversation.
9402 * @return This object for method chaining.
9403 */
9404 public Builder setLatestTimestamp(long timestamp) {
9405 mLatestTimestamp = timestamp;
9406 return this;
9407 }
9408
9409 /**
9410 * Builds a new unread conversation object.
9411 *
9412 * @return The new unread conversation object.
9413 */
9414 public UnreadConversation build() {
9415 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9416 String[] participants = { mParticipant };
9417 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9418 mReadPendingIntent, participants, mLatestTimestamp);
9419 }
9420 }
9421 }
9422
9423 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009424 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9425 * with a TV extension:
9426 *
9427 * <ol>
9428 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9429 * <li>Create a {@link TvExtender}.
9430 * <li>Set TV-specific properties using the {@code set} methods of
9431 * {@link TvExtender}.
9432 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9433 * to apply the extension to a notification.
9434 * </ol>
9435 *
9436 * <pre class="prettyprint">
9437 * Notification notification = new Notification.Builder(context)
9438 * ...
9439 * .extend(new TvExtender()
9440 * .set*(...))
9441 * .build();
9442 * </pre>
9443 *
9444 * <p>TV extensions can be accessed on an existing notification by using the
9445 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9446 * to access values.
9447 *
9448 * @hide
9449 */
9450 @SystemApi
9451 public static final class TvExtender implements Extender {
9452 private static final String TAG = "TvExtender";
9453
9454 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9455 private static final String EXTRA_FLAGS = "flags";
9456 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9457 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009458 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009459 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009460
9461 // Flags bitwise-ored to mFlags
9462 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9463
9464 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009465 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009466 private PendingIntent mContentIntent;
9467 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009468 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009469
9470 /**
9471 * Create a {@link TvExtender} with default options.
9472 */
9473 public TvExtender() {
9474 mFlags = FLAG_AVAILABLE_ON_TV;
9475 }
9476
9477 /**
9478 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9479 *
9480 * @param notif The notification from which to copy options.
9481 */
9482 public TvExtender(Notification notif) {
9483 Bundle bundle = notif.extras == null ?
9484 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9485 if (bundle != null) {
9486 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009487 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009488 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009489 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9490 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9491 }
9492 }
9493
9494 /**
9495 * Apply a TV extension to a notification that is being built. This is typically called by
9496 * the {@link Notification.Builder#extend(Notification.Extender)}
9497 * method of {@link Notification.Builder}.
9498 */
9499 @Override
9500 public Notification.Builder extend(Notification.Builder builder) {
9501 Bundle bundle = new Bundle();
9502
9503 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009504 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009505 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009506 if (mContentIntent != null) {
9507 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9508 }
9509
9510 if (mDeleteIntent != null) {
9511 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9512 }
9513
9514 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9515 return builder;
9516 }
9517
9518 /**
9519 * Returns true if this notification should be shown on TV. This method return true
9520 * if the notification was extended with a TvExtender.
9521 */
9522 public boolean isAvailableOnTv() {
9523 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9524 }
9525
9526 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009527 * Specifies the channel the notification should be delivered on when shown on TV.
9528 * It can be different from the channel that the notification is delivered to when
9529 * posting on a non-TV device.
9530 */
9531 public TvExtender setChannel(String channelId) {
9532 mChannelId = channelId;
9533 return this;
9534 }
9535
9536 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009537 * Specifies the channel the notification should be delivered on when shown on TV.
9538 * It can be different from the channel that the notification is delivered to when
9539 * posting on a non-TV device.
9540 */
9541 public TvExtender setChannelId(String channelId) {
9542 mChannelId = channelId;
9543 return this;
9544 }
9545
Jeff Sharkey000ce802017-04-29 13:13:27 -06009546 /** @removed */
9547 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009548 public String getChannel() {
9549 return mChannelId;
9550 }
9551
9552 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009553 * Returns the id of the channel this notification posts to on TV.
9554 */
9555 public String getChannelId() {
9556 return mChannelId;
9557 }
9558
9559 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009560 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9561 * If provided, it is used instead of the content intent specified
9562 * at the level of Notification.
9563 */
9564 public TvExtender setContentIntent(PendingIntent intent) {
9565 mContentIntent = intent;
9566 return this;
9567 }
9568
9569 /**
9570 * Returns the TV-specific content intent. If this method returns null, the
9571 * main content intent on the notification should be used.
9572 *
9573 * @see {@link Notification#contentIntent}
9574 */
9575 public PendingIntent getContentIntent() {
9576 return mContentIntent;
9577 }
9578
9579 /**
9580 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9581 * by the user on TV. If provided, it is used instead of the delete intent specified
9582 * at the level of Notification.
9583 */
9584 public TvExtender setDeleteIntent(PendingIntent intent) {
9585 mDeleteIntent = intent;
9586 return this;
9587 }
9588
9589 /**
9590 * Returns the TV-specific delete intent. If this method returns null, the
9591 * main delete intent on the notification should be used.
9592 *
9593 * @see {@link Notification#deleteIntent}
9594 */
9595 public PendingIntent getDeleteIntent() {
9596 return mDeleteIntent;
9597 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009598
9599 /**
9600 * Specifies whether this notification should suppress showing a message over top of apps
9601 * outside of the launcher.
9602 */
9603 public TvExtender setSuppressShowOverApps(boolean suppress) {
9604 mSuppressShowOverApps = suppress;
9605 return this;
9606 }
9607
9608 /**
9609 * Returns true if this notification should not show messages over top of apps
9610 * outside of the launcher.
9611 */
9612 public boolean getSuppressShowOverApps() {
9613 return mSuppressShowOverApps;
9614 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009615 }
9616
9617 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009618 * Get an array of Notification objects from a parcelable array bundle field.
9619 * Update the bundle to have a typed array so fetches in the future don't need
9620 * to do an array copy.
9621 */
9622 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9623 Parcelable[] array = bundle.getParcelableArray(key);
9624 if (array instanceof Notification[] || array == null) {
9625 return (Notification[]) array;
9626 }
9627 Notification[] typedArray = Arrays.copyOf(array, array.length,
9628 Notification[].class);
9629 bundle.putParcelableArray(key, typedArray);
9630 return typedArray;
9631 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009632
9633 private static class BuilderRemoteViews extends RemoteViews {
9634 public BuilderRemoteViews(Parcel parcel) {
9635 super(parcel);
9636 }
9637
Kenny Guy77320062014-08-27 21:37:15 +01009638 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9639 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009640 }
9641
9642 @Override
9643 public BuilderRemoteViews clone() {
9644 Parcel p = Parcel.obtain();
9645 writeToParcel(p, 0);
9646 p.setDataPosition(0);
9647 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9648 p.recycle();
9649 return brv;
9650 }
9651 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009652
Selim Cinek384804b2018-04-18 14:31:07 +08009653 /**
9654 * A result object where information about the template that was created is saved.
9655 */
9656 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009657 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009658
9659 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009660 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009661 * with the right actions.
9662 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009663 public int getIconMarginEnd() {
9664 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009665 }
9666
Selim Cinek1c72fa02018-04-23 18:00:54 +08009667 public void setIconMarginEnd(int iconMarginEnd) {
9668 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009669 }
9670 }
9671
Adrian Roos70d7aa32017-01-11 15:39:06 -08009672 private static class StandardTemplateParams {
9673 boolean hasProgress = true;
9674 boolean ambient = false;
9675 CharSequence title;
9676 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009677 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009678 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009679 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009680 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009681
9682 final StandardTemplateParams reset() {
9683 hasProgress = true;
9684 ambient = false;
9685 title = null;
9686 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009687 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009688 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009689 return this;
9690 }
9691
9692 final StandardTemplateParams hasProgress(boolean hasProgress) {
9693 this.hasProgress = hasProgress;
9694 return this;
9695 }
9696
9697 final StandardTemplateParams title(CharSequence title) {
9698 this.title = title;
9699 return this;
9700 }
9701
9702 final StandardTemplateParams text(CharSequence text) {
9703 this.text = text;
9704 return this;
9705 }
9706
Selim Cinekafeed292017-12-12 17:32:44 -08009707 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9708 this.headerTextSecondary = text;
9709 return this;
9710 }
9711
Selim Cinek384804b2018-04-18 14:31:07 +08009712 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9713 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009714 return this;
9715 }
9716
Selim Cinek384804b2018-04-18 14:31:07 +08009717 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9718 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009719 return this;
9720 }
9721
Adrian Roos70d7aa32017-01-11 15:39:06 -08009722 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009723 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009724 this.ambient = ambient;
9725 return this;
9726 }
9727
9728 final StandardTemplateParams fillTextsFrom(Builder b) {
9729 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009730 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9731
9732 // Big text notifications should contain their content when viewed in ambient mode.
9733 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9734 if (!ambient || TextUtils.isEmpty(text)) {
9735 text = extras.getCharSequence(EXTRA_TEXT);
9736 }
9737 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009738 return this;
9739 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009740
9741 /**
9742 * Set the maximum lines of remote input history lines allowed.
9743 * @param maxRemoteInputHistory The number of lines.
9744 * @return The builder for method chaining.
9745 */
9746 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9747 this.maxRemoteInputHistory = maxRemoteInputHistory;
9748 return this;
9749 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009751}