blob: 590fbc7f6a7c051697c7357a513ff998c2f69cc8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Lucas Dupina291d192018-06-07 13:59:42 -070019import static com.android.internal.util.ContrastColorUtil.satisfiesTextContrast;
Selim Cinek389edcd2017-05-11 19:16:44 -070020
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Lucas Dupina291d192018-06-07 13:59:42 -070082import com.android.internal.util.ContrastColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Julia Reynolds7217dc92018-03-07 12:12:09 -050092import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050093import java.util.Set;
Jeff Sharkey23b31182018-04-18 21:32:12 -060094import java.util.function.Consumer;
Joe Onorato561d3852010-11-20 18:09:34 -080095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096/**
97 * A class that represents how a persistent notification is to be presented to
98 * the user using the {@link android.app.NotificationManager}.
99 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800100 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
101 * easier to construct Notifications.</p>
102 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700103 * <div class="special reference">
104 * <h3>Developer Guides</h3>
105 * <p>For a guide to creating notifications, read the
106 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
107 * developer guide.</p>
108 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
110public class Notification implements Parcelable
111{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400112 private static final String TAG = "Notification";
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400116 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400117 */
118 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
119 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
120 = "android.intent.category.NOTIFICATION_PREFERENCES";
121
122 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500123 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
124 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500126 */
127 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
128
129 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400130 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400131 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
132 * what settings should be shown in the target app.
133 */
134 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
135
136 /**
137 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400138 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
139 * that can be used to narrow down what settings should be shown in the target app.
140 */
141 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
142
143 /**
144 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
145 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
146 * that can be used to narrow down what settings should be shown in the target app.
147 */
148 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
149
150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * Use all default values (where applicable).
152 */
153 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /**
156 * Use the default notification sound. This will ignore any given
157 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500158 *
Chris Wren47c20a12014-06-18 17:27:29 -0400159 * <p>
160 * A notification that is noisy is more likely to be presented as a heads-up notification.
161 * </p>
162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 */
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final int DEFAULT_SOUND = 1;
167
168 /**
169 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700171 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500172 *
Chris Wren47c20a12014-06-18 17:27:29 -0400173 * <p>
174 * A notification that vibrates is more likely to be presented as a heads-up notification.
175 * </p>
176 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 */
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 /**
183 * Use the default notification lights. This will ignore the
184 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
185 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188 */
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200193 * Maximum length of CharSequences accepted by Builder and friends.
194 *
195 * <p>
196 * Avoids spamming the system with overly large strings such as full e-mails.
197 */
198 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
199
200 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800201 * Maximum entries of reply text that are accepted by Builder and friends.
202 */
203 private static final int MAX_REPLY_HISTORY = 5;
204
Tony Mak09db2ea2018-06-27 18:12:48 +0100205 /**
206 * Maximum numbers of action buttons in a notification.
207 * @hide
208 */
209 public static final int MAX_ACTION_BUTTONS = 3;
Selim Cinekde4de0e2018-01-24 16:21:07 -0800210
211 /**
212 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
213 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
214 *
215 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
216 * sends messages.</p>
217 */
218 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
219
Adrian Roose458aa82015-12-08 16:17:19 -0800220 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500221 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * Default value: {@link System#currentTimeMillis() Now}.
224 *
225 * Choose a timestamp that will be most relevant to the user. For most finite events, this
226 * corresponds to the time the event happened (or will happen, in the case of events that have
227 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800228 * timestamped according to when the activity began.
229 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500230 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800231 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500232 * <ul>
233 * <li>Notification of a new chat message should be stamped when the message was received.</li>
234 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
235 * <li>Notification of a completed file download should be stamped when the download finished.</li>
236 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
237 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
238 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800239 * </ul>
240 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700241 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
242 * anymore by default and must be opted into by using
243 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 */
245 public long when;
246
247 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700248 * The creation time of the notification
249 */
250 private long creationTime;
251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400254 *
255 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 */
Dan Sandler86647982015-05-13 23:41:13 -0400257 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700258 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public int icon;
260
261 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800262 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
263 * leave it at its default value of 0.
264 *
265 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700266 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800267 */
268 public int iconLevel;
269
270 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500271 * The number of events that this notification represents. For example, in a new mail
272 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800273 *
Julia Reynolds30331982017-04-27 10:12:50 -0400274 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500275 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
276 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 */
Julia Reynolds30331982017-04-27 10:12:50 -0400278 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
280 /**
281 * The intent to execute when the expanded status entry is clicked. If
282 * this is an activity, it must include the
283 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800284 * that you take care of task management as described in the
285 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800286 * Stack</a> document. In particular, make sure to read the notification section
287 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
288 * Notifications</a> for the correct ways to launch an application from a
289 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 */
291 public PendingIntent contentIntent;
292
293 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500294 * The intent to execute when the notification is explicitly dismissed by the user, either with
295 * the "Clear All" button or by swiping it away individually.
296 *
297 * This probably shouldn't be launching an activity since several of those will be sent
298 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 */
300 public PendingIntent deleteIntent;
301
302 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700303 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800304 *
Chris Wren47c20a12014-06-18 17:27:29 -0400305 * <p>
306 * The system UI may choose to display a heads-up notification, instead of
307 * launching this intent, while the user is using the device.
308 * </p>
309 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800310 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400311 */
312 public PendingIntent fullScreenIntent;
313
314 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400315 * Text that summarizes this notification for accessibility services.
316 *
317 * As of the L release, this text is no longer shown on screen, but it is still useful to
318 * accessibility services (where it serves as an audible announcement of the notification's
319 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400320 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800321 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 */
323 public CharSequence tickerText;
324
325 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400326 * Formerly, a view showing the {@link #tickerText}.
327 *
328 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400329 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400330 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800331 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400332
333 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 * The view that will represent this notification in the notification list (which is pulled
335 * down from the status bar).
336 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500337 * As of N, this field may be null. The notification view is determined by the inputs
338 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400339 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400341 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 public RemoteViews contentView;
343
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400344 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400345 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400346 * opportunity to show more detail. The system UI may choose to show this
347 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400348 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500349 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400350 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
351 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400352 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400353 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400354 public RemoteViews bigContentView;
355
Chris Wren8fd39ec2014-02-27 17:43:26 -0500356
357 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400358 * A medium-format version of {@link #contentView}, providing the Notification an
359 * opportunity to add action buttons to contentView. At its discretion, the system UI may
360 * choose to show this as a heads-up notification, which will pop up so the user can see
361 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400362 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500363 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400364 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
365 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500366 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400367 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500368 public RemoteViews headsUpContentView;
369
Julia Reynoldsfc640012018-02-21 12:25:27 -0500370 private boolean mUsesStandardHeader;
371
372 private static final ArraySet<Integer> STANDARD_LAYOUTS = new ArraySet<>();
373 static {
374 STANDARD_LAYOUTS.add(R.layout.notification_template_material_base);
375 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_base);
376 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_picture);
377 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_text);
378 STANDARD_LAYOUTS.add(R.layout.notification_template_material_inbox);
379 STANDARD_LAYOUTS.add(R.layout.notification_template_material_messaging);
380 STANDARD_LAYOUTS.add(R.layout.notification_template_material_media);
381 STANDARD_LAYOUTS.add(R.layout.notification_template_material_big_media);
382 STANDARD_LAYOUTS.add(R.layout.notification_template_ambient_header);
383 STANDARD_LAYOUTS.add(R.layout.notification_template_header);
384 STANDARD_LAYOUTS.add(R.layout.notification_template_material_ambient);
385 }
386
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400387 /**
Dan Sandler86647982015-05-13 23:41:13 -0400388 * A large bitmap to be shown in the notification content area.
389 *
390 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
Dan Sandler86647982015-05-13 23:41:13 -0400392 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800393 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
395 /**
396 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400399 * A notification that is noisy is more likely to be presented as a heads-up notification.
400 * </p>
401 *
402 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500405 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500407 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public Uri sound;
409
410 /**
411 * Use this constant as the value for audioStreamType to request that
412 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700413 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400414 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500415 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700417 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 public static final int STREAM_DEFAULT = -1;
419
420 /**
421 * The audio stream type to use when playing the sound.
422 * Should be one of the STREAM_ constants from
423 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400424 *
425 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700427 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 public int audioStreamType = STREAM_DEFAULT;
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400431 * The default value of {@link #audioAttributes}.
432 */
433 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
434 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
435 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
436 .build();
437
438 /**
439 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500440 *
441 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400442 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400444 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
445
446 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500447 * The pattern with which to vibrate.
448 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * <p>
450 * To vibrate the default pattern, see {@link #defaults}.
451 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500454 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public long[] vibrate;
458
459 /**
460 * The color of the led. The hardware will do its best approximation.
461 *
462 * @see #FLAG_SHOW_LIGHTS
463 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500464 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
Tor Norbye80756e32015-03-02 09:39:27 -0800466 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500467 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 public int ledARGB;
469
470 /**
471 * The number of milliseconds for the LED to be on while it's flashing.
472 * The hardware will do its best approximation.
473 *
474 * @see #FLAG_SHOW_LIGHTS
475 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500478 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 public int ledOnMS;
480
481 /**
482 * The number of milliseconds for the LED to be off while it's flashing.
483 * The hardware will do its best approximation.
484 *
485 * @see #FLAG_SHOW_LIGHTS
486 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500487 *
488 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500490 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public int ledOffMS;
492
493 /**
494 * Specifies which values should be taken from the defaults.
495 * <p>
496 * To set, OR the desired from {@link #DEFAULT_SOUND},
497 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
498 * values, use {@link #DEFAULT_ALL}.
499 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500500 *
501 * @deprecated use {@link NotificationChannel#getSound()} and
502 * {@link NotificationChannel#shouldShowLights()} and
503 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500505 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 public int defaults;
507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 /**
509 * Bit to be bitwise-ored into the {@link #flags} field that should be
510 * set if you want the LED on for this notification.
511 * <ul>
512 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
513 * or 0 for both ledOnMS and ledOffMS.</li>
514 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
515 * <li>To flash the LED, pass the number of milliseconds that it should
516 * be on and off to ledOnMS and ledOffMS.</li>
517 * </ul>
518 * <p>
519 * Since hardware varies, you are not guaranteed that any of the values
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800520 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 * because they will be set to values that work on any given hardware.
522 * <p>
523 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500524 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500525 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500527 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
529
530 /**
531 * Bit to be bitwise-ored into the {@link #flags} field that should be
532 * set if this notification is in reference to something that is ongoing,
533 * like a phone call. It should not be set if this notification is in
534 * reference to something that happened at a particular point in time,
535 * like a missed phone call.
536 */
537 public static final int FLAG_ONGOING_EVENT = 0x00000002;
538
539 /**
540 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700541 * the audio will be repeated until the notification is
542 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 */
544 public static final int FLAG_INSISTENT = 0x00000004;
545
546 /**
547 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700548 * set if you would only like the sound, vibrate and ticker to be played
549 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 */
551 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
552
553 /**
554 * Bit to be bitwise-ored into the {@link #flags} field that should be
555 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500556 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 */
558 public static final int FLAG_AUTO_CANCEL = 0x00000010;
559
560 /**
561 * Bit to be bitwise-ored into the {@link #flags} field that should be
562 * set if the notification should not be canceled when the user clicks
563 * the Clear all button.
564 */
565 public static final int FLAG_NO_CLEAR = 0x00000020;
566
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700567 /**
568 * Bit to be bitwise-ored into the {@link #flags} field that should be
569 * set if this notification represents a currently running service. This
570 * will normally be set for you by {@link Service#startForeground}.
571 */
572 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
573
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400574 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500575 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800576 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500577 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400578 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700579 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400581
Griff Hazendfcb0802014-02-11 12:00:00 -0800582 /**
583 * Bit to be bitswise-ored into the {@link #flags} field that should be
584 * set if this notification is relevant to the current device only
585 * and it is not recommended that it bridge to other devices.
586 */
587 public static final int FLAG_LOCAL_ONLY = 0x00000100;
588
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700589 /**
590 * Bit to be bitswise-ored into the {@link #flags} field that should be
591 * set if this notification is the group summary for a group of notifications.
592 * Grouped notifications may display in a cluster or stack on devices which
593 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
594 */
595 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
596
Julia Reynoldse46bb372016-03-17 11:05:58 -0400597 /**
598 * Bit to be bitswise-ored into the {@link #flags} field that should be
599 * set if this notification is the group summary for an auto-group of notifications.
600 *
601 * @hide
602 */
603 @SystemApi
604 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
605
Julia Reynolds4db59552017-06-30 13:34:01 -0400606 /**
607 * @hide
608 */
609 public static final int FLAG_CAN_COLORIZE = 0x00000800;
610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 public int flags;
612
Tor Norbyed9273d62013-05-30 15:59:53 -0700613 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700614 @IntDef(prefix = { "PRIORITY_" }, value = {
615 PRIORITY_DEFAULT,
616 PRIORITY_LOW,
617 PRIORITY_MIN,
618 PRIORITY_HIGH,
619 PRIORITY_MAX
620 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700621 @Retention(RetentionPolicy.SOURCE)
622 public @interface Priority {}
623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 * Default notification {@link #priority}. If your application does not prioritize its own
626 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500627 *
628 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500630 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631 public static final int PRIORITY_DEFAULT = 0;
632
633 /**
634 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
635 * items smaller, or at a different position in the list, compared with your app's
636 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500637 *
638 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500640 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641 public static final int PRIORITY_LOW = -1;
642
643 /**
644 * Lowest {@link #priority}; these items might not be shown to the user except under special
645 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500646 *
647 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500649 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 public static final int PRIORITY_MIN = -2;
651
652 /**
653 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
654 * show these items larger, or at a different position in notification lists, compared with
655 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500656 *
657 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500659 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 public static final int PRIORITY_HIGH = 1;
661
662 /**
663 * Highest {@link #priority}, for your application's most important items that require the
664 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500665 *
666 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500668 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 public static final int PRIORITY_MAX = 2;
670
671 /**
672 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800673 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500674 * Priority is an indication of how much of the user's valuable attention should be consumed by
675 * this notification. Low-priority notifications may be hidden from the user in certain
676 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500677 * system will make a determination about how to interpret this priority when presenting
678 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400679 *
680 * <p>
681 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
682 * as a heads-up notification.
683 * </p>
684 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500685 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500686 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700687 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500688 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800690
Dan Sandler26e81cf2014-05-06 10:01:27 -0400691 /**
692 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
693 * to be applied by the standard Style templates when presenting this notification.
694 *
695 * The current template design constructs a colorful header image by overlaying the
696 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
697 * ignored.
698 */
Tor Norbye80756e32015-03-02 09:39:27 -0800699 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400700 public int color = COLOR_DEFAULT;
701
702 /**
703 * Special value of {@link #color} telling the system not to decorate this notification with
704 * any special color but instead use default colors when presenting this notification.
705 */
Tor Norbye80756e32015-03-02 09:39:27 -0800706 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400707 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600708
709 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800710 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800711 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800712 */
713 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800714 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800715
716 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700717 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
718 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600719 * lockscreen).
720 *
721 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
722 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
723 * shown in all situations, but the contents are only available if the device is unlocked for
724 * the appropriate user.
725 *
726 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
727 * can be read even in an "insecure" context (that is, above a secure lockscreen).
728 * To modify the public version of this notification—for example, to redact some portions—see
729 * {@link Builder#setPublicVersion(Notification)}.
730 *
731 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
732 * and ticker until the user has bypassed the lockscreen.
733 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600734 public @Visibility int visibility;
735
736 /** @hide */
737 @IntDef(prefix = { "VISIBILITY_" }, value = {
738 VISIBILITY_PUBLIC,
739 VISIBILITY_PRIVATE,
740 VISIBILITY_SECRET,
741 })
742 @Retention(RetentionPolicy.SOURCE)
743 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600744
Griff Hazenfc3922d2014-08-20 11:56:44 -0700745 /**
746 * Notification visibility: Show this notification in its entirety on all lockscreens.
747 *
748 * {@see #visibility}
749 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600750 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700751
752 /**
753 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
754 * private information on secure lockscreens.
755 *
756 * {@see #visibility}
757 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600758 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700759
760 /**
761 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
762 *
763 * {@see #visibility}
764 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600765 public static final int VISIBILITY_SECRET = -1;
766
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500767 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400768 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500769 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400770 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771
772 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700773 * Notification category: map turn-by-turn navigation.
774 */
775 public static final String CATEGORY_NAVIGATION = "navigation";
776
777 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400778 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500779 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400780 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500781
782 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500784 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400785 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500786
787 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400788 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500789 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400790 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500791
792 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400793 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500794 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400795 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500796
797 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400798 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500799 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400800 public static final String CATEGORY_ALARM = "alarm";
801
802 /**
803 * Notification category: progress of a long-running background operation.
804 */
805 public static final String CATEGORY_PROGRESS = "progress";
806
807 /**
808 * Notification category: social network or sharing update.
809 */
810 public static final String CATEGORY_SOCIAL = "social";
811
812 /**
813 * Notification category: error in background operation or authentication status.
814 */
815 public static final String CATEGORY_ERROR = "err";
816
817 /**
818 * Notification category: media transport control for playback.
819 */
820 public static final String CATEGORY_TRANSPORT = "transport";
821
822 /**
823 * Notification category: system or device status update. Reserved for system use.
824 */
825 public static final String CATEGORY_SYSTEM = "sys";
826
827 /**
828 * Notification category: indication of running background service.
829 */
830 public static final String CATEGORY_SERVICE = "service";
831
832 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400833 * Notification category: a specific, timely recommendation for a single thing.
834 * For example, a news app might want to recommend a news story it believes the user will
835 * want to read next.
836 */
837 public static final String CATEGORY_RECOMMENDATION = "recommendation";
838
839 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400840 * Notification category: ongoing information about device or contextual status.
841 */
842 public static final String CATEGORY_STATUS = "status";
843
844 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400845 * Notification category: user-scheduled reminder.
846 */
847 public static final String CATEGORY_REMINDER = "reminder";
848
849 /**
Adora Zhangaa90e872018-03-12 14:07:50 -0700850 * Notification category: extreme car emergencies.
851 * @hide
852 */
853 @SystemApi
854 public static final String CATEGORY_CAR_EMERGENCY = "car_emergency";
855
856 /**
857 * Notification category: car warnings.
858 * @hide
859 */
860 @SystemApi
861 public static final String CATEGORY_CAR_WARNING = "car_warning";
862
863 /**
864 * Notification category: general car system information.
865 * @hide
866 */
867 @SystemApi
868 public static final String CATEGORY_CAR_INFORMATION = "car_information";
869
870 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400871 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
872 * that best describes this Notification. May be used by the system for ranking and filtering.
873 */
874 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500875
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700876 private String mGroupKey;
877
878 /**
879 * Get the key used to group this notification into a cluster or stack
880 * with other notifications on devices which support such rendering.
881 */
882 public String getGroup() {
883 return mGroupKey;
884 }
885
886 private String mSortKey;
887
888 /**
889 * Get a sort key that orders this notification among other notifications from the
890 * same package. This can be useful if an external sort was already applied and an app
891 * would like to preserve this. Notifications will be sorted lexicographically using this
892 * value, although providing different priorities in addition to providing sort key may
893 * cause this value to be ignored.
894 *
895 * <p>This sort key can also be used to order members of a notification group. See
896 * {@link Builder#setGroup}.
897 *
898 * @see String#compareTo(String)
899 */
900 public String getSortKey() {
901 return mSortKey;
902 }
903
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500904 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400905 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400906 * <p>
907 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
908 * APIs, and are intended to be used by
909 * {@link android.service.notification.NotificationListenerService} implementations to extract
910 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500911 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400912 public Bundle extras = new Bundle();
913
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700915 * All pending intents in the notification as the system needs to be able to access them but
916 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700917 * custom parcelable objects.
918 *
919 * @hide
920 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700921 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700922
923 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700924 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
925 * pending intents inside of it, so only those will get the behavior.
926 *
927 * @hide
928 */
Adrian Roosfb921842017-10-26 14:49:56 +0200929 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700930
931 /**
932 * Must be set by a process to start associating tokens with Notification objects
933 * coming in to it. This is set by NotificationManagerService.
934 *
935 * @hide
936 */
937 static public IBinder processWhitelistToken;
938
939 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940 * {@link #extras} key: this is the title of the notification,
941 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
942 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500943 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944
945 /**
946 * {@link #extras} key: this is the title of the notification when shown in expanded form,
947 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
948 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400949 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is the main text payload, as supplied to
953 * {@link Builder#setContentText(CharSequence)}.
954 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500955 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956
957 /**
958 * {@link #extras} key: this is a third line of text, as supplied to
959 * {@link Builder#setSubText(CharSequence)}.
960 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400961 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962
963 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800964 * {@link #extras} key: this is the remote input history, as supplied to
965 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700966 *
967 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
968 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
969 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
970 * notifications once the other party has responded).
971 *
972 * The extra with this key is of type CharSequence[] and contains the most recent entry at
973 * the 0 index, the second most recent at the 1 index, etc.
974 *
975 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800976 */
977 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
978
979 /**
Kenny Guya0f6de82018-04-06 16:20:16 +0100980 * {@link #extras} key: boolean as supplied to
981 * {@link Builder#setShowRemoteInputSpinner(boolean)}.
982 *
983 * If set to true, then the view displaying the remote input history from
984 * {@link Builder#setRemoteInputHistory(CharSequence[])} will have a progress spinner.
985 *
986 * @see Builder#setShowRemoteInputSpinner(boolean)
987 * @hide
988 */
989 public static final String EXTRA_SHOW_REMOTE_INPUT_SPINNER = "android.remoteInputSpinner";
990
991 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +0100992 * {@link #extras} key: boolean as supplied to
993 * {@link Builder#setHideSmartReplies(boolean)}.
994 *
995 * If set to true, then any smart reply buttons will be hidden.
996 *
997 * @see Builder#setHideSmartReplies(boolean)
998 * @hide
999 */
1000 public static final String EXTRA_HIDE_SMART_REPLIES = "android.hideSmartReplies";
1001
1002 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003 * {@link #extras} key: this is a small piece of additional text as supplied to
1004 * {@link Builder#setContentInfo(CharSequence)}.
1005 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001006 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001007
1008 /**
1009 * {@link #extras} key: this is a line of summary information intended to be shown
1010 * alongside expanded notifications, as supplied to (e.g.)
1011 * {@link BigTextStyle#setSummaryText(CharSequence)}.
1012 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001013 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001014
1015 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001016 * {@link #extras} key: this is the longer text shown in the big form of a
1017 * {@link BigTextStyle} notification, as supplied to
1018 * {@link BigTextStyle#bigText(CharSequence)}.
1019 */
1020 public static final String EXTRA_BIG_TEXT = "android.bigText";
1021
1022 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001023 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
1024 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001025 *
1026 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001028 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001029 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030
1031 /**
1032 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
1033 * notification payload, as
1034 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -04001035 *
1036 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04001038 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001039 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001040
1041 /**
1042 * {@link #extras} key: this is a bitmap to be used instead of the one from
1043 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
1044 * shown in its expanded form, as supplied to
1045 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
1046 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001047 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048
1049 /**
1050 * {@link #extras} key: this is the progress value supplied to
1051 * {@link Builder#setProgress(int, int, boolean)}.
1052 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001053 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054
1055 /**
1056 * {@link #extras} key: this is the maximum value supplied to
1057 * {@link Builder#setProgress(int, int, boolean)}.
1058 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001059 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060
1061 /**
1062 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
1063 * {@link Builder#setProgress(int, int, boolean)}.
1064 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001065 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001066
1067 /**
1068 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
1069 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
1070 * {@link Builder#setUsesChronometer(boolean)}.
1071 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001072 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001073
1074 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001075 * {@link #extras} key: whether the chronometer set on the notification should count down
1076 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001077 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001078 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001079 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001080
1081 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001082 * {@link #extras} key: whether {@link #when} should be shown,
1083 * as supplied to {@link Builder#setShowWhen(boolean)}.
1084 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001085 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001086
1087 /**
1088 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1089 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1090 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001091 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001092
1093 /**
1094 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1095 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1096 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001097 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001098
1099 /**
1100 * {@link #extras} key: A string representing the name of the specific
1101 * {@link android.app.Notification.Style} used to create this notification.
1102 */
Chris Wren91ad5632013-06-05 15:05:57 -04001103 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001104
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001105 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001106 * {@link #extras} key: A String array containing the people that this notification relates to,
1107 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001108 *
1109 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001110 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001111 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001112
1113 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001114 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1115 * this notification relates to.
1116 */
1117 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1118
1119 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001120 * Allow certain system-generated notifications to appear before the device is provisioned.
1121 * Only available to notifications coming from the android package.
1122 * @hide
1123 */
Maurice Lam96c10032017-03-29 15:42:38 -07001124 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001125 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001126 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1127
1128 /**
Robin Leed107af62018-04-27 13:55:56 +02001129 * {@link #extras} key:
1130 * flat {@link String} representation of a {@link android.content.ContentUris content URI}
1131 * pointing to an image that can be displayed in the background when the notification is
1132 * selected. Used on television platforms. The URI must point to an image stream suitable for
1133 * passing into {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001134 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001135 */
1136 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1137
1138 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001139 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001140 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001141 * {@link android.app.Notification.MediaStyle} notification.
1142 */
1143 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1144
1145 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001146 * {@link #extras} key: the indices of actions to be shown in the compact view,
1147 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1148 */
1149 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1150
Christoph Studer943aa672014-08-03 20:31:16 +02001151 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001152 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1153 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001154 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1155 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001156 *
1157 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001158 */
1159 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1160
1161 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001162 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1163 * direct replies
1164 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1165 * {@link Person}
1166 */
1167 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1168
1169 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001170 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001171 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001172 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001173 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001174
1175 /**
1176 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1177 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001178 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1179 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001180 */
1181 public static final String EXTRA_MESSAGES = "android.messages";
1182
1183 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001184 * {@link #extras} key: an array of
1185 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1186 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1187 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1188 * array of bundles.
1189 */
1190 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1191
1192 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001193 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1194 * represents a group conversation.
1195 */
1196 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1197
1198 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001199 * {@link #extras} key: whether the notification should be colorized as
1200 * supplied to {@link Builder#setColorized(boolean)}}.
1201 */
1202 public static final String EXTRA_COLORIZED = "android.colorized";
1203
1204 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001205 * @hide
1206 */
1207 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1208
Selim Cinek247fa012016-02-18 09:50:48 -08001209 /**
1210 * @hide
1211 */
1212 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1213
Shane Brennan472a3b32016-12-12 15:28:10 -08001214 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001215 * @hide
1216 */
1217 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1218
1219 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001220 * {@link #extras} key: the audio contents of this notification.
1221 *
1222 * This is for use when rendering the notification on an audio-focused interface;
1223 * the audio contents are a complete sound sample that contains the contents/body of the
1224 * notification. This may be used in substitute of a Text-to-Speech reading of the
1225 * notification. For example if the notification represents a voice message this should point
1226 * to the audio of that message.
1227 *
1228 * The data stored under this key should be a String representation of a Uri that contains the
1229 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1230 *
1231 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1232 * has a field for holding data URI. That field can be used for audio.
1233 * See {@code Message#setData}.
1234 *
1235 * Example usage:
1236 * <pre>
1237 * {@code
1238 * Notification.Builder myBuilder = (build your Notification as normal);
1239 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1240 * }
1241 * </pre>
1242 */
1243 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1244
Dan Sandler80eaa592016-04-14 23:34:54 -04001245 /** @hide */
1246 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001247 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001248 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1249
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001250 /**
Julia Reynolds3c7de112018-03-28 09:33:13 -04001251 * This is set on the notifications shown by system_server about apps running foreground
1252 * services. It indicates that the notification should be shown
1253 * only if any of the given apps do not already have a properly tagged
1254 * {@link #FLAG_FOREGROUND_SERVICE} notification currently visible to the user.
1255 * This is a string array of all package names of the apps.
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001256 * @hide
1257 */
1258 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1259
Dan Sandlerd63f9322015-05-06 15:18:49 -04001260 private Icon mSmallIcon;
1261 private Icon mLargeIcon;
1262
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001263 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001264 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001265
Julia Reynolds13d898c2017-02-02 12:22:05 -05001266 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001267 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001268
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001269 /** @hide */
1270 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1271 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1272 })
1273 @Retention(RetentionPolicy.SOURCE)
1274 public @interface GroupAlertBehavior {}
1275
1276 /**
1277 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1278 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1279 * notification will not be muted when it is in a group.
1280 */
1281 public static final int GROUP_ALERT_ALL = 0;
1282
1283 /**
1284 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1285 * notification in a group should be silenced (no sound or vibration) even if they are posted
1286 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001287 * mute this notification if this notification is a group child. This must be applied to all
1288 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001289 *
1290 * <p> For example, you might want to use this constant if you post a number of children
1291 * notifications at once (say, after a periodic sync), and only need to notify the user
1292 * audibly once.
1293 */
1294 public static final int GROUP_ALERT_SUMMARY = 1;
1295
1296 /**
1297 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1298 * notification in a group should be silenced (no sound or vibration) even if they are
1299 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1300 * to mute this notification if this notification is a group summary.
1301 *
1302 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001303 * in your group have content and the summary is only used to visually group notifications
1304 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001305 */
1306 public static final int GROUP_ALERT_CHILDREN = 2;
1307
Julia Reynolds2f431e22017-06-07 14:12:09 +00001308 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001309
Julia Reynolds13d898c2017-02-02 12:22:05 -05001310 /**
1311 * If this notification is being shown as a badge, always show as a number.
1312 */
1313 public static final int BADGE_ICON_NONE = 0;
1314
1315 /**
1316 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1317 * represent this notification.
1318 */
1319 public static final int BADGE_ICON_SMALL = 1;
1320
1321 /**
1322 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1323 * represent this notification.
1324 */
1325 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001326 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001327
Chris Wren51c75102013-07-16 20:49:17 -04001328 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001329 * Structure to encapsulate a named action that can be shown as part of this notification.
1330 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1331 * selected by the user.
1332 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001333 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1334 * or {@link Notification.Builder#addAction(Notification.Action)}
1335 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001336 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001337 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001338 /**
1339 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1340 * {@link RemoteInput}s.
1341 *
1342 * This is intended for {@link RemoteInput}s that only accept data, meaning
1343 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1344 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1345 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1346 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1347 *
1348 * You can test if a RemoteInput matches these constraints using
1349 * {@link RemoteInput#isDataOnly}.
1350 */
1351 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1352
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001353 /**
1354 * {@link }: No semantic action defined.
1355 */
1356 public static final int SEMANTIC_ACTION_NONE = 0;
1357
1358 /**
1359 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1360 * may be appropriate.
1361 */
1362 public static final int SEMANTIC_ACTION_REPLY = 1;
1363
1364 /**
1365 * {@code SemanticAction}: Mark content as read.
1366 */
1367 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1368
1369 /**
1370 * {@code SemanticAction}: Mark content as unread.
1371 */
1372 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1373
1374 /**
1375 * {@code SemanticAction}: Delete the content associated with the notification. This
1376 * could mean deleting an email, message, etc.
1377 */
1378 public static final int SEMANTIC_ACTION_DELETE = 4;
1379
1380 /**
1381 * {@code SemanticAction}: Archive the content associated with the notification. This
1382 * could mean archiving an email, message, etc.
1383 */
1384 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1385
1386 /**
1387 * {@code SemanticAction}: Mute the content associated with the notification. This could
1388 * mean silencing a conversation or currently playing media.
1389 */
1390 public static final int SEMANTIC_ACTION_MUTE = 6;
1391
1392 /**
1393 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1394 * mean un-silencing a conversation or currently playing media.
1395 */
1396 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1397
1398 /**
1399 * {@code SemanticAction}: Mark content with a thumbs up.
1400 */
1401 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1402
1403 /**
1404 * {@code SemanticAction}: Mark content with a thumbs down.
1405 */
1406 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1407
Kodlee Yincda5b092018-02-15 15:34:53 -08001408 /**
1409 * {@code SemanticAction}: Call a contact, group, etc.
1410 */
1411 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001412
Griff Hazen959591e2014-05-15 22:26:18 -07001413 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001414 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001415 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001416 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001417 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001418
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001419 /**
1420 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001421 *
1422 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001423 */
Dan Sandler86647982015-05-13 23:41:13 -04001424 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001425 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001426
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001427 /**
1428 * Title of the action.
1429 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001430 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001432 /**
1433 * Intent to send when the user invokes this action. May be null, in which case the action
1434 * may be rendered in a disabled presentation by the system UI.
1435 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001436 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001437
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001438 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001439 if (in.readInt() != 0) {
1440 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001441 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1442 icon = mIcon.getResId();
1443 }
Dan Sandler86647982015-05-13 23:41:13 -04001444 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001445 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1446 if (in.readInt() == 1) {
1447 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1448 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001449 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001450 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001451 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001452 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001453 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001454
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001455 /**
Dan Sandler86647982015-05-13 23:41:13 -04001456 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001457 */
Dan Sandler86647982015-05-13 23:41:13 -04001458 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001459 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001460 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1461 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001462 }
1463
Adrian Roos7af53622016-10-12 13:44:05 -07001464 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001465 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001466 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1467 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001468 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001469 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1470 this.icon = icon.getResId();
1471 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001472 this.title = title;
1473 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001474 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001475 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001476 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001477 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001478 }
1479
1480 /**
Dan Sandler86647982015-05-13 23:41:13 -04001481 * Return an icon representing the action.
1482 */
1483 public Icon getIcon() {
1484 if (mIcon == null && icon != 0) {
1485 // you snuck an icon in here without using the builder; let's try to keep it
1486 mIcon = Icon.createWithResource("", icon);
1487 }
1488 return mIcon;
1489 }
1490
1491 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001492 * Get additional metadata carried around with this Action.
1493 */
1494 public Bundle getExtras() {
1495 return mExtras;
1496 }
1497
1498 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001499 * Return whether the platform should automatically generate possible replies for this
1500 * {@link Action}
1501 */
1502 public boolean getAllowGeneratedReplies() {
1503 return mAllowGeneratedReplies;
1504 }
1505
1506 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001507 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001508 * May return null if no remote inputs were added. Only returns inputs which accept
1509 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001510 */
1511 public RemoteInput[] getRemoteInputs() {
1512 return mRemoteInputs;
1513 }
1514
1515 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001516 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1517 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1518 * (eg. reply, mark as read, delete, etc).
1519 */
1520 public @SemanticAction int getSemanticAction() {
1521 return mSemanticAction;
1522 }
1523
1524 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001525 * Get the list of inputs to be collected from the user that ONLY accept data when this
1526 * action is sent. These remote inputs are guaranteed to return true on a call to
1527 * {@link RemoteInput#isDataOnly}.
1528 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001529 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001530 *
1531 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1532 * of non-textual RemoteInputs do not access these remote inputs.
1533 */
1534 public RemoteInput[] getDataOnlyRemoteInputs() {
1535 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1536 }
1537
1538 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001539 * Builder class for {@link Action} objects.
1540 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001541 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001542 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001543 private final CharSequence mTitle;
1544 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001545 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001546 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001547 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001548 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001549
1550 /**
1551 * Construct a new builder for {@link Action} object.
1552 * @param icon icon to show for this action
1553 * @param title the title of the action
1554 * @param intent the {@link PendingIntent} to fire when users trigger this action
1555 */
Dan Sandler86647982015-05-13 23:41:13 -04001556 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001557 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001558 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001559 }
1560
1561 /**
1562 * Construct a new builder for {@link Action} object.
1563 * @param icon icon to show for this action
1564 * @param title the title of the action
1565 * @param intent the {@link PendingIntent} to fire when users trigger this action
1566 */
1567 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001568 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001569 }
1570
1571 /**
1572 * Construct a new builder for {@link Action} object using the fields from an
1573 * {@link Action}.
1574 * @param action the action to read fields from.
1575 */
1576 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001577 this(action.getIcon(), action.title, action.actionIntent,
1578 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001579 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001580 }
1581
Dan Sandler86647982015-05-13 23:41:13 -04001582 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001583 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1584 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001585 mIcon = icon;
1586 mTitle = title;
1587 mIntent = intent;
1588 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001589 if (remoteInputs != null) {
1590 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1591 Collections.addAll(mRemoteInputs, remoteInputs);
1592 }
Adrian Roos7af53622016-10-12 13:44:05 -07001593 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001594 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001595 }
1596
1597 /**
1598 * Merge additional metadata into this builder.
1599 *
1600 * <p>Values within the Bundle will replace existing extras values in this Builder.
1601 *
1602 * @see Notification.Action#extras
1603 */
1604 public Builder addExtras(Bundle extras) {
1605 if (extras != null) {
1606 mExtras.putAll(extras);
1607 }
1608 return this;
1609 }
1610
1611 /**
1612 * Get the metadata Bundle used by this Builder.
1613 *
1614 * <p>The returned Bundle is shared with this Builder.
1615 */
1616 public Bundle getExtras() {
1617 return mExtras;
1618 }
1619
1620 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001621 * Add an input to be collected from the user when this action is sent.
1622 * Response values can be retrieved from the fired intent by using the
1623 * {@link RemoteInput#getResultsFromIntent} function.
1624 * @param remoteInput a {@link RemoteInput} to add to the action
1625 * @return this object for method chaining
1626 */
1627 public Builder addRemoteInput(RemoteInput remoteInput) {
1628 if (mRemoteInputs == null) {
1629 mRemoteInputs = new ArrayList<RemoteInput>();
1630 }
1631 mRemoteInputs.add(remoteInput);
1632 return this;
1633 }
1634
1635 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001636 * Set whether the platform should automatically generate possible replies to add to
1637 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1638 * {@link RemoteInput}, this has no effect.
1639 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1640 * otherwise
1641 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001642 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001643 */
1644 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1645 mAllowGeneratedReplies = allowGeneratedReplies;
1646 return this;
1647 }
1648
1649 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001650 * Sets the {@code SemanticAction} for this {@link Action}. A
1651 * {@code SemanticAction} denotes what an {@link Action}'s
1652 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1653 * @param semanticAction a SemanticAction defined within {@link Action} with
1654 * {@code SEMANTIC_ACTION_} prefixes
1655 * @return this object for method chaining
1656 */
1657 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1658 mSemanticAction = semanticAction;
1659 return this;
1660 }
1661
1662 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001663 * Apply an extender to this action builder. Extenders may be used to add
1664 * metadata or change options on this builder.
1665 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001666 public Builder extend(Extender extender) {
1667 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001668 return this;
1669 }
1670
1671 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001672 * Combine all of the options that have been set and return a new {@link Action}
1673 * object.
1674 * @return the built action
1675 */
1676 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001677 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1678 RemoteInput[] previousDataInputs =
1679 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001680 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001681 for (RemoteInput input : previousDataInputs) {
1682 dataOnlyInputs.add(input);
1683 }
1684 }
1685 List<RemoteInput> textInputs = new ArrayList<>();
1686 if (mRemoteInputs != null) {
1687 for (RemoteInput input : mRemoteInputs) {
1688 if (input.isDataOnly()) {
1689 dataOnlyInputs.add(input);
1690 } else {
1691 textInputs.add(input);
1692 }
1693 }
1694 }
1695 if (!dataOnlyInputs.isEmpty()) {
1696 RemoteInput[] dataInputsArr =
1697 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1698 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1699 }
1700 RemoteInput[] textInputsArr = textInputs.isEmpty()
1701 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1702 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001703 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001704 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001705 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001706
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001707 @Override
1708 public Action clone() {
1709 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001710 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001711 title,
1712 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001713 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001714 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001715 getAllowGeneratedReplies(),
1716 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001717 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001718
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001719 @Override
1720 public int describeContents() {
1721 return 0;
1722 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001723
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001724 @Override
1725 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001726 final Icon ic = getIcon();
1727 if (ic != null) {
1728 out.writeInt(1);
1729 ic.writeToParcel(out, 0);
1730 } else {
1731 out.writeInt(0);
1732 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001733 TextUtils.writeToParcel(title, out, flags);
1734 if (actionIntent != null) {
1735 out.writeInt(1);
1736 actionIntent.writeToParcel(out, flags);
1737 } else {
1738 out.writeInt(0);
1739 }
Griff Hazen959591e2014-05-15 22:26:18 -07001740 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001741 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001742 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001743 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001744 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001745
Griff Hazen959591e2014-05-15 22:26:18 -07001746 public static final Parcelable.Creator<Action> CREATOR =
1747 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001748 public Action createFromParcel(Parcel in) {
1749 return new Action(in);
1750 }
1751 public Action[] newArray(int size) {
1752 return new Action[size];
1753 }
1754 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001755
1756 /**
1757 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1758 * metadata or change options on an action builder.
1759 */
1760 public interface Extender {
1761 /**
1762 * Apply this extender to a notification action builder.
1763 * @param builder the builder to be modified.
1764 * @return the build object for chaining.
1765 */
1766 public Builder extend(Builder builder);
1767 }
1768
1769 /**
1770 * Wearable extender for notification actions. To add extensions to an action,
1771 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1772 * the {@code WearableExtender()} constructor and apply it to a
1773 * {@link android.app.Notification.Action.Builder} using
1774 * {@link android.app.Notification.Action.Builder#extend}.
1775 *
1776 * <pre class="prettyprint">
1777 * Notification.Action action = new Notification.Action.Builder(
1778 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001779 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001780 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001781 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001782 */
1783 public static final class WearableExtender implements Extender {
1784 /** Notification action extra which contains wearable extensions */
1785 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1786
Pete Gastaf6781d2014-10-07 15:17:05 -04001787 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001788 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001789 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1790 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1791 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001792
1793 // Flags bitwise-ored to mFlags
1794 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001795 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001796 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001797
1798 // Default value for flags integer
1799 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1800
1801 private int mFlags = DEFAULT_FLAGS;
1802
Pete Gastaf6781d2014-10-07 15:17:05 -04001803 private CharSequence mInProgressLabel;
1804 private CharSequence mConfirmLabel;
1805 private CharSequence mCancelLabel;
1806
Griff Hazen61a9e862014-05-22 16:05:19 -07001807 /**
1808 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1809 * options.
1810 */
1811 public WearableExtender() {
1812 }
1813
1814 /**
1815 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1816 * wearable options present in an existing notification action.
1817 * @param action the notification action to inspect.
1818 */
1819 public WearableExtender(Action action) {
1820 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1821 if (wearableBundle != null) {
1822 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001823 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1824 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1825 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001826 }
1827 }
1828
1829 /**
1830 * Apply wearable extensions to a notification action that is being built. This is
1831 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1832 * method of {@link android.app.Notification.Action.Builder}.
1833 */
1834 @Override
1835 public Action.Builder extend(Action.Builder builder) {
1836 Bundle wearableBundle = new Bundle();
1837
1838 if (mFlags != DEFAULT_FLAGS) {
1839 wearableBundle.putInt(KEY_FLAGS, mFlags);
1840 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001841 if (mInProgressLabel != null) {
1842 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1843 }
1844 if (mConfirmLabel != null) {
1845 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1846 }
1847 if (mCancelLabel != null) {
1848 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1849 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001850
1851 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1852 return builder;
1853 }
1854
1855 @Override
1856 public WearableExtender clone() {
1857 WearableExtender that = new WearableExtender();
1858 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001859 that.mInProgressLabel = this.mInProgressLabel;
1860 that.mConfirmLabel = this.mConfirmLabel;
1861 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001862 return that;
1863 }
1864
1865 /**
1866 * Set whether this action is available when the wearable device is not connected to
1867 * a companion device. The user can still trigger this action when the wearable device is
1868 * offline, but a visual hint will indicate that the action may not be available.
1869 * Defaults to true.
1870 */
1871 public WearableExtender setAvailableOffline(boolean availableOffline) {
1872 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1873 return this;
1874 }
1875
1876 /**
1877 * Get whether this action is available when the wearable device is not connected to
1878 * a companion device. The user can still trigger this action when the wearable device is
1879 * offline, but a visual hint will indicate that the action may not be available.
1880 * Defaults to true.
1881 */
1882 public boolean isAvailableOffline() {
1883 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1884 }
1885
1886 private void setFlag(int mask, boolean value) {
1887 if (value) {
1888 mFlags |= mask;
1889 } else {
1890 mFlags &= ~mask;
1891 }
1892 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001893
1894 /**
1895 * Set a label to display while the wearable is preparing to automatically execute the
1896 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1897 *
1898 * @param label the label to display while the action is being prepared to execute
1899 * @return this object for method chaining
1900 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001901 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001902 public WearableExtender setInProgressLabel(CharSequence label) {
1903 mInProgressLabel = label;
1904 return this;
1905 }
1906
1907 /**
1908 * Get the label to display while the wearable is preparing to automatically execute
1909 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1910 *
1911 * @return the label to display while the action is being prepared to execute
1912 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001913 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001914 public CharSequence getInProgressLabel() {
1915 return mInProgressLabel;
1916 }
1917
1918 /**
1919 * Set a label to display to confirm that the action should be executed.
1920 * This is usually an imperative verb like "Send".
1921 *
1922 * @param label the label to confirm the action should be executed
1923 * @return this object for method chaining
1924 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001925 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001926 public WearableExtender setConfirmLabel(CharSequence label) {
1927 mConfirmLabel = label;
1928 return this;
1929 }
1930
1931 /**
1932 * Get the label to display to confirm that the action should be executed.
1933 * This is usually an imperative verb like "Send".
1934 *
1935 * @return the label to confirm the action should be executed
1936 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001937 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001938 public CharSequence getConfirmLabel() {
1939 return mConfirmLabel;
1940 }
1941
1942 /**
1943 * Set a label to display to cancel the action.
1944 * This is usually an imperative verb, like "Cancel".
1945 *
1946 * @param label the label to display to cancel the action
1947 * @return this object for method chaining
1948 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001949 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001950 public WearableExtender setCancelLabel(CharSequence label) {
1951 mCancelLabel = label;
1952 return this;
1953 }
1954
1955 /**
1956 * Get the label to display to cancel the action.
1957 * This is usually an imperative verb like "Cancel".
1958 *
1959 * @return the label to display to cancel the action
1960 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001961 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001962 public CharSequence getCancelLabel() {
1963 return mCancelLabel;
1964 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001965
1966 /**
1967 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1968 * platform that it can generate the appropriate transitions.
1969 * @param hintLaunchesActivity {@code true} if the content intent will launch
1970 * an activity and transitions should be generated, false otherwise.
1971 * @return this object for method chaining
1972 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001973 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001974 boolean hintLaunchesActivity) {
1975 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1976 return this;
1977 }
1978
1979 /**
1980 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1981 * platform that it can generate the appropriate transitions
1982 * @return {@code true} if the content intent will launch an activity and transitions
1983 * should be generated, false otherwise. The default value is {@code false} if this was
1984 * never set.
1985 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001986 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001987 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1988 }
Alex Hills9f087612016-06-07 09:08:59 -04001989
1990 /**
1991 * Set a hint that this Action should be displayed inline.
1992 *
1993 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1994 * otherwise
1995 * @return this object for method chaining
1996 */
1997 public WearableExtender setHintDisplayActionInline(
1998 boolean hintDisplayInline) {
1999 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
2000 return this;
2001 }
2002
2003 /**
2004 * Get a hint that this Action should be displayed inline.
2005 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08002006 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04002007 * otherwise. The default value is {@code false} if this was never set.
2008 */
2009 public boolean getHintDisplayActionInline() {
2010 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
2011 }
Griff Hazen61a9e862014-05-22 16:05:19 -07002012 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002013
2014 /**
2015 * Provides meaning to an {@link Action} that hints at what the associated
2016 * {@link PendingIntent} will do. For example, an {@link Action} with a
2017 * {@link PendingIntent} that replies to a text message notification may have the
2018 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
2019 *
2020 * @hide
2021 */
2022 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
2023 SEMANTIC_ACTION_NONE,
2024 SEMANTIC_ACTION_REPLY,
2025 SEMANTIC_ACTION_MARK_AS_READ,
2026 SEMANTIC_ACTION_MARK_AS_UNREAD,
2027 SEMANTIC_ACTION_DELETE,
2028 SEMANTIC_ACTION_ARCHIVE,
2029 SEMANTIC_ACTION_MUTE,
2030 SEMANTIC_ACTION_UNMUTE,
2031 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08002032 SEMANTIC_ACTION_THUMBS_DOWN,
2033 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00002034 })
2035 @Retention(RetentionPolicy.SOURCE)
2036 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002037 }
2038
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002039 /**
2040 * Array of all {@link Action} structures attached to this notification by
2041 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
2042 * {@link android.service.notification.NotificationListenerService} that provide an alternative
2043 * interface for invoking actions.
2044 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05002045 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002046
2047 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002048 * Replacement version of this notification whose content will be shown
2049 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
2050 * and {@link #VISIBILITY_PUBLIC}.
2051 */
2052 public Notification publicVersion;
2053
2054 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002055 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08002056 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 */
2058 public Notification()
2059 {
2060 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002061 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002062 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064
2065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 * @hide
2067 */
2068 public Notification(Context context, int icon, CharSequence tickerText, long when,
2069 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
2070 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002071 new Builder(context)
2072 .setWhen(when)
2073 .setSmallIcon(icon)
2074 .setTicker(tickerText)
2075 .setContentTitle(contentTitle)
2076 .setContentText(contentText)
2077 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2078 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080
2081 /**
2082 * Constructs a Notification object with the information needed to
2083 * have a status bar icon without the standard expanded view.
2084 *
2085 * @param icon The resource id of the icon to put in the status bar.
2086 * @param tickerText The text that flows by in the status bar when the notification first
2087 * activates.
2088 * @param when The time to show in the time field. In the System.currentTimeMillis
2089 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002090 *
2091 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002093 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 public Notification(int icon, CharSequence tickerText, long when)
2095 {
2096 this.icon = icon;
2097 this.tickerText = tickerText;
2098 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002099 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101
2102 /**
2103 * Unflatten the notification from a parcel.
2104 */
Svet Ganovddb94882016-06-23 19:55:24 -07002105 @SuppressWarnings("unchecked")
2106 public Notification(Parcel parcel) {
2107 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2108 // intents in extras are always written as the last entry.
2109 readFromParcelImpl(parcel);
2110 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002111 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002112 }
2113
2114 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 {
2116 int version = parcel.readInt();
2117
Adrian Roosfb921842017-10-26 14:49:56 +02002118 mWhitelistToken = parcel.readStrongBinder();
2119 if (mWhitelistToken == null) {
2120 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002121 }
2122 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002123 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002126 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002127 if (parcel.readInt() != 0) {
2128 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002129 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2130 icon = mSmallIcon.getResId();
2131 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 number = parcel.readInt();
2134 if (parcel.readInt() != 0) {
2135 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2136 }
2137 if (parcel.readInt() != 0) {
2138 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2139 }
2140 if (parcel.readInt() != 0) {
2141 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2142 }
2143 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002144 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002145 }
2146 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2148 }
Joe Onorato561d3852010-11-20 18:09:34 -08002149 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002150 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 defaults = parcel.readInt();
2153 flags = parcel.readInt();
2154 if (parcel.readInt() != 0) {
2155 sound = Uri.CREATOR.createFromParcel(parcel);
2156 }
2157
2158 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002159 if (parcel.readInt() != 0) {
2160 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 vibrate = parcel.createLongArray();
2163 ledARGB = parcel.readInt();
2164 ledOnMS = parcel.readInt();
2165 ledOffMS = parcel.readInt();
2166 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002167
2168 if (parcel.readInt() != 0) {
2169 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2170 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002171
2172 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002173
John Spurlockfd7f1e02014-03-18 16:41:57 -04002174 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002175
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002176 mGroupKey = parcel.readString();
2177
2178 mSortKey = parcel.readString();
2179
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002180 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002181 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002182
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002183 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2184
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002185 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002186 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2187 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002188
Chris Wren8fd39ec2014-02-27 17:43:26 -05002189 if (parcel.readInt() != 0) {
2190 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2191 }
2192
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002193 visibility = parcel.readInt();
2194
2195 if (parcel.readInt() != 0) {
2196 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2197 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002198
2199 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002200
2201 if (parcel.readInt() != 0) {
2202 mChannelId = parcel.readString();
2203 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002204 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002205
2206 if (parcel.readInt() != 0) {
2207 mShortcutId = parcel.readString();
2208 }
2209
2210 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002211
2212 if (parcel.readInt() != 0) {
2213 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2214 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002215
2216 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 }
2218
Andy Stadler110988c2010-12-03 14:29:16 -08002219 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002220 public Notification clone() {
2221 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002222 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002223 return that;
2224 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002225
Daniel Sandler1a497d32013-04-18 14:52:45 -04002226 /**
2227 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2228 * of this into that.
2229 * @hide
2230 */
2231 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002232 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002233 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002234 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002235 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002236 that.number = this.number;
2237
2238 // PendingIntents are global, so there's no reason (or way) to clone them.
2239 that.contentIntent = this.contentIntent;
2240 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002241 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002242
2243 if (this.tickerText != null) {
2244 that.tickerText = this.tickerText.toString();
2245 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002246 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002247 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002248 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002249 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002250 that.contentView = this.contentView.clone();
2251 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002252 if (heavy && this.mLargeIcon != null) {
2253 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002254 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002255 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002256 that.sound = this.sound; // android.net.Uri is immutable
2257 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002258 if (this.audioAttributes != null) {
2259 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2260 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002261
2262 final long[] vibrate = this.vibrate;
2263 if (vibrate != null) {
2264 final int N = vibrate.length;
2265 final long[] vib = that.vibrate = new long[N];
2266 System.arraycopy(vibrate, 0, vib, 0, N);
2267 }
2268
2269 that.ledARGB = this.ledARGB;
2270 that.ledOnMS = this.ledOnMS;
2271 that.ledOffMS = this.ledOffMS;
2272 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002273
Joe Onorato18e69df2010-05-17 22:26:12 -07002274 that.flags = this.flags;
2275
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002277
John Spurlockfd7f1e02014-03-18 16:41:57 -04002278 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002280 that.mGroupKey = this.mGroupKey;
2281
2282 that.mSortKey = this.mSortKey;
2283
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002284 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002285 try {
2286 that.extras = new Bundle(this.extras);
2287 // will unparcel
2288 that.extras.size();
2289 } catch (BadParcelableException e) {
2290 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2291 that.extras = null;
2292 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002293 }
2294
Felipe Lemedd85da62016-06-28 11:29:54 -07002295 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2296 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002297 }
2298
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002299 if (this.actions != null) {
2300 that.actions = new Action[this.actions.length];
2301 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002302 if ( this.actions[i] != null) {
2303 that.actions[i] = this.actions[i].clone();
2304 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002305 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002306 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002307
Daniel Sandler1a497d32013-04-18 14:52:45 -04002308 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002309 that.bigContentView = this.bigContentView.clone();
2310 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002311
Chris Wren8fd39ec2014-02-27 17:43:26 -05002312 if (heavy && this.headsUpContentView != null) {
2313 that.headsUpContentView = this.headsUpContentView.clone();
2314 }
2315
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002316 that.visibility = this.visibility;
2317
2318 if (this.publicVersion != null) {
2319 that.publicVersion = new Notification();
2320 this.publicVersion.cloneInto(that.publicVersion, heavy);
2321 }
2322
Dan Sandler26e81cf2014-05-06 10:01:27 -04002323 that.color = this.color;
2324
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002325 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002326 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002327 that.mShortcutId = this.mShortcutId;
2328 that.mBadgeIcon = this.mBadgeIcon;
2329 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002330 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002331
Daniel Sandler1a497d32013-04-18 14:52:45 -04002332 if (!heavy) {
2333 that.lightenPayload(); // will clean out extras
2334 }
2335 }
2336
2337 /**
Jeff Sharkey23b31182018-04-18 21:32:12 -06002338 * Note all {@link Uri} that are referenced internally, with the expectation
2339 * that Uri permission grants will need to be issued to ensure the recipient
2340 * of this object is able to render its contents.
2341 *
2342 * @hide
2343 */
2344 public void visitUris(@NonNull Consumer<Uri> visitor) {
2345 visitor.accept(sound);
2346
2347 if (tickerView != null) tickerView.visitUris(visitor);
2348 if (contentView != null) contentView.visitUris(visitor);
2349 if (bigContentView != null) bigContentView.visitUris(visitor);
2350 if (headsUpContentView != null) headsUpContentView.visitUris(visitor);
2351
2352 if (extras != null) {
2353 visitor.accept(extras.getParcelable(EXTRA_AUDIO_CONTENTS_URI));
Robin Leed107af62018-04-27 13:55:56 +02002354 if (extras.containsKey(EXTRA_BACKGROUND_IMAGE_URI)) {
2355 visitor.accept(Uri.parse(extras.getString(EXTRA_BACKGROUND_IMAGE_URI)));
2356 }
Jeff Sharkey23b31182018-04-18 21:32:12 -06002357 }
2358
2359 if (MessagingStyle.class.equals(getNotificationStyle()) && extras != null) {
2360 final Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
2361 if (!ArrayUtils.isEmpty(messages)) {
2362 for (MessagingStyle.Message message : MessagingStyle.Message
2363 .getMessagesFromBundleArray(messages)) {
2364 visitor.accept(message.getDataUri());
2365 }
2366 }
2367
2368 final Parcelable[] historic = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
2369 if (!ArrayUtils.isEmpty(historic)) {
2370 for (MessagingStyle.Message message : MessagingStyle.Message
2371 .getMessagesFromBundleArray(historic)) {
2372 visitor.accept(message.getDataUri());
2373 }
2374 }
2375 }
2376 }
2377
2378 /**
Daniel Sandler1a497d32013-04-18 14:52:45 -04002379 * Removes heavyweight parts of the Notification object for archival or for sending to
2380 * listeners when the full contents are not necessary.
2381 * @hide
2382 */
2383 public final void lightenPayload() {
2384 tickerView = null;
2385 contentView = null;
2386 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002387 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002388 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002389 if (extras != null && !extras.isEmpty()) {
2390 final Set<String> keyset = extras.keySet();
2391 final int N = keyset.size();
2392 final String[] keys = keyset.toArray(new String[N]);
2393 for (int i=0; i<N; i++) {
2394 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002395 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2396 continue;
2397 }
Dan Sandler50128532015-12-08 15:42:41 -05002398 final Object obj = extras.get(key);
2399 if (obj != null &&
2400 ( obj instanceof Parcelable
2401 || obj instanceof Parcelable[]
2402 || obj instanceof SparseArray
2403 || obj instanceof ArrayList)) {
2404 extras.remove(key);
2405 }
2406 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002407 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002408 }
2409
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002410 /**
2411 * Make sure this CharSequence is safe to put into a bundle, which basically
2412 * means it had better not be some custom Parcelable implementation.
2413 * @hide
2414 */
2415 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002416 if (cs == null) return cs;
2417 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2418 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2419 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002420 if (cs instanceof Parcelable) {
2421 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2422 + " instance is a custom Parcelable and not allowed in Notification");
2423 return cs.toString();
2424 }
Selim Cinek60a54252016-02-26 17:03:25 -08002425 return removeTextSizeSpans(cs);
2426 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002427
Selim Cinek60a54252016-02-26 17:03:25 -08002428 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2429 if (charSequence instanceof Spanned) {
2430 Spanned ss = (Spanned) charSequence;
2431 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2432 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2433 for (Object span : spans) {
2434 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002435 if (resultSpan instanceof CharacterStyle) {
2436 resultSpan = ((CharacterStyle) span).getUnderlying();
2437 }
2438 if (resultSpan instanceof TextAppearanceSpan) {
2439 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002440 resultSpan = new TextAppearanceSpan(
2441 originalSpan.getFamily(),
2442 originalSpan.getTextStyle(),
2443 -1,
2444 originalSpan.getTextColor(),
2445 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002446 } else if (resultSpan instanceof RelativeSizeSpan
2447 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002448 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002449 } else {
2450 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002451 }
2452 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2453 ss.getSpanFlags(span));
2454 }
2455 return builder;
2456 }
2457 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002458 }
2459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 public int describeContents() {
2461 return 0;
2462 }
2463
2464 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002465 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 */
Svet Ganovddb94882016-06-23 19:55:24 -07002467 public void writeToParcel(Parcel parcel, int flags) {
2468 // We need to mark all pending intents getting into the notification
2469 // system as being put there to later allow the notification ranker
2470 // to launch them and by doing so add the app to the battery saver white
2471 // list for a short period of time. The problem is that the system
2472 // cannot look into the extras as there may be parcelables there that
2473 // the platform does not know how to handle. To go around that we have
2474 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002475 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002476 if (collectPendingIntents) {
2477 PendingIntent.setOnMarshaledListener(
2478 (PendingIntent intent, Parcel out, int outFlags) -> {
2479 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002480 if (allPendingIntents == null) {
2481 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002482 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002483 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002484 }
2485 });
2486 }
2487 try {
2488 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002489 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002490 writeToParcelImpl(parcel, flags);
2491 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002492 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002493 } finally {
2494 if (collectPendingIntents) {
2495 PendingIntent.setOnMarshaledListener(null);
2496 }
2497 }
2498 }
2499
2500 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 parcel.writeInt(1);
2502
Adrian Roosfb921842017-10-26 14:49:56 +02002503 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002505 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002506 if (mSmallIcon == null && icon != 0) {
2507 // you snuck an icon in here without using the builder; let's try to keep it
2508 mSmallIcon = Icon.createWithResource("", icon);
2509 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002510 if (mSmallIcon != null) {
2511 parcel.writeInt(1);
2512 mSmallIcon.writeToParcel(parcel, 0);
2513 } else {
2514 parcel.writeInt(0);
2515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 parcel.writeInt(number);
2517 if (contentIntent != null) {
2518 parcel.writeInt(1);
2519 contentIntent.writeToParcel(parcel, 0);
2520 } else {
2521 parcel.writeInt(0);
2522 }
2523 if (deleteIntent != null) {
2524 parcel.writeInt(1);
2525 deleteIntent.writeToParcel(parcel, 0);
2526 } else {
2527 parcel.writeInt(0);
2528 }
2529 if (tickerText != null) {
2530 parcel.writeInt(1);
2531 TextUtils.writeToParcel(tickerText, parcel, flags);
2532 } else {
2533 parcel.writeInt(0);
2534 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002535 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002536 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002537 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002538 } else {
2539 parcel.writeInt(0);
2540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 if (contentView != null) {
2542 parcel.writeInt(1);
2543 contentView.writeToParcel(parcel, 0);
2544 } else {
2545 parcel.writeInt(0);
2546 }
Selim Cinek279fa862016-06-14 10:57:25 -07002547 if (mLargeIcon == null && largeIcon != null) {
2548 // you snuck an icon in here without using the builder; let's try to keep it
2549 mLargeIcon = Icon.createWithBitmap(largeIcon);
2550 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002551 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002552 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002553 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002554 } else {
2555 parcel.writeInt(0);
2556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557
2558 parcel.writeInt(defaults);
2559 parcel.writeInt(this.flags);
2560
2561 if (sound != null) {
2562 parcel.writeInt(1);
2563 sound.writeToParcel(parcel, 0);
2564 } else {
2565 parcel.writeInt(0);
2566 }
2567 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002568
2569 if (audioAttributes != null) {
2570 parcel.writeInt(1);
2571 audioAttributes.writeToParcel(parcel, 0);
2572 } else {
2573 parcel.writeInt(0);
2574 }
2575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 parcel.writeLongArray(vibrate);
2577 parcel.writeInt(ledARGB);
2578 parcel.writeInt(ledOnMS);
2579 parcel.writeInt(ledOffMS);
2580 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002581
2582 if (fullScreenIntent != null) {
2583 parcel.writeInt(1);
2584 fullScreenIntent.writeToParcel(parcel, 0);
2585 } else {
2586 parcel.writeInt(0);
2587 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002588
2589 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002590
John Spurlockfd7f1e02014-03-18 16:41:57 -04002591 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002592
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002593 parcel.writeString(mGroupKey);
2594
2595 parcel.writeString(mSortKey);
2596
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002597 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002598
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002599 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002600
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002601 if (bigContentView != null) {
2602 parcel.writeInt(1);
2603 bigContentView.writeToParcel(parcel, 0);
2604 } else {
2605 parcel.writeInt(0);
2606 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002607
Chris Wren8fd39ec2014-02-27 17:43:26 -05002608 if (headsUpContentView != null) {
2609 parcel.writeInt(1);
2610 headsUpContentView.writeToParcel(parcel, 0);
2611 } else {
2612 parcel.writeInt(0);
2613 }
2614
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002615 parcel.writeInt(visibility);
2616
2617 if (publicVersion != null) {
2618 parcel.writeInt(1);
2619 publicVersion.writeToParcel(parcel, 0);
2620 } else {
2621 parcel.writeInt(0);
2622 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002623
2624 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002625
2626 if (mChannelId != null) {
2627 parcel.writeInt(1);
2628 parcel.writeString(mChannelId);
2629 } else {
2630 parcel.writeInt(0);
2631 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002632 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002633
2634 if (mShortcutId != null) {
2635 parcel.writeInt(1);
2636 parcel.writeString(mShortcutId);
2637 } else {
2638 parcel.writeInt(0);
2639 }
2640
2641 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002642
2643 if (mSettingsText != null) {
2644 parcel.writeInt(1);
2645 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2646 } else {
2647 parcel.writeInt(0);
2648 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002649
2650 parcel.writeInt(mGroupAlertBehavior);
Julia Reynoldsfc640012018-02-21 12:25:27 -05002651
2652 // mUsesStandardHeader is not written because it should be recomputed in listeners
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 }
2654
2655 /**
2656 * Parcelable.Creator that instantiates Notification objects
2657 */
2658 public static final Parcelable.Creator<Notification> CREATOR
2659 = new Parcelable.Creator<Notification>()
2660 {
2661 public Notification createFromParcel(Parcel parcel)
2662 {
2663 return new Notification(parcel);
2664 }
2665
2666 public Notification[] newArray(int size)
2667 {
2668 return new Notification[size];
2669 }
2670 };
2671
2672 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05002673 * @hide
2674 */
2675 public static boolean areActionsVisiblyDifferent(Notification first, Notification second) {
2676 Notification.Action[] firstAs = first.actions;
2677 Notification.Action[] secondAs = second.actions;
2678 if (firstAs == null && secondAs != null || firstAs != null && secondAs == null) {
2679 return true;
2680 }
2681 if (firstAs != null && secondAs != null) {
2682 if (firstAs.length != secondAs.length) {
2683 return true;
2684 }
2685 for (int i = 0; i < firstAs.length; i++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002686 if (!Objects.equals(String.valueOf(firstAs[i].title),
2687 String.valueOf(secondAs[i].title))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002688 return true;
2689 }
2690 RemoteInput[] firstRs = firstAs[i].getRemoteInputs();
2691 RemoteInput[] secondRs = secondAs[i].getRemoteInputs();
2692 if (firstRs == null) {
2693 firstRs = new RemoteInput[0];
2694 }
2695 if (secondRs == null) {
2696 secondRs = new RemoteInput[0];
2697 }
2698 if (firstRs.length != secondRs.length) {
2699 return true;
2700 }
2701 for (int j = 0; j < firstRs.length; j++) {
Julia Reynoldsa4fb9da2018-06-04 12:27:58 -04002702 if (!Objects.equals(String.valueOf(firstRs[j].getLabel()),
2703 String.valueOf(secondRs[j].getLabel()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05002704 return true;
2705 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05002706 }
2707 }
2708 }
2709 return false;
2710 }
2711
2712 /**
2713 * @hide
2714 */
2715 public static boolean areStyledNotificationsVisiblyDifferent(Builder first, Builder second) {
2716 if (first.getStyle() == null) {
2717 return second.getStyle() != null;
2718 }
2719 if (second.getStyle() == null) {
2720 return true;
2721 }
2722 return first.getStyle().areNotificationsVisiblyDifferent(second.getStyle());
2723 }
2724
2725 /**
2726 * @hide
2727 */
2728 public static boolean areRemoteViewsChanged(Builder first, Builder second) {
Julia Reynoldse5c60452018-04-30 14:41:36 -04002729 if (!Objects.equals(first.usesStandardHeader(), second.usesStandardHeader())) {
2730 return true;
2731 }
2732
2733 if (areRemoteViewsChanged(first.mN.contentView, second.mN.contentView)) {
2734 return true;
2735 }
2736 if (areRemoteViewsChanged(first.mN.bigContentView, second.mN.bigContentView)) {
2737 return true;
2738 }
2739 if (areRemoteViewsChanged(first.mN.headsUpContentView, second.mN.headsUpContentView)) {
2740 return true;
2741 }
2742
2743 return false;
2744 }
2745
2746 private static boolean areRemoteViewsChanged(RemoteViews first, RemoteViews second) {
2747 if (first == null && second == null) {
2748 return false;
2749 }
2750 if (first == null && second != null || first != null && second == null) {
2751 return true;
2752 }
2753
2754 if (!Objects.equals(first.getLayoutId(), second.getLayoutId())) {
2755 return true;
2756 }
2757
2758 if (!Objects.equals(first.getSequenceNumber(), second.getSequenceNumber())) {
2759 return true;
2760 }
2761
2762 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05002763 }
2764
2765 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002766 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2767 * <p>
2768 * For backwards compatibility {@code extras} holds some references to "real" member data such
2769 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2770 * fine as long as the object stays in one process.
2771 * <p>
2772 * However, once the notification goes into a parcel each reference gets marshalled separately,
2773 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2774 */
2775 private void fixDuplicateExtras() {
2776 if (extras != null) {
Robin Leead7e72a2017-12-04 15:45:46 +01002777 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2778 }
2779 }
2780
2781 /**
2782 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2783 * separate object, replace it with the field's version to avoid holding duplicate copies.
2784 */
2785 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2786 if (original != null && extras.getParcelable(extraName) != null) {
2787 extras.putParcelable(extraName, original);
2788 }
2789 }
2790
2791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2793 * layout.
2794 *
2795 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2796 * in the view.</p>
2797 * @param context The context for your application / activity.
2798 * @param contentTitle The title that goes in the expanded entry.
2799 * @param contentText The text that goes in the expanded entry.
2800 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2801 * If this is an activity, it must include the
2802 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002803 * that you take care of task management as described in the
2804 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2805 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002806 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002807 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002808 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002810 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 public void setLatestEventInfo(Context context,
2812 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002813 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2814 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2815 new Throwable());
2816 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002817
Selim Cinek4ac6f602016-06-13 15:47:03 -07002818 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2819 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2820 }
2821
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002822 // ensure that any information already set directly is preserved
2823 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002824
2825 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002827 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
2829 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002830 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002832 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833
2834 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
2836
Julia Reynoldsda303542015-11-23 14:00:20 -05002837 /**
2838 * @hide
2839 */
2840 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002841 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002842 }
2843
2844 /**
2845 * @hide
2846 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002847 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002848 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002849 }
2850
Yi Jin6b514142017-10-30 14:54:12 -07002851 /**
2852 * @hide
2853 */
2854 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2855 long token = proto.start(fieldId);
2856 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2857 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2858 proto.write(NotificationProto.FLAGS, this.flags);
2859 proto.write(NotificationProto.COLOR, this.color);
2860 proto.write(NotificationProto.CATEGORY, this.category);
2861 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2862 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2863 if (this.actions != null) {
2864 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2865 }
2866 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2867 proto.write(NotificationProto.VISIBILITY, this.visibility);
2868 }
2869 if (publicVersion != null) {
2870 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2871 }
2872 proto.end(token);
2873 }
2874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 @Override
2876 public String toString() {
2877 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002878 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002879 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002880 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002881 sb.append(priority);
2882 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002883 if (contentView != null) {
2884 sb.append(contentView.getPackage());
2885 sb.append("/0x");
2886 sb.append(Integer.toHexString(contentView.getLayoutId()));
2887 } else {
2888 sb.append("null");
2889 }
2890 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002891 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2892 sb.append("default");
2893 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 int N = this.vibrate.length-1;
2895 sb.append("[");
2896 for (int i=0; i<N; i++) {
2897 sb.append(this.vibrate[i]);
2898 sb.append(',');
2899 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002900 if (N != -1) {
2901 sb.append(this.vibrate[N]);
2902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 } else {
2905 sb.append("null");
2906 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002907 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002908 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002910 } else if (this.sound != null) {
2911 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 } else {
2913 sb.append("null");
2914 }
Chris Wren365b6d32015-07-16 10:39:26 -04002915 if (this.tickerText != null) {
2916 sb.append(" tick");
2917 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002918 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002920 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002921 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002922 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002923 if (this.category != null) {
2924 sb.append(" category=");
2925 sb.append(this.category);
2926 }
2927 if (this.mGroupKey != null) {
2928 sb.append(" groupKey=");
2929 sb.append(this.mGroupKey);
2930 }
2931 if (this.mSortKey != null) {
2932 sb.append(" sortKey=");
2933 sb.append(this.mSortKey);
2934 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002935 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002936 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002937 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002938 }
2939 sb.append(" vis=");
2940 sb.append(visibilityToString(this.visibility));
2941 if (this.publicVersion != null) {
2942 sb.append(" publicVersion=");
2943 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002944 }
2945 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 return sb.toString();
2947 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002948
Dan Sandler1b718782014-07-18 12:43:45 -04002949 /**
2950 * {@hide}
2951 */
2952 public static String visibilityToString(int vis) {
2953 switch (vis) {
2954 case VISIBILITY_PRIVATE:
2955 return "PRIVATE";
2956 case VISIBILITY_PUBLIC:
2957 return "PUBLIC";
2958 case VISIBILITY_SECRET:
2959 return "SECRET";
2960 default:
2961 return "UNKNOWN(" + String.valueOf(vis) + ")";
2962 }
2963 }
2964
Joe Onoratocb109a02011-01-18 17:57:41 -08002965 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002966 * {@hide}
2967 */
2968 public static String priorityToString(@Priority int pri) {
2969 switch (pri) {
2970 case PRIORITY_MIN:
2971 return "MIN";
2972 case PRIORITY_LOW:
2973 return "LOW";
2974 case PRIORITY_DEFAULT:
2975 return "DEFAULT";
2976 case PRIORITY_HIGH:
2977 return "HIGH";
2978 case PRIORITY_MAX:
2979 return "MAX";
2980 default:
2981 return "UNKNOWN(" + String.valueOf(pri) + ")";
2982 }
2983 }
2984
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002985 /**
2986 * @hide
2987 */
2988 public boolean hasCompletedProgress() {
2989 // not a progress notification; can't be complete
2990 if (!extras.containsKey(EXTRA_PROGRESS)
2991 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2992 return false;
2993 }
2994 // many apps use max 0 for 'indeterminate'; not complete
2995 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2996 return false;
2997 }
2998 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2999 }
3000
Jeff Sharkey000ce802017-04-29 13:13:27 -06003001 /** @removed */
3002 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05003003 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003004 return mChannelId;
3005 }
3006
3007 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003008 * Returns the id of the channel this notification posts to.
3009 */
3010 public String getChannelId() {
3011 return mChannelId;
3012 }
3013
Jeff Sharkey000ce802017-04-29 13:13:27 -06003014 /** @removed */
3015 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05003016 public long getTimeout() {
3017 return mTimeout;
3018 }
3019
3020 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003021 * Returns the duration from posting after which this notification should be canceled by the
3022 * system, if it's not canceled already.
3023 */
3024 public long getTimeoutAfter() {
3025 return mTimeout;
3026 }
3027
3028 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003029 * Returns what icon should be shown for this notification if it is being displayed in a
3030 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
3031 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
3032 */
3033 public int getBadgeIconType() {
3034 return mBadgeIcon;
3035 }
3036
3037 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003038 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04003039 *
3040 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
3041 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003042 */
3043 public String getShortcutId() {
3044 return mShortcutId;
3045 }
3046
Julia Reynolds3aedded2017-03-31 14:42:09 -04003047
3048 /**
3049 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
3050 */
3051 public CharSequence getSettingsText() {
3052 return mSettingsText;
3053 }
3054
Julia Reynolds13d898c2017-02-02 12:22:05 -05003055 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003056 * Returns which type of notifications in a group are responsible for audibly alerting the
3057 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
3058 * {@link #GROUP_ALERT_SUMMARY}.
3059 */
3060 public @GroupAlertBehavior int getGroupAlertBehavior() {
3061 return mGroupAlertBehavior;
3062 }
3063
3064 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003065 * The small icon representing this notification in the status bar and content view.
3066 *
3067 * @return the small icon representing this notification.
3068 *
3069 * @see Builder#getSmallIcon()
3070 * @see Builder#setSmallIcon(Icon)
3071 */
3072 public Icon getSmallIcon() {
3073 return mSmallIcon;
3074 }
3075
3076 /**
3077 * Used when notifying to clean up legacy small icons.
3078 * @hide
3079 */
3080 public void setSmallIcon(Icon icon) {
3081 mSmallIcon = icon;
3082 }
3083
3084 /**
3085 * The large icon shown in this notification's content view.
3086 * @see Builder#getLargeIcon()
3087 * @see Builder#setLargeIcon(Icon)
3088 */
3089 public Icon getLargeIcon() {
3090 return mLargeIcon;
3091 }
3092
3093 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02003094 * @hide
3095 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02003096 public boolean isGroupSummary() {
3097 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
3098 }
3099
3100 /**
3101 * @hide
3102 */
3103 public boolean isGroupChild() {
3104 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
3105 }
3106
3107 /**
Julia Reynolds30203152017-05-26 13:36:31 -04003108 * @hide
3109 */
3110 public boolean suppressAlertingDueToGrouping() {
3111 if (isGroupSummary()
3112 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
3113 return true;
3114 } else if (isGroupChild()
3115 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
3116 return true;
3117 }
3118 return false;
3119 }
3120
3121 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003122 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08003123 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003124 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07003125 * content views using the platform's notification layout template. If your app supports
3126 * versions of Android as old as API level 4, you can instead use
3127 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
3128 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
3129 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08003130 *
Scott Main183bf112012-08-13 19:12:13 -07003131 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08003132 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003133 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07003134 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003135 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3136 * .setContentText(subject)
3137 * .setSmallIcon(R.drawable.new_mail)
3138 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003139 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003140 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08003141 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003142 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003143 /**
3144 * @hide
3145 */
3146 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
3147 "android.rebuild.contentViewActionCount";
3148 /**
3149 * @hide
3150 */
3151 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
3152 = "android.rebuild.bigViewActionCount";
3153 /**
3154 * @hide
3155 */
3156 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
3157 = "android.rebuild.hudViewActionCount";
3158
Selim Cinek6743c0b2017-01-18 18:24:01 -08003159 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
3160 SystemProperties.getBoolean("notifications.only_title", true);
3161
Selim Cinek389edcd2017-05-11 19:16:44 -07003162 /**
3163 * The lightness difference that has to be added to the primary text color to obtain the
3164 * secondary text color when the background is light.
3165 */
3166 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
3167
3168 /**
3169 * The lightness difference that has to be added to the primary text color to obtain the
3170 * secondary text color when the background is dark.
3171 * A bit less then the above value, since it looks better on dark backgrounds.
3172 */
3173 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
3174
Joe Onorato46439ce2010-11-19 13:56:21 -08003175 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003176 private Notification mN;
3177 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003178 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003179 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08003180 private ArrayList<Person> mPersonList = new ArrayList<>();
Lucas Dupina291d192018-06-07 13:59:42 -07003181 private ContrastColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003182 private boolean mIsLegacy;
3183 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02003184
3185 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08003186 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
3187 */
3188 private int mCachedContrastColor = COLOR_INVALID;
3189 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08003190 /**
Selim Cinek4717d862018-04-19 09:19:15 +08003191 * Caches a ambient version of {@link #mCachedAmbientColorIsFor}.
Adrian Roos487374f2017-01-11 15:48:14 -08003192 */
3193 private int mCachedAmbientColor = COLOR_INVALID;
3194 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Selim Cinek4717d862018-04-19 09:19:15 +08003195 /**
3196 * A neutral color color that can be used for icons.
3197 */
3198 private int mNeutralColor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08003199
3200 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08003201 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
3202 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
3203 */
3204 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003205 private int mTextColorsAreForBackground = COLOR_INVALID;
3206 private int mPrimaryTextColor = COLOR_INVALID;
3207 private int mSecondaryTextColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07003208 private int mBackgroundColor = COLOR_INVALID;
3209 private int mForegroundColor = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003210 /**
3211 * A temporary location where actions are stored. If != null the view originally has action
3212 * but doesn't have any for this inflation.
3213 */
3214 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003215 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003216
Anthony Chenad4d1582017-04-10 16:07:58 -07003217 private boolean mTintActionButtons;
3218 private boolean mInNightMode;
3219
Adrian Roos70d7aa32017-01-11 15:39:06 -08003220 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003221 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003223 * @param context
3224 * A {@link Context} that will be used by the Builder to construct the
3225 * RemoteViews. The Context will not be held past the lifetime of this Builder
3226 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003227 * @param channelId
3228 * The constructed Notification will be posted on this
3229 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3230 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003231 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003232 public Builder(Context context, String channelId) {
3233 this(context, (Notification) null);
3234 mN.mChannelId = channelId;
3235 }
3236
3237 /**
3238 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3239 * instead. All posted Notifications must specify a NotificationChannel Id.
3240 */
3241 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003242 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003243 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003244 }
3245
Joe Onoratocb109a02011-01-18 17:57:41 -08003246 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003247 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003248 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003249 public Builder(Context context, Notification toAdopt) {
3250 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003251 Resources res = mContext.getResources();
3252 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3253
3254 if (res.getBoolean(R.bool.config_enableNightMode)) {
3255 Configuration currentConfig = res.getConfiguration();
3256 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3257 == Configuration.UI_MODE_NIGHT_YES;
3258 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003259
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003260 if (toAdopt == null) {
3261 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003262 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3263 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3264 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003265 mN.priority = PRIORITY_DEFAULT;
3266 mN.visibility = VISIBILITY_PRIVATE;
3267 } else {
3268 mN = toAdopt;
3269 if (mN.actions != null) {
3270 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003271 }
3272
Selim Cineke7238dd2017-12-14 17:48:32 -08003273 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3274 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3275 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003276 }
3277
Selim Cinek4ac6f602016-06-13 15:47:03 -07003278 if (mN.getSmallIcon() == null && mN.icon != 0) {
3279 setSmallIcon(mN.icon);
3280 }
3281
3282 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3283 setLargeIcon(mN.largeIcon);
3284 }
3285
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003286 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3287 if (!TextUtils.isEmpty(templateClass)) {
3288 final Class<? extends Style> styleClass
3289 = getNotificationStyleClass(templateClass);
3290 if (styleClass == null) {
3291 Log.d(TAG, "Unknown style class: " + templateClass);
3292 } else {
3293 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003294 final Constructor<? extends Style> ctor =
3295 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003296 ctor.setAccessible(true);
3297 final Style style = ctor.newInstance();
3298 style.restoreFromExtras(mN.extras);
3299
3300 if (style != null) {
3301 setStyle(style);
3302 }
3303 } catch (Throwable t) {
3304 Log.e(TAG, "Could not create Style", t);
3305 }
3306 }
3307 }
3308
3309 }
3310 }
3311
Lucas Dupina291d192018-06-07 13:59:42 -07003312 private ContrastColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003313 if (mColorUtil == null) {
Lucas Dupina291d192018-06-07 13:59:42 -07003314 mColorUtil = ContrastColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003315 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003316 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003317 }
3318
3319 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003320 * If this notification is duplicative of a Launcher shortcut, sets the
3321 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3322 * the shortcut.
3323 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003324 * This field will be ignored by Launchers that don't support badging, don't show
3325 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003326 *
3327 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3328 * supersedes
3329 */
3330 public Builder setShortcutId(String shortcutId) {
3331 mN.mShortcutId = shortcutId;
3332 return this;
3333 }
3334
3335 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003336 * Sets which icon to display as a badge for this notification.
3337 *
3338 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3339 * {@link #BADGE_ICON_LARGE}.
3340 *
3341 * Note: This value might be ignored, for launchers that don't support badge icons.
3342 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003343 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003344 mN.mBadgeIcon = icon;
3345 return this;
3346 }
3347
3348 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003349 * Sets the group alert behavior for this notification. Use this method to mute this
3350 * notification if alerts for this notification's group should be handled by a different
3351 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003352 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3353 * mute. For example, if you want only the summary of your group to make noise, all
3354 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003355 *
3356 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3357 */
3358 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3359 mN.mGroupAlertBehavior = groupAlertBehavior;
3360 return this;
3361 }
3362
Jeff Sharkey000ce802017-04-29 13:13:27 -06003363 /** @removed */
3364 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003365 public Builder setChannel(String channelId) {
3366 mN.mChannelId = channelId;
3367 return this;
3368 }
3369
3370 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003371 * Specifies the channel the notification should be delivered on.
3372 */
3373 public Builder setChannelId(String channelId) {
3374 mN.mChannelId = channelId;
3375 return this;
3376 }
3377
Jeff Sharkey000ce802017-04-29 13:13:27 -06003378 /** @removed */
3379 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003380 public Builder setTimeout(long durationMs) {
3381 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003382 return this;
3383 }
3384
3385 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003386 * Specifies a duration in milliseconds after which this notification should be canceled,
3387 * if it is not already canceled.
3388 */
3389 public Builder setTimeoutAfter(long durationMs) {
3390 mN.mTimeout = durationMs;
3391 return this;
3392 }
3393
3394 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003395 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003396 *
3397 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3398 * shown anymore by default and must be opted into by using
3399 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003400 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003401 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003402 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003403 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003404 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003405 return this;
3406 }
3407
Joe Onoratocb109a02011-01-18 17:57:41 -08003408 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003409 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003410 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003411 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3412 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003413 */
3414 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003416 return this;
3417 }
3418
3419 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003420 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003421 *
3422 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003423 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003424 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003425 * Useful when showing an elapsed time (like an ongoing phone call).
3426 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003427 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003428 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003429 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003430 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003431 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003432 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003433 */
3434 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003435 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003436 return this;
3437 }
3438
3439 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003440 * Sets the Chronometer to count down instead of counting up.
3441 *
3442 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3443 * If it isn't set the chronometer will count up.
3444 *
3445 * @see #setUsesChronometer(boolean)
3446 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003447 public Builder setChronometerCountDown(boolean countDown) {
3448 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003449 return this;
3450 }
3451
3452 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003453 * Set the small icon resource, which will be used to represent the notification in the
3454 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003455 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003456
3457 * The platform template for the expanded view will draw this icon in the left, unless a
3458 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3459 * icon will be moved to the right-hand side.
3460 *
3461
3462 * @param icon
3463 * A resource ID in the application's package of the drawable to use.
3464 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003465 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003466 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003467 return setSmallIcon(icon != 0
3468 ? Icon.createWithResource(mContext, icon)
3469 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003470 }
3471
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 /**
3473 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3474 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3475 * LevelListDrawable}.
3476 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003477 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003478 * @param level The level to use for the icon.
3479 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003480 * @see Notification#icon
3481 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003482 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003483 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003484 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003485 return setSmallIcon(icon);
3486 }
3487
3488 /**
3489 * Set the small icon, which will be used to represent the notification in the
3490 * status bar and content view (unless overriden there by a
3491 * {@link #setLargeIcon(Bitmap) large icon}).
3492 *
3493 * @param icon An Icon object to use.
3494 * @see Notification#icon
3495 */
3496 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 mN.setSmallIcon(icon);
3498 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3499 mN.icon = icon.getResId();
3500 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003501 return this;
3502 }
3503
Joe Onoratocb109a02011-01-18 17:57:41 -08003504 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003507 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003508 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003509 return this;
3510 }
3511
Joe Onoratocb109a02011-01-18 17:57:41 -08003512 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003513 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003514 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003515 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003516 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003517 return this;
3518 }
3519
Joe Onoratocb109a02011-01-18 17:57:41 -08003520 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003521 * This provides some additional information that is displayed in the notification. No
3522 * guarantees are given where exactly it is displayed.
3523 *
3524 * <p>This information should only be provided if it provides an essential
3525 * benefit to the understanding of the notification. The more text you provide the
3526 * less readable it becomes. For example, an email client should only provide the account
3527 * name here if more than one email account has been added.</p>
3528 *
3529 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3530 * notification header area.
3531 *
3532 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3533 * this will be shown in the third line of text in the platform notification template.
3534 * You should not be using {@link #setProgress(int, int, boolean)} at the
3535 * same time on those versions; they occupy the same place.
3536 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003537 */
3538 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003539 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003540 return this;
3541 }
3542
3543 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003544 * Provides text that will appear as a link to your application's settings.
3545 *
3546 * <p>This text does not appear within notification {@link Style templates} but may
3547 * appear when the user uses an affordance to learn more about the notification.
3548 * Additionally, this text will not appear unless you provide a valid link target by
3549 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3550 *
3551 * <p>This text is meant to be concise description about what the user can customize
3552 * when they click on this link. The recommended maximum length is 40 characters.
3553 * @param text
3554 * @return
3555 */
3556 public Builder setSettingsText(CharSequence text) {
3557 mN.mSettingsText = safeCharSequence(text);
3558 return this;
3559 }
3560
3561 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003562 * Set the remote input history.
3563 *
3564 * This should be set to the most recent inputs that have been sent
3565 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3566 * longer relevant (e.g. for chat notifications once the other party has responded).
3567 *
3568 * The most recent input must be stored at the 0 index, the second most recent at the
3569 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3570 * and how much of each individual input is shown.
3571 *
3572 * <p>Note: The reply text will only be shown on notifications that have least one action
3573 * with a {@code RemoteInput}.</p>
3574 */
3575 public Builder setRemoteInputHistory(CharSequence[] text) {
3576 if (text == null) {
3577 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3578 } else {
3579 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3580 CharSequence[] safe = new CharSequence[N];
3581 for (int i = 0; i < N; i++) {
3582 safe[i] = safeCharSequence(text[i]);
3583 }
3584 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3585 }
3586 return this;
3587 }
3588
3589 /**
Kenny Guya0f6de82018-04-06 16:20:16 +01003590 * Sets whether remote history entries view should have a spinner.
3591 * @hide
3592 */
3593 public Builder setShowRemoteInputSpinner(boolean showSpinner) {
3594 mN.extras.putBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER, showSpinner);
3595 return this;
3596 }
3597
3598 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +01003599 * Sets whether smart reply buttons should be hidden.
3600 * @hide
3601 */
3602 public Builder setHideSmartReplies(boolean hideSmartReplies) {
3603 mN.extras.putBoolean(EXTRA_HIDE_SMART_REPLIES, hideSmartReplies);
3604 return this;
3605 }
3606
3607 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003608 * Sets the number of items this notification represents. May be displayed as a badge count
3609 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003610 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003611 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003612 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003613 return this;
3614 }
3615
Joe Onoratocb109a02011-01-18 17:57:41 -08003616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003617 * A small piece of additional information pertaining to this notification.
3618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003619 * The platform template will draw this on the last line of the notification, at the far
3620 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003621 *
3622 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3623 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3624 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003625 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003626 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003627 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003628 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003629 return this;
3630 }
3631
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003633 * Set the progress this notification represents.
3634 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003635 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003636 */
3637 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 mN.extras.putInt(EXTRA_PROGRESS, progress);
3639 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3640 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003641 return this;
3642 }
3643
3644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003645 * Supply a custom RemoteViews to use instead of the platform template.
3646 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003647 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003648 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003649 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003650 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003651 return setCustomContentView(views);
3652 }
3653
3654 /**
3655 * Supply custom RemoteViews to use instead of the platform template.
3656 *
3657 * This will override the layout that would otherwise be constructed by this Builder
3658 * object.
3659 */
3660 public Builder setCustomContentView(RemoteViews contentView) {
3661 mN.contentView = contentView;
3662 return this;
3663 }
3664
3665 /**
3666 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3667 *
3668 * This will override the expanded layout that would otherwise be constructed by this
3669 * Builder object.
3670 */
3671 public Builder setCustomBigContentView(RemoteViews contentView) {
3672 mN.bigContentView = contentView;
3673 return this;
3674 }
3675
3676 /**
3677 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3678 *
3679 * This will override the heads-up layout that would otherwise be constructed by this
3680 * Builder object.
3681 */
3682 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3683 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003684 return this;
3685 }
3686
Joe Onoratocb109a02011-01-18 17:57:41 -08003687 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003688 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003690 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3691 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3692 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003693 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003694 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003695 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003696 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003697 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003698 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003700 return this;
3701 }
3702
Joe Onoratocb109a02011-01-18 17:57:41 -08003703 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003704 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3705 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003706 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003707 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003708 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003709 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003710 return this;
3711 }
3712
Joe Onoratocb109a02011-01-18 17:57:41 -08003713 /**
3714 * An intent to launch instead of posting the notification to the status bar.
3715 * Only for use with extremely high-priority notifications demanding the user's
3716 * <strong>immediate</strong> attention, such as an incoming phone call or
3717 * alarm clock that the user has explicitly set to a particular time.
3718 * If this facility is used for something else, please give the user an option
3719 * to turn it off and use a normal notification, as this can be extremely
3720 * disruptive.
3721 *
Chris Wren47c20a12014-06-18 17:27:29 -04003722 * <p>
3723 * The system UI may choose to display a heads-up notification, instead of
3724 * launching this intent, while the user is using the device.
3725 * </p>
3726 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003727 * @param intent The pending intent to launch.
3728 * @param highPriority Passing true will cause this notification to be sent
3729 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003730 *
3731 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003732 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003733 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003735 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3736 return this;
3737 }
3738
Joe Onoratocb109a02011-01-18 17:57:41 -08003739 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003740 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003741 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003743 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003744 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003745 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003746 return this;
3747 }
3748
Joe Onoratocb109a02011-01-18 17:57:41 -08003749 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003750 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003751 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003752 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003753 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003754 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003755 setTicker(tickerText);
3756 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003757 return this;
3758 }
3759
Joe Onoratocb109a02011-01-18 17:57:41 -08003760 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003761 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003762 *
3763 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003764 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3765 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003766 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003767 public Builder setLargeIcon(Bitmap b) {
3768 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3769 }
3770
3771 /**
3772 * Add a large icon to the notification content view.
3773 *
3774 * In the platform template, this image will be shown on the left of the notification view
3775 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3776 * badge atop the large icon).
3777 */
3778 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003779 mN.mLargeIcon = icon;
3780 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003781 return this;
3782 }
3783
Joe Onoratocb109a02011-01-18 17:57:41 -08003784 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003785 * Set the sound to play.
3786 *
John Spurlockc0650f022014-07-19 13:22:39 -04003787 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3788 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003789 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003790 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003791 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003792 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003793 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003794 mN.sound = sound;
3795 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003796 return this;
3797 }
3798
Joe Onoratocb109a02011-01-18 17:57:41 -08003799 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003800 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003801 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003802 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3803 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003804 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003805 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003806 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003807 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003808 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003809 mN.sound = sound;
3810 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003811 return this;
3812 }
3813
Joe Onoratocb109a02011-01-18 17:57:41 -08003814 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003815 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3816 * use during playback.
3817 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003818 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003819 * @see Notification#sound
3820 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003821 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003822 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003823 mN.sound = sound;
3824 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003825 return this;
3826 }
3827
3828 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003829 * Set the vibration pattern to use.
3830 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003831 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3832 * <code>pattern</code> parameter.
3833 *
Chris Wren47c20a12014-06-18 17:27:29 -04003834 * <p>
3835 * A notification that vibrates is more likely to be presented as a heads-up notification.
3836 * </p>
3837 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003838 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003839 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003840 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003841 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003842 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003843 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003844 return this;
3845 }
3846
Joe Onoratocb109a02011-01-18 17:57:41 -08003847 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003848 * Set the desired color for the indicator LED on the device, as well as the
3849 * blink duty cycle (specified in milliseconds).
3850 *
3851
3852 * Not all devices will honor all (or even any) of these values.
3853 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003854 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003855 * @see Notification#ledARGB
3856 * @see Notification#ledOnMS
3857 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003858 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003859 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003860 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003861 mN.ledARGB = argb;
3862 mN.ledOnMS = onMs;
3863 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003864 if (onMs != 0 || offMs != 0) {
3865 mN.flags |= FLAG_SHOW_LIGHTS;
3866 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003867 return this;
3868 }
3869
Joe Onoratocb109a02011-01-18 17:57:41 -08003870 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003871 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003872 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003873
3874 * Ongoing notifications cannot be dismissed by the user, so your application or service
3875 * must take care of canceling them.
3876 *
3877
3878 * They are typically used to indicate a background task that the user is actively engaged
3879 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3880 * (e.g., a file download, sync operation, active network connection).
3881 *
3882
3883 * @see Notification#FLAG_ONGOING_EVENT
Joe Onoratocb109a02011-01-18 17:57:41 -08003884 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003885 public Builder setOngoing(boolean ongoing) {
3886 setFlag(FLAG_ONGOING_EVENT, ongoing);
3887 return this;
3888 }
3889
Joe Onoratocb109a02011-01-18 17:57:41 -08003890 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003891 * Set whether this notification should be colorized. When set, the color set with
3892 * {@link #setColor(int)} will be used as the background color of this notification.
3893 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003894 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3895 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003896 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003897 * For most styles, the coloring will only be applied if the notification is for a
3898 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003899 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003900 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003901 *
Aurimas Liutikas7f695332018-05-31 21:07:32 -07003902 * @see #setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003903 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003904 */
3905 public Builder setColorized(boolean colorize) {
3906 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3907 return this;
3908 }
3909
3910 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003911 * Set this flag if you would only like the sound, vibrate
3912 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003913 *
3914 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003915 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003916 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3917 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3918 return this;
3919 }
3920
Joe Onoratocb109a02011-01-18 17:57:41 -08003921 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003922 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003923 *
3924 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003925 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003926 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003927 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003928 return this;
3929 }
3930
Joe Onoratocb109a02011-01-18 17:57:41 -08003931 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003932 * Set whether or not this notification should not bridge to other devices.
3933 *
3934 * <p>Some notifications can be bridged to other devices for remote display.
3935 * This hint can be set to recommend this notification not be bridged.
3936 */
3937 public Builder setLocalOnly(boolean localOnly) {
3938 setFlag(FLAG_LOCAL_ONLY, localOnly);
3939 return this;
3940 }
3941
3942 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003943 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003944 * <p>
3945 * The value should be one or more of the following fields combined with
3946 * bitwise-or:
3947 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3948 * <p>
3949 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003950 *
3951 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003952 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003953 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003954 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003955 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003956 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003957 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003958 return this;
3959 }
3960
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003961 /**
3962 * Set the priority of this notification.
3963 *
3964 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003965 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003966 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003967 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003968 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003969 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003970 return this;
3971 }
Joe Malin8d40d042012-11-05 11:36:40 -08003972
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003973 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003974 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003975 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003976 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003977 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003978 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003979 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003980 return this;
3981 }
3982
3983 /**
Chris Wrendde75302014-03-26 17:24:15 -04003984 * Add a person that is relevant to this notification.
3985 *
Chris Wrene6c48932014-09-29 17:19:27 -04003986 * <P>
3987 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003988 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3989 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3990 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003991 * </P>
3992 *
3993 * <P>
3994 * The person should be specified by the {@code String} representation of a
3995 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3996 * </P>
3997 *
3998 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3999 * URIs. The path part of these URIs must exist in the contacts database, in the
4000 * appropriate column, or the reference will be discarded as invalid. Telephone schema
4001 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08004002 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
4003 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04004004 * </P>
4005 *
4006 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04004007 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08004008 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04004009 */
Chris Wrene6c48932014-09-29 17:19:27 -04004010 public Builder addPerson(String uri) {
Selim Cinek9acd6732018-03-23 16:39:02 -07004011 addPerson(new Person.Builder().setUri(uri).build());
Selim Cineke7238dd2017-12-14 17:48:32 -08004012 return this;
4013 }
4014
4015 /**
4016 * Add a person that is relevant to this notification.
4017 *
4018 * <P>
4019 * Depending on user preferences, this annotation may allow the notification to pass
4020 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
4021 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
4022 * appear more prominently in the user interface.
4023 * </P>
4024 *
4025 * <P>
4026 * A person should usually contain a uri in order to benefit from the ranking boost.
4027 * However, even if no uri is provided, it's beneficial to provide other people in the
4028 * notification, such that listeners and voice only devices can announce and handle them
4029 * properly.
4030 * </P>
4031 *
4032 * @param person the person to add.
4033 * @see Notification#EXTRA_PEOPLE_LIST
4034 */
4035 public Builder addPerson(Person person) {
4036 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04004037 return this;
4038 }
4039
4040 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004041 * Set this notification to be part of a group of notifications sharing the same key.
4042 * Grouped notifications may display in a cluster or stack on devices which
4043 * support such rendering.
4044 *
4045 * <p>To make this notification the summary for its group, also call
4046 * {@link #setGroupSummary}. A sort order can be specified for group members by using
4047 * {@link #setSortKey}.
4048 * @param groupKey The group key of the group.
4049 * @return this object for method chaining
4050 */
4051 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004052 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004053 return this;
4054 }
4055
4056 /**
4057 * Set this notification to be the group summary for a group of notifications.
4058 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04004059 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
4060 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004061 * @param isGroupSummary Whether this notification should be a group summary.
4062 * @return this object for method chaining
4063 */
4064 public Builder setGroupSummary(boolean isGroupSummary) {
4065 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
4066 return this;
4067 }
4068
4069 /**
4070 * Set a sort key that orders this notification among other notifications from the
4071 * same package. This can be useful if an external sort was already applied and an app
4072 * would like to preserve this. Notifications will be sorted lexicographically using this
4073 * value, although providing different priorities in addition to providing sort key may
4074 * cause this value to be ignored.
4075 *
4076 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07004077 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004078 *
4079 * @see String#compareTo(String)
4080 */
4081 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004082 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004083 return this;
4084 }
4085
4086 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004087 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004088 *
Griff Hazen720042b2014-02-24 15:46:56 -08004089 * <p>Values within the Bundle will replace existing extras values in this Builder.
4090 *
4091 * @see Notification#extras
4092 */
Griff Hazen959591e2014-05-15 22:26:18 -07004093 public Builder addExtras(Bundle extras) {
4094 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004095 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08004096 }
4097 return this;
4098 }
4099
4100 /**
4101 * Set metadata for this notification.
4102 *
4103 * <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 -04004104 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004105 * called.
4106 *
Griff Hazen720042b2014-02-24 15:46:56 -08004107 * <p>Replaces any existing extras values with those from the provided Bundle.
4108 * Use {@link #addExtras} to merge in metadata instead.
4109 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004110 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004111 */
Griff Hazen959591e2014-05-15 22:26:18 -07004112 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004113 if (extras != null) {
4114 mUserExtras = extras;
4115 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05004116 return this;
4117 }
4118
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004119 /**
Griff Hazen720042b2014-02-24 15:46:56 -08004120 * Get the current metadata Bundle used by this notification Builder.
4121 *
4122 * <p>The returned Bundle is shared with this Builder.
4123 *
4124 * <p>The current contents of this Bundle are copied into the Notification each time
4125 * {@link #build()} is called.
4126 *
4127 * @see Notification#extras
4128 */
4129 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004130 return mUserExtras;
4131 }
4132
4133 private Bundle getAllExtras() {
4134 final Bundle saveExtras = (Bundle) mUserExtras.clone();
4135 saveExtras.putAll(mN.extras);
4136 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08004137 }
4138
4139 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004140 * Add an action to this notification. Actions are typically displayed by
4141 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004142 * <p>
4143 * Every action must have an icon (32dp square and matching the
4144 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4145 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4146 * <p>
4147 * A notification in its expanded form can display up to 3 actions, from left to right in
4148 * the order they were added. Actions will not be displayed when the notification is
4149 * collapsed, however, so be sure that any essential functions may be accessed by the user
4150 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004151 *
4152 * @param icon Resource ID of a drawable that represents the action.
4153 * @param title Text describing the action.
4154 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04004155 *
4156 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004157 */
Dan Sandler86647982015-05-13 23:41:13 -04004158 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004159 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004160 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004161 return this;
4162 }
4163
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004164 /**
Griff Hazen959591e2014-05-15 22:26:18 -07004165 * Add an action to this notification. Actions are typically displayed by
4166 * the system as a button adjacent to the notification content.
4167 * <p>
4168 * Every action must have an icon (32dp square and matching the
4169 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
4170 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
4171 * <p>
4172 * A notification in its expanded form can display up to 3 actions, from left to right in
4173 * the order they were added. Actions will not be displayed when the notification is
4174 * collapsed, however, so be sure that any essential functions may be accessed by the user
4175 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
4176 *
4177 * @param action The action to add.
4178 */
4179 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08004180 if (action != null) {
4181 mActions.add(action);
4182 }
Griff Hazen959591e2014-05-15 22:26:18 -07004183 return this;
4184 }
4185
4186 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004187 * Alter the complete list of actions attached to this notification.
4188 * @see #addAction(Action).
4189 *
4190 * @param actions
4191 * @return
4192 */
4193 public Builder setActions(Action... actions) {
4194 mActions.clear();
4195 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08004196 if (actions[i] != null) {
4197 mActions.add(actions[i]);
4198 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004199 }
4200 return this;
4201 }
4202
4203 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004204 * Add a rich notification style to be applied at build time.
4205 *
4206 * @param style Object responsible for modifying the notification style.
4207 */
4208 public Builder setStyle(Style style) {
4209 if (mStyle != style) {
4210 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004211 if (mStyle != null) {
4212 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004213 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
4214 } else {
4215 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004216 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004217 }
4218 return this;
4219 }
4220
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004221 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05004222 * Returns the style set by {@link #setStyle(Style)}.
4223 */
4224 public Style getStyle() {
4225 return mStyle;
4226 }
4227
4228 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004229 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07004230 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004231 * @return The same Builder.
4232 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004233 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004234 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004235 return this;
4236 }
4237
4238 /**
4239 * Supply a replacement Notification whose contents should be shown in insecure contexts
4240 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4241 * @param n A replacement notification, presumably with some or all info redacted.
4242 * @return The same Builder.
4243 */
4244 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004245 if (n != null) {
4246 mN.publicVersion = new Notification();
4247 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4248 } else {
4249 mN.publicVersion = null;
4250 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004251 return this;
4252 }
4253
Griff Hazenb720abe2014-05-20 13:15:30 -07004254 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004255 * Apply an extender to this notification builder. Extenders may be used to add
4256 * metadata or change options on this builder.
4257 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004258 public Builder extend(Extender extender) {
4259 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004260 return this;
4261 }
4262
Dan Sandler4e787062015-06-17 15:09:48 -04004263 /**
4264 * @hide
4265 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004266 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004267 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004268 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004269 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004270 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004271 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004272 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004273 }
4274
Dan Sandler26e81cf2014-05-06 10:01:27 -04004275 /**
4276 * Sets {@link Notification#color}.
4277 *
4278 * @param argb The accent color to use
4279 *
4280 * @return The same Builder.
4281 */
Tor Norbye80756e32015-03-02 09:39:27 -08004282 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004283 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004284 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004285 return this;
4286 }
4287
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004288 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004289 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4290 // This user can never be a badged profile,
4291 // and also includes USER_ALL system notifications.
4292 return null;
4293 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004294 // Note: This assumes that the current user can read the profile badge of the
4295 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004296 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004297 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004298 }
4299
4300 private Bitmap getProfileBadge() {
4301 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004302 if (badge == null) {
4303 return null;
4304 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004305 final int size = mContext.getResources().getDimensionPixelSize(
4306 R.dimen.notification_badge_size);
4307 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004308 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004309 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004310 badge.draw(canvas);
4311 return bitmap;
4312 }
4313
Selim Cinekc848c3a2016-01-13 15:27:30 -08004314 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004315 Bitmap profileBadge = getProfileBadge();
4316
Kenny Guy98193ea2014-07-24 19:54:37 +01004317 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004318 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4319 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004320 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004321 contentView.setDrawableTint(R.id.profile_badge, false,
4322 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004323 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004324 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004325 }
4326
Julia Reynoldsfc640012018-02-21 12:25:27 -05004327 /**
4328 * @hide
4329 */
4330 public boolean usesStandardHeader() {
4331 if (mN.mUsesStandardHeader) {
4332 return true;
4333 }
4334 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
4335 if (mN.contentView == null && mN.bigContentView == null) {
4336 return true;
4337 }
4338 }
4339 boolean contentViewUsesHeader = mN.contentView == null
4340 || STANDARD_LAYOUTS.contains(mN.contentView.getLayoutId());
4341 boolean bigContentViewUsesHeader = mN.bigContentView == null
4342 || STANDARD_LAYOUTS.contains(mN.bigContentView.getLayoutId());
4343 return contentViewUsesHeader && bigContentViewUsesHeader;
4344 }
4345
Christoph Studerfe718432014-09-01 18:21:18 +02004346 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004347 resetNotificationHeader(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004348 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004349 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004350 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004351 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004352 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004353 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004354 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004355 }
4356
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004357 /**
4358 * Resets the notification header to its original state
4359 */
4360 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004361 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4362 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004363 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004364 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004365 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004366 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004367 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004368 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4369 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004370 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004371 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004372 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004373 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004374 contentView.setImageViewIcon(R.id.profile_badge, null);
4375 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004376 mN.mUsesStandardHeader = false;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004377 }
4378
Selim Cinek384804b2018-04-18 14:31:07 +08004379 private RemoteViews applyStandardTemplate(int resId, TemplateBindResult result) {
4380 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this),
4381 result);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004382 }
4383
4384 /**
4385 * @param hasProgress whether the progress bar should be shown and set
Selim Cinek384804b2018-04-18 14:31:07 +08004386 * @param result
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004387 */
Selim Cinek384804b2018-04-18 14:31:07 +08004388 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
4389 TemplateBindResult result) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004390 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
Selim Cinek384804b2018-04-18 14:31:07 +08004391 .fillTextsFrom(this), result);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004392 }
4393
Selim Cinek384804b2018-04-18 14:31:07 +08004394 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p,
4395 TemplateBindResult result) {
Kenny Guy77320062014-08-27 21:37:15 +01004396 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004397
Christoph Studerfe718432014-09-01 18:21:18 +02004398 resetStandardTemplate(contentView);
4399
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004400 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004401 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004402 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Selim Cinek384804b2018-04-18 14:31:07 +08004403 bindLargeIconAndReply(contentView, p, result);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004404 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4405 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004406 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004407 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004408 if (!p.ambient) {
4409 setTextViewColorPrimary(contentView, R.id.title);
4410 }
Selim Cinek954cc232016-05-20 13:29:23 -07004411 contentView.setViewLayoutWidth(R.id.title, showProgress
4412 ? ViewGroup.LayoutParams.WRAP_CONTENT
4413 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004414 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004415 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004416 int textId = showProgress ? com.android.internal.R.id.text_line_1
4417 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004418 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004419 if (!p.ambient) {
4420 setTextViewColorSecondary(contentView, textId);
4421 }
Selim Cinek41598732016-01-11 16:58:37 -08004422 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004423 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004424
Selim Cinek279fa862016-06-14 10:57:25 -07004425 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004426
Selim Cinek29603462015-11-17 19:04:39 -08004427 return contentView;
4428 }
4429
Selim Cinek48f66b72017-08-18 16:17:51 -07004430 private CharSequence processTextSpans(CharSequence text) {
4431 if (hasForegroundColor()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004432 return ContrastColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004433 }
4434 return text;
4435 }
4436
Selim Cinek7b9605b2017-01-19 17:36:00 -08004437 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4438 ensureColors();
4439 contentView.setTextColor(id, mPrimaryTextColor);
4440 }
4441
Selim Cinek48f66b72017-08-18 16:17:51 -07004442 private boolean hasForegroundColor() {
4443 return mForegroundColor != COLOR_INVALID;
4444 }
4445
Selim Cinek389edcd2017-05-11 19:16:44 -07004446 /**
4447 * @return the primary text color
4448 * @hide
4449 */
4450 @VisibleForTesting
4451 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004452 ensureColors();
4453 return mPrimaryTextColor;
4454 }
4455
Selim Cinek389edcd2017-05-11 19:16:44 -07004456 /**
4457 * @return the secondary text color
4458 * @hide
4459 */
4460 @VisibleForTesting
4461 public int getSecondaryTextColor() {
4462 ensureColors();
4463 return mSecondaryTextColor;
4464 }
4465
Selim Cinek7b9605b2017-01-19 17:36:00 -08004466 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4467 ensureColors();
4468 contentView.setTextColor(id, mSecondaryTextColor);
4469 }
4470
4471 private void ensureColors() {
4472 int backgroundColor = getBackgroundColor();
4473 if (mPrimaryTextColor == COLOR_INVALID
4474 || mSecondaryTextColor == COLOR_INVALID
Selim Cinek7b9605b2017-01-19 17:36:00 -08004475 || mTextColorsAreForBackground != backgroundColor) {
4476 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004477 if (!hasForegroundColor() || !isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004478 mPrimaryTextColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004479 backgroundColor);
Lucas Dupina291d192018-06-07 13:59:42 -07004480 mSecondaryTextColor = ContrastColorUtil.resolveSecondaryColor(mContext,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004481 backgroundColor);
Selim Cinekc7f5a822018-03-20 19:32:06 -07004482 if (backgroundColor != COLOR_DEFAULT && isColorized()) {
Lucas Dupina291d192018-06-07 13:59:42 -07004483 mPrimaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004484 mPrimaryTextColor, backgroundColor, 4.5);
Lucas Dupina291d192018-06-07 13:59:42 -07004485 mSecondaryTextColor = ContrastColorUtil.findAlphaToMeetContrast(
Selim Cinekac5f0272017-05-02 16:05:41 -07004486 mSecondaryTextColor, backgroundColor, 4.5);
4487 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004488 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07004489 double backLum = ContrastColorUtil.calculateLuminance(backgroundColor);
4490 double textLum = ContrastColorUtil.calculateLuminance(mForegroundColor);
4491 double contrast = ContrastColorUtil.calculateContrast(mForegroundColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004492 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004493 // We only respect the given colors if worst case Black or White still has
4494 // contrast
4495 boolean backgroundLight = backLum > textLum
4496 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4497 || backLum <= textLum
4498 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004499 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004500 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004501 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004502 mForegroundColor,
4503 backgroundColor,
4504 true /* findFG */,
4505 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004506 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004507 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004508 } else {
4509 mSecondaryTextColor =
Lucas Dupina291d192018-06-07 13:59:42 -07004510 ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004511 mForegroundColor,
4512 backgroundColor,
4513 true /* findFG */,
4514 4.5f);
Lucas Dupina291d192018-06-07 13:59:42 -07004515 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004516 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004517 }
4518 } else {
4519 mPrimaryTextColor = mForegroundColor;
Lucas Dupina291d192018-06-07 13:59:42 -07004520 mSecondaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004521 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4522 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Lucas Dupina291d192018-06-07 13:59:42 -07004523 if (ContrastColorUtil.calculateContrast(mSecondaryTextColor,
Selim Cinek5fb73f82017-04-20 16:55:38 -07004524 backgroundColor) < 4.5f) {
4525 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004526 if (backgroundLight) {
Lucas Dupina291d192018-06-07 13:59:42 -07004527 mSecondaryTextColor = ContrastColorUtil.findContrastColor(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004528 mSecondaryTextColor,
4529 backgroundColor,
4530 true /* findFG */,
4531 4.5f);
4532 } else {
4533 mSecondaryTextColor
Lucas Dupina291d192018-06-07 13:59:42 -07004534 = ContrastColorUtil.findContrastColorAgainstDark(
Selim Cinek5fb73f82017-04-20 16:55:38 -07004535 mSecondaryTextColor,
4536 backgroundColor,
4537 true /* findFG */,
4538 4.5f);
4539 }
Lucas Dupina291d192018-06-07 13:59:42 -07004540 mPrimaryTextColor = ContrastColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004541 mSecondaryTextColor, backgroundLight
4542 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4543 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004544 }
4545 }
4546 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004547 }
4548 }
4549
4550 private void updateBackgroundColor(RemoteViews contentView) {
4551 if (isColorized()) {
4552 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4553 getBackgroundColor());
4554 } else {
4555 // Clear it!
4556 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4557 0);
4558 }
4559 }
4560
Selim Cinek860b6da2015-12-16 19:02:19 -08004561 /**
4562 * @param remoteView the remote view to update the minheight in
4563 * @param hasMinHeight does it have a mimHeight
4564 * @hide
4565 */
4566 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4567 int minHeight = 0;
4568 if (hasMinHeight) {
4569 // we need to set the minHeight of the notification
4570 minHeight = mContext.getResources().getDimensionPixelSize(
4571 com.android.internal.R.dimen.notification_min_content_height);
4572 }
4573 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4574 }
4575
Selim Cinek29603462015-11-17 19:04:39 -08004576 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004577 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4578 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4579 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4580 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004581 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004582 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004583 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004584 contentView.setProgressBackgroundTintList(
4585 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4586 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004587 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004588 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004589 contentView.setProgressTintList(R.id.progress, colorStateList);
4590 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004591 }
Selim Cinek29603462015-11-17 19:04:39 -08004592 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004593 } else {
4594 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004595 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004596 }
Joe Onorato561d3852010-11-20 18:09:34 -08004597 }
4598
Selim Cinek384804b2018-04-18 14:31:07 +08004599 private void bindLargeIconAndReply(RemoteViews contentView, StandardTemplateParams p,
4600 TemplateBindResult result) {
4601 boolean largeIconShown = bindLargeIcon(contentView, p.hideLargeIcon || p.ambient);
Selim Cinek1c72fa02018-04-23 18:00:54 +08004602 boolean replyIconShown = bindReplyIcon(contentView, p.hideReplyIcon || p.ambient);
Selim Cinek384804b2018-04-18 14:31:07 +08004603 contentView.setViewVisibility(R.id.right_icon_container,
Selim Cinek1c72fa02018-04-23 18:00:54 +08004604 largeIconShown || replyIconShown ? View.VISIBLE : View.GONE);
4605 int marginEnd = calculateMarginEnd(largeIconShown, replyIconShown);
4606 contentView.setViewLayoutMarginEnd(R.id.line1, marginEnd);
4607 contentView.setViewLayoutMarginEnd(R.id.text, marginEnd);
4608 contentView.setViewLayoutMarginEnd(R.id.progress, marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004609 if (result != null) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08004610 result.setIconMarginEnd(marginEnd);
Selim Cinek384804b2018-04-18 14:31:07 +08004611 }
4612 }
4613
Selim Cinek1c72fa02018-04-23 18:00:54 +08004614 private int calculateMarginEnd(boolean largeIconShown, boolean replyIconShown) {
4615 int marginEnd = 0;
4616 int contentMargin = mContext.getResources().getDimensionPixelSize(
4617 R.dimen.notification_content_margin_end);
4618 int iconSize = mContext.getResources().getDimensionPixelSize(
4619 R.dimen.notification_right_icon_size);
4620 if (replyIconShown) {
4621 // The size of the reply icon
4622 marginEnd += iconSize;
4623
4624 int replyInset = mContext.getResources().getDimensionPixelSize(
4625 R.dimen.notification_reply_inset);
4626 // We're subtracting the inset of the reply icon to make sure it's
4627 // aligned nicely on the right, and remove it from the following padding
4628 marginEnd -= replyInset * 2;
4629 }
4630 if (largeIconShown) {
4631 // adding size of the right icon
4632 marginEnd += iconSize;
4633
4634 if (replyIconShown) {
4635 // We also add some padding to the reply icon if it's around
4636 marginEnd += contentMargin;
4637 }
4638 }
4639 if (replyIconShown || largeIconShown) {
4640 // The padding to the content
4641 marginEnd += contentMargin;
4642 }
4643 return marginEnd;
4644 }
4645
Selim Cinek384804b2018-04-18 14:31:07 +08004646 /**
4647 * Bind the large icon.
4648 * @return if the largeIcon is visible
4649 */
4650 private boolean bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon) {
Selim Cinek279fa862016-06-14 10:57:25 -07004651 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4652 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4653 }
Selim Cinek88188f22017-09-19 16:46:56 -07004654 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4655 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004656 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4657 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4658 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Selim Cinek88188f22017-09-19 16:46:56 -07004659 }
Selim Cinek384804b2018-04-18 14:31:07 +08004660 return showLargeIcon;
4661 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004662
Selim Cinek384804b2018-04-18 14:31:07 +08004663 /**
4664 * Bind the reply icon.
4665 * @return if the reply icon is visible
4666 */
4667 private boolean bindReplyIcon(RemoteViews contentView, boolean hideReplyIcon) {
4668 boolean actionVisible = !hideReplyIcon;
4669 Action action = null;
Selim Cinek88188f22017-09-19 16:46:56 -07004670 if (actionVisible) {
Selim Cinek384804b2018-04-18 14:31:07 +08004671 action = findReplyAction();
4672 actionVisible = action != null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004673 }
Selim Cinek384804b2018-04-18 14:31:07 +08004674 if (actionVisible) {
4675 contentView.setViewVisibility(R.id.reply_icon_action, View.VISIBLE);
4676 contentView.setDrawableTint(R.id.reply_icon_action,
4677 false /* targetBackground */,
Selim Cinek4717d862018-04-19 09:19:15 +08004678 getNeutralColor(),
Selim Cinek384804b2018-04-18 14:31:07 +08004679 PorterDuff.Mode.SRC_ATOP);
4680 contentView.setOnClickPendingIntent(R.id.reply_icon_action, action.actionIntent);
4681 contentView.setRemoteInputs(R.id.reply_icon_action, action.mRemoteInputs);
4682 } else {
4683 contentView.setRemoteInputs(R.id.reply_icon_action, null);
4684 }
4685 contentView.setViewVisibility(R.id.reply_icon_action,
4686 actionVisible ? View.VISIBLE : View.GONE);
4687 return actionVisible;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004688 }
4689
4690 private Action findReplyAction() {
4691 ArrayList<Action> actions = mActions;
4692 if (mOriginalActions != null) {
4693 actions = mOriginalActions;
4694 }
4695 int numActions = actions.size();
4696 for (int i = 0; i < numActions; i++) {
4697 Action action = actions.get(i);
4698 if (hasValidRemoteInput(action)) {
4699 return action;
4700 }
4701 }
4702 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004703 }
4704
Selim Cinekafeed292017-12-12 17:32:44 -08004705 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4706 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004707 bindSmallIcon(contentView, ambient);
4708 bindHeaderAppName(contentView, ambient);
4709 if (!ambient) {
4710 // Ambient view does not have these
4711 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004712 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004713 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004714 bindProfileBadge(contentView);
4715 }
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004716 bindActivePermissions(contentView, ambient);
Adrian Roosd83e9992017-03-16 15:17:57 -07004717 bindExpandButton(contentView);
Julia Reynoldsfc640012018-02-21 12:25:27 -05004718 mN.mUsesStandardHeader = true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004719 }
4720
Julia Reynolds6013e5c2018-04-18 08:43:34 -04004721 private void bindActivePermissions(RemoteViews contentView, boolean ambient) {
Selim Cinek4717d862018-04-19 09:19:15 +08004722 int color = ambient ? resolveAmbientColor() : getNeutralColor();
Julia Reynoldsb887b232018-04-10 16:38:08 -04004723 contentView.setDrawableTint(R.id.camera, false, color, PorterDuff.Mode.SRC_ATOP);
4724 contentView.setDrawableTint(R.id.mic, false, color, PorterDuff.Mode.SRC_ATOP);
4725 contentView.setDrawableTint(R.id.overlay, false, color, PorterDuff.Mode.SRC_ATOP);
4726 }
4727
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004728 private void bindExpandButton(RemoteViews contentView) {
Selim Cinekc7f5a822018-03-20 19:32:06 -07004729 int color = isColorized() ? getPrimaryTextColor() : getSecondaryTextColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004730 contentView.setDrawableTint(R.id.expand_button, false, color,
4731 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004732 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004733 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004734 }
4735
4736 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4737 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004738 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004739 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004740 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4741 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4742 contentView.setLong(R.id.chronometer, "setBase",
4743 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4744 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004745 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004746 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004747 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004748 } else {
4749 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4750 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004751 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004752 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004753 } else {
4754 // We still want a time to be set but gone, such that we can show and hide it
4755 // on demand in case it's a child notification without anything in the header
4756 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004757 }
4758 }
4759
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004760 private void bindHeaderText(RemoteViews contentView) {
4761 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4762 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004763 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004764 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004765 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004766 if (headerText == null
4767 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4768 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4769 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4770 }
4771 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004772 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004773 contentView.setTextViewText(R.id.header_text, processTextSpans(
4774 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004775 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004776 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4777 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004778 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004779 }
4780 }
4781
Selim Cinekafeed292017-12-12 17:32:44 -08004782 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4783 if (!TextUtils.isEmpty(secondaryText)) {
4784 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4785 processLegacyText(secondaryText)));
4786 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4787 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4788 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4789 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4790 }
4791 }
4792
Adrian Rooseba05822016-04-22 17:09:27 -07004793 /**
4794 * @hide
4795 */
4796 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004797 CharSequence name = null;
4798 final PackageManager pm = mContext.getPackageManager();
4799 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4800 // only system packages which lump together a bunch of unrelated stuff
4801 // may substitute a different name to make the purpose of the
4802 // notification more clear. the correct package label should always
4803 // be accessible via SystemUI.
4804 final String pkg = mContext.getPackageName();
4805 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4806 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4807 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4808 name = subName;
4809 } else {
4810 Log.w(TAG, "warning: pkg "
4811 + pkg + " attempting to substitute app name '" + subName
4812 + "' without holding perm "
4813 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4814 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004815 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004816 if (TextUtils.isEmpty(name)) {
4817 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4818 }
4819 if (TextUtils.isEmpty(name)) {
4820 // still nothing?
4821 return null;
4822 }
4823
4824 return String.valueOf(name);
4825 }
Adrian Roos487374f2017-01-11 15:48:14 -08004826 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004827 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004828 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004829 setTextViewColorPrimary(contentView, R.id.app_name_text);
4830 } else {
4831 contentView.setTextColor(R.id.app_name_text,
Selim Cinekc7f5a822018-03-20 19:32:06 -07004832 ambient ? resolveAmbientColor() : getSecondaryTextColor());
Selim Cinek7b9605b2017-01-19 17:36:00 -08004833 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004834 }
4835
Adrian Roos487374f2017-01-11 15:48:14 -08004836 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004837 if (mN.mSmallIcon == null && mN.icon != 0) {
4838 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4839 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004840 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004841 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004842 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004843 }
4844
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004845 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004846 * @return true if the built notification will show the time or the chronometer; false
4847 * otherwise
4848 */
4849 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004850 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004851 }
4852
Christoph Studerfe718432014-09-01 18:21:18 +02004853 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004854 // actions_container is only reset when there are no actions to avoid focus issues with
4855 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004856 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004857 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004858
4859 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4860 big.setTextViewText(R.id.notification_material_reply_text_1, null);
Kenny Guya0f6de82018-04-06 16:20:16 +01004861 big.setViewVisibility(R.id.notification_material_reply_text_1_container, View.GONE);
4862 big.setViewVisibility(R.id.notification_material_reply_progress, View.GONE);
Adrian Roose458aa82015-12-08 16:17:19 -08004863
4864 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4865 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4866 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4867 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004868
Selim Cineked64a142018-02-06 18:06:01 -08004869 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4870 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004871 }
4872
Selim Cinek384804b2018-04-18 14:31:07 +08004873 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4874 TemplateBindResult result) {
4875 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this),
4876 result);
Adrian Roos48d746a2016-04-12 14:57:28 -07004877 }
4878
Adrian Roos70d7aa32017-01-11 15:39:06 -08004879 private RemoteViews applyStandardTemplateWithActions(int layoutId,
Selim Cinek384804b2018-04-18 14:31:07 +08004880 StandardTemplateParams p, TemplateBindResult result) {
4881 RemoteViews big = applyStandardTemplate(layoutId, p, result);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004882
Christoph Studerfe718432014-09-01 18:21:18 +02004883 resetStandardTemplateWithActions(big);
4884
Adrian Roose458aa82015-12-08 16:17:19 -08004885 boolean validRemoteInput = false;
4886
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004887 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004888 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004889 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004890 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004891 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004892 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004893 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004894 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004895 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004896 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004897 boolean actionHasValidInput = hasValidRemoteInput(action);
4898 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004899
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004900 final RemoteViews button = generateActionButton(action, emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07004901 p.ambient);
4902 if (actionHasValidInput && !emphazisedMode) {
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004903 // Clear the drawable
4904 button.setInt(R.id.action0, "setBackgroundResource", 0);
4905 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004906 big.addView(R.id.actions, button);
4907 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004908 } else {
4909 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004910 }
Adrian Roose458aa82015-12-08 16:17:19 -08004911
4912 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004913 if (!p.ambient && validRemoteInput && replyText != null
Selim Cinekbee4e072018-05-21 22:06:43 -07004914 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])
4915 && p.maxRemoteInputHistory > 0) {
Kenny Guya0f6de82018-04-06 16:20:16 +01004916 boolean showSpinner = mN.extras.getBoolean(EXTRA_SHOW_REMOTE_INPUT_SPINNER);
Adrian Roose458aa82015-12-08 16:17:19 -08004917 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Kenny Guya0f6de82018-04-06 16:20:16 +01004918 big.setViewVisibility(R.id.notification_material_reply_text_1_container,
4919 View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004920 big.setTextViewText(R.id.notification_material_reply_text_1,
4921 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004922 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Kenny Guya0f6de82018-04-06 16:20:16 +01004923 big.setViewVisibility(R.id.notification_material_reply_progress,
4924 showSpinner ? View.VISIBLE : View.GONE);
4925 big.setProgressIndeterminateTintList(
4926 R.id.notification_material_reply_progress,
4927 ColorStateList.valueOf(
4928 isColorized() ? getPrimaryTextColor() : resolveContrastColor()));
Adrian Roose458aa82015-12-08 16:17:19 -08004929
Selim Cinekbee4e072018-05-21 22:06:43 -07004930 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])
4931 && p.maxRemoteInputHistory > 1) {
Adrian Roose458aa82015-12-08 16:17:19 -08004932 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004933 big.setTextViewText(R.id.notification_material_reply_text_2,
4934 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004935 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004936
Selim Cinekbee4e072018-05-21 22:06:43 -07004937 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])
4938 && p.maxRemoteInputHistory > 2) {
Adrian Roose458aa82015-12-08 16:17:19 -08004939 big.setViewVisibility(
4940 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004941 big.setTextViewText(R.id.notification_material_reply_text_3,
4942 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004943 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004944 }
4945 }
4946 }
4947
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004948 return big;
4949 }
4950
Adrian Roose458aa82015-12-08 16:17:19 -08004951 private boolean hasValidRemoteInput(Action action) {
4952 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4953 // Weird actions
4954 return false;
4955 }
4956
4957 RemoteInput[] remoteInputs = action.getRemoteInputs();
4958 if (remoteInputs == null) {
4959 return false;
4960 }
4961
4962 for (RemoteInput r : remoteInputs) {
4963 CharSequence[] choices = r.getChoices();
4964 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4965 return true;
4966 }
4967 }
4968 return false;
4969 }
4970
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004971 /**
4972 * Construct a RemoteViews for the final 1U notification layout. In order:
4973 * 1. Custom contentView from the caller
4974 * 2. Style's proposed content view
4975 * 3. Standard template view
4976 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004977 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004978 return createContentView(false /* increasedheight */ );
4979 }
4980
4981 /**
4982 * Construct a RemoteViews for the smaller content view.
4983 *
4984 * @param increasedHeight true if this layout be created with an increased height. Some
4985 * styles may support showing more then just that basic 1U size
4986 * and the system may decide to render important notifications
4987 * slightly bigger even when collapsed.
4988 *
4989 * @hide
4990 */
4991 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004992 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004993 return mN.contentView;
4994 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004995 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004996 if (styleView != null) {
4997 return styleView;
4998 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004999 }
Selim Cinek384804b2018-04-18 14:31:07 +08005000 return applyStandardTemplate(getBaseLayoutResource(), null /* result */);
Joe Onorato46439ce2010-11-19 13:56:21 -08005001 }
5002
Selim Cineka7679b62017-05-10 16:33:25 -07005003 private boolean useExistingRemoteView() {
5004 return mStyle == null || (!mStyle.displayCustomViewInline()
5005 && !mRebuildStyledRemoteViews);
5006 }
5007
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005008 /**
5009 * Construct a RemoteViews for the final big notification layout.
5010 */
Julia Reynolds3b848122016-02-26 10:45:32 -05005011 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05005012 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07005013 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005014 return mN.bigContentView;
5015 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05005016 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08005017 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005018 } else if (mActions.size() != 0) {
Selim Cinek384804b2018-04-18 14:31:07 +08005019 result = applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5020 null /* result */);
Selim Cinek850a8542015-11-11 11:48:36 -05005021 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08005022 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05005023 return result;
5024 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005025
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005026 /**
Selim Cinek414ad332017-02-24 19:06:12 -08005027 * Construct a RemoteViews for the final notification header only. This will not be
5028 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005029 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07005030 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005031 * @hide
5032 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07005033 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08005034 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
5035 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005036 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07005037 ambient ? R.layout.notification_template_ambient_header
5038 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005039 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08005040 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08005041 if (colorized != null) {
5042 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
5043 } else {
5044 mN.extras.remove(EXTRA_COLORIZED);
5045 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08005046 return header;
5047 }
5048
Adrian Roos487374f2017-01-11 15:48:14 -08005049 /**
5050 * Construct a RemoteViews for the ambient version of the notification.
5051 *
5052 * @hide
5053 */
5054 public RemoteViews makeAmbientNotification() {
5055 RemoteViews ambient = applyStandardTemplateWithActions(
5056 R.layout.notification_template_material_ambient,
Selim Cinek384804b2018-04-18 14:31:07 +08005057 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false),
5058 null /* result */);
Adrian Roos487374f2017-01-11 15:48:14 -08005059 return ambient;
5060 }
5061
Selim Cinek29603462015-11-17 19:04:39 -08005062 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005063 if (result != null) {
5064 result.setViewVisibility(R.id.text_line_1, View.GONE);
5065 }
Selim Cinek29603462015-11-17 19:04:39 -08005066 }
5067
Selim Cinek6743c0b2017-01-18 18:24:01 -08005068 /**
5069 * Adapt the Notification header if this view is used as an expanded view.
5070 *
5071 * @hide
5072 */
5073 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005074 if (result != null) {
5075 result.setBoolean(R.id.notification_header, "setExpanded", true);
5076 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005077 }
5078
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005079 /**
5080 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005081 *
5082 * @param increasedHeight true if this layout be created with an increased height. Some
5083 * styles may support showing more then just that basic 1U size
5084 * and the system may decide to render important notifications
5085 * slightly bigger even when collapsed.
5086 *
5087 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005088 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005089 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07005090 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05005091 return mN.headsUpContentView;
5092 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08005093 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
5094 if (styleView != null) {
5095 return styleView;
5096 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05005097 } else if (mActions.size() == 0) {
5098 return null;
5099 }
5100
Selim Cinekbee4e072018-05-21 22:06:43 -07005101 // We only want at most a single remote input history to be shown here, otherwise
5102 // the content would become squished.
5103 StandardTemplateParams p = mParams.reset().fillTextsFrom(this)
5104 .setMaxRemoteInputHistory(1);
5105 return applyStandardTemplateWithActions(getBigBaseLayoutResource(),
5106 p,
5107 null /* result */);
Chris Wren8fd39ec2014-02-27 17:43:26 -05005108 }
5109
Selim Cinek624c02db2015-12-14 21:00:02 -08005110 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08005111 * Construct a RemoteViews for the final heads-up notification layout.
5112 */
5113 public RemoteViews createHeadsUpContentView() {
5114 return createHeadsUpContentView(false /* useIncreasedHeight */);
5115 }
5116
5117 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08005118 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5119 *
5120 * @hide
5121 */
5122 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005123 return makePublicView(false /* ambient */);
5124 }
5125
5126 /**
5127 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
5128 *
5129 * @hide
5130 */
5131 public RemoteViews makePublicAmbientNotification() {
5132 return makePublicView(true /* ambient */);
5133 }
5134
5135 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08005136 if (mN.publicVersion != null) {
5137 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005138 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08005139 }
5140 Bundle savedBundle = mN.extras;
5141 Style style = mStyle;
5142 mStyle = null;
5143 Icon largeIcon = mN.mLargeIcon;
5144 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07005145 Bitmap largeIconLegacy = mN.largeIcon;
5146 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005147 ArrayList<Action> actions = mActions;
5148 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08005149 Bundle publicExtras = new Bundle();
5150 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
5151 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
5152 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
5153 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07005154 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
5155 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cineked07b962018-04-30 14:39:35 -07005156 String appName = savedBundle.getString(EXTRA_SUBSTITUTE_APP_NAME);
5157 if (appName != null) {
5158 publicExtras.putString(EXTRA_SUBSTITUTE_APP_NAME, appName);
5159 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005160 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07005161 RemoteViews view;
5162 if (ambient) {
5163 publicExtras.putCharSequence(EXTRA_TITLE,
5164 mContext.getString(com.android.internal.R.string.notification_hidden_text));
5165 view = makeAmbientNotification();
5166 } else{
5167 view = makeNotificationHeader(false /* ambient */);
5168 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
5169 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005170 mN.extras = savedBundle;
5171 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07005172 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07005173 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08005174 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07005175 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08005176 }
5177
Selim Cinek6743c0b2017-01-18 18:24:01 -08005178 /**
5179 * Construct a content view for the display when low - priority
5180 *
5181 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
5182 * a new subtext is created consisting of the content of the
5183 * notification.
5184 * @hide
5185 */
5186 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
5187 int color = mN.color;
5188 mN.color = COLOR_DEFAULT;
5189 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
5190 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
5191 CharSequence newSummary = createSummaryText();
5192 if (!TextUtils.isEmpty(newSummary)) {
5193 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
5194 }
5195 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08005196
Adrian Roos6f6e1592017-05-02 16:22:53 -07005197 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08005198 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08005199 if (summary != null) {
5200 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
5201 } else {
5202 mN.extras.remove(EXTRA_SUB_TEXT);
5203 }
5204 mN.color = color;
5205 return header;
5206 }
Selim Cinek624c02db2015-12-14 21:00:02 -08005207
Selim Cinek6743c0b2017-01-18 18:24:01 -08005208 private CharSequence createSummaryText() {
5209 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
5210 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
5211 return titleText;
5212 }
5213 SpannableStringBuilder summary = new SpannableStringBuilder();
5214 if (titleText == null) {
5215 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
5216 }
5217 BidiFormatter bidi = BidiFormatter.getInstance();
5218 if (titleText != null) {
5219 summary.append(bidi.unicodeWrap(titleText));
5220 }
5221 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
5222 if (titleText != null && contentText != null) {
5223 summary.append(bidi.unicodeWrap(mContext.getText(
5224 R.string.notification_header_divider_symbol_with_spaces)));
5225 }
5226 if (contentText != null) {
5227 summary.append(bidi.unicodeWrap(contentText));
5228 }
5229 return summary;
5230 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05005231
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005232 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Selim Cinek396caca2018-04-10 17:46:46 -07005233 boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04005234 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005235 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005236 emphazisedMode ? getEmphasizedActionLayoutResource()
5237 : tombstone ? getActionTombstoneLayoutResource()
5238 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04005239 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04005240 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04005241 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005242 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005243 if (action.mRemoteInputs != null) {
5244 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
5245 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005246 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07005247 // change the background bgColor
Selim Cinek981962e2016-07-20 20:41:58 -07005248 CharSequence title = action.title;
5249 ColorStateList[] outResultColor = null;
Selim Cinek396caca2018-04-10 17:46:46 -07005250 int background = resolveBackgroundColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005251 if (isLegacy()) {
Lucas Dupina291d192018-06-07 13:59:42 -07005252 title = ContrastColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07005253 } else {
5254 outResultColor = new ColorStateList[1];
Selim Cinek396caca2018-04-10 17:46:46 -07005255 title = ensureColorSpanContrast(title, background, outResultColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005256 }
Selim Cinek48f66b72017-08-18 16:17:51 -07005257 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005258 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek396caca2018-04-10 17:46:46 -07005259 int rippleColor;
5260 boolean hasColorOverride = outResultColor != null && outResultColor[0] != null;
5261 if (hasColorOverride) {
5262 // There's a span spanning the full text, let's take it and use it as the
5263 // background color
5264 background = outResultColor[0].getDefaultColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005265 int textColor = ContrastColorUtil.resolvePrimaryColor(mContext,
Selim Cinek396caca2018-04-10 17:46:46 -07005266 background);
5267 button.setTextColor(R.id.action0, textColor);
5268 rippleColor = textColor;
Anthony Chenad4d1582017-04-10 16:07:58 -07005269 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek396caca2018-04-10 17:46:46 -07005270 rippleColor = resolveContrastColor();
5271 button.setTextColor(R.id.action0, rippleColor);
5272 } else {
5273 rippleColor = getPrimaryTextColor();
Selim Cinek981962e2016-07-20 20:41:58 -07005274 }
Selim Cinek396caca2018-04-10 17:46:46 -07005275 // We only want about 20% alpha for the ripple
5276 rippleColor = (rippleColor & 0x00ffffff) | 0x33000000;
5277 button.setColorStateList(R.id.action0, "setRippleColor",
5278 ColorStateList.valueOf(rippleColor));
5279 button.setColorStateList(R.id.action0, "setButtonBackground",
5280 ColorStateList.valueOf(background));
5281 button.setBoolean(R.id.action0, "setHasStroke", !hasColorOverride);
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005282 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07005283 button.setTextViewText(R.id.action0, processTextSpans(
5284 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08005285 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005286 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07005287 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08005288 button.setTextColor(R.id.action0,
5289 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005290 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08005291 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04005292 return button;
5293 }
5294
Joe Onoratocb109a02011-01-18 17:57:41 -08005295 /**
Selim Cinek981962e2016-07-20 20:41:58 -07005296 * Ensures contrast on color spans against a background color. also returns the color of the
5297 * text if a span was found that spans over the whole text.
5298 *
5299 * @param charSequence the charSequence on which the spans are
5300 * @param background the background color to ensure the contrast against
5301 * @param outResultColor an array in which a color will be returned as the first element if
5302 * there exists a full length color span.
5303 * @return the contrasted charSequence
5304 */
5305 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5306 ColorStateList[] outResultColor) {
5307 if (charSequence instanceof Spanned) {
5308 Spanned ss = (Spanned) charSequence;
5309 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5310 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5311 for (Object span : spans) {
5312 Object resultSpan = span;
5313 int spanStart = ss.getSpanStart(span);
5314 int spanEnd = ss.getSpanEnd(span);
5315 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5316 if (resultSpan instanceof CharacterStyle) {
5317 resultSpan = ((CharacterStyle) span).getUnderlying();
5318 }
5319 if (resultSpan instanceof TextAppearanceSpan) {
5320 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5321 ColorStateList textColor = originalSpan.getTextColor();
5322 if (textColor != null) {
5323 int[] colors = textColor.getColors();
5324 int[] newColors = new int[colors.length];
5325 for (int i = 0; i < newColors.length; i++) {
Lucas Dupina291d192018-06-07 13:59:42 -07005326 newColors[i] = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005327 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005328 }
5329 textColor = new ColorStateList(textColor.getStates().clone(),
5330 newColors);
Selim Cinek396caca2018-04-10 17:46:46 -07005331 if (fullLength) {
5332 outResultColor[0] = textColor;
5333 // Let's drop the color from the span
5334 textColor = null;
5335 }
Selim Cinek981962e2016-07-20 20:41:58 -07005336 resultSpan = new TextAppearanceSpan(
5337 originalSpan.getFamily(),
5338 originalSpan.getTextStyle(),
5339 originalSpan.getTextSize(),
5340 textColor,
5341 originalSpan.getLinkTextColor());
Selim Cinek981962e2016-07-20 20:41:58 -07005342 }
5343 } else if (resultSpan instanceof ForegroundColorSpan) {
5344 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5345 int foregroundColor = originalSpan.getForegroundColor();
Lucas Dupina291d192018-06-07 13:59:42 -07005346 foregroundColor = ContrastColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005347 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005348 if (fullLength) {
5349 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
Selim Cinek396caca2018-04-10 17:46:46 -07005350 resultSpan = null;
5351 } else {
5352 resultSpan = new ForegroundColorSpan(foregroundColor);
Selim Cinek981962e2016-07-20 20:41:58 -07005353 }
5354 } else {
5355 resultSpan = span;
5356 }
Selim Cinek396caca2018-04-10 17:46:46 -07005357 if (resultSpan != null) {
5358 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5359 }
Selim Cinek981962e2016-07-20 20:41:58 -07005360 }
5361 return builder;
5362 }
5363 return charSequence;
5364 }
5365
5366 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005367 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005368 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005369 */
5370 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005371 if (!mIsLegacyInitialized) {
5372 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5373 < Build.VERSION_CODES.LOLLIPOP;
5374 mIsLegacyInitialized = true;
5375 }
5376 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005377 }
5378
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005379 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005380 return processLegacyText(charSequence, false /* ambient */);
5381 }
5382
5383 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5384 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5385 boolean wantLightText = ambient;
5386 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005387 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005388 } else {
5389 return charSequence;
5390 }
5391 }
5392
Dan Sandler26e81cf2014-05-06 10:01:27 -04005393 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005394 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005395 */
Adrian Roos487374f2017-01-11 15:48:14 -08005396 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5397 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005398 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinekc7f5a822018-03-20 19:32:06 -07005399 int color;
5400 if (ambient) {
5401 color = resolveAmbientColor();
5402 } else if (isColorized()) {
5403 color = getPrimaryTextColor();
5404 } else {
5405 color = resolveContrastColor();
5406 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005407 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005408 contentView.setDrawableTint(R.id.icon, false, color,
5409 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005410
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005411 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005412 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005413 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005414 }
5415
Dan Sandler26e81cf2014-05-06 10:01:27 -04005416 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005417 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005418 * if it's grayscale).
5419 */
5420 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005421 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5422 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005423 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005424 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005425 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5426 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005427 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005428 }
5429
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005430 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005431 if (mN.color != COLOR_DEFAULT) {
5432 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005433 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005434 }
5435
Adrian Roos4ff3b122016-02-01 12:26:13 -08005436 int resolveContrastColor() {
5437 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5438 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005439 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005440
Selim Cinekac5f0272017-05-02 16:05:41 -07005441 int color;
Selim Cinekc7f5a822018-03-20 19:32:06 -07005442 int background = mContext.getColor(
5443 com.android.internal.R.color.notification_material_background_color);
Selim Cinekac5f0272017-05-02 16:05:41 -07005444 if (mN.color == COLOR_DEFAULT) {
5445 ensureColors();
Lucas Dupina291d192018-06-07 13:59:42 -07005446 color = ContrastColorUtil.resolveDefaultColor(mContext, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005447 } else {
Lucas Dupina291d192018-06-07 13:59:42 -07005448 color = ContrastColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005449 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005450 }
5451 if (Color.alpha(color) < 255) {
5452 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005453 color = ContrastColorUtil.compositeColors(color, background);
Selim Cinekac5f0272017-05-02 16:05:41 -07005454 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005455 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005456 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005457 }
5458
Selim Cinek4717d862018-04-19 09:19:15 +08005459 int resolveNeutralColor() {
5460 if (mNeutralColor != COLOR_INVALID) {
5461 return mNeutralColor;
5462 }
5463 int background = mContext.getColor(
5464 com.android.internal.R.color.notification_material_background_color);
Lucas Dupina291d192018-06-07 13:59:42 -07005465 mNeutralColor = ContrastColorUtil.resolveDefaultColor(mContext, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005466 if (Color.alpha(mNeutralColor) < 255) {
5467 // alpha doesn't go well for color filters, so let's blend it manually
Lucas Dupina291d192018-06-07 13:59:42 -07005468 mNeutralColor = ContrastColorUtil.compositeColors(mNeutralColor, background);
Selim Cinek4717d862018-04-19 09:19:15 +08005469 }
5470 return mNeutralColor;
5471 }
5472
Adrian Roos487374f2017-01-11 15:48:14 -08005473 int resolveAmbientColor() {
5474 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5475 return mCachedAmbientColor;
5476 }
Lucas Dupina291d192018-06-07 13:59:42 -07005477 final int contrasted = ContrastColorUtil.resolveAmbientColor(mContext, mN.color);
Adrian Roos487374f2017-01-11 15:48:14 -08005478
5479 mCachedAmbientColorIsFor = mN.color;
5480 return mCachedAmbientColor = contrasted;
5481 }
5482
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005483 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005484 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005485 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005486 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005487 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005488 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005489 mN.actions = new Action[mActions.size()];
5490 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005491 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005492 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005493 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005494 }
Selim Cinek247fa012016-02-18 09:50:48 -08005495 if (mN.bigContentView != null || mN.contentView != null
5496 || mN.headsUpContentView != null) {
5497 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5498 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005499 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005500 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005501
Julia Reynolds3b848122016-02-26 10:45:32 -05005502 /**
5503 * Creates a Builder from an existing notification so further changes can be made.
5504 * @param context The context for your application / activity.
5505 * @param n The notification to create a Builder from.
5506 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005507 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005508 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005509 ApplicationInfo applicationInfo = n.extras.getParcelable(
5510 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005511 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005512 if (applicationInfo != null) {
5513 try {
5514 builderContext = context.createApplicationContext(applicationInfo,
5515 Context.CONTEXT_RESTRICTED);
5516 } catch (NameNotFoundException e) {
5517 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5518 builderContext = context; // try with our context
5519 }
5520 } else {
5521 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005522 }
5523
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005524 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005525 }
5526
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005527 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005528 * @deprecated Use {@link #build()} instead.
5529 */
5530 @Deprecated
5531 public Notification getNotification() {
5532 return build();
5533 }
5534
5535 /**
5536 * Combine all of the options that have been set and return a new {@link Notification}
5537 * object.
5538 */
5539 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005540 // first, add any extras from the calling code
5541 if (mUserExtras != null) {
5542 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005543 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005544
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005545 mN.creationTime = System.currentTimeMillis();
5546
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005547 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005548 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005549
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005550 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005551
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005552 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005553 mStyle.reduceImageSizes(mContext);
5554 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005555 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005556 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005557 }
Selim Cinekd0426622017-07-11 13:19:59 +02005558 mN.reduceImageSizes(mContext);
5559
Adrian Roos5081c0d2016-02-26 16:04:19 -08005560 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005561 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005562 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005563 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005564 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5565 mN.contentView.getSequenceNumber());
5566 }
5567 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005568 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005569 if (mN.bigContentView != null) {
5570 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5571 mN.bigContentView.getSequenceNumber());
5572 }
5573 }
5574 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005575 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005576 if (mN.headsUpContentView != null) {
5577 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5578 mN.headsUpContentView.getSequenceNumber());
5579 }
5580 }
5581 }
5582
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005583 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5584 mN.flags |= FLAG_SHOW_LIGHTS;
5585 }
5586
Adrian Roosfb921842017-10-26 14:49:56 +02005587 mN.allPendingIntents = null;
5588
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005589 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005590 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005591
5592 /**
5593 * Apply this Builder to an existing {@link Notification} object.
5594 *
5595 * @hide
5596 */
5597 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005598 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005599 return n;
5600 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005601
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005602 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005603 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005604 * change. Also removes extenders on low ram devices, as
5605 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005606 *
5607 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5608 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005609 * @hide
5610 */
Kristian Monsen05f34792018-04-09 10:27:16 +02005611 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam,
5612 Context context) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005613 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005614
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005615 // Only strip views for known Styles because we won't know how to
5616 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005617 if (!isLowRam
5618 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005619 && getNotificationStyleClass(templateClass) == null) {
5620 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005621 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005622
5623 // Only strip unmodified BuilderRemoteViews.
5624 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005625 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005626 n.contentView.getSequenceNumber();
5627 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005628 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005629 n.bigContentView.getSequenceNumber();
5630 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005631 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005632 n.headsUpContentView.getSequenceNumber();
5633
5634 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005635 if (!isLowRam
5636 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005637 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005638 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005639
5640 Notification clone = n.clone();
5641 if (stripContentView) {
5642 clone.contentView = null;
5643 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5644 }
5645 if (stripBigContentView) {
5646 clone.bigContentView = null;
5647 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5648 }
5649 if (stripHeadsUpContentView) {
5650 clone.headsUpContentView = null;
5651 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5652 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005653 if (isLowRam) {
Kristian Monsen05f34792018-04-09 10:27:16 +02005654 String[] allowedServices = context.getResources().getStringArray(
5655 R.array.config_allowedManagedServicesOnLowRamDevices);
5656 if (allowedServices.length == 0) {
5657 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5658 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5659 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5660 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005661 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005662 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005663 }
5664
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005665 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005666 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005667 }
5668
5669 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005670 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005671 }
5672
5673 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005674 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005675 }
5676
5677 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005678 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005679 }
5680
5681 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005682 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005683 }
5684
Adrian Roosc1a80b02016-04-05 14:54:55 -07005685 private int getMessagingLayoutResource() {
5686 return R.layout.notification_template_material_messaging;
5687 }
5688
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005689 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005690 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005691 }
5692
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005693 private int getEmphasizedActionLayoutResource() {
5694 return R.layout.notification_material_action_emphasized;
5695 }
5696
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005697 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005698 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005699 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005700
5701 private int getBackgroundColor() {
5702 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005703 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005704 } else {
Selim Cinekc7f5a822018-03-20 19:32:06 -07005705 return COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005706 }
5707 }
5708
Selim Cinek396caca2018-04-10 17:46:46 -07005709 /**
Selim Cinek4717d862018-04-19 09:19:15 +08005710 * Gets a neutral color that can be used for icons or similar that should not stand out.
5711 */
5712 private int getNeutralColor() {
5713 if (isColorized()) {
5714 return getSecondaryTextColor();
5715 } else {
5716 return resolveNeutralColor();
5717 }
5718 }
5719
5720 /**
Selim Cinek396caca2018-04-10 17:46:46 -07005721 * Same as getBackgroundColor but also resolved the default color to the background.
5722 */
5723 private int resolveBackgroundColor() {
5724 int backgroundColor = getBackgroundColor();
5725 if (backgroundColor == COLOR_DEFAULT) {
5726 backgroundColor = mContext.getColor(
5727 com.android.internal.R.color.notification_material_background_color);
5728 }
5729 return backgroundColor;
5730 }
5731
Selim Cinek7b9605b2017-01-19 17:36:00 -08005732 private boolean isColorized() {
5733 return mN.isColorized();
5734 }
Selim Cinek99104832017-01-25 14:47:33 -08005735
Anthony Chenad4d1582017-04-10 16:07:58 -07005736 private boolean shouldTintActionButtons() {
5737 return mTintActionButtons;
5738 }
5739
Selim Cinek99104832017-01-25 14:47:33 -08005740 private boolean textColorsNeedInversion() {
5741 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5742 return false;
5743 }
5744 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5745 return targetSdkVersion > Build.VERSION_CODES.M
5746 && targetSdkVersion < Build.VERSION_CODES.O;
5747 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005748
5749 /**
5750 * Set a color palette to be used as the background and textColors
5751 *
5752 * @param backgroundColor the color to be used as the background
5753 * @param foregroundColor the color to be used as the foreground
5754 *
5755 * @hide
5756 */
5757 public void setColorPalette(int backgroundColor, int foregroundColor) {
5758 mBackgroundColor = backgroundColor;
5759 mForegroundColor = foregroundColor;
5760 mTextColorsAreForBackground = COLOR_INVALID;
5761 ensureColors();
5762 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005763
5764 /**
Selim Cineka7679b62017-05-10 16:33:25 -07005765 * Forces all styled remoteViews to be built from scratch and not use any cached
5766 * RemoteViews.
5767 * This is needed for legacy apps that are baking in their remoteviews into the
5768 * notification.
5769 *
5770 * @hide
5771 */
5772 public void setRebuildStyledRemoteViews(boolean rebuild) {
5773 mRebuildStyledRemoteViews = rebuild;
5774 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -08005775
5776 /**
5777 * Get the text that should be displayed in the statusBar when heads upped. This is
5778 * usually just the app name, but may be different depending on the style.
5779 *
5780 * @param publicMode If true, return a text that is safe to display in public.
5781 *
5782 * @hide
5783 */
5784 public CharSequence getHeadsUpStatusBarText(boolean publicMode) {
5785 if (mStyle != null && !publicMode) {
5786 CharSequence text = mStyle.getHeadsUpStatusBarText();
5787 if (!TextUtils.isEmpty(text)) {
5788 return text;
5789 }
5790 }
5791 return loadHeaderAppName();
5792 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005793 }
5794
5795 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005796 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5797 * remote views.
5798 *
5799 * @hide
5800 */
5801 void reduceImageSizes(Context context) {
5802 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5803 return;
5804 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005805 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005806 if (mLargeIcon != null || largeIcon != null) {
5807 Resources resources = context.getResources();
5808 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005809 int maxWidth = resources.getDimensionPixelSize(isLowRam
5810 ? R.dimen.notification_right_icon_size_low_ram
5811 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005812 int maxHeight = maxWidth;
5813 if (MediaStyle.class.equals(style)
5814 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005815 maxHeight = resources.getDimensionPixelSize(isLowRam
5816 ? R.dimen.notification_media_image_max_height_low_ram
5817 : R.dimen.notification_media_image_max_height);
5818 maxWidth = resources.getDimensionPixelSize(isLowRam
5819 ? R.dimen.notification_media_image_max_width_low_ram
5820 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005821 }
5822 if (mLargeIcon != null) {
5823 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5824 }
5825 if (largeIcon != null) {
5826 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5827 }
5828 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005829 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5830 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5831 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005832 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5833 }
5834
Selim Cineka8cb1262017-08-15 16:53:44 -07005835 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5836 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005837 if (remoteView != null) {
5838 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005839 int maxWidth = resources.getDimensionPixelSize(isLowRam
5840 ? R.dimen.notification_custom_view_max_image_width_low_ram
5841 : R.dimen.notification_custom_view_max_image_width);
5842 int maxHeight = resources.getDimensionPixelSize(isLowRam
5843 ? R.dimen.notification_custom_view_max_image_height_low_ram
5844 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005845 remoteView.reduceImageSizes(maxWidth, maxHeight);
5846 }
5847 }
5848
5849 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005850 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005851 */
Selim Cinek22714f12017-04-13 16:23:53 -07005852 private boolean isForegroundService() {
5853 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005854 }
5855
5856 /**
Selim Cinek99104832017-01-25 14:47:33 -08005857 * @return whether this notification has a media session attached
5858 * @hide
5859 */
5860 public boolean hasMediaSession() {
5861 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5862 }
5863
5864 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005865 * @return the style class of this notification
5866 * @hide
5867 */
5868 public Class<? extends Notification.Style> getNotificationStyle() {
5869 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5870
5871 if (!TextUtils.isEmpty(templateClass)) {
5872 return Notification.getNotificationStyleClass(templateClass);
5873 }
5874 return null;
5875 }
5876
5877 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005878 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005879 *
5880 * @hide
5881 */
5882 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005883 if (isColorizedMedia()) {
5884 return true;
5885 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005886 return extras.getBoolean(EXTRA_COLORIZED)
5887 && (hasColorizedPermission() || isForegroundService());
5888 }
5889
5890 /**
5891 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5892 * permission. The permission is checked when a notification is enqueued.
5893 */
5894 private boolean hasColorizedPermission() {
5895 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005896 }
5897
5898 /**
5899 * @return true if this notification is colorized and it is a media notification
5900 *
5901 * @hide
5902 */
5903 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005904 Class<? extends Style> style = getNotificationStyle();
5905 if (MediaStyle.class.equals(style)) {
5906 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5907 if ((colorized == null || colorized) && hasMediaSession()) {
5908 return true;
5909 }
5910 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5911 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5912 return true;
5913 }
5914 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005915 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005916 }
5917
Selim Cinek0847acd2017-04-24 19:48:29 -07005918
5919 /**
5920 * @return true if this is a media notification
5921 *
5922 * @hide
5923 */
5924 public boolean isMediaNotification() {
5925 Class<? extends Style> style = getNotificationStyle();
5926 if (MediaStyle.class.equals(style)) {
5927 return true;
5928 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5929 return true;
5930 }
5931 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005932 }
5933
Selim Cinek279fa862016-06-14 10:57:25 -07005934 private boolean hasLargeIcon() {
5935 return mLargeIcon != null || largeIcon != null;
5936 }
5937
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005938 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005939 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005940 * @hide
5941 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005942 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005943 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5944 }
5945
5946 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005947 * @return true if the notification will show a chronometer; false otherwise
5948 * @hide
5949 */
5950 public boolean showsChronometer() {
5951 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5952 }
5953
5954 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005955 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005956 */
5957 @SystemApi
5958 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5959 Class<? extends Style>[] classes = new Class[] {
5960 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5961 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5962 MessagingStyle.class };
5963 for (Class<? extends Style> innerClass : classes) {
5964 if (templateClass.equals(innerClass.getName())) {
5965 return innerClass;
5966 }
5967 }
5968 return null;
5969 }
5970
5971 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005972 * An object that can apply a rich notification style to a {@link Notification.Builder}
5973 * object.
5974 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005975 public static abstract class Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07005976
5977 /**
5978 * The number of items allowed simulatanously in the remote input history.
5979 * @hide
5980 */
5981 static final int MAX_REMOTE_INPUT_HISTORY_LINES = 3;
Chris Wrend6297db2012-05-03 16:20:13 -04005982 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005983
5984 /**
5985 * @hide
5986 */
5987 protected CharSequence mSummaryText = null;
5988
5989 /**
5990 * @hide
5991 */
5992 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005993
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005994 protected Builder mBuilder;
5995
Chris Wrend6297db2012-05-03 16:20:13 -04005996 /**
5997 * Overrides ContentTitle in the big form of the template.
5998 * This defaults to the value passed to setContentTitle().
5999 */
6000 protected void internalSetBigContentTitle(CharSequence title) {
6001 mBigContentTitle = title;
6002 }
6003
6004 /**
6005 * Set the first line of text after the detail section in the big form of the template.
6006 */
6007 protected void internalSetSummaryText(CharSequence cs) {
6008 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04006009 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04006010 }
6011
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006012 public void setBuilder(Builder builder) {
6013 if (mBuilder != builder) {
6014 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07006015 if (mBuilder != null) {
6016 mBuilder.setStyle(this);
6017 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006018 }
6019 }
6020
Chris Wrend6297db2012-05-03 16:20:13 -04006021 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006022 if (mBuilder == null) {
6023 throw new IllegalArgumentException("Style requires a valid Builder object");
6024 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006025 }
Chris Wrend6297db2012-05-03 16:20:13 -04006026
6027 protected RemoteViews getStandardView(int layoutId) {
Selim Cinek384804b2018-04-18 14:31:07 +08006028 return getStandardView(layoutId, null);
6029 }
6030
6031 /**
6032 * Get the standard view for this style.
6033 *
6034 * @param layoutId The layout id to use
6035 * @param result The result where template bind information is saved.
6036 * @return A remoteView for this style.
6037 * @hide
6038 */
6039 protected RemoteViews getStandardView(int layoutId, TemplateBindResult result) {
Chris Wrend6297db2012-05-03 16:20:13 -04006040 checkBuilder();
6041
Christoph Studer4600f9b2014-07-22 22:44:43 +02006042 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006043 CharSequence oldBuilderContentTitle =
6044 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006045 if (mBigContentTitle != null) {
6046 mBuilder.setContentTitle(mBigContentTitle);
6047 }
6048
Selim Cinek384804b2018-04-18 14:31:07 +08006049 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId, result);
Chris Wrend6297db2012-05-03 16:20:13 -04006050
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006051 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006052
Chris Wrend6297db2012-05-03 16:20:13 -04006053 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
6054 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04006055 } else {
6056 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04006057 }
6058
Chris Wrend6297db2012-05-03 16:20:13 -04006059 return contentView;
6060 }
6061
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006062 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006063 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006064 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08006065 *
6066 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006067 * @hide
6068 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08006069 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006070 return null;
6071 }
6072
6073 /**
6074 * Construct a Style-specific RemoteViews for the final big notification layout.
6075 * @hide
6076 */
6077 public RemoteViews makeBigContentView() {
6078 return null;
6079 }
6080
6081 /**
6082 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08006083 *
6084 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006085 * @hide
6086 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006087 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006088 return null;
6089 }
6090
6091 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006092 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006093 * @hide
6094 */
6095 public void addExtras(Bundle extras) {
6096 if (mSummaryTextSet) {
6097 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
6098 }
6099 if (mBigContentTitle != null) {
6100 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
6101 }
Chris Wren91ad5632013-06-05 15:05:57 -04006102 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006103 }
6104
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006105 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006106 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006107 * @hide
6108 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02006109 protected void restoreFromExtras(Bundle extras) {
6110 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
6111 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
6112 mSummaryTextSet = true;
6113 }
6114 if (extras.containsKey(EXTRA_TITLE_BIG)) {
6115 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
6116 }
6117 }
6118
6119
6120 /**
6121 * @hide
6122 */
6123 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006124 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006125 return wip;
6126 }
6127
Daniel Sandler0ec46202015-06-24 01:27:05 -04006128 /**
6129 * @hide
6130 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006131 public void purgeResources() {}
6132
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006133 /**
6134 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
6135 * attached to.
6136 *
6137 * @return the fully constructed Notification.
6138 */
6139 public Notification build() {
6140 checkBuilder();
6141 return mBuilder.build();
6142 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006143
6144 /**
6145 * @hide
6146 * @return true if the style positions the progress bar on the second line; false if the
6147 * style hides the progress bar
6148 */
6149 protected boolean hasProgress() {
6150 return true;
6151 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006152
6153 /**
6154 * @hide
6155 * @return Whether we should put the summary be put into the notification header
6156 */
6157 public boolean hasSummaryInHeader() {
6158 return true;
6159 }
Selim Cinek593610c2016-02-16 18:42:57 -08006160
6161 /**
6162 * @hide
6163 * @return Whether custom content views are displayed inline in the style
6164 */
6165 public boolean displayCustomViewInline() {
6166 return false;
6167 }
Selim Cinekd0426622017-07-11 13:19:59 +02006168
6169 /**
6170 * Reduces the image sizes contained in this style.
6171 *
6172 * @hide
6173 */
6174 public void reduceImageSizes(Context context) {
6175 }
Selim Cinek90343862018-02-01 11:07:11 -08006176
6177 /**
6178 * Validate that this style was properly composed. This is called at build time.
6179 * @hide
6180 */
6181 public void validate(Context context) {
6182 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006183
6184 /**
6185 * @hide
6186 */
6187 public abstract boolean areNotificationsVisiblyDifferent(Style other);
Selim Cinekc7f5a822018-03-20 19:32:06 -07006188
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006189 /**
6190 * @return the the text that should be displayed in the statusBar when heads-upped.
6191 * If {@code null} is returned, the default implementation will be used.
6192 *
6193 * @hide
6194 */
6195 public CharSequence getHeadsUpStatusBarText() {
6196 return null;
6197 }
Joe Onorato46439ce2010-11-19 13:56:21 -08006198 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006199
6200 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006201 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08006202 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006203 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006204 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006205 * Notification notif = new Notification.Builder(mContext)
6206 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
6207 * .setContentText(subject)
6208 * .setSmallIcon(R.drawable.new_post)
6209 * .setLargeIcon(aBitmap)
6210 * .setStyle(new Notification.BigPictureStyle()
6211 * .bigPicture(aBigBitmap))
6212 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006213 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006214 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006215 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006216 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006217 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006218 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006219 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006220 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006221
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006222 public BigPictureStyle() {
6223 }
6224
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006225 /**
6226 * @deprecated use {@code BigPictureStyle()}.
6227 */
6228 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006229 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006230 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006231 }
6232
Chris Wrend6297db2012-05-03 16:20:13 -04006233 /**
6234 * Overrides ContentTitle in the big form of the template.
6235 * This defaults to the value passed to setContentTitle().
6236 */
6237 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006238 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006239 return this;
6240 }
6241
6242 /**
6243 * Set the first line of text after the detail section in the big form of the template.
6244 */
6245 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006246 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006247 return this;
6248 }
6249
Chris Wren0bd664d2012-08-01 13:56:56 -04006250 /**
Julia Reynolds7217dc92018-03-07 12:12:09 -05006251 * @hide
6252 */
6253 public Bitmap getBigPicture() {
6254 return mPicture;
6255 }
6256
6257 /**
Chris Wren0bd664d2012-08-01 13:56:56 -04006258 * Provide the bitmap to be used as the payload for the BigPicture notification.
6259 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006260 public BigPictureStyle bigPicture(Bitmap b) {
6261 mPicture = b;
6262 return this;
6263 }
6264
Chris Wren3745a3d2012-05-22 15:11:52 -04006265 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04006266 * Override the large icon when the big notification is shown.
6267 */
6268 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04006269 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
6270 }
6271
6272 /**
6273 * Override the large icon when the big notification is shown.
6274 */
6275 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04006276 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04006277 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04006278 return this;
6279 }
6280
Riley Andrews0394a0c2015-11-03 23:36:52 -08006281 /** @hide */
6282 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
6283
Daniel Sandler0ec46202015-06-24 01:27:05 -04006284 /**
6285 * @hide
6286 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006287 @Override
6288 public void purgeResources() {
6289 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08006290 if (mPicture != null &&
6291 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08006292 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07006293 mPicture = mPicture.createAshmemBitmap();
6294 }
6295 if (mBigLargeIcon != null) {
6296 mBigLargeIcon.convertToAshmem();
6297 }
6298 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01006299
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006300 /**
6301 * @hide
6302 */
Selim Cinekd0426622017-07-11 13:19:59 +02006303 @Override
6304 public void reduceImageSizes(Context context) {
6305 super.reduceImageSizes(context);
6306 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07006307 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02006308 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006309 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
6310 ? R.dimen.notification_big_picture_max_height_low_ram
6311 : R.dimen.notification_big_picture_max_height);
6312 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
6313 ? R.dimen.notification_big_picture_max_width_low_ram
6314 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02006315 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
6316 }
6317 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07006318 int rightIconSize = resources.getDimensionPixelSize(isLowRam
6319 ? R.dimen.notification_right_icon_size_low_ram
6320 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02006321 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
6322 }
6323 }
6324
6325 /**
6326 * @hide
6327 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006328 public RemoteViews makeBigContentView() {
6329 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01006330 // This covers the following cases:
6331 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006332 // mN.mLargeIcon
6333 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04006334 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07006335 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006336 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006337 oldLargeIcon = mBuilder.mN.mLargeIcon;
6338 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006339 // The legacy largeIcon might not allow us to clear the image, as it's taken in
6340 // replacement if the other one is null. Because we're restoring these legacy icons
6341 // for old listeners, this is in general non-null.
6342 largeIconLegacy = mBuilder.mN.largeIcon;
6343 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006344 }
6345
Selim Cinek384804b2018-04-18 14:31:07 +08006346 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource(),
6347 null /* result */);
Selim Cinek03d0d652015-11-13 13:18:09 -05006348 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006349 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
6350 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006351 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08006352 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05006353 }
Selim Cinek279fa862016-06-14 10:57:25 -07006354 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08006355
Christoph Studer5c510ee2014-12-15 16:32:27 +01006356 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006357 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07006358 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01006359 }
6360
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006361 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006362 return contentView;
6363 }
6364
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006365 /**
6366 * @hide
6367 */
6368 public void addExtras(Bundle extras) {
6369 super.addExtras(extras);
6370
6371 if (mBigLargeIconSet) {
6372 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
6373 }
6374 extras.putParcelable(EXTRA_PICTURE, mPicture);
6375 }
6376
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04006377 /**
6378 * @hide
6379 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006380 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02006381 protected void restoreFromExtras(Bundle extras) {
6382 super.restoreFromExtras(extras);
6383
6384 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01006385 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02006386 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04006387 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006388 mPicture = extras.getParcelable(EXTRA_PICTURE);
6389 }
Selim Cinek03d0d652015-11-13 13:18:09 -05006390
6391 /**
6392 * @hide
6393 */
6394 @Override
6395 public boolean hasSummaryInHeader() {
6396 return false;
6397 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05006398
6399 /**
6400 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006401 * Note that we aren't actually comparing the contents of the bitmaps here, so this
6402 * is only doing a cursory inspection. Bitmaps of equal size will appear the same.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006403 */
6404 @Override
6405 public boolean areNotificationsVisiblyDifferent(Style other) {
6406 if (other == null || getClass() != other.getClass()) {
6407 return true;
6408 }
6409 BigPictureStyle otherS = (BigPictureStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006410 return areBitmapsObviouslyDifferent(getBigPicture(), otherS.getBigPicture());
6411 }
6412
6413 private static boolean areBitmapsObviouslyDifferent(Bitmap a, Bitmap b) {
6414 if (a == b) {
6415 return false;
6416 }
6417 if (a == null || b == null) {
6418 return true;
6419 }
6420 return a.getWidth() != b.getWidth()
6421 || a.getHeight() != b.getHeight()
6422 || a.getConfig() != b.getConfig()
6423 || a.getGenerationId() != b.getGenerationId();
Julia Reynolds7217dc92018-03-07 12:12:09 -05006424 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006425 }
6426
6427 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006428 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006429 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006430 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006431 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006432 * Notification notif = new Notification.Builder(mContext)
6433 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6434 * .setContentText(subject)
6435 * .setSmallIcon(R.drawable.new_mail)
6436 * .setLargeIcon(aBitmap)
6437 * .setStyle(new Notification.BigTextStyle()
6438 * .bigText(aVeryLongString))
6439 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006440 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006441 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006442 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006443 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006444 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006445
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006446 private CharSequence mBigText;
6447
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006448 public BigTextStyle() {
6449 }
6450
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006451 /**
6452 * @deprecated use {@code BigTextStyle()}.
6453 */
6454 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006455 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006456 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006457 }
6458
Chris Wrend6297db2012-05-03 16:20:13 -04006459 /**
6460 * Overrides ContentTitle in the big form of the template.
6461 * This defaults to the value passed to setContentTitle().
6462 */
6463 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006464 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006465 return this;
6466 }
6467
6468 /**
6469 * Set the first line of text after the detail section in the big form of the template.
6470 */
6471 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006472 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006473 return this;
6474 }
6475
Chris Wren0bd664d2012-08-01 13:56:56 -04006476 /**
6477 * Provide the longer text to be displayed in the big form of the
6478 * template in place of the content text.
6479 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006480 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006481 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006482 return this;
6483 }
6484
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006485 /**
6486 * @hide
6487 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05006488 public CharSequence getBigText() {
6489 return mBigText;
6490 }
6491
6492 /**
6493 * @hide
6494 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006495 public void addExtras(Bundle extras) {
6496 super.addExtras(extras);
6497
Christoph Studer4600f9b2014-07-22 22:44:43 +02006498 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6499 }
6500
6501 /**
6502 * @hide
6503 */
6504 @Override
6505 protected void restoreFromExtras(Bundle extras) {
6506 super.restoreFromExtras(extras);
6507
6508 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006509 }
6510
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006511 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006512 * @param increasedHeight true if this layout be created with an increased height.
6513 *
6514 * @hide
6515 */
6516 @Override
6517 public RemoteViews makeContentView(boolean increasedHeight) {
6518 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006519 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006520 mBuilder.mActions = new ArrayList<>();
6521 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006522 mBuilder.mActions = mBuilder.mOriginalActions;
6523 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006524 return remoteViews;
6525 }
6526 return super.makeContentView(increasedHeight);
6527 }
6528
6529 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006530 * @hide
6531 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006532 @Override
6533 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6534 if (increasedHeight && mBuilder.mActions.size() > 0) {
6535 return makeBigContentView();
6536 }
6537 return super.makeHeadsUpContentView(increasedHeight);
6538 }
6539
6540 /**
6541 * @hide
6542 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006543 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006544
6545 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006546 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006547 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006548
Selim Cinek384804b2018-04-18 14:31:07 +08006549 TemplateBindResult result = new TemplateBindResult();
6550 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource(), result);
Selim Cinek1c72fa02018-04-23 18:00:54 +08006551 contentView.setInt(R.id.big_text, "setImageEndMargin", result.getIconMarginEnd());
Joe Malin8d40d042012-11-05 11:36:40 -08006552
Selim Cinek75998782016-04-26 10:39:17 -07006553 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006554
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006555 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006556 if (TextUtils.isEmpty(bigTextText)) {
6557 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6558 // experience
6559 bigTextText = mBuilder.processLegacyText(text);
6560 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006561 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006562
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006563 return contentView;
6564 }
6565
Julia Reynolds7217dc92018-03-07 12:12:09 -05006566 /**
6567 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006568 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006569 */
6570 @Override
6571 public boolean areNotificationsVisiblyDifferent(Style other) {
6572 if (other == null || getClass() != other.getClass()) {
6573 return true;
6574 }
6575 BigTextStyle newS = (BigTextStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04006576 return !Objects.equals(String.valueOf(getBigText()), String.valueOf(newS.getBigText()));
Julia Reynolds7217dc92018-03-07 12:12:09 -05006577 }
6578
Adrian Roosb1f427c2016-05-26 12:27:15 -07006579 static void applyBigTextContentView(Builder builder,
6580 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006581 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006582 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006583 contentView.setViewVisibility(R.id.big_text,
6584 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006585 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006586 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006587 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006588
6589 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006590 * Helper class for generating large-format notifications that include multiple back-and-forth
6591 * messages of varying types between any number of people.
6592 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006593 * <p>
Alex Hillsfc737de2016-03-23 17:33:02 -04006594 * If the platform does not provide large-format notifications, this method has no effect. The
6595 * user will always see the normal notification view.
Selim Cinekce8794f2018-05-23 16:46:05 -07006596 *
6597 * <p>
6598 * If the app is targeting Android P and above, it is required to use the {@link Person}
6599 * class in order to get an optimal rendering of the notification and its avatars. For
6600 * conversations involving multiple people, the app should also make sure that it marks the
6601 * conversation as a group with {@link #setGroupConversation(boolean)}.
6602 *
6603 * <p>
6604 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior.
6605 * Here's an example of how this may be used:
Alex Hillsfc737de2016-03-23 17:33:02 -04006606 * <pre class="prettyprint">
6607 *
Selim Cinekce8794f2018-05-23 16:46:05 -07006608 * Person user = new Person.Builder().setIcon(userIcon).setName(userName).build();
6609 * MessagingStyle style = new MessagingStyle(user)
6610 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
6611 * .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
6612 * .setGroupConversation(hasMultiplePeople());
6613 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006614 * Notification noti = new Notification.Builder()
Selim Cinekce8794f2018-05-23 16:46:05 -07006615 * .setContentTitle(&quot;2 new messages with &quot; + sender.toString())
Alex Hillsfc737de2016-03-23 17:33:02 -04006616 * .setContentText(subject)
6617 * .setSmallIcon(R.drawable.new_message)
6618 * .setLargeIcon(aBitmap)
Selim Cinekce8794f2018-05-23 16:46:05 -07006619 * .setStyle(style)
Alex Hillsfc737de2016-03-23 17:33:02 -04006620 * .build();
6621 * </pre>
6622 */
6623 public static class MessagingStyle extends Style {
6624
6625 /**
6626 * The maximum number of messages that will be retained in the Notification itself (the
6627 * number displayed is up to the platform).
6628 */
6629 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6630
Selim Cinekcb8b9852017-12-15 18:01:52 -08006631 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006632 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006633 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006634 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006635 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006636
6637 MessagingStyle() {
6638 }
6639
6640 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006641 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6642 * user before the posting app reposts the notification with those messages after they've
6643 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006644 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006645 *
6646 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006647 */
Alex Hillsfd590442016-10-07 09:52:44 -04006648 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinek9acd6732018-03-23 16:39:02 -07006649 this(new Person.Builder().setName(userDisplayName).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006650 }
6651
6652 /**
6653 * @param user Required - The person displayed for any messages that are sent by the
6654 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6655 * who don't have a Person associated with it will be displayed as if they were sent
Selim Cinek90343862018-02-01 11:07:11 -08006656 * by this user. The user also needs to have a valid name associated with it, which will
6657 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006658 */
6659 public MessagingStyle(@NonNull Person user) {
6660 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006661 }
6662
6663 /**
6664 * Validate that this style was properly composed. This is called at build time.
6665 * @hide
6666 */
6667 @Override
6668 public void validate(Context context) {
6669 super.validate(context);
6670 if (context.getApplicationInfo().targetSdkVersion
6671 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6672 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006673 }
6674 }
6675
6676 /**
Selim Cinekaa9db1f2018-02-27 17:35:47 -08006677 * @return the the text that should be displayed in the statusBar when heads upped.
6678 * If {@code null} is returned, the default implementation will be used.
6679 *
6680 * @hide
6681 */
6682 @Override
6683 public CharSequence getHeadsUpStatusBarText() {
6684 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
6685 ? super.mBigContentTitle
6686 : mConversationTitle;
6687 if (!TextUtils.isEmpty(conversationTitle) && !hasOnlyWhiteSpaceSenders()) {
6688 return conversationTitle;
6689 }
6690 return null;
6691 }
6692
6693 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08006694 * @return the user to be displayed for any replies sent by the user
6695 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006696 @NonNull
Selim Cinekcb8b9852017-12-15 18:01:52 -08006697 public Person getUser() {
6698 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006699 }
6700
6701 /**
6702 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006703 *
6704 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006705 */
6706 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006707 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006708 }
6709
6710 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006711 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6712 *
Kodlee Yin14656422017-12-22 17:00:46 -08006713 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6714 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6715 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6716 * {@code true} and passing {@code null} will make it return {@code false}. In
6717 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6718 * to set group conversation status.
6719 *
6720 * @param conversationTitle Title displayed for this conversation
6721 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006722 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006723 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006724 mConversationTitle = conversationTitle;
6725 return this;
6726 }
6727
6728 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006729 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006730 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006731 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006732 public CharSequence getConversationTitle() {
6733 return mConversationTitle;
6734 }
6735
6736 /**
6737 * Adds a message for display by this notification. Convenience call for a simple
6738 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6739 * @param text A {@link CharSequence} to be displayed as the message content
6740 * @param timestamp Time at which the message arrived
6741 * @param sender A {@link CharSequence} to be used for displaying the name of the
6742 * sender. Should be <code>null</code> for messages by the current user, in which case
6743 * the platform will insert {@link #getUserDisplayName()}.
6744 * Should be unique amongst all individuals in the conversation, and should be
6745 * consistent during re-posts of the notification.
6746 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006747 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Alex Hillsfc737de2016-03-23 17:33:02 -04006748 *
6749 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006750 *
6751 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006752 */
6753 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006754 return addMessage(text, timestamp,
Selim Cinek9acd6732018-03-23 16:39:02 -07006755 sender == null ? null : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08006756 }
6757
6758 /**
6759 * Adds a message for display by this notification. Convenience call for a simple
6760 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6761 * @param text A {@link CharSequence} to be displayed as the message content
6762 * @param timestamp Time at which the message arrived
6763 * @param sender The {@link Person} who sent the message.
6764 * Should be <code>null</code> for messages by the current user, in which case
6765 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6766 *
Aurimas Liutikase701dc12018-06-01 16:04:37 -07006767 * @see Message#Notification.MessagingStyle.Message(CharSequence, long, CharSequence)
Selim Cinekcb8b9852017-12-15 18:01:52 -08006768 *
6769 * @return this object for method chaining
6770 */
Selim Cinekeb53c222018-04-06 12:56:09 -07006771 public MessagingStyle addMessage(@NonNull CharSequence text, long timestamp,
6772 @Nullable Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006773 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006774 }
6775
6776 /**
6777 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006778 *
6779 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6780 * the newest last.
6781 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006782 * @param message The {@link Message} to be displayed
6783 * @return this object for method chaining
6784 */
6785 public MessagingStyle addMessage(Message message) {
6786 mMessages.add(message);
6787 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6788 mMessages.remove(0);
6789 }
6790 return this;
6791 }
6792
6793 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006794 * Adds a {@link Message} for historic context in this notification.
6795 *
6796 * <p>Messages should be added as historic if they are not the main subject of the
6797 * notification but may give context to a conversation. The system may choose to present
6798 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6799 *
6800 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6801 * the newest last.
6802 *
6803 * @param message The historic {@link Message} to be added
6804 * @return this object for method chaining
6805 */
6806 public MessagingStyle addHistoricMessage(Message message) {
6807 mHistoricMessages.add(message);
6808 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6809 mHistoricMessages.remove(0);
6810 }
6811 return this;
6812 }
6813
6814 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006815 * Gets the list of {@code Message} objects that represent the notification
6816 */
6817 public List<Message> getMessages() {
6818 return mMessages;
6819 }
6820
6821 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006822 * Gets the list of historic {@code Message}s in the notification.
6823 */
6824 public List<Message> getHistoricMessages() {
6825 return mHistoricMessages;
6826 }
6827
6828 /**
Selim Cinekce8794f2018-05-23 16:46:05 -07006829 * Sets whether this conversation notification represents a group. If the app is targeting
6830 * Android P, this is required if the app wants to display the largeIcon set with
6831 * {@link Notification.Builder#setLargeIcon(Bitmap)}, otherwise it will be hidden.
Kodlee Yin14656422017-12-22 17:00:46 -08006832 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006833 * @param isGroupConversation {@code true} if the conversation represents a group,
6834 * {@code false} otherwise.
6835 * @return this object for method chaining
6836 */
6837 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6838 mIsGroupConversation = isGroupConversation;
6839 return this;
6840 }
6841
6842 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006843 * Returns {@code true} if this notification represents a group conversation, otherwise
6844 * {@code false}.
6845 *
6846 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6847 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6848 * not the conversation title is set; returning {@code true} if the conversation title is
6849 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6850 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6851 * named, non-group conversations.
6852 *
6853 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006854 */
6855 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006856 // When target SDK version is < P, a non-null conversation title dictates if this is
6857 // as group conversation.
6858 if (mBuilder != null
6859 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6860 < Build.VERSION_CODES.P) {
6861 return mConversationTitle != null;
6862 }
6863
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006864 return mIsGroupConversation;
6865 }
6866
6867 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006868 * @hide
6869 */
6870 @Override
6871 public void addExtras(Bundle extras) {
6872 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006873 if (mUser != null) {
6874 // For legacy usages
6875 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6876 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006877 }
6878 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006879 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006880 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006881 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6882 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006883 }
Adrian Roos437cd562017-01-18 15:47:03 -08006884 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6885 Message.getBundleArrayForMessages(mHistoricMessages));
6886 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006887
6888 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006889 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006890 }
6891
6892 private void fixTitleAndTextExtras(Bundle extras) {
6893 Message m = findLatestIncomingMessage();
6894 CharSequence text = (m == null) ? null : m.mText;
6895 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006896 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6897 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006898 CharSequence title;
6899 if (!TextUtils.isEmpty(mConversationTitle)) {
6900 if (!TextUtils.isEmpty(sender)) {
6901 BidiFormatter bidi = BidiFormatter.getInstance();
6902 title = mBuilder.mContext.getString(
6903 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006904 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006905 } else {
6906 title = mConversationTitle;
6907 }
6908 } else {
6909 title = sender;
6910 }
6911
6912 if (title != null) {
6913 extras.putCharSequence(EXTRA_TITLE, title);
6914 }
6915 if (text != null) {
6916 extras.putCharSequence(EXTRA_TEXT, text);
6917 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006918 }
6919
6920 /**
6921 * @hide
6922 */
6923 @Override
6924 protected void restoreFromExtras(Bundle extras) {
6925 super.restoreFromExtras(extras);
6926
Selim Cinekcb8b9852017-12-15 18:01:52 -08006927 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6928 if (mUser == null) {
6929 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
Selim Cinek9acd6732018-03-23 16:39:02 -07006930 mUser = new Person.Builder().setName(displayName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08006931 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006932 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006933 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006934 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006935 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006936 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006937 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006938 }
6939
6940 /**
6941 * @hide
6942 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006943 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006944 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006945 mBuilder.mOriginalActions = mBuilder.mActions;
6946 mBuilder.mActions = new ArrayList<>();
Selim Cinek85d0e6e2018-03-23 18:08:32 -07006947 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08006948 false /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006949 mBuilder.mActions = mBuilder.mOriginalActions;
6950 mBuilder.mOriginalActions = null;
6951 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006952 }
6953
Julia Reynolds7217dc92018-03-07 12:12:09 -05006954 /**
6955 * @hide
Dan Sandler7d67bd42018-05-15 14:06:38 -04006956 * Spans are ignored when comparing text for visual difference.
Julia Reynolds7217dc92018-03-07 12:12:09 -05006957 */
6958 @Override
6959 public boolean areNotificationsVisiblyDifferent(Style other) {
6960 if (other == null || getClass() != other.getClass()) {
6961 return true;
6962 }
6963 MessagingStyle newS = (MessagingStyle) other;
6964 List<MessagingStyle.Message> oldMs = getMessages();
6965 List<MessagingStyle.Message> newMs = newS.getMessages();
6966
Dan Sandler7d67bd42018-05-15 14:06:38 -04006967 if (oldMs == null || newMs == null) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006968 newMs = new ArrayList<>();
6969 }
6970
6971 int n = oldMs.size();
6972 if (n != newMs.size()) {
6973 return true;
6974 }
6975 for (int i = 0; i < n; i++) {
6976 MessagingStyle.Message oldM = oldMs.get(i);
6977 MessagingStyle.Message newM = newMs.get(i);
Dan Sandler7d67bd42018-05-15 14:06:38 -04006978 if (!Objects.equals(
6979 String.valueOf(oldM.getText()),
6980 String.valueOf(newM.getText()))) {
Julia Reynolds7217dc92018-03-07 12:12:09 -05006981 return true;
6982 }
6983 if (!Objects.equals(oldM.getDataUri(), newM.getDataUri())) {
6984 return true;
6985 }
Dan Sandler7d67bd42018-05-15 14:06:38 -04006986 String oldSender = String.valueOf(oldM.getSenderPerson() == null
6987 ? oldM.getSender()
6988 : oldM.getSenderPerson().getName());
6989 String newSender = String.valueOf(newM.getSenderPerson() == null
6990 ? newM.getSender()
6991 : newM.getSenderPerson().getName());
Julia Reynolds7217dc92018-03-07 12:12:09 -05006992 if (!Objects.equals(oldSender, newSender)) {
6993 return true;
6994 }
6995
6996 String oldKey = oldM.getSenderPerson() == null
6997 ? null : oldM.getSenderPerson().getKey();
6998 String newKey = newM.getSenderPerson() == null
6999 ? null : newM.getSenderPerson().getKey();
7000 if (!Objects.equals(oldKey, newKey)) {
7001 return true;
7002 }
7003 // Other fields (like timestamp) intentionally excluded
7004 }
7005 return false;
7006 }
7007
Adrian Roosc1a80b02016-04-05 14:54:55 -07007008 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07007009 return findLatestIncomingMessage(mMessages);
7010 }
7011
7012 /**
7013 * @hide
7014 */
7015 @Nullable
7016 public static Message findLatestIncomingMessage(
7017 List<Message> messages) {
7018 for (int i = messages.size() - 1; i >= 0; i--) {
7019 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07007020 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007021 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07007022 return m;
7023 }
7024 }
Selim Cinek88188f22017-09-19 16:46:56 -07007025 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007026 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07007027 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07007028 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07007029 return null;
7030 }
7031
7032 /**
7033 * @hide
7034 */
7035 @Override
7036 public RemoteViews makeBigContentView() {
Selim Cinek384804b2018-04-18 14:31:07 +08007037 return makeMessagingView(false /* displayImagesAtEnd */, true /* hideLargeIcon */);
Selim Cinekafeed292017-12-12 17:32:44 -08007038 }
7039
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007040 /**
7041 * Create a messaging layout.
7042 *
7043 * @param displayImagesAtEnd should images be displayed at the end of the content instead
7044 * of inline.
Selim Cinek384804b2018-04-18 14:31:07 +08007045 * @param hideRightIcons Should the reply affordance be shown at the end of the notification
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007046 * @return the created remoteView.
7047 */
Selim Cinekafeed292017-12-12 17:32:44 -08007048 @NonNull
Selim Cinek384804b2018-04-18 14:31:07 +08007049 private RemoteViews makeMessagingView(boolean displayImagesAtEnd, boolean hideRightIcons) {
Selim Cinek88188f22017-09-19 16:46:56 -07007050 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07007051 ? super.mBigContentTitle
7052 : mConversationTitle;
Selim Cinekce8794f2018-05-23 16:46:05 -07007053 boolean atLeastP = mBuilder.mContext.getApplicationInfo().targetSdkVersion
7054 >= Build.VERSION_CODES.P;
7055 boolean isOneToOne;
Selim Cinek2dd3e722018-01-19 11:06:06 -08007056 CharSequence nameReplacement = null;
Selim Cinekce8794f2018-05-23 16:46:05 -07007057 Icon avatarReplacement = null;
7058 if (!atLeastP) {
7059 isOneToOne = TextUtils.isEmpty(conversationTitle);
7060 avatarReplacement = mBuilder.mN.mLargeIcon;
7061 if (hasOnlyWhiteSpaceSenders()) {
7062 isOneToOne = true;
7063 nameReplacement = conversationTitle;
7064 conversationTitle = null;
7065 }
7066 } else {
7067 isOneToOne = !isGroupConversation();
Adrian Roosb1f427c2016-05-26 12:27:15 -07007068 }
Selim Cinek384804b2018-04-18 14:31:07 +08007069 TemplateBindResult bindResult = new TemplateBindResult();
Adrian Roos48d746a2016-04-12 14:57:28 -07007070 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07007071 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07007072 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek384804b2018-04-18 14:31:07 +08007073 .hideLargeIcon(hideRightIcons || isOneToOne)
7074 .hideReplyIcon(hideRightIcons)
7075 .headerTextSecondary(conversationTitle),
7076 bindResult);
Selim Cinek88188f22017-09-19 16:46:56 -07007077 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08007078 // also update the end margin if there is an image
Selim Cinek1c72fa02018-04-23 18:00:54 +08007079 contentView.setViewLayoutMarginEnd(R.id.notification_messaging,
7080 bindResult.getIconMarginEnd());
Selim Cinek88188f22017-09-19 16:46:56 -07007081 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
Kenny Guy14d035c2018-05-02 19:10:36 +01007082 mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7083 : mBuilder.resolveContrastColor());
7084 contentView.setInt(R.id.status_bar_latest_event_content, "setSenderTextColor",
7085 mBuilder.getPrimaryTextColor());
7086 contentView.setInt(R.id.status_bar_latest_event_content, "setMessageTextColor",
7087 mBuilder.getSecondaryTextColor());
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007088 contentView.setBoolean(R.id.status_bar_latest_event_content, "setDisplayImagesAtEnd",
7089 displayImagesAtEnd);
Selim Cinekce8794f2018-05-23 16:46:05 -07007090 contentView.setIcon(R.id.status_bar_latest_event_content, "setAvatarReplacement",
7091 avatarReplacement);
Selim Cinek2dd3e722018-01-19 11:06:06 -08007092 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
7093 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07007094 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
7095 isOneToOne);
7096 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
7097 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04007098 return contentView;
7099 }
7100
Selim Cinekf7409db2017-10-24 16:17:14 -07007101 private boolean hasOnlyWhiteSpaceSenders() {
7102 for (int i = 0; i < mMessages.size(); i++) {
7103 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007104 Person sender = m.getSenderPerson();
7105 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07007106 return false;
7107 }
7108 }
7109 return true;
7110 }
7111
7112 private boolean isWhiteSpace(CharSequence sender) {
7113 if (TextUtils.isEmpty(sender)) {
7114 return true;
7115 }
7116 if (sender.toString().matches("^\\s*$")) {
7117 return true;
7118 }
7119 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
7120 // For the presentation that we had.
7121 for (int i = 0; i < sender.length(); i++) {
7122 char c = sender.charAt(i);
7123 if (c != '\u200B') {
7124 return false;
7125 }
7126 }
7127 return true;
7128 }
7129
Selim Cinek88188f22017-09-19 16:46:56 -07007130 private CharSequence createConversationTitleFromMessages() {
7131 ArraySet<CharSequence> names = new ArraySet<>();
7132 for (int i = 0; i < mMessages.size(); i++) {
7133 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08007134 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07007135 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007136 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07007137 }
7138 }
7139 SpannableStringBuilder title = new SpannableStringBuilder();
7140 int size = names.size();
7141 for (int i = 0; i < size; i++) {
7142 CharSequence name = names.valueAt(i);
7143 if (!TextUtils.isEmpty(title)) {
7144 title.append(", ");
7145 }
7146 title.append(BidiFormatter.getInstance().unicodeWrap(name));
7147 }
7148 return title;
7149 }
7150
Adrian Roosdedd1df2016-04-26 16:38:47 -07007151 /**
7152 * @hide
7153 */
7154 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007155 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek85d0e6e2018-03-23 18:08:32 -07007156 RemoteViews remoteViews = makeMessagingView(true /* displayImagesAtEnd */,
Selim Cinek384804b2018-04-18 14:31:07 +08007157 true /* hideLargeIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07007158 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
7159 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07007160 }
7161
Adrian Roosc1a80b02016-04-05 14:54:55 -07007162 private static TextAppearanceSpan makeFontColorSpan(int color) {
7163 return new TextAppearanceSpan(null, 0, 0,
7164 ColorStateList.valueOf(color), null);
7165 }
7166
Alex Hillsd9b04d92016-04-11 16:38:16 -04007167 public static final class Message {
Tony Mak09db2ea2018-06-27 18:12:48 +01007168 /** @hide */
7169 public static final String KEY_TEXT = "text";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007170 static final String KEY_TIMESTAMP = "time";
7171 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08007172 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04007173 static final String KEY_DATA_MIME_TYPE = "type";
7174 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08007175 static final String KEY_EXTRAS_BUNDLE = "extras";
Kenny Guya0f6de82018-04-06 16:20:16 +01007176 static final String KEY_REMOTE_INPUT_HISTORY = "remote_input_history";
Alex Hillsfc737de2016-03-23 17:33:02 -04007177
7178 private final CharSequence mText;
7179 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08007180 @Nullable
7181 private final Person mSender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007182 /** True if this message was generated from the extra
7183 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}
7184 */
7185 private final boolean mRemoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007186
Shane Brennan5a871862017-03-11 13:14:17 -08007187 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007188 private String mDataMimeType;
7189 private Uri mDataUri;
7190
7191 /**
7192 * Constructor
7193 * @param text A {@link CharSequence} to be displayed as the message content
7194 * @param timestamp Time at which the message arrived
7195 * @param sender A {@link CharSequence} to be used for displaying the name of the
7196 * sender. Should be <code>null</code> for messages by the current user, in which case
7197 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
7198 * Should be unique amongst all individuals in the conversation, and should be
7199 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007200 *
7201 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04007202 */
7203 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinek9acd6732018-03-23 16:39:02 -07007204 this(text, timestamp, sender == null ? null
7205 : new Person.Builder().setName(sender).build());
Selim Cinekcb8b9852017-12-15 18:01:52 -08007206 }
7207
7208 /**
7209 * Constructor
7210 * @param text A {@link CharSequence} to be displayed as the message content
7211 * @param timestamp Time at which the message arrived
7212 * @param sender The {@link Person} who sent the message.
7213 * Should be <code>null</code> for messages by the current user, in which case
7214 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7215 * <p>
Selim Cinek9acd6732018-03-23 16:39:02 -07007216 * The person provided should contain an Icon, set with
7217 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7218 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7219 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7220 * to differentiate between the different users.
Selim Cinekcb8b9852017-12-15 18:01:52 -08007221 * </p>
7222 */
Selim Cinekeb53c222018-04-06 12:56:09 -07007223 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender) {
Kenny Guya0f6de82018-04-06 16:20:16 +01007224 this(text, timestamp, sender, false /* remoteHistory */);
7225 }
7226
7227 /**
7228 * Constructor
7229 * @param text A {@link CharSequence} to be displayed as the message content
7230 * @param timestamp Time at which the message arrived
7231 * @param sender The {@link Person} who sent the message.
7232 * Should be <code>null</code> for messages by the current user, in which case
7233 * the platform will insert the user set in {@code MessagingStyle(Person)}.
7234 * @param remoteInputHistory True if the messages was generated from the extra
7235 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7236 * <p>
7237 * The person provided should contain an Icon, set with
7238 * {@link Person.Builder#setIcon(Icon)} and also have a name provided
7239 * with {@link Person.Builder#setName(CharSequence)}. If multiple users have the same
7240 * name, consider providing a key with {@link Person.Builder#setKey(String)} in order
7241 * to differentiate between the different users.
7242 * </p>
7243 * @hide
7244 */
7245 public Message(@NonNull CharSequence text, long timestamp, @Nullable Person sender,
7246 boolean remoteInputHistory) {
Alex Hillsfc737de2016-03-23 17:33:02 -04007247 mText = text;
7248 mTimestamp = timestamp;
7249 mSender = sender;
Kenny Guya0f6de82018-04-06 16:20:16 +01007250 mRemoteInputHistory = remoteInputHistory;
Alex Hillsfc737de2016-03-23 17:33:02 -04007251 }
7252
7253 /**
7254 * Sets a binary blob of data and an associated MIME type for a message. In the case
7255 * where the platform doesn't support the MIME type, the original text provided in the
7256 * constructor will be used.
7257 * @param dataMimeType The MIME type of the content. See
7258 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
7259 * types on Android and Android Wear.
7260 * @param dataUri The uri containing the content whose type is given by the MIME type.
7261 * <p class="note">
7262 * <ol>
7263 * <li>Notification Listeners including the System UI need permission to access the
7264 * data the Uri points to. The recommended ways to do this are:</li>
7265 * <li>Store the data in your own ContentProvider, making sure that other apps have
7266 * the correct permission to access your provider. The preferred mechanism for
7267 * providing access is to use per-URI permissions which are temporary and only
7268 * grant access to the receiving application. An easy way to create a
7269 * ContentProvider like this is to use the FileProvider helper class.</li>
7270 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
7271 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
7272 * also store non-media types (see MediaStore.Files for more info). Files can be
7273 * inserted into the MediaStore using scanFile() after which a content:// style
7274 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
7275 * Note that once added to the system MediaStore the content is accessible to any
7276 * app on the device.</li>
7277 * </ol>
7278 * @return this object for method chaining
7279 */
7280 public Message setData(String dataMimeType, Uri dataUri) {
7281 mDataMimeType = dataMimeType;
7282 mDataUri = dataUri;
7283 return this;
7284 }
7285
Alex Hillsfc737de2016-03-23 17:33:02 -04007286 /**
7287 * Get the text to be used for this message, or the fallback text if a type and content
7288 * Uri have been set
7289 */
7290 public CharSequence getText() {
7291 return mText;
7292 }
7293
7294 /**
7295 * Get the time at which this message arrived
7296 */
7297 public long getTimestamp() {
7298 return mTimestamp;
7299 }
7300
7301 /**
Shane Brennan5a871862017-03-11 13:14:17 -08007302 * Get the extras Bundle for this message.
7303 */
7304 public Bundle getExtras() {
7305 return mExtras;
7306 }
7307
7308 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04007309 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08007310 *
7311 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04007312 */
7313 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007314 return mSender == null ? null : mSender.getName();
7315 }
7316
7317 /**
7318 * Get the sender associated with this message.
7319 */
7320 @Nullable
7321 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04007322 return mSender;
7323 }
7324
7325 /**
7326 * Get the MIME type of the data pointed to by the Uri
7327 */
7328 public String getDataMimeType() {
7329 return mDataMimeType;
7330 }
7331
7332 /**
7333 * Get the the Uri pointing to the content of the message. Can be null, in which case
7334 * {@see #getText()} is used.
7335 */
7336 public Uri getDataUri() {
7337 return mDataUri;
7338 }
7339
Kenny Guya0f6de82018-04-06 16:20:16 +01007340 /**
7341 * @return True if the message was generated from
7342 * {@link Notification#EXTRA_REMOTE_INPUT_HISTORY}.
7343 * @hide
7344 */
7345 public boolean isRemoteInputHistory() {
7346 return mRemoteInputHistory;
7347 }
7348
Alex Hillsd9b04d92016-04-11 16:38:16 -04007349 private Bundle toBundle() {
7350 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04007351 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007352 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04007353 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007354 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04007355 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007356 // Legacy listeners need this
7357 bundle.putCharSequence(KEY_SENDER, mSender.getName());
7358 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04007359 }
7360 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007361 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04007362 }
7363 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007364 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04007365 }
Shane Brennan5a871862017-03-11 13:14:17 -08007366 if (mExtras != null) {
7367 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
7368 }
Kenny Guya0f6de82018-04-06 16:20:16 +01007369 if (mRemoteInputHistory) {
7370 bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, mRemoteInputHistory);
7371 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007372 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04007373 }
7374
Alex Hillsd9b04d92016-04-11 16:38:16 -04007375 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
7376 Bundle[] bundles = new Bundle[messages.size()];
7377 final int N = messages.size();
7378 for (int i = 0; i < N; i++) {
7379 bundles[i] = messages.get(i).toBundle();
7380 }
7381 return bundles;
7382 }
7383
Selim Cinek88188f22017-09-19 16:46:56 -07007384 /**
7385 * @return A list of messages read from the bundles.
7386 *
7387 * @hide
7388 */
7389 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
7390 if (bundles == null) {
7391 return new ArrayList<>();
7392 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007393 List<Message> messages = new ArrayList<>(bundles.length);
7394 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07007395 if (bundles[i] instanceof Bundle) {
7396 Message message = getMessageFromBundle((Bundle)bundles[i]);
7397 if (message != null) {
7398 messages.add(message);
7399 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007400 }
7401 }
7402 return messages;
7403 }
7404
Selim Cinekb0dc61b2018-05-22 18:49:36 -07007405 /**
7406 * @return The message that is stored in the bundle or null if the message couldn't be
7407 * resolved.
7408 *
7409 * @hide
7410 */
7411 @Nullable
7412 public static Message getMessageFromBundle(Bundle bundle) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007413 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07007414 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007415 return null;
7416 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08007417
7418 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
7419 if (senderPerson == null) {
7420 // Legacy apps that use compat don't actually provide the sender objects
7421 // We need to fix the compat version to provide people / use
7422 // the native api instead
7423 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
7424 if (senderName != null) {
Selim Cinek9acd6732018-03-23 16:39:02 -07007425 senderPerson = new Person.Builder().setName(senderName).build();
Selim Cinekcb8b9852017-12-15 18:01:52 -08007426 }
7427 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007428 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08007429 bundle.getLong(KEY_TIMESTAMP),
Kenny Guya0f6de82018-04-06 16:20:16 +01007430 senderPerson,
7431 bundle.getBoolean(KEY_REMOTE_INPUT_HISTORY, false));
Alex Hillsd9b04d92016-04-11 16:38:16 -04007432 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
7433 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04007434 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
7435 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04007436 }
Shane Brennan5a871862017-03-11 13:14:17 -08007437 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
7438 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
7439 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04007440 return message;
7441 }
7442 } catch (ClassCastException e) {
7443 return null;
7444 }
7445 }
Alex Hillsfc737de2016-03-23 17:33:02 -04007446 }
7447 }
7448
7449 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04007450 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08007451 *
Robert Ly91c5ce32014-06-08 15:37:00 -07007452 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04007453 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07007454 * Notification notif = new Notification.Builder(mContext)
7455 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
7456 * .setContentText(subject)
7457 * .setSmallIcon(R.drawable.new_mail)
7458 * .setLargeIcon(aBitmap)
7459 * .setStyle(new Notification.InboxStyle()
7460 * .addLine(str1)
7461 * .addLine(str2)
7462 * .setContentTitle(&quot;&quot;)
7463 * .setSummaryText(&quot;+3 more&quot;))
7464 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04007465 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08007466 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04007467 * @see Notification#bigContentView
7468 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007469 public static class InboxStyle extends Style {
Selim Cinekbee4e072018-05-21 22:06:43 -07007470
7471 /**
7472 * The number of lines of remote input history allowed until we start reducing lines.
7473 */
7474 private static final int NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION = 1;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007475 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
7476
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007477 public InboxStyle() {
7478 }
7479
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007480 /**
7481 * @deprecated use {@code InboxStyle()}.
7482 */
7483 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04007484 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04007485 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04007486 }
7487
Chris Wrend6297db2012-05-03 16:20:13 -04007488 /**
7489 * Overrides ContentTitle in the big form of the template.
7490 * This defaults to the value passed to setContentTitle().
7491 */
7492 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007493 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04007494 return this;
7495 }
7496
7497 /**
7498 * Set the first line of text after the detail section in the big form of the template.
7499 */
7500 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007501 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04007502 return this;
7503 }
7504
Chris Wren0bd664d2012-08-01 13:56:56 -04007505 /**
7506 * Append a line to the digest section of the Inbox notification.
7507 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04007508 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04007509 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04007510 return this;
7511 }
7512
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007513 /**
7514 * @hide
7515 */
Julia Reynolds7217dc92018-03-07 12:12:09 -05007516 public ArrayList<CharSequence> getLines() {
7517 return mTexts;
7518 }
7519
7520 /**
7521 * @hide
7522 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007523 public void addExtras(Bundle extras) {
7524 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007525
Daniel Sandlerf45564e2013-04-15 15:05:08 -04007526 CharSequence[] a = new CharSequence[mTexts.size()];
7527 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
7528 }
7529
Christoph Studer4600f9b2014-07-22 22:44:43 +02007530 /**
7531 * @hide
7532 */
7533 @Override
7534 protected void restoreFromExtras(Bundle extras) {
7535 super.restoreFromExtras(extras);
7536
7537 mTexts.clear();
7538 if (extras.containsKey(EXTRA_TEXT_LINES)) {
7539 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
7540 }
7541 }
7542
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007543 /**
7544 * @hide
7545 */
7546 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08007547 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02007548 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007549 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
7550 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007551
Selim Cinek384804b2018-04-18 14:31:07 +08007552 TemplateBindResult result = new TemplateBindResult();
7553 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource(), result);
Daniel Sandler619738c2012-06-07 16:33:08 -04007554
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007555 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007556
Chris Wrend6297db2012-05-03 16:20:13 -04007557 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 -04007558 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04007559
Chris Wren4ed80d52012-05-17 09:30:03 -04007560 // Make sure all rows are gone in case we reuse a view.
7561 for (int rowId : rowIds) {
7562 contentView.setViewVisibility(rowId, View.GONE);
7563 }
7564
Daniel Sandler879c5e02012-04-17 16:46:51 -04007565 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07007566 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7567 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08007568 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07007569 int onlyViewId = 0;
7570 int maxRows = rowIds.length;
7571 if (mBuilder.mActions.size() > 0) {
7572 maxRows--;
7573 }
Selim Cinekbee4e072018-05-21 22:06:43 -07007574 CharSequence[] remoteInputHistory = mBuilder.mN.extras.getCharSequenceArray(
7575 EXTRA_REMOTE_INPUT_HISTORY);
7576 if (remoteInputHistory != null
7577 && remoteInputHistory.length > NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION) {
7578 // Let's remove some messages to make room for the remote input history.
7579 // 1 is always able to fit, but let's remove them if they are 2 or 3
7580 int numRemoteInputs = Math.min(remoteInputHistory.length,
7581 MAX_REMOTE_INPUT_HISTORY_LINES);
7582 int totalNumRows = mTexts.size() + numRemoteInputs
7583 - NUMBER_OF_HISTORY_ALLOWED_UNTIL_REDUCTION;
7584 if (totalNumRows > maxRows) {
7585 int overflow = totalNumRows - maxRows;
7586 if (mTexts.size() > maxRows) {
7587 // Heuristic: if the Texts don't fit anyway, we'll rather drop the last
7588 // few messages, even with the remote input
7589 maxRows -= overflow;
7590 } else {
7591 // otherwise we drop the first messages
7592 i = overflow;
7593 }
7594 }
7595 }
Selim Cinek07c80172016-04-21 16:40:47 -07007596 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007597 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07007598 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04007599 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07007600 contentView.setTextViewText(rowIds[i],
7601 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08007602 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07007603 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek384804b2018-04-18 14:31:07 +08007604 handleInboxImageMargin(contentView, rowIds[i], first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007605 result.getIconMarginEnd());
Selim Cinek07c80172016-04-21 16:40:47 -07007606 if (first) {
7607 onlyViewId = rowIds[i];
7608 } else {
7609 onlyViewId = 0;
7610 }
Selim Cinek247fa012016-02-18 09:50:48 -08007611 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04007612 }
7613 i++;
7614 }
Selim Cinek07c80172016-04-21 16:40:47 -07007615 if (onlyViewId != 0) {
7616 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
7617 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
7618 R.dimen.notification_text_margin_top);
7619 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
7620 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007621
Daniel Sandler879c5e02012-04-17 16:46:51 -04007622 return contentView;
7623 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007624
Julia Reynolds7217dc92018-03-07 12:12:09 -05007625 /**
7626 * @hide
7627 */
7628 @Override
7629 public boolean areNotificationsVisiblyDifferent(Style other) {
7630 if (other == null || getClass() != other.getClass()) {
7631 return true;
7632 }
7633 InboxStyle newS = (InboxStyle) other;
Dan Sandler7d67bd42018-05-15 14:06:38 -04007634
7635 final ArrayList<CharSequence> myLines = getLines();
7636 final ArrayList<CharSequence> newLines = newS.getLines();
7637 final int n = myLines.size();
7638 if (n != newLines.size()) {
7639 return true;
7640 }
7641
7642 for (int i = 0; i < n; i++) {
7643 if (!Objects.equals(
7644 String.valueOf(myLines.get(i)),
7645 String.valueOf(newLines.get(i)))) {
7646 return true;
7647 }
7648 }
7649 return false;
Julia Reynolds7217dc92018-03-07 12:12:09 -05007650 }
7651
Selim Cinek384804b2018-04-18 14:31:07 +08007652 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first,
Selim Cinek1c72fa02018-04-23 18:00:54 +08007653 int marginEndValue) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08007654 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08007655 if (first) {
7656 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
7657 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
7658 boolean hasProgress = max != 0 || ind;
Selim Cinek384804b2018-04-18 14:31:07 +08007659 if (!hasProgress) {
Selim Cinek1c72fa02018-04-23 18:00:54 +08007660 endMargin = marginEndValue;
Selim Cinek247fa012016-02-18 09:50:48 -08007661 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08007662 }
Selim Cinek1c72fa02018-04-23 18:00:54 +08007663 contentView.setViewLayoutMarginEnd(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08007664 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04007665 }
Dan Sandler842dd772014-05-15 09:36:47 -04007666
7667 /**
7668 * Notification style for media playback notifications.
7669 *
7670 * In the expanded form, {@link Notification#bigContentView}, up to 5
7671 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04007672 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04007673 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7674 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7675 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007676 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007677 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7678 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007679 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007680 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007681 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007682 * Notifications created with MediaStyle will have their category set to
7683 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7684 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007685 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007686 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7687 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007688 * the System UI can identify this as a notification representing an active media session
7689 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7690 *
Selim Cinek99104832017-01-25 14:47:33 -08007691 * <p>
7692 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7693 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7694 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7695 * <p>
7696 *
Dan Sandler842dd772014-05-15 09:36:47 -04007697 * To use this style with your Notification, feed it to
7698 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7699 * <pre class="prettyprint">
7700 * Notification noti = new Notification.Builder()
7701 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007702 * .setContentTitle(&quot;Track title&quot;)
7703 * .setContentText(&quot;Artist - Album&quot;)
7704 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007705 * .setStyle(<b>new Notification.MediaStyle()</b>
7706 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007707 * .build();
7708 * </pre>
7709 *
7710 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007711 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007712 */
7713 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007714 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007715 static final int MAX_MEDIA_BUTTONS = 5;
7716
7717 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007718 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007719
7720 public MediaStyle() {
7721 }
7722
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007723 /**
7724 * @deprecated use {@code MediaStyle()}.
7725 */
7726 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007727 public MediaStyle(Builder builder) {
7728 setBuilder(builder);
7729 }
7730
7731 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007732 * 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 -04007733 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007734 *
7735 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007736 */
7737 public MediaStyle setShowActionsInCompactView(int...actions) {
7738 mActionsToShowInCompact = actions;
7739 return this;
7740 }
7741
7742 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007743 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7744 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007745 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007746 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007747 mToken = token;
7748 return this;
7749 }
7750
Christoph Studer4600f9b2014-07-22 22:44:43 +02007751 /**
7752 * @hide
7753 */
Dan Sandler842dd772014-05-15 09:36:47 -04007754 @Override
7755 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007756 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007757 if (wip.category == null) {
7758 wip.category = Notification.CATEGORY_TRANSPORT;
7759 }
Dan Sandler842dd772014-05-15 09:36:47 -04007760 return wip;
7761 }
7762
Christoph Studer4600f9b2014-07-22 22:44:43 +02007763 /**
7764 * @hide
7765 */
7766 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007767 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007768 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007769 }
7770
7771 /**
7772 * @hide
7773 */
7774 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007775 public RemoteViews makeBigContentView() {
7776 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007777 }
7778
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007779 /**
7780 * @hide
7781 */
7782 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007783 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007784 RemoteViews expanded = makeMediaBigContentView();
7785 return expanded != null ? expanded : makeMediaContentView();
7786 }
7787
Dan Sandler842dd772014-05-15 09:36:47 -04007788 /** @hide */
7789 @Override
7790 public void addExtras(Bundle extras) {
7791 super.addExtras(extras);
7792
7793 if (mToken != null) {
7794 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7795 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007796 if (mActionsToShowInCompact != null) {
7797 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7798 }
Dan Sandler842dd772014-05-15 09:36:47 -04007799 }
7800
Christoph Studer4600f9b2014-07-22 22:44:43 +02007801 /**
7802 * @hide
7803 */
7804 @Override
7805 protected void restoreFromExtras(Bundle extras) {
7806 super.restoreFromExtras(extras);
7807
7808 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7809 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7810 }
7811 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7812 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7813 }
7814 }
7815
Julia Reynolds7217dc92018-03-07 12:12:09 -05007816 /**
7817 * @hide
7818 */
7819 @Override
7820 public boolean areNotificationsVisiblyDifferent(Style other) {
7821 if (other == null || getClass() != other.getClass()) {
7822 return true;
7823 }
7824 // All fields to compare are on the Notification object
7825 return false;
7826 }
7827
Selim Cinek5bf069a2015-11-10 19:14:27 -05007828 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007829 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007830 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007831 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007832 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007833
7834 // If the action buttons should not be tinted, then just use the default
7835 // notification color. Otherwise, just use the passed-in color.
7836 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7837 ? color
Lucas Dupina291d192018-06-07 13:59:42 -07007838 : ContrastColorUtil.resolveColor(mBuilder.mContext,
Anthony Chenad4d1582017-04-10 16:07:58 -07007839 Notification.COLOR_DEFAULT);
7840
Sunny Goyal5b153922017-09-21 21:00:36 -07007841 button.setDrawableTint(R.id.action0, false, tintColor,
7842 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007843 if (!tombstone) {
7844 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7845 }
7846 button.setContentDescription(R.id.action0, action.title);
7847 return button;
7848 }
7849
7850 private RemoteViews makeMediaContentView() {
7851 RemoteViews view = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007852 R.layout.notification_template_material_media, false, /* hasProgress */
7853 null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007854
7855 final int numActions = mBuilder.mActions.size();
7856 final int N = mActionsToShowInCompact == null
7857 ? 0
7858 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7859 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007860 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007861 for (int i = 0; i < N; i++) {
7862 if (i >= numActions) {
7863 throw new IllegalArgumentException(String.format(
7864 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7865 i, numActions - 1));
7866 }
7867
7868 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinekc7f5a822018-03-20 19:32:06 -07007869 final RemoteViews button = generateMediaActionButton(action, getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007870 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007871 }
7872 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007873 handleImage(view);
7874 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007875 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007876 if (mBuilder.mN.hasLargeIcon()) {
Selim Cinek384804b2018-04-18 14:31:07 +08007877 endMargin = R.dimen.notification_media_image_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007878 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007879 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007880 return view;
7881 }
7882
Selim Cinekc7f5a822018-03-20 19:32:06 -07007883 private int getActionColor() {
7884 return mBuilder.isColorized() ? mBuilder.getPrimaryTextColor()
7885 : mBuilder.resolveContrastColor();
Selim Cinek99104832017-01-25 14:47:33 -08007886 }
7887
Dan Sandler842dd772014-05-15 09:36:47 -04007888 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007889 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007890 // Dont add an expanded view if there is no more content to be revealed
7891 int actionsInCompact = mActionsToShowInCompact == null
7892 ? 0
7893 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007894 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007895 return null;
7896 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007897 RemoteViews big = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08007898 R.layout.notification_template_material_big_media, false, null /* result */);
Dan Sandler842dd772014-05-15 09:36:47 -04007899
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007900 if (actionCount > 0) {
7901 big.removeAllViews(com.android.internal.R.id.media_actions);
7902 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007903 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinekc7f5a822018-03-20 19:32:06 -07007904 getActionColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007905 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007906 }
7907 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007908 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007909 return big;
7910 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007911
Selim Cinek5bf069a2015-11-10 19:14:27 -05007912 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007913 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007914 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7915 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007916 }
7917 }
7918
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007919 /**
7920 * @hide
7921 */
7922 @Override
7923 protected boolean hasProgress() {
7924 return false;
7925 }
Dan Sandler842dd772014-05-15 09:36:47 -04007926 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007927
Selim Cinek593610c2016-02-16 18:42:57 -08007928 /**
7929 * Notification style for custom views that are decorated by the system
7930 *
7931 * <p>Instead of providing a notification that is completely custom, a developer can set this
7932 * style and still obtain system decorations like the notification header with the expand
7933 * affordance and actions.
7934 *
7935 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7936 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7937 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7938 * corresponding custom views to display.
7939 *
7940 * To use this style with your Notification, feed it to
7941 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7942 * <pre class="prettyprint">
7943 * Notification noti = new Notification.Builder()
7944 * .setSmallIcon(R.drawable.ic_stat_player)
7945 * .setLargeIcon(albumArtBitmap))
7946 * .setCustomContentView(contentView);
7947 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7948 * .build();
7949 * </pre>
7950 */
7951 public static class DecoratedCustomViewStyle extends Style {
7952
7953 public DecoratedCustomViewStyle() {
7954 }
7955
Selim Cinek593610c2016-02-16 18:42:57 -08007956 /**
7957 * @hide
7958 */
7959 public boolean displayCustomViewInline() {
7960 return true;
7961 }
7962
7963 /**
7964 * @hide
7965 */
7966 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007967 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007968 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7969 }
7970
7971 /**
7972 * @hide
7973 */
7974 @Override
7975 public RemoteViews makeBigContentView() {
7976 return makeDecoratedBigContentView();
7977 }
7978
7979 /**
7980 * @hide
7981 */
7982 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007983 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007984 return makeDecoratedHeadsUpContentView();
7985 }
7986
Selim Cinek593610c2016-02-16 18:42:57 -08007987 private RemoteViews makeDecoratedHeadsUpContentView() {
7988 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7989 ? mBuilder.mN.contentView
7990 : mBuilder.mN.headsUpContentView;
7991 if (mBuilder.mActions.size() == 0) {
7992 return makeStandardTemplateWithCustomContent(headsUpContentView);
7993 }
Selim Cinek384804b2018-04-18 14:31:07 +08007994 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08007995 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08007996 mBuilder.getBigBaseLayoutResource(), result);
7997 buildIntoRemoteViewContent(remoteViews, headsUpContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08007998 return remoteViews;
7999 }
8000
Selim Cinek593610c2016-02-16 18:42:57 -08008001 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
Selim Cinek384804b2018-04-18 14:31:07 +08008002 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008003 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
Selim Cinek384804b2018-04-18 14:31:07 +08008004 mBuilder.getBaseLayoutResource(), result);
8005 buildIntoRemoteViewContent(remoteViews, customContent, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008006 return remoteViews;
8007 }
8008
Selim Cinek593610c2016-02-16 18:42:57 -08008009 private RemoteViews makeDecoratedBigContentView() {
8010 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
8011 ? mBuilder.mN.contentView
8012 : mBuilder.mN.bigContentView;
8013 if (mBuilder.mActions.size() == 0) {
8014 return makeStandardTemplateWithCustomContent(bigContentView);
8015 }
Selim Cinek384804b2018-04-18 14:31:07 +08008016 TemplateBindResult result = new TemplateBindResult();
Selim Cinek593610c2016-02-16 18:42:57 -08008017 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
Selim Cinek384804b2018-04-18 14:31:07 +08008018 mBuilder.getBigBaseLayoutResource(), result);
8019 buildIntoRemoteViewContent(remoteViews, bigContentView, result);
Selim Cinek593610c2016-02-16 18:42:57 -08008020 return remoteViews;
8021 }
Selim Cinek247fa012016-02-18 09:50:48 -08008022
8023 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
Selim Cinek384804b2018-04-18 14:31:07 +08008024 RemoteViews customContent, TemplateBindResult result) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008025 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008026 // Need to clone customContent before adding, because otherwise it can no longer be
8027 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008028 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07008029 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
8030 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008031 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008032 }
Selim Cinek247fa012016-02-18 09:50:48 -08008033 // also update the end margin if there is an image
Selim Cinek384804b2018-04-18 14:31:07 +08008034 Resources resources = mBuilder.mContext.getResources();
8035 int endMargin = resources.getDimensionPixelSize(
Selim Cinek1c72fa02018-04-23 18:00:54 +08008036 R.dimen.notification_content_margin_end) + result.getIconMarginEnd();
Selim Cinek384804b2018-04-18 14:31:07 +08008037 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08008038 }
Julia Reynolds7217dc92018-03-07 12:12:09 -05008039
8040 /**
8041 * @hide
8042 */
8043 @Override
8044 public boolean areNotificationsVisiblyDifferent(Style other) {
8045 if (other == null || getClass() != other.getClass()) {
8046 return true;
8047 }
8048 // Comparison done for all custom RemoteViews, independent of style
8049 return false;
8050 }
Selim Cinek593610c2016-02-16 18:42:57 -08008051 }
8052
Selim Cinek03eb3b72016-02-18 10:39:45 -08008053 /**
8054 * Notification style for media custom views that are decorated by the system
8055 *
8056 * <p>Instead of providing a media notification that is completely custom, a developer can set
8057 * this style and still obtain system decorations like the notification header with the expand
8058 * affordance and actions.
8059 *
8060 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
8061 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
8062 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
8063 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08008064 * <p>
8065 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
8066 * notification by using {@link Notification.Builder#setColorized(boolean)}.
8067 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08008068 * To use this style with your Notification, feed it to
8069 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
8070 * <pre class="prettyprint">
8071 * Notification noti = new Notification.Builder()
8072 * .setSmallIcon(R.drawable.ic_stat_player)
8073 * .setLargeIcon(albumArtBitmap))
8074 * .setCustomContentView(contentView);
8075 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
8076 * .setMediaSession(mySession))
8077 * .build();
8078 * </pre>
8079 *
8080 * @see android.app.Notification.DecoratedCustomViewStyle
8081 * @see android.app.Notification.MediaStyle
8082 */
8083 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
8084
8085 public DecoratedMediaCustomViewStyle() {
8086 }
8087
Selim Cinek03eb3b72016-02-18 10:39:45 -08008088 /**
8089 * @hide
8090 */
8091 public boolean displayCustomViewInline() {
8092 return true;
8093 }
8094
8095 /**
8096 * @hide
8097 */
8098 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08008099 public RemoteViews makeContentView(boolean increasedHeight) {
8100 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008101 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8102 mBuilder.mN.contentView);
8103 }
8104
8105 /**
8106 * @hide
8107 */
8108 @Override
8109 public RemoteViews makeBigContentView() {
8110 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
8111 ? mBuilder.mN.bigContentView
8112 : mBuilder.mN.contentView;
8113 return makeBigContentViewWithCustomContent(customRemoteView);
8114 }
8115
8116 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
8117 RemoteViews remoteViews = super.makeBigContentView();
8118 if (remoteViews != null) {
8119 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
8120 customRemoteView);
8121 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08008122 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08008123 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
8124 customRemoteView);
8125 } else {
8126 return null;
8127 }
8128 }
8129
8130 /**
8131 * @hide
8132 */
8133 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08008134 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08008135 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
8136 ? mBuilder.mN.headsUpContentView
8137 : mBuilder.mN.contentView;
8138 return makeBigContentViewWithCustomContent(customRemoteView);
8139 }
8140
Julia Reynolds7217dc92018-03-07 12:12:09 -05008141 /**
8142 * @hide
8143 */
8144 @Override
8145 public boolean areNotificationsVisiblyDifferent(Style other) {
8146 if (other == null || getClass() != other.getClass()) {
8147 return true;
8148 }
8149 // Comparison done for all custom RemoteViews, independent of style
8150 return false;
8151 }
8152
Selim Cinek03eb3b72016-02-18 10:39:45 -08008153 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
8154 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08008155 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07008156 // Need to clone customContent before adding, because otherwise it can no longer be
8157 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08008158 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07008159 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07008160 remoteViews.removeAllViews(id);
8161 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07008162 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08008163 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08008164 return remoteViews;
8165 }
8166 }
8167
Christoph Studer4600f9b2014-07-22 22:44:43 +02008168 // When adding a new Style subclass here, don't forget to update
8169 // Builder.getNotificationStyleClass.
8170
Griff Hazen61a9e862014-05-22 16:05:19 -07008171 /**
8172 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
8173 * metadata or change options on a notification builder.
8174 */
8175 public interface Extender {
8176 /**
8177 * Apply this extender to a notification builder.
8178 * @param builder the builder to be modified.
8179 * @return the build object for chaining.
8180 */
8181 public Builder extend(Builder builder);
8182 }
8183
8184 /**
8185 * Helper class to add wearable extensions to notifications.
8186 * <p class="note"> See
8187 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
8188 * for Android Wear</a> for more information on how to use this class.
8189 * <p>
8190 * To create a notification with wearable extensions:
8191 * <ol>
8192 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
8193 * properties.
8194 * <li>Create a {@link android.app.Notification.WearableExtender}.
8195 * <li>Set wearable-specific properties using the
8196 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
8197 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
8198 * notification.
8199 * <li>Post the notification to the notification system with the
8200 * {@code NotificationManager.notify(...)} methods.
8201 * </ol>
8202 *
8203 * <pre class="prettyprint">
8204 * Notification notif = new Notification.Builder(mContext)
8205 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
8206 * .setContentText(subject)
8207 * .setSmallIcon(R.drawable.new_mail)
8208 * .extend(new Notification.WearableExtender()
8209 * .setContentIcon(R.drawable.new_mail))
8210 * .build();
8211 * NotificationManager notificationManger =
8212 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
8213 * notificationManger.notify(0, notif);</pre>
8214 *
8215 * <p>Wearable extensions can be accessed on an existing notification by using the
8216 * {@code WearableExtender(Notification)} constructor,
8217 * and then using the {@code get} methods to access values.
8218 *
8219 * <pre class="prettyprint">
8220 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
8221 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07008222 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008223 */
8224 public static final class WearableExtender implements Extender {
8225 /**
8226 * Sentinel value for an action index that is unset.
8227 */
8228 public static final int UNSET_ACTION_INDEX = -1;
8229
8230 /**
8231 * Size value for use with {@link #setCustomSizePreset} to show this notification with
8232 * default sizing.
8233 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07008234 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07008235 * on their content.
8236 */
8237 public static final int SIZE_DEFAULT = 0;
8238
8239 /**
8240 * Size value for use with {@link #setCustomSizePreset} to show this notification
8241 * with an extra small size.
8242 * <p>This value is only applicable for custom display notifications created using
8243 * {@link #setDisplayIntent}.
8244 */
8245 public static final int SIZE_XSMALL = 1;
8246
8247 /**
8248 * Size value for use with {@link #setCustomSizePreset} to show this notification
8249 * with a small size.
8250 * <p>This value is only applicable for custom display notifications created using
8251 * {@link #setDisplayIntent}.
8252 */
8253 public static final int SIZE_SMALL = 2;
8254
8255 /**
8256 * Size value for use with {@link #setCustomSizePreset} to show this notification
8257 * with a medium size.
8258 * <p>This value is only applicable for custom display notifications created using
8259 * {@link #setDisplayIntent}.
8260 */
8261 public static final int SIZE_MEDIUM = 3;
8262
8263 /**
8264 * Size value for use with {@link #setCustomSizePreset} to show this notification
8265 * with a large size.
8266 * <p>This value is only applicable for custom display notifications created using
8267 * {@link #setDisplayIntent}.
8268 */
8269 public static final int SIZE_LARGE = 4;
8270
Griff Hazend5f11f92014-05-27 15:40:09 -07008271 /**
8272 * Size value for use with {@link #setCustomSizePreset} to show this notification
8273 * full screen.
8274 * <p>This value is only applicable for custom display notifications created using
8275 * {@link #setDisplayIntent}.
8276 */
8277 public static final int SIZE_FULL_SCREEN = 5;
8278
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008279 /**
8280 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
8281 * short amount of time when this notification is displayed on the screen. This
8282 * is the default value.
8283 */
8284 public static final int SCREEN_TIMEOUT_SHORT = 0;
8285
8286 /**
8287 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
8288 * for a longer amount of time when this notification is displayed on the screen.
8289 */
8290 public static final int SCREEN_TIMEOUT_LONG = -1;
8291
Griff Hazen61a9e862014-05-22 16:05:19 -07008292 /** Notification extra which contains wearable extensions */
8293 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
8294
Pete Gastaf6781d2014-10-07 15:17:05 -04008295 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07008296 private static final String KEY_ACTIONS = "actions";
8297 private static final String KEY_FLAGS = "flags";
8298 private static final String KEY_DISPLAY_INTENT = "displayIntent";
8299 private static final String KEY_PAGES = "pages";
8300 private static final String KEY_BACKGROUND = "background";
8301 private static final String KEY_CONTENT_ICON = "contentIcon";
8302 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
8303 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
8304 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
8305 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
8306 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008307 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04008308 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008309 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07008310
8311 // Flags bitwise-ored to mFlags
8312 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
8313 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
8314 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
8315 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008316 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04008317 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04008318 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07008319
8320 // Default value for flags integer
8321 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
8322
8323 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
8324 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
8325
8326 private ArrayList<Action> mActions = new ArrayList<Action>();
8327 private int mFlags = DEFAULT_FLAGS;
8328 private PendingIntent mDisplayIntent;
8329 private ArrayList<Notification> mPages = new ArrayList<Notification>();
8330 private Bitmap mBackground;
8331 private int mContentIcon;
8332 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
8333 private int mContentActionIndex = UNSET_ACTION_INDEX;
8334 private int mCustomSizePreset = SIZE_DEFAULT;
8335 private int mCustomContentHeight;
8336 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008337 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008338 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008339 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008340
8341 /**
8342 * Create a {@link android.app.Notification.WearableExtender} with default
8343 * options.
8344 */
8345 public WearableExtender() {
8346 }
8347
8348 public WearableExtender(Notification notif) {
8349 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
8350 if (wearableBundle != null) {
8351 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
8352 if (actions != null) {
8353 mActions.addAll(actions);
8354 }
8355
8356 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
8357 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
8358
8359 Notification[] pages = getNotificationArrayFromBundle(
8360 wearableBundle, KEY_PAGES);
8361 if (pages != null) {
8362 Collections.addAll(mPages, pages);
8363 }
8364
8365 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
8366 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
8367 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
8368 DEFAULT_CONTENT_ICON_GRAVITY);
8369 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
8370 UNSET_ACTION_INDEX);
8371 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
8372 SIZE_DEFAULT);
8373 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
8374 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008375 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04008376 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008377 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07008378 }
8379 }
8380
8381 /**
8382 * Apply wearable extensions to a notification that is being built. This is typically
8383 * called by the {@link android.app.Notification.Builder#extend} method of
8384 * {@link android.app.Notification.Builder}.
8385 */
8386 @Override
8387 public Notification.Builder extend(Notification.Builder builder) {
8388 Bundle wearableBundle = new Bundle();
8389
8390 if (!mActions.isEmpty()) {
8391 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
8392 }
8393 if (mFlags != DEFAULT_FLAGS) {
8394 wearableBundle.putInt(KEY_FLAGS, mFlags);
8395 }
8396 if (mDisplayIntent != null) {
8397 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
8398 }
8399 if (!mPages.isEmpty()) {
8400 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
8401 new Notification[mPages.size()]));
8402 }
8403 if (mBackground != null) {
8404 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
8405 }
8406 if (mContentIcon != 0) {
8407 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
8408 }
8409 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
8410 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
8411 }
8412 if (mContentActionIndex != UNSET_ACTION_INDEX) {
8413 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
8414 mContentActionIndex);
8415 }
8416 if (mCustomSizePreset != SIZE_DEFAULT) {
8417 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
8418 }
8419 if (mCustomContentHeight != 0) {
8420 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
8421 }
8422 if (mGravity != DEFAULT_GRAVITY) {
8423 wearableBundle.putInt(KEY_GRAVITY, mGravity);
8424 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008425 if (mHintScreenTimeout != 0) {
8426 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
8427 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04008428 if (mDismissalId != null) {
8429 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
8430 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008431 if (mBridgeTag != null) {
8432 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
8433 }
Griff Hazen61a9e862014-05-22 16:05:19 -07008434
8435 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
8436 return builder;
8437 }
8438
8439 @Override
8440 public WearableExtender clone() {
8441 WearableExtender that = new WearableExtender();
8442 that.mActions = new ArrayList<Action>(this.mActions);
8443 that.mFlags = this.mFlags;
8444 that.mDisplayIntent = this.mDisplayIntent;
8445 that.mPages = new ArrayList<Notification>(this.mPages);
8446 that.mBackground = this.mBackground;
8447 that.mContentIcon = this.mContentIcon;
8448 that.mContentIconGravity = this.mContentIconGravity;
8449 that.mContentActionIndex = this.mContentActionIndex;
8450 that.mCustomSizePreset = this.mCustomSizePreset;
8451 that.mCustomContentHeight = this.mCustomContentHeight;
8452 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008453 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04008454 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008455 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07008456 return that;
8457 }
8458
8459 /**
8460 * Add a wearable action to this notification.
8461 *
8462 * <p>When wearable actions are added using this method, the set of actions that
8463 * show on a wearable device splits from devices that only show actions added
8464 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8465 * of which actions display on different devices.
8466 *
8467 * @param action the action to add to this notification
8468 * @return this object for method chaining
8469 * @see android.app.Notification.Action
8470 */
8471 public WearableExtender addAction(Action action) {
8472 mActions.add(action);
8473 return this;
8474 }
8475
8476 /**
8477 * Adds wearable actions to this notification.
8478 *
8479 * <p>When wearable actions are added using this method, the set of actions that
8480 * show on a wearable device splits from devices that only show actions added
8481 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
8482 * of which actions display on different devices.
8483 *
8484 * @param actions the actions to add to this notification
8485 * @return this object for method chaining
8486 * @see android.app.Notification.Action
8487 */
8488 public WearableExtender addActions(List<Action> actions) {
8489 mActions.addAll(actions);
8490 return this;
8491 }
8492
8493 /**
8494 * Clear all wearable actions present on this builder.
8495 * @return this object for method chaining.
8496 * @see #addAction
8497 */
8498 public WearableExtender clearActions() {
8499 mActions.clear();
8500 return this;
8501 }
8502
8503 /**
8504 * Get the wearable actions present on this notification.
8505 */
8506 public List<Action> getActions() {
8507 return mActions;
8508 }
8509
8510 /**
8511 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07008512 * this notification. The {@link PendingIntent} provided should be for an activity.
8513 *
8514 * <pre class="prettyprint">
8515 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
8516 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
8517 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
8518 * Notification notif = new Notification.Builder(context)
8519 * .extend(new Notification.WearableExtender()
8520 * .setDisplayIntent(displayPendingIntent)
8521 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
8522 * .build();</pre>
8523 *
8524 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07008525 * should have an empty task affinity. It is also recommended to use the device
8526 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07008527 *
8528 * <p>Example AndroidManifest.xml entry:
8529 * <pre class="prettyprint">
8530 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
8531 * android:exported=&quot;true&quot;
8532 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07008533 * android:taskAffinity=&quot;&quot;
8534 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07008535 *
8536 * @param intent the {@link PendingIntent} for an activity
8537 * @return this object for method chaining
8538 * @see android.app.Notification.WearableExtender#getDisplayIntent
8539 */
8540 public WearableExtender setDisplayIntent(PendingIntent intent) {
8541 mDisplayIntent = intent;
8542 return this;
8543 }
8544
8545 /**
8546 * Get the intent to launch inside of an activity view when displaying this
8547 * notification. This {@code PendingIntent} should be for an activity.
8548 */
8549 public PendingIntent getDisplayIntent() {
8550 return mDisplayIntent;
8551 }
8552
8553 /**
8554 * Add an additional page of content to display with this notification. The current
8555 * notification forms the first page, and pages added using this function form
8556 * subsequent pages. This field can be used to separate a notification into multiple
8557 * sections.
8558 *
8559 * @param page the notification to add as another page
8560 * @return this object for method chaining
8561 * @see android.app.Notification.WearableExtender#getPages
8562 */
8563 public WearableExtender addPage(Notification page) {
8564 mPages.add(page);
8565 return this;
8566 }
8567
8568 /**
8569 * Add additional pages of content to display with this notification. The current
8570 * notification forms the first page, and pages added using this function form
8571 * subsequent pages. This field can be used to separate a notification into multiple
8572 * sections.
8573 *
8574 * @param pages a list of notifications
8575 * @return this object for method chaining
8576 * @see android.app.Notification.WearableExtender#getPages
8577 */
8578 public WearableExtender addPages(List<Notification> pages) {
8579 mPages.addAll(pages);
8580 return this;
8581 }
8582
8583 /**
8584 * Clear all additional pages present on this builder.
8585 * @return this object for method chaining.
8586 * @see #addPage
8587 */
8588 public WearableExtender clearPages() {
8589 mPages.clear();
8590 return this;
8591 }
8592
8593 /**
8594 * Get the array of additional pages of content for displaying this notification. The
8595 * current notification forms the first page, and elements within this array form
8596 * subsequent pages. This field can be used to separate a notification into multiple
8597 * sections.
8598 * @return the pages for this notification
8599 */
8600 public List<Notification> getPages() {
8601 return mPages;
8602 }
8603
8604 /**
8605 * Set a background image to be displayed behind the notification content.
8606 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8607 * will work with any notification style.
8608 *
8609 * @param background the background bitmap
8610 * @return this object for method chaining
8611 * @see android.app.Notification.WearableExtender#getBackground
8612 */
8613 public WearableExtender setBackground(Bitmap background) {
8614 mBackground = background;
8615 return this;
8616 }
8617
8618 /**
8619 * Get a background image to be displayed behind the notification content.
8620 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8621 * will work with any notification style.
8622 *
8623 * @return the background image
8624 * @see android.app.Notification.WearableExtender#setBackground
8625 */
8626 public Bitmap getBackground() {
8627 return mBackground;
8628 }
8629
8630 /**
8631 * Set an icon that goes with the content of this notification.
8632 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008633 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008634 public WearableExtender setContentIcon(int icon) {
8635 mContentIcon = icon;
8636 return this;
8637 }
8638
8639 /**
8640 * Get an icon that goes with the content of this notification.
8641 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008642 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008643 public int getContentIcon() {
8644 return mContentIcon;
8645 }
8646
8647 /**
8648 * Set the gravity that the content icon should have within the notification display.
8649 * Supported values include {@link android.view.Gravity#START} and
8650 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8651 * @see #setContentIcon
8652 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008653 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008654 public WearableExtender setContentIconGravity(int contentIconGravity) {
8655 mContentIconGravity = contentIconGravity;
8656 return this;
8657 }
8658
8659 /**
8660 * Get the gravity that the content icon should have within the notification display.
8661 * Supported values include {@link android.view.Gravity#START} and
8662 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8663 * @see #getContentIcon
8664 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008665 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008666 public int getContentIconGravity() {
8667 return mContentIconGravity;
8668 }
8669
8670 /**
8671 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008672 * this notification. This action will no longer display separately from the
8673 * notification's content.
8674 *
Griff Hazenca48d352014-05-28 22:37:13 -07008675 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008676 * set, although the list of available actions comes from the main notification and not
8677 * from the child page's notification.
8678 *
8679 * @param actionIndex The index of the action to hoist onto the current notification page.
8680 * If wearable actions were added to the main notification, this index
8681 * will apply to that list, otherwise it will apply to the regular
8682 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008683 */
8684 public WearableExtender setContentAction(int actionIndex) {
8685 mContentActionIndex = actionIndex;
8686 return this;
8687 }
8688
8689 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008690 * Get the index of the notification action, if any, that was specified as being clickable
8691 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008692 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008693 *
Griff Hazenca48d352014-05-28 22:37:13 -07008694 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008695 * set, although the list of available actions comes from the main notification and not
8696 * from the child page's notification.
8697 *
8698 * <p>If wearable specific actions were added to the main notification, this index will
8699 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008700 *
8701 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008702 */
8703 public int getContentAction() {
8704 return mContentActionIndex;
8705 }
8706
8707 /**
8708 * Set the gravity that this notification should have within the available viewport space.
8709 * Supported values include {@link android.view.Gravity#TOP},
8710 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8711 * The default value is {@link android.view.Gravity#BOTTOM}.
8712 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008713 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008714 public WearableExtender setGravity(int gravity) {
8715 mGravity = gravity;
8716 return this;
8717 }
8718
8719 /**
8720 * Get the gravity that this notification should have within the available viewport space.
8721 * Supported values include {@link android.view.Gravity#TOP},
8722 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8723 * The default value is {@link android.view.Gravity#BOTTOM}.
8724 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008725 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008726 public int getGravity() {
8727 return mGravity;
8728 }
8729
8730 /**
8731 * Set the custom size preset for the display of this notification out of the available
8732 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8733 * {@link #SIZE_LARGE}.
8734 * <p>Some custom size presets are only applicable for custom display notifications created
8735 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8736 * documentation for the preset in question. See also
8737 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8738 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008739 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008740 public WearableExtender setCustomSizePreset(int sizePreset) {
8741 mCustomSizePreset = sizePreset;
8742 return this;
8743 }
8744
8745 /**
8746 * Get the custom size preset for the display of this notification out of the available
8747 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8748 * {@link #SIZE_LARGE}.
8749 * <p>Some custom size presets are only applicable for custom display notifications created
8750 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8751 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8752 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008753 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008754 public int getCustomSizePreset() {
8755 return mCustomSizePreset;
8756 }
8757
8758 /**
8759 * Set the custom height in pixels for the display of this notification's content.
8760 * <p>This option is only available for custom display notifications created
8761 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8762 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8763 * {@link #getCustomContentHeight}.
8764 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008765 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008766 public WearableExtender setCustomContentHeight(int height) {
8767 mCustomContentHeight = height;
8768 return this;
8769 }
8770
8771 /**
8772 * Get the custom height in pixels for the display of this notification's content.
8773 * <p>This option is only available for custom display notifications created
8774 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8775 * {@link #setCustomContentHeight}.
8776 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008777 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008778 public int getCustomContentHeight() {
8779 return mCustomContentHeight;
8780 }
8781
8782 /**
8783 * Set whether the scrolling position for the contents of this notification should start
8784 * at the bottom of the contents instead of the top when the contents are too long to
8785 * display within the screen. Default is false (start scroll at the top).
8786 */
8787 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8788 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8789 return this;
8790 }
8791
8792 /**
8793 * Get whether the scrolling position for the contents of this notification should start
8794 * at the bottom of the contents instead of the top when the contents are too long to
8795 * display within the screen. Default is false (start scroll at the top).
8796 */
8797 public boolean getStartScrollBottom() {
8798 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8799 }
8800
8801 /**
8802 * Set whether the content intent is available when the wearable device is not connected
8803 * to a companion device. The user can still trigger this intent when the wearable device
8804 * is offline, but a visual hint will indicate that the content intent may not be available.
8805 * Defaults to true.
8806 */
8807 public WearableExtender setContentIntentAvailableOffline(
8808 boolean contentIntentAvailableOffline) {
8809 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8810 return this;
8811 }
8812
8813 /**
8814 * Get whether the content intent is available when the wearable device is not connected
8815 * to a companion device. The user can still trigger this intent when the wearable device
8816 * is offline, but a visual hint will indicate that the content intent may not be available.
8817 * Defaults to true.
8818 */
8819 public boolean getContentIntentAvailableOffline() {
8820 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8821 }
8822
8823 /**
8824 * Set a hint that this notification's icon should not be displayed.
8825 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8826 * @return this object for method chaining
8827 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008828 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008829 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8830 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8831 return this;
8832 }
8833
8834 /**
8835 * Get a hint that this notification's icon should not be displayed.
8836 * @return {@code true} if this icon should not be displayed, false otherwise.
8837 * The default value is {@code false} if this was never set.
8838 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008839 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008840 public boolean getHintHideIcon() {
8841 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8842 }
8843
8844 /**
8845 * Set a visual hint that only the background image of this notification should be
8846 * displayed, and other semantic content should be hidden. This hint is only applicable
8847 * to sub-pages added using {@link #addPage}.
8848 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008849 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008850 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8851 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8852 return this;
8853 }
8854
8855 /**
8856 * Get a visual hint that only the background image of this notification should be
8857 * displayed, and other semantic content should be hidden. This hint is only applicable
8858 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8859 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008860 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008861 public boolean getHintShowBackgroundOnly() {
8862 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8863 }
8864
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008865 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008866 * Set a hint that this notification's background should not be clipped if possible,
8867 * and should instead be resized to fully display on the screen, retaining the aspect
8868 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008869 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8870 * @return this object for method chaining
8871 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008872 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008873 public WearableExtender setHintAvoidBackgroundClipping(
8874 boolean hintAvoidBackgroundClipping) {
8875 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8876 return this;
8877 }
8878
8879 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008880 * Get a hint that this notification's background should not be clipped if possible,
8881 * and should instead be resized to fully display on the screen, retaining the aspect
8882 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008883 * @return {@code true} if it's ok if the background is clipped on the screen, false
8884 * otherwise. The default value is {@code false} if this was never set.
8885 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008886 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008887 public boolean getHintAvoidBackgroundClipping() {
8888 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8889 }
8890
8891 /**
8892 * Set a hint that the screen should remain on for at least this duration when
8893 * this notification is displayed on the screen.
8894 * @param timeout The requested screen timeout in milliseconds. Can also be either
8895 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8896 * @return this object for method chaining
8897 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008898 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008899 public WearableExtender setHintScreenTimeout(int timeout) {
8900 mHintScreenTimeout = timeout;
8901 return this;
8902 }
8903
8904 /**
8905 * Get the duration, in milliseconds, that the screen should remain on for
8906 * when this notification is displayed.
8907 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8908 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8909 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008910 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008911 public int getHintScreenTimeout() {
8912 return mHintScreenTimeout;
8913 }
8914
Alex Hills9ab3a232016-04-05 14:54:56 -04008915 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008916 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8917 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8918 * qr codes, as well as other simple black-and-white tickets.
8919 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8920 * @return this object for method chaining
8921 */
8922 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8923 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8924 return this;
8925 }
8926
8927 /**
8928 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8929 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8930 * qr codes, as well as other simple black-and-white tickets.
8931 * @return {@code true} if it should be displayed in ambient, false otherwise
8932 * otherwise. The default value is {@code false} if this was never set.
8933 */
8934 public boolean getHintAmbientBigPicture() {
8935 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8936 }
8937
8938 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008939 * Set a hint that this notification's content intent will launch an {@link Activity}
8940 * directly, telling the platform that it can generate the appropriate transitions.
8941 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8942 * an activity and transitions should be generated, false otherwise.
8943 * @return this object for method chaining
8944 */
8945 public WearableExtender setHintContentIntentLaunchesActivity(
8946 boolean hintContentIntentLaunchesActivity) {
8947 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8948 return this;
8949 }
8950
8951 /**
8952 * Get a hint that this notification's content intent will launch an {@link Activity}
8953 * directly, telling the platform that it can generate the appropriate transitions
8954 * @return {@code true} if the content intent will launch an activity and transitions should
8955 * be generated, false otherwise. The default value is {@code false} if this was never set.
8956 */
8957 public boolean getHintContentIntentLaunchesActivity() {
8958 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8959 }
8960
Nadia Benbernou948627e2016-04-14 14:41:08 -04008961 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008962 * Sets the dismissal id for this notification. If a notification is posted with a
8963 * dismissal id, then when that notification is canceled, notifications on other wearables
8964 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008965 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008966 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008967 * @param dismissalId the dismissal id of the notification.
8968 * @return this object for method chaining
8969 */
8970 public WearableExtender setDismissalId(String dismissalId) {
8971 mDismissalId = dismissalId;
8972 return this;
8973 }
8974
8975 /**
8976 * Returns the dismissal id of the notification.
8977 * @return the dismissal id of the notification or null if it has not been set.
8978 */
8979 public String getDismissalId() {
8980 return mDismissalId;
8981 }
8982
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008983 /**
8984 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8985 * posted from a phone to provide finer-grained control on what notifications are bridged
8986 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8987 * Features to Notifications</a> for more information.
8988 * @param bridgeTag the bridge tag of the notification.
8989 * @return this object for method chaining
8990 */
8991 public WearableExtender setBridgeTag(String bridgeTag) {
8992 mBridgeTag = bridgeTag;
8993 return this;
8994 }
8995
8996 /**
8997 * Returns the bridge tag of the notification.
8998 * @return the bridge tag or null if not present.
8999 */
9000 public String getBridgeTag() {
9001 return mBridgeTag;
9002 }
9003
Griff Hazen61a9e862014-05-22 16:05:19 -07009004 private void setFlag(int mask, boolean value) {
9005 if (value) {
9006 mFlags |= mask;
9007 } else {
9008 mFlags &= ~mask;
9009 }
9010 }
9011 }
9012
9013 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009014 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
9015 * with car extensions:
9016 *
9017 * <ol>
9018 * <li>Create an {@link Notification.Builder}, setting any desired
9019 * properties.
9020 * <li>Create a {@link CarExtender}.
9021 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
9022 * {@link CarExtender}.
9023 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9024 * to apply the extensions to a notification.
9025 * </ol>
9026 *
9027 * <pre class="prettyprint">
9028 * Notification notification = new Notification.Builder(context)
9029 * ...
9030 * .extend(new CarExtender()
9031 * .set*(...))
9032 * .build();
9033 * </pre>
9034 *
9035 * <p>Car extensions can be accessed on an existing notification by using the
9036 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
9037 * to access values.
9038 */
9039 public static final class CarExtender implements Extender {
9040 private static final String TAG = "CarExtender";
9041
9042 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
9043 private static final String EXTRA_LARGE_ICON = "large_icon";
9044 private static final String EXTRA_CONVERSATION = "car_conversation";
9045 private static final String EXTRA_COLOR = "app_color";
9046
9047 private Bitmap mLargeIcon;
9048 private UnreadConversation mUnreadConversation;
9049 private int mColor = Notification.COLOR_DEFAULT;
9050
9051 /**
9052 * Create a {@link CarExtender} with default options.
9053 */
9054 public CarExtender() {
9055 }
9056
9057 /**
9058 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
9059 *
9060 * @param notif The notification from which to copy options.
9061 */
9062 public CarExtender(Notification notif) {
9063 Bundle carBundle = notif.extras == null ?
9064 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
9065 if (carBundle != null) {
9066 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
9067 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
9068
9069 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
9070 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
9071 }
9072 }
9073
9074 /**
9075 * Apply car extensions to a notification that is being built. This is typically called by
9076 * the {@link Notification.Builder#extend(Notification.Extender)}
9077 * method of {@link Notification.Builder}.
9078 */
9079 @Override
9080 public Notification.Builder extend(Notification.Builder builder) {
9081 Bundle carExtensions = new Bundle();
9082
9083 if (mLargeIcon != null) {
9084 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
9085 }
9086 if (mColor != Notification.COLOR_DEFAULT) {
9087 carExtensions.putInt(EXTRA_COLOR, mColor);
9088 }
9089
9090 if (mUnreadConversation != null) {
9091 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
9092 carExtensions.putBundle(EXTRA_CONVERSATION, b);
9093 }
9094
9095 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
9096 return builder;
9097 }
9098
9099 /**
9100 * Sets the accent color to use when Android Auto presents the notification.
9101 *
9102 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
9103 * to accent the displayed notification. However, not all colors are acceptable in an
9104 * automotive setting. This method can be used to override the color provided in the
9105 * notification in such a situation.
9106 */
Tor Norbye80756e32015-03-02 09:39:27 -08009107 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009108 mColor = color;
9109 return this;
9110 }
9111
9112 /**
9113 * Gets the accent color.
9114 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04009115 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009116 */
Tor Norbye80756e32015-03-02 09:39:27 -08009117 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08009118 public int getColor() {
9119 return mColor;
9120 }
9121
9122 /**
9123 * Sets the large icon of the car notification.
9124 *
9125 * If no large icon is set in the extender, Android Auto will display the icon
9126 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
9127 *
9128 * @param largeIcon The large icon to use in the car notification.
9129 * @return This object for method chaining.
9130 */
9131 public CarExtender setLargeIcon(Bitmap largeIcon) {
9132 mLargeIcon = largeIcon;
9133 return this;
9134 }
9135
9136 /**
9137 * Gets the large icon used in this car notification, or null if no icon has been set.
9138 *
9139 * @return The large icon for the car notification.
9140 * @see CarExtender#setLargeIcon
9141 */
9142 public Bitmap getLargeIcon() {
9143 return mLargeIcon;
9144 }
9145
9146 /**
9147 * Sets the unread conversation in a message notification.
9148 *
9149 * @param unreadConversation The unread part of the conversation this notification conveys.
9150 * @return This object for method chaining.
9151 */
9152 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
9153 mUnreadConversation = unreadConversation;
9154 return this;
9155 }
9156
9157 /**
9158 * Returns the unread conversation conveyed by this notification.
9159 * @see #setUnreadConversation(UnreadConversation)
9160 */
9161 public UnreadConversation getUnreadConversation() {
9162 return mUnreadConversation;
9163 }
9164
9165 /**
9166 * A class which holds the unread messages from a conversation.
9167 */
9168 public static class UnreadConversation {
9169 private static final String KEY_AUTHOR = "author";
9170 private static final String KEY_TEXT = "text";
9171 private static final String KEY_MESSAGES = "messages";
9172 private static final String KEY_REMOTE_INPUT = "remote_input";
9173 private static final String KEY_ON_REPLY = "on_reply";
9174 private static final String KEY_ON_READ = "on_read";
9175 private static final String KEY_PARTICIPANTS = "participants";
9176 private static final String KEY_TIMESTAMP = "timestamp";
9177
9178 private final String[] mMessages;
9179 private final RemoteInput mRemoteInput;
9180 private final PendingIntent mReplyPendingIntent;
9181 private final PendingIntent mReadPendingIntent;
9182 private final String[] mParticipants;
9183 private final long mLatestTimestamp;
9184
9185 UnreadConversation(String[] messages, RemoteInput remoteInput,
9186 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
9187 String[] participants, long latestTimestamp) {
9188 mMessages = messages;
9189 mRemoteInput = remoteInput;
9190 mReadPendingIntent = readPendingIntent;
9191 mReplyPendingIntent = replyPendingIntent;
9192 mParticipants = participants;
9193 mLatestTimestamp = latestTimestamp;
9194 }
9195
9196 /**
9197 * Gets the list of messages conveyed by this notification.
9198 */
9199 public String[] getMessages() {
9200 return mMessages;
9201 }
9202
9203 /**
9204 * Gets the remote input that will be used to convey the response to a message list, or
9205 * null if no such remote input exists.
9206 */
9207 public RemoteInput getRemoteInput() {
9208 return mRemoteInput;
9209 }
9210
9211 /**
9212 * Gets the pending intent that will be triggered when the user replies to this
9213 * notification.
9214 */
9215 public PendingIntent getReplyPendingIntent() {
9216 return mReplyPendingIntent;
9217 }
9218
9219 /**
9220 * Gets the pending intent that Android Auto will send after it reads aloud all messages
9221 * in this object's message list.
9222 */
9223 public PendingIntent getReadPendingIntent() {
9224 return mReadPendingIntent;
9225 }
9226
9227 /**
9228 * Gets the participants in the conversation.
9229 */
9230 public String[] getParticipants() {
9231 return mParticipants;
9232 }
9233
9234 /**
9235 * Gets the firs participant in the conversation.
9236 */
9237 public String getParticipant() {
9238 return mParticipants.length > 0 ? mParticipants[0] : null;
9239 }
9240
9241 /**
9242 * Gets the timestamp of the conversation.
9243 */
9244 public long getLatestTimestamp() {
9245 return mLatestTimestamp;
9246 }
9247
9248 Bundle getBundleForUnreadConversation() {
9249 Bundle b = new Bundle();
9250 String author = null;
9251 if (mParticipants != null && mParticipants.length > 1) {
9252 author = mParticipants[0];
9253 }
9254 Parcelable[] messages = new Parcelable[mMessages.length];
9255 for (int i = 0; i < messages.length; i++) {
9256 Bundle m = new Bundle();
9257 m.putString(KEY_TEXT, mMessages[i]);
9258 m.putString(KEY_AUTHOR, author);
9259 messages[i] = m;
9260 }
9261 b.putParcelableArray(KEY_MESSAGES, messages);
9262 if (mRemoteInput != null) {
9263 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
9264 }
9265 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
9266 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
9267 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
9268 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
9269 return b;
9270 }
9271
9272 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
9273 if (b == null) {
9274 return null;
9275 }
9276 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
9277 String[] messages = null;
9278 if (parcelableMessages != null) {
9279 String[] tmp = new String[parcelableMessages.length];
9280 boolean success = true;
9281 for (int i = 0; i < tmp.length; i++) {
9282 if (!(parcelableMessages[i] instanceof Bundle)) {
9283 success = false;
9284 break;
9285 }
9286 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
9287 if (tmp[i] == null) {
9288 success = false;
9289 break;
9290 }
9291 }
9292 if (success) {
9293 messages = tmp;
9294 } else {
9295 return null;
9296 }
9297 }
9298
9299 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
9300 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
9301
9302 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
9303
9304 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
9305 if (participants == null || participants.length != 1) {
9306 return null;
9307 }
9308
9309 return new UnreadConversation(messages,
9310 remoteInput,
9311 onReply,
9312 onRead,
9313 participants, b.getLong(KEY_TIMESTAMP));
9314 }
9315 };
9316
9317 /**
9318 * Builder class for {@link CarExtender.UnreadConversation} objects.
9319 */
9320 public static class Builder {
9321 private final List<String> mMessages = new ArrayList<String>();
9322 private final String mParticipant;
9323 private RemoteInput mRemoteInput;
9324 private PendingIntent mReadPendingIntent;
9325 private PendingIntent mReplyPendingIntent;
9326 private long mLatestTimestamp;
9327
9328 /**
9329 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
9330 *
9331 * @param name The name of the other participant in the conversation.
9332 */
9333 public Builder(String name) {
9334 mParticipant = name;
9335 }
9336
9337 /**
9338 * Appends a new unread message to the list of messages for this conversation.
9339 *
9340 * The messages should be added from oldest to newest.
9341 *
9342 * @param message The text of the new unread message.
9343 * @return This object for method chaining.
9344 */
9345 public Builder addMessage(String message) {
9346 mMessages.add(message);
9347 return this;
9348 }
9349
9350 /**
9351 * Sets the pending intent and remote input which will convey the reply to this
9352 * notification.
9353 *
9354 * @param pendingIntent The pending intent which will be triggered on a reply.
9355 * @param remoteInput The remote input parcelable which will carry the reply.
9356 * @return This object for method chaining.
9357 *
9358 * @see CarExtender.UnreadConversation#getRemoteInput
9359 * @see CarExtender.UnreadConversation#getReplyPendingIntent
9360 */
9361 public Builder setReplyAction(
9362 PendingIntent pendingIntent, RemoteInput remoteInput) {
9363 mRemoteInput = remoteInput;
9364 mReplyPendingIntent = pendingIntent;
9365
9366 return this;
9367 }
9368
9369 /**
9370 * Sets the pending intent that will be sent once the messages in this notification
9371 * are read.
9372 *
9373 * @param pendingIntent The pending intent to use.
9374 * @return This object for method chaining.
9375 */
9376 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
9377 mReadPendingIntent = pendingIntent;
9378 return this;
9379 }
9380
9381 /**
9382 * Sets the timestamp of the most recent message in an unread conversation.
9383 *
9384 * If a messaging notification has been posted by your application and has not
9385 * yet been cancelled, posting a later notification with the same id and tag
9386 * but without a newer timestamp may result in Android Auto not displaying a
9387 * heads up notification for the later notification.
9388 *
9389 * @param timestamp The timestamp of the most recent message in the conversation.
9390 * @return This object for method chaining.
9391 */
9392 public Builder setLatestTimestamp(long timestamp) {
9393 mLatestTimestamp = timestamp;
9394 return this;
9395 }
9396
9397 /**
9398 * Builds a new unread conversation object.
9399 *
9400 * @return The new unread conversation object.
9401 */
9402 public UnreadConversation build() {
9403 String[] messages = mMessages.toArray(new String[mMessages.size()]);
9404 String[] participants = { mParticipant };
9405 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
9406 mReadPendingIntent, participants, mLatestTimestamp);
9407 }
9408 }
9409 }
9410
9411 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009412 * <p>Helper class to add Android TV extensions to notifications. To create a notification
9413 * with a TV extension:
9414 *
9415 * <ol>
9416 * <li>Create an {@link Notification.Builder}, setting any desired properties.
9417 * <li>Create a {@link TvExtender}.
9418 * <li>Set TV-specific properties using the {@code set} methods of
9419 * {@link TvExtender}.
9420 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
9421 * to apply the extension to a notification.
9422 * </ol>
9423 *
9424 * <pre class="prettyprint">
9425 * Notification notification = new Notification.Builder(context)
9426 * ...
9427 * .extend(new TvExtender()
9428 * .set*(...))
9429 * .build();
9430 * </pre>
9431 *
9432 * <p>TV extensions can be accessed on an existing notification by using the
9433 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
9434 * to access values.
9435 *
9436 * @hide
9437 */
9438 @SystemApi
9439 public static final class TvExtender implements Extender {
9440 private static final String TAG = "TvExtender";
9441
9442 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
9443 private static final String EXTRA_FLAGS = "flags";
9444 private static final String EXTRA_CONTENT_INTENT = "content_intent";
9445 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009446 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08009447 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009448
9449 // Flags bitwise-ored to mFlags
9450 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
9451
9452 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009453 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009454 private PendingIntent mContentIntent;
9455 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08009456 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009457
9458 /**
9459 * Create a {@link TvExtender} with default options.
9460 */
9461 public TvExtender() {
9462 mFlags = FLAG_AVAILABLE_ON_TV;
9463 }
9464
9465 /**
9466 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
9467 *
9468 * @param notif The notification from which to copy options.
9469 */
9470 public TvExtender(Notification notif) {
9471 Bundle bundle = notif.extras == null ?
9472 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
9473 if (bundle != null) {
9474 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009475 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009476 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009477 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
9478 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
9479 }
9480 }
9481
9482 /**
9483 * Apply a TV extension to a notification that is being built. This is typically called by
9484 * the {@link Notification.Builder#extend(Notification.Extender)}
9485 * method of {@link Notification.Builder}.
9486 */
9487 @Override
9488 public Notification.Builder extend(Notification.Builder builder) {
9489 Bundle bundle = new Bundle();
9490
9491 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009492 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08009493 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009494 if (mContentIntent != null) {
9495 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
9496 }
9497
9498 if (mDeleteIntent != null) {
9499 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
9500 }
9501
9502 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
9503 return builder;
9504 }
9505
9506 /**
9507 * Returns true if this notification should be shown on TV. This method return true
9508 * if the notification was extended with a TvExtender.
9509 */
9510 public boolean isAvailableOnTv() {
9511 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
9512 }
9513
9514 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009515 * Specifies the channel the notification should be delivered on when shown on TV.
9516 * It can be different from the channel that the notification is delivered to when
9517 * posting on a non-TV device.
9518 */
9519 public TvExtender setChannel(String channelId) {
9520 mChannelId = channelId;
9521 return this;
9522 }
9523
9524 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009525 * Specifies the channel the notification should be delivered on when shown on TV.
9526 * It can be different from the channel that the notification is delivered to when
9527 * posting on a non-TV device.
9528 */
9529 public TvExtender setChannelId(String channelId) {
9530 mChannelId = channelId;
9531 return this;
9532 }
9533
Jeff Sharkey000ce802017-04-29 13:13:27 -06009534 /** @removed */
9535 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08009536 public String getChannel() {
9537 return mChannelId;
9538 }
9539
9540 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04009541 * Returns the id of the channel this notification posts to on TV.
9542 */
9543 public String getChannelId() {
9544 return mChannelId;
9545 }
9546
9547 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009548 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9549 * If provided, it is used instead of the content intent specified
9550 * at the level of Notification.
9551 */
9552 public TvExtender setContentIntent(PendingIntent intent) {
9553 mContentIntent = intent;
9554 return this;
9555 }
9556
9557 /**
9558 * Returns the TV-specific content intent. If this method returns null, the
9559 * main content intent on the notification should be used.
9560 *
9561 * @see {@link Notification#contentIntent}
9562 */
9563 public PendingIntent getContentIntent() {
9564 return mContentIntent;
9565 }
9566
9567 /**
9568 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9569 * by the user on TV. If provided, it is used instead of the delete intent specified
9570 * at the level of Notification.
9571 */
9572 public TvExtender setDeleteIntent(PendingIntent intent) {
9573 mDeleteIntent = intent;
9574 return this;
9575 }
9576
9577 /**
9578 * Returns the TV-specific delete intent. If this method returns null, the
9579 * main delete intent on the notification should be used.
9580 *
9581 * @see {@link Notification#deleteIntent}
9582 */
9583 public PendingIntent getDeleteIntent() {
9584 return mDeleteIntent;
9585 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009586
9587 /**
9588 * Specifies whether this notification should suppress showing a message over top of apps
9589 * outside of the launcher.
9590 */
9591 public TvExtender setSuppressShowOverApps(boolean suppress) {
9592 mSuppressShowOverApps = suppress;
9593 return this;
9594 }
9595
9596 /**
9597 * Returns true if this notification should not show messages over top of apps
9598 * outside of the launcher.
9599 */
9600 public boolean getSuppressShowOverApps() {
9601 return mSuppressShowOverApps;
9602 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009603 }
9604
9605 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009606 * Get an array of Notification objects from a parcelable array bundle field.
9607 * Update the bundle to have a typed array so fetches in the future don't need
9608 * to do an array copy.
9609 */
9610 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9611 Parcelable[] array = bundle.getParcelableArray(key);
9612 if (array instanceof Notification[] || array == null) {
9613 return (Notification[]) array;
9614 }
9615 Notification[] typedArray = Arrays.copyOf(array, array.length,
9616 Notification[].class);
9617 bundle.putParcelableArray(key, typedArray);
9618 return typedArray;
9619 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009620
9621 private static class BuilderRemoteViews extends RemoteViews {
9622 public BuilderRemoteViews(Parcel parcel) {
9623 super(parcel);
9624 }
9625
Kenny Guy77320062014-08-27 21:37:15 +01009626 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9627 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009628 }
9629
9630 @Override
9631 public BuilderRemoteViews clone() {
9632 Parcel p = Parcel.obtain();
9633 writeToParcel(p, 0);
9634 p.setDataPosition(0);
9635 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9636 p.recycle();
9637 return brv;
9638 }
9639 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009640
Selim Cinek384804b2018-04-18 14:31:07 +08009641 /**
9642 * A result object where information about the template that was created is saved.
9643 */
9644 private static class TemplateBindResult {
Selim Cinek1c72fa02018-04-23 18:00:54 +08009645 int mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009646
9647 /**
Selim Cinek1c72fa02018-04-23 18:00:54 +08009648 * Get the margin end that needs to be added to any fields that may overlap
Selim Cinek384804b2018-04-18 14:31:07 +08009649 * with the right actions.
9650 */
Selim Cinek1c72fa02018-04-23 18:00:54 +08009651 public int getIconMarginEnd() {
9652 return mIconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009653 }
9654
Selim Cinek1c72fa02018-04-23 18:00:54 +08009655 public void setIconMarginEnd(int iconMarginEnd) {
9656 this.mIconMarginEnd = iconMarginEnd;
Selim Cinek384804b2018-04-18 14:31:07 +08009657 }
9658 }
9659
Adrian Roos70d7aa32017-01-11 15:39:06 -08009660 private static class StandardTemplateParams {
9661 boolean hasProgress = true;
9662 boolean ambient = false;
9663 CharSequence title;
9664 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009665 CharSequence headerTextSecondary;
Selim Cinekbee4e072018-05-21 22:06:43 -07009666 int maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Selim Cinek88188f22017-09-19 16:46:56 -07009667 boolean hideLargeIcon;
Selim Cinek384804b2018-04-18 14:31:07 +08009668 boolean hideReplyIcon;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009669
9670 final StandardTemplateParams reset() {
9671 hasProgress = true;
9672 ambient = false;
9673 title = null;
9674 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009675 headerTextSecondary = null;
Selim Cinekbee4e072018-05-21 22:06:43 -07009676 maxRemoteInputHistory = Style.MAX_REMOTE_INPUT_HISTORY_LINES;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009677 return this;
9678 }
9679
9680 final StandardTemplateParams hasProgress(boolean hasProgress) {
9681 this.hasProgress = hasProgress;
9682 return this;
9683 }
9684
9685 final StandardTemplateParams title(CharSequence title) {
9686 this.title = title;
9687 return this;
9688 }
9689
9690 final StandardTemplateParams text(CharSequence text) {
9691 this.text = text;
9692 return this;
9693 }
9694
Selim Cinekafeed292017-12-12 17:32:44 -08009695 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9696 this.headerTextSecondary = text;
9697 return this;
9698 }
9699
Selim Cinek384804b2018-04-18 14:31:07 +08009700 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9701 this.hideLargeIcon = hideLargeIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009702 return this;
9703 }
9704
Selim Cinek384804b2018-04-18 14:31:07 +08009705 final StandardTemplateParams hideReplyIcon(boolean hideReplyIcon) {
9706 this.hideReplyIcon = hideReplyIcon;
Selim Cinek88188f22017-09-19 16:46:56 -07009707 return this;
9708 }
9709
Adrian Roos70d7aa32017-01-11 15:39:06 -08009710 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009711 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009712 this.ambient = ambient;
9713 return this;
9714 }
9715
9716 final StandardTemplateParams fillTextsFrom(Builder b) {
9717 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009718 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9719
9720 // Big text notifications should contain their content when viewed in ambient mode.
9721 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9722 if (!ambient || TextUtils.isEmpty(text)) {
9723 text = extras.getCharSequence(EXTRA_TEXT);
9724 }
9725 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009726 return this;
9727 }
Selim Cinekbee4e072018-05-21 22:06:43 -07009728
9729 /**
9730 * Set the maximum lines of remote input history lines allowed.
9731 * @param maxRemoteInputHistory The number of lines.
9732 * @return The builder for method chaining.
9733 */
9734 public StandardTemplateParams setMaxRemoteInputHistory(int maxRemoteInputHistory) {
9735 this.maxRemoteInputHistory = maxRemoteInputHistory;
9736 return this;
9737 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739}