blob: d8e1c895a6ebdac7ac34104050767a99b39b559b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050092import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050093import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095/**
96 * A class that represents how a persistent notification is to be presented to
97 * the user using the {@link android.app.NotificationManager}.
98 *
Joe Onoratocb109a02011-01-18 17:57:41 -080099 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
100 * easier to construct Notifications.</p>
101 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700102 * <div class="special reference">
103 * <h3>Developer Guides</h3>
104 * <p>For a guide to creating notifications, read the
105 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
106 * developer guide.</p>
107 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 */
109public class Notification implements Parcelable
110{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400111 private static final String TAG = "Notification";
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400114 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400115 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400116 */
117 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
118 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
119 = "android.intent.category.NOTIFICATION_PREFERENCES";
120
121 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500122 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
123 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400124 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500125 */
126 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
127
128 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400129 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400130 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
131 * what settings should be shown in the target app.
132 */
133 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
134
135 /**
136 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400137 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
138 * that can be used to narrow down what settings should be shown in the target app.
139 */
140 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
141
142 /**
143 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
144 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
145 * that can be used to narrow down what settings should be shown in the target app.
146 */
147 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
148
149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * Use all default values (where applicable).
151 */
152 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /**
155 * Use the default notification sound. This will ignore any given
156 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157 *
Chris Wren47c20a12014-06-18 17:27:29 -0400158 * <p>
159 * A notification that is noisy is more likely to be presented as a heads-up notification.
160 * </p>
161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 */
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 public static final int DEFAULT_SOUND = 1;
166
167 /**
168 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700170 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 *
Chris Wren47c20a12014-06-18 17:27:29 -0400172 * <p>
173 * A notification that vibrates is more likely to be presented as a heads-up notification.
174 * </p>
175 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 */
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 /**
182 * Use the default notification lights. This will ignore the
183 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
184 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500187 */
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200192 * Maximum length of CharSequences accepted by Builder and friends.
193 *
194 * <p>
195 * Avoids spamming the system with overly large strings such as full e-mails.
196 */
197 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
198
199 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800200 * Maximum entries of reply text that are accepted by Builder and friends.
201 */
202 private static final int MAX_REPLY_HISTORY = 5;
203
Selim Cinekde4de0e2018-01-24 16:21:07 -0800204
205 /**
206 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
207 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
208 *
209 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
210 * sends messages.</p>
211 */
212 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
213
Adrian Roose458aa82015-12-08 16:17:19 -0800214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * Default value: {@link System#currentTimeMillis() Now}.
218 *
219 * Choose a timestamp that will be most relevant to the user. For most finite events, this
220 * corresponds to the time the event happened (or will happen, in the case of events that have
221 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800222 * timestamped according to when the activity began.
223 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500224 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800225 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * <ul>
227 * <li>Notification of a new chat message should be stamped when the message was received.</li>
228 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
229 * <li>Notification of a completed file download should be stamped when the download finished.</li>
230 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
231 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
232 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800233 * </ul>
234 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700235 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
236 * anymore by default and must be opted into by using
237 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
239 public long when;
240
241 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700242 * The creation time of the notification
243 */
244 private long creationTime;
245
246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400248 *
249 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Dan Sandler86647982015-05-13 23:41:13 -0400251 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700252 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public int icon;
254
255 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800256 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
257 * leave it at its default value of 0.
258 *
259 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700260 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800261 */
262 public int iconLevel;
263
264 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500265 * The number of events that this notification represents. For example, in a new mail
266 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800267 *
Julia Reynolds30331982017-04-27 10:12:50 -0400268 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500269 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
270 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 */
Julia Reynolds30331982017-04-27 10:12:50 -0400272 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
274 /**
275 * The intent to execute when the expanded status entry is clicked. If
276 * this is an activity, it must include the
277 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800278 * that you take care of task management as described in the
279 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800280 * Stack</a> document. In particular, make sure to read the notification section
281 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
282 * Notifications</a> for the correct ways to launch an application from a
283 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 */
285 public PendingIntent contentIntent;
286
287 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500288 * The intent to execute when the notification is explicitly dismissed by the user, either with
289 * the "Clear All" button or by swiping it away individually.
290 *
291 * This probably shouldn't be launching an activity since several of those will be sent
292 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 */
294 public PendingIntent deleteIntent;
295
296 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700297 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800298 *
Chris Wren47c20a12014-06-18 17:27:29 -0400299 * <p>
300 * The system UI may choose to display a heads-up notification, instead of
301 * launching this intent, while the user is using the device.
302 * </p>
303 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800304 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400305 */
306 public PendingIntent fullScreenIntent;
307
308 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400309 * Text that summarizes this notification for accessibility services.
310 *
311 * As of the L release, this text is no longer shown on screen, but it is still useful to
312 * accessibility services (where it serves as an audible announcement of the notification's
313 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400314 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800315 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
317 public CharSequence tickerText;
318
319 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400320 * Formerly, a view showing the {@link #tickerText}.
321 *
322 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400323 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400324 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800325 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400326
327 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400328 * The view that will represent this notification in the notification list (which is pulled
329 * down from the status bar).
330 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500331 * As of N, this field may be null. The notification view is determined by the inputs
332 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400333 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 public RemoteViews contentView;
337
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400338 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400339 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400340 * opportunity to show more detail. The system UI may choose to show this
341 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400342 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500343 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
345 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400346 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400347 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400348 public RemoteViews bigContentView;
349
Chris Wren8fd39ec2014-02-27 17:43:26 -0500350
351 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400352 * A medium-format version of {@link #contentView}, providing the Notification an
353 * opportunity to add action buttons to contentView. At its discretion, the system UI may
354 * choose to show this as a heads-up notification, which will pop up so the user can see
355 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400356 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500357 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400358 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
359 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500360 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400361 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500362 public RemoteViews headsUpContentView;
363
Julia Reynoldsfc640012018-02-21 12:25:27 -0500364 private boolean mUsesStandardHeader;
365
366 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
367 static {
368 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
369 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
370 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
371 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
372 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
373 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
379 }
380
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400381 /**
Dan Sandler86647982015-05-13 23:41:13 -0400382 * A large bitmap to be shown in the notification content area.
383 *
384 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 */
Dan Sandler86647982015-05-13 23:41:13 -0400386 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800387 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
389 /**
390 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500391 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400393 * A notification that is noisy is more likely to be presented as a heads-up notification.
394 * </p>
395 *
396 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500397 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500401 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 public Uri sound;
403
404 /**
405 * Use this constant as the value for audioStreamType to request that
406 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700407 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400408 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public static final int STREAM_DEFAULT = -1;
413
414 /**
415 * The audio stream type to use when playing the sound.
416 * Should be one of the STREAM_ constants from
417 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 *
419 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public int audioStreamType = STREAM_DEFAULT;
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400425 * The default value of {@link #audioAttributes}.
426 */
427 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
428 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
429 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
430 .build();
431
432 /**
433 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 *
435 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400436 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500437 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400438 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
439
440 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500441 * The pattern with which to vibrate.
442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * <p>
444 * To vibrate the default pattern, see {@link #defaults}.
445 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500446 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500448 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500450 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 public long[] vibrate;
452
453 /**
454 * The color of the led. The hardware will do its best approximation.
455 *
456 * @see #FLAG_SHOW_LIGHTS
457 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500458 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
Tor Norbye80756e32015-03-02 09:39:27 -0800460 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public int ledARGB;
463
464 /**
465 * The number of milliseconds for the LED to be on while it's flashing.
466 * The hardware will do its best approximation.
467 *
468 * @see #FLAG_SHOW_LIGHTS
469 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500470 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500472 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public int ledOnMS;
474
475 /**
476 * The number of milliseconds for the LED to be off while it's flashing.
477 * The hardware will do its best approximation.
478 *
479 * @see #FLAG_SHOW_LIGHTS
480 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 *
482 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500484 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 public int ledOffMS;
486
487 /**
488 * Specifies which values should be taken from the defaults.
489 * <p>
490 * To set, OR the desired from {@link #DEFAULT_SOUND},
491 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
492 * values, use {@link #DEFAULT_ALL}.
493 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500494 *
495 * @deprecated use {@link NotificationChannel#getSound()} and
496 * {@link NotificationChannel#shouldShowLights()} and
497 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500499 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public int defaults;
501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
504 * set if you want the LED on for this notification.
505 * <ul>
506 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
507 * or 0 for both ledOnMS and ledOffMS.</li>
508 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
509 * <li>To flash the LED, pass the number of milliseconds that it should
510 * be on and off to ledOnMS and ledOffMS.</li>
511 * </ul>
512 * <p>
513 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800514 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * because they will be set to values that work on any given hardware.
516 * <p>
517 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500518 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500519 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500521 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
523
524 /**
525 * Bit to be bitwise-ored into the {@link #flags} field that should be
526 * set if this notification is in reference to something that is ongoing,
527 * like a phone call. It should not be set if this notification is in
528 * reference to something that happened at a particular point in time,
529 * like a missed phone call.
530 */
531 public static final int FLAG_ONGOING_EVENT = 0x00000002;
532
533 /**
534 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700535 * the audio will be repeated until the notification is
536 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 */
538 public static final int FLAG_INSISTENT = 0x00000004;
539
540 /**
541 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700542 * set if you would only like the sound, vibrate and ticker to be played
543 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 */
545 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
546
547 /**
548 * Bit to be bitwise-ored into the {@link #flags} field that should be
549 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500550 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 */
552 public static final int FLAG_AUTO_CANCEL = 0x00000010;
553
554 /**
555 * Bit to be bitwise-ored into the {@link #flags} field that should be
556 * set if the notification should not be canceled when the user clicks
557 * the Clear all button.
558 */
559 public static final int FLAG_NO_CLEAR = 0x00000020;
560
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700561 /**
562 * Bit to be bitwise-ored into the {@link #flags} field that should be
563 * set if this notification represents a currently running service. This
564 * will normally be set for you by {@link Service#startForeground}.
565 */
566 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
567
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400568 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800570 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400572 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700573 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400575
Griff Hazendfcb0802014-02-11 12:00:00 -0800576 /**
577 * Bit to be bitswise-ored into the {@link #flags} field that should be
578 * set if this notification is relevant to the current device only
579 * and it is not recommended that it bridge to other devices.
580 */
581 public static final int FLAG_LOCAL_ONLY = 0x00000100;
582
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700583 /**
584 * Bit to be bitswise-ored into the {@link #flags} field that should be
585 * set if this notification is the group summary for a group of notifications.
586 * Grouped notifications may display in a cluster or stack on devices which
587 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
588 */
589 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
590
Julia Reynoldse46bb372016-03-17 11:05:58 -0400591 /**
592 * Bit to be bitswise-ored into the {@link #flags} field that should be
593 * set if this notification is the group summary for an auto-group of notifications.
594 *
595 * @hide
596 */
597 @SystemApi
598 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
599
Julia Reynolds4db59552017-06-30 13:34:01 -0400600 /**
601 * @hide
602 */
603 public static final int FLAG_CAN_COLORIZE = 0x00000800;
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public int flags;
606
Tor Norbyed9273d62013-05-30 15:59:53 -0700607 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700608 @IntDef(prefix = { "PRIORITY_" }, value = {
609 PRIORITY_DEFAULT,
610 PRIORITY_LOW,
611 PRIORITY_MIN,
612 PRIORITY_HIGH,
613 PRIORITY_MAX
614 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700615 @Retention(RetentionPolicy.SOURCE)
616 public @interface Priority {}
617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 * Default notification {@link #priority}. If your application does not prioritize its own
620 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500621 *
622 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500623 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500624 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 public static final int PRIORITY_DEFAULT = 0;
626
627 /**
628 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
629 * items smaller, or at a different position in the list, compared with your app's
630 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500631 *
632 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500634 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 public static final int PRIORITY_LOW = -1;
636
637 /**
638 * Lowest {@link #priority}; these items might not be shown to the user except under special
639 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500640 *
641 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500642 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500643 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 public static final int PRIORITY_MIN = -2;
645
646 /**
647 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
648 * show these items larger, or at a different position in notification lists, compared with
649 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500650 *
651 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500653 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500654 public static final int PRIORITY_HIGH = 1;
655
656 /**
657 * Highest {@link #priority}, for your application's most important items that require the
658 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500659 *
660 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500662 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663 public static final int PRIORITY_MAX = 2;
664
665 /**
666 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800667 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668 * Priority is an indication of how much of the user's valuable attention should be consumed by
669 * this notification. Low-priority notifications may be hidden from the user in certain
670 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500671 * system will make a determination about how to interpret this priority when presenting
672 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400673 *
674 * <p>
675 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
676 * as a heads-up notification.
677 * </p>
678 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500679 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500680 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700681 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500682 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500683 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800684
Dan Sandler26e81cf2014-05-06 10:01:27 -0400685 /**
686 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
687 * to be applied by the standard Style templates when presenting this notification.
688 *
689 * The current template design constructs a colorful header image by overlaying the
690 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
691 * ignored.
692 */
Tor Norbye80756e32015-03-02 09:39:27 -0800693 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400694 public int color = COLOR_DEFAULT;
695
696 /**
697 * Special value of {@link #color} telling the system not to decorate this notification with
698 * any special color but instead use default colors when presenting this notification.
699 */
Tor Norbye80756e32015-03-02 09:39:27 -0800700 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400701 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600702
703 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800704 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800705 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800706 */
707 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800708 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800709
710 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700711 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
712 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600713 * lockscreen).
714 *
715 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
716 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
717 * shown in all situations, but the contents are only available if the device is unlocked for
718 * the appropriate user.
719 *
720 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
721 * can be read even in an "insecure" context (that is, above a secure lockscreen).
722 * To modify the public version of this notification—for example, to redact some portions—see
723 * {@link Builder#setPublicVersion(Notification)}.
724 *
725 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
726 * and ticker until the user has bypassed the lockscreen.
727 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600728 public @Visibility int visibility;
729
730 /** @hide */
731 @IntDef(prefix = { "VISIBILITY_" }, value = {
732 VISIBILITY_PUBLIC,
733 VISIBILITY_PRIVATE,
734 VISIBILITY_SECRET,
735 })
736 @Retention(RetentionPolicy.SOURCE)
737 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600738
Griff Hazenfc3922d2014-08-20 11:56:44 -0700739 /**
740 * Notification visibility: Show this notification in its entirety on all lockscreens.
741 *
742 * {@see #visibility}
743 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600744 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700745
746 /**
747 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
748 * private information on secure lockscreens.
749 *
750 * {@see #visibility}
751 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600752 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700753
754 /**
755 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
756 *
757 * {@see #visibility}
758 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600759 public static final int VISIBILITY_SECRET = -1;
760
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500761 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400762 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500763 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400764 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500765
766 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700767 * Notification category: map turn-by-turn navigation.
768 */
769 public static final String CATEGORY_NAVIGATION = "navigation";
770
771 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400772 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400774 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500775
776 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400777 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500778 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400779 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500780
781 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400782 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500783 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400784 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
786 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400787 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500788 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400789 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500790
791 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400792 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500793 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 public static final String CATEGORY_ALARM = "alarm";
795
796 /**
797 * Notification category: progress of a long-running background operation.
798 */
799 public static final String CATEGORY_PROGRESS = "progress";
800
801 /**
802 * Notification category: social network or sharing update.
803 */
804 public static final String CATEGORY_SOCIAL = "social";
805
806 /**
807 * Notification category: error in background operation or authentication status.
808 */
809 public static final String CATEGORY_ERROR = "err";
810
811 /**
812 * Notification category: media transport control for playback.
813 */
814 public static final String CATEGORY_TRANSPORT = "transport";
815
816 /**
817 * Notification category: system or device status update. Reserved for system use.
818 */
819 public static final String CATEGORY_SYSTEM = "sys";
820
821 /**
822 * Notification category: indication of running background service.
823 */
824 public static final String CATEGORY_SERVICE = "service";
825
826 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400827 * Notification category: a specific, timely recommendation for a single thing.
828 * For example, a news app might want to recommend a news story it believes the user will
829 * want to read next.
830 */
831 public static final String CATEGORY_RECOMMENDATION = "recommendation";
832
833 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400834 * Notification category: ongoing information about device or contextual status.
835 */
836 public static final String CATEGORY_STATUS = "status";
837
838 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400839 * Notification category: user-scheduled reminder.
840 */
841 public static final String CATEGORY_REMINDER = "reminder";
842
843 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700844 * Notification category: extreme car emergencies.
845 * @hide
846 */
847 @SystemApi
848 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
849
850 /**
851 * Notification category: car warnings.
852 * @hide
853 */
854 @SystemApi
855 public static final String CATEGORY_CAR_WARNING = "car_warning";
856
857 /**
858 * Notification category: general car system information.
859 * @hide
860 */
861 @SystemApi
862 public static final String CATEGORY_CAR_INFORMATION = "car_information";
863
864 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400865 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
866 * that best describes this Notification. May be used by the system for ranking and filtering.
867 */
868 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500869
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700870 private String mGroupKey;
871
872 /**
873 * Get the key used to group this notification into a cluster or stack
874 * with other notifications on devices which support such rendering.
875 */
876 public String getGroup() {
877 return mGroupKey;
878 }
879
880 private String mSortKey;
881
882 /**
883 * Get a sort key that orders this notification among other notifications from the
884 * same package. This can be useful if an external sort was already applied and an app
885 * would like to preserve this. Notifications will be sorted lexicographically using this
886 * value, although providing different priorities in addition to providing sort key may
887 * cause this value to be ignored.
888 *
889 * <p>This sort key can also be used to order members of a notification group. See
890 * {@link Builder#setGroup}.
891 *
892 * @see String#compareTo(String)
893 */
894 public String getSortKey() {
895 return mSortKey;
896 }
897
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500898 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400899 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900 * <p>
901 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
902 * APIs, and are intended to be used by
903 * {@link android.service.notification.NotificationListenerService} implementations to extract
904 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500905 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400906 public Bundle extras = new Bundle();
907
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400908 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700909 * All pending intents in the notification as the system needs to be able to access them but
910 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700911 * custom parcelable objects.
912 *
913 * @hide
914 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700915 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700916
917 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700918 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
919 * pending intents inside of it, so only those will get the behavior.
920 *
921 * @hide
922 */
Adrian Roosfb921842017-10-26 14:49:56 +0200923 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700924
925 /**
926 * Must be set by a process to start associating tokens with Notification objects
927 * coming in to it. This is set by NotificationManagerService.
928 *
929 * @hide
930 */
931 static public IBinder processWhitelistToken;
932
933 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934 * {@link #extras} key: this is the title of the notification,
935 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
936 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500937 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400938
939 /**
940 * {@link #extras} key: this is the title of the notification when shown in expanded form,
941 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
942 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400943 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944
945 /**
946 * {@link #extras} key: this is the main text payload, as supplied to
947 * {@link Builder#setContentText(CharSequence)}.
948 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500949 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is a third line of text, as supplied to
953 * {@link Builder#setSubText(CharSequence)}.
954 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800958 * {@link #extras} key: this is the remote input history, as supplied to
959 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700960 *
961 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
962 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
963 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
964 * notifications once the other party has responded).
965 *
966 * The extra with this key is of type CharSequence[] and contains the most recent entry at
967 * the 0 index, the second most recent at the 1 index, etc.
968 *
969 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800970 */
971 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
972
973 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974 * {@link #extras} key: this is a small piece of additional text as supplied to
975 * {@link Builder#setContentInfo(CharSequence)}.
976 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400977 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400978
979 /**
980 * {@link #extras} key: this is a line of summary information intended to be shown
981 * alongside expanded notifications, as supplied to (e.g.)
982 * {@link BigTextStyle#setSummaryText(CharSequence)}.
983 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400984 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400985
986 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200987 * {@link #extras} key: this is the longer text shown in the big form of a
988 * {@link BigTextStyle} notification, as supplied to
989 * {@link BigTextStyle#bigText(CharSequence)}.
990 */
991 public static final String EXTRA_BIG_TEXT = "android.bigText";
992
993 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400994 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
995 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400996 *
997 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400998 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400999 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001000 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001
1002 /**
1003 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1004 * notification payload, as
1005 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001006 *
1007 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001009 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001010 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001011
1012 /**
1013 * {@link #extras} key: this is a bitmap to be used instead of the one from
1014 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1015 * shown in its expanded form, as supplied to
1016 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1017 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001018 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019
1020 /**
1021 * {@link #extras} key: this is the progress value supplied to
1022 * {@link Builder#setProgress(int, int, boolean)}.
1023 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001024 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001025
1026 /**
1027 * {@link #extras} key: this is the maximum value supplied to
1028 * {@link Builder#setProgress(int, int, boolean)}.
1029 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001030 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001031
1032 /**
1033 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1034 * {@link Builder#setProgress(int, int, boolean)}.
1035 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001036 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037
1038 /**
1039 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1040 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1041 * {@link Builder#setUsesChronometer(boolean)}.
1042 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001043 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044
1045 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001046 * {@link #extras} key: whether the chronometer set on the notification should count down
1047 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001048 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001049 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001050 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001051
1052 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001053 * {@link #extras} key: whether {@link #when} should be shown,
1054 * as supplied to {@link Builder#setShowWhen(boolean)}.
1055 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001056 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001057
1058 /**
1059 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1060 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1061 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001062 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001063
1064 /**
1065 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1066 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1067 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001068 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001069
1070 /**
1071 * {@link #extras} key: A string representing the name of the specific
1072 * {@link android.app.Notification.Style} used to create this notification.
1073 */
Chris Wren91ad5632013-06-05 15:05:57 -04001074 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001075
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001076 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001077 * {@link #extras} key: A String array containing the people that this notification relates to,
1078 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001079 *
1080 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001081 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001082 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001083
1084 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001085 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1086 * this notification relates to.
1087 */
1088 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1089
1090 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001091 * Allow certain system-generated notifications to appear before the device is provisioned.
1092 * Only available to notifications coming from the android package.
1093 * @hide
1094 */
Maurice Lam96c10032017-03-29 15:42:38 -07001095 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001096 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001097 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1098
1099 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001100 * {@link #extras} key: A
1101 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001102 * in the background when the notification is selected. Used on television platforms.
1103 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001104 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001105 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001106 */
1107 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1108
1109 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001110 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001111 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001112 * {@link android.app.Notification.MediaStyle} notification.
1113 */
1114 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1115
1116 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001117 * {@link #extras} key: the indices of actions to be shown in the compact view,
1118 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1119 */
1120 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1121
Christoph Studer943aa672014-08-03 20:31:16 +02001122 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001123 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1124 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001125 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1126 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001127 *
1128 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001129 */
1130 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1131
1132 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001133 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1134 * direct replies
1135 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1136 * {@link Person}
1137 */
1138 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1139
1140 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001141 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001142 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001143 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001144 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001145
1146 /**
1147 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1148 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001149 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1150 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001151 */
1152 public static final String EXTRA_MESSAGES = "android.messages";
1153
1154 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001155 * {@link #extras} key: an array of
1156 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1157 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1158 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1159 * array of bundles.
1160 */
1161 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1162
1163 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001164 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1165 * represents a group conversation.
1166 */
1167 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1168
1169 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001170 * {@link #extras} key: whether the notification should be colorized as
1171 * supplied to {@link Builder#setColorized(boolean)}}.
1172 */
1173 public static final String EXTRA_COLORIZED = "android.colorized";
1174
1175 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001176 * @hide
1177 */
1178 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1179
Selim Cinek247fa012016-02-18 09:50:48 -08001180 /**
1181 * @hide
1182 */
1183 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1184
Shane Brennan472a3b32016-12-12 15:28:10 -08001185 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001186 * @hide
1187 */
1188 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1189
1190 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001191 * {@link #extras} key: the audio contents of this notification.
1192 *
1193 * This is for use when rendering the notification on an audio-focused interface;
1194 * the audio contents are a complete sound sample that contains the contents/body of the
1195 * notification. This may be used in substitute of a Text-to-Speech reading of the
1196 * notification. For example if the notification represents a voice message this should point
1197 * to the audio of that message.
1198 *
1199 * The data stored under this key should be a String representation of a Uri that contains the
1200 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1201 *
1202 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1203 * has a field for holding data URI. That field can be used for audio.
1204 * See {@code Message#setData}.
1205 *
1206 * Example usage:
1207 * <pre>
1208 * {@code
1209 * Notification.Builder myBuilder = (build your Notification as normal);
1210 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1211 * }
1212 * </pre>
1213 */
1214 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1215
Dan Sandler80eaa592016-04-14 23:34:54 -04001216 /** @hide */
1217 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001218 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001219 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1220
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001221 /**
1222 * This is set on the notification shown by the activity manager about all apps
1223 * running in the background. It indicates that the notification should be shown
1224 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1225 * notification currently visible to the user. This is a string array of all
1226 * package names of the apps.
1227 * @hide
1228 */
1229 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1230
Dan Sandlerd63f9322015-05-06 15:18:49 -04001231 private Icon mSmallIcon;
1232 private Icon mLargeIcon;
1233
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001234 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001235 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001236
Julia Reynolds13d898c2017-02-02 12:22:05 -05001237 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001238 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001239
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001240 /** @hide */
1241 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1242 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1243 })
1244 @Retention(RetentionPolicy.SOURCE)
1245 public @interface GroupAlertBehavior {}
1246
1247 /**
1248 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1249 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1250 * notification will not be muted when it is in a group.
1251 */
1252 public static final int GROUP_ALERT_ALL = 0;
1253
1254 /**
1255 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1256 * notification in a group should be silenced (no sound or vibration) even if they are posted
1257 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001258 * mute this notification if this notification is a group child. This must be applied to all
1259 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001260 *
1261 * <p> For example, you might want to use this constant if you post a number of children
1262 * notifications at once (say, after a periodic sync), and only need to notify the user
1263 * audibly once.
1264 */
1265 public static final int GROUP_ALERT_SUMMARY = 1;
1266
1267 /**
1268 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1269 * notification in a group should be silenced (no sound or vibration) even if they are
1270 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1271 * to mute this notification if this notification is a group summary.
1272 *
1273 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001274 * in your group have content and the summary is only used to visually group notifications
1275 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001276 */
1277 public static final int GROUP_ALERT_CHILDREN = 2;
1278
Julia Reynolds2f431e22017-06-07 14:12:09 +00001279 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001280
Julia Reynolds13d898c2017-02-02 12:22:05 -05001281 /**
1282 * If this notification is being shown as a badge, always show as a number.
1283 */
1284 public static final int BADGE_ICON_NONE = 0;
1285
1286 /**
1287 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1288 * represent this notification.
1289 */
1290 public static final int BADGE_ICON_SMALL = 1;
1291
1292 /**
1293 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1294 * represent this notification.
1295 */
1296 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001297 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001298
Chris Wren51c75102013-07-16 20:49:17 -04001299 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001300 * Structure to encapsulate a named action that can be shown as part of this notification.
1301 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1302 * selected by the user.
1303 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001304 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1305 * or {@link Notification.Builder#addAction(Notification.Action)}
1306 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001307 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001308 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001309 /**
1310 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1311 * {@link RemoteInput}s.
1312 *
1313 * This is intended for {@link RemoteInput}s that only accept data, meaning
1314 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1315 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1316 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1317 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1318 *
1319 * You can test if a RemoteInput matches these constraints using
1320 * {@link RemoteInput#isDataOnly}.
1321 */
1322 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1323
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001324 /**
1325 * {@link }: No semantic action defined.
1326 */
1327 public static final int SEMANTIC_ACTION_NONE = 0;
1328
1329 /**
1330 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1331 * may be appropriate.
1332 */
1333 public static final int SEMANTIC_ACTION_REPLY = 1;
1334
1335 /**
1336 * {@code SemanticAction}: Mark content as read.
1337 */
1338 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1339
1340 /**
1341 * {@code SemanticAction}: Mark content as unread.
1342 */
1343 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1344
1345 /**
1346 * {@code SemanticAction}: Delete the content associated with the notification. This
1347 * could mean deleting an email, message, etc.
1348 */
1349 public static final int SEMANTIC_ACTION_DELETE = 4;
1350
1351 /**
1352 * {@code SemanticAction}: Archive the content associated with the notification. This
1353 * could mean archiving an email, message, etc.
1354 */
1355 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1356
1357 /**
1358 * {@code SemanticAction}: Mute the content associated with the notification. This could
1359 * mean silencing a conversation or currently playing media.
1360 */
1361 public static final int SEMANTIC_ACTION_MUTE = 6;
1362
1363 /**
1364 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1365 * mean un-silencing a conversation or currently playing media.
1366 */
1367 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1368
1369 /**
1370 * {@code SemanticAction}: Mark content with a thumbs up.
1371 */
1372 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1373
1374 /**
1375 * {@code SemanticAction}: Mark content with a thumbs down.
1376 */
1377 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1378
Kodlee Yincda5b092018-02-15 15:34:53 -08001379 /**
1380 * {@code SemanticAction}: Call a contact, group, etc.
1381 */
1382 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001383
Griff Hazen959591e2014-05-15 22:26:18 -07001384 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001385 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001386 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001387 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001388 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001389
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001390 /**
1391 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001392 *
1393 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001394 */
Dan Sandler86647982015-05-13 23:41:13 -04001395 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001396 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001397
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001398 /**
1399 * Title of the action.
1400 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001401 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001402
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001403 /**
1404 * Intent to send when the user invokes this action. May be null, in which case the action
1405 * may be rendered in a disabled presentation by the system UI.
1406 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001407 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001408
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001409 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001410 if (in.readInt() != 0) {
1411 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001412 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1413 icon = mIcon.getResId();
1414 }
Dan Sandler86647982015-05-13 23:41:13 -04001415 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001416 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1417 if (in.readInt() == 1) {
1418 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1419 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001420 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001421 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001422 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001423 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001424 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001425
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001426 /**
Dan Sandler86647982015-05-13 23:41:13 -04001427 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001428 */
Dan Sandler86647982015-05-13 23:41:13 -04001429 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001430 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001431 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1432 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001433 }
1434
Adrian Roos7af53622016-10-12 13:44:05 -07001435 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001436 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001437 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1438 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001439 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001440 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1441 this.icon = icon.getResId();
1442 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001443 this.title = title;
1444 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001445 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001446 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001447 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001448 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001449 }
1450
1451 /**
Dan Sandler86647982015-05-13 23:41:13 -04001452 * Return an icon representing the action.
1453 */
1454 public Icon getIcon() {
1455 if (mIcon == null && icon != 0) {
1456 // you snuck an icon in here without using the builder; let's try to keep it
1457 mIcon = Icon.createWithResource("", icon);
1458 }
1459 return mIcon;
1460 }
1461
1462 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001463 * Get additional metadata carried around with this Action.
1464 */
1465 public Bundle getExtras() {
1466 return mExtras;
1467 }
1468
1469 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001470 * Return whether the platform should automatically generate possible replies for this
1471 * {@link Action}
1472 */
1473 public boolean getAllowGeneratedReplies() {
1474 return mAllowGeneratedReplies;
1475 }
1476
1477 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001478 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001479 * May return null if no remote inputs were added. Only returns inputs which accept
1480 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001481 */
1482 public RemoteInput[] getRemoteInputs() {
1483 return mRemoteInputs;
1484 }
1485
1486 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001487 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1488 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1489 * (eg. reply, mark as read, delete, etc).
1490 */
1491 public @SemanticAction int getSemanticAction() {
1492 return mSemanticAction;
1493 }
1494
1495 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001496 * Get the list of inputs to be collected from the user that ONLY accept data when this
1497 * action is sent. These remote inputs are guaranteed to return true on a call to
1498 * {@link RemoteInput#isDataOnly}.
1499 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001500 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001501 *
1502 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1503 * of non-textual RemoteInputs do not access these remote inputs.
1504 */
1505 public RemoteInput[] getDataOnlyRemoteInputs() {
1506 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1507 }
1508
1509 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001510 * Builder class for {@link Action} objects.
1511 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001512 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001513 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001514 private final CharSequence mTitle;
1515 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001516 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001517 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001518 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001519 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001520
1521 /**
1522 * Construct a new builder for {@link Action} object.
1523 * @param icon icon to show for this action
1524 * @param title the title of the action
1525 * @param intent the {@link PendingIntent} to fire when users trigger this action
1526 */
Dan Sandler86647982015-05-13 23:41:13 -04001527 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001528 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001529 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001530 }
1531
1532 /**
1533 * Construct a new builder for {@link Action} object.
1534 * @param icon icon to show for this action
1535 * @param title the title of the action
1536 * @param intent the {@link PendingIntent} to fire when users trigger this action
1537 */
1538 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001539 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001540 }
1541
1542 /**
1543 * Construct a new builder for {@link Action} object using the fields from an
1544 * {@link Action}.
1545 * @param action the action to read fields from.
1546 */
1547 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001548 this(action.getIcon(), action.title, action.actionIntent,
1549 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001550 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001551 }
1552
Dan Sandler86647982015-05-13 23:41:13 -04001553 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001554 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1555 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001556 mIcon = icon;
1557 mTitle = title;
1558 mIntent = intent;
1559 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001560 if (remoteInputs != null) {
1561 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1562 Collections.addAll(mRemoteInputs, remoteInputs);
1563 }
Adrian Roos7af53622016-10-12 13:44:05 -07001564 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001565 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001566 }
1567
1568 /**
1569 * Merge additional metadata into this builder.
1570 *
1571 * <p>Values within the Bundle will replace existing extras values in this Builder.
1572 *
1573 * @see Notification.Action#extras
1574 */
1575 public Builder addExtras(Bundle extras) {
1576 if (extras != null) {
1577 mExtras.putAll(extras);
1578 }
1579 return this;
1580 }
1581
1582 /**
1583 * Get the metadata Bundle used by this Builder.
1584 *
1585 * <p>The returned Bundle is shared with this Builder.
1586 */
1587 public Bundle getExtras() {
1588 return mExtras;
1589 }
1590
1591 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001592 * Add an input to be collected from the user when this action is sent.
1593 * Response values can be retrieved from the fired intent by using the
1594 * {@link RemoteInput#getResultsFromIntent} function.
1595 * @param remoteInput a {@link RemoteInput} to add to the action
1596 * @return this object for method chaining
1597 */
1598 public Builder addRemoteInput(RemoteInput remoteInput) {
1599 if (mRemoteInputs == null) {
1600 mRemoteInputs = new ArrayList<RemoteInput>();
1601 }
1602 mRemoteInputs.add(remoteInput);
1603 return this;
1604 }
1605
1606 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001607 * Set whether the platform should automatically generate possible replies to add to
1608 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1609 * {@link RemoteInput}, this has no effect.
1610 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1611 * otherwise
1612 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001613 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001614 */
1615 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1616 mAllowGeneratedReplies = allowGeneratedReplies;
1617 return this;
1618 }
1619
1620 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001621 * Sets the {@code SemanticAction} for this {@link Action}. A
1622 * {@code SemanticAction} denotes what an {@link Action}'s
1623 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1624 * @param semanticAction a SemanticAction defined within {@link Action} with
1625 * {@code SEMANTIC_ACTION_} prefixes
1626 * @return this object for method chaining
1627 */
1628 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1629 mSemanticAction = semanticAction;
1630 return this;
1631 }
1632
1633 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001634 * Apply an extender to this action builder. Extenders may be used to add
1635 * metadata or change options on this builder.
1636 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001637 public Builder extend(Extender extender) {
1638 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001639 return this;
1640 }
1641
1642 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001643 * Combine all of the options that have been set and return a new {@link Action}
1644 * object.
1645 * @return the built action
1646 */
1647 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001648 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1649 RemoteInput[] previousDataInputs =
1650 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001651 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001652 for (RemoteInput input : previousDataInputs) {
1653 dataOnlyInputs.add(input);
1654 }
1655 }
1656 List<RemoteInput> textInputs = new ArrayList<>();
1657 if (mRemoteInputs != null) {
1658 for (RemoteInput input : mRemoteInputs) {
1659 if (input.isDataOnly()) {
1660 dataOnlyInputs.add(input);
1661 } else {
1662 textInputs.add(input);
1663 }
1664 }
1665 }
1666 if (!dataOnlyInputs.isEmpty()) {
1667 RemoteInput[] dataInputsArr =
1668 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1669 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1670 }
1671 RemoteInput[] textInputsArr = textInputs.isEmpty()
1672 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1673 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001674 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001675 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001676 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001677
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001678 @Override
1679 public Action clone() {
1680 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001681 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001682 title,
1683 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001684 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001685 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001686 getAllowGeneratedReplies(),
1687 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001688 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001689
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001690 @Override
1691 public int describeContents() {
1692 return 0;
1693 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001694
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001695 @Override
1696 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001697 final Icon ic = getIcon();
1698 if (ic != null) {
1699 out.writeInt(1);
1700 ic.writeToParcel(out, 0);
1701 } else {
1702 out.writeInt(0);
1703 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001704 TextUtils.writeToParcel(title, out, flags);
1705 if (actionIntent != null) {
1706 out.writeInt(1);
1707 actionIntent.writeToParcel(out, flags);
1708 } else {
1709 out.writeInt(0);
1710 }
Griff Hazen959591e2014-05-15 22:26:18 -07001711 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001712 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001713 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001714 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001715 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001716
Griff Hazen959591e2014-05-15 22:26:18 -07001717 public static final Parcelable.Creator<Action> CREATOR =
1718 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 public Action createFromParcel(Parcel in) {
1720 return new Action(in);
1721 }
1722 public Action[] newArray(int size) {
1723 return new Action[size];
1724 }
1725 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001726
1727 /**
1728 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1729 * metadata or change options on an action builder.
1730 */
1731 public interface Extender {
1732 /**
1733 * Apply this extender to a notification action builder.
1734 * @param builder the builder to be modified.
1735 * @return the build object for chaining.
1736 */
1737 public Builder extend(Builder builder);
1738 }
1739
1740 /**
1741 * Wearable extender for notification actions. To add extensions to an action,
1742 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1743 * the {@code WearableExtender()} constructor and apply it to a
1744 * {@link android.app.Notification.Action.Builder} using
1745 * {@link android.app.Notification.Action.Builder#extend}.
1746 *
1747 * <pre class="prettyprint">
1748 * Notification.Action action = new Notification.Action.Builder(
1749 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001750 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001751 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001752 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001753 */
1754 public static final class WearableExtender implements Extender {
1755 /** Notification action extra which contains wearable extensions */
1756 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1757
Pete Gastaf6781d2014-10-07 15:17:05 -04001758 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001759 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001760 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1761 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1762 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001763
1764 // Flags bitwise-ored to mFlags
1765 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001766 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001767 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001768
1769 // Default value for flags integer
1770 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1771
1772 private int mFlags = DEFAULT_FLAGS;
1773
Pete Gastaf6781d2014-10-07 15:17:05 -04001774 private CharSequence mInProgressLabel;
1775 private CharSequence mConfirmLabel;
1776 private CharSequence mCancelLabel;
1777
Griff Hazen61a9e862014-05-22 16:05:19 -07001778 /**
1779 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1780 * options.
1781 */
1782 public WearableExtender() {
1783 }
1784
1785 /**
1786 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1787 * wearable options present in an existing notification action.
1788 * @param action the notification action to inspect.
1789 */
1790 public WearableExtender(Action action) {
1791 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1792 if (wearableBundle != null) {
1793 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001794 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1795 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1796 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001797 }
1798 }
1799
1800 /**
1801 * Apply wearable extensions to a notification action that is being built. This is
1802 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1803 * method of {@link android.app.Notification.Action.Builder}.
1804 */
1805 @Override
1806 public Action.Builder extend(Action.Builder builder) {
1807 Bundle wearableBundle = new Bundle();
1808
1809 if (mFlags != DEFAULT_FLAGS) {
1810 wearableBundle.putInt(KEY_FLAGS, mFlags);
1811 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001812 if (mInProgressLabel != null) {
1813 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1814 }
1815 if (mConfirmLabel != null) {
1816 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1817 }
1818 if (mCancelLabel != null) {
1819 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1820 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001821
1822 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1823 return builder;
1824 }
1825
1826 @Override
1827 public WearableExtender clone() {
1828 WearableExtender that = new WearableExtender();
1829 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001830 that.mInProgressLabel = this.mInProgressLabel;
1831 that.mConfirmLabel = this.mConfirmLabel;
1832 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001833 return that;
1834 }
1835
1836 /**
1837 * Set whether this action is available when the wearable device is not connected to
1838 * a companion device. The user can still trigger this action when the wearable device is
1839 * offline, but a visual hint will indicate that the action may not be available.
1840 * Defaults to true.
1841 */
1842 public WearableExtender setAvailableOffline(boolean availableOffline) {
1843 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1844 return this;
1845 }
1846
1847 /**
1848 * Get whether this action is available when the wearable device is not connected to
1849 * a companion device. The user can still trigger this action when the wearable device is
1850 * offline, but a visual hint will indicate that the action may not be available.
1851 * Defaults to true.
1852 */
1853 public boolean isAvailableOffline() {
1854 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1855 }
1856
1857 private void setFlag(int mask, boolean value) {
1858 if (value) {
1859 mFlags |= mask;
1860 } else {
1861 mFlags &= ~mask;
1862 }
1863 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001864
1865 /**
1866 * Set a label to display while the wearable is preparing to automatically execute the
1867 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1868 *
1869 * @param label the label to display while the action is being prepared to execute
1870 * @return this object for method chaining
1871 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001872 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001873 public WearableExtender setInProgressLabel(CharSequence label) {
1874 mInProgressLabel = label;
1875 return this;
1876 }
1877
1878 /**
1879 * Get the label to display while the wearable is preparing to automatically execute
1880 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1881 *
1882 * @return the label to display while the action is being prepared to execute
1883 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001884 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001885 public CharSequence getInProgressLabel() {
1886 return mInProgressLabel;
1887 }
1888
1889 /**
1890 * Set a label to display to confirm that the action should be executed.
1891 * This is usually an imperative verb like "Send".
1892 *
1893 * @param label the label to confirm the action should be executed
1894 * @return this object for method chaining
1895 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001896 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001897 public WearableExtender setConfirmLabel(CharSequence label) {
1898 mConfirmLabel = label;
1899 return this;
1900 }
1901
1902 /**
1903 * Get the label to display to confirm that the action should be executed.
1904 * This is usually an imperative verb like "Send".
1905 *
1906 * @return the label to confirm the action should be executed
1907 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001908 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001909 public CharSequence getConfirmLabel() {
1910 return mConfirmLabel;
1911 }
1912
1913 /**
1914 * Set a label to display to cancel the action.
1915 * This is usually an imperative verb, like "Cancel".
1916 *
1917 * @param label the label to display to cancel the action
1918 * @return this object for method chaining
1919 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001920 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001921 public WearableExtender setCancelLabel(CharSequence label) {
1922 mCancelLabel = label;
1923 return this;
1924 }
1925
1926 /**
1927 * Get the label to display to cancel the action.
1928 * This is usually an imperative verb like "Cancel".
1929 *
1930 * @return the label to display to cancel the action
1931 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001932 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001933 public CharSequence getCancelLabel() {
1934 return mCancelLabel;
1935 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001936
1937 /**
1938 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1939 * platform that it can generate the appropriate transitions.
1940 * @param hintLaunchesActivity {@code true} if the content intent will launch
1941 * an activity and transitions should be generated, false otherwise.
1942 * @return this object for method chaining
1943 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001944 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001945 boolean hintLaunchesActivity) {
1946 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1947 return this;
1948 }
1949
1950 /**
1951 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1952 * platform that it can generate the appropriate transitions
1953 * @return {@code true} if the content intent will launch an activity and transitions
1954 * should be generated, false otherwise. The default value is {@code false} if this was
1955 * never set.
1956 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001957 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001958 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1959 }
Alex Hills9f087612016-06-07 09:08:59 -04001960
1961 /**
1962 * Set a hint that this Action should be displayed inline.
1963 *
1964 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1965 * otherwise
1966 * @return this object for method chaining
1967 */
1968 public WearableExtender setHintDisplayActionInline(
1969 boolean hintDisplayInline) {
1970 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1971 return this;
1972 }
1973
1974 /**
1975 * Get a hint that this Action should be displayed inline.
1976 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001977 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001978 * otherwise. The default value is {@code false} if this was never set.
1979 */
1980 public boolean getHintDisplayActionInline() {
1981 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1982 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001983 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001984
1985 /**
1986 * Provides meaning to an {@link Action} that hints at what the associated
1987 * {@link PendingIntent} will do. For example, an {@link Action} with a
1988 * {@link PendingIntent} that replies to a text message notification may have the
1989 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1990 *
1991 * @hide
1992 */
1993 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1994 SEMANTIC_ACTION_NONE,
1995 SEMANTIC_ACTION_REPLY,
1996 SEMANTIC_ACTION_MARK_AS_READ,
1997 SEMANTIC_ACTION_MARK_AS_UNREAD,
1998 SEMANTIC_ACTION_DELETE,
1999 SEMANTIC_ACTION_ARCHIVE,
2000 SEMANTIC_ACTION_MUTE,
2001 SEMANTIC_ACTION_UNMUTE,
2002 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002003 SEMANTIC_ACTION_THUMBS_DOWN,
2004 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002005 })
2006 @Retention(RetentionPolicy.SOURCE)
2007 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002008 }
2009
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002010 /**
2011 * Array of all {@link Action} structures attached to this notification by
2012 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2013 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2014 * interface for invoking actions.
2015 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002016 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002017
2018 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002019 * Replacement version of this notification whose content will be shown
2020 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2021 * and {@link #VISIBILITY_PUBLIC}.
2022 */
2023 public Notification publicVersion;
2024
2025 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002026 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002027 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 */
2029 public Notification()
2030 {
2031 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002032 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002033 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035
2036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 * @hide
2038 */
2039 public Notification(Context context, int icon, CharSequence tickerText, long when,
2040 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2041 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002042 new Builder(context)
2043 .setWhen(when)
2044 .setSmallIcon(icon)
2045 .setTicker(tickerText)
2046 .setContentTitle(contentTitle)
2047 .setContentText(contentText)
2048 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2049 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
2051
2052 /**
2053 * Constructs a Notification object with the information needed to
2054 * have a status bar icon without the standard expanded view.
2055 *
2056 * @param icon The resource id of the icon to put in the status bar.
2057 * @param tickerText The text that flows by in the status bar when the notification first
2058 * activates.
2059 * @param when The time to show in the time field. In the System.currentTimeMillis
2060 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002061 *
2062 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002064 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 public Notification(int icon, CharSequence tickerText, long when)
2066 {
2067 this.icon = icon;
2068 this.tickerText = tickerText;
2069 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002070 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
2072
2073 /**
2074 * Unflatten the notification from a parcel.
2075 */
Svet Ganovddb94882016-06-23 19:55:24 -07002076 @SuppressWarnings("unchecked")
2077 public Notification(Parcel parcel) {
2078 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2079 // intents in extras are always written as the last entry.
2080 readFromParcelImpl(parcel);
2081 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002082 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002083 }
2084
2085 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 {
2087 int version = parcel.readInt();
2088
Adrian Roosfb921842017-10-26 14:49:56 +02002089 mWhitelistToken = parcel.readStrongBinder();
2090 if (mWhitelistToken == null) {
2091 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002092 }
2093 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002094 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002097 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002098 if (parcel.readInt() != 0) {
2099 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002100 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2101 icon = mSmallIcon.getResId();
2102 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 number = parcel.readInt();
2105 if (parcel.readInt() != 0) {
2106 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2107 }
2108 if (parcel.readInt() != 0) {
2109 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2110 }
2111 if (parcel.readInt() != 0) {
2112 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2113 }
2114 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002115 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002116 }
2117 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2119 }
Joe Onorato561d3852010-11-20 18:09:34 -08002120 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002121 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 defaults = parcel.readInt();
2124 flags = parcel.readInt();
2125 if (parcel.readInt() != 0) {
2126 sound = Uri.CREATOR.createFromParcel(parcel);
2127 }
2128
2129 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002130 if (parcel.readInt() != 0) {
2131 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 vibrate = parcel.createLongArray();
2134 ledARGB = parcel.readInt();
2135 ledOnMS = parcel.readInt();
2136 ledOffMS = parcel.readInt();
2137 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002138
2139 if (parcel.readInt() != 0) {
2140 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2141 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142
2143 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002144
John Spurlockfd7f1e02014-03-18 16:41:57 -04002145 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002147 mGroupKey = parcel.readString();
2148
2149 mSortKey = parcel.readString();
2150
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002151 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002152 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002153
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002154 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2155
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002156 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002157 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2158 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002159
Chris Wren8fd39ec2014-02-27 17:43:26 -05002160 if (parcel.readInt() != 0) {
2161 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2162 }
2163
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002164 visibility = parcel.readInt();
2165
2166 if (parcel.readInt() != 0) {
2167 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2168 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002169
2170 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002171
2172 if (parcel.readInt() != 0) {
2173 mChannelId = parcel.readString();
2174 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002175 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002176
2177 if (parcel.readInt() != 0) {
2178 mShortcutId = parcel.readString();
2179 }
2180
2181 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002182
2183 if (parcel.readInt() != 0) {
2184 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2185 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002186
2187 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189
Andy Stadler110988c2010-12-03 14:29:16 -08002190 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002191 public Notification clone() {
2192 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002193 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002194 return that;
2195 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002196
Daniel Sandler1a497d32013-04-18 14:52:45 -04002197 /**
2198 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2199 * of this into that.
2200 * @hide
2201 */
2202 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002203 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002204 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002205 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002206 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002207 that.number = this.number;
2208
2209 // PendingIntents are global, so there's no reason (or way) to clone them.
2210 that.contentIntent = this.contentIntent;
2211 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002212 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002213
2214 if (this.tickerText != null) {
2215 that.tickerText = this.tickerText.toString();
2216 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002217 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002218 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002219 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002220 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002221 that.contentView = this.contentView.clone();
2222 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002223 if (heavy && this.mLargeIcon != null) {
2224 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002225 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002226 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002227 that.sound = this.sound; // android.net.Uri is immutable
2228 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002229 if (this.audioAttributes != null) {
2230 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2231 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002232
2233 final long[] vibrate = this.vibrate;
2234 if (vibrate != null) {
2235 final int N = vibrate.length;
2236 final long[] vib = that.vibrate = new long[N];
2237 System.arraycopy(vibrate, 0, vib, 0, N);
2238 }
2239
2240 that.ledARGB = this.ledARGB;
2241 that.ledOnMS = this.ledOnMS;
2242 that.ledOffMS = this.ledOffMS;
2243 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002244
Joe Onorato18e69df2010-05-17 22:26:12 -07002245 that.flags = this.flags;
2246
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002247 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002248
John Spurlockfd7f1e02014-03-18 16:41:57 -04002249 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002250
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002251 that.mGroupKey = this.mGroupKey;
2252
2253 that.mSortKey = this.mSortKey;
2254
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002255 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002256 try {
2257 that.extras = new Bundle(this.extras);
2258 // will unparcel
2259 that.extras.size();
2260 } catch (BadParcelableException e) {
2261 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2262 that.extras = null;
2263 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002264 }
2265
Felipe Lemedd85da62016-06-28 11:29:54 -07002266 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2267 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002268 }
2269
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002270 if (this.actions != null) {
2271 that.actions = new Action[this.actions.length];
2272 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002273 if ( this.actions[i] != null) {
2274 that.actions[i] = this.actions[i].clone();
2275 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002276 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002277 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002278
Daniel Sandler1a497d32013-04-18 14:52:45 -04002279 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002280 that.bigContentView = this.bigContentView.clone();
2281 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002282
Chris Wren8fd39ec2014-02-27 17:43:26 -05002283 if (heavy && this.headsUpContentView != null) {
2284 that.headsUpContentView = this.headsUpContentView.clone();
2285 }
2286
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002287 that.visibility = this.visibility;
2288
2289 if (this.publicVersion != null) {
2290 that.publicVersion = new Notification();
2291 this.publicVersion.cloneInto(that.publicVersion, heavy);
2292 }
2293
Dan Sandler26e81cf2014-05-06 10:01:27 -04002294 that.color = this.color;
2295
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002296 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002297 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002298 that.mShortcutId = this.mShortcutId;
2299 that.mBadgeIcon = this.mBadgeIcon;
2300 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002301 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002302
Daniel Sandler1a497d32013-04-18 14:52:45 -04002303 if (!heavy) {
2304 that.lightenPayload(); // will clean out extras
2305 }
2306 }
2307
2308 /**
2309 * Removes heavyweight parts of the Notification object for archival or for sending to
2310 * listeners when the full contents are not necessary.
2311 * @hide
2312 */
2313 public final void lightenPayload() {
2314 tickerView = null;
2315 contentView = null;
2316 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002317 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002318 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002319 if (extras != null && !extras.isEmpty()) {
2320 final Set<String> keyset = extras.keySet();
2321 final int N = keyset.size();
2322 final String[] keys = keyset.toArray(new String[N]);
2323 for (int i=0; i<N; i++) {
2324 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002325 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2326 continue;
2327 }
Dan Sandler50128532015-12-08 15:42:41 -05002328 final Object obj = extras.get(key);
2329 if (obj != null &&
2330 ( obj instanceof Parcelable
2331 || obj instanceof Parcelable[]
2332 || obj instanceof SparseArray
2333 || obj instanceof ArrayList)) {
2334 extras.remove(key);
2335 }
2336 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002337 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002338 }
2339
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002340 /**
2341 * Make sure this CharSequence is safe to put into a bundle, which basically
2342 * means it had better not be some custom Parcelable implementation.
2343 * @hide
2344 */
2345 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002346 if (cs == null) return cs;
2347 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2348 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2349 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002350 if (cs instanceof Parcelable) {
2351 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2352 + " instance is a custom Parcelable and not allowed in Notification");
2353 return cs.toString();
2354 }
Selim Cinek60a54252016-02-26 17:03:25 -08002355 return removeTextSizeSpans(cs);
2356 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002357
Selim Cinek60a54252016-02-26 17:03:25 -08002358 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2359 if (charSequence instanceof Spanned) {
2360 Spanned ss = (Spanned) charSequence;
2361 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2362 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2363 for (Object span : spans) {
2364 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002365 if (resultSpan instanceof CharacterStyle) {
2366 resultSpan = ((CharacterStyle) span).getUnderlying();
2367 }
2368 if (resultSpan instanceof TextAppearanceSpan) {
2369 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002370 resultSpan = new TextAppearanceSpan(
2371 originalSpan.getFamily(),
2372 originalSpan.getTextStyle(),
2373 -1,
2374 originalSpan.getTextColor(),
2375 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002376 } else if (resultSpan instanceof RelativeSizeSpan
2377 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002378 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002379 } else {
2380 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002381 }
2382 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2383 ss.getSpanFlags(span));
2384 }
2385 return builder;
2386 }
2387 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002388 }
2389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 public int describeContents() {
2391 return 0;
2392 }
2393
2394 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002395 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 */
Svet Ganovddb94882016-06-23 19:55:24 -07002397 public void writeToParcel(Parcel parcel, int flags) {
2398 // We need to mark all pending intents getting into the notification
2399 // system as being put there to later allow the notification ranker
2400 // to launch them and by doing so add the app to the battery saver white
2401 // list for a short period of time. The problem is that the system
2402 // cannot look into the extras as there may be parcelables there that
2403 // the platform does not know how to handle. To go around that we have
2404 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002405 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002406 if (collectPendingIntents) {
2407 PendingIntent.setOnMarshaledListener(
2408 (PendingIntent intent, Parcel out, int outFlags) -> {
2409 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002410 if (allPendingIntents == null) {
2411 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002412 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002413 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002414 }
2415 });
2416 }
2417 try {
2418 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002419 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002420 writeToParcelImpl(parcel, flags);
2421 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002422 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002423 } finally {
2424 if (collectPendingIntents) {
2425 PendingIntent.setOnMarshaledListener(null);
2426 }
2427 }
2428 }
2429
2430 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 parcel.writeInt(1);
2432
Adrian Roosfb921842017-10-26 14:49:56 +02002433 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002435 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002436 if (mSmallIcon == null && icon != 0) {
2437 // you snuck an icon in here without using the builder; let's try to keep it
2438 mSmallIcon = Icon.createWithResource("", icon);
2439 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002440 if (mSmallIcon != null) {
2441 parcel.writeInt(1);
2442 mSmallIcon.writeToParcel(parcel, 0);
2443 } else {
2444 parcel.writeInt(0);
2445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 parcel.writeInt(number);
2447 if (contentIntent != null) {
2448 parcel.writeInt(1);
2449 contentIntent.writeToParcel(parcel, 0);
2450 } else {
2451 parcel.writeInt(0);
2452 }
2453 if (deleteIntent != null) {
2454 parcel.writeInt(1);
2455 deleteIntent.writeToParcel(parcel, 0);
2456 } else {
2457 parcel.writeInt(0);
2458 }
2459 if (tickerText != null) {
2460 parcel.writeInt(1);
2461 TextUtils.writeToParcel(tickerText, parcel, flags);
2462 } else {
2463 parcel.writeInt(0);
2464 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002465 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002466 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002467 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002468 } else {
2469 parcel.writeInt(0);
2470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 if (contentView != null) {
2472 parcel.writeInt(1);
2473 contentView.writeToParcel(parcel, 0);
2474 } else {
2475 parcel.writeInt(0);
2476 }
Selim Cinek279fa862016-06-14 10:57:25 -07002477 if (mLargeIcon == null && largeIcon != null) {
2478 // you snuck an icon in here without using the builder; let's try to keep it
2479 mLargeIcon = Icon.createWithBitmap(largeIcon);
2480 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002481 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002482 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002483 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002484 } else {
2485 parcel.writeInt(0);
2486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487
2488 parcel.writeInt(defaults);
2489 parcel.writeInt(this.flags);
2490
2491 if (sound != null) {
2492 parcel.writeInt(1);
2493 sound.writeToParcel(parcel, 0);
2494 } else {
2495 parcel.writeInt(0);
2496 }
2497 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002498
2499 if (audioAttributes != null) {
2500 parcel.writeInt(1);
2501 audioAttributes.writeToParcel(parcel, 0);
2502 } else {
2503 parcel.writeInt(0);
2504 }
2505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 parcel.writeLongArray(vibrate);
2507 parcel.writeInt(ledARGB);
2508 parcel.writeInt(ledOnMS);
2509 parcel.writeInt(ledOffMS);
2510 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002511
2512 if (fullScreenIntent != null) {
2513 parcel.writeInt(1);
2514 fullScreenIntent.writeToParcel(parcel, 0);
2515 } else {
2516 parcel.writeInt(0);
2517 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002518
2519 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002520
John Spurlockfd7f1e02014-03-18 16:41:57 -04002521 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002522
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002523 parcel.writeString(mGroupKey);
2524
2525 parcel.writeString(mSortKey);
2526
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002527 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002528
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002529 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002530
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002531 if (bigContentView != null) {
2532 parcel.writeInt(1);
2533 bigContentView.writeToParcel(parcel, 0);
2534 } else {
2535 parcel.writeInt(0);
2536 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002537
Chris Wren8fd39ec2014-02-27 17:43:26 -05002538 if (headsUpContentView != null) {
2539 parcel.writeInt(1);
2540 headsUpContentView.writeToParcel(parcel, 0);
2541 } else {
2542 parcel.writeInt(0);
2543 }
2544
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002545 parcel.writeInt(visibility);
2546
2547 if (publicVersion != null) {
2548 parcel.writeInt(1);
2549 publicVersion.writeToParcel(parcel, 0);
2550 } else {
2551 parcel.writeInt(0);
2552 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002553
2554 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002555
2556 if (mChannelId != null) {
2557 parcel.writeInt(1);
2558 parcel.writeString(mChannelId);
2559 } else {
2560 parcel.writeInt(0);
2561 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002562 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002563
2564 if (mShortcutId != null) {
2565 parcel.writeInt(1);
2566 parcel.writeString(mShortcutId);
2567 } else {
2568 parcel.writeInt(0);
2569 }
2570
2571 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002572
2573 if (mSettingsText != null) {
2574 parcel.writeInt(1);
2575 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2576 } else {
2577 parcel.writeInt(0);
2578 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002579
2580 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002581
2582 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584
2585 /**
2586 * Parcelable.Creator that instantiates Notification objects
2587 */
2588 public static final Parcelable.Creator<Notification> CREATOR
2589 = new Parcelable.Creator<Notification>()
2590 {
2591 public Notification createFromParcel(Parcel parcel)
2592 {
2593 return new Notification(parcel);
2594 }
2595
2596 public Notification[] newArray(int size)
2597 {
2598 return new Notification[size];
2599 }
2600 };
2601
2602 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002603 * @hide
2604 */
2605 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2606 Notification.Action[] firstAs = first.actions;
2607 Notification.Action[] secondAs = second.actions;
2608 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2609 return true;
2610 }
2611 if (firstAs != null && secondAs != null) {
2612 if (firstAs.length != secondAs.length) {
2613 return true;
2614 }
2615 for (int i = 0; i < firstAs.length; i++) {
2616 if (!Objects.equals(firstAs[i].title, secondAs[i].title)) {
2617 return true;
2618 }
2619 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2620 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2621 if (firstRs == null) {
2622 firstRs = new RemoteInput[0];
2623 }
2624 if (secondRs == null) {
2625 secondRs = new RemoteInput[0];
2626 }
2627 if (firstRs.length != secondRs.length) {
2628 return true;
2629 }
2630 for (int j = 0; j < firstRs.length; j++) {
2631 if (!Objects.equals(firstRs[j].getLabel(), secondRs[j].getLabel())) {
2632 return true;
2633 }
2634 CharSequence[] firstCs = firstRs[i].getChoices();
2635 CharSequence[] secondCs = secondRs[i].getChoices();
2636 if (firstCs == null) {
2637 firstCs = new CharSequence[0];
2638 }
2639 if (secondCs == null) {
2640 secondCs = new CharSequence[0];
2641 }
2642 if (firstCs.length != secondCs.length) {
2643 return true;
2644 }
2645 for (int k = 0; k < firstCs.length; k++) {
2646 if (!Objects.equals(firstCs[k], secondCs[k])) {
2647 return true;
2648 }
2649 }
2650 }
2651 }
2652 }
2653 return false;
2654 }
2655
2656 /**
2657 * @hide
2658 */
2659 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2660 if (first.getStyle() == null) {
2661 return second.getStyle() != null;
2662 }
2663 if (second.getStyle() == null) {
2664 return true;
2665 }
2666 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2667 }
2668
2669 /**
2670 * @hide
2671 */
2672 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
2673 return !first.usesStandardHeader() || !second.usesStandardHeader();
2674 }
2675
2676 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002677 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2678 * <p>
2679 * For backwards compatibility {@code extras} holds some references to "real" member data such
2680 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2681 * fine as long as the object stays in one process.
2682 * <p>
2683 * However, once the notification goes into a parcel each reference gets marshalled separately,
2684 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2685 */
2686 private void fixDuplicateExtras() {
2687 if (extras != null) {
2688 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2689 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2690 }
2691 }
2692
2693 /**
2694 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2695 * separate object, replace it with the field's version to avoid holding duplicate copies.
2696 */
2697 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2698 if (original != null && extras.getParcelable(extraName) != null) {
2699 extras.putParcelable(extraName, original);
2700 }
2701 }
2702
2703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2705 * layout.
2706 *
2707 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2708 * in the view.</p>
2709 * @param context The context for your application / activity.
2710 * @param contentTitle The title that goes in the expanded entry.
2711 * @param contentText The text that goes in the expanded entry.
2712 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2713 * If this is an activity, it must include the
2714 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002715 * that you take care of task management as described in the
2716 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2717 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002718 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002719 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002720 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 public void setLatestEventInfo(Context context,
2724 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002725 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2726 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2727 new Throwable());
2728 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002729
Selim Cinek4ac6f602016-06-13 15:47:03 -07002730 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2731 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2732 }
2733
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002734 // ensure that any information already set directly is preserved
2735 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002736
2737 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002739 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
2741 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002742 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002744 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002745
2746 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 }
2748
Julia Reynoldsda303542015-11-23 14:00:20 -05002749 /**
2750 * @hide
2751 */
2752 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002753 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002754 }
2755
2756 /**
2757 * @hide
2758 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002759 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002760 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002761 }
2762
Yi Jin6b514142017-10-30 14:54:12 -07002763 /**
2764 * @hide
2765 */
2766 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2767 long token = proto.start(fieldId);
2768 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2769 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2770 proto.write(NotificationProto.FLAGS, this.flags);
2771 proto.write(NotificationProto.COLOR, this.color);
2772 proto.write(NotificationProto.CATEGORY, this.category);
2773 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2774 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2775 if (this.actions != null) {
2776 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2777 }
2778 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2779 proto.write(NotificationProto.VISIBILITY, this.visibility);
2780 }
2781 if (publicVersion != null) {
2782 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2783 }
2784 proto.end(token);
2785 }
2786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 @Override
2788 public String toString() {
2789 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002790 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002791 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002792 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002793 sb.append(priority);
2794 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002795 if (contentView != null) {
2796 sb.append(contentView.getPackage());
2797 sb.append("/0x");
2798 sb.append(Integer.toHexString(contentView.getLayoutId()));
2799 } else {
2800 sb.append("null");
2801 }
2802 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002803 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2804 sb.append("default");
2805 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 int N = this.vibrate.length-1;
2807 sb.append("[");
2808 for (int i=0; i<N; i++) {
2809 sb.append(this.vibrate[i]);
2810 sb.append(',');
2811 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002812 if (N != -1) {
2813 sb.append(this.vibrate[N]);
2814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 } else {
2817 sb.append("null");
2818 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002819 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002820 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002822 } else if (this.sound != null) {
2823 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 } else {
2825 sb.append("null");
2826 }
Chris Wren365b6d32015-07-16 10:39:26 -04002827 if (this.tickerText != null) {
2828 sb.append(" tick");
2829 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002832 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002833 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002834 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002835 if (this.category != null) {
2836 sb.append(" category=");
2837 sb.append(this.category);
2838 }
2839 if (this.mGroupKey != null) {
2840 sb.append(" groupKey=");
2841 sb.append(this.mGroupKey);
2842 }
2843 if (this.mSortKey != null) {
2844 sb.append(" sortKey=");
2845 sb.append(this.mSortKey);
2846 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002847 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002848 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002849 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002850 }
2851 sb.append(" vis=");
2852 sb.append(visibilityToString(this.visibility));
2853 if (this.publicVersion != null) {
2854 sb.append(" publicVersion=");
2855 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002856 }
2857 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 return sb.toString();
2859 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002860
Dan Sandler1b718782014-07-18 12:43:45 -04002861 /**
2862 * {@hide}
2863 */
2864 public static String visibilityToString(int vis) {
2865 switch (vis) {
2866 case VISIBILITY_PRIVATE:
2867 return "PRIVATE";
2868 case VISIBILITY_PUBLIC:
2869 return "PUBLIC";
2870 case VISIBILITY_SECRET:
2871 return "SECRET";
2872 default:
2873 return "UNKNOWN(" + String.valueOf(vis) + ")";
2874 }
2875 }
2876
Joe Onoratocb109a02011-01-18 17:57:41 -08002877 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002878 * {@hide}
2879 */
2880 public static String priorityToString(@Priority int pri) {
2881 switch (pri) {
2882 case PRIORITY_MIN:
2883 return "MIN";
2884 case PRIORITY_LOW:
2885 return "LOW";
2886 case PRIORITY_DEFAULT:
2887 return "DEFAULT";
2888 case PRIORITY_HIGH:
2889 return "HIGH";
2890 case PRIORITY_MAX:
2891 return "MAX";
2892 default:
2893 return "UNKNOWN(" + String.valueOf(pri) + ")";
2894 }
2895 }
2896
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002897 /**
2898 * @hide
2899 */
2900 public boolean hasCompletedProgress() {
2901 // not a progress notification; can't be complete
2902 if (!extras.containsKey(EXTRA_PROGRESS)
2903 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2904 return false;
2905 }
2906 // many apps use max 0 for 'indeterminate'; not complete
2907 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2908 return false;
2909 }
2910 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2911 }
2912
Jeff Sharkey000ce802017-04-29 13:13:27 -06002913 /** @removed */
2914 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002915 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002916 return mChannelId;
2917 }
2918
2919 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002920 * Returns the id of the channel this notification posts to.
2921 */
2922 public String getChannelId() {
2923 return mChannelId;
2924 }
2925
Jeff Sharkey000ce802017-04-29 13:13:27 -06002926 /** @removed */
2927 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002928 public long getTimeout() {
2929 return mTimeout;
2930 }
2931
2932 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002933 * Returns the duration from posting after which this notification should be canceled by the
2934 * system, if it's not canceled already.
2935 */
2936 public long getTimeoutAfter() {
2937 return mTimeout;
2938 }
2939
2940 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002941 * Returns what icon should be shown for this notification if it is being displayed in a
2942 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2943 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2944 */
2945 public int getBadgeIconType() {
2946 return mBadgeIcon;
2947 }
2948
2949 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002950 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002951 *
2952 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2953 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002954 */
2955 public String getShortcutId() {
2956 return mShortcutId;
2957 }
2958
Julia Reynolds3aedded2017-03-31 14:42:09 -04002959
2960 /**
2961 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2962 */
2963 public CharSequence getSettingsText() {
2964 return mSettingsText;
2965 }
2966
Julia Reynolds13d898c2017-02-02 12:22:05 -05002967 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002968 * Returns which type of notifications in a group are responsible for audibly alerting the
2969 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2970 * {@link #GROUP_ALERT_SUMMARY}.
2971 */
2972 public @GroupAlertBehavior int getGroupAlertBehavior() {
2973 return mGroupAlertBehavior;
2974 }
2975
2976 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002977 * The small icon representing this notification in the status bar and content view.
2978 *
2979 * @return the small icon representing this notification.
2980 *
2981 * @see Builder#getSmallIcon()
2982 * @see Builder#setSmallIcon(Icon)
2983 */
2984 public Icon getSmallIcon() {
2985 return mSmallIcon;
2986 }
2987
2988 /**
2989 * Used when notifying to clean up legacy small icons.
2990 * @hide
2991 */
2992 public void setSmallIcon(Icon icon) {
2993 mSmallIcon = icon;
2994 }
2995
2996 /**
2997 * The large icon shown in this notification's content view.
2998 * @see Builder#getLargeIcon()
2999 * @see Builder#setLargeIcon(Icon)
3000 */
3001 public Icon getLargeIcon() {
3002 return mLargeIcon;
3003 }
3004
3005 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003006 * @hide
3007 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02003008 public boolean isGroupSummary() {
3009 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3010 }
3011
3012 /**
3013 * @hide
3014 */
3015 public boolean isGroupChild() {
3016 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3017 }
3018
3019 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003020 * @hide
3021 */
3022 public boolean suppressAlertingDueToGrouping() {
3023 if (isGroupSummary()
3024 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3025 return true;
3026 } else if (isGroupChild()
3027 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3028 return true;
3029 }
3030 return false;
3031 }
3032
3033 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003035 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003036 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003037 * content views using the platform's notification layout template. If your app supports
3038 * versions of Android as old as API level 4, you can instead use
3039 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3040 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3041 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003042 *
Scott Main183bf112012-08-13 19:12:13 -07003043 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003044 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003045 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003046 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003047 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3048 * .setContentText(subject)
3049 * .setSmallIcon(R.drawable.new_mail)
3050 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003051 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003052 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003053 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003054 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003055 /**
3056 * @hide
3057 */
3058 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3059 "android.rebuild.contentViewActionCount";
3060 /**
3061 * @hide
3062 */
3063 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3064 = "android.rebuild.bigViewActionCount";
3065 /**
3066 * @hide
3067 */
3068 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3069 = "android.rebuild.hudViewActionCount";
3070
Daniel Sandler602ad1c2012-06-12 16:06:27 -04003071 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04003072
Selim Cinek6743c0b2017-01-18 18:24:01 -08003073 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3074 SystemProperties.getBoolean("notifications.only_title", true);
3075
Selim Cinek389edcd2017-05-11 19:16:44 -07003076 /**
3077 * The lightness difference that has to be added to the primary text color to obtain the
3078 * secondary text color when the background is light.
3079 */
3080 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3081
3082 /**
3083 * The lightness difference that has to be added to the primary text color to obtain the
3084 * secondary text color when the background is dark.
3085 * A bit less then the above value, since it looks better on dark backgrounds.
3086 */
3087 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3088
Joe Onorato46439ce2010-11-19 13:56:21 -08003089 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003090 private Notification mN;
3091 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003092 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003093 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003094 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003095 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003096 private boolean mIsLegacy;
3097 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003098
3099 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003100 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3101 */
3102 private int mCachedContrastColor = COLOR_INVALID;
3103 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003104 /**
3105 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
3106 */
3107 private int mCachedAmbientColor = COLOR_INVALID;
3108 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003109
3110 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003111 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3112 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3113 */
3114 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003115 private int mTextColorsAreForBackground = COLOR_INVALID;
3116 private int mPrimaryTextColor = COLOR_INVALID;
3117 private int mSecondaryTextColor = COLOR_INVALID;
3118 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003119 private int mBackgroundColor = COLOR_INVALID;
3120 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07003121 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003122 /**
3123 * A temporary location where actions are stored. If != null the view originally has action
3124 * but doesn't have any for this inflation.
3125 */
3126 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003127 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003128
Anthony Chenad4d1582017-04-10 16:07:58 -07003129 private boolean mTintActionButtons;
3130 private boolean mInNightMode;
3131
Adrian Roos70d7aa32017-01-11 15:39:06 -08003132 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003133 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003135 * @param context
3136 * A {@link Context} that will be used by the Builder to construct the
3137 * RemoteViews. The Context will not be held past the lifetime of this Builder
3138 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003139 * @param channelId
3140 * The constructed Notification will be posted on this
3141 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3142 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003143 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003144 public Builder(Context context, String channelId) {
3145 this(context, (Notification) null);
3146 mN.mChannelId = channelId;
3147 }
3148
3149 /**
3150 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3151 * instead. All posted Notifications must specify a NotificationChannel Id.
3152 */
3153 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003154 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003155 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003156 }
3157
Joe Onoratocb109a02011-01-18 17:57:41 -08003158 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003159 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003160 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003161 public Builder(Context context, Notification toAdopt) {
3162 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003163 Resources res = mContext.getResources();
3164 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3165
3166 if (res.getBoolean(R.bool.config_enableNightMode)) {
3167 Configuration currentConfig = res.getConfiguration();
3168 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3169 == Configuration.UI_MODE_NIGHT_YES;
3170 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003171
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003172 if (toAdopt == null) {
3173 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003174 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3175 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3176 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003177 mN.priority = PRIORITY_DEFAULT;
3178 mN.visibility = VISIBILITY_PRIVATE;
3179 } else {
3180 mN = toAdopt;
3181 if (mN.actions != null) {
3182 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003183 }
3184
Selim Cineke7238dd2017-12-14 17:48:32 -08003185 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3186 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3187 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003188 }
3189
Selim Cinek4ac6f602016-06-13 15:47:03 -07003190 if (mN.getSmallIcon() == null && mN.icon != 0) {
3191 setSmallIcon(mN.icon);
3192 }
3193
3194 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3195 setLargeIcon(mN.largeIcon);
3196 }
3197
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003198 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3199 if (!TextUtils.isEmpty(templateClass)) {
3200 final Class<? extends Style> styleClass
3201 = getNotificationStyleClass(templateClass);
3202 if (styleClass == null) {
3203 Log.d(TAG, "Unknown style class: " + templateClass);
3204 } else {
3205 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003206 final Constructor<? extends Style> ctor =
3207 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003208 ctor.setAccessible(true);
3209 final Style style = ctor.newInstance();
3210 style.restoreFromExtras(mN.extras);
3211
3212 if (style != null) {
3213 setStyle(style);
3214 }
3215 } catch (Throwable t) {
3216 Log.e(TAG, "Could not create Style", t);
3217 }
3218 }
3219 }
3220
3221 }
3222 }
3223
3224 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003225 if (mColorUtil == null) {
3226 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003227 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003228 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003229 }
3230
3231 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003232 * If this notification is duplicative of a Launcher shortcut, sets the
3233 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3234 * the shortcut.
3235 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003236 * This field will be ignored by Launchers that don't support badging, don't show
3237 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003238 *
3239 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3240 * supersedes
3241 */
3242 public Builder setShortcutId(String shortcutId) {
3243 mN.mShortcutId = shortcutId;
3244 return this;
3245 }
3246
3247 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003248 * Sets which icon to display as a badge for this notification.
3249 *
3250 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3251 * {@link #BADGE_ICON_LARGE}.
3252 *
3253 * Note: This value might be ignored, for launchers that don't support badge icons.
3254 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003255 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003256 mN.mBadgeIcon = icon;
3257 return this;
3258 }
3259
3260 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003261 * Sets the group alert behavior for this notification. Use this method to mute this
3262 * notification if alerts for this notification's group should be handled by a different
3263 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003264 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3265 * mute. For example, if you want only the summary of your group to make noise, all
3266 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003267 *
3268 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3269 */
3270 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3271 mN.mGroupAlertBehavior = groupAlertBehavior;
3272 return this;
3273 }
3274
Jeff Sharkey000ce802017-04-29 13:13:27 -06003275 /** @removed */
3276 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003277 public Builder setChannel(String channelId) {
3278 mN.mChannelId = channelId;
3279 return this;
3280 }
3281
3282 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003283 * Specifies the channel the notification should be delivered on.
3284 */
3285 public Builder setChannelId(String channelId) {
3286 mN.mChannelId = channelId;
3287 return this;
3288 }
3289
Jeff Sharkey000ce802017-04-29 13:13:27 -06003290 /** @removed */
3291 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003292 public Builder setTimeout(long durationMs) {
3293 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003294 return this;
3295 }
3296
3297 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003298 * Specifies a duration in milliseconds after which this notification should be canceled,
3299 * if it is not already canceled.
3300 */
3301 public Builder setTimeoutAfter(long durationMs) {
3302 mN.mTimeout = durationMs;
3303 return this;
3304 }
3305
3306 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003307 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003308 *
3309 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3310 * shown anymore by default and must be opted into by using
3311 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003312 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003313 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003314 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003315 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003316 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003317 return this;
3318 }
3319
Joe Onoratocb109a02011-01-18 17:57:41 -08003320 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003321 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003322 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003323 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3324 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003325 */
3326 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003327 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003328 return this;
3329 }
3330
3331 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003332 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003333 *
3334 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003335 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003336 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003337 * Useful when showing an elapsed time (like an ongoing phone call).
3338 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003339 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003340 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003341 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003342 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003343 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003344 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003345 */
3346 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003347 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003348 return this;
3349 }
3350
3351 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003352 * Sets the Chronometer to count down instead of counting up.
3353 *
3354 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3355 * If it isn't set the chronometer will count up.
3356 *
3357 * @see #setUsesChronometer(boolean)
3358 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003359 public Builder setChronometerCountDown(boolean countDown) {
3360 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003361 return this;
3362 }
3363
3364 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003365 * Set the small icon resource, which will be used to represent the notification in the
3366 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003367 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003368
3369 * The platform template for the expanded view will draw this icon in the left, unless a
3370 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3371 * icon will be moved to the right-hand side.
3372 *
3373
3374 * @param icon
3375 * A resource ID in the application's package of the drawable to use.
3376 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003377 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003378 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003379 return setSmallIcon(icon != 0
3380 ? Icon.createWithResource(mContext, icon)
3381 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003382 }
3383
Joe Onoratocb109a02011-01-18 17:57:41 -08003384 /**
3385 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3386 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3387 * LevelListDrawable}.
3388 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003389 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003390 * @param level The level to use for the icon.
3391 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003392 * @see Notification#icon
3393 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003394 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003395 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003396 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003397 return setSmallIcon(icon);
3398 }
3399
3400 /**
3401 * Set the small icon, which will be used to represent the notification in the
3402 * status bar and content view (unless overriden there by a
3403 * {@link #setLargeIcon(Bitmap) large icon}).
3404 *
3405 * @param icon An Icon object to use.
3406 * @see Notification#icon
3407 */
3408 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003409 mN.setSmallIcon(icon);
3410 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3411 mN.icon = icon.getResId();
3412 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003413 return this;
3414 }
3415
Joe Onoratocb109a02011-01-18 17:57:41 -08003416 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003417 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003418 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003419 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003420 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003421 return this;
3422 }
3423
Joe Onoratocb109a02011-01-18 17:57:41 -08003424 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003425 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003427 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003428 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003429 return this;
3430 }
3431
Joe Onoratocb109a02011-01-18 17:57:41 -08003432 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003433 * This provides some additional information that is displayed in the notification. No
3434 * guarantees are given where exactly it is displayed.
3435 *
3436 * <p>This information should only be provided if it provides an essential
3437 * benefit to the understanding of the notification. The more text you provide the
3438 * less readable it becomes. For example, an email client should only provide the account
3439 * name here if more than one email account has been added.</p>
3440 *
3441 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3442 * notification header area.
3443 *
3444 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3445 * this will be shown in the third line of text in the platform notification template.
3446 * You should not be using {@link #setProgress(int, int, boolean)} at the
3447 * same time on those versions; they occupy the same place.
3448 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003449 */
3450 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003451 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003452 return this;
3453 }
3454
3455 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003456 * Provides text that will appear as a link to your application's settings.
3457 *
3458 * <p>This text does not appear within notification {@link Style templates} but may
3459 * appear when the user uses an affordance to learn more about the notification.
3460 * Additionally, this text will not appear unless you provide a valid link target by
3461 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3462 *
3463 * <p>This text is meant to be concise description about what the user can customize
3464 * when they click on this link. The recommended maximum length is 40 characters.
3465 * @param text
3466 * @return
3467 */
3468 public Builder setSettingsText(CharSequence text) {
3469 mN.mSettingsText = safeCharSequence(text);
3470 return this;
3471 }
3472
3473 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003474 * Set the remote input history.
3475 *
3476 * This should be set to the most recent inputs that have been sent
3477 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3478 * longer relevant (e.g. for chat notifications once the other party has responded).
3479 *
3480 * The most recent input must be stored at the 0 index, the second most recent at the
3481 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3482 * and how much of each individual input is shown.
3483 *
3484 * <p>Note: The reply text will only be shown on notifications that have least one action
3485 * with a {@code RemoteInput}.</p>
3486 */
3487 public Builder setRemoteInputHistory(CharSequence[] text) {
3488 if (text == null) {
3489 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3490 } else {
3491 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3492 CharSequence[] safe = new CharSequence[N];
3493 for (int i = 0; i < N; i++) {
3494 safe[i] = safeCharSequence(text[i]);
3495 }
3496 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3497 }
3498 return this;
3499 }
3500
3501 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003502 * Sets the number of items this notification represents. May be displayed as a badge count
3503 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003504 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003505 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003506 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003507 return this;
3508 }
3509
Joe Onoratocb109a02011-01-18 17:57:41 -08003510 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003511 * A small piece of additional information pertaining to this notification.
3512 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003513 * The platform template will draw this on the last line of the notification, at the far
3514 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003515 *
3516 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3517 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3518 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003519 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003520 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003521 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003522 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003523 return this;
3524 }
3525
Joe Onoratocb109a02011-01-18 17:57:41 -08003526 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003527 * Set the progress this notification represents.
3528 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003529 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003530 */
3531 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003532 mN.extras.putInt(EXTRA_PROGRESS, progress);
3533 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3534 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003535 return this;
3536 }
3537
3538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003539 * Supply a custom RemoteViews to use instead of the platform template.
3540 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003541 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003542 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003544 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003545 return setCustomContentView(views);
3546 }
3547
3548 /**
3549 * Supply custom RemoteViews to use instead of the platform template.
3550 *
3551 * This will override the layout that would otherwise be constructed by this Builder
3552 * object.
3553 */
3554 public Builder setCustomContentView(RemoteViews contentView) {
3555 mN.contentView = contentView;
3556 return this;
3557 }
3558
3559 /**
3560 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3561 *
3562 * This will override the expanded layout that would otherwise be constructed by this
3563 * Builder object.
3564 */
3565 public Builder setCustomBigContentView(RemoteViews contentView) {
3566 mN.bigContentView = contentView;
3567 return this;
3568 }
3569
3570 /**
3571 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3572 *
3573 * This will override the heads-up layout that would otherwise be constructed by this
3574 * Builder object.
3575 */
3576 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3577 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003578 return this;
3579 }
3580
Joe Onoratocb109a02011-01-18 17:57:41 -08003581 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003582 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3583 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003584 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3585 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3586 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003587 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003588 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003589 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003590 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003591 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003592 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003593 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003594 return this;
3595 }
3596
Joe Onoratocb109a02011-01-18 17:57:41 -08003597 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003598 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3599 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003600 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003601 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003602 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003603 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003604 return this;
3605 }
3606
Joe Onoratocb109a02011-01-18 17:57:41 -08003607 /**
3608 * An intent to launch instead of posting the notification to the status bar.
3609 * Only for use with extremely high-priority notifications demanding the user's
3610 * <strong>immediate</strong> attention, such as an incoming phone call or
3611 * alarm clock that the user has explicitly set to a particular time.
3612 * If this facility is used for something else, please give the user an option
3613 * to turn it off and use a normal notification, as this can be extremely
3614 * disruptive.
3615 *
Chris Wren47c20a12014-06-18 17:27:29 -04003616 * <p>
3617 * The system UI may choose to display a heads-up notification, instead of
3618 * launching this intent, while the user is using the device.
3619 * </p>
3620 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003621 * @param intent The pending intent to launch.
3622 * @param highPriority Passing true will cause this notification to be sent
3623 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003624 *
3625 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003627 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003628 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003629 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3630 return this;
3631 }
3632
Joe Onoratocb109a02011-01-18 17:57:41 -08003633 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003634 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003635 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003636 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003637 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003638 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003639 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003640 return this;
3641 }
3642
Joe Onoratocb109a02011-01-18 17:57:41 -08003643 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003644 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003645 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003646 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003647 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003648 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 setTicker(tickerText);
3650 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003651 return this;
3652 }
3653
Joe Onoratocb109a02011-01-18 17:57:41 -08003654 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003655 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003656 *
3657 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003658 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3659 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003660 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003661 public Builder setLargeIcon(Bitmap b) {
3662 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3663 }
3664
3665 /**
3666 * Add a large icon to the notification content view.
3667 *
3668 * In the platform template, this image will be shown on the left of the notification view
3669 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3670 * badge atop the large icon).
3671 */
3672 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003673 mN.mLargeIcon = icon;
3674 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003675 return this;
3676 }
3677
Joe Onoratocb109a02011-01-18 17:57:41 -08003678 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003679 * Set the sound to play.
3680 *
John Spurlockc0650f022014-07-19 13:22:39 -04003681 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3682 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003683 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003684 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003685 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003686 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003687 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003688 mN.sound = sound;
3689 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003690 return this;
3691 }
3692
Joe Onoratocb109a02011-01-18 17:57:41 -08003693 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003694 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003695 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003696 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3697 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003698 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003699 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003700 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003701 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003702 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003703 mN.sound = sound;
3704 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003705 return this;
3706 }
3707
Joe Onoratocb109a02011-01-18 17:57:41 -08003708 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003709 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3710 * use during playback.
3711 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003712 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003713 * @see Notification#sound
3714 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003715 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003716 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003717 mN.sound = sound;
3718 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003719 return this;
3720 }
3721
3722 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003723 * Set the vibration pattern to use.
3724 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003725 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3726 * <code>pattern</code> parameter.
3727 *
Chris Wren47c20a12014-06-18 17:27:29 -04003728 * <p>
3729 * A notification that vibrates is more likely to be presented as a heads-up notification.
3730 * </p>
3731 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003732 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003733 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003734 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003735 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003736 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003737 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003738 return this;
3739 }
3740
Joe Onoratocb109a02011-01-18 17:57:41 -08003741 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * Set the desired color for the indicator LED on the device, as well as the
3743 * blink duty cycle (specified in milliseconds).
3744 *
3745
3746 * Not all devices will honor all (or even any) of these values.
3747 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003748 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003749 * @see Notification#ledARGB
3750 * @see Notification#ledOnMS
3751 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003752 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003753 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003754 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 mN.ledARGB = argb;
3756 mN.ledOnMS = onMs;
3757 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003758 if (onMs != 0 || offMs != 0) {
3759 mN.flags |= FLAG_SHOW_LIGHTS;
3760 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003761 return this;
3762 }
3763
Joe Onoratocb109a02011-01-18 17:57:41 -08003764 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003765 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003766 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003767
3768 * Ongoing notifications cannot be dismissed by the user, so your application or service
3769 * must take care of canceling them.
3770 *
3771
3772 * They are typically used to indicate a background task that the user is actively engaged
3773 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3774 * (e.g., a file download, sync operation, active network connection).
3775 *
3776
3777 * @see Notification#FLAG_ONGOING_EVENT
3778 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003779 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003780 public Builder setOngoing(boolean ongoing) {
3781 setFlag(FLAG_ONGOING_EVENT, ongoing);
3782 return this;
3783 }
3784
Joe Onoratocb109a02011-01-18 17:57:41 -08003785 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003786 * Set whether this notification should be colorized. When set, the color set with
3787 * {@link #setColor(int)} will be used as the background color of this notification.
3788 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003789 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3790 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003791 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003792 * For most styles, the coloring will only be applied if the notification is for a
3793 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003794 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003795 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003796 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003797 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003798 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003799 */
3800 public Builder setColorized(boolean colorize) {
3801 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3802 return this;
3803 }
3804
3805 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003806 * Set this flag if you would only like the sound, vibrate
3807 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003808 *
3809 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003810 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003811 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3812 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3813 return this;
3814 }
3815
Joe Onoratocb109a02011-01-18 17:57:41 -08003816 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003817 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003818 *
3819 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003820 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003821 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003822 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003823 return this;
3824 }
3825
Joe Onoratocb109a02011-01-18 17:57:41 -08003826 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003827 * Set whether or not this notification should not bridge to other devices.
3828 *
3829 * <p>Some notifications can be bridged to other devices for remote display.
3830 * This hint can be set to recommend this notification not be bridged.
3831 */
3832 public Builder setLocalOnly(boolean localOnly) {
3833 setFlag(FLAG_LOCAL_ONLY, localOnly);
3834 return this;
3835 }
3836
3837 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003838 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003839 * <p>
3840 * The value should be one or more of the following fields combined with
3841 * bitwise-or:
3842 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3843 * <p>
3844 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003845 *
3846 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003847 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003848 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003849 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003850 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003851 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003852 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003853 return this;
3854 }
3855
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003856 /**
3857 * Set the priority of this notification.
3858 *
3859 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003860 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003861 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003862 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003863 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003864 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003865 return this;
3866 }
Joe Malin8d40d042012-11-05 11:36:40 -08003867
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003868 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003869 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003870 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003871 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003872 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003873 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003874 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003875 return this;
3876 }
3877
3878 /**
Chris Wrendde75302014-03-26 17:24:15 -04003879 * Add a person that is relevant to this notification.
3880 *
Chris Wrene6c48932014-09-29 17:19:27 -04003881 * <P>
3882 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003883 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3884 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3885 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003886 * </P>
3887 *
3888 * <P>
3889 * The person should be specified by the {@code String} representation of a
3890 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3891 * </P>
3892 *
3893 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3894 * URIs. The path part of these URIs must exist in the contacts database, in the
3895 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3896 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003897 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3898 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003899 * </P>
3900 *
3901 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003902 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003903 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003904 */
Chris Wrene6c48932014-09-29 17:19:27 -04003905 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003906 addPerson(new Person().setUri(uri));
3907 return this;
3908 }
3909
3910 /**
3911 * Add a person that is relevant to this notification.
3912 *
3913 * <P>
3914 * Depending on user preferences, this annotation may allow the notification to pass
3915 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3916 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3917 * appear more prominently in the user interface.
3918 * </P>
3919 *
3920 * <P>
3921 * A person should usually contain a uri in order to benefit from the ranking boost.
3922 * However, even if no uri is provided, it's beneficial to provide other people in the
3923 * notification, such that listeners and voice only devices can announce and handle them
3924 * properly.
3925 * </P>
3926 *
3927 * @param person the person to add.
3928 * @see Notification#EXTRA_PEOPLE_LIST
3929 */
3930 public Builder addPerson(Person person) {
3931 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003932 return this;
3933 }
3934
3935 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003936 * Set this notification to be part of a group of notifications sharing the same key.
3937 * Grouped notifications may display in a cluster or stack on devices which
3938 * support such rendering.
3939 *
3940 * <p>To make this notification the summary for its group, also call
3941 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3942 * {@link #setSortKey}.
3943 * @param groupKey The group key of the group.
3944 * @return this object for method chaining
3945 */
3946 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003947 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003948 return this;
3949 }
3950
3951 /**
3952 * Set this notification to be the group summary for a group of notifications.
3953 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003954 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3955 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003956 * @param isGroupSummary Whether this notification should be a group summary.
3957 * @return this object for method chaining
3958 */
3959 public Builder setGroupSummary(boolean isGroupSummary) {
3960 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3961 return this;
3962 }
3963
3964 /**
3965 * Set a sort key that orders this notification among other notifications from the
3966 * same package. This can be useful if an external sort was already applied and an app
3967 * would like to preserve this. Notifications will be sorted lexicographically using this
3968 * value, although providing different priorities in addition to providing sort key may
3969 * cause this value to be ignored.
3970 *
3971 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003972 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003973 *
3974 * @see String#compareTo(String)
3975 */
3976 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003977 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003978 return this;
3979 }
3980
3981 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003982 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003983 *
Griff Hazen720042b2014-02-24 15:46:56 -08003984 * <p>Values within the Bundle will replace existing extras values in this Builder.
3985 *
3986 * @see Notification#extras
3987 */
Griff Hazen959591e2014-05-15 22:26:18 -07003988 public Builder addExtras(Bundle extras) {
3989 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003991 }
3992 return this;
3993 }
3994
3995 /**
3996 * Set metadata for this notification.
3997 *
3998 * <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 -04003999 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004000 * called.
4001 *
Griff Hazen720042b2014-02-24 15:46:56 -08004002 * <p>Replaces any existing extras values with those from the provided Bundle.
4003 * Use {@link #addExtras} to merge in metadata instead.
4004 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004005 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004006 */
Griff Hazen959591e2014-05-15 22:26:18 -07004007 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004008 if (extras != null) {
4009 mUserExtras = extras;
4010 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004011 return this;
4012 }
4013
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004014 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004015 * Get the current metadata Bundle used by this notification Builder.
4016 *
4017 * <p>The returned Bundle is shared with this Builder.
4018 *
4019 * <p>The current contents of this Bundle are copied into the Notification each time
4020 * {@link #build()} is called.
4021 *
4022 * @see Notification#extras
4023 */
4024 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 return mUserExtras;
4026 }
4027
4028 private Bundle getAllExtras() {
4029 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4030 saveExtras.putAll(mN.extras);
4031 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004032 }
4033
4034 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004035 * Add an action to this notification. Actions are typically displayed by
4036 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004037 * <p>
4038 * Every action must have an icon (32dp square and matching the
4039 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4040 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4041 * <p>
4042 * A notification in its expanded form can display up to 3 actions, from left to right in
4043 * the order they were added. Actions will not be displayed when the notification is
4044 * collapsed, however, so be sure that any essential functions may be accessed by the user
4045 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004046 *
4047 * @param icon Resource ID of a drawable that represents the action.
4048 * @param title Text describing the action.
4049 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004050 *
4051 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004052 */
Dan Sandler86647982015-05-13 23:41:13 -04004053 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004054 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004055 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004056 return this;
4057 }
4058
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004059 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004060 * Add an action to this notification. Actions are typically displayed by
4061 * the system as a button adjacent to the notification content.
4062 * <p>
4063 * Every action must have an icon (32dp square and matching the
4064 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4065 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4066 * <p>
4067 * A notification in its expanded form can display up to 3 actions, from left to right in
4068 * the order they were added. Actions will not be displayed when the notification is
4069 * collapsed, however, so be sure that any essential functions may be accessed by the user
4070 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4071 *
4072 * @param action The action to add.
4073 */
4074 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004075 if (action != null) {
4076 mActions.add(action);
4077 }
Griff Hazen959591e2014-05-15 22:26:18 -07004078 return this;
4079 }
4080
4081 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004082 * Alter the complete list of actions attached to this notification.
4083 * @see #addAction(Action).
4084 *
4085 * @param actions
4086 * @return
4087 */
4088 public Builder setActions(Action... actions) {
4089 mActions.clear();
4090 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004091 if (actions[i] != null) {
4092 mActions.add(actions[i]);
4093 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004094 }
4095 return this;
4096 }
4097
4098 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004099 * Add a rich notification style to be applied at build time.
4100 *
4101 * @param style Object responsible for modifying the notification style.
4102 */
4103 public Builder setStyle(Style style) {
4104 if (mStyle != style) {
4105 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004106 if (mStyle != null) {
4107 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004108 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4109 } else {
4110 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004111 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004112 }
4113 return this;
4114 }
4115
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004116 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004117 * Returns the style set by {@link #setStyle(Style)}.
4118 */
4119 public Style getStyle() {
4120 return mStyle;
4121 }
4122
4123 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004124 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004125 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004126 * @return The same Builder.
4127 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004128 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004129 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004130 return this;
4131 }
4132
4133 /**
4134 * Supply a replacement Notification whose contents should be shown in insecure contexts
4135 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4136 * @param n A replacement notification, presumably with some or all info redacted.
4137 * @return The same Builder.
4138 */
4139 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004140 if (n != null) {
4141 mN.publicVersion = new Notification();
4142 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4143 } else {
4144 mN.publicVersion = null;
4145 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004146 return this;
4147 }
4148
Griff Hazenb720abe2014-05-20 13:15:30 -07004149 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004150 * Apply an extender to this notification builder. Extenders may be used to add
4151 * metadata or change options on this builder.
4152 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004153 public Builder extend(Extender extender) {
4154 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004155 return this;
4156 }
4157
Dan Sandler4e787062015-06-17 15:09:48 -04004158 /**
4159 * @hide
4160 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004161 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004162 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004163 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004164 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004165 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004166 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004167 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004168 }
4169
Dan Sandler26e81cf2014-05-06 10:01:27 -04004170 /**
4171 * Sets {@link Notification#color}.
4172 *
4173 * @param argb The accent color to use
4174 *
4175 * @return The same Builder.
4176 */
Tor Norbye80756e32015-03-02 09:39:27 -08004177 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004178 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004179 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004180 return this;
4181 }
4182
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004183 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004184 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4185 // This user can never be a badged profile,
4186 // and also includes USER_ALL system notifications.
4187 return null;
4188 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004189 // Note: This assumes that the current user can read the profile badge of the
4190 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004191 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004192 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004193 }
4194
4195 private Bitmap getProfileBadge() {
4196 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004197 if (badge == null) {
4198 return null;
4199 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004200 final int size = mContext.getResources().getDimensionPixelSize(
4201 R.dimen.notification_badge_size);
4202 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004203 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004204 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004205 badge.draw(canvas);
4206 return bitmap;
4207 }
4208
Selim Cinekc848c3a2016-01-13 15:27:30 -08004209 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004210 Bitmap profileBadge = getProfileBadge();
4211
Kenny Guy98193ea2014-07-24 19:54:37 +01004212 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004213 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4214 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004215 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004216 contentView.setDrawableTint(R.id.profile_badge, false,
4217 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004218 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004219 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004220 }
4221
Julia Reynoldsfc640012018-02-21 12:25:27 -05004222 /**
4223 * @hide
4224 */
4225 public boolean usesStandardHeader() {
4226 if (mN.mUsesStandardHeader) {
4227 return true;
4228 }
4229 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4230 if (mN.contentView == null && mN.bigContentView == null) {
4231 return true;
4232 }
4233 }
4234 boolean contentViewUsesHeader = mN.contentView == null
4235 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4236 boolean bigContentViewUsesHeader = mN.bigContentView == null
4237 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4238 return contentViewUsesHeader && bigContentViewUsesHeader;
4239 }
4240
Christoph Studerfe718432014-09-01 18:21:18 +02004241 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004242 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004243 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004244 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004245 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004246 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004247 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004248 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004249 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004250 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004251 }
4252
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004253 /**
4254 * Resets the notification header to its original state
4255 */
4256 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004257 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4258 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004259 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004260 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004261 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004262 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004263 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004264 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4265 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004266 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004267 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004268 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004269 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004270 contentView.setImageViewIcon(R.id.profile_badge, null);
4271 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004272 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004273 }
4274
4275 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004276 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4277 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004278 }
4279
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004280 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004281 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004282 }
4283
4284 /**
4285 * @param hasProgress whether the progress bar should be shown and set
4286 */
4287 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004288 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4289 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004290 }
4291
Adrian Roos70d7aa32017-01-11 15:39:06 -08004292 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004293 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004294
Christoph Studerfe718432014-09-01 18:21:18 +02004295 resetStandardTemplate(contentView);
4296
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004297 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004298 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004299 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004300 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004301 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4302 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004303 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004304 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004305 if (!p.ambient) {
4306 setTextViewColorPrimary(contentView, R.id.title);
4307 }
Selim Cinek954cc232016-05-20 13:29:23 -07004308 contentView.setViewLayoutWidth(R.id.title, showProgress
4309 ? ViewGroup.LayoutParams.WRAP_CONTENT
4310 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004311 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004312 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004313 int textId = showProgress ? com.android.internal.R.id.text_line_1
4314 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004315 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004316 if (!p.ambient) {
4317 setTextViewColorSecondary(contentView, textId);
4318 }
Selim Cinek41598732016-01-11 16:58:37 -08004319 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004320 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004321
Selim Cinek279fa862016-06-14 10:57:25 -07004322 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004323
Selim Cinek29603462015-11-17 19:04:39 -08004324 return contentView;
4325 }
4326
Selim Cinek48f66b72017-08-18 16:17:51 -07004327 private CharSequence processTextSpans(CharSequence text) {
4328 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004329 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004330 }
4331 return text;
4332 }
4333
Selim Cinek7b9605b2017-01-19 17:36:00 -08004334 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4335 ensureColors();
4336 contentView.setTextColor(id, mPrimaryTextColor);
4337 }
4338
Selim Cinek48f66b72017-08-18 16:17:51 -07004339 private boolean hasForegroundColor() {
4340 return mForegroundColor != COLOR_INVALID;
4341 }
4342
Selim Cinek389edcd2017-05-11 19:16:44 -07004343 /**
4344 * @return the primary text color
4345 * @hide
4346 */
4347 @VisibleForTesting
4348 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004349 ensureColors();
4350 return mPrimaryTextColor;
4351 }
4352
Selim Cinek389edcd2017-05-11 19:16:44 -07004353 /**
4354 * @return the secondary text color
4355 * @hide
4356 */
4357 @VisibleForTesting
4358 public int getSecondaryTextColor() {
4359 ensureColors();
4360 return mSecondaryTextColor;
4361 }
4362
Selim Cinek7b9605b2017-01-19 17:36:00 -08004363 private int getActionBarColor() {
4364 ensureColors();
4365 return mActionBarColor;
4366 }
4367
Selim Cinek622c64a2017-04-17 17:10:05 -07004368 private int getActionBarColorDeEmphasized() {
4369 int backgroundColor = getBackgroundColor();
4370 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4371 }
4372
Selim Cinek7b9605b2017-01-19 17:36:00 -08004373 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4374 ensureColors();
4375 contentView.setTextColor(id, mSecondaryTextColor);
4376 }
4377
4378 private void ensureColors() {
4379 int backgroundColor = getBackgroundColor();
4380 if (mPrimaryTextColor == COLOR_INVALID
4381 || mSecondaryTextColor == COLOR_INVALID
4382 || mActionBarColor == COLOR_INVALID
4383 || mTextColorsAreForBackground != backgroundColor) {
4384 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004385 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004386 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4387 backgroundColor);
4388 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4389 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004390 if (backgroundColor != COLOR_DEFAULT
4391 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4392 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4393 mPrimaryTextColor, backgroundColor, 4.5);
4394 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4395 mSecondaryTextColor, backgroundColor, 4.5);
4396 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004397 } else {
4398 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4399 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4400 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4401 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004402 // We only respect the given colors if worst case Black or White still has
4403 // contrast
4404 boolean backgroundLight = backLum > textLum
4405 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4406 || backLum <= textLum
4407 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004408 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004409 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004410 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4411 mForegroundColor,
4412 backgroundColor,
4413 true /* findFG */,
4414 4.5f);
4415 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004416 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004417 } else {
4418 mSecondaryTextColor =
4419 NotificationColorUtil.findContrastColorAgainstDark(
4420 mForegroundColor,
4421 backgroundColor,
4422 true /* findFG */,
4423 4.5f);
4424 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004425 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004426 }
4427 } else {
4428 mPrimaryTextColor = mForegroundColor;
4429 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004430 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4431 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004432 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4433 backgroundColor) < 4.5f) {
4434 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004435 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004436 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4437 mSecondaryTextColor,
4438 backgroundColor,
4439 true /* findFG */,
4440 4.5f);
4441 } else {
4442 mSecondaryTextColor
4443 = NotificationColorUtil.findContrastColorAgainstDark(
4444 mSecondaryTextColor,
4445 backgroundColor,
4446 true /* findFG */,
4447 4.5f);
4448 }
4449 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004450 mSecondaryTextColor, backgroundLight
4451 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4452 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004453 }
4454 }
4455 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004456 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4457 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004458 }
4459 }
4460
4461 private void updateBackgroundColor(RemoteViews contentView) {
4462 if (isColorized()) {
4463 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4464 getBackgroundColor());
4465 } else {
4466 // Clear it!
4467 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4468 0);
4469 }
4470 }
4471
Selim Cinek860b6da2015-12-16 19:02:19 -08004472 /**
4473 * @param remoteView the remote view to update the minheight in
4474 * @param hasMinHeight does it have a mimHeight
4475 * @hide
4476 */
4477 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4478 int minHeight = 0;
4479 if (hasMinHeight) {
4480 // we need to set the minHeight of the notification
4481 minHeight = mContext.getResources().getDimensionPixelSize(
4482 com.android.internal.R.dimen.notification_min_content_height);
4483 }
4484 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4485 }
4486
Selim Cinek29603462015-11-17 19:04:39 -08004487 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004488 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4489 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4490 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4491 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004492 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004493 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004494 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004495 contentView.setProgressBackgroundTintList(
4496 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4497 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004498 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004499 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004500 contentView.setProgressTintList(R.id.progress, colorStateList);
4501 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004502 }
Selim Cinek29603462015-11-17 19:04:39 -08004503 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004504 } else {
4505 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004506 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004507 }
Joe Onorato561d3852010-11-20 18:09:34 -08004508 }
4509
Selim Cinek88188f22017-09-19 16:46:56 -07004510 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4511 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004512 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4513 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4514 }
Selim Cinek88188f22017-09-19 16:46:56 -07004515 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4516 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004517 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4518 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4519 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004520 int endMargin = R.dimen.notification_content_picture_margin;
4521 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4522 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4523 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004524 }
4525 // Bind the reply action
4526 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004527
Selim Cinek88188f22017-09-19 16:46:56 -07004528 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4529 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4530 if (actionVisible) {
4531 // We're only showing the icon as big if we're hiding the large icon
4532 int contrastColor = resolveContrastColor();
4533 int iconColor;
4534 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004535 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004536 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004537 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004538 contentView.setOnClickPendingIntent(R.id.right_icon,
4539 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004540 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004541 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4542 ? Color.BLACK : Color.WHITE;
4543 } else {
4544 contentView.setImageViewResource(R.id.right_icon,
4545 R.drawable.ic_reply_notification_large);
4546 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4547 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004548 }
Selim Cinek88188f22017-09-19 16:46:56 -07004549 contentView.setDrawableTint(replyId,
4550 false /* targetBackground */,
4551 iconColor,
4552 PorterDuff.Mode.SRC_ATOP);
4553 contentView.setOnClickPendingIntent(replyId,
4554 action.actionIntent);
4555 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4556 } else {
4557 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004558 }
Selim Cinek88188f22017-09-19 16:46:56 -07004559 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4560 ? View.VISIBLE
4561 : View.GONE);
4562 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004563 ? View.VISIBLE
4564 : View.GONE);
4565 }
4566
4567 private Action findReplyAction() {
4568 ArrayList<Action> actions = mActions;
4569 if (mOriginalActions != null) {
4570 actions = mOriginalActions;
4571 }
4572 int numActions = actions.size();
4573 for (int i = 0; i < numActions; i++) {
4574 Action action = actions.get(i);
4575 if (hasValidRemoteInput(action)) {
4576 return action;
4577 }
4578 }
4579 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004580 }
4581
Selim Cinekafeed292017-12-12 17:32:44 -08004582 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4583 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004584 bindSmallIcon(contentView, ambient);
4585 bindHeaderAppName(contentView, ambient);
4586 if (!ambient) {
4587 // Ambient view does not have these
4588 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004589 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004590 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004591 bindProfileBadge(contentView);
4592 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004593 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004594 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004595 }
4596
4597 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004598 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004599 contentView.setDrawableTint(R.id.expand_button, false, color,
4600 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004601 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004602 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004603 }
4604
Selim Cinek99104832017-01-25 14:47:33 -08004605 /**
4606 * @return the color that is used as the first primary highlight color. This is applied
4607 * in several places like the action buttons or the app name in the header.
4608 */
4609 private int getPrimaryHighlightColor() {
4610 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4611 }
4612
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004613 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4614 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004615 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004616 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004617 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4618 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4619 contentView.setLong(R.id.chronometer, "setBase",
4620 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4621 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004622 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004623 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004624 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004625 } else {
4626 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4627 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004628 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004629 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004630 } else {
4631 // We still want a time to be set but gone, such that we can show and hide it
4632 // on demand in case it's a child notification without anything in the header
4633 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004634 }
4635 }
4636
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004637 private void bindHeaderText(RemoteViews contentView) {
4638 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4639 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004640 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004641 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004642 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004643 if (headerText == null
4644 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4645 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4646 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4647 }
4648 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004649 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004650 contentView.setTextViewText(R.id.header_text, processTextSpans(
4651 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004652 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004653 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4654 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004655 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004656 }
4657 }
4658
Selim Cinekafeed292017-12-12 17:32:44 -08004659 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4660 if (!TextUtils.isEmpty(secondaryText)) {
4661 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4662 processLegacyText(secondaryText)));
4663 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4664 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4665 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4666 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4667 }
4668 }
4669
Adrian Rooseba05822016-04-22 17:09:27 -07004670 /**
4671 * @hide
4672 */
4673 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004674 CharSequence name = null;
4675 final PackageManager pm = mContext.getPackageManager();
4676 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4677 // only system packages which lump together a bunch of unrelated stuff
4678 // may substitute a different name to make the purpose of the
4679 // notification more clear. the correct package label should always
4680 // be accessible via SystemUI.
4681 final String pkg = mContext.getPackageName();
4682 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4683 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4684 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4685 name = subName;
4686 } else {
4687 Log.w(TAG, "warning: pkg "
4688 + pkg + " attempting to substitute app name '" + subName
4689 + "' without holding perm "
4690 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4691 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004692 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004693 if (TextUtils.isEmpty(name)) {
4694 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4695 }
4696 if (TextUtils.isEmpty(name)) {
4697 // still nothing?
4698 return null;
4699 }
4700
4701 return String.valueOf(name);
4702 }
Adrian Roos487374f2017-01-11 15:48:14 -08004703 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004704 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004705 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004706 setTextViewColorPrimary(contentView, R.id.app_name_text);
4707 } else {
4708 contentView.setTextColor(R.id.app_name_text,
4709 ambient ? resolveAmbientColor() : resolveContrastColor());
4710 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004711 }
4712
Adrian Roos487374f2017-01-11 15:48:14 -08004713 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004714 if (mN.mSmallIcon == null && mN.icon != 0) {
4715 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4716 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004717 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004718 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004719 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004720 }
4721
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004722 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004723 * @return true if the built notification will show the time or the chronometer; false
4724 * otherwise
4725 */
4726 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004727 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004728 }
4729
Christoph Studerfe718432014-09-01 18:21:18 +02004730 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004731 // actions_container is only reset when there are no actions to avoid focus issues with
4732 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004733 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004734 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004735
4736 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4737 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4738
4739 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4740 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4741 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4742 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004743
Selim Cineked64a142018-02-06 18:06:01 -08004744 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4745 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004746 }
4747
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004748 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004749 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004750 }
4751
Adrian Roos70d7aa32017-01-11 15:39:06 -08004752 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4753 StandardTemplateParams p) {
4754 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004755
Christoph Studerfe718432014-09-01 18:21:18 +02004756 resetStandardTemplateWithActions(big);
4757
Adrian Roose458aa82015-12-08 16:17:19 -08004758 boolean validRemoteInput = false;
4759
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004760 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004761 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004762 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004763 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004764 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004765 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004766 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004767 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004768 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004769 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004770 boolean actionHasValidInput = hasValidRemoteInput(action);
4771 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004772
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004773 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004774 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004775 if (actionHasValidInput) {
4776 // Clear the drawable
4777 button.setInt(R.id.action0, "setBackgroundResource", 0);
4778 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004779 big.addView(R.id.actions, button);
4780 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004781 } else {
4782 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004783 }
Adrian Roose458aa82015-12-08 16:17:19 -08004784
4785 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004786 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004787 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4788 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004789 big.setTextViewText(R.id.notification_material_reply_text_1,
4790 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004791 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004792
4793 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4794 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004795 big.setTextViewText(R.id.notification_material_reply_text_2,
4796 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004797 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004798
4799 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4800 big.setViewVisibility(
4801 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004802 big.setTextViewText(R.id.notification_material_reply_text_3,
4803 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004804 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004805 }
4806 }
4807 }
4808
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004809 return big;
4810 }
4811
Adrian Roose458aa82015-12-08 16:17:19 -08004812 private boolean hasValidRemoteInput(Action action) {
4813 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4814 // Weird actions
4815 return false;
4816 }
4817
4818 RemoteInput[] remoteInputs = action.getRemoteInputs();
4819 if (remoteInputs == null) {
4820 return false;
4821 }
4822
4823 for (RemoteInput r : remoteInputs) {
4824 CharSequence[] choices = r.getChoices();
4825 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4826 return true;
4827 }
4828 }
4829 return false;
4830 }
4831
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004832 /**
4833 * Construct a RemoteViews for the final 1U notification layout. In order:
4834 * 1. Custom contentView from the caller
4835 * 2. Style's proposed content view
4836 * 3. Standard template view
4837 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004838 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004839 return createContentView(false /* increasedheight */ );
4840 }
4841
4842 /**
4843 * Construct a RemoteViews for the smaller content view.
4844 *
4845 * @param increasedHeight true if this layout be created with an increased height. Some
4846 * styles may support showing more then just that basic 1U size
4847 * and the system may decide to render important notifications
4848 * slightly bigger even when collapsed.
4849 *
4850 * @hide
4851 */
4852 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004853 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004854 return mN.contentView;
4855 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004856 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004857 if (styleView != null) {
4858 return styleView;
4859 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004860 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004861 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004862 }
4863
Selim Cineka7679b62017-05-10 16:33:25 -07004864 private boolean useExistingRemoteView() {
4865 return mStyle == null || (!mStyle.displayCustomViewInline()
4866 && !mRebuildStyledRemoteViews);
4867 }
4868
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004869 /**
4870 * Construct a RemoteViews for the final big notification layout.
4871 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004872 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004873 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004874 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004875 return mN.bigContentView;
4876 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004877 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004878 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004879 } else if (mActions.size() != 0) {
4880 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004881 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004882 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004883 return result;
4884 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004885
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004886 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004887 * Construct a RemoteViews for the final notification header only. This will not be
4888 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004889 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004890 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004891 * @hide
4892 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004893 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004894 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4895 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004896 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004897 ambient ? R.layout.notification_template_ambient_header
4898 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004899 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004900 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004901 if (colorized != null) {
4902 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4903 } else {
4904 mN.extras.remove(EXTRA_COLORIZED);
4905 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004906 return header;
4907 }
4908
Adrian Roos487374f2017-01-11 15:48:14 -08004909 /**
4910 * Construct a RemoteViews for the ambient version of the notification.
4911 *
4912 * @hide
4913 */
4914 public RemoteViews makeAmbientNotification() {
4915 RemoteViews ambient = applyStandardTemplateWithActions(
4916 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004917 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004918 return ambient;
4919 }
4920
Selim Cinek29603462015-11-17 19:04:39 -08004921 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004922 if (result != null) {
4923 result.setViewVisibility(R.id.text_line_1, View.GONE);
4924 }
Selim Cinek29603462015-11-17 19:04:39 -08004925 }
4926
Selim Cinek6743c0b2017-01-18 18:24:01 -08004927 /**
4928 * Adapt the Notification header if this view is used as an expanded view.
4929 *
4930 * @hide
4931 */
4932 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004933 if (result != null) {
4934 result.setBoolean(R.id.notification_header, "setExpanded", true);
4935 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004936 }
4937
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004938 /**
4939 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004940 *
4941 * @param increasedHeight true if this layout be created with an increased height. Some
4942 * styles may support showing more then just that basic 1U size
4943 * and the system may decide to render important notifications
4944 * slightly bigger even when collapsed.
4945 *
4946 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004947 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004948 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004949 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004950 return mN.headsUpContentView;
4951 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004952 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4953 if (styleView != null) {
4954 return styleView;
4955 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004956 } else if (mActions.size() == 0) {
4957 return null;
4958 }
4959
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004960 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004961 }
4962
Selim Cinek624c02db2015-12-14 21:00:02 -08004963 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004964 * Construct a RemoteViews for the final heads-up notification layout.
4965 */
4966 public RemoteViews createHeadsUpContentView() {
4967 return createHeadsUpContentView(false /* useIncreasedHeight */);
4968 }
4969
4970 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004971 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4972 *
4973 * @hide
4974 */
4975 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004976 return makePublicView(false /* ambient */);
4977 }
4978
4979 /**
4980 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4981 *
4982 * @hide
4983 */
4984 public RemoteViews makePublicAmbientNotification() {
4985 return makePublicView(true /* ambient */);
4986 }
4987
4988 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004989 if (mN.publicVersion != null) {
4990 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004991 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004992 }
4993 Bundle savedBundle = mN.extras;
4994 Style style = mStyle;
4995 mStyle = null;
4996 Icon largeIcon = mN.mLargeIcon;
4997 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004998 Bitmap largeIconLegacy = mN.largeIcon;
4999 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005000 ArrayList<Action> actions = mActions;
5001 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005002 Bundle publicExtras = new Bundle();
5003 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5004 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5005 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5006 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005007 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5008 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08005009 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005010 RemoteViews view;
5011 if (ambient) {
5012 publicExtras.putCharSequence(EXTRA_TITLE,
5013 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5014 view = makeAmbientNotification();
5015 } else{
5016 view = makeNotificationHeader(false /* ambient */);
5017 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5018 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005019 mN.extras = savedBundle;
5020 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005021 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005022 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005023 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005024 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005025 }
5026
Selim Cinek6743c0b2017-01-18 18:24:01 -08005027 /**
5028 * Construct a content view for the display when low - priority
5029 *
5030 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5031 * a new subtext is created consisting of the content of the
5032 * notification.
5033 * @hide
5034 */
5035 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5036 int color = mN.color;
5037 mN.color = COLOR_DEFAULT;
5038 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5039 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5040 CharSequence newSummary = createSummaryText();
5041 if (!TextUtils.isEmpty(newSummary)) {
5042 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5043 }
5044 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005045
Adrian Roos6f6e1592017-05-02 16:22:53 -07005046 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005047 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005048 if (summary != null) {
5049 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5050 } else {
5051 mN.extras.remove(EXTRA_SUB_TEXT);
5052 }
5053 mN.color = color;
5054 return header;
5055 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005056
Selim Cinek6743c0b2017-01-18 18:24:01 -08005057 private CharSequence createSummaryText() {
5058 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5059 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5060 return titleText;
5061 }
5062 SpannableStringBuilder summary = new SpannableStringBuilder();
5063 if (titleText == null) {
5064 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5065 }
5066 BidiFormatter bidi = BidiFormatter.getInstance();
5067 if (titleText != null) {
5068 summary.append(bidi.unicodeWrap(titleText));
5069 }
5070 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5071 if (titleText != null && contentText != null) {
5072 summary.append(bidi.unicodeWrap(mContext.getText(
5073 R.string.notification_header_divider_symbol_with_spaces)));
5074 }
5075 if (contentText != null) {
5076 summary.append(bidi.unicodeWrap(contentText));
5077 }
5078 return summary;
5079 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005080
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005081 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08005082 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005083 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005084 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005085 emphazisedMode ? getEmphasizedActionLayoutResource()
5086 : tombstone ? getActionTombstoneLayoutResource()
5087 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005088 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005089 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005090 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005091 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005092 if (action.mRemoteInputs != null) {
5093 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5094 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005095 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005096 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005097 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07005098 int bgColor;
5099 if (isColorized()) {
5100 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
5101 } else {
5102 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
5103 : R.color.notification_action_list_dark);
5104 }
Sunny Goyal5b153922017-09-21 21:00:36 -07005105 button.setDrawableTint(R.id.button_holder, true,
5106 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07005107 CharSequence title = action.title;
5108 ColorStateList[] outResultColor = null;
5109 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07005110 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005111 } else {
5112 outResultColor = new ColorStateList[1];
5113 title = ensureColorSpanContrast(title, bgColor, outResultColor);
5114 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005115 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005116 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07005117 if (outResultColor != null && outResultColor[0] != null) {
5118 // We need to set the text color as well since changing a text to uppercase
5119 // clears its spans.
5120 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07005121 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07005122 button.setTextColor(R.id.action0,resolveContrastColor());
5123 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005124 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005125 button.setTextViewText(R.id.action0, processTextSpans(
5126 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005127 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005128 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005129 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005130 button.setTextColor(R.id.action0,
5131 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005132 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005133 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005134 return button;
5135 }
5136
Joe Onoratocb109a02011-01-18 17:57:41 -08005137 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005138 * Ensures contrast on color spans against a background color. also returns the color of the
5139 * text if a span was found that spans over the whole text.
5140 *
5141 * @param charSequence the charSequence on which the spans are
5142 * @param background the background color to ensure the contrast against
5143 * @param outResultColor an array in which a color will be returned as the first element if
5144 * there exists a full length color span.
5145 * @return the contrasted charSequence
5146 */
5147 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5148 ColorStateList[] outResultColor) {
5149 if (charSequence instanceof Spanned) {
5150 Spanned ss = (Spanned) charSequence;
5151 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5152 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5153 for (Object span : spans) {
5154 Object resultSpan = span;
5155 int spanStart = ss.getSpanStart(span);
5156 int spanEnd = ss.getSpanEnd(span);
5157 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5158 if (resultSpan instanceof CharacterStyle) {
5159 resultSpan = ((CharacterStyle) span).getUnderlying();
5160 }
5161 if (resultSpan instanceof TextAppearanceSpan) {
5162 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5163 ColorStateList textColor = originalSpan.getTextColor();
5164 if (textColor != null) {
5165 int[] colors = textColor.getColors();
5166 int[] newColors = new int[colors.length];
5167 for (int i = 0; i < newColors.length; i++) {
5168 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005169 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005170 }
5171 textColor = new ColorStateList(textColor.getStates().clone(),
5172 newColors);
5173 resultSpan = new TextAppearanceSpan(
5174 originalSpan.getFamily(),
5175 originalSpan.getTextStyle(),
5176 originalSpan.getTextSize(),
5177 textColor,
5178 originalSpan.getLinkTextColor());
5179 if (fullLength) {
5180 outResultColor[0] = new ColorStateList(
5181 textColor.getStates().clone(), newColors);
5182 }
5183 }
5184 } else if (resultSpan instanceof ForegroundColorSpan) {
5185 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5186 int foregroundColor = originalSpan.getForegroundColor();
5187 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005188 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005189 resultSpan = new ForegroundColorSpan(foregroundColor);
5190 if (fullLength) {
5191 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5192 }
5193 } else {
5194 resultSpan = span;
5195 }
5196
5197 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5198 }
5199 return builder;
5200 }
5201 return charSequence;
5202 }
5203
5204 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005205 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005206 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005207 */
5208 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005209 if (!mIsLegacyInitialized) {
5210 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5211 < Build.VERSION_CODES.LOLLIPOP;
5212 mIsLegacyInitialized = true;
5213 }
5214 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005215 }
5216
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005217 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005218 return processLegacyText(charSequence, false /* ambient */);
5219 }
5220
5221 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5222 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5223 boolean wantLightText = ambient;
5224 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005225 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005226 } else {
5227 return charSequence;
5228 }
5229 }
5230
Dan Sandler26e81cf2014-05-06 10:01:27 -04005231 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005232 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005233 */
Adrian Roos487374f2017-01-11 15:48:14 -08005234 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5235 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005236 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005237 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005238 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005239 contentView.setDrawableTint(R.id.icon, false, color,
5240 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005241
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005242 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005243 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005244 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005245 }
5246
Dan Sandler26e81cf2014-05-06 10:01:27 -04005247 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005248 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005249 * if it's grayscale).
5250 */
5251 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005252 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5253 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005254 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005255 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005256 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5257 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005258 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005259 }
5260
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005261 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005262 if (mN.color != COLOR_DEFAULT) {
5263 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005264 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005265 }
5266
Adrian Roos4ff3b122016-02-01 12:26:13 -08005267 int resolveContrastColor() {
5268 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5269 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005270 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005271
Selim Cinekac5f0272017-05-02 16:05:41 -07005272 int color;
5273 int background = mBackgroundColorHint;
5274 if (mBackgroundColorHint == COLOR_INVALID) {
5275 background = mContext.getColor(
5276 com.android.internal.R.color.notification_material_background_color);
5277 }
5278 if (mN.color == COLOR_DEFAULT) {
5279 ensureColors();
5280 color = mSecondaryTextColor;
5281 } else {
5282 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005283 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005284 }
5285 if (Color.alpha(color) < 255) {
5286 // alpha doesn't go well for color filters, so let's blend it manually
5287 color = NotificationColorUtil.compositeColors(color, background);
5288 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005289 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005290 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005291 }
5292
Adrian Roos487374f2017-01-11 15:48:14 -08005293 int resolveAmbientColor() {
5294 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5295 return mCachedAmbientColor;
5296 }
5297 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5298
5299 mCachedAmbientColorIsFor = mN.color;
5300 return mCachedAmbientColor = contrasted;
5301 }
5302
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005303 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005304 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005305 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005306 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005307 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005308 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005309 mN.actions = new Action[mActions.size()];
5310 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005311 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005312 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005313 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005314 }
Selim Cinek247fa012016-02-18 09:50:48 -08005315 if (mN.bigContentView != null || mN.contentView != null
5316 || mN.headsUpContentView != null) {
5317 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5318 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005319 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005320 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005321
Julia Reynolds3b848122016-02-26 10:45:32 -05005322 /**
5323 * Creates a Builder from an existing notification so further changes can be made.
5324 * @param context The context for your application / activity.
5325 * @param n The notification to create a Builder from.
5326 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005327 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005328 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005329 ApplicationInfo applicationInfo = n.extras.getParcelable(
5330 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005331 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005332 if (applicationInfo != null) {
5333 try {
5334 builderContext = context.createApplicationContext(applicationInfo,
5335 Context.CONTEXT_RESTRICTED);
5336 } catch (NameNotFoundException e) {
5337 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5338 builderContext = context; // try with our context
5339 }
5340 } else {
5341 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005342 }
5343
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005344 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005345 }
5346
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005347 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005348 * @deprecated Use {@link #build()} instead.
5349 */
5350 @Deprecated
5351 public Notification getNotification() {
5352 return build();
5353 }
5354
5355 /**
5356 * Combine all of the options that have been set and return a new {@link Notification}
5357 * object.
5358 */
5359 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005360 // first, add any extras from the calling code
5361 if (mUserExtras != null) {
5362 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005363 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005364
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005365 mN.creationTime = System.currentTimeMillis();
5366
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005367 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005368 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005369
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005370 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005371
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005372 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005373 mStyle.reduceImageSizes(mContext);
5374 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005375 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005376 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005377 }
Selim Cinekd0426622017-07-11 13:19:59 +02005378 mN.reduceImageSizes(mContext);
5379
Adrian Roos5081c0d2016-02-26 16:04:19 -08005380 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005381 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005382 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005383 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005384 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5385 mN.contentView.getSequenceNumber());
5386 }
5387 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005388 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005389 if (mN.bigContentView != null) {
5390 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5391 mN.bigContentView.getSequenceNumber());
5392 }
5393 }
5394 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005395 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005396 if (mN.headsUpContentView != null) {
5397 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5398 mN.headsUpContentView.getSequenceNumber());
5399 }
5400 }
5401 }
5402
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005403 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5404 mN.flags |= FLAG_SHOW_LIGHTS;
5405 }
5406
Adrian Roosfb921842017-10-26 14:49:56 +02005407 mN.allPendingIntents = null;
5408
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005409 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005410 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005411
5412 /**
5413 * Apply this Builder to an existing {@link Notification} object.
5414 *
5415 * @hide
5416 */
5417 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005418 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005419 return n;
5420 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005421
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005422 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005423 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005424 * change. Also removes extenders on low ram devices, as
5425 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005426 *
5427 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5428 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005429 * @hide
5430 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005431 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005432 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005433
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005434 // Only strip views for known Styles because we won't know how to
5435 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005436 if (!isLowRam
5437 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005438 && getNotificationStyleClass(templateClass) == null) {
5439 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005440 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005441
5442 // Only strip unmodified BuilderRemoteViews.
5443 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005444 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005445 n.contentView.getSequenceNumber();
5446 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005447 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005448 n.bigContentView.getSequenceNumber();
5449 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005450 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005451 n.headsUpContentView.getSequenceNumber();
5452
5453 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005454 if (!isLowRam
5455 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005456 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005457 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005458
5459 Notification clone = n.clone();
5460 if (stripContentView) {
5461 clone.contentView = null;
5462 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5463 }
5464 if (stripBigContentView) {
5465 clone.bigContentView = null;
5466 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5467 }
5468 if (stripHeadsUpContentView) {
5469 clone.headsUpContentView = null;
5470 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5471 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005472 if (isLowRam) {
5473 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5474 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5475 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5476 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005477 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005478 }
5479
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005480 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005481 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005482 }
5483
5484 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005485 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005486 }
5487
5488 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005489 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005490 }
5491
5492 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005493 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005494 }
5495
5496 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005497 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005498 }
5499
Adrian Roosc1a80b02016-04-05 14:54:55 -07005500 private int getMessagingLayoutResource() {
5501 return R.layout.notification_template_material_messaging;
5502 }
5503
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005504 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005505 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005506 }
5507
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005508 private int getEmphasizedActionLayoutResource() {
5509 return R.layout.notification_material_action_emphasized;
5510 }
5511
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005512 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005513 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005514 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005515
5516 private int getBackgroundColor() {
5517 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005518 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005519 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005520 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5521 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005522 }
5523 }
5524
5525 private boolean isColorized() {
5526 return mN.isColorized();
5527 }
Selim Cinek99104832017-01-25 14:47:33 -08005528
Anthony Chenad4d1582017-04-10 16:07:58 -07005529 private boolean shouldTintActionButtons() {
5530 return mTintActionButtons;
5531 }
5532
Selim Cinek99104832017-01-25 14:47:33 -08005533 private boolean textColorsNeedInversion() {
5534 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5535 return false;
5536 }
5537 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5538 return targetSdkVersion > Build.VERSION_CODES.M
5539 && targetSdkVersion < Build.VERSION_CODES.O;
5540 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005541
5542 /**
5543 * Set a color palette to be used as the background and textColors
5544 *
5545 * @param backgroundColor the color to be used as the background
5546 * @param foregroundColor the color to be used as the foreground
5547 *
5548 * @hide
5549 */
5550 public void setColorPalette(int backgroundColor, int foregroundColor) {
5551 mBackgroundColor = backgroundColor;
5552 mForegroundColor = foregroundColor;
5553 mTextColorsAreForBackground = COLOR_INVALID;
5554 ensureColors();
5555 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005556
5557 /**
5558 * Sets the background color for this notification to be a different one then the default.
5559 * This is mainly used to calculate contrast and won't necessarily be applied to the
5560 * background.
5561 *
5562 * @hide
5563 */
5564 public void setBackgroundColorHint(int backgroundColor) {
5565 mBackgroundColorHint = backgroundColor;
5566 }
Selim Cineka7679b62017-05-10 16:33:25 -07005567
5568
5569 /**
5570 * Forces all styled remoteViews to be built from scratch and not use any cached
5571 * RemoteViews.
5572 * This is needed for legacy apps that are baking in their remoteviews into the
5573 * notification.
5574 *
5575 * @hide
5576 */
5577 public void setRebuildStyledRemoteViews(boolean rebuild) {
5578 mRebuildStyledRemoteViews = rebuild;
5579 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005580 }
5581
5582 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005583 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5584 * remote views.
5585 *
5586 * @hide
5587 */
5588 void reduceImageSizes(Context context) {
5589 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5590 return;
5591 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005592 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005593 if (mLargeIcon != null || largeIcon != null) {
5594 Resources resources = context.getResources();
5595 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005596 int maxWidth = resources.getDimensionPixelSize(isLowRam
5597 ? R.dimen.notification_right_icon_size_low_ram
5598 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005599 int maxHeight = maxWidth;
5600 if (MediaStyle.class.equals(style)
5601 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005602 maxHeight = resources.getDimensionPixelSize(isLowRam
5603 ? R.dimen.notification_media_image_max_height_low_ram
5604 : R.dimen.notification_media_image_max_height);
5605 maxWidth = resources.getDimensionPixelSize(isLowRam
5606 ? R.dimen.notification_media_image_max_width_low_ram
5607 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005608 }
5609 if (mLargeIcon != null) {
5610 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5611 }
5612 if (largeIcon != null) {
5613 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5614 }
5615 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005616 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5617 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5618 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005619 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5620 }
5621
Selim Cineka8cb1262017-08-15 16:53:44 -07005622 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5623 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005624 if (remoteView != null) {
5625 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005626 int maxWidth = resources.getDimensionPixelSize(isLowRam
5627 ? R.dimen.notification_custom_view_max_image_width_low_ram
5628 : R.dimen.notification_custom_view_max_image_width);
5629 int maxHeight = resources.getDimensionPixelSize(isLowRam
5630 ? R.dimen.notification_custom_view_max_image_height_low_ram
5631 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005632 remoteView.reduceImageSizes(maxWidth, maxHeight);
5633 }
5634 }
5635
5636 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005637 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005638 */
Selim Cinek22714f12017-04-13 16:23:53 -07005639 private boolean isForegroundService() {
5640 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005641 }
5642
5643 /**
Selim Cinek99104832017-01-25 14:47:33 -08005644 * @return whether this notification has a media session attached
5645 * @hide
5646 */
5647 public boolean hasMediaSession() {
5648 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5649 }
5650
5651 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005652 * @return the style class of this notification
5653 * @hide
5654 */
5655 public Class<? extends Notification.Style> getNotificationStyle() {
5656 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5657
5658 if (!TextUtils.isEmpty(templateClass)) {
5659 return Notification.getNotificationStyleClass(templateClass);
5660 }
5661 return null;
5662 }
5663
5664 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005665 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005666 *
5667 * @hide
5668 */
5669 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005670 if (isColorizedMedia()) {
5671 return true;
5672 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005673 return extras.getBoolean(EXTRA_COLORIZED)
5674 && (hasColorizedPermission() || isForegroundService());
5675 }
5676
5677 /**
5678 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5679 * permission. The permission is checked when a notification is enqueued.
5680 */
5681 private boolean hasColorizedPermission() {
5682 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005683 }
5684
5685 /**
5686 * @return true if this notification is colorized and it is a media notification
5687 *
5688 * @hide
5689 */
5690 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005691 Class<? extends Style> style = getNotificationStyle();
5692 if (MediaStyle.class.equals(style)) {
5693 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5694 if ((colorized == null || colorized) && hasMediaSession()) {
5695 return true;
5696 }
5697 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5698 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5699 return true;
5700 }
5701 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005702 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005703 }
5704
Selim Cinek0847acd2017-04-24 19:48:29 -07005705
5706 /**
5707 * @return true if this is a media notification
5708 *
5709 * @hide
5710 */
5711 public boolean isMediaNotification() {
5712 Class<? extends Style> style = getNotificationStyle();
5713 if (MediaStyle.class.equals(style)) {
5714 return true;
5715 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5716 return true;
5717 }
5718 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005719 }
5720
Selim Cinek279fa862016-06-14 10:57:25 -07005721 private boolean hasLargeIcon() {
5722 return mLargeIcon != null || largeIcon != null;
5723 }
5724
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005725 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005726 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005727 * @hide
5728 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005729 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005730 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5731 }
5732
5733 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005734 * @return true if the notification will show a chronometer; false otherwise
5735 * @hide
5736 */
5737 public boolean showsChronometer() {
5738 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5739 }
5740
5741 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005742 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005743 */
5744 @SystemApi
5745 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5746 Class<? extends Style>[] classes = new Class[] {
5747 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5748 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5749 MessagingStyle.class };
5750 for (Class<? extends Style> innerClass : classes) {
5751 if (templateClass.equals(innerClass.getName())) {
5752 return innerClass;
5753 }
5754 }
5755 return null;
5756 }
5757
5758 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005759 * An object that can apply a rich notification style to a {@link Notification.Builder}
5760 * object.
5761 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005762 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005763 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005764
5765 /**
5766 * @hide
5767 */
5768 protected CharSequence mSummaryText = null;
5769
5770 /**
5771 * @hide
5772 */
5773 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005774
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005775 protected Builder mBuilder;
5776
Chris Wrend6297db2012-05-03 16:20:13 -04005777 /**
5778 * Overrides ContentTitle in the big form of the template.
5779 * This defaults to the value passed to setContentTitle().
5780 */
5781 protected void internalSetBigContentTitle(CharSequence title) {
5782 mBigContentTitle = title;
5783 }
5784
5785 /**
5786 * Set the first line of text after the detail section in the big form of the template.
5787 */
5788 protected void internalSetSummaryText(CharSequence cs) {
5789 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005790 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005791 }
5792
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005793 public void setBuilder(Builder builder) {
5794 if (mBuilder != builder) {
5795 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005796 if (mBuilder != null) {
5797 mBuilder.setStyle(this);
5798 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005799 }
5800 }
5801
Chris Wrend6297db2012-05-03 16:20:13 -04005802 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005803 if (mBuilder == null) {
5804 throw new IllegalArgumentException("Style requires a valid Builder object");
5805 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005806 }
Chris Wrend6297db2012-05-03 16:20:13 -04005807
5808 protected RemoteViews getStandardView(int layoutId) {
5809 checkBuilder();
5810
Christoph Studer4600f9b2014-07-22 22:44:43 +02005811 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005812 CharSequence oldBuilderContentTitle =
5813 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005814 if (mBigContentTitle != null) {
5815 mBuilder.setContentTitle(mBigContentTitle);
5816 }
5817
Chris Wrend6297db2012-05-03 16:20:13 -04005818 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5819
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005820 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005821
Chris Wrend6297db2012-05-03 16:20:13 -04005822 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5823 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005824 } else {
5825 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005826 }
5827
Chris Wrend6297db2012-05-03 16:20:13 -04005828 return contentView;
5829 }
5830
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005831 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005832 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005833 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005834 *
5835 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005836 * @hide
5837 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005838 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005839 return null;
5840 }
5841
5842 /**
5843 * Construct a Style-specific RemoteViews for the final big notification layout.
5844 * @hide
5845 */
5846 public RemoteViews makeBigContentView() {
5847 return null;
5848 }
5849
5850 /**
5851 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005852 *
5853 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005854 * @hide
5855 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005856 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005857 return null;
5858 }
5859
5860 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005861 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005862 * @hide
5863 */
5864 public void addExtras(Bundle extras) {
5865 if (mSummaryTextSet) {
5866 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5867 }
5868 if (mBigContentTitle != null) {
5869 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5870 }
Chris Wren91ad5632013-06-05 15:05:57 -04005871 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005872 }
5873
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005874 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005875 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005876 * @hide
5877 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005878 protected void restoreFromExtras(Bundle extras) {
5879 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5880 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5881 mSummaryTextSet = true;
5882 }
5883 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5884 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5885 }
5886 }
5887
5888
5889 /**
5890 * @hide
5891 */
5892 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005893 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005894 return wip;
5895 }
5896
Daniel Sandler0ec46202015-06-24 01:27:05 -04005897 /**
5898 * @hide
5899 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005900 public void purgeResources() {}
5901
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005902 /**
5903 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5904 * attached to.
5905 *
5906 * @return the fully constructed Notification.
5907 */
5908 public Notification build() {
5909 checkBuilder();
5910 return mBuilder.build();
5911 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005912
5913 /**
5914 * @hide
5915 * @return true if the style positions the progress bar on the second line; false if the
5916 * style hides the progress bar
5917 */
5918 protected boolean hasProgress() {
5919 return true;
5920 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005921
5922 /**
5923 * @hide
5924 * @return Whether we should put the summary be put into the notification header
5925 */
5926 public boolean hasSummaryInHeader() {
5927 return true;
5928 }
Selim Cinek593610c2016-02-16 18:42:57 -08005929
5930 /**
5931 * @hide
5932 * @return Whether custom content views are displayed inline in the style
5933 */
5934 public boolean displayCustomViewInline() {
5935 return false;
5936 }
Selim Cinekd0426622017-07-11 13:19:59 +02005937
5938 /**
5939 * Reduces the image sizes contained in this style.
5940 *
5941 * @hide
5942 */
5943 public void reduceImageSizes(Context context) {
5944 }
Selim Cinek90343862018-02-01 11:07:11 -08005945
5946 /**
5947 * Validate that this style was properly composed. This is called at build time.
5948 * @hide
5949 */
5950 public void validate(Context context) {
5951 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05005952
5953 /**
5954 * @hide
5955 */
5956 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Joe Onorato46439ce2010-11-19 13:56:21 -08005957 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005958
5959 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005960 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005961 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005962 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005963 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005964 * Notification notif = new Notification.Builder(mContext)
5965 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5966 * .setContentText(subject)
5967 * .setSmallIcon(R.drawable.new_post)
5968 * .setLargeIcon(aBitmap)
5969 * .setStyle(new Notification.BigPictureStyle()
5970 * .bigPicture(aBigBitmap))
5971 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005972 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005973 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005974 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005975 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005976 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005977 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005978 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005979 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005980
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005981 public BigPictureStyle() {
5982 }
5983
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005984 /**
5985 * @deprecated use {@code BigPictureStyle()}.
5986 */
5987 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005988 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005989 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005990 }
5991
Chris Wrend6297db2012-05-03 16:20:13 -04005992 /**
5993 * Overrides ContentTitle in the big form of the template.
5994 * This defaults to the value passed to setContentTitle().
5995 */
5996 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005997 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005998 return this;
5999 }
6000
6001 /**
6002 * Set the first line of text after the detail section in the big form of the template.
6003 */
6004 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006005 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006006 return this;
6007 }
6008
Chris Wren0bd664d2012-08-01 13:56:56 -04006009 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006010 * @hide
6011 */
6012 public Bitmap getBigPicture() {
6013 return mPicture;
6014 }
6015
6016 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006017 * Provide the bitmap to be used as the payload for the BigPicture notification.
6018 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006019 public BigPictureStyle bigPicture(Bitmap b) {
6020 mPicture = b;
6021 return this;
6022 }
6023
Chris Wren3745a3d2012-05-22 15:11:52 -04006024 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006025 * Override the large icon when the big notification is shown.
6026 */
6027 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006028 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6029 }
6030
6031 /**
6032 * Override the large icon when the big notification is shown.
6033 */
6034 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006035 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006036 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006037 return this;
6038 }
6039
Riley Andrews0394a0c2015-11-03 23:36:52 -08006040 /** @hide */
6041 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6042
Daniel Sandler0ec46202015-06-24 01:27:05 -04006043 /**
6044 * @hide
6045 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006046 @Override
6047 public void purgeResources() {
6048 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006049 if (mPicture != null &&
6050 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006051 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006052 mPicture = mPicture.createAshmemBitmap();
6053 }
6054 if (mBigLargeIcon != null) {
6055 mBigLargeIcon.convertToAshmem();
6056 }
6057 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006058
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006059 /**
6060 * @hide
6061 */
Selim Cinekd0426622017-07-11 13:19:59 +02006062 @Override
6063 public void reduceImageSizes(Context context) {
6064 super.reduceImageSizes(context);
6065 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006066 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006067 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006068 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6069 ? R.dimen.notification_big_picture_max_height_low_ram
6070 : R.dimen.notification_big_picture_max_height);
6071 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6072 ? R.dimen.notification_big_picture_max_width_low_ram
6073 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006074 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6075 }
6076 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006077 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6078 ? R.dimen.notification_right_icon_size_low_ram
6079 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006080 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6081 }
6082 }
6083
6084 /**
6085 * @hide
6086 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006087 public RemoteViews makeBigContentView() {
6088 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006089 // This covers the following cases:
6090 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006091 // mN.mLargeIcon
6092 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006093 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006094 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006095 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006096 oldLargeIcon = mBuilder.mN.mLargeIcon;
6097 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006098 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6099 // replacement if the other one is null. Because we're restoring these legacy icons
6100 // for old listeners, this is in general non-null.
6101 largeIconLegacy = mBuilder.mN.largeIcon;
6102 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006103 }
6104
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006105 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05006106 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006107 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6108 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006109 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006110 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006111 }
Selim Cinek279fa862016-06-14 10:57:25 -07006112 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006113
Christoph Studer5c510ee2014-12-15 16:32:27 +01006114 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006115 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006116 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006117 }
6118
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006119 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006120 return contentView;
6121 }
6122
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006123 /**
6124 * @hide
6125 */
6126 public void addExtras(Bundle extras) {
6127 super.addExtras(extras);
6128
6129 if (mBigLargeIconSet) {
6130 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6131 }
6132 extras.putParcelable(EXTRA_PICTURE, mPicture);
6133 }
6134
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006135 /**
6136 * @hide
6137 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006138 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006139 protected void restoreFromExtras(Bundle extras) {
6140 super.restoreFromExtras(extras);
6141
6142 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006143 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006144 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006145 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006146 mPicture = extras.getParcelable(EXTRA_PICTURE);
6147 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006148
6149 /**
6150 * @hide
6151 */
6152 @Override
6153 public boolean hasSummaryInHeader() {
6154 return false;
6155 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006156
6157 /**
6158 * @hide
6159 */
6160 @Override
6161 public boolean areNotificationsVisiblyDifferent(Style other) {
6162 if (other == null || getClass() != other.getClass()) {
6163 return true;
6164 }
6165 BigPictureStyle otherS = (BigPictureStyle) other;
6166 return !Objects.equals(getBigPicture(), otherS.getBigPicture());
6167 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006168 }
6169
6170 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006171 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006172 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006173 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006174 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006175 * Notification notif = new Notification.Builder(mContext)
6176 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6177 * .setContentText(subject)
6178 * .setSmallIcon(R.drawable.new_mail)
6179 * .setLargeIcon(aBitmap)
6180 * .setStyle(new Notification.BigTextStyle()
6181 * .bigText(aVeryLongString))
6182 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006183 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006184 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006185 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006186 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006187 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006188
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006189 private CharSequence mBigText;
6190
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006191 public BigTextStyle() {
6192 }
6193
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006194 /**
6195 * @deprecated use {@code BigTextStyle()}.
6196 */
6197 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006198 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006199 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006200 }
6201
Chris Wrend6297db2012-05-03 16:20:13 -04006202 /**
6203 * Overrides ContentTitle in the big form of the template.
6204 * This defaults to the value passed to setContentTitle().
6205 */
6206 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006207 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006208 return this;
6209 }
6210
6211 /**
6212 * Set the first line of text after the detail section in the big form of the template.
6213 */
6214 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006215 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006216 return this;
6217 }
6218
Chris Wren0bd664d2012-08-01 13:56:56 -04006219 /**
6220 * Provide the longer text to be displayed in the big form of the
6221 * template in place of the content text.
6222 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006223 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006224 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006225 return this;
6226 }
6227
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006228 /**
6229 * @hide
6230 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006231 public CharSequence getBigText() {
6232 return mBigText;
6233 }
6234
6235 /**
6236 * @hide
6237 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006238 public void addExtras(Bundle extras) {
6239 super.addExtras(extras);
6240
Christoph Studer4600f9b2014-07-22 22:44:43 +02006241 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6242 }
6243
6244 /**
6245 * @hide
6246 */
6247 @Override
6248 protected void restoreFromExtras(Bundle extras) {
6249 super.restoreFromExtras(extras);
6250
6251 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006252 }
6253
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006254 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006255 * @param increasedHeight true if this layout be created with an increased height.
6256 *
6257 * @hide
6258 */
6259 @Override
6260 public RemoteViews makeContentView(boolean increasedHeight) {
6261 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006262 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006263 mBuilder.mActions = new ArrayList<>();
6264 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006265 mBuilder.mActions = mBuilder.mOriginalActions;
6266 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006267 return remoteViews;
6268 }
6269 return super.makeContentView(increasedHeight);
6270 }
6271
6272 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006273 * @hide
6274 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006275 @Override
6276 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6277 if (increasedHeight && mBuilder.mActions.size() > 0) {
6278 return makeBigContentView();
6279 }
6280 return super.makeHeadsUpContentView(increasedHeight);
6281 }
6282
6283 /**
6284 * @hide
6285 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006286 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006287
6288 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006289 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006290 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006291
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006292 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006293
Selim Cinek75998782016-04-26 10:39:17 -07006294 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006295
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006296 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006297 if (TextUtils.isEmpty(bigTextText)) {
6298 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6299 // experience
6300 bigTextText = mBuilder.processLegacyText(text);
6301 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006302 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006303
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006304 return contentView;
6305 }
6306
Julia Reynolds7217dc92018-03-07 12:12:09 -05006307 /**
6308 * @hide
6309 */
6310 @Override
6311 public boolean areNotificationsVisiblyDifferent(Style other) {
6312 if (other == null || getClass() != other.getClass()) {
6313 return true;
6314 }
6315 BigTextStyle newS = (BigTextStyle) other;
6316 return !Objects.equals(getBigText(), newS.getBigText());
6317 }
6318
Adrian Roosb1f427c2016-05-26 12:27:15 -07006319 static void applyBigTextContentView(Builder builder,
6320 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006321 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006322 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006323 contentView.setViewVisibility(R.id.big_text,
6324 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006325 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006326 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006327 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006328
6329 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006330 * Helper class for generating large-format notifications that include multiple back-and-forth
6331 * messages of varying types between any number of people.
6332 *
6333 * <br>
6334 * If the platform does not provide large-format notifications, this method has no effect. The
6335 * user will always see the normal notification view.
6336 * <br>
6337 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6338 * so:
6339 * <pre class="prettyprint">
6340 *
6341 * Notification noti = new Notification.Builder()
6342 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6343 * .setContentText(subject)
6344 * .setSmallIcon(R.drawable.new_message)
6345 * .setLargeIcon(aBitmap)
6346 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6347 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6348 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6349 * .build();
6350 * </pre>
6351 */
6352 public static class MessagingStyle extends Style {
6353
6354 /**
6355 * The maximum number of messages that will be retained in the Notification itself (the
6356 * number displayed is up to the platform).
6357 */
6358 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6359
Selim Cinekcb8b9852017-12-15 18:01:52 -08006360 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006361 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006362 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006363 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006364 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006365
6366 MessagingStyle() {
6367 }
6368
6369 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006370 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6371 * user before the posting app reposts the notification with those messages after they've
6372 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006373 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006374 *
6375 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006376 */
Alex Hillsfd590442016-10-07 09:52:44 -04006377 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006378 this(new Person().setName(userDisplayName));
6379 }
6380
6381 /**
6382 * @param user Required - The person displayed for any messages that are sent by the
6383 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6384 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006385 * by this user. The user also needs to have a valid name associated with it, which will
6386 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006387 */
6388 public MessagingStyle(@NonNull Person user) {
6389 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006390 }
6391
6392 /**
6393 * Validate that this style was properly composed. This is called at build time.
6394 * @hide
6395 */
6396 @Override
6397 public void validate(Context context) {
6398 super.validate(context);
6399 if (context.getApplicationInfo().targetSdkVersion
6400 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6401 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006402 }
6403 }
6404
6405 /**
6406 * @return the user to be displayed for any replies sent by the user
6407 */
6408 public Person getUser() {
6409 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006410 }
6411
6412 /**
6413 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006414 *
6415 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006416 */
6417 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006418 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006419 }
6420
6421 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006422 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6423 *
Kodlee Yin14656422017-12-22 17:00:46 -08006424 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6425 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6426 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6427 * {@code true} and passing {@code null} will make it return {@code false}. In
6428 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6429 * to set group conversation status.
6430 *
6431 * @param conversationTitle Title displayed for this conversation
6432 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006433 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006434 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006435 mConversationTitle = conversationTitle;
6436 return this;
6437 }
6438
6439 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006440 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006441 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006442 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006443 public CharSequence getConversationTitle() {
6444 return mConversationTitle;
6445 }
6446
6447 /**
6448 * Adds a message for display by this notification. Convenience call for a simple
6449 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6450 * @param text A {@link CharSequence} to be displayed as the message content
6451 * @param timestamp Time at which the message arrived
6452 * @param sender A {@link CharSequence} to be used for displaying the name of the
6453 * sender. Should be <code>null</code> for messages by the current user, in which case
6454 * the platform will insert {@link #getUserDisplayName()}.
6455 * Should be unique amongst all individuals in the conversation, and should be
6456 * consistent during re-posts of the notification.
6457 *
6458 * @see Message#Message(CharSequence, long, CharSequence)
6459 *
6460 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006461 *
6462 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006463 */
6464 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006465 return addMessage(text, timestamp,
6466 sender == null ? null : new Person().setName(sender));
6467 }
6468
6469 /**
6470 * Adds a message for display by this notification. Convenience call for a simple
6471 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6472 * @param text A {@link CharSequence} to be displayed as the message content
6473 * @param timestamp Time at which the message arrived
6474 * @param sender The {@link Person} who sent the message.
6475 * Should be <code>null</code> for messages by the current user, in which case
6476 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6477 *
6478 * @see Message#Message(CharSequence, long, CharSequence)
6479 *
6480 * @return this object for method chaining
6481 */
6482 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006483 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006484 }
6485
6486 /**
6487 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006488 *
6489 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6490 * the newest last.
6491 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006492 * @param message The {@link Message} to be displayed
6493 * @return this object for method chaining
6494 */
6495 public MessagingStyle addMessage(Message message) {
6496 mMessages.add(message);
6497 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6498 mMessages.remove(0);
6499 }
6500 return this;
6501 }
6502
6503 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006504 * Adds a {@link Message} for historic context in this notification.
6505 *
6506 * <p>Messages should be added as historic if they are not the main subject of the
6507 * notification but may give context to a conversation. The system may choose to present
6508 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6509 *
6510 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6511 * the newest last.
6512 *
6513 * @param message The historic {@link Message} to be added
6514 * @return this object for method chaining
6515 */
6516 public MessagingStyle addHistoricMessage(Message message) {
6517 mHistoricMessages.add(message);
6518 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6519 mHistoricMessages.remove(0);
6520 }
6521 return this;
6522 }
6523
6524 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006525 * Gets the list of {@code Message} objects that represent the notification
6526 */
6527 public List<Message> getMessages() {
6528 return mMessages;
6529 }
6530
6531 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006532 * Gets the list of historic {@code Message}s in the notification.
6533 */
6534 public List<Message> getHistoricMessages() {
6535 return mHistoricMessages;
6536 }
6537
6538 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006539 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006540 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006541 * @param isGroupConversation {@code true} if the conversation represents a group,
6542 * {@code false} otherwise.
6543 * @return this object for method chaining
6544 */
6545 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6546 mIsGroupConversation = isGroupConversation;
6547 return this;
6548 }
6549
6550 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006551 * Returns {@code true} if this notification represents a group conversation, otherwise
6552 * {@code false}.
6553 *
6554 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6555 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6556 * not the conversation title is set; returning {@code true} if the conversation title is
6557 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6558 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6559 * named, non-group conversations.
6560 *
6561 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006562 */
6563 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006564 // When target SDK version is < P, a non-null conversation title dictates if this is
6565 // as group conversation.
6566 if (mBuilder != null
6567 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6568 < Build.VERSION_CODES.P) {
6569 return mConversationTitle != null;
6570 }
6571
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006572 return mIsGroupConversation;
6573 }
6574
6575 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006576 * @hide
6577 */
6578 @Override
6579 public void addExtras(Bundle extras) {
6580 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006581 if (mUser != null) {
6582 // For legacy usages
6583 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6584 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006585 }
6586 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006587 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006588 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006589 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6590 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006591 }
Adrian Roos437cd562017-01-18 15:47:03 -08006592 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6593 Message.getBundleArrayForMessages(mHistoricMessages));
6594 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006595
6596 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006597 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006598 }
6599
6600 private void fixTitleAndTextExtras(Bundle extras) {
6601 Message m = findLatestIncomingMessage();
6602 CharSequence text = (m == null) ? null : m.mText;
6603 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006604 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6605 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006606 CharSequence title;
6607 if (!TextUtils.isEmpty(mConversationTitle)) {
6608 if (!TextUtils.isEmpty(sender)) {
6609 BidiFormatter bidi = BidiFormatter.getInstance();
6610 title = mBuilder.mContext.getString(
6611 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006612 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006613 } else {
6614 title = mConversationTitle;
6615 }
6616 } else {
6617 title = sender;
6618 }
6619
6620 if (title != null) {
6621 extras.putCharSequence(EXTRA_TITLE, title);
6622 }
6623 if (text != null) {
6624 extras.putCharSequence(EXTRA_TEXT, text);
6625 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006626 }
6627
6628 /**
6629 * @hide
6630 */
6631 @Override
6632 protected void restoreFromExtras(Bundle extras) {
6633 super.restoreFromExtras(extras);
6634
Selim Cinekcb8b9852017-12-15 18:01:52 -08006635 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6636 if (mUser == null) {
6637 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6638 mUser = new Person().setName(displayName);
6639 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006640 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006641 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006642 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006643 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006644 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006645 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006646 }
6647
6648 /**
6649 * @hide
6650 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006651 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006652 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006653 mBuilder.mOriginalActions = mBuilder.mActions;
6654 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006655 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006656 mBuilder.mActions = mBuilder.mOriginalActions;
6657 mBuilder.mOriginalActions = null;
6658 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006659 }
6660
Julia Reynolds7217dc92018-03-07 12:12:09 -05006661 /**
6662 * @hide
6663 */
6664 @Override
6665 public boolean areNotificationsVisiblyDifferent(Style other) {
6666 if (other == null || getClass() != other.getClass()) {
6667 return true;
6668 }
6669 MessagingStyle newS = (MessagingStyle) other;
6670 List<MessagingStyle.Message> oldMs = getMessages();
6671 List<MessagingStyle.Message> newMs = newS.getMessages();
6672
6673 if (oldMs == null) {
6674 oldMs = new ArrayList<>();
6675 }
6676 if (newMs == null) {
6677 newMs = new ArrayList<>();
6678 }
6679
6680 int n = oldMs.size();
6681 if (n != newMs.size()) {
6682 return true;
6683 }
6684 for (int i = 0; i < n; i++) {
6685 MessagingStyle.Message oldM = oldMs.get(i);
6686 MessagingStyle.Message newM = newMs.get(i);
6687 if (!Objects.equals(oldM.getText(), newM.getText())) {
6688 return true;
6689 }
6690 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
6691 return true;
6692 }
6693 CharSequence oldSender = oldM.getSenderPerson() == null ? oldM.getSender()
6694 : oldM.getSenderPerson().getName();
6695 CharSequence newSender = newM.getSenderPerson() == null ? newM.getSender()
6696 : newM.getSenderPerson().getName();
6697 if (!Objects.equals(oldSender, newSender)) {
6698 return true;
6699 }
6700
6701 String oldKey = oldM.getSenderPerson() == null
6702 ? null : oldM.getSenderPerson().getKey();
6703 String newKey = newM.getSenderPerson() == null
6704 ? null : newM.getSenderPerson().getKey();
6705 if (!Objects.equals(oldKey, newKey)) {
6706 return true;
6707 }
6708 // Other fields (like timestamp) intentionally excluded
6709 }
6710 return false;
6711 }
6712
Adrian Roosc1a80b02016-04-05 14:54:55 -07006713 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006714 return findLatestIncomingMessage(mMessages);
6715 }
6716
6717 /**
6718 * @hide
6719 */
6720 @Nullable
6721 public static Message findLatestIncomingMessage(
6722 List<Message> messages) {
6723 for (int i = messages.size() - 1; i >= 0; i--) {
6724 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006725 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006726 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006727 return m;
6728 }
6729 }
Selim Cinek88188f22017-09-19 16:46:56 -07006730 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006731 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006732 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006733 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006734 return null;
6735 }
6736
6737 /**
6738 * @hide
6739 */
6740 @Override
6741 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006742 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006743 }
6744
6745 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006746 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006747 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006748 ? super.mBigContentTitle
6749 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006750 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006751 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006752 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006753 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006754 nameReplacement = conversationTitle;
6755 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006756 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006757 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006758 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006759 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006760 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006761 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006762 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006763 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006764 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006765 // also update the end margin if there is an image
6766 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006767 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006768 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6769 }
6770 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006771 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6772 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006773 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6774 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006775 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6776 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006777 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6778 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006779 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6780 isOneToOne);
6781 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6782 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006783 return contentView;
6784 }
6785
Selim Cinekf7409db2017-10-24 16:17:14 -07006786 private boolean hasOnlyWhiteSpaceSenders() {
6787 for (int i = 0; i < mMessages.size(); i++) {
6788 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006789 Person sender = m.getSenderPerson();
6790 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006791 return false;
6792 }
6793 }
6794 return true;
6795 }
6796
6797 private boolean isWhiteSpace(CharSequence sender) {
6798 if (TextUtils.isEmpty(sender)) {
6799 return true;
6800 }
6801 if (sender.toString().matches("^\\s*$")) {
6802 return true;
6803 }
6804 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6805 // For the presentation that we had.
6806 for (int i = 0; i < sender.length(); i++) {
6807 char c = sender.charAt(i);
6808 if (c != '\u200B') {
6809 return false;
6810 }
6811 }
6812 return true;
6813 }
6814
Selim Cinek88188f22017-09-19 16:46:56 -07006815 private CharSequence createConversationTitleFromMessages() {
6816 ArraySet<CharSequence> names = new ArraySet<>();
6817 for (int i = 0; i < mMessages.size(); i++) {
6818 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006819 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006820 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006821 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006822 }
6823 }
6824 SpannableStringBuilder title = new SpannableStringBuilder();
6825 int size = names.size();
6826 for (int i = 0; i < size; i++) {
6827 CharSequence name = names.valueAt(i);
6828 if (!TextUtils.isEmpty(title)) {
6829 title.append(", ");
6830 }
6831 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6832 }
6833 return title;
6834 }
6835
Adrian Roosdedd1df2016-04-26 16:38:47 -07006836 /**
6837 * @hide
6838 */
6839 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006840 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006841 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006842 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6843 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006844 }
6845
Adrian Roosc1a80b02016-04-05 14:54:55 -07006846 private static TextAppearanceSpan makeFontColorSpan(int color) {
6847 return new TextAppearanceSpan(null, 0, 0,
6848 ColorStateList.valueOf(color), null);
6849 }
6850
Alex Hillsd9b04d92016-04-11 16:38:16 -04006851 public static final class Message {
6852
6853 static final String KEY_TEXT = "text";
6854 static final String KEY_TIMESTAMP = "time";
6855 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006856 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006857 static final String KEY_DATA_MIME_TYPE = "type";
6858 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006859 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006860
6861 private final CharSequence mText;
6862 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006863 @Nullable
6864 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006865
Shane Brennan5a871862017-03-11 13:14:17 -08006866 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006867 private String mDataMimeType;
6868 private Uri mDataUri;
6869
6870 /**
6871 * Constructor
6872 * @param text A {@link CharSequence} to be displayed as the message content
6873 * @param timestamp Time at which the message arrived
6874 * @param sender A {@link CharSequence} to be used for displaying the name of the
6875 * sender. Should be <code>null</code> for messages by the current user, in which case
6876 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6877 * Should be unique amongst all individuals in the conversation, and should be
6878 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006879 *
6880 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006881 */
6882 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006883 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6884 }
6885
6886 /**
6887 * Constructor
6888 * @param text A {@link CharSequence} to be displayed as the message content
6889 * @param timestamp Time at which the message arrived
6890 * @param sender The {@link Person} who sent the message.
6891 * Should be <code>null</code> for messages by the current user, in which case
6892 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6893 * <p>
6894 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6895 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6896 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6897 * in order to differentiate between the different users.
6898 * </p>
6899 */
6900 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006901 mText = text;
6902 mTimestamp = timestamp;
6903 mSender = sender;
6904 }
6905
6906 /**
6907 * Sets a binary blob of data and an associated MIME type for a message. In the case
6908 * where the platform doesn't support the MIME type, the original text provided in the
6909 * constructor will be used.
6910 * @param dataMimeType The MIME type of the content. See
6911 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6912 * types on Android and Android Wear.
6913 * @param dataUri The uri containing the content whose type is given by the MIME type.
6914 * <p class="note">
6915 * <ol>
6916 * <li>Notification Listeners including the System UI need permission to access the
6917 * data the Uri points to. The recommended ways to do this are:</li>
6918 * <li>Store the data in your own ContentProvider, making sure that other apps have
6919 * the correct permission to access your provider. The preferred mechanism for
6920 * providing access is to use per-URI permissions which are temporary and only
6921 * grant access to the receiving application. An easy way to create a
6922 * ContentProvider like this is to use the FileProvider helper class.</li>
6923 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6924 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6925 * also store non-media types (see MediaStore.Files for more info). Files can be
6926 * inserted into the MediaStore using scanFile() after which a content:// style
6927 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6928 * Note that once added to the system MediaStore the content is accessible to any
6929 * app on the device.</li>
6930 * </ol>
6931 * @return this object for method chaining
6932 */
6933 public Message setData(String dataMimeType, Uri dataUri) {
6934 mDataMimeType = dataMimeType;
6935 mDataUri = dataUri;
6936 return this;
6937 }
6938
Alex Hillsfc737de2016-03-23 17:33:02 -04006939 /**
6940 * Get the text to be used for this message, or the fallback text if a type and content
6941 * Uri have been set
6942 */
6943 public CharSequence getText() {
6944 return mText;
6945 }
6946
6947 /**
6948 * Get the time at which this message arrived
6949 */
6950 public long getTimestamp() {
6951 return mTimestamp;
6952 }
6953
6954 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006955 * Get the extras Bundle for this message.
6956 */
6957 public Bundle getExtras() {
6958 return mExtras;
6959 }
6960
6961 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006962 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006963 *
6964 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006965 */
6966 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006967 return mSender == null ? null : mSender.getName();
6968 }
6969
6970 /**
6971 * Get the sender associated with this message.
6972 */
6973 @Nullable
6974 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006975 return mSender;
6976 }
6977
6978 /**
6979 * Get the MIME type of the data pointed to by the Uri
6980 */
6981 public String getDataMimeType() {
6982 return mDataMimeType;
6983 }
6984
6985 /**
6986 * Get the the Uri pointing to the content of the message. Can be null, in which case
6987 * {@see #getText()} is used.
6988 */
6989 public Uri getDataUri() {
6990 return mDataUri;
6991 }
6992
Alex Hillsd9b04d92016-04-11 16:38:16 -04006993 private Bundle toBundle() {
6994 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006995 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006996 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006997 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006998 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006999 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007000 // Legacy listeners need this
7001 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7002 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007003 }
7004 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007005 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007006 }
7007 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007008 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007009 }
Shane Brennan5a871862017-03-11 13:14:17 -08007010 if (mExtras != null) {
7011 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7012 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007013 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007014 }
7015
Alex Hillsd9b04d92016-04-11 16:38:16 -04007016 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7017 Bundle[] bundles = new Bundle[messages.size()];
7018 final int N = messages.size();
7019 for (int i = 0; i < N; i++) {
7020 bundles[i] = messages.get(i).toBundle();
7021 }
7022 return bundles;
7023 }
7024
Selim Cinek88188f22017-09-19 16:46:56 -07007025 /**
7026 * @return A list of messages read from the bundles.
7027 *
7028 * @hide
7029 */
7030 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7031 if (bundles == null) {
7032 return new ArrayList<>();
7033 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007034 List<Message> messages = new ArrayList<>(bundles.length);
7035 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007036 if (bundles[i] instanceof Bundle) {
7037 Message message = getMessageFromBundle((Bundle)bundles[i]);
7038 if (message != null) {
7039 messages.add(message);
7040 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007041 }
7042 }
7043 return messages;
7044 }
7045
7046 static Message getMessageFromBundle(Bundle bundle) {
7047 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007048 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007049 return null;
7050 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007051
7052 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7053 if (senderPerson == null) {
7054 // Legacy apps that use compat don't actually provide the sender objects
7055 // We need to fix the compat version to provide people / use
7056 // the native api instead
7057 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7058 if (senderName != null) {
7059 senderPerson = new Person().setName(senderName);
7060 }
7061 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007062 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007063 bundle.getLong(KEY_TIMESTAMP),
7064 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04007065 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7066 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007067 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7068 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007069 }
Shane Brennan5a871862017-03-11 13:14:17 -08007070 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7071 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7072 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007073 return message;
7074 }
7075 } catch (ClassCastException e) {
7076 return null;
7077 }
7078 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007079 }
7080 }
7081
7082 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007083 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007084 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007085 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007086 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007087 * Notification notif = new Notification.Builder(mContext)
7088 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7089 * .setContentText(subject)
7090 * .setSmallIcon(R.drawable.new_mail)
7091 * .setLargeIcon(aBitmap)
7092 * .setStyle(new Notification.InboxStyle()
7093 * .addLine(str1)
7094 * .addLine(str2)
7095 * .setContentTitle(&quot;&quot;)
7096 * .setSummaryText(&quot;+3 more&quot;))
7097 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007098 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007099 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007100 * @see Notification#bigContentView
7101 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007102 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007103 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7104
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007105 public InboxStyle() {
7106 }
7107
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007108 /**
7109 * @deprecated use {@code InboxStyle()}.
7110 */
7111 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007112 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007113 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007114 }
7115
Chris Wrend6297db2012-05-03 16:20:13 -04007116 /**
7117 * Overrides ContentTitle in the big form of the template.
7118 * This defaults to the value passed to setContentTitle().
7119 */
7120 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007121 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007122 return this;
7123 }
7124
7125 /**
7126 * Set the first line of text after the detail section in the big form of the template.
7127 */
7128 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007129 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007130 return this;
7131 }
7132
Chris Wren0bd664d2012-08-01 13:56:56 -04007133 /**
7134 * Append a line to the digest section of the Inbox notification.
7135 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007136 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007137 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007138 return this;
7139 }
7140
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007141 /**
7142 * @hide
7143 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007144 public ArrayList<CharSequence> getLines() {
7145 return mTexts;
7146 }
7147
7148 /**
7149 * @hide
7150 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007151 public void addExtras(Bundle extras) {
7152 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007153
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007154 CharSequence[] a = new CharSequence[mTexts.size()];
7155 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7156 }
7157
Christoph Studer4600f9b2014-07-22 22:44:43 +02007158 /**
7159 * @hide
7160 */
7161 @Override
7162 protected void restoreFromExtras(Bundle extras) {
7163 super.restoreFromExtras(extras);
7164
7165 mTexts.clear();
7166 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7167 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7168 }
7169 }
7170
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007171 /**
7172 * @hide
7173 */
7174 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007175 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007176 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007177 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7178 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007179
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01007180 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04007181
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007182 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007183
Chris Wrend6297db2012-05-03 16:20:13 -04007184 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 -04007185 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007186
Chris Wren4ed80d52012-05-17 09:30:03 -04007187 // Make sure all rows are gone in case we reuse a view.
7188 for (int rowId : rowIds) {
7189 contentView.setViewVisibility(rowId, View.GONE);
7190 }
7191
Daniel Sandler879c5e02012-04-17 16:46:51 -04007192 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007193 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7194 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007195 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007196 int onlyViewId = 0;
7197 int maxRows = rowIds.length;
7198 if (mBuilder.mActions.size() > 0) {
7199 maxRows--;
7200 }
7201 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007202 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007203 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007204 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007205 contentView.setTextViewText(rowIds[i],
7206 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007207 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007208 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08007209 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07007210 if (first) {
7211 onlyViewId = rowIds[i];
7212 } else {
7213 onlyViewId = 0;
7214 }
Selim Cinek247fa012016-02-18 09:50:48 -08007215 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007216 }
7217 i++;
7218 }
Selim Cinek07c80172016-04-21 16:40:47 -07007219 if (onlyViewId != 0) {
7220 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7221 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7222 R.dimen.notification_text_margin_top);
7223 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7224 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007225
Daniel Sandler879c5e02012-04-17 16:46:51 -04007226 return contentView;
7227 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007228
Julia Reynolds7217dc92018-03-07 12:12:09 -05007229 /**
7230 * @hide
7231 */
7232 @Override
7233 public boolean areNotificationsVisiblyDifferent(Style other) {
7234 if (other == null || getClass() != other.getClass()) {
7235 return true;
7236 }
7237 InboxStyle newS = (InboxStyle) other;
7238 return !Objects.equals(getLines(), newS.getLines());
7239 }
7240
Selim Cinek247fa012016-02-18 09:50:48 -08007241 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007242 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007243 if (first) {
7244 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7245 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7246 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07007247 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007248 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08007249 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007250 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007251 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007252 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007253 }
Dan Sandler842dd772014-05-15 09:36:47 -04007254
7255 /**
7256 * Notification style for media playback notifications.
7257 *
7258 * In the expanded form, {@link Notification#bigContentView}, up to 5
7259 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007260 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007261 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7262 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7263 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007264 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007265 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7266 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007267 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007268 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007269 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007270 * Notifications created with MediaStyle will have their category set to
7271 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7272 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007273 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007274 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7275 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007276 * the System UI can identify this as a notification representing an active media session
7277 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7278 *
Selim Cinek99104832017-01-25 14:47:33 -08007279 * <p>
7280 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7281 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7282 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7283 * <p>
7284 *
Dan Sandler842dd772014-05-15 09:36:47 -04007285 * To use this style with your Notification, feed it to
7286 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7287 * <pre class="prettyprint">
7288 * Notification noti = new Notification.Builder()
7289 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007290 * .setContentTitle(&quot;Track title&quot;)
7291 * .setContentText(&quot;Artist - Album&quot;)
7292 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007293 * .setStyle(<b>new Notification.MediaStyle()</b>
7294 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007295 * .build();
7296 * </pre>
7297 *
7298 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007299 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007300 */
7301 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007302 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007303 static final int MAX_MEDIA_BUTTONS = 5;
7304
7305 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007306 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007307
7308 public MediaStyle() {
7309 }
7310
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007311 /**
7312 * @deprecated use {@code MediaStyle()}.
7313 */
7314 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007315 public MediaStyle(Builder builder) {
7316 setBuilder(builder);
7317 }
7318
7319 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007320 * 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 -04007321 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007322 *
7323 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007324 */
7325 public MediaStyle setShowActionsInCompactView(int...actions) {
7326 mActionsToShowInCompact = actions;
7327 return this;
7328 }
7329
7330 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007331 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7332 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007333 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007334 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007335 mToken = token;
7336 return this;
7337 }
7338
Christoph Studer4600f9b2014-07-22 22:44:43 +02007339 /**
7340 * @hide
7341 */
Dan Sandler842dd772014-05-15 09:36:47 -04007342 @Override
7343 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007344 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007345 if (wip.category == null) {
7346 wip.category = Notification.CATEGORY_TRANSPORT;
7347 }
Dan Sandler842dd772014-05-15 09:36:47 -04007348 return wip;
7349 }
7350
Christoph Studer4600f9b2014-07-22 22:44:43 +02007351 /**
7352 * @hide
7353 */
7354 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007355 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007356 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007357 }
7358
7359 /**
7360 * @hide
7361 */
7362 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007363 public RemoteViews makeBigContentView() {
7364 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007365 }
7366
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007367 /**
7368 * @hide
7369 */
7370 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007371 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007372 RemoteViews expanded = makeMediaBigContentView();
7373 return expanded != null ? expanded : makeMediaContentView();
7374 }
7375
Dan Sandler842dd772014-05-15 09:36:47 -04007376 /** @hide */
7377 @Override
7378 public void addExtras(Bundle extras) {
7379 super.addExtras(extras);
7380
7381 if (mToken != null) {
7382 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7383 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007384 if (mActionsToShowInCompact != null) {
7385 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7386 }
Dan Sandler842dd772014-05-15 09:36:47 -04007387 }
7388
Christoph Studer4600f9b2014-07-22 22:44:43 +02007389 /**
7390 * @hide
7391 */
7392 @Override
7393 protected void restoreFromExtras(Bundle extras) {
7394 super.restoreFromExtras(extras);
7395
7396 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7397 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7398 }
7399 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7400 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7401 }
7402 }
7403
Julia Reynolds7217dc92018-03-07 12:12:09 -05007404 /**
7405 * @hide
7406 */
7407 @Override
7408 public boolean areNotificationsVisiblyDifferent(Style other) {
7409 if (other == null || getClass() != other.getClass()) {
7410 return true;
7411 }
7412 // All fields to compare are on the Notification object
7413 return false;
7414 }
7415
Selim Cinek5bf069a2015-11-10 19:14:27 -05007416 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007417 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007418 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007419 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007420 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007421
7422 // If the action buttons should not be tinted, then just use the default
7423 // notification color. Otherwise, just use the passed-in color.
7424 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7425 ? color
7426 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7427 Notification.COLOR_DEFAULT);
7428
Sunny Goyal5b153922017-09-21 21:00:36 -07007429 button.setDrawableTint(R.id.action0, false, tintColor,
7430 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007431 if (!tombstone) {
7432 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7433 }
7434 button.setContentDescription(R.id.action0, action.title);
7435 return button;
7436 }
7437
7438 private RemoteViews makeMediaContentView() {
7439 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007440 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007441
7442 final int numActions = mBuilder.mActions.size();
7443 final int N = mActionsToShowInCompact == null
7444 ? 0
7445 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7446 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007447 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007448 for (int i = 0; i < N; i++) {
7449 if (i >= numActions) {
7450 throw new IllegalArgumentException(String.format(
7451 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7452 i, numActions - 1));
7453 }
7454
7455 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007456 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007457 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007458 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007459 }
7460 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007461 handleImage(view);
7462 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007463 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007464 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007465 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007466 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007467 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007468 return view;
7469 }
7470
Selim Cinek99104832017-01-25 14:47:33 -08007471 private int getPrimaryHighlightColor() {
7472 return mBuilder.getPrimaryHighlightColor();
7473 }
7474
Dan Sandler842dd772014-05-15 09:36:47 -04007475 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007476 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007477 // Dont add an expanded view if there is no more content to be revealed
7478 int actionsInCompact = mActionsToShowInCompact == null
7479 ? 0
7480 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007481 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007482 return null;
7483 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007484 RemoteViews big = mBuilder.applyStandardTemplate(
7485 R.layout.notification_template_material_big_media,
7486 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007487
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007488 if (actionCount > 0) {
7489 big.removeAllViews(com.android.internal.R.id.media_actions);
7490 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007491 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007492 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007493 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007494 }
7495 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007496 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007497 return big;
7498 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007499
Selim Cinek5bf069a2015-11-10 19:14:27 -05007500 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007501 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007502 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7503 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007504 }
7505 }
7506
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007507 /**
7508 * @hide
7509 */
7510 @Override
7511 protected boolean hasProgress() {
7512 return false;
7513 }
Dan Sandler842dd772014-05-15 09:36:47 -04007514 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007515
Selim Cinek593610c2016-02-16 18:42:57 -08007516 /**
7517 * Notification style for custom views that are decorated by the system
7518 *
7519 * <p>Instead of providing a notification that is completely custom, a developer can set this
7520 * style and still obtain system decorations like the notification header with the expand
7521 * affordance and actions.
7522 *
7523 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7524 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7525 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7526 * corresponding custom views to display.
7527 *
7528 * To use this style with your Notification, feed it to
7529 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7530 * <pre class="prettyprint">
7531 * Notification noti = new Notification.Builder()
7532 * .setSmallIcon(R.drawable.ic_stat_player)
7533 * .setLargeIcon(albumArtBitmap))
7534 * .setCustomContentView(contentView);
7535 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7536 * .build();
7537 * </pre>
7538 */
7539 public static class DecoratedCustomViewStyle extends Style {
7540
7541 public DecoratedCustomViewStyle() {
7542 }
7543
Selim Cinek593610c2016-02-16 18:42:57 -08007544 /**
7545 * @hide
7546 */
7547 public boolean displayCustomViewInline() {
7548 return true;
7549 }
7550
7551 /**
7552 * @hide
7553 */
7554 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007555 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007556 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7557 }
7558
7559 /**
7560 * @hide
7561 */
7562 @Override
7563 public RemoteViews makeBigContentView() {
7564 return makeDecoratedBigContentView();
7565 }
7566
7567 /**
7568 * @hide
7569 */
7570 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007571 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007572 return makeDecoratedHeadsUpContentView();
7573 }
7574
Selim Cinek593610c2016-02-16 18:42:57 -08007575 private RemoteViews makeDecoratedHeadsUpContentView() {
7576 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7577 ? mBuilder.mN.contentView
7578 : mBuilder.mN.headsUpContentView;
7579 if (mBuilder.mActions.size() == 0) {
7580 return makeStandardTemplateWithCustomContent(headsUpContentView);
7581 }
7582 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7583 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007584 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007585 return remoteViews;
7586 }
7587
Selim Cinek593610c2016-02-16 18:42:57 -08007588 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7589 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7590 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007591 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007592 return remoteViews;
7593 }
7594
Selim Cinek593610c2016-02-16 18:42:57 -08007595 private RemoteViews makeDecoratedBigContentView() {
7596 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7597 ? mBuilder.mN.contentView
7598 : mBuilder.mN.bigContentView;
7599 if (mBuilder.mActions.size() == 0) {
7600 return makeStandardTemplateWithCustomContent(bigContentView);
7601 }
7602 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7603 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007604 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007605 return remoteViews;
7606 }
Selim Cinek247fa012016-02-18 09:50:48 -08007607
7608 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7609 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007610 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007611 // Need to clone customContent before adding, because otherwise it can no longer be
7612 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007613 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007614 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7615 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007616 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007617 }
Selim Cinek247fa012016-02-18 09:50:48 -08007618 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007619 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007620 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007621 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007622 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007623 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007624 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05007625
7626 /**
7627 * @hide
7628 */
7629 @Override
7630 public boolean areNotificationsVisiblyDifferent(Style other) {
7631 if (other == null || getClass() != other.getClass()) {
7632 return true;
7633 }
7634 // Comparison done for all custom RemoteViews, independent of style
7635 return false;
7636 }
Selim Cinek593610c2016-02-16 18:42:57 -08007637 }
7638
Selim Cinek03eb3b72016-02-18 10:39:45 -08007639 /**
7640 * Notification style for media custom views that are decorated by the system
7641 *
7642 * <p>Instead of providing a media notification that is completely custom, a developer can set
7643 * this style and still obtain system decorations like the notification header with the expand
7644 * affordance and actions.
7645 *
7646 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7647 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7648 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7649 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007650 * <p>
7651 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7652 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7653 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007654 * To use this style with your Notification, feed it to
7655 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7656 * <pre class="prettyprint">
7657 * Notification noti = new Notification.Builder()
7658 * .setSmallIcon(R.drawable.ic_stat_player)
7659 * .setLargeIcon(albumArtBitmap))
7660 * .setCustomContentView(contentView);
7661 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7662 * .setMediaSession(mySession))
7663 * .build();
7664 * </pre>
7665 *
7666 * @see android.app.Notification.DecoratedCustomViewStyle
7667 * @see android.app.Notification.MediaStyle
7668 */
7669 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7670
7671 public DecoratedMediaCustomViewStyle() {
7672 }
7673
Selim Cinek03eb3b72016-02-18 10:39:45 -08007674 /**
7675 * @hide
7676 */
7677 public boolean displayCustomViewInline() {
7678 return true;
7679 }
7680
7681 /**
7682 * @hide
7683 */
7684 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007685 public RemoteViews makeContentView(boolean increasedHeight) {
7686 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007687 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7688 mBuilder.mN.contentView);
7689 }
7690
7691 /**
7692 * @hide
7693 */
7694 @Override
7695 public RemoteViews makeBigContentView() {
7696 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7697 ? mBuilder.mN.bigContentView
7698 : mBuilder.mN.contentView;
7699 return makeBigContentViewWithCustomContent(customRemoteView);
7700 }
7701
7702 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7703 RemoteViews remoteViews = super.makeBigContentView();
7704 if (remoteViews != null) {
7705 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7706 customRemoteView);
7707 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007708 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007709 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7710 customRemoteView);
7711 } else {
7712 return null;
7713 }
7714 }
7715
7716 /**
7717 * @hide
7718 */
7719 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007720 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007721 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7722 ? mBuilder.mN.headsUpContentView
7723 : mBuilder.mN.contentView;
7724 return makeBigContentViewWithCustomContent(customRemoteView);
7725 }
7726
Julia Reynolds7217dc92018-03-07 12:12:09 -05007727 /**
7728 * @hide
7729 */
7730 @Override
7731 public boolean areNotificationsVisiblyDifferent(Style other) {
7732 if (other == null || getClass() != other.getClass()) {
7733 return true;
7734 }
7735 // Comparison done for all custom RemoteViews, independent of style
7736 return false;
7737 }
7738
Selim Cinek03eb3b72016-02-18 10:39:45 -08007739 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7740 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007741 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007742 // Need to clone customContent before adding, because otherwise it can no longer be
7743 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007744 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007745 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007746 remoteViews.removeAllViews(id);
7747 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007748 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007749 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007750 return remoteViews;
7751 }
7752 }
7753
Selim Cineke7238dd2017-12-14 17:48:32 -08007754 /**
7755 * A Person associated with this Notification.
7756 */
7757 public static final class Person implements Parcelable {
7758 @Nullable private CharSequence mName;
7759 @Nullable private Icon mIcon;
7760 @Nullable private String mUri;
7761 @Nullable private String mKey;
Julia Reynolds18c35df2018-03-15 08:37:10 -04007762 private boolean mBot;
7763 private boolean mImportant;
Selim Cineke7238dd2017-12-14 17:48:32 -08007764
7765 protected Person(Parcel in) {
7766 mName = in.readCharSequence();
7767 if (in.readInt() != 0) {
7768 mIcon = Icon.CREATOR.createFromParcel(in);
7769 }
7770 mUri = in.readString();
7771 mKey = in.readString();
Julia Reynolds18c35df2018-03-15 08:37:10 -04007772 mImportant = in.readBoolean();
7773 mBot = in.readBoolean();
Selim Cineke7238dd2017-12-14 17:48:32 -08007774 }
7775
7776 /**
7777 * Create a new person.
7778 */
7779 public Person() {
7780 }
7781
7782 /**
7783 * Give this person a name.
7784 *
7785 * @param name the name of this person
7786 */
7787 public Person setName(@Nullable CharSequence name) {
7788 this.mName = name;
7789 return this;
7790 }
7791
7792 /**
7793 * Add an icon for this person.
7794 * <br />
7795 * This is currently only used for {@link MessagingStyle} notifications and should not be
7796 * provided otherwise, in order to save memory. The system will prefer this icon over any
7797 * images that are resolved from the URI.
7798 *
7799 * @param icon the icon of the person
7800 */
7801 public Person setIcon(@Nullable Icon icon) {
7802 this.mIcon = icon;
7803 return this;
7804 }
7805
7806 /**
7807 * Set a URI associated with this person.
7808 *
7809 * <P>
7810 * Depending on user preferences, adding a URI to a Person may allow the notification to
7811 * pass through interruption filters, if this notification is of
7812 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7813 * The addition of people may also cause this notification to appear more prominently in
7814 * the user interface.
7815 * </P>
7816 *
7817 * <P>
7818 * The person should be specified by the {@code String} representation of a
7819 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7820 * </P>
7821 *
7822 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7823 * URIs. The path part of these URIs must exist in the contacts database, in the
7824 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7825 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7826 * </P>
7827 *
7828 * @param uri a URI for the person
7829 */
7830 public Person setUri(@Nullable String uri) {
7831 mUri = uri;
7832 return this;
7833 }
7834
7835 /**
7836 * Add a key to this person in order to uniquely identify it.
7837 * This is especially useful if the name doesn't uniquely identify this person or if the
7838 * display name is a short handle of the actual name.
7839 *
7840 * <P>If no key is provided, the name serves as as the key for the purpose of
7841 * identification.</P>
7842 *
7843 * @param key the key that uniquely identifies this person
7844 */
7845 public Person setKey(@Nullable String key) {
7846 mKey = key;
7847 return this;
7848 }
7849
Julia Reynolds18c35df2018-03-15 08:37:10 -04007850 /**
7851 * Sets whether this is an important person. Use this method to denote users who frequently
7852 * interact with the user of this device, when it is not possible to refer to the user
7853 * by {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7854 *
7855 * @param isImportant {@code true} if this is an important person, {@code false} otherwise.
7856 */
7857 public Person setImportant(boolean isImportant) {
7858 mImportant = isImportant;
7859 return this;
7860 }
7861
7862 /**
7863 * Sets whether this person is a machine rather than a human.
7864 *
7865 * @param isBot {@code true} if this person is a machine, {@code false} otherwise.
7866 */
7867 public Person setBot(boolean isBot) {
7868 mBot = isBot;
7869 return this;
7870 }
Selim Cineke7238dd2017-12-14 17:48:32 -08007871
7872 /**
7873 * @return the uri provided for this person or {@code null} if no Uri was provided
7874 */
7875 @Nullable
7876 public String getUri() {
7877 return mUri;
7878 }
7879
7880 /**
7881 * @return the name provided for this person or {@code null} if no name was provided
7882 */
7883 @Nullable
7884 public CharSequence getName() {
7885 return mName;
7886 }
7887
7888 /**
7889 * @return the icon provided for this person or {@code null} if no icon was provided
7890 */
7891 @Nullable
7892 public Icon getIcon() {
7893 return mIcon;
7894 }
7895
7896 /**
7897 * @return the key provided for this person or {@code null} if no key was provided
7898 */
7899 @Nullable
7900 public String getKey() {
7901 return mKey;
7902 }
7903
7904 /**
Julia Reynolds18c35df2018-03-15 08:37:10 -04007905 * @return whether this Person is a machine.
7906 */
7907 public boolean isBot() {
7908 return mBot;
7909 }
7910
7911 /**
7912 * @return whether this Person is important.
7913 */
7914 public boolean isImportant() {
7915 return mImportant;
7916 }
7917
7918 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08007919 * @return the URI associated with this person, or "name:mName" otherwise
7920 * @hide
7921 */
7922 public String resolveToLegacyUri() {
7923 if (mUri != null) {
7924 return mUri;
7925 }
7926 if (mName != null) {
7927 return "name:" + mName;
7928 }
7929 return "";
7930 }
7931
7932 @Override
7933 public int describeContents() {
7934 return 0;
7935 }
7936
7937 @Override
7938 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7939 dest.writeCharSequence(mName);
7940 if (mIcon != null) {
7941 dest.writeInt(1);
7942 mIcon.writeToParcel(dest, 0);
7943 } else {
7944 dest.writeInt(0);
7945 }
7946 dest.writeString(mUri);
7947 dest.writeString(mKey);
Julia Reynolds18c35df2018-03-15 08:37:10 -04007948 dest.writeBoolean(mImportant);
7949 dest.writeBoolean(mBot);
Selim Cineke7238dd2017-12-14 17:48:32 -08007950 }
7951
7952 public static final Creator<Person> CREATOR = new Creator<Person>() {
7953 @Override
7954 public Person createFromParcel(Parcel in) {
7955 return new Person(in);
7956 }
7957
7958 @Override
7959 public Person[] newArray(int size) {
7960 return new Person[size];
7961 }
7962 };
7963 }
7964
Christoph Studer4600f9b2014-07-22 22:44:43 +02007965 // When adding a new Style subclass here, don't forget to update
7966 // Builder.getNotificationStyleClass.
7967
Griff Hazen61a9e862014-05-22 16:05:19 -07007968 /**
7969 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7970 * metadata or change options on a notification builder.
7971 */
7972 public interface Extender {
7973 /**
7974 * Apply this extender to a notification builder.
7975 * @param builder the builder to be modified.
7976 * @return the build object for chaining.
7977 */
7978 public Builder extend(Builder builder);
7979 }
7980
7981 /**
7982 * Helper class to add wearable extensions to notifications.
7983 * <p class="note"> See
7984 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7985 * for Android Wear</a> for more information on how to use this class.
7986 * <p>
7987 * To create a notification with wearable extensions:
7988 * <ol>
7989 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7990 * properties.
7991 * <li>Create a {@link android.app.Notification.WearableExtender}.
7992 * <li>Set wearable-specific properties using the
7993 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7994 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7995 * notification.
7996 * <li>Post the notification to the notification system with the
7997 * {@code NotificationManager.notify(...)} methods.
7998 * </ol>
7999 *
8000 * <pre class="prettyprint">
8001 * Notification notif = new Notification.Builder(mContext)
8002 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8003 * .setContentText(subject)
8004 * .setSmallIcon(R.drawable.new_mail)
8005 * .extend(new Notification.WearableExtender()
8006 * .setContentIcon(R.drawable.new_mail))
8007 * .build();
8008 * NotificationManager notificationManger =
8009 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8010 * notificationManger.notify(0, notif);</pre>
8011 *
8012 * <p>Wearable extensions can be accessed on an existing notification by using the
8013 * {@code WearableExtender(Notification)} constructor,
8014 * and then using the {@code get} methods to access values.
8015 *
8016 * <pre class="prettyprint">
8017 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8018 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008019 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008020 */
8021 public static final class WearableExtender implements Extender {
8022 /**
8023 * Sentinel value for an action index that is unset.
8024 */
8025 public static final int UNSET_ACTION_INDEX = -1;
8026
8027 /**
8028 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8029 * default sizing.
8030 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008031 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008032 * on their content.
8033 */
8034 public static final int SIZE_DEFAULT = 0;
8035
8036 /**
8037 * Size value for use with {@link #setCustomSizePreset} to show this notification
8038 * with an extra small size.
8039 * <p>This value is only applicable for custom display notifications created using
8040 * {@link #setDisplayIntent}.
8041 */
8042 public static final int SIZE_XSMALL = 1;
8043
8044 /**
8045 * Size value for use with {@link #setCustomSizePreset} to show this notification
8046 * with a small size.
8047 * <p>This value is only applicable for custom display notifications created using
8048 * {@link #setDisplayIntent}.
8049 */
8050 public static final int SIZE_SMALL = 2;
8051
8052 /**
8053 * Size value for use with {@link #setCustomSizePreset} to show this notification
8054 * with a medium size.
8055 * <p>This value is only applicable for custom display notifications created using
8056 * {@link #setDisplayIntent}.
8057 */
8058 public static final int SIZE_MEDIUM = 3;
8059
8060 /**
8061 * Size value for use with {@link #setCustomSizePreset} to show this notification
8062 * with a large size.
8063 * <p>This value is only applicable for custom display notifications created using
8064 * {@link #setDisplayIntent}.
8065 */
8066 public static final int SIZE_LARGE = 4;
8067
Griff Hazend5f11f92014-05-27 15:40:09 -07008068 /**
8069 * Size value for use with {@link #setCustomSizePreset} to show this notification
8070 * full screen.
8071 * <p>This value is only applicable for custom display notifications created using
8072 * {@link #setDisplayIntent}.
8073 */
8074 public static final int SIZE_FULL_SCREEN = 5;
8075
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008076 /**
8077 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8078 * short amount of time when this notification is displayed on the screen. This
8079 * is the default value.
8080 */
8081 public static final int SCREEN_TIMEOUT_SHORT = 0;
8082
8083 /**
8084 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8085 * for a longer amount of time when this notification is displayed on the screen.
8086 */
8087 public static final int SCREEN_TIMEOUT_LONG = -1;
8088
Griff Hazen61a9e862014-05-22 16:05:19 -07008089 /** Notification extra which contains wearable extensions */
8090 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8091
Pete Gastaf6781d2014-10-07 15:17:05 -04008092 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008093 private static final String KEY_ACTIONS = "actions";
8094 private static final String KEY_FLAGS = "flags";
8095 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8096 private static final String KEY_PAGES = "pages";
8097 private static final String KEY_BACKGROUND = "background";
8098 private static final String KEY_CONTENT_ICON = "contentIcon";
8099 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8100 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8101 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8102 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8103 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008104 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008105 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008106 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008107
8108 // Flags bitwise-ored to mFlags
8109 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8110 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8111 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8112 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008113 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008114 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008115 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008116
8117 // Default value for flags integer
8118 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8119
8120 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8121 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8122
8123 private ArrayList<Action> mActions = new ArrayList<Action>();
8124 private int mFlags = DEFAULT_FLAGS;
8125 private PendingIntent mDisplayIntent;
8126 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8127 private Bitmap mBackground;
8128 private int mContentIcon;
8129 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8130 private int mContentActionIndex = UNSET_ACTION_INDEX;
8131 private int mCustomSizePreset = SIZE_DEFAULT;
8132 private int mCustomContentHeight;
8133 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008134 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008135 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008136 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008137
8138 /**
8139 * Create a {@link android.app.Notification.WearableExtender} with default
8140 * options.
8141 */
8142 public WearableExtender() {
8143 }
8144
8145 public WearableExtender(Notification notif) {
8146 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8147 if (wearableBundle != null) {
8148 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8149 if (actions != null) {
8150 mActions.addAll(actions);
8151 }
8152
8153 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8154 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8155
8156 Notification[] pages = getNotificationArrayFromBundle(
8157 wearableBundle, KEY_PAGES);
8158 if (pages != null) {
8159 Collections.addAll(mPages, pages);
8160 }
8161
8162 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8163 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8164 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8165 DEFAULT_CONTENT_ICON_GRAVITY);
8166 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8167 UNSET_ACTION_INDEX);
8168 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8169 SIZE_DEFAULT);
8170 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8171 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008172 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008173 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008174 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008175 }
8176 }
8177
8178 /**
8179 * Apply wearable extensions to a notification that is being built. This is typically
8180 * called by the {@link android.app.Notification.Builder#extend} method of
8181 * {@link android.app.Notification.Builder}.
8182 */
8183 @Override
8184 public Notification.Builder extend(Notification.Builder builder) {
8185 Bundle wearableBundle = new Bundle();
8186
8187 if (!mActions.isEmpty()) {
8188 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8189 }
8190 if (mFlags != DEFAULT_FLAGS) {
8191 wearableBundle.putInt(KEY_FLAGS, mFlags);
8192 }
8193 if (mDisplayIntent != null) {
8194 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8195 }
8196 if (!mPages.isEmpty()) {
8197 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8198 new Notification[mPages.size()]));
8199 }
8200 if (mBackground != null) {
8201 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8202 }
8203 if (mContentIcon != 0) {
8204 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8205 }
8206 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8207 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8208 }
8209 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8210 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8211 mContentActionIndex);
8212 }
8213 if (mCustomSizePreset != SIZE_DEFAULT) {
8214 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8215 }
8216 if (mCustomContentHeight != 0) {
8217 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8218 }
8219 if (mGravity != DEFAULT_GRAVITY) {
8220 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8221 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008222 if (mHintScreenTimeout != 0) {
8223 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8224 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008225 if (mDismissalId != null) {
8226 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8227 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008228 if (mBridgeTag != null) {
8229 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8230 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008231
8232 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8233 return builder;
8234 }
8235
8236 @Override
8237 public WearableExtender clone() {
8238 WearableExtender that = new WearableExtender();
8239 that.mActions = new ArrayList<Action>(this.mActions);
8240 that.mFlags = this.mFlags;
8241 that.mDisplayIntent = this.mDisplayIntent;
8242 that.mPages = new ArrayList<Notification>(this.mPages);
8243 that.mBackground = this.mBackground;
8244 that.mContentIcon = this.mContentIcon;
8245 that.mContentIconGravity = this.mContentIconGravity;
8246 that.mContentActionIndex = this.mContentActionIndex;
8247 that.mCustomSizePreset = this.mCustomSizePreset;
8248 that.mCustomContentHeight = this.mCustomContentHeight;
8249 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008250 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008251 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008252 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008253 return that;
8254 }
8255
8256 /**
8257 * Add a wearable action to this notification.
8258 *
8259 * <p>When wearable actions are added using this method, the set of actions that
8260 * show on a wearable device splits from devices that only show actions added
8261 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8262 * of which actions display on different devices.
8263 *
8264 * @param action the action to add to this notification
8265 * @return this object for method chaining
8266 * @see android.app.Notification.Action
8267 */
8268 public WearableExtender addAction(Action action) {
8269 mActions.add(action);
8270 return this;
8271 }
8272
8273 /**
8274 * Adds wearable actions to this notification.
8275 *
8276 * <p>When wearable actions are added using this method, the set of actions that
8277 * show on a wearable device splits from devices that only show actions added
8278 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8279 * of which actions display on different devices.
8280 *
8281 * @param actions the actions to add to this notification
8282 * @return this object for method chaining
8283 * @see android.app.Notification.Action
8284 */
8285 public WearableExtender addActions(List<Action> actions) {
8286 mActions.addAll(actions);
8287 return this;
8288 }
8289
8290 /**
8291 * Clear all wearable actions present on this builder.
8292 * @return this object for method chaining.
8293 * @see #addAction
8294 */
8295 public WearableExtender clearActions() {
8296 mActions.clear();
8297 return this;
8298 }
8299
8300 /**
8301 * Get the wearable actions present on this notification.
8302 */
8303 public List<Action> getActions() {
8304 return mActions;
8305 }
8306
8307 /**
8308 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008309 * this notification. The {@link PendingIntent} provided should be for an activity.
8310 *
8311 * <pre class="prettyprint">
8312 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8313 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8314 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8315 * Notification notif = new Notification.Builder(context)
8316 * .extend(new Notification.WearableExtender()
8317 * .setDisplayIntent(displayPendingIntent)
8318 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8319 * .build();</pre>
8320 *
8321 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008322 * should have an empty task affinity. It is also recommended to use the device
8323 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008324 *
8325 * <p>Example AndroidManifest.xml entry:
8326 * <pre class="prettyprint">
8327 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8328 * android:exported=&quot;true&quot;
8329 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008330 * android:taskAffinity=&quot;&quot;
8331 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008332 *
8333 * @param intent the {@link PendingIntent} for an activity
8334 * @return this object for method chaining
8335 * @see android.app.Notification.WearableExtender#getDisplayIntent
8336 */
8337 public WearableExtender setDisplayIntent(PendingIntent intent) {
8338 mDisplayIntent = intent;
8339 return this;
8340 }
8341
8342 /**
8343 * Get the intent to launch inside of an activity view when displaying this
8344 * notification. This {@code PendingIntent} should be for an activity.
8345 */
8346 public PendingIntent getDisplayIntent() {
8347 return mDisplayIntent;
8348 }
8349
8350 /**
8351 * Add an additional page of content to display with this notification. The current
8352 * notification forms the first page, and pages added using this function form
8353 * subsequent pages. This field can be used to separate a notification into multiple
8354 * sections.
8355 *
8356 * @param page the notification to add as another page
8357 * @return this object for method chaining
8358 * @see android.app.Notification.WearableExtender#getPages
8359 */
8360 public WearableExtender addPage(Notification page) {
8361 mPages.add(page);
8362 return this;
8363 }
8364
8365 /**
8366 * Add additional pages of content to display with this notification. The current
8367 * notification forms the first page, and pages added using this function form
8368 * subsequent pages. This field can be used to separate a notification into multiple
8369 * sections.
8370 *
8371 * @param pages a list of notifications
8372 * @return this object for method chaining
8373 * @see android.app.Notification.WearableExtender#getPages
8374 */
8375 public WearableExtender addPages(List<Notification> pages) {
8376 mPages.addAll(pages);
8377 return this;
8378 }
8379
8380 /**
8381 * Clear all additional pages present on this builder.
8382 * @return this object for method chaining.
8383 * @see #addPage
8384 */
8385 public WearableExtender clearPages() {
8386 mPages.clear();
8387 return this;
8388 }
8389
8390 /**
8391 * Get the array of additional pages of content for displaying this notification. The
8392 * current notification forms the first page, and elements within this array form
8393 * subsequent pages. This field can be used to separate a notification into multiple
8394 * sections.
8395 * @return the pages for this notification
8396 */
8397 public List<Notification> getPages() {
8398 return mPages;
8399 }
8400
8401 /**
8402 * Set a background image to be displayed behind the notification content.
8403 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8404 * will work with any notification style.
8405 *
8406 * @param background the background bitmap
8407 * @return this object for method chaining
8408 * @see android.app.Notification.WearableExtender#getBackground
8409 */
8410 public WearableExtender setBackground(Bitmap background) {
8411 mBackground = background;
8412 return this;
8413 }
8414
8415 /**
8416 * Get a background image to be displayed behind the notification content.
8417 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8418 * will work with any notification style.
8419 *
8420 * @return the background image
8421 * @see android.app.Notification.WearableExtender#setBackground
8422 */
8423 public Bitmap getBackground() {
8424 return mBackground;
8425 }
8426
8427 /**
8428 * Set an icon that goes with the content of this notification.
8429 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008430 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008431 public WearableExtender setContentIcon(int icon) {
8432 mContentIcon = icon;
8433 return this;
8434 }
8435
8436 /**
8437 * Get an icon that goes with the content of this notification.
8438 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008439 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008440 public int getContentIcon() {
8441 return mContentIcon;
8442 }
8443
8444 /**
8445 * Set the gravity that the content icon should have within the notification display.
8446 * Supported values include {@link android.view.Gravity#START} and
8447 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8448 * @see #setContentIcon
8449 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008450 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008451 public WearableExtender setContentIconGravity(int contentIconGravity) {
8452 mContentIconGravity = contentIconGravity;
8453 return this;
8454 }
8455
8456 /**
8457 * Get the gravity that the content icon should have within the notification display.
8458 * Supported values include {@link android.view.Gravity#START} and
8459 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8460 * @see #getContentIcon
8461 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008462 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008463 public int getContentIconGravity() {
8464 return mContentIconGravity;
8465 }
8466
8467 /**
8468 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008469 * this notification. This action will no longer display separately from the
8470 * notification's content.
8471 *
Griff Hazenca48d352014-05-28 22:37:13 -07008472 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008473 * set, although the list of available actions comes from the main notification and not
8474 * from the child page's notification.
8475 *
8476 * @param actionIndex The index of the action to hoist onto the current notification page.
8477 * If wearable actions were added to the main notification, this index
8478 * will apply to that list, otherwise it will apply to the regular
8479 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008480 */
8481 public WearableExtender setContentAction(int actionIndex) {
8482 mContentActionIndex = actionIndex;
8483 return this;
8484 }
8485
8486 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008487 * Get the index of the notification action, if any, that was specified as being clickable
8488 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008489 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008490 *
Griff Hazenca48d352014-05-28 22:37:13 -07008491 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008492 * set, although the list of available actions comes from the main notification and not
8493 * from the child page's notification.
8494 *
8495 * <p>If wearable specific actions were added to the main notification, this index will
8496 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008497 *
8498 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008499 */
8500 public int getContentAction() {
8501 return mContentActionIndex;
8502 }
8503
8504 /**
8505 * Set the gravity that this notification should have within the available viewport space.
8506 * Supported values include {@link android.view.Gravity#TOP},
8507 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8508 * The default value is {@link android.view.Gravity#BOTTOM}.
8509 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008510 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008511 public WearableExtender setGravity(int gravity) {
8512 mGravity = gravity;
8513 return this;
8514 }
8515
8516 /**
8517 * Get the gravity that this notification should have within the available viewport space.
8518 * Supported values include {@link android.view.Gravity#TOP},
8519 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8520 * The default value is {@link android.view.Gravity#BOTTOM}.
8521 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008522 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008523 public int getGravity() {
8524 return mGravity;
8525 }
8526
8527 /**
8528 * Set the custom size preset for the display of this notification out of the available
8529 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8530 * {@link #SIZE_LARGE}.
8531 * <p>Some custom size presets are only applicable for custom display notifications created
8532 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8533 * documentation for the preset in question. See also
8534 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8535 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008536 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008537 public WearableExtender setCustomSizePreset(int sizePreset) {
8538 mCustomSizePreset = sizePreset;
8539 return this;
8540 }
8541
8542 /**
8543 * Get the custom size preset for the display of this notification out of the available
8544 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8545 * {@link #SIZE_LARGE}.
8546 * <p>Some custom size presets are only applicable for custom display notifications created
8547 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8548 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8549 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008550 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008551 public int getCustomSizePreset() {
8552 return mCustomSizePreset;
8553 }
8554
8555 /**
8556 * Set the custom height in pixels for the display of this notification's content.
8557 * <p>This option is only available for custom display notifications created
8558 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8559 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8560 * {@link #getCustomContentHeight}.
8561 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008562 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008563 public WearableExtender setCustomContentHeight(int height) {
8564 mCustomContentHeight = height;
8565 return this;
8566 }
8567
8568 /**
8569 * Get the custom height in pixels for the display of this notification's content.
8570 * <p>This option is only available for custom display notifications created
8571 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8572 * {@link #setCustomContentHeight}.
8573 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008574 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008575 public int getCustomContentHeight() {
8576 return mCustomContentHeight;
8577 }
8578
8579 /**
8580 * Set whether the scrolling position for the contents of this notification should start
8581 * at the bottom of the contents instead of the top when the contents are too long to
8582 * display within the screen. Default is false (start scroll at the top).
8583 */
8584 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8585 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8586 return this;
8587 }
8588
8589 /**
8590 * Get whether the scrolling position for the contents of this notification should start
8591 * at the bottom of the contents instead of the top when the contents are too long to
8592 * display within the screen. Default is false (start scroll at the top).
8593 */
8594 public boolean getStartScrollBottom() {
8595 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8596 }
8597
8598 /**
8599 * Set whether the content intent is available when the wearable device is not connected
8600 * to a companion device. The user can still trigger this intent when the wearable device
8601 * is offline, but a visual hint will indicate that the content intent may not be available.
8602 * Defaults to true.
8603 */
8604 public WearableExtender setContentIntentAvailableOffline(
8605 boolean contentIntentAvailableOffline) {
8606 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8607 return this;
8608 }
8609
8610 /**
8611 * Get whether the content intent is available when the wearable device is not connected
8612 * to a companion device. The user can still trigger this intent when the wearable device
8613 * is offline, but a visual hint will indicate that the content intent may not be available.
8614 * Defaults to true.
8615 */
8616 public boolean getContentIntentAvailableOffline() {
8617 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8618 }
8619
8620 /**
8621 * Set a hint that this notification's icon should not be displayed.
8622 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8623 * @return this object for method chaining
8624 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008625 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008626 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8627 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8628 return this;
8629 }
8630
8631 /**
8632 * Get a hint that this notification's icon should not be displayed.
8633 * @return {@code true} if this icon should not be displayed, false otherwise.
8634 * The default value is {@code false} if this was never set.
8635 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008636 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008637 public boolean getHintHideIcon() {
8638 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8639 }
8640
8641 /**
8642 * Set a visual hint that only the background image of this notification should be
8643 * displayed, and other semantic content should be hidden. This hint is only applicable
8644 * to sub-pages added using {@link #addPage}.
8645 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008646 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008647 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8648 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8649 return this;
8650 }
8651
8652 /**
8653 * Get a visual hint that only the background image of this notification should be
8654 * displayed, and other semantic content should be hidden. This hint is only applicable
8655 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8656 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008657 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008658 public boolean getHintShowBackgroundOnly() {
8659 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8660 }
8661
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008662 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008663 * Set a hint that this notification's background should not be clipped if possible,
8664 * and should instead be resized to fully display on the screen, retaining the aspect
8665 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008666 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8667 * @return this object for method chaining
8668 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008669 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008670 public WearableExtender setHintAvoidBackgroundClipping(
8671 boolean hintAvoidBackgroundClipping) {
8672 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8673 return this;
8674 }
8675
8676 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008677 * Get a hint that this notification's background should not be clipped if possible,
8678 * and should instead be resized to fully display on the screen, retaining the aspect
8679 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008680 * @return {@code true} if it's ok if the background is clipped on the screen, false
8681 * otherwise. The default value is {@code false} if this was never set.
8682 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008683 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008684 public boolean getHintAvoidBackgroundClipping() {
8685 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8686 }
8687
8688 /**
8689 * Set a hint that the screen should remain on for at least this duration when
8690 * this notification is displayed on the screen.
8691 * @param timeout The requested screen timeout in milliseconds. Can also be either
8692 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8693 * @return this object for method chaining
8694 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008695 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008696 public WearableExtender setHintScreenTimeout(int timeout) {
8697 mHintScreenTimeout = timeout;
8698 return this;
8699 }
8700
8701 /**
8702 * Get the duration, in milliseconds, that the screen should remain on for
8703 * when this notification is displayed.
8704 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8705 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8706 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008707 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008708 public int getHintScreenTimeout() {
8709 return mHintScreenTimeout;
8710 }
8711
Alex Hills9ab3a232016-04-05 14:54:56 -04008712 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008713 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8714 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8715 * qr codes, as well as other simple black-and-white tickets.
8716 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8717 * @return this object for method chaining
8718 */
8719 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8720 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8721 return this;
8722 }
8723
8724 /**
8725 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8726 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8727 * qr codes, as well as other simple black-and-white tickets.
8728 * @return {@code true} if it should be displayed in ambient, false otherwise
8729 * otherwise. The default value is {@code false} if this was never set.
8730 */
8731 public boolean getHintAmbientBigPicture() {
8732 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8733 }
8734
8735 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008736 * Set a hint that this notification's content intent will launch an {@link Activity}
8737 * directly, telling the platform that it can generate the appropriate transitions.
8738 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8739 * an activity and transitions should be generated, false otherwise.
8740 * @return this object for method chaining
8741 */
8742 public WearableExtender setHintContentIntentLaunchesActivity(
8743 boolean hintContentIntentLaunchesActivity) {
8744 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8745 return this;
8746 }
8747
8748 /**
8749 * Get a hint that this notification's content intent will launch an {@link Activity}
8750 * directly, telling the platform that it can generate the appropriate transitions
8751 * @return {@code true} if the content intent will launch an activity and transitions should
8752 * be generated, false otherwise. The default value is {@code false} if this was never set.
8753 */
8754 public boolean getHintContentIntentLaunchesActivity() {
8755 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8756 }
8757
Nadia Benbernou948627e2016-04-14 14:41:08 -04008758 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008759 * Sets the dismissal id for this notification. If a notification is posted with a
8760 * dismissal id, then when that notification is canceled, notifications on other wearables
8761 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008762 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008763 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008764 * @param dismissalId the dismissal id of the notification.
8765 * @return this object for method chaining
8766 */
8767 public WearableExtender setDismissalId(String dismissalId) {
8768 mDismissalId = dismissalId;
8769 return this;
8770 }
8771
8772 /**
8773 * Returns the dismissal id of the notification.
8774 * @return the dismissal id of the notification or null if it has not been set.
8775 */
8776 public String getDismissalId() {
8777 return mDismissalId;
8778 }
8779
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008780 /**
8781 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8782 * posted from a phone to provide finer-grained control on what notifications are bridged
8783 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8784 * Features to Notifications</a> for more information.
8785 * @param bridgeTag the bridge tag of the notification.
8786 * @return this object for method chaining
8787 */
8788 public WearableExtender setBridgeTag(String bridgeTag) {
8789 mBridgeTag = bridgeTag;
8790 return this;
8791 }
8792
8793 /**
8794 * Returns the bridge tag of the notification.
8795 * @return the bridge tag or null if not present.
8796 */
8797 public String getBridgeTag() {
8798 return mBridgeTag;
8799 }
8800
Griff Hazen61a9e862014-05-22 16:05:19 -07008801 private void setFlag(int mask, boolean value) {
8802 if (value) {
8803 mFlags |= mask;
8804 } else {
8805 mFlags &= ~mask;
8806 }
8807 }
8808 }
8809
8810 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008811 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8812 * with car extensions:
8813 *
8814 * <ol>
8815 * <li>Create an {@link Notification.Builder}, setting any desired
8816 * properties.
8817 * <li>Create a {@link CarExtender}.
8818 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8819 * {@link CarExtender}.
8820 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8821 * to apply the extensions to a notification.
8822 * </ol>
8823 *
8824 * <pre class="prettyprint">
8825 * Notification notification = new Notification.Builder(context)
8826 * ...
8827 * .extend(new CarExtender()
8828 * .set*(...))
8829 * .build();
8830 * </pre>
8831 *
8832 * <p>Car extensions can be accessed on an existing notification by using the
8833 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8834 * to access values.
8835 */
8836 public static final class CarExtender implements Extender {
8837 private static final String TAG = "CarExtender";
8838
8839 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8840 private static final String EXTRA_LARGE_ICON = "large_icon";
8841 private static final String EXTRA_CONVERSATION = "car_conversation";
8842 private static final String EXTRA_COLOR = "app_color";
8843
8844 private Bitmap mLargeIcon;
8845 private UnreadConversation mUnreadConversation;
8846 private int mColor = Notification.COLOR_DEFAULT;
8847
8848 /**
8849 * Create a {@link CarExtender} with default options.
8850 */
8851 public CarExtender() {
8852 }
8853
8854 /**
8855 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8856 *
8857 * @param notif The notification from which to copy options.
8858 */
8859 public CarExtender(Notification notif) {
8860 Bundle carBundle = notif.extras == null ?
8861 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8862 if (carBundle != null) {
8863 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8864 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8865
8866 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8867 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8868 }
8869 }
8870
8871 /**
8872 * Apply car extensions to a notification that is being built. This is typically called by
8873 * the {@link Notification.Builder#extend(Notification.Extender)}
8874 * method of {@link Notification.Builder}.
8875 */
8876 @Override
8877 public Notification.Builder extend(Notification.Builder builder) {
8878 Bundle carExtensions = new Bundle();
8879
8880 if (mLargeIcon != null) {
8881 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8882 }
8883 if (mColor != Notification.COLOR_DEFAULT) {
8884 carExtensions.putInt(EXTRA_COLOR, mColor);
8885 }
8886
8887 if (mUnreadConversation != null) {
8888 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8889 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8890 }
8891
8892 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8893 return builder;
8894 }
8895
8896 /**
8897 * Sets the accent color to use when Android Auto presents the notification.
8898 *
8899 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8900 * to accent the displayed notification. However, not all colors are acceptable in an
8901 * automotive setting. This method can be used to override the color provided in the
8902 * notification in such a situation.
8903 */
Tor Norbye80756e32015-03-02 09:39:27 -08008904 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008905 mColor = color;
8906 return this;
8907 }
8908
8909 /**
8910 * Gets the accent color.
8911 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008912 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008913 */
Tor Norbye80756e32015-03-02 09:39:27 -08008914 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008915 public int getColor() {
8916 return mColor;
8917 }
8918
8919 /**
8920 * Sets the large icon of the car notification.
8921 *
8922 * If no large icon is set in the extender, Android Auto will display the icon
8923 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8924 *
8925 * @param largeIcon The large icon to use in the car notification.
8926 * @return This object for method chaining.
8927 */
8928 public CarExtender setLargeIcon(Bitmap largeIcon) {
8929 mLargeIcon = largeIcon;
8930 return this;
8931 }
8932
8933 /**
8934 * Gets the large icon used in this car notification, or null if no icon has been set.
8935 *
8936 * @return The large icon for the car notification.
8937 * @see CarExtender#setLargeIcon
8938 */
8939 public Bitmap getLargeIcon() {
8940 return mLargeIcon;
8941 }
8942
8943 /**
8944 * Sets the unread conversation in a message notification.
8945 *
8946 * @param unreadConversation The unread part of the conversation this notification conveys.
8947 * @return This object for method chaining.
8948 */
8949 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8950 mUnreadConversation = unreadConversation;
8951 return this;
8952 }
8953
8954 /**
8955 * Returns the unread conversation conveyed by this notification.
8956 * @see #setUnreadConversation(UnreadConversation)
8957 */
8958 public UnreadConversation getUnreadConversation() {
8959 return mUnreadConversation;
8960 }
8961
8962 /**
8963 * A class which holds the unread messages from a conversation.
8964 */
8965 public static class UnreadConversation {
8966 private static final String KEY_AUTHOR = "author";
8967 private static final String KEY_TEXT = "text";
8968 private static final String KEY_MESSAGES = "messages";
8969 private static final String KEY_REMOTE_INPUT = "remote_input";
8970 private static final String KEY_ON_REPLY = "on_reply";
8971 private static final String KEY_ON_READ = "on_read";
8972 private static final String KEY_PARTICIPANTS = "participants";
8973 private static final String KEY_TIMESTAMP = "timestamp";
8974
8975 private final String[] mMessages;
8976 private final RemoteInput mRemoteInput;
8977 private final PendingIntent mReplyPendingIntent;
8978 private final PendingIntent mReadPendingIntent;
8979 private final String[] mParticipants;
8980 private final long mLatestTimestamp;
8981
8982 UnreadConversation(String[] messages, RemoteInput remoteInput,
8983 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8984 String[] participants, long latestTimestamp) {
8985 mMessages = messages;
8986 mRemoteInput = remoteInput;
8987 mReadPendingIntent = readPendingIntent;
8988 mReplyPendingIntent = replyPendingIntent;
8989 mParticipants = participants;
8990 mLatestTimestamp = latestTimestamp;
8991 }
8992
8993 /**
8994 * Gets the list of messages conveyed by this notification.
8995 */
8996 public String[] getMessages() {
8997 return mMessages;
8998 }
8999
9000 /**
9001 * Gets the remote input that will be used to convey the response to a message list, or
9002 * null if no such remote input exists.
9003 */
9004 public RemoteInput getRemoteInput() {
9005 return mRemoteInput;
9006 }
9007
9008 /**
9009 * Gets the pending intent that will be triggered when the user replies to this
9010 * notification.
9011 */
9012 public PendingIntent getReplyPendingIntent() {
9013 return mReplyPendingIntent;
9014 }
9015
9016 /**
9017 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9018 * in this object's message list.
9019 */
9020 public PendingIntent getReadPendingIntent() {
9021 return mReadPendingIntent;
9022 }
9023
9024 /**
9025 * Gets the participants in the conversation.
9026 */
9027 public String[] getParticipants() {
9028 return mParticipants;
9029 }
9030
9031 /**
9032 * Gets the firs participant in the conversation.
9033 */
9034 public String getParticipant() {
9035 return mParticipants.length > 0 ? mParticipants[0] : null;
9036 }
9037
9038 /**
9039 * Gets the timestamp of the conversation.
9040 */
9041 public long getLatestTimestamp() {
9042 return mLatestTimestamp;
9043 }
9044
9045 Bundle getBundleForUnreadConversation() {
9046 Bundle b = new Bundle();
9047 String author = null;
9048 if (mParticipants != null && mParticipants.length > 1) {
9049 author = mParticipants[0];
9050 }
9051 Parcelable[] messages = new Parcelable[mMessages.length];
9052 for (int i = 0; i < messages.length; i++) {
9053 Bundle m = new Bundle();
9054 m.putString(KEY_TEXT, mMessages[i]);
9055 m.putString(KEY_AUTHOR, author);
9056 messages[i] = m;
9057 }
9058 b.putParcelableArray(KEY_MESSAGES, messages);
9059 if (mRemoteInput != null) {
9060 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9061 }
9062 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9063 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9064 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9065 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9066 return b;
9067 }
9068
9069 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9070 if (b == null) {
9071 return null;
9072 }
9073 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9074 String[] messages = null;
9075 if (parcelableMessages != null) {
9076 String[] tmp = new String[parcelableMessages.length];
9077 boolean success = true;
9078 for (int i = 0; i < tmp.length; i++) {
9079 if (!(parcelableMessages[i] instanceof Bundle)) {
9080 success = false;
9081 break;
9082 }
9083 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9084 if (tmp[i] == null) {
9085 success = false;
9086 break;
9087 }
9088 }
9089 if (success) {
9090 messages = tmp;
9091 } else {
9092 return null;
9093 }
9094 }
9095
9096 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9097 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9098
9099 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9100
9101 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9102 if (participants == null || participants.length != 1) {
9103 return null;
9104 }
9105
9106 return new UnreadConversation(messages,
9107 remoteInput,
9108 onReply,
9109 onRead,
9110 participants, b.getLong(KEY_TIMESTAMP));
9111 }
9112 };
9113
9114 /**
9115 * Builder class for {@link CarExtender.UnreadConversation} objects.
9116 */
9117 public static class Builder {
9118 private final List<String> mMessages = new ArrayList<String>();
9119 private final String mParticipant;
9120 private RemoteInput mRemoteInput;
9121 private PendingIntent mReadPendingIntent;
9122 private PendingIntent mReplyPendingIntent;
9123 private long mLatestTimestamp;
9124
9125 /**
9126 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9127 *
9128 * @param name The name of the other participant in the conversation.
9129 */
9130 public Builder(String name) {
9131 mParticipant = name;
9132 }
9133
9134 /**
9135 * Appends a new unread message to the list of messages for this conversation.
9136 *
9137 * The messages should be added from oldest to newest.
9138 *
9139 * @param message The text of the new unread message.
9140 * @return This object for method chaining.
9141 */
9142 public Builder addMessage(String message) {
9143 mMessages.add(message);
9144 return this;
9145 }
9146
9147 /**
9148 * Sets the pending intent and remote input which will convey the reply to this
9149 * notification.
9150 *
9151 * @param pendingIntent The pending intent which will be triggered on a reply.
9152 * @param remoteInput The remote input parcelable which will carry the reply.
9153 * @return This object for method chaining.
9154 *
9155 * @see CarExtender.UnreadConversation#getRemoteInput
9156 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9157 */
9158 public Builder setReplyAction(
9159 PendingIntent pendingIntent, RemoteInput remoteInput) {
9160 mRemoteInput = remoteInput;
9161 mReplyPendingIntent = pendingIntent;
9162
9163 return this;
9164 }
9165
9166 /**
9167 * Sets the pending intent that will be sent once the messages in this notification
9168 * are read.
9169 *
9170 * @param pendingIntent The pending intent to use.
9171 * @return This object for method chaining.
9172 */
9173 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9174 mReadPendingIntent = pendingIntent;
9175 return this;
9176 }
9177
9178 /**
9179 * Sets the timestamp of the most recent message in an unread conversation.
9180 *
9181 * If a messaging notification has been posted by your application and has not
9182 * yet been cancelled, posting a later notification with the same id and tag
9183 * but without a newer timestamp may result in Android Auto not displaying a
9184 * heads up notification for the later notification.
9185 *
9186 * @param timestamp The timestamp of the most recent message in the conversation.
9187 * @return This object for method chaining.
9188 */
9189 public Builder setLatestTimestamp(long timestamp) {
9190 mLatestTimestamp = timestamp;
9191 return this;
9192 }
9193
9194 /**
9195 * Builds a new unread conversation object.
9196 *
9197 * @return The new unread conversation object.
9198 */
9199 public UnreadConversation build() {
9200 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9201 String[] participants = { mParticipant };
9202 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9203 mReadPendingIntent, participants, mLatestTimestamp);
9204 }
9205 }
9206 }
9207
9208 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009209 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9210 * with a TV extension:
9211 *
9212 * <ol>
9213 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9214 * <li>Create a {@link TvExtender}.
9215 * <li>Set TV-specific properties using the {@code set} methods of
9216 * {@link TvExtender}.
9217 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9218 * to apply the extension to a notification.
9219 * </ol>
9220 *
9221 * <pre class="prettyprint">
9222 * Notification notification = new Notification.Builder(context)
9223 * ...
9224 * .extend(new TvExtender()
9225 * .set*(...))
9226 * .build();
9227 * </pre>
9228 *
9229 * <p>TV extensions can be accessed on an existing notification by using the
9230 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9231 * to access values.
9232 *
9233 * @hide
9234 */
9235 @SystemApi
9236 public static final class TvExtender implements Extender {
9237 private static final String TAG = "TvExtender";
9238
9239 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9240 private static final String EXTRA_FLAGS = "flags";
9241 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9242 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009243 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009244 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009245
9246 // Flags bitwise-ored to mFlags
9247 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9248
9249 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009250 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009251 private PendingIntent mContentIntent;
9252 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009253 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009254
9255 /**
9256 * Create a {@link TvExtender} with default options.
9257 */
9258 public TvExtender() {
9259 mFlags = FLAG_AVAILABLE_ON_TV;
9260 }
9261
9262 /**
9263 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9264 *
9265 * @param notif The notification from which to copy options.
9266 */
9267 public TvExtender(Notification notif) {
9268 Bundle bundle = notif.extras == null ?
9269 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9270 if (bundle != null) {
9271 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009272 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009273 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009274 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9275 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9276 }
9277 }
9278
9279 /**
9280 * Apply a TV extension to a notification that is being built. This is typically called by
9281 * the {@link Notification.Builder#extend(Notification.Extender)}
9282 * method of {@link Notification.Builder}.
9283 */
9284 @Override
9285 public Notification.Builder extend(Notification.Builder builder) {
9286 Bundle bundle = new Bundle();
9287
9288 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009289 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009290 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009291 if (mContentIntent != null) {
9292 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9293 }
9294
9295 if (mDeleteIntent != null) {
9296 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9297 }
9298
9299 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9300 return builder;
9301 }
9302
9303 /**
9304 * Returns true if this notification should be shown on TV. This method return true
9305 * if the notification was extended with a TvExtender.
9306 */
9307 public boolean isAvailableOnTv() {
9308 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9309 }
9310
9311 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009312 * Specifies the channel the notification should be delivered on when shown on TV.
9313 * It can be different from the channel that the notification is delivered to when
9314 * posting on a non-TV device.
9315 */
9316 public TvExtender setChannel(String channelId) {
9317 mChannelId = channelId;
9318 return this;
9319 }
9320
9321 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009322 * Specifies the channel the notification should be delivered on when shown on TV.
9323 * It can be different from the channel that the notification is delivered to when
9324 * posting on a non-TV device.
9325 */
9326 public TvExtender setChannelId(String channelId) {
9327 mChannelId = channelId;
9328 return this;
9329 }
9330
Jeff Sharkey000ce802017-04-29 13:13:27 -06009331 /** @removed */
9332 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009333 public String getChannel() {
9334 return mChannelId;
9335 }
9336
9337 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009338 * Returns the id of the channel this notification posts to on TV.
9339 */
9340 public String getChannelId() {
9341 return mChannelId;
9342 }
9343
9344 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009345 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9346 * If provided, it is used instead of the content intent specified
9347 * at the level of Notification.
9348 */
9349 public TvExtender setContentIntent(PendingIntent intent) {
9350 mContentIntent = intent;
9351 return this;
9352 }
9353
9354 /**
9355 * Returns the TV-specific content intent. If this method returns null, the
9356 * main content intent on the notification should be used.
9357 *
9358 * @see {@link Notification#contentIntent}
9359 */
9360 public PendingIntent getContentIntent() {
9361 return mContentIntent;
9362 }
9363
9364 /**
9365 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9366 * by the user on TV. If provided, it is used instead of the delete intent specified
9367 * at the level of Notification.
9368 */
9369 public TvExtender setDeleteIntent(PendingIntent intent) {
9370 mDeleteIntent = intent;
9371 return this;
9372 }
9373
9374 /**
9375 * Returns the TV-specific delete intent. If this method returns null, the
9376 * main delete intent on the notification should be used.
9377 *
9378 * @see {@link Notification#deleteIntent}
9379 */
9380 public PendingIntent getDeleteIntent() {
9381 return mDeleteIntent;
9382 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009383
9384 /**
9385 * Specifies whether this notification should suppress showing a message over top of apps
9386 * outside of the launcher.
9387 */
9388 public TvExtender setSuppressShowOverApps(boolean suppress) {
9389 mSuppressShowOverApps = suppress;
9390 return this;
9391 }
9392
9393 /**
9394 * Returns true if this notification should not show messages over top of apps
9395 * outside of the launcher.
9396 */
9397 public boolean getSuppressShowOverApps() {
9398 return mSuppressShowOverApps;
9399 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009400 }
9401
9402 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009403 * Get an array of Notification objects from a parcelable array bundle field.
9404 * Update the bundle to have a typed array so fetches in the future don't need
9405 * to do an array copy.
9406 */
9407 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9408 Parcelable[] array = bundle.getParcelableArray(key);
9409 if (array instanceof Notification[] || array == null) {
9410 return (Notification[]) array;
9411 }
9412 Notification[] typedArray = Arrays.copyOf(array, array.length,
9413 Notification[].class);
9414 bundle.putParcelableArray(key, typedArray);
9415 return typedArray;
9416 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009417
9418 private static class BuilderRemoteViews extends RemoteViews {
9419 public BuilderRemoteViews(Parcel parcel) {
9420 super(parcel);
9421 }
9422
Kenny Guy77320062014-08-27 21:37:15 +01009423 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9424 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009425 }
9426
9427 @Override
9428 public BuilderRemoteViews clone() {
9429 Parcel p = Parcel.obtain();
9430 writeToParcel(p, 0);
9431 p.setDataPosition(0);
9432 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9433 p.recycle();
9434 return brv;
9435 }
9436 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009437
9438 private static class StandardTemplateParams {
9439 boolean hasProgress = true;
9440 boolean ambient = false;
9441 CharSequence title;
9442 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009443 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009444 boolean hideLargeIcon;
9445 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009446
9447 final StandardTemplateParams reset() {
9448 hasProgress = true;
9449 ambient = false;
9450 title = null;
9451 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009452 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009453 return this;
9454 }
9455
9456 final StandardTemplateParams hasProgress(boolean hasProgress) {
9457 this.hasProgress = hasProgress;
9458 return this;
9459 }
9460
9461 final StandardTemplateParams title(CharSequence title) {
9462 this.title = title;
9463 return this;
9464 }
9465
9466 final StandardTemplateParams text(CharSequence text) {
9467 this.text = text;
9468 return this;
9469 }
9470
Selim Cinekafeed292017-12-12 17:32:44 -08009471 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9472 this.headerTextSecondary = text;
9473 return this;
9474 }
9475
Selim Cinek88188f22017-09-19 16:46:56 -07009476 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9477 this.alwaysShowReply = alwaysShowReply;
9478 return this;
9479 }
9480
9481 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9482 this.hideLargeIcon = hideLargeIcon;
9483 return this;
9484 }
9485
Adrian Roos70d7aa32017-01-11 15:39:06 -08009486 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009487 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009488 this.ambient = ambient;
9489 return this;
9490 }
9491
9492 final StandardTemplateParams fillTextsFrom(Builder b) {
9493 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009494 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9495
9496 // Big text notifications should contain their content when viewed in ambient mode.
9497 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9498 if (!ambient || TextUtils.isEmpty(text)) {
9499 text = extras.getCharSequence(EXTRA_TEXT);
9500 }
9501 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009502 return this;
9503 }
9504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009505}