blob: 2a28fad4ac27f4922406c63e47fce34eca5a0913 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Selim Cinek389edcd2017-05-11 19:16:44 -070019import static com.android.internal.util.NotificationColorUtil.satisfiesTextContrast;
20
Tor Norbye80756e32015-03-02 09:39:27 -080021import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070023import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040024import android.annotation.NonNull;
Selim Cinek88188f22017-09-19 16:46:56 -070025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.RequiresPermission;
Daniel Sandler01df1c62014-06-09 10:54:01 -040027import android.annotation.SdkConstant;
28import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040029import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Context;
31import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010032import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040033import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020034import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050035import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020036import android.content.res.ColorStateList;
Anthony Chenad4d1582017-04-10 16:07:58 -070037import android.content.res.Configuration;
38import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040039import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010040import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070041import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010042import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010043import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040044import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040045import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080047import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070048import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040050import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020051import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050052import android.os.Bundle;
Dianne Hackborn98305522017-05-05 17:53:53 -070053import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Parcel;
55import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040056import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080057import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070058import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070059import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.SpannableStringBuilder;
61import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080063import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080064import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070065import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080066import android.text.style.RelativeSizeSpan;
67import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070068import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040069import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050070import android.util.SparseArray;
Yi Jin6b514142017-10-30 14:54:12 -070071import android.util.proto.ProtoOutputStream;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080073import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080074import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070075import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070076import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.RemoteViews;
78
Griff Hazen959591e2014-05-15 22:26:18 -070079import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070080import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070081import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070082import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080083import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070084
Tor Norbyed9273d62013-05-30 15:59:53 -070085import java.lang.annotation.Retention;
86import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020087import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050088import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070089import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070090import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070091import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050092import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094/**
95 * A class that represents how a persistent notification is to be presented to
96 * the user using the {@link android.app.NotificationManager}.
97 *
Joe Onoratocb109a02011-01-18 17:57:41 -080098 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
99 * easier to construct Notifications.</p>
100 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700101 * <div class="special reference">
102 * <h3>Developer Guides</h3>
103 * <p>For a guide to creating notifications, read the
104 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
105 * developer guide.</p>
106 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 */
108public class Notification implements Parcelable
109{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400110 private static final String TAG = "Notification";
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400113 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400114 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400115 */
116 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
117 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
118 = "android.intent.category.NOTIFICATION_PREFERENCES";
119
120 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500121 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
122 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400123 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500124 */
125 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
126
127 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400128 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400129 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
130 * what settings should be shown in the target app.
131 */
132 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
133
134 /**
135 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400136 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
137 * that can be used to narrow down what settings should be shown in the target app.
138 */
139 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
140
141 /**
142 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
143 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
144 * that can be used to narrow down what settings should be shown in the target app.
145 */
146 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Use all default values (where applicable).
150 */
151 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Use the default notification sound. This will ignore any given
155 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 *
Chris Wren47c20a12014-06-18 17:27:29 -0400157 * <p>
158 * A notification that is noisy is more likely to be presented as a heads-up notification.
159 * </p>
160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 */
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public static final int DEFAULT_SOUND = 1;
165
166 /**
167 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500168 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700169 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500170 *
Chris Wren47c20a12014-06-18 17:27:29 -0400171 * <p>
172 * A notification that vibrates is more likely to be presented as a heads-up notification.
173 * </p>
174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 */
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
181 * Use the default notification lights. This will ignore the
182 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
183 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 */
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200191 * Maximum length of CharSequences accepted by Builder and friends.
192 *
193 * <p>
194 * Avoids spamming the system with overly large strings such as full e-mails.
195 */
196 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
197
198 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800199 * Maximum entries of reply text that are accepted by Builder and friends.
200 */
201 private static final int MAX_REPLY_HISTORY = 5;
202
Selim Cinekde4de0e2018-01-24 16:21:07 -0800203
204 /**
205 * If the notification contained an unsent draft for a RemoteInput when the user clicked on it,
206 * we're adding the draft as a String extra to the {@link #contentIntent} using this key.
207 *
208 * <p>Apps may use this extra to prepopulate text fields in the app, where the user usually
209 * sends messages.</p>
210 */
211 public static final String EXTRA_REMOTE_INPUT_DRAFT = "android.remoteInputDraft";
212
Adrian Roose458aa82015-12-08 16:17:19 -0800213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800215 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500216 * Default value: {@link System#currentTimeMillis() Now}.
217 *
218 * Choose a timestamp that will be most relevant to the user. For most finite events, this
219 * corresponds to the time the event happened (or will happen, in the case of events that have
220 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800221 * timestamped according to when the activity began.
222 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800224 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * <ul>
226 * <li>Notification of a new chat message should be stamped when the message was received.</li>
227 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
228 * <li>Notification of a completed file download should be stamped when the download finished.</li>
229 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
230 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
231 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800232 * </ul>
233 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700234 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
235 * anymore by default and must be opted into by using
236 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public long when;
239
240 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700241 * The creation time of the notification
242 */
243 private long creationTime;
244
245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400247 *
248 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
Dan Sandler86647982015-05-13 23:41:13 -0400250 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700251 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 public int icon;
253
254 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800255 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
256 * leave it at its default value of 0.
257 *
258 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700259 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800260 */
261 public int iconLevel;
262
263 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500264 * The number of events that this notification represents. For example, in a new mail
265 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800266 *
Julia Reynolds30331982017-04-27 10:12:50 -0400267 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500268 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
269 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 */
Julia Reynolds30331982017-04-27 10:12:50 -0400271 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
273 /**
274 * The intent to execute when the expanded status entry is clicked. If
275 * this is an activity, it must include the
276 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800277 * that you take care of task management as described in the
278 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800279 * Stack</a> document. In particular, make sure to read the notification section
280 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
281 * Notifications</a> for the correct ways to launch an application from a
282 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 */
284 public PendingIntent contentIntent;
285
286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500287 * The intent to execute when the notification is explicitly dismissed by the user, either with
288 * the "Clear All" button or by swiping it away individually.
289 *
290 * This probably shouldn't be launching an activity since several of those will be sent
291 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 */
293 public PendingIntent deleteIntent;
294
295 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700296 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800297 *
Chris Wren47c20a12014-06-18 17:27:29 -0400298 * <p>
299 * The system UI may choose to display a heads-up notification, instead of
300 * launching this intent, while the user is using the device.
301 * </p>
302 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800303 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400304 */
305 public PendingIntent fullScreenIntent;
306
307 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400308 * Text that summarizes this notification for accessibility services.
309 *
310 * As of the L release, this text is no longer shown on screen, but it is still useful to
311 * accessibility services (where it serves as an audible announcement of the notification's
312 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400313 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800314 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 */
316 public CharSequence tickerText;
317
318 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400319 * Formerly, a view showing the {@link #tickerText}.
320 *
321 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400322 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400323 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800324 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400325
326 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400327 * The view that will represent this notification in the notification list (which is pulled
328 * down from the status bar).
329 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500330 * As of N, this field may be null. The notification view is determined by the inputs
331 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400332 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400334 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 public RemoteViews contentView;
336
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400337 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400338 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400339 * opportunity to show more detail. The system UI may choose to show this
340 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400341 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500342 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400343 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
344 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400345 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400347 public RemoteViews bigContentView;
348
Chris Wren8fd39ec2014-02-27 17:43:26 -0500349
350 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400351 * A medium-format version of {@link #contentView}, providing the Notification an
352 * opportunity to add action buttons to contentView. At its discretion, the system UI may
353 * choose to show this as a heads-up notification, which will pop up so the user can see
354 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400355 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500356 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400357 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
358 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500359 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400360 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500361 public RemoteViews headsUpContentView;
362
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400363 /**
Dan Sandler86647982015-05-13 23:41:13 -0400364 * A large bitmap to be shown in the notification content area.
365 *
366 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 */
Dan Sandler86647982015-05-13 23:41:13 -0400368 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800369 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
371 /**
372 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500373 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400375 * A notification that is noisy is more likely to be presented as a heads-up notification.
376 * </p>
377 *
378 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500379 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500381 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public Uri sound;
385
386 /**
387 * Use this constant as the value for audioStreamType to request that
388 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700389 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400390 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500391 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700393 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public static final int STREAM_DEFAULT = -1;
395
396 /**
397 * The audio stream type to use when playing the sound.
398 * Should be one of the STREAM_ constants from
399 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400400 *
401 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700403 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 public int audioStreamType = STREAM_DEFAULT;
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400407 * The default value of {@link #audioAttributes}.
408 */
409 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
410 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
411 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
412 .build();
413
414 /**
415 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500416 *
417 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400418 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400420 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
421
422 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500423 * The pattern with which to vibrate.
424 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * <p>
426 * To vibrate the default pattern, see {@link #defaults}.
427 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500430 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public long[] vibrate;
434
435 /**
436 * The color of the led. The hardware will do its best approximation.
437 *
438 * @see #FLAG_SHOW_LIGHTS
439 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500440 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 */
Tor Norbye80756e32015-03-02 09:39:27 -0800442 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public int ledARGB;
445
446 /**
447 * The number of milliseconds for the LED to be on while it's flashing.
448 * The hardware will do its best approximation.
449 *
450 * @see #FLAG_SHOW_LIGHTS
451 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500452 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500454 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 public int ledOnMS;
456
457 /**
458 * The number of milliseconds for the LED to be off while it's flashing.
459 * The hardware will do its best approximation.
460 *
461 * @see #FLAG_SHOW_LIGHTS
462 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500463 *
464 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500466 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 public int ledOffMS;
468
469 /**
470 * Specifies which values should be taken from the defaults.
471 * <p>
472 * To set, OR the desired from {@link #DEFAULT_SOUND},
473 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
474 * values, use {@link #DEFAULT_ALL}.
475 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500476 *
477 * @deprecated use {@link NotificationChannel#getSound()} and
478 * {@link NotificationChannel#shouldShowLights()} and
479 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 public int defaults;
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 /**
485 * Bit to be bitwise-ored into the {@link #flags} field that should be
486 * set if you want the LED on for this notification.
487 * <ul>
488 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
489 * or 0 for both ledOnMS and ledOffMS.</li>
490 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
491 * <li>To flash the LED, pass the number of milliseconds that it should
492 * be on and off to ledOnMS and ledOffMS.</li>
493 * </ul>
494 * <p>
495 * Since hardware varies, you are not guaranteed that any of the values
496 * you pass are honored exactly. Use the system defaults (TODO) if possible
497 * because they will be set to values that work on any given hardware.
498 * <p>
499 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500500 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500501 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500503 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
505
506 /**
507 * Bit to be bitwise-ored into the {@link #flags} field that should be
508 * set if this notification is in reference to something that is ongoing,
509 * like a phone call. It should not be set if this notification is in
510 * reference to something that happened at a particular point in time,
511 * like a missed phone call.
512 */
513 public static final int FLAG_ONGOING_EVENT = 0x00000002;
514
515 /**
516 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700517 * the audio will be repeated until the notification is
518 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 */
520 public static final int FLAG_INSISTENT = 0x00000004;
521
522 /**
523 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700524 * set if you would only like the sound, vibrate and ticker to be played
525 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 */
527 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
528
529 /**
530 * Bit to be bitwise-ored into the {@link #flags} field that should be
531 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500532 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 */
534 public static final int FLAG_AUTO_CANCEL = 0x00000010;
535
536 /**
537 * Bit to be bitwise-ored into the {@link #flags} field that should be
538 * set if the notification should not be canceled when the user clicks
539 * the Clear all button.
540 */
541 public static final int FLAG_NO_CLEAR = 0x00000020;
542
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700543 /**
544 * Bit to be bitwise-ored into the {@link #flags} field that should be
545 * set if this notification represents a currently running service. This
546 * will normally be set for you by {@link Service#startForeground}.
547 */
548 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
549
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400550 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500551 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800552 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500553 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400554 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700555 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500556 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400557
Griff Hazendfcb0802014-02-11 12:00:00 -0800558 /**
559 * Bit to be bitswise-ored into the {@link #flags} field that should be
560 * set if this notification is relevant to the current device only
561 * and it is not recommended that it bridge to other devices.
562 */
563 public static final int FLAG_LOCAL_ONLY = 0x00000100;
564
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700565 /**
566 * Bit to be bitswise-ored into the {@link #flags} field that should be
567 * set if this notification is the group summary for a group of notifications.
568 * Grouped notifications may display in a cluster or stack on devices which
569 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
570 */
571 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
572
Julia Reynoldse46bb372016-03-17 11:05:58 -0400573 /**
574 * Bit to be bitswise-ored into the {@link #flags} field that should be
575 * set if this notification is the group summary for an auto-group of notifications.
576 *
577 * @hide
578 */
579 @SystemApi
580 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
581
Julia Reynolds4db59552017-06-30 13:34:01 -0400582 /**
583 * @hide
584 */
585 public static final int FLAG_CAN_COLORIZE = 0x00000800;
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public int flags;
588
Tor Norbyed9273d62013-05-30 15:59:53 -0700589 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700590 @IntDef(prefix = { "PRIORITY_" }, value = {
591 PRIORITY_DEFAULT,
592 PRIORITY_LOW,
593 PRIORITY_MIN,
594 PRIORITY_HIGH,
595 PRIORITY_MAX
596 })
Tor Norbyed9273d62013-05-30 15:59:53 -0700597 @Retention(RetentionPolicy.SOURCE)
598 public @interface Priority {}
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500601 * Default notification {@link #priority}. If your application does not prioritize its own
602 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500603 *
604 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500606 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 public static final int PRIORITY_DEFAULT = 0;
608
609 /**
610 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
611 * items smaller, or at a different position in the list, compared with your app's
612 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500613 *
614 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500616 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 public static final int PRIORITY_LOW = -1;
618
619 /**
620 * Lowest {@link #priority}; these items might not be shown to the user except under special
621 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500622 *
623 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500625 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 public static final int PRIORITY_MIN = -2;
627
628 /**
629 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
630 * show these items larger, or at a different position in notification lists, compared with
631 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500632 *
633 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500635 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500636 public static final int PRIORITY_HIGH = 1;
637
638 /**
639 * Highest {@link #priority}, for your application's most important items that require the
640 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500641 *
642 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 public static final int PRIORITY_MAX = 2;
646
647 /**
648 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800649 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 * Priority is an indication of how much of the user's valuable attention should be consumed by
651 * this notification. Low-priority notifications may be hidden from the user in certain
652 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500653 * system will make a determination about how to interpret this priority when presenting
654 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400655 *
656 * <p>
657 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
658 * as a heads-up notification.
659 * </p>
660 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500661 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700663 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500664 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800666
Dan Sandler26e81cf2014-05-06 10:01:27 -0400667 /**
668 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
669 * to be applied by the standard Style templates when presenting this notification.
670 *
671 * The current template design constructs a colorful header image by overlaying the
672 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
673 * ignored.
674 */
Tor Norbye80756e32015-03-02 09:39:27 -0800675 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400676 public int color = COLOR_DEFAULT;
677
678 /**
679 * Special value of {@link #color} telling the system not to decorate this notification with
680 * any special color but instead use default colors when presenting this notification.
681 */
Tor Norbye80756e32015-03-02 09:39:27 -0800682 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400683 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600684
685 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800686 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800687 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800688 */
689 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800690 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800691
692 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700693 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
694 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600695 * lockscreen).
696 *
697 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
698 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
699 * shown in all situations, but the contents are only available if the device is unlocked for
700 * the appropriate user.
701 *
702 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
703 * can be read even in an "insecure" context (that is, above a secure lockscreen).
704 * To modify the public version of this notification—for example, to redact some portions—see
705 * {@link Builder#setPublicVersion(Notification)}.
706 *
707 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
708 * and ticker until the user has bypassed the lockscreen.
709 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600710 public @Visibility int visibility;
711
712 /** @hide */
713 @IntDef(prefix = { "VISIBILITY_" }, value = {
714 VISIBILITY_PUBLIC,
715 VISIBILITY_PRIVATE,
716 VISIBILITY_SECRET,
717 })
718 @Retention(RetentionPolicy.SOURCE)
719 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600720
Griff Hazenfc3922d2014-08-20 11:56:44 -0700721 /**
722 * Notification visibility: Show this notification in its entirety on all lockscreens.
723 *
724 * {@see #visibility}
725 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600726 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700727
728 /**
729 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
730 * private information on secure lockscreens.
731 *
732 * {@see #visibility}
733 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600734 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700735
736 /**
737 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
738 *
739 * {@see #visibility}
740 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600741 public static final int VISIBILITY_SECRET = -1;
742
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400746 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747
748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400751 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500752
753 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400756 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757
758 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400759 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500760 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400761 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762
763 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400764 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500765 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400766 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500767
768 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400769 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500770 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400771 public static final String CATEGORY_ALARM = "alarm";
772
773 /**
774 * Notification category: progress of a long-running background operation.
775 */
776 public static final String CATEGORY_PROGRESS = "progress";
777
778 /**
779 * Notification category: social network or sharing update.
780 */
781 public static final String CATEGORY_SOCIAL = "social";
782
783 /**
784 * Notification category: error in background operation or authentication status.
785 */
786 public static final String CATEGORY_ERROR = "err";
787
788 /**
789 * Notification category: media transport control for playback.
790 */
791 public static final String CATEGORY_TRANSPORT = "transport";
792
793 /**
794 * Notification category: system or device status update. Reserved for system use.
795 */
796 public static final String CATEGORY_SYSTEM = "sys";
797
798 /**
799 * Notification category: indication of running background service.
800 */
801 public static final String CATEGORY_SERVICE = "service";
802
803 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400804 * Notification category: a specific, timely recommendation for a single thing.
805 * For example, a news app might want to recommend a news story it believes the user will
806 * want to read next.
807 */
808 public static final String CATEGORY_RECOMMENDATION = "recommendation";
809
810 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400811 * Notification category: ongoing information about device or contextual status.
812 */
813 public static final String CATEGORY_STATUS = "status";
814
815 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400816 * Notification category: user-scheduled reminder.
817 */
818 public static final String CATEGORY_REMINDER = "reminder";
819
820 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400821 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
822 * that best describes this Notification. May be used by the system for ranking and filtering.
823 */
824 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500825
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700826 private String mGroupKey;
827
828 /**
829 * Get the key used to group this notification into a cluster or stack
830 * with other notifications on devices which support such rendering.
831 */
832 public String getGroup() {
833 return mGroupKey;
834 }
835
836 private String mSortKey;
837
838 /**
839 * Get a sort key that orders this notification among other notifications from the
840 * same package. This can be useful if an external sort was already applied and an app
841 * would like to preserve this. Notifications will be sorted lexicographically using this
842 * value, although providing different priorities in addition to providing sort key may
843 * cause this value to be ignored.
844 *
845 * <p>This sort key can also be used to order members of a notification group. See
846 * {@link Builder#setGroup}.
847 *
848 * @see String#compareTo(String)
849 */
850 public String getSortKey() {
851 return mSortKey;
852 }
853
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400855 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400856 * <p>
857 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
858 * APIs, and are intended to be used by
859 * {@link android.service.notification.NotificationListenerService} implementations to extract
860 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500861 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 public Bundle extras = new Bundle();
863
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700865 * All pending intents in the notification as the system needs to be able to access them but
866 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700867 * custom parcelable objects.
868 *
869 * @hide
870 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700871 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700872
873 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700874 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
875 * pending intents inside of it, so only those will get the behavior.
876 *
877 * @hide
878 */
Adrian Roosfb921842017-10-26 14:49:56 +0200879 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700880
881 /**
882 * Must be set by a process to start associating tokens with Notification objects
883 * coming in to it. This is set by NotificationManagerService.
884 *
885 * @hide
886 */
887 static public IBinder processWhitelistToken;
888
889 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400890 * {@link #extras} key: this is the title of the notification,
891 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
892 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500893 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894
895 /**
896 * {@link #extras} key: this is the title of the notification when shown in expanded form,
897 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
898 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400899 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900
901 /**
902 * {@link #extras} key: this is the main text payload, as supplied to
903 * {@link Builder#setContentText(CharSequence)}.
904 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500905 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400906
907 /**
908 * {@link #extras} key: this is a third line of text, as supplied to
909 * {@link Builder#setSubText(CharSequence)}.
910 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400911 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400912
913 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800914 * {@link #extras} key: this is the remote input history, as supplied to
915 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700916 *
917 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
918 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
919 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
920 * notifications once the other party has responded).
921 *
922 * The extra with this key is of type CharSequence[] and contains the most recent entry at
923 * the 0 index, the second most recent at the 1 index, etc.
924 *
925 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800926 */
927 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
928
929 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400930 * {@link #extras} key: this is a small piece of additional text as supplied to
931 * {@link Builder#setContentInfo(CharSequence)}.
932 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400933 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934
935 /**
936 * {@link #extras} key: this is a line of summary information intended to be shown
937 * alongside expanded notifications, as supplied to (e.g.)
938 * {@link BigTextStyle#setSummaryText(CharSequence)}.
939 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400940 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400941
942 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200943 * {@link #extras} key: this is the longer text shown in the big form of a
944 * {@link BigTextStyle} notification, as supplied to
945 * {@link BigTextStyle#bigText(CharSequence)}.
946 */
947 public static final String EXTRA_BIG_TEXT = "android.bigText";
948
949 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
951 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400952 *
953 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400955 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500956 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
960 * notification payload, as
961 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400962 *
963 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400965 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400966 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400967
968 /**
969 * {@link #extras} key: this is a bitmap to be used instead of the one from
970 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
971 * shown in its expanded form, as supplied to
972 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
973 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400974 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400975
976 /**
977 * {@link #extras} key: this is the progress value supplied to
978 * {@link Builder#setProgress(int, int, boolean)}.
979 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400980 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400981
982 /**
983 * {@link #extras} key: this is the maximum value supplied to
984 * {@link Builder#setProgress(int, int, boolean)}.
985 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400986 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400987
988 /**
989 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
990 * {@link Builder#setProgress(int, int, boolean)}.
991 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400992 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400993
994 /**
995 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
996 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
997 * {@link Builder#setUsesChronometer(boolean)}.
998 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400999 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001000
1001 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08001002 * {@link #extras} key: whether the chronometer set on the notification should count down
1003 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -07001004 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -08001005 */
Adrian Roos96b7e202016-05-17 13:50:38 -07001006 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -08001007
1008 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 * {@link #extras} key: whether {@link #when} should be shown,
1010 * as supplied to {@link Builder#setShowWhen(boolean)}.
1011 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001012 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013
1014 /**
1015 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
1016 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1017 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001018 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019
1020 /**
1021 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1022 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1023 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001024 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001025
1026 /**
1027 * {@link #extras} key: A string representing the name of the specific
1028 * {@link android.app.Notification.Style} used to create this notification.
1029 */
Chris Wren91ad5632013-06-05 15:05:57 -04001030 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001031
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001033 * {@link #extras} key: A String array containing the people that this notification relates to,
1034 * each of which was supplied to {@link Builder#addPerson(String)}.
Selim Cineke7238dd2017-12-14 17:48:32 -08001035 *
1036 * @deprecated the actual objects are now in {@link #EXTRA_PEOPLE_LIST}
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001037 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001038 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001039
1040 /**
Selim Cineke7238dd2017-12-14 17:48:32 -08001041 * {@link #extras} key: An arrayList of {@link Person} objects containing the people that
1042 * this notification relates to.
1043 */
1044 public static final String EXTRA_PEOPLE_LIST = "android.people.list";
1045
1046 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001047 * Allow certain system-generated notifications to appear before the device is provisioned.
1048 * Only available to notifications coming from the android package.
1049 * @hide
1050 */
Maurice Lam96c10032017-03-29 15:42:38 -07001051 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001052 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001053 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1054
1055 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001056 * {@link #extras} key: A
1057 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1058 * in the background when the notification is selected. The URI must point to an image stream
1059 * suitable for passing into
1060 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1061 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1062 * URI used for this purpose must require no permissions to read the image data.
1063 */
1064 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1065
1066 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001067 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001068 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001069 * {@link android.app.Notification.MediaStyle} notification.
1070 */
1071 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1072
1073 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001074 * {@link #extras} key: the indices of actions to be shown in the compact view,
1075 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1076 */
1077 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1078
Christoph Studer943aa672014-08-03 20:31:16 +02001079 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001080 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1081 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001082 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1083 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001084 *
1085 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001086 */
1087 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1088
1089 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001090 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1091 * direct replies
1092 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1093 * {@link Person}
1094 */
1095 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1096
1097 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001098 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001099 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001100 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001101 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001102
1103 /**
1104 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1105 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001106 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1107 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001108 */
1109 public static final String EXTRA_MESSAGES = "android.messages";
1110
1111 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001112 * {@link #extras} key: an array of
1113 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1114 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1115 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1116 * array of bundles.
1117 */
1118 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1119
1120 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001121 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1122 * represents a group conversation.
1123 */
1124 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1125
1126 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001127 * {@link #extras} key: whether the notification should be colorized as
1128 * supplied to {@link Builder#setColorized(boolean)}}.
1129 */
1130 public static final String EXTRA_COLORIZED = "android.colorized";
1131
1132 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001133 * @hide
1134 */
1135 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1136
Selim Cinek247fa012016-02-18 09:50:48 -08001137 /**
1138 * @hide
1139 */
1140 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1141
Shane Brennan472a3b32016-12-12 15:28:10 -08001142 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001143 * @hide
1144 */
1145 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1146
1147 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001148 * {@link #extras} key: the audio contents of this notification.
1149 *
1150 * This is for use when rendering the notification on an audio-focused interface;
1151 * the audio contents are a complete sound sample that contains the contents/body of the
1152 * notification. This may be used in substitute of a Text-to-Speech reading of the
1153 * notification. For example if the notification represents a voice message this should point
1154 * to the audio of that message.
1155 *
1156 * The data stored under this key should be a String representation of a Uri that contains the
1157 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1158 *
1159 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1160 * has a field for holding data URI. That field can be used for audio.
1161 * See {@code Message#setData}.
1162 *
1163 * Example usage:
1164 * <pre>
1165 * {@code
1166 * Notification.Builder myBuilder = (build your Notification as normal);
1167 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1168 * }
1169 * </pre>
1170 */
1171 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1172
Dan Sandler80eaa592016-04-14 23:34:54 -04001173 /** @hide */
1174 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001175 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001176 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1177
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001178 /**
1179 * This is set on the notification shown by the activity manager about all apps
1180 * running in the background. It indicates that the notification should be shown
1181 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1182 * notification currently visible to the user. This is a string array of all
1183 * package names of the apps.
1184 * @hide
1185 */
1186 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1187
Dan Sandlerd63f9322015-05-06 15:18:49 -04001188 private Icon mSmallIcon;
1189 private Icon mLargeIcon;
1190
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001191 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001192 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001193
Julia Reynolds13d898c2017-02-02 12:22:05 -05001194 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001195 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001196
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001197 /** @hide */
1198 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1199 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1200 })
1201 @Retention(RetentionPolicy.SOURCE)
1202 public @interface GroupAlertBehavior {}
1203
1204 /**
1205 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1206 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1207 * notification will not be muted when it is in a group.
1208 */
1209 public static final int GROUP_ALERT_ALL = 0;
1210
1211 /**
1212 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1213 * notification in a group should be silenced (no sound or vibration) even if they are posted
1214 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001215 * mute this notification if this notification is a group child. This must be applied to all
1216 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001217 *
1218 * <p> For example, you might want to use this constant if you post a number of children
1219 * notifications at once (say, after a periodic sync), and only need to notify the user
1220 * audibly once.
1221 */
1222 public static final int GROUP_ALERT_SUMMARY = 1;
1223
1224 /**
1225 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1226 * notification in a group should be silenced (no sound or vibration) even if they are
1227 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1228 * to mute this notification if this notification is a group summary.
1229 *
1230 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001231 * in your group have content and the summary is only used to visually group notifications
1232 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001233 */
1234 public static final int GROUP_ALERT_CHILDREN = 2;
1235
Julia Reynolds2f431e22017-06-07 14:12:09 +00001236 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001237
Julia Reynolds13d898c2017-02-02 12:22:05 -05001238 /**
1239 * If this notification is being shown as a badge, always show as a number.
1240 */
1241 public static final int BADGE_ICON_NONE = 0;
1242
1243 /**
1244 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1245 * represent this notification.
1246 */
1247 public static final int BADGE_ICON_SMALL = 1;
1248
1249 /**
1250 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1251 * represent this notification.
1252 */
1253 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001254 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001255
Chris Wren51c75102013-07-16 20:49:17 -04001256 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001257 * Structure to encapsulate a named action that can be shown as part of this notification.
1258 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1259 * selected by the user.
1260 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001261 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1262 * or {@link Notification.Builder#addAction(Notification.Action)}
1263 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001265 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001266 /**
1267 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1268 * {@link RemoteInput}s.
1269 *
1270 * This is intended for {@link RemoteInput}s that only accept data, meaning
1271 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1272 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1273 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1274 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1275 *
1276 * You can test if a RemoteInput matches these constraints using
1277 * {@link RemoteInput#isDataOnly}.
1278 */
1279 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1280
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001281 /**
1282 * {@link }: No semantic action defined.
1283 */
1284 public static final int SEMANTIC_ACTION_NONE = 0;
1285
1286 /**
1287 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1288 * may be appropriate.
1289 */
1290 public static final int SEMANTIC_ACTION_REPLY = 1;
1291
1292 /**
1293 * {@code SemanticAction}: Mark content as read.
1294 */
1295 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1296
1297 /**
1298 * {@code SemanticAction}: Mark content as unread.
1299 */
1300 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1301
1302 /**
1303 * {@code SemanticAction}: Delete the content associated with the notification. This
1304 * could mean deleting an email, message, etc.
1305 */
1306 public static final int SEMANTIC_ACTION_DELETE = 4;
1307
1308 /**
1309 * {@code SemanticAction}: Archive the content associated with the notification. This
1310 * could mean archiving an email, message, etc.
1311 */
1312 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1313
1314 /**
1315 * {@code SemanticAction}: Mute the content associated with the notification. This could
1316 * mean silencing a conversation or currently playing media.
1317 */
1318 public static final int SEMANTIC_ACTION_MUTE = 6;
1319
1320 /**
1321 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1322 * mean un-silencing a conversation or currently playing media.
1323 */
1324 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1325
1326 /**
1327 * {@code SemanticAction}: Mark content with a thumbs up.
1328 */
1329 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1330
1331 /**
1332 * {@code SemanticAction}: Mark content with a thumbs down.
1333 */
1334 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1335
1336
Griff Hazen959591e2014-05-15 22:26:18 -07001337 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001338 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001339 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001340 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001341 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001342
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001343 /**
1344 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001345 *
1346 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001347 */
Dan Sandler86647982015-05-13 23:41:13 -04001348 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001349 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001350
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001351 /**
1352 * Title of the action.
1353 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001354 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001355
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001356 /**
1357 * Intent to send when the user invokes this action. May be null, in which case the action
1358 * may be rendered in a disabled presentation by the system UI.
1359 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001360 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001361
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001362 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001363 if (in.readInt() != 0) {
1364 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001365 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1366 icon = mIcon.getResId();
1367 }
Dan Sandler86647982015-05-13 23:41:13 -04001368 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001369 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1370 if (in.readInt() == 1) {
1371 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1372 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001373 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001374 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001375 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001376 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001377 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001378
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001379 /**
Dan Sandler86647982015-05-13 23:41:13 -04001380 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001381 */
Dan Sandler86647982015-05-13 23:41:13 -04001382 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001383 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001384 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1385 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001386 }
1387
Adrian Roos7af53622016-10-12 13:44:05 -07001388 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001389 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001390 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1391 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001392 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001393 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1394 this.icon = icon.getResId();
1395 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001396 this.title = title;
1397 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001398 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001399 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001400 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001401 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001402 }
1403
1404 /**
Dan Sandler86647982015-05-13 23:41:13 -04001405 * Return an icon representing the action.
1406 */
1407 public Icon getIcon() {
1408 if (mIcon == null && icon != 0) {
1409 // you snuck an icon in here without using the builder; let's try to keep it
1410 mIcon = Icon.createWithResource("", icon);
1411 }
1412 return mIcon;
1413 }
1414
1415 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001416 * Get additional metadata carried around with this Action.
1417 */
1418 public Bundle getExtras() {
1419 return mExtras;
1420 }
1421
1422 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001423 * Return whether the platform should automatically generate possible replies for this
1424 * {@link Action}
1425 */
1426 public boolean getAllowGeneratedReplies() {
1427 return mAllowGeneratedReplies;
1428 }
1429
1430 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001432 * May return null if no remote inputs were added. Only returns inputs which accept
1433 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001434 */
1435 public RemoteInput[] getRemoteInputs() {
1436 return mRemoteInputs;
1437 }
1438
1439 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001440 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1441 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1442 * (eg. reply, mark as read, delete, etc).
1443 */
1444 public @SemanticAction int getSemanticAction() {
1445 return mSemanticAction;
1446 }
1447
1448 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001449 * Get the list of inputs to be collected from the user that ONLY accept data when this
1450 * action is sent. These remote inputs are guaranteed to return true on a call to
1451 * {@link RemoteInput#isDataOnly}.
1452 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001453 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001454 *
1455 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1456 * of non-textual RemoteInputs do not access these remote inputs.
1457 */
1458 public RemoteInput[] getDataOnlyRemoteInputs() {
1459 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1460 }
1461
1462 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001463 * Builder class for {@link Action} objects.
1464 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001465 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001466 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001467 private final CharSequence mTitle;
1468 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001469 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001470 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001471 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001472 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001473
1474 /**
1475 * Construct a new builder for {@link Action} object.
1476 * @param icon icon to show for this action
1477 * @param title the title of the action
1478 * @param intent the {@link PendingIntent} to fire when users trigger this action
1479 */
Dan Sandler86647982015-05-13 23:41:13 -04001480 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001481 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001482 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001483 }
1484
1485 /**
1486 * Construct a new builder for {@link Action} object.
1487 * @param icon icon to show for this action
1488 * @param title the title of the action
1489 * @param intent the {@link PendingIntent} to fire when users trigger this action
1490 */
1491 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001492 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001493 }
1494
1495 /**
1496 * Construct a new builder for {@link Action} object using the fields from an
1497 * {@link Action}.
1498 * @param action the action to read fields from.
1499 */
1500 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001501 this(action.getIcon(), action.title, action.actionIntent,
1502 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001503 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001504 }
1505
Dan Sandler86647982015-05-13 23:41:13 -04001506 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001507 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1508 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001509 mIcon = icon;
1510 mTitle = title;
1511 mIntent = intent;
1512 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001513 if (remoteInputs != null) {
1514 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1515 Collections.addAll(mRemoteInputs, remoteInputs);
1516 }
Adrian Roos7af53622016-10-12 13:44:05 -07001517 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001518 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001519 }
1520
1521 /**
1522 * Merge additional metadata into this builder.
1523 *
1524 * <p>Values within the Bundle will replace existing extras values in this Builder.
1525 *
1526 * @see Notification.Action#extras
1527 */
1528 public Builder addExtras(Bundle extras) {
1529 if (extras != null) {
1530 mExtras.putAll(extras);
1531 }
1532 return this;
1533 }
1534
1535 /**
1536 * Get the metadata Bundle used by this Builder.
1537 *
1538 * <p>The returned Bundle is shared with this Builder.
1539 */
1540 public Bundle getExtras() {
1541 return mExtras;
1542 }
1543
1544 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001545 * Add an input to be collected from the user when this action is sent.
1546 * Response values can be retrieved from the fired intent by using the
1547 * {@link RemoteInput#getResultsFromIntent} function.
1548 * @param remoteInput a {@link RemoteInput} to add to the action
1549 * @return this object for method chaining
1550 */
1551 public Builder addRemoteInput(RemoteInput remoteInput) {
1552 if (mRemoteInputs == null) {
1553 mRemoteInputs = new ArrayList<RemoteInput>();
1554 }
1555 mRemoteInputs.add(remoteInput);
1556 return this;
1557 }
1558
1559 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001560 * Set whether the platform should automatically generate possible replies to add to
1561 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1562 * {@link RemoteInput}, this has no effect.
1563 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1564 * otherwise
1565 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001566 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001567 */
1568 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1569 mAllowGeneratedReplies = allowGeneratedReplies;
1570 return this;
1571 }
1572
1573 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001574 * Sets the {@code SemanticAction} for this {@link Action}. A
1575 * {@code SemanticAction} denotes what an {@link Action}'s
1576 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1577 * @param semanticAction a SemanticAction defined within {@link Action} with
1578 * {@code SEMANTIC_ACTION_} prefixes
1579 * @return this object for method chaining
1580 */
1581 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1582 mSemanticAction = semanticAction;
1583 return this;
1584 }
1585
1586 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001587 * Apply an extender to this action builder. Extenders may be used to add
1588 * metadata or change options on this builder.
1589 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001590 public Builder extend(Extender extender) {
1591 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001592 return this;
1593 }
1594
1595 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001596 * Combine all of the options that have been set and return a new {@link Action}
1597 * object.
1598 * @return the built action
1599 */
1600 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001601 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1602 RemoteInput[] previousDataInputs =
1603 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001604 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001605 for (RemoteInput input : previousDataInputs) {
1606 dataOnlyInputs.add(input);
1607 }
1608 }
1609 List<RemoteInput> textInputs = new ArrayList<>();
1610 if (mRemoteInputs != null) {
1611 for (RemoteInput input : mRemoteInputs) {
1612 if (input.isDataOnly()) {
1613 dataOnlyInputs.add(input);
1614 } else {
1615 textInputs.add(input);
1616 }
1617 }
1618 }
1619 if (!dataOnlyInputs.isEmpty()) {
1620 RemoteInput[] dataInputsArr =
1621 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1622 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1623 }
1624 RemoteInput[] textInputsArr = textInputs.isEmpty()
1625 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1626 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001627 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001628 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001629 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001630
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001631 @Override
1632 public Action clone() {
1633 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001634 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001635 title,
1636 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001637 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001638 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001639 getAllowGeneratedReplies(),
1640 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001641 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001642
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001643 @Override
1644 public int describeContents() {
1645 return 0;
1646 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001647
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001648 @Override
1649 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001650 final Icon ic = getIcon();
1651 if (ic != null) {
1652 out.writeInt(1);
1653 ic.writeToParcel(out, 0);
1654 } else {
1655 out.writeInt(0);
1656 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001657 TextUtils.writeToParcel(title, out, flags);
1658 if (actionIntent != null) {
1659 out.writeInt(1);
1660 actionIntent.writeToParcel(out, flags);
1661 } else {
1662 out.writeInt(0);
1663 }
Griff Hazen959591e2014-05-15 22:26:18 -07001664 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001665 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001666 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001667 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001668 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001669
Griff Hazen959591e2014-05-15 22:26:18 -07001670 public static final Parcelable.Creator<Action> CREATOR =
1671 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001672 public Action createFromParcel(Parcel in) {
1673 return new Action(in);
1674 }
1675 public Action[] newArray(int size) {
1676 return new Action[size];
1677 }
1678 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001679
1680 /**
1681 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1682 * metadata or change options on an action builder.
1683 */
1684 public interface Extender {
1685 /**
1686 * Apply this extender to a notification action builder.
1687 * @param builder the builder to be modified.
1688 * @return the build object for chaining.
1689 */
1690 public Builder extend(Builder builder);
1691 }
1692
1693 /**
1694 * Wearable extender for notification actions. To add extensions to an action,
1695 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1696 * the {@code WearableExtender()} constructor and apply it to a
1697 * {@link android.app.Notification.Action.Builder} using
1698 * {@link android.app.Notification.Action.Builder#extend}.
1699 *
1700 * <pre class="prettyprint">
1701 * Notification.Action action = new Notification.Action.Builder(
1702 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001703 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001704 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001705 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001706 */
1707 public static final class WearableExtender implements Extender {
1708 /** Notification action extra which contains wearable extensions */
1709 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1710
Pete Gastaf6781d2014-10-07 15:17:05 -04001711 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001712 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001713 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1714 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1715 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001716
1717 // Flags bitwise-ored to mFlags
1718 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001719 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001720 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001721
1722 // Default value for flags integer
1723 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1724
1725 private int mFlags = DEFAULT_FLAGS;
1726
Pete Gastaf6781d2014-10-07 15:17:05 -04001727 private CharSequence mInProgressLabel;
1728 private CharSequence mConfirmLabel;
1729 private CharSequence mCancelLabel;
1730
Griff Hazen61a9e862014-05-22 16:05:19 -07001731 /**
1732 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1733 * options.
1734 */
1735 public WearableExtender() {
1736 }
1737
1738 /**
1739 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1740 * wearable options present in an existing notification action.
1741 * @param action the notification action to inspect.
1742 */
1743 public WearableExtender(Action action) {
1744 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1745 if (wearableBundle != null) {
1746 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001747 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1748 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1749 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001750 }
1751 }
1752
1753 /**
1754 * Apply wearable extensions to a notification action that is being built. This is
1755 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1756 * method of {@link android.app.Notification.Action.Builder}.
1757 */
1758 @Override
1759 public Action.Builder extend(Action.Builder builder) {
1760 Bundle wearableBundle = new Bundle();
1761
1762 if (mFlags != DEFAULT_FLAGS) {
1763 wearableBundle.putInt(KEY_FLAGS, mFlags);
1764 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001765 if (mInProgressLabel != null) {
1766 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1767 }
1768 if (mConfirmLabel != null) {
1769 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1770 }
1771 if (mCancelLabel != null) {
1772 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1773 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001774
1775 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1776 return builder;
1777 }
1778
1779 @Override
1780 public WearableExtender clone() {
1781 WearableExtender that = new WearableExtender();
1782 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001783 that.mInProgressLabel = this.mInProgressLabel;
1784 that.mConfirmLabel = this.mConfirmLabel;
1785 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001786 return that;
1787 }
1788
1789 /**
1790 * Set whether this action is available when the wearable device is not connected to
1791 * a companion device. The user can still trigger this action when the wearable device is
1792 * offline, but a visual hint will indicate that the action may not be available.
1793 * Defaults to true.
1794 */
1795 public WearableExtender setAvailableOffline(boolean availableOffline) {
1796 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1797 return this;
1798 }
1799
1800 /**
1801 * Get whether this action is available when the wearable device is not connected to
1802 * a companion device. The user can still trigger this action when the wearable device is
1803 * offline, but a visual hint will indicate that the action may not be available.
1804 * Defaults to true.
1805 */
1806 public boolean isAvailableOffline() {
1807 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1808 }
1809
1810 private void setFlag(int mask, boolean value) {
1811 if (value) {
1812 mFlags |= mask;
1813 } else {
1814 mFlags &= ~mask;
1815 }
1816 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001817
1818 /**
1819 * Set a label to display while the wearable is preparing to automatically execute the
1820 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1821 *
1822 * @param label the label to display while the action is being prepared to execute
1823 * @return this object for method chaining
1824 */
1825 public WearableExtender setInProgressLabel(CharSequence label) {
1826 mInProgressLabel = label;
1827 return this;
1828 }
1829
1830 /**
1831 * Get the label to display while the wearable is preparing to automatically execute
1832 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1833 *
1834 * @return the label to display while the action is being prepared to execute
1835 */
1836 public CharSequence getInProgressLabel() {
1837 return mInProgressLabel;
1838 }
1839
1840 /**
1841 * Set a label to display to confirm that the action should be executed.
1842 * This is usually an imperative verb like "Send".
1843 *
1844 * @param label the label to confirm the action should be executed
1845 * @return this object for method chaining
1846 */
1847 public WearableExtender setConfirmLabel(CharSequence label) {
1848 mConfirmLabel = label;
1849 return this;
1850 }
1851
1852 /**
1853 * Get the label to display to confirm that the action should be executed.
1854 * This is usually an imperative verb like "Send".
1855 *
1856 * @return the label to confirm the action should be executed
1857 */
1858 public CharSequence getConfirmLabel() {
1859 return mConfirmLabel;
1860 }
1861
1862 /**
1863 * Set a label to display to cancel the action.
1864 * This is usually an imperative verb, like "Cancel".
1865 *
1866 * @param label the label to display to cancel the action
1867 * @return this object for method chaining
1868 */
1869 public WearableExtender setCancelLabel(CharSequence label) {
1870 mCancelLabel = label;
1871 return this;
1872 }
1873
1874 /**
1875 * Get the label to display to cancel the action.
1876 * This is usually an imperative verb like "Cancel".
1877 *
1878 * @return the label to display to cancel the action
1879 */
1880 public CharSequence getCancelLabel() {
1881 return mCancelLabel;
1882 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001883
1884 /**
1885 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1886 * platform that it can generate the appropriate transitions.
1887 * @param hintLaunchesActivity {@code true} if the content intent will launch
1888 * an activity and transitions should be generated, false otherwise.
1889 * @return this object for method chaining
1890 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001891 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001892 boolean hintLaunchesActivity) {
1893 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1894 return this;
1895 }
1896
1897 /**
1898 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1899 * platform that it can generate the appropriate transitions
1900 * @return {@code true} if the content intent will launch an activity and transitions
1901 * should be generated, false otherwise. The default value is {@code false} if this was
1902 * never set.
1903 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001904 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001905 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1906 }
Alex Hills9f087612016-06-07 09:08:59 -04001907
1908 /**
1909 * Set a hint that this Action should be displayed inline.
1910 *
1911 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1912 * otherwise
1913 * @return this object for method chaining
1914 */
1915 public WearableExtender setHintDisplayActionInline(
1916 boolean hintDisplayInline) {
1917 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1918 return this;
1919 }
1920
1921 /**
1922 * Get a hint that this Action should be displayed inline.
1923 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001924 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001925 * otherwise. The default value is {@code false} if this was never set.
1926 */
1927 public boolean getHintDisplayActionInline() {
1928 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1929 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001930 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001931
1932 /**
1933 * Provides meaning to an {@link Action} that hints at what the associated
1934 * {@link PendingIntent} will do. For example, an {@link Action} with a
1935 * {@link PendingIntent} that replies to a text message notification may have the
1936 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1937 *
1938 * @hide
1939 */
1940 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1941 SEMANTIC_ACTION_NONE,
1942 SEMANTIC_ACTION_REPLY,
1943 SEMANTIC_ACTION_MARK_AS_READ,
1944 SEMANTIC_ACTION_MARK_AS_UNREAD,
1945 SEMANTIC_ACTION_DELETE,
1946 SEMANTIC_ACTION_ARCHIVE,
1947 SEMANTIC_ACTION_MUTE,
1948 SEMANTIC_ACTION_UNMUTE,
1949 SEMANTIC_ACTION_THUMBS_UP,
1950 SEMANTIC_ACTION_THUMBS_DOWN
1951 })
1952 @Retention(RetentionPolicy.SOURCE)
1953 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001954 }
1955
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001956 /**
1957 * Array of all {@link Action} structures attached to this notification by
1958 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1959 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1960 * interface for invoking actions.
1961 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001962 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001963
1964 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001965 * Replacement version of this notification whose content will be shown
1966 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1967 * and {@link #VISIBILITY_PUBLIC}.
1968 */
1969 public Notification publicVersion;
1970
1971 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001972 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001973 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 */
1975 public Notification()
1976 {
1977 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001978 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001979 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
1981
1982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 * @hide
1984 */
1985 public Notification(Context context, int icon, CharSequence tickerText, long when,
1986 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1987 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001988 new Builder(context)
1989 .setWhen(when)
1990 .setSmallIcon(icon)
1991 .setTicker(tickerText)
1992 .setContentTitle(contentTitle)
1993 .setContentText(contentText)
1994 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1995 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997
1998 /**
1999 * Constructs a Notification object with the information needed to
2000 * have a status bar icon without the standard expanded view.
2001 *
2002 * @param icon The resource id of the icon to put in the status bar.
2003 * @param tickerText The text that flows by in the status bar when the notification first
2004 * activates.
2005 * @param when The time to show in the time field. In the System.currentTimeMillis
2006 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002007 *
2008 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002010 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 public Notification(int icon, CharSequence tickerText, long when)
2012 {
2013 this.icon = icon;
2014 this.tickerText = tickerText;
2015 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002016 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
2018
2019 /**
2020 * Unflatten the notification from a parcel.
2021 */
Svet Ganovddb94882016-06-23 19:55:24 -07002022 @SuppressWarnings("unchecked")
2023 public Notification(Parcel parcel) {
2024 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2025 // intents in extras are always written as the last entry.
2026 readFromParcelImpl(parcel);
2027 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002028 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002029 }
2030
2031 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 {
2033 int version = parcel.readInt();
2034
Adrian Roosfb921842017-10-26 14:49:56 +02002035 mWhitelistToken = parcel.readStrongBinder();
2036 if (mWhitelistToken == null) {
2037 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002038 }
2039 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002040 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002043 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002044 if (parcel.readInt() != 0) {
2045 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002046 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2047 icon = mSmallIcon.getResId();
2048 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 number = parcel.readInt();
2051 if (parcel.readInt() != 0) {
2052 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2053 }
2054 if (parcel.readInt() != 0) {
2055 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2056 }
2057 if (parcel.readInt() != 0) {
2058 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2059 }
2060 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002061 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002062 }
2063 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2065 }
Joe Onorato561d3852010-11-20 18:09:34 -08002066 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002067 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 defaults = parcel.readInt();
2070 flags = parcel.readInt();
2071 if (parcel.readInt() != 0) {
2072 sound = Uri.CREATOR.createFromParcel(parcel);
2073 }
2074
2075 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002076 if (parcel.readInt() != 0) {
2077 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 vibrate = parcel.createLongArray();
2080 ledARGB = parcel.readInt();
2081 ledOnMS = parcel.readInt();
2082 ledOffMS = parcel.readInt();
2083 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002084
2085 if (parcel.readInt() != 0) {
2086 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2087 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002088
2089 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002090
John Spurlockfd7f1e02014-03-18 16:41:57 -04002091 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002093 mGroupKey = parcel.readString();
2094
2095 mSortKey = parcel.readString();
2096
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002097 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002098 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002099
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002100 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2101
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002102 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002103 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2104 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002105
Chris Wren8fd39ec2014-02-27 17:43:26 -05002106 if (parcel.readInt() != 0) {
2107 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2108 }
2109
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002110 visibility = parcel.readInt();
2111
2112 if (parcel.readInt() != 0) {
2113 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2114 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002115
2116 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002117
2118 if (parcel.readInt() != 0) {
2119 mChannelId = parcel.readString();
2120 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002121 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002122
2123 if (parcel.readInt() != 0) {
2124 mShortcutId = parcel.readString();
2125 }
2126
2127 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002128
2129 if (parcel.readInt() != 0) {
2130 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2131 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002132
2133 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
2135
Andy Stadler110988c2010-12-03 14:29:16 -08002136 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002137 public Notification clone() {
2138 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002139 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002140 return that;
2141 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002142
Daniel Sandler1a497d32013-04-18 14:52:45 -04002143 /**
2144 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2145 * of this into that.
2146 * @hide
2147 */
2148 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002149 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002150 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002151 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002152 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002153 that.number = this.number;
2154
2155 // PendingIntents are global, so there's no reason (or way) to clone them.
2156 that.contentIntent = this.contentIntent;
2157 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002158 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002159
2160 if (this.tickerText != null) {
2161 that.tickerText = this.tickerText.toString();
2162 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002163 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002164 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002165 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002166 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002167 that.contentView = this.contentView.clone();
2168 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002169 if (heavy && this.mLargeIcon != null) {
2170 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002171 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002172 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002173 that.sound = this.sound; // android.net.Uri is immutable
2174 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002175 if (this.audioAttributes != null) {
2176 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2177 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002178
2179 final long[] vibrate = this.vibrate;
2180 if (vibrate != null) {
2181 final int N = vibrate.length;
2182 final long[] vib = that.vibrate = new long[N];
2183 System.arraycopy(vibrate, 0, vib, 0, N);
2184 }
2185
2186 that.ledARGB = this.ledARGB;
2187 that.ledOnMS = this.ledOnMS;
2188 that.ledOffMS = this.ledOffMS;
2189 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002190
Joe Onorato18e69df2010-05-17 22:26:12 -07002191 that.flags = this.flags;
2192
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002194
John Spurlockfd7f1e02014-03-18 16:41:57 -04002195 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002197 that.mGroupKey = this.mGroupKey;
2198
2199 that.mSortKey = this.mSortKey;
2200
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002201 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002202 try {
2203 that.extras = new Bundle(this.extras);
2204 // will unparcel
2205 that.extras.size();
2206 } catch (BadParcelableException e) {
2207 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2208 that.extras = null;
2209 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002210 }
2211
Felipe Lemedd85da62016-06-28 11:29:54 -07002212 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2213 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002214 }
2215
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002216 if (this.actions != null) {
2217 that.actions = new Action[this.actions.length];
2218 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002219 if ( this.actions[i] != null) {
2220 that.actions[i] = this.actions[i].clone();
2221 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002222 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002223 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002224
Daniel Sandler1a497d32013-04-18 14:52:45 -04002225 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002226 that.bigContentView = this.bigContentView.clone();
2227 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002228
Chris Wren8fd39ec2014-02-27 17:43:26 -05002229 if (heavy && this.headsUpContentView != null) {
2230 that.headsUpContentView = this.headsUpContentView.clone();
2231 }
2232
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002233 that.visibility = this.visibility;
2234
2235 if (this.publicVersion != null) {
2236 that.publicVersion = new Notification();
2237 this.publicVersion.cloneInto(that.publicVersion, heavy);
2238 }
2239
Dan Sandler26e81cf2014-05-06 10:01:27 -04002240 that.color = this.color;
2241
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002242 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002243 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002244 that.mShortcutId = this.mShortcutId;
2245 that.mBadgeIcon = this.mBadgeIcon;
2246 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002247 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002248
Daniel Sandler1a497d32013-04-18 14:52:45 -04002249 if (!heavy) {
2250 that.lightenPayload(); // will clean out extras
2251 }
2252 }
2253
2254 /**
2255 * Removes heavyweight parts of the Notification object for archival or for sending to
2256 * listeners when the full contents are not necessary.
2257 * @hide
2258 */
2259 public final void lightenPayload() {
2260 tickerView = null;
2261 contentView = null;
2262 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002263 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002264 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002265 if (extras != null && !extras.isEmpty()) {
2266 final Set<String> keyset = extras.keySet();
2267 final int N = keyset.size();
2268 final String[] keys = keyset.toArray(new String[N]);
2269 for (int i=0; i<N; i++) {
2270 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002271 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2272 continue;
2273 }
Dan Sandler50128532015-12-08 15:42:41 -05002274 final Object obj = extras.get(key);
2275 if (obj != null &&
2276 ( obj instanceof Parcelable
2277 || obj instanceof Parcelable[]
2278 || obj instanceof SparseArray
2279 || obj instanceof ArrayList)) {
2280 extras.remove(key);
2281 }
2282 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002283 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002284 }
2285
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002286 /**
2287 * Make sure this CharSequence is safe to put into a bundle, which basically
2288 * means it had better not be some custom Parcelable implementation.
2289 * @hide
2290 */
2291 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002292 if (cs == null) return cs;
2293 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2294 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2295 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002296 if (cs instanceof Parcelable) {
2297 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2298 + " instance is a custom Parcelable and not allowed in Notification");
2299 return cs.toString();
2300 }
Selim Cinek60a54252016-02-26 17:03:25 -08002301 return removeTextSizeSpans(cs);
2302 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002303
Selim Cinek60a54252016-02-26 17:03:25 -08002304 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2305 if (charSequence instanceof Spanned) {
2306 Spanned ss = (Spanned) charSequence;
2307 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2308 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2309 for (Object span : spans) {
2310 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002311 if (resultSpan instanceof CharacterStyle) {
2312 resultSpan = ((CharacterStyle) span).getUnderlying();
2313 }
2314 if (resultSpan instanceof TextAppearanceSpan) {
2315 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002316 resultSpan = new TextAppearanceSpan(
2317 originalSpan.getFamily(),
2318 originalSpan.getTextStyle(),
2319 -1,
2320 originalSpan.getTextColor(),
2321 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002322 } else if (resultSpan instanceof RelativeSizeSpan
2323 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002324 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002325 } else {
2326 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002327 }
2328 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2329 ss.getSpanFlags(span));
2330 }
2331 return builder;
2332 }
2333 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002334 }
2335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 public int describeContents() {
2337 return 0;
2338 }
2339
2340 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002341 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 */
Svet Ganovddb94882016-06-23 19:55:24 -07002343 public void writeToParcel(Parcel parcel, int flags) {
2344 // We need to mark all pending intents getting into the notification
2345 // system as being put there to later allow the notification ranker
2346 // to launch them and by doing so add the app to the battery saver white
2347 // list for a short period of time. The problem is that the system
2348 // cannot look into the extras as there may be parcelables there that
2349 // the platform does not know how to handle. To go around that we have
2350 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002351 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002352 if (collectPendingIntents) {
2353 PendingIntent.setOnMarshaledListener(
2354 (PendingIntent intent, Parcel out, int outFlags) -> {
2355 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002356 if (allPendingIntents == null) {
2357 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002358 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002359 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002360 }
2361 });
2362 }
2363 try {
2364 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002365 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002366 writeToParcelImpl(parcel, flags);
2367 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002368 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002369 } finally {
2370 if (collectPendingIntents) {
2371 PendingIntent.setOnMarshaledListener(null);
2372 }
2373 }
2374 }
2375
2376 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 parcel.writeInt(1);
2378
Adrian Roosfb921842017-10-26 14:49:56 +02002379 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002381 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002382 if (mSmallIcon == null && icon != 0) {
2383 // you snuck an icon in here without using the builder; let's try to keep it
2384 mSmallIcon = Icon.createWithResource("", icon);
2385 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002386 if (mSmallIcon != null) {
2387 parcel.writeInt(1);
2388 mSmallIcon.writeToParcel(parcel, 0);
2389 } else {
2390 parcel.writeInt(0);
2391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 parcel.writeInt(number);
2393 if (contentIntent != null) {
2394 parcel.writeInt(1);
2395 contentIntent.writeToParcel(parcel, 0);
2396 } else {
2397 parcel.writeInt(0);
2398 }
2399 if (deleteIntent != null) {
2400 parcel.writeInt(1);
2401 deleteIntent.writeToParcel(parcel, 0);
2402 } else {
2403 parcel.writeInt(0);
2404 }
2405 if (tickerText != null) {
2406 parcel.writeInt(1);
2407 TextUtils.writeToParcel(tickerText, parcel, flags);
2408 } else {
2409 parcel.writeInt(0);
2410 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002411 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002412 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002413 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002414 } else {
2415 parcel.writeInt(0);
2416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (contentView != null) {
2418 parcel.writeInt(1);
2419 contentView.writeToParcel(parcel, 0);
2420 } else {
2421 parcel.writeInt(0);
2422 }
Selim Cinek279fa862016-06-14 10:57:25 -07002423 if (mLargeIcon == null && largeIcon != null) {
2424 // you snuck an icon in here without using the builder; let's try to keep it
2425 mLargeIcon = Icon.createWithBitmap(largeIcon);
2426 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002427 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002428 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002429 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002430 } else {
2431 parcel.writeInt(0);
2432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
2434 parcel.writeInt(defaults);
2435 parcel.writeInt(this.flags);
2436
2437 if (sound != null) {
2438 parcel.writeInt(1);
2439 sound.writeToParcel(parcel, 0);
2440 } else {
2441 parcel.writeInt(0);
2442 }
2443 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002444
2445 if (audioAttributes != null) {
2446 parcel.writeInt(1);
2447 audioAttributes.writeToParcel(parcel, 0);
2448 } else {
2449 parcel.writeInt(0);
2450 }
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 parcel.writeLongArray(vibrate);
2453 parcel.writeInt(ledARGB);
2454 parcel.writeInt(ledOnMS);
2455 parcel.writeInt(ledOffMS);
2456 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002457
2458 if (fullScreenIntent != null) {
2459 parcel.writeInt(1);
2460 fullScreenIntent.writeToParcel(parcel, 0);
2461 } else {
2462 parcel.writeInt(0);
2463 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464
2465 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002466
John Spurlockfd7f1e02014-03-18 16:41:57 -04002467 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002468
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002469 parcel.writeString(mGroupKey);
2470
2471 parcel.writeString(mSortKey);
2472
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002473 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002474
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002475 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002476
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002477 if (bigContentView != null) {
2478 parcel.writeInt(1);
2479 bigContentView.writeToParcel(parcel, 0);
2480 } else {
2481 parcel.writeInt(0);
2482 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002483
Chris Wren8fd39ec2014-02-27 17:43:26 -05002484 if (headsUpContentView != null) {
2485 parcel.writeInt(1);
2486 headsUpContentView.writeToParcel(parcel, 0);
2487 } else {
2488 parcel.writeInt(0);
2489 }
2490
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002491 parcel.writeInt(visibility);
2492
2493 if (publicVersion != null) {
2494 parcel.writeInt(1);
2495 publicVersion.writeToParcel(parcel, 0);
2496 } else {
2497 parcel.writeInt(0);
2498 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002499
2500 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002501
2502 if (mChannelId != null) {
2503 parcel.writeInt(1);
2504 parcel.writeString(mChannelId);
2505 } else {
2506 parcel.writeInt(0);
2507 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002508 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002509
2510 if (mShortcutId != null) {
2511 parcel.writeInt(1);
2512 parcel.writeString(mShortcutId);
2513 } else {
2514 parcel.writeInt(0);
2515 }
2516
2517 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002518
2519 if (mSettingsText != null) {
2520 parcel.writeInt(1);
2521 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2522 } else {
2523 parcel.writeInt(0);
2524 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002525
2526 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
2528
2529 /**
2530 * Parcelable.Creator that instantiates Notification objects
2531 */
2532 public static final Parcelable.Creator<Notification> CREATOR
2533 = new Parcelable.Creator<Notification>()
2534 {
2535 public Notification createFromParcel(Parcel parcel)
2536 {
2537 return new Notification(parcel);
2538 }
2539
2540 public Notification[] newArray(int size)
2541 {
2542 return new Notification[size];
2543 }
2544 };
2545
2546 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002547 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2548 * <p>
2549 * For backwards compatibility {@code extras} holds some references to "real" member data such
2550 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2551 * fine as long as the object stays in one process.
2552 * <p>
2553 * However, once the notification goes into a parcel each reference gets marshalled separately,
2554 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2555 */
2556 private void fixDuplicateExtras() {
2557 if (extras != null) {
2558 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2559 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2560 }
2561 }
2562
2563 /**
2564 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2565 * separate object, replace it with the field's version to avoid holding duplicate copies.
2566 */
2567 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2568 if (original != null && extras.getParcelable(extraName) != null) {
2569 extras.putParcelable(extraName, original);
2570 }
2571 }
2572
2573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2575 * layout.
2576 *
2577 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2578 * in the view.</p>
2579 * @param context The context for your application / activity.
2580 * @param contentTitle The title that goes in the expanded entry.
2581 * @param contentText The text that goes in the expanded entry.
2582 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2583 * If this is an activity, it must include the
2584 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002585 * that you take care of task management as described in the
2586 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2587 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002588 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002590 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002592 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 public void setLatestEventInfo(Context context,
2594 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002595 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2596 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2597 new Throwable());
2598 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002599
Selim Cinek4ac6f602016-06-13 15:47:03 -07002600 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2601 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2602 }
2603
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002604 // ensure that any information already set directly is preserved
2605 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002606
2607 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002609 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
2611 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002612 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002614 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002615
2616 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
2618
Julia Reynoldsda303542015-11-23 14:00:20 -05002619 /**
2620 * @hide
2621 */
2622 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002623 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002624 }
2625
2626 /**
2627 * @hide
2628 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002629 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002630 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002631 }
2632
Yi Jin6b514142017-10-30 14:54:12 -07002633 /**
2634 * @hide
2635 */
2636 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2637 long token = proto.start(fieldId);
2638 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2639 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2640 proto.write(NotificationProto.FLAGS, this.flags);
2641 proto.write(NotificationProto.COLOR, this.color);
2642 proto.write(NotificationProto.CATEGORY, this.category);
2643 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2644 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2645 if (this.actions != null) {
2646 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2647 }
2648 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2649 proto.write(NotificationProto.VISIBILITY, this.visibility);
2650 }
2651 if (publicVersion != null) {
2652 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2653 }
2654 proto.end(token);
2655 }
2656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 @Override
2658 public String toString() {
2659 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002660 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002661 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002662 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002663 sb.append(priority);
2664 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002665 if (contentView != null) {
2666 sb.append(contentView.getPackage());
2667 sb.append("/0x");
2668 sb.append(Integer.toHexString(contentView.getLayoutId()));
2669 } else {
2670 sb.append("null");
2671 }
2672 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002673 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2674 sb.append("default");
2675 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 int N = this.vibrate.length-1;
2677 sb.append("[");
2678 for (int i=0; i<N; i++) {
2679 sb.append(this.vibrate[i]);
2680 sb.append(',');
2681 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002682 if (N != -1) {
2683 sb.append(this.vibrate[N]);
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 } else {
2687 sb.append("null");
2688 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002689 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002690 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002692 } else if (this.sound != null) {
2693 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 } else {
2695 sb.append("null");
2696 }
Chris Wren365b6d32015-07-16 10:39:26 -04002697 if (this.tickerText != null) {
2698 sb.append(" tick");
2699 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002700 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002702 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002703 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002704 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002705 if (this.category != null) {
2706 sb.append(" category=");
2707 sb.append(this.category);
2708 }
2709 if (this.mGroupKey != null) {
2710 sb.append(" groupKey=");
2711 sb.append(this.mGroupKey);
2712 }
2713 if (this.mSortKey != null) {
2714 sb.append(" sortKey=");
2715 sb.append(this.mSortKey);
2716 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002717 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002718 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002719 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002720 }
2721 sb.append(" vis=");
2722 sb.append(visibilityToString(this.visibility));
2723 if (this.publicVersion != null) {
2724 sb.append(" publicVersion=");
2725 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002726 }
2727 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 return sb.toString();
2729 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002730
Dan Sandler1b718782014-07-18 12:43:45 -04002731 /**
2732 * {@hide}
2733 */
2734 public static String visibilityToString(int vis) {
2735 switch (vis) {
2736 case VISIBILITY_PRIVATE:
2737 return "PRIVATE";
2738 case VISIBILITY_PUBLIC:
2739 return "PUBLIC";
2740 case VISIBILITY_SECRET:
2741 return "SECRET";
2742 default:
2743 return "UNKNOWN(" + String.valueOf(vis) + ")";
2744 }
2745 }
2746
Joe Onoratocb109a02011-01-18 17:57:41 -08002747 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002748 * {@hide}
2749 */
2750 public static String priorityToString(@Priority int pri) {
2751 switch (pri) {
2752 case PRIORITY_MIN:
2753 return "MIN";
2754 case PRIORITY_LOW:
2755 return "LOW";
2756 case PRIORITY_DEFAULT:
2757 return "DEFAULT";
2758 case PRIORITY_HIGH:
2759 return "HIGH";
2760 case PRIORITY_MAX:
2761 return "MAX";
2762 default:
2763 return "UNKNOWN(" + String.valueOf(pri) + ")";
2764 }
2765 }
2766
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002767 /**
2768 * @hide
2769 */
2770 public boolean hasCompletedProgress() {
2771 // not a progress notification; can't be complete
2772 if (!extras.containsKey(EXTRA_PROGRESS)
2773 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2774 return false;
2775 }
2776 // many apps use max 0 for 'indeterminate'; not complete
2777 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2778 return false;
2779 }
2780 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2781 }
2782
Jeff Sharkey000ce802017-04-29 13:13:27 -06002783 /** @removed */
2784 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002785 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002786 return mChannelId;
2787 }
2788
2789 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002790 * Returns the id of the channel this notification posts to.
2791 */
2792 public String getChannelId() {
2793 return mChannelId;
2794 }
2795
Jeff Sharkey000ce802017-04-29 13:13:27 -06002796 /** @removed */
2797 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002798 public long getTimeout() {
2799 return mTimeout;
2800 }
2801
2802 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002803 * Returns the duration from posting after which this notification should be canceled by the
2804 * system, if it's not canceled already.
2805 */
2806 public long getTimeoutAfter() {
2807 return mTimeout;
2808 }
2809
2810 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002811 * Returns what icon should be shown for this notification if it is being displayed in a
2812 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2813 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2814 */
2815 public int getBadgeIconType() {
2816 return mBadgeIcon;
2817 }
2818
2819 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002820 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002821 *
2822 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2823 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002824 */
2825 public String getShortcutId() {
2826 return mShortcutId;
2827 }
2828
Julia Reynolds3aedded2017-03-31 14:42:09 -04002829
2830 /**
2831 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2832 */
2833 public CharSequence getSettingsText() {
2834 return mSettingsText;
2835 }
2836
Julia Reynolds13d898c2017-02-02 12:22:05 -05002837 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002838 * Returns which type of notifications in a group are responsible for audibly alerting the
2839 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2840 * {@link #GROUP_ALERT_SUMMARY}.
2841 */
2842 public @GroupAlertBehavior int getGroupAlertBehavior() {
2843 return mGroupAlertBehavior;
2844 }
2845
2846 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002847 * The small icon representing this notification in the status bar and content view.
2848 *
2849 * @return the small icon representing this notification.
2850 *
2851 * @see Builder#getSmallIcon()
2852 * @see Builder#setSmallIcon(Icon)
2853 */
2854 public Icon getSmallIcon() {
2855 return mSmallIcon;
2856 }
2857
2858 /**
2859 * Used when notifying to clean up legacy small icons.
2860 * @hide
2861 */
2862 public void setSmallIcon(Icon icon) {
2863 mSmallIcon = icon;
2864 }
2865
2866 /**
2867 * The large icon shown in this notification's content view.
2868 * @see Builder#getLargeIcon()
2869 * @see Builder#setLargeIcon(Icon)
2870 */
2871 public Icon getLargeIcon() {
2872 return mLargeIcon;
2873 }
2874
2875 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002876 * @hide
2877 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002878 public boolean isGroupSummary() {
2879 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2880 }
2881
2882 /**
2883 * @hide
2884 */
2885 public boolean isGroupChild() {
2886 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2887 }
2888
2889 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002890 * @hide
2891 */
2892 public boolean suppressAlertingDueToGrouping() {
2893 if (isGroupSummary()
2894 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2895 return true;
2896 } else if (isGroupChild()
2897 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2898 return true;
2899 }
2900 return false;
2901 }
2902
2903 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002904 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002905 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002906 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002907 * content views using the platform's notification layout template. If your app supports
2908 * versions of Android as old as API level 4, you can instead use
2909 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2910 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2911 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002912 *
Scott Main183bf112012-08-13 19:12:13 -07002913 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002914 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002915 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002916 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002917 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2918 * .setContentText(subject)
2919 * .setSmallIcon(R.drawable.new_mail)
2920 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002921 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002922 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002923 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002924 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002925 /**
2926 * @hide
2927 */
2928 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2929 "android.rebuild.contentViewActionCount";
2930 /**
2931 * @hide
2932 */
2933 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2934 = "android.rebuild.bigViewActionCount";
2935 /**
2936 * @hide
2937 */
2938 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2939 = "android.rebuild.hudViewActionCount";
2940
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002941 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002942
Selim Cinek6743c0b2017-01-18 18:24:01 -08002943 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2944 SystemProperties.getBoolean("notifications.only_title", true);
2945
Selim Cinek389edcd2017-05-11 19:16:44 -07002946 /**
2947 * The lightness difference that has to be added to the primary text color to obtain the
2948 * secondary text color when the background is light.
2949 */
2950 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2951
2952 /**
2953 * The lightness difference that has to be added to the primary text color to obtain the
2954 * secondary text color when the background is dark.
2955 * A bit less then the above value, since it looks better on dark backgrounds.
2956 */
2957 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2958
Joe Onorato46439ce2010-11-19 13:56:21 -08002959 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002960 private Notification mN;
2961 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002962 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002963 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002964 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002966 private boolean mIsLegacy;
2967 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002968
2969 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002970 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2971 */
2972 private int mCachedContrastColor = COLOR_INVALID;
2973 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002974 /**
2975 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2976 */
2977 private int mCachedAmbientColor = COLOR_INVALID;
2978 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002979
2980 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002981 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2982 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2983 */
2984 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002985 private int mTextColorsAreForBackground = COLOR_INVALID;
2986 private int mPrimaryTextColor = COLOR_INVALID;
2987 private int mSecondaryTextColor = COLOR_INVALID;
2988 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002989 private int mBackgroundColor = COLOR_INVALID;
2990 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002991 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002992 /**
2993 * A temporary location where actions are stored. If != null the view originally has action
2994 * but doesn't have any for this inflation.
2995 */
2996 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002997 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002998
Anthony Chenad4d1582017-04-10 16:07:58 -07002999 private boolean mTintActionButtons;
3000 private boolean mInNightMode;
3001
Adrian Roos70d7aa32017-01-11 15:39:06 -08003002 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003003 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003004 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003005 * @param context
3006 * A {@link Context} that will be used by the Builder to construct the
3007 * RemoteViews. The Context will not be held past the lifetime of this Builder
3008 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003009 * @param channelId
3010 * The constructed Notification will be posted on this
3011 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3012 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003013 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003014 public Builder(Context context, String channelId) {
3015 this(context, (Notification) null);
3016 mN.mChannelId = channelId;
3017 }
3018
3019 /**
3020 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3021 * instead. All posted Notifications must specify a NotificationChannel Id.
3022 */
3023 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003024 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003025 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003026 }
3027
Joe Onoratocb109a02011-01-18 17:57:41 -08003028 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003029 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003030 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003031 public Builder(Context context, Notification toAdopt) {
3032 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003033 Resources res = mContext.getResources();
3034 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3035
3036 if (res.getBoolean(R.bool.config_enableNightMode)) {
3037 Configuration currentConfig = res.getConfiguration();
3038 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3039 == Configuration.UI_MODE_NIGHT_YES;
3040 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003041
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003042 if (toAdopt == null) {
3043 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003044 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3045 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3046 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003047 mN.priority = PRIORITY_DEFAULT;
3048 mN.visibility = VISIBILITY_PRIVATE;
3049 } else {
3050 mN = toAdopt;
3051 if (mN.actions != null) {
3052 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003053 }
3054
Selim Cineke7238dd2017-12-14 17:48:32 -08003055 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3056 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3057 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 }
3059
Selim Cinek4ac6f602016-06-13 15:47:03 -07003060 if (mN.getSmallIcon() == null && mN.icon != 0) {
3061 setSmallIcon(mN.icon);
3062 }
3063
3064 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3065 setLargeIcon(mN.largeIcon);
3066 }
3067
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003068 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3069 if (!TextUtils.isEmpty(templateClass)) {
3070 final Class<? extends Style> styleClass
3071 = getNotificationStyleClass(templateClass);
3072 if (styleClass == null) {
3073 Log.d(TAG, "Unknown style class: " + templateClass);
3074 } else {
3075 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003076 final Constructor<? extends Style> ctor =
3077 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 ctor.setAccessible(true);
3079 final Style style = ctor.newInstance();
3080 style.restoreFromExtras(mN.extras);
3081
3082 if (style != null) {
3083 setStyle(style);
3084 }
3085 } catch (Throwable t) {
3086 Log.e(TAG, "Could not create Style", t);
3087 }
3088 }
3089 }
3090
3091 }
3092 }
3093
3094 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003095 if (mColorUtil == null) {
3096 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003097 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003098 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003099 }
3100
3101 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003102 * If this notification is duplicative of a Launcher shortcut, sets the
3103 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3104 * the shortcut.
3105 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003106 * This field will be ignored by Launchers that don't support badging, don't show
3107 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003108 *
3109 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3110 * supersedes
3111 */
3112 public Builder setShortcutId(String shortcutId) {
3113 mN.mShortcutId = shortcutId;
3114 return this;
3115 }
3116
3117 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003118 * Sets which icon to display as a badge for this notification.
3119 *
3120 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3121 * {@link #BADGE_ICON_LARGE}.
3122 *
3123 * Note: This value might be ignored, for launchers that don't support badge icons.
3124 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003125 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003126 mN.mBadgeIcon = icon;
3127 return this;
3128 }
3129
3130 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003131 * Sets the group alert behavior for this notification. Use this method to mute this
3132 * notification if alerts for this notification's group should be handled by a different
3133 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003134 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3135 * mute. For example, if you want only the summary of your group to make noise, all
3136 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003137 *
3138 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3139 */
3140 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3141 mN.mGroupAlertBehavior = groupAlertBehavior;
3142 return this;
3143 }
3144
Jeff Sharkey000ce802017-04-29 13:13:27 -06003145 /** @removed */
3146 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003147 public Builder setChannel(String channelId) {
3148 mN.mChannelId = channelId;
3149 return this;
3150 }
3151
3152 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003153 * Specifies the channel the notification should be delivered on.
3154 */
3155 public Builder setChannelId(String channelId) {
3156 mN.mChannelId = channelId;
3157 return this;
3158 }
3159
Jeff Sharkey000ce802017-04-29 13:13:27 -06003160 /** @removed */
3161 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003162 public Builder setTimeout(long durationMs) {
3163 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003164 return this;
3165 }
3166
3167 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003168 * Specifies a duration in milliseconds after which this notification should be canceled,
3169 * if it is not already canceled.
3170 */
3171 public Builder setTimeoutAfter(long durationMs) {
3172 mN.mTimeout = durationMs;
3173 return this;
3174 }
3175
3176 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003177 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003178 *
3179 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3180 * shown anymore by default and must be opted into by using
3181 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003182 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003183 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003184 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003186 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003187 return this;
3188 }
3189
Joe Onoratocb109a02011-01-18 17:57:41 -08003190 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003191 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003192 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003193 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3194 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003195 */
3196 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003197 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003198 return this;
3199 }
3200
3201 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003202 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003203 *
3204 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003205 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003206 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003207 * Useful when showing an elapsed time (like an ongoing phone call).
3208 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003209 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003210 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003211 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003212 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003213 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003214 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003215 */
3216 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003217 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003218 return this;
3219 }
3220
3221 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003222 * Sets the Chronometer to count down instead of counting up.
3223 *
3224 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3225 * If it isn't set the chronometer will count up.
3226 *
3227 * @see #setUsesChronometer(boolean)
3228 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003229 public Builder setChronometerCountDown(boolean countDown) {
3230 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003231 return this;
3232 }
3233
3234 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 * Set the small icon resource, which will be used to represent the notification in the
3236 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003237 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003238
3239 * The platform template for the expanded view will draw this icon in the left, unless a
3240 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3241 * icon will be moved to the right-hand side.
3242 *
3243
3244 * @param icon
3245 * A resource ID in the application's package of the drawable to use.
3246 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003247 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003248 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003249 return setSmallIcon(icon != 0
3250 ? Icon.createWithResource(mContext, icon)
3251 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003252 }
3253
Joe Onoratocb109a02011-01-18 17:57:41 -08003254 /**
3255 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3256 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3257 * LevelListDrawable}.
3258 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003259 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003260 * @param level The level to use for the icon.
3261 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003262 * @see Notification#icon
3263 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003264 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003265 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003266 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003267 return setSmallIcon(icon);
3268 }
3269
3270 /**
3271 * Set the small icon, which will be used to represent the notification in the
3272 * status bar and content view (unless overriden there by a
3273 * {@link #setLargeIcon(Bitmap) large icon}).
3274 *
3275 * @param icon An Icon object to use.
3276 * @see Notification#icon
3277 */
3278 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003279 mN.setSmallIcon(icon);
3280 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3281 mN.icon = icon.getResId();
3282 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003283 return this;
3284 }
3285
Joe Onoratocb109a02011-01-18 17:57:41 -08003286 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003287 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 return this;
3292 }
3293
Joe Onoratocb109a02011-01-18 17:57:41 -08003294 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003295 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003296 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003297 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003298 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003299 return this;
3300 }
3301
Joe Onoratocb109a02011-01-18 17:57:41 -08003302 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003303 * This provides some additional information that is displayed in the notification. No
3304 * guarantees are given where exactly it is displayed.
3305 *
3306 * <p>This information should only be provided if it provides an essential
3307 * benefit to the understanding of the notification. The more text you provide the
3308 * less readable it becomes. For example, an email client should only provide the account
3309 * name here if more than one email account has been added.</p>
3310 *
3311 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3312 * notification header area.
3313 *
3314 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3315 * this will be shown in the third line of text in the platform notification template.
3316 * You should not be using {@link #setProgress(int, int, boolean)} at the
3317 * same time on those versions; they occupy the same place.
3318 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003319 */
3320 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003321 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003322 return this;
3323 }
3324
3325 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003326 * Provides text that will appear as a link to your application's settings.
3327 *
3328 * <p>This text does not appear within notification {@link Style templates} but may
3329 * appear when the user uses an affordance to learn more about the notification.
3330 * Additionally, this text will not appear unless you provide a valid link target by
3331 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3332 *
3333 * <p>This text is meant to be concise description about what the user can customize
3334 * when they click on this link. The recommended maximum length is 40 characters.
3335 * @param text
3336 * @return
3337 */
3338 public Builder setSettingsText(CharSequence text) {
3339 mN.mSettingsText = safeCharSequence(text);
3340 return this;
3341 }
3342
3343 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003344 * Set the remote input history.
3345 *
3346 * This should be set to the most recent inputs that have been sent
3347 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3348 * longer relevant (e.g. for chat notifications once the other party has responded).
3349 *
3350 * The most recent input must be stored at the 0 index, the second most recent at the
3351 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3352 * and how much of each individual input is shown.
3353 *
3354 * <p>Note: The reply text will only be shown on notifications that have least one action
3355 * with a {@code RemoteInput}.</p>
3356 */
3357 public Builder setRemoteInputHistory(CharSequence[] text) {
3358 if (text == null) {
3359 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3360 } else {
3361 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3362 CharSequence[] safe = new CharSequence[N];
3363 for (int i = 0; i < N; i++) {
3364 safe[i] = safeCharSequence(text[i]);
3365 }
3366 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3367 }
3368 return this;
3369 }
3370
3371 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003372 * Sets the number of items this notification represents. May be displayed as a badge count
3373 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003374 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003375 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003376 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003377 return this;
3378 }
3379
Joe Onoratocb109a02011-01-18 17:57:41 -08003380 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003381 * A small piece of additional information pertaining to this notification.
3382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003383 * The platform template will draw this on the last line of the notification, at the far
3384 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003385 *
3386 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3387 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3388 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003389 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003390 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003391 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003392 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003393 return this;
3394 }
3395
Joe Onoratocb109a02011-01-18 17:57:41 -08003396 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003397 * Set the progress this notification represents.
3398 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003399 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003400 */
3401 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003402 mN.extras.putInt(EXTRA_PROGRESS, progress);
3403 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3404 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003405 return this;
3406 }
3407
3408 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003409 * Supply a custom RemoteViews to use instead of the platform template.
3410 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003411 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003412 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003413 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003414 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 return setCustomContentView(views);
3416 }
3417
3418 /**
3419 * Supply custom RemoteViews to use instead of the platform template.
3420 *
3421 * This will override the layout that would otherwise be constructed by this Builder
3422 * object.
3423 */
3424 public Builder setCustomContentView(RemoteViews contentView) {
3425 mN.contentView = contentView;
3426 return this;
3427 }
3428
3429 /**
3430 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3431 *
3432 * This will override the expanded layout that would otherwise be constructed by this
3433 * Builder object.
3434 */
3435 public Builder setCustomBigContentView(RemoteViews contentView) {
3436 mN.bigContentView = contentView;
3437 return this;
3438 }
3439
3440 /**
3441 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3442 *
3443 * This will override the heads-up layout that would otherwise be constructed by this
3444 * Builder object.
3445 */
3446 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3447 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003448 return this;
3449 }
3450
Joe Onoratocb109a02011-01-18 17:57:41 -08003451 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003452 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3453 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003454 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3455 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3456 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003457 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003458 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003459 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003460 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003462 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003464 return this;
3465 }
3466
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003468 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3469 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003470 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
3478 * An intent to launch instead of posting the notification to the status bar.
3479 * Only for use with extremely high-priority notifications demanding the user's
3480 * <strong>immediate</strong> attention, such as an incoming phone call or
3481 * alarm clock that the user has explicitly set to a particular time.
3482 * If this facility is used for something else, please give the user an option
3483 * to turn it off and use a normal notification, as this can be extremely
3484 * disruptive.
3485 *
Chris Wren47c20a12014-06-18 17:27:29 -04003486 * <p>
3487 * The system UI may choose to display a heads-up notification, instead of
3488 * launching this intent, while the user is using the device.
3489 * </p>
3490 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003491 * @param intent The pending intent to launch.
3492 * @param highPriority Passing true will cause this notification to be sent
3493 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003494 *
3495 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003496 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003497 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003498 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003499 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3500 return this;
3501 }
3502
Joe Onoratocb109a02011-01-18 17:57:41 -08003503 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003504 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003506 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003507 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003508 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003509 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003510 return this;
3511 }
3512
Joe Onoratocb109a02011-01-18 17:57:41 -08003513 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003514 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003515 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003516 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003517 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003518 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 setTicker(tickerText);
3520 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003521 return this;
3522 }
3523
Joe Onoratocb109a02011-01-18 17:57:41 -08003524 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003525 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003526 *
3527 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003528 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3529 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003530 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003531 public Builder setLargeIcon(Bitmap b) {
3532 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3533 }
3534
3535 /**
3536 * Add a large icon to the notification content view.
3537 *
3538 * In the platform template, this image will be shown on the left of the notification view
3539 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3540 * badge atop the large icon).
3541 */
3542 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543 mN.mLargeIcon = icon;
3544 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003545 return this;
3546 }
3547
Joe Onoratocb109a02011-01-18 17:57:41 -08003548 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003549 * Set the sound to play.
3550 *
John Spurlockc0650f022014-07-19 13:22:39 -04003551 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3552 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003553 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003554 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003555 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003556 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003557 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003558 mN.sound = sound;
3559 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003560 return this;
3561 }
3562
Joe Onoratocb109a02011-01-18 17:57:41 -08003563 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003564 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003565 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003566 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3567 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003568 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003569 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003570 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003571 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003572 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003573 mN.sound = sound;
3574 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003575 return this;
3576 }
3577
Joe Onoratocb109a02011-01-18 17:57:41 -08003578 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003579 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3580 * use during playback.
3581 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003582 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003583 * @see Notification#sound
3584 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003585 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003586 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 mN.sound = sound;
3588 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003589 return this;
3590 }
3591
3592 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003593 * Set the vibration pattern to use.
3594 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003595 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3596 * <code>pattern</code> parameter.
3597 *
Chris Wren47c20a12014-06-18 17:27:29 -04003598 * <p>
3599 * A notification that vibrates is more likely to be presented as a heads-up notification.
3600 * </p>
3601 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003602 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003603 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003604 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003605 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003606 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003607 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003608 return this;
3609 }
3610
Joe Onoratocb109a02011-01-18 17:57:41 -08003611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003612 * Set the desired color for the indicator LED on the device, as well as the
3613 * blink duty cycle (specified in milliseconds).
3614 *
3615
3616 * Not all devices will honor all (or even any) of these values.
3617 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003618 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003619 * @see Notification#ledARGB
3620 * @see Notification#ledOnMS
3621 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003622 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003623 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003624 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003625 mN.ledARGB = argb;
3626 mN.ledOnMS = onMs;
3627 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003628 if (onMs != 0 || offMs != 0) {
3629 mN.flags |= FLAG_SHOW_LIGHTS;
3630 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003631 return this;
3632 }
3633
Joe Onoratocb109a02011-01-18 17:57:41 -08003634 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003635 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003636 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003637
3638 * Ongoing notifications cannot be dismissed by the user, so your application or service
3639 * must take care of canceling them.
3640 *
3641
3642 * They are typically used to indicate a background task that the user is actively engaged
3643 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3644 * (e.g., a file download, sync operation, active network connection).
3645 *
3646
3647 * @see Notification#FLAG_ONGOING_EVENT
3648 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003649 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003650 public Builder setOngoing(boolean ongoing) {
3651 setFlag(FLAG_ONGOING_EVENT, ongoing);
3652 return this;
3653 }
3654
Joe Onoratocb109a02011-01-18 17:57:41 -08003655 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003656 * Set whether this notification should be colorized. When set, the color set with
3657 * {@link #setColor(int)} will be used as the background color of this notification.
3658 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003659 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3660 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003661 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003662 * For most styles, the coloring will only be applied if the notification is for a
3663 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003664 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003665 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003666 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003667 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003668 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003669 */
3670 public Builder setColorized(boolean colorize) {
3671 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3672 return this;
3673 }
3674
3675 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003676 * Set this flag if you would only like the sound, vibrate
3677 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003678 *
3679 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003680 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003681 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3682 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3683 return this;
3684 }
3685
Joe Onoratocb109a02011-01-18 17:57:41 -08003686 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003687 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003688 *
3689 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003690 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003691 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003692 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003693 return this;
3694 }
3695
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003697 * Set whether or not this notification should not bridge to other devices.
3698 *
3699 * <p>Some notifications can be bridged to other devices for remote display.
3700 * This hint can be set to recommend this notification not be bridged.
3701 */
3702 public Builder setLocalOnly(boolean localOnly) {
3703 setFlag(FLAG_LOCAL_ONLY, localOnly);
3704 return this;
3705 }
3706
3707 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003708 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003709 * <p>
3710 * The value should be one or more of the following fields combined with
3711 * bitwise-or:
3712 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3713 * <p>
3714 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003715 *
3716 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003717 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003718 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003719 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003720 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003721 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003722 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003723 return this;
3724 }
3725
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003726 /**
3727 * Set the priority of this notification.
3728 *
3729 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003730 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003731 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003732 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003733 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003734 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003735 return this;
3736 }
Joe Malin8d40d042012-11-05 11:36:40 -08003737
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003738 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003739 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003740 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003741 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003743 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003745 return this;
3746 }
3747
3748 /**
Chris Wrendde75302014-03-26 17:24:15 -04003749 * Add a person that is relevant to this notification.
3750 *
Chris Wrene6c48932014-09-29 17:19:27 -04003751 * <P>
3752 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003753 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3754 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3755 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003756 * </P>
3757 *
3758 * <P>
3759 * The person should be specified by the {@code String} representation of a
3760 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3761 * </P>
3762 *
3763 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3764 * URIs. The path part of these URIs must exist in the contacts database, in the
3765 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3766 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003767 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3768 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003769 * </P>
3770 *
3771 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003772 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003773 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003774 */
Chris Wrene6c48932014-09-29 17:19:27 -04003775 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003776 addPerson(new Person().setUri(uri));
3777 return this;
3778 }
3779
3780 /**
3781 * Add a person that is relevant to this notification.
3782 *
3783 * <P>
3784 * Depending on user preferences, this annotation may allow the notification to pass
3785 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3786 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3787 * appear more prominently in the user interface.
3788 * </P>
3789 *
3790 * <P>
3791 * A person should usually contain a uri in order to benefit from the ranking boost.
3792 * However, even if no uri is provided, it's beneficial to provide other people in the
3793 * notification, such that listeners and voice only devices can announce and handle them
3794 * properly.
3795 * </P>
3796 *
3797 * @param person the person to add.
3798 * @see Notification#EXTRA_PEOPLE_LIST
3799 */
3800 public Builder addPerson(Person person) {
3801 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003802 return this;
3803 }
3804
3805 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003806 * Set this notification to be part of a group of notifications sharing the same key.
3807 * Grouped notifications may display in a cluster or stack on devices which
3808 * support such rendering.
3809 *
3810 * <p>To make this notification the summary for its group, also call
3811 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3812 * {@link #setSortKey}.
3813 * @param groupKey The group key of the group.
3814 * @return this object for method chaining
3815 */
3816 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003817 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003818 return this;
3819 }
3820
3821 /**
3822 * Set this notification to be the group summary for a group of notifications.
3823 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003824 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3825 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003826 * @param isGroupSummary Whether this notification should be a group summary.
3827 * @return this object for method chaining
3828 */
3829 public Builder setGroupSummary(boolean isGroupSummary) {
3830 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3831 return this;
3832 }
3833
3834 /**
3835 * Set a sort key that orders this notification among other notifications from the
3836 * same package. This can be useful if an external sort was already applied and an app
3837 * would like to preserve this. Notifications will be sorted lexicographically using this
3838 * value, although providing different priorities in addition to providing sort key may
3839 * cause this value to be ignored.
3840 *
3841 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003842 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003843 *
3844 * @see String#compareTo(String)
3845 */
3846 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003847 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003848 return this;
3849 }
3850
3851 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003852 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003853 *
Griff Hazen720042b2014-02-24 15:46:56 -08003854 * <p>Values within the Bundle will replace existing extras values in this Builder.
3855 *
3856 * @see Notification#extras
3857 */
Griff Hazen959591e2014-05-15 22:26:18 -07003858 public Builder addExtras(Bundle extras) {
3859 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003860 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003861 }
3862 return this;
3863 }
3864
3865 /**
3866 * Set metadata for this notification.
3867 *
3868 * <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 -04003869 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003870 * called.
3871 *
Griff Hazen720042b2014-02-24 15:46:56 -08003872 * <p>Replaces any existing extras values with those from the provided Bundle.
3873 * Use {@link #addExtras} to merge in metadata instead.
3874 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003875 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003876 */
Griff Hazen959591e2014-05-15 22:26:18 -07003877 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003878 if (extras != null) {
3879 mUserExtras = extras;
3880 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003881 return this;
3882 }
3883
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003884 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003885 * Get the current metadata Bundle used by this notification Builder.
3886 *
3887 * <p>The returned Bundle is shared with this Builder.
3888 *
3889 * <p>The current contents of this Bundle are copied into the Notification each time
3890 * {@link #build()} is called.
3891 *
3892 * @see Notification#extras
3893 */
3894 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003895 return mUserExtras;
3896 }
3897
3898 private Bundle getAllExtras() {
3899 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3900 saveExtras.putAll(mN.extras);
3901 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003902 }
3903
3904 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003905 * Add an action to this notification. Actions are typically displayed by
3906 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003907 * <p>
3908 * Every action must have an icon (32dp square and matching the
3909 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3910 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3911 * <p>
3912 * A notification in its expanded form can display up to 3 actions, from left to right in
3913 * the order they were added. Actions will not be displayed when the notification is
3914 * collapsed, however, so be sure that any essential functions may be accessed by the user
3915 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003916 *
3917 * @param icon Resource ID of a drawable that represents the action.
3918 * @param title Text describing the action.
3919 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003920 *
3921 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003922 */
Dan Sandler86647982015-05-13 23:41:13 -04003923 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003924 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003925 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003926 return this;
3927 }
3928
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003929 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003930 * Add an action to this notification. Actions are typically displayed by
3931 * the system as a button adjacent to the notification content.
3932 * <p>
3933 * Every action must have an icon (32dp square and matching the
3934 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3935 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3936 * <p>
3937 * A notification in its expanded form can display up to 3 actions, from left to right in
3938 * the order they were added. Actions will not be displayed when the notification is
3939 * collapsed, however, so be sure that any essential functions may be accessed by the user
3940 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3941 *
3942 * @param action The action to add.
3943 */
3944 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003945 if (action != null) {
3946 mActions.add(action);
3947 }
Griff Hazen959591e2014-05-15 22:26:18 -07003948 return this;
3949 }
3950
3951 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003952 * Alter the complete list of actions attached to this notification.
3953 * @see #addAction(Action).
3954 *
3955 * @param actions
3956 * @return
3957 */
3958 public Builder setActions(Action... actions) {
3959 mActions.clear();
3960 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003961 if (actions[i] != null) {
3962 mActions.add(actions[i]);
3963 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003964 }
3965 return this;
3966 }
3967
3968 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003969 * Add a rich notification style to be applied at build time.
3970 *
3971 * @param style Object responsible for modifying the notification style.
3972 */
3973 public Builder setStyle(Style style) {
3974 if (mStyle != style) {
3975 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003976 if (mStyle != null) {
3977 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3979 } else {
3980 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003981 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003982 }
3983 return this;
3984 }
3985
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003986 /**
3987 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003988 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003989 * @return The same Builder.
3990 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003991 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003992 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003993 return this;
3994 }
3995
3996 /**
3997 * Supply a replacement Notification whose contents should be shown in insecure contexts
3998 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3999 * @param n A replacement notification, presumably with some or all info redacted.
4000 * @return The same Builder.
4001 */
4002 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 if (n != null) {
4004 mN.publicVersion = new Notification();
4005 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4006 } else {
4007 mN.publicVersion = null;
4008 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004009 return this;
4010 }
4011
Griff Hazenb720abe2014-05-20 13:15:30 -07004012 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004013 * Apply an extender to this notification builder. Extenders may be used to add
4014 * metadata or change options on this builder.
4015 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004016 public Builder extend(Extender extender) {
4017 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004018 return this;
4019 }
4020
Dan Sandler4e787062015-06-17 15:09:48 -04004021 /**
4022 * @hide
4023 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004024 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004025 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004026 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004027 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004028 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004029 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004030 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004031 }
4032
Dan Sandler26e81cf2014-05-06 10:01:27 -04004033 /**
4034 * Sets {@link Notification#color}.
4035 *
4036 * @param argb The accent color to use
4037 *
4038 * @return The same Builder.
4039 */
Tor Norbye80756e32015-03-02 09:39:27 -08004040 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004042 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004043 return this;
4044 }
4045
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004046 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004047 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4048 // This user can never be a badged profile,
4049 // and also includes USER_ALL system notifications.
4050 return null;
4051 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004052 // Note: This assumes that the current user can read the profile badge of the
4053 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004054 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004055 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004056 }
4057
4058 private Bitmap getProfileBadge() {
4059 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004060 if (badge == null) {
4061 return null;
4062 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004063 final int size = mContext.getResources().getDimensionPixelSize(
4064 R.dimen.notification_badge_size);
4065 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004066 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004067 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004068 badge.draw(canvas);
4069 return bitmap;
4070 }
4071
Selim Cinekc848c3a2016-01-13 15:27:30 -08004072 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004073 Bitmap profileBadge = getProfileBadge();
4074
Kenny Guy98193ea2014-07-24 19:54:37 +01004075 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004076 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4077 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004078 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004079 contentView.setDrawableTint(R.id.profile_badge, false,
4080 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004081 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004082 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004083 }
4084
Christoph Studerfe718432014-09-01 18:21:18 +02004085 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004086 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004087 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004088 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004089 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004090 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004091 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004092 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004093 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004094 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004095 }
4096
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004097 /**
4098 * Resets the notification header to its original state
4099 */
4100 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004101 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4102 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004103 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004104 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004105 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004106 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004107 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004108 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4109 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004110 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004111 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004112 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004113 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004114 contentView.setImageViewIcon(R.id.profile_badge, null);
4115 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004116 }
4117
4118 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004119 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4120 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004121 }
4122
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004123 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004124 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004125 }
4126
4127 /**
4128 * @param hasProgress whether the progress bar should be shown and set
4129 */
4130 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004131 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4132 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004133 }
4134
Adrian Roos70d7aa32017-01-11 15:39:06 -08004135 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004136 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004137
Christoph Studerfe718432014-09-01 18:21:18 +02004138 resetStandardTemplate(contentView);
4139
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004140 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004141 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004142 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004143 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004144 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4145 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004146 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004147 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004148 if (!p.ambient) {
4149 setTextViewColorPrimary(contentView, R.id.title);
4150 }
Selim Cinek954cc232016-05-20 13:29:23 -07004151 contentView.setViewLayoutWidth(R.id.title, showProgress
4152 ? ViewGroup.LayoutParams.WRAP_CONTENT
4153 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004154 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004155 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004156 int textId = showProgress ? com.android.internal.R.id.text_line_1
4157 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004158 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004159 if (!p.ambient) {
4160 setTextViewColorSecondary(contentView, textId);
4161 }
Selim Cinek41598732016-01-11 16:58:37 -08004162 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004163 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004164
Selim Cinek279fa862016-06-14 10:57:25 -07004165 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004166
Selim Cinek29603462015-11-17 19:04:39 -08004167 return contentView;
4168 }
4169
Selim Cinek48f66b72017-08-18 16:17:51 -07004170 private CharSequence processTextSpans(CharSequence text) {
4171 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004172 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004173 }
4174 return text;
4175 }
4176
Selim Cinek7b9605b2017-01-19 17:36:00 -08004177 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4178 ensureColors();
4179 contentView.setTextColor(id, mPrimaryTextColor);
4180 }
4181
Selim Cinek48f66b72017-08-18 16:17:51 -07004182 private boolean hasForegroundColor() {
4183 return mForegroundColor != COLOR_INVALID;
4184 }
4185
Selim Cinek389edcd2017-05-11 19:16:44 -07004186 /**
4187 * @return the primary text color
4188 * @hide
4189 */
4190 @VisibleForTesting
4191 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004192 ensureColors();
4193 return mPrimaryTextColor;
4194 }
4195
Selim Cinek389edcd2017-05-11 19:16:44 -07004196 /**
4197 * @return the secondary text color
4198 * @hide
4199 */
4200 @VisibleForTesting
4201 public int getSecondaryTextColor() {
4202 ensureColors();
4203 return mSecondaryTextColor;
4204 }
4205
Selim Cinek7b9605b2017-01-19 17:36:00 -08004206 private int getActionBarColor() {
4207 ensureColors();
4208 return mActionBarColor;
4209 }
4210
Selim Cinek622c64a2017-04-17 17:10:05 -07004211 private int getActionBarColorDeEmphasized() {
4212 int backgroundColor = getBackgroundColor();
4213 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4214 }
4215
Selim Cinek7b9605b2017-01-19 17:36:00 -08004216 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4217 ensureColors();
4218 contentView.setTextColor(id, mSecondaryTextColor);
4219 }
4220
4221 private void ensureColors() {
4222 int backgroundColor = getBackgroundColor();
4223 if (mPrimaryTextColor == COLOR_INVALID
4224 || mSecondaryTextColor == COLOR_INVALID
4225 || mActionBarColor == COLOR_INVALID
4226 || mTextColorsAreForBackground != backgroundColor) {
4227 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004228 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004229 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4230 backgroundColor);
4231 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4232 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004233 if (backgroundColor != COLOR_DEFAULT
4234 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4235 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4236 mPrimaryTextColor, backgroundColor, 4.5);
4237 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4238 mSecondaryTextColor, backgroundColor, 4.5);
4239 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004240 } else {
4241 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4242 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4243 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4244 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004245 // We only respect the given colors if worst case Black or White still has
4246 // contrast
4247 boolean backgroundLight = backLum > textLum
4248 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4249 || backLum <= textLum
4250 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004251 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004252 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004253 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4254 mForegroundColor,
4255 backgroundColor,
4256 true /* findFG */,
4257 4.5f);
4258 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004259 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004260 } else {
4261 mSecondaryTextColor =
4262 NotificationColorUtil.findContrastColorAgainstDark(
4263 mForegroundColor,
4264 backgroundColor,
4265 true /* findFG */,
4266 4.5f);
4267 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004268 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004269 }
4270 } else {
4271 mPrimaryTextColor = mForegroundColor;
4272 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004273 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4274 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004275 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4276 backgroundColor) < 4.5f) {
4277 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004278 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004279 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4280 mSecondaryTextColor,
4281 backgroundColor,
4282 true /* findFG */,
4283 4.5f);
4284 } else {
4285 mSecondaryTextColor
4286 = NotificationColorUtil.findContrastColorAgainstDark(
4287 mSecondaryTextColor,
4288 backgroundColor,
4289 true /* findFG */,
4290 4.5f);
4291 }
4292 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004293 mSecondaryTextColor, backgroundLight
4294 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4295 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004296 }
4297 }
4298 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004299 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4300 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004301 }
4302 }
4303
4304 private void updateBackgroundColor(RemoteViews contentView) {
4305 if (isColorized()) {
4306 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4307 getBackgroundColor());
4308 } else {
4309 // Clear it!
4310 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4311 0);
4312 }
4313 }
4314
Selim Cinek860b6da2015-12-16 19:02:19 -08004315 /**
4316 * @param remoteView the remote view to update the minheight in
4317 * @param hasMinHeight does it have a mimHeight
4318 * @hide
4319 */
4320 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4321 int minHeight = 0;
4322 if (hasMinHeight) {
4323 // we need to set the minHeight of the notification
4324 minHeight = mContext.getResources().getDimensionPixelSize(
4325 com.android.internal.R.dimen.notification_min_content_height);
4326 }
4327 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4328 }
4329
Selim Cinek29603462015-11-17 19:04:39 -08004330 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004331 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4332 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4333 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4334 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004335 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004336 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004337 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004338 contentView.setProgressBackgroundTintList(
4339 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4340 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004341 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004342 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004343 contentView.setProgressTintList(R.id.progress, colorStateList);
4344 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004345 }
Selim Cinek29603462015-11-17 19:04:39 -08004346 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004347 } else {
4348 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004349 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004350 }
Joe Onorato561d3852010-11-20 18:09:34 -08004351 }
4352
Selim Cinek88188f22017-09-19 16:46:56 -07004353 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4354 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004355 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4356 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4357 }
Selim Cinek88188f22017-09-19 16:46:56 -07004358 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4359 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004360 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4361 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4362 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004363 int endMargin = R.dimen.notification_content_picture_margin;
4364 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4365 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4366 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004367 }
4368 // Bind the reply action
4369 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004370
Selim Cinek88188f22017-09-19 16:46:56 -07004371 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4372 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4373 if (actionVisible) {
4374 // We're only showing the icon as big if we're hiding the large icon
4375 int contrastColor = resolveContrastColor();
4376 int iconColor;
4377 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004378 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004379 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004380 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004381 contentView.setOnClickPendingIntent(R.id.right_icon,
4382 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004383 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004384 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4385 ? Color.BLACK : Color.WHITE;
4386 } else {
4387 contentView.setImageViewResource(R.id.right_icon,
4388 R.drawable.ic_reply_notification_large);
4389 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4390 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004391 }
Selim Cinek88188f22017-09-19 16:46:56 -07004392 contentView.setDrawableTint(replyId,
4393 false /* targetBackground */,
4394 iconColor,
4395 PorterDuff.Mode.SRC_ATOP);
4396 contentView.setOnClickPendingIntent(replyId,
4397 action.actionIntent);
4398 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4399 } else {
4400 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004401 }
Selim Cinek88188f22017-09-19 16:46:56 -07004402 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4403 ? View.VISIBLE
4404 : View.GONE);
4405 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004406 ? View.VISIBLE
4407 : View.GONE);
4408 }
4409
4410 private Action findReplyAction() {
4411 ArrayList<Action> actions = mActions;
4412 if (mOriginalActions != null) {
4413 actions = mOriginalActions;
4414 }
4415 int numActions = actions.size();
4416 for (int i = 0; i < numActions; i++) {
4417 Action action = actions.get(i);
4418 if (hasValidRemoteInput(action)) {
4419 return action;
4420 }
4421 }
4422 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004423 }
4424
Selim Cinekafeed292017-12-12 17:32:44 -08004425 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4426 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004427 bindSmallIcon(contentView, ambient);
4428 bindHeaderAppName(contentView, ambient);
4429 if (!ambient) {
4430 // Ambient view does not have these
4431 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004432 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004433 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004434 bindProfileBadge(contentView);
4435 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004436 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004437 }
4438
4439 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004440 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004441 contentView.setDrawableTint(R.id.expand_button, false, color,
4442 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004443 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004444 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004445 }
4446
Selim Cinek99104832017-01-25 14:47:33 -08004447 /**
4448 * @return the color that is used as the first primary highlight color. This is applied
4449 * in several places like the action buttons or the app name in the header.
4450 */
4451 private int getPrimaryHighlightColor() {
4452 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4453 }
4454
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004455 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4456 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004457 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004458 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004459 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4460 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4461 contentView.setLong(R.id.chronometer, "setBase",
4462 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4463 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004464 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004465 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004466 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004467 } else {
4468 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4469 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004470 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004471 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004472 } else {
4473 // We still want a time to be set but gone, such that we can show and hide it
4474 // on demand in case it's a child notification without anything in the header
4475 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004476 }
4477 }
4478
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004479 private void bindHeaderText(RemoteViews contentView) {
4480 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4481 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004482 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004483 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004484 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004485 if (headerText == null
4486 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4487 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4488 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4489 }
4490 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004491 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004492 contentView.setTextViewText(R.id.header_text, processTextSpans(
4493 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004494 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004495 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4496 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004497 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004498 }
4499 }
4500
Selim Cinekafeed292017-12-12 17:32:44 -08004501 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4502 if (!TextUtils.isEmpty(secondaryText)) {
4503 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4504 processLegacyText(secondaryText)));
4505 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4506 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4507 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4508 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4509 }
4510 }
4511
Adrian Rooseba05822016-04-22 17:09:27 -07004512 /**
4513 * @hide
4514 */
4515 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004516 CharSequence name = null;
4517 final PackageManager pm = mContext.getPackageManager();
4518 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4519 // only system packages which lump together a bunch of unrelated stuff
4520 // may substitute a different name to make the purpose of the
4521 // notification more clear. the correct package label should always
4522 // be accessible via SystemUI.
4523 final String pkg = mContext.getPackageName();
4524 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4525 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4526 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4527 name = subName;
4528 } else {
4529 Log.w(TAG, "warning: pkg "
4530 + pkg + " attempting to substitute app name '" + subName
4531 + "' without holding perm "
4532 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4533 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004534 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004535 if (TextUtils.isEmpty(name)) {
4536 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4537 }
4538 if (TextUtils.isEmpty(name)) {
4539 // still nothing?
4540 return null;
4541 }
4542
4543 return String.valueOf(name);
4544 }
Adrian Roos487374f2017-01-11 15:48:14 -08004545 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004546 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004547 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004548 setTextViewColorPrimary(contentView, R.id.app_name_text);
4549 } else {
4550 contentView.setTextColor(R.id.app_name_text,
4551 ambient ? resolveAmbientColor() : resolveContrastColor());
4552 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004553 }
4554
Adrian Roos487374f2017-01-11 15:48:14 -08004555 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004556 if (mN.mSmallIcon == null && mN.icon != 0) {
4557 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4558 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004559 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004560 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004561 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004562 }
4563
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004564 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004565 * @return true if the built notification will show the time or the chronometer; false
4566 * otherwise
4567 */
4568 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004569 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004570 }
4571
Christoph Studerfe718432014-09-01 18:21:18 +02004572 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004573 // actions_container is only reset when there are no actions to avoid focus issues with
4574 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004575 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004576 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004577
4578 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4579 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4580
4581 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4582 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4583 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4584 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004585
4586 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004587 }
4588
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004589 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004590 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004591 }
4592
Adrian Roos70d7aa32017-01-11 15:39:06 -08004593 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4594 StandardTemplateParams p) {
4595 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004596
Christoph Studerfe718432014-09-01 18:21:18 +02004597 resetStandardTemplateWithActions(big);
4598
Adrian Roose458aa82015-12-08 16:17:19 -08004599 boolean validRemoteInput = false;
4600
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004601 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004602 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004603 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004604 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004605 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004606 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004607 if (p.ambient) {
4608 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004609 } else if (isColorized()) {
4610 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4611 } else {
4612 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4613 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004614 }
Adrian Roosf852a422016-06-03 13:33:43 -07004615 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4616 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004617 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004618 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004619 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004620 boolean actionHasValidInput = hasValidRemoteInput(action);
4621 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004622
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004623 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004624 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004625 if (actionHasValidInput) {
4626 // Clear the drawable
4627 button.setInt(R.id.action0, "setBackgroundResource", 0);
4628 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004629 big.addView(R.id.actions, button);
4630 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004631 } else {
4632 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004633 }
Adrian Roose458aa82015-12-08 16:17:19 -08004634
4635 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004636 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004637 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4638 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004639 big.setTextViewText(R.id.notification_material_reply_text_1,
4640 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004641 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004642
4643 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4644 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004645 big.setTextViewText(R.id.notification_material_reply_text_2,
4646 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004647 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004648
4649 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4650 big.setViewVisibility(
4651 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004652 big.setTextViewText(R.id.notification_material_reply_text_3,
4653 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004654 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004655 }
4656 }
4657 }
4658
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004659 return big;
4660 }
4661
Adrian Roose458aa82015-12-08 16:17:19 -08004662 private boolean hasValidRemoteInput(Action action) {
4663 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4664 // Weird actions
4665 return false;
4666 }
4667
4668 RemoteInput[] remoteInputs = action.getRemoteInputs();
4669 if (remoteInputs == null) {
4670 return false;
4671 }
4672
4673 for (RemoteInput r : remoteInputs) {
4674 CharSequence[] choices = r.getChoices();
4675 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4676 return true;
4677 }
4678 }
4679 return false;
4680 }
4681
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004682 /**
4683 * Construct a RemoteViews for the final 1U notification layout. In order:
4684 * 1. Custom contentView from the caller
4685 * 2. Style's proposed content view
4686 * 3. Standard template view
4687 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004688 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004689 return createContentView(false /* increasedheight */ );
4690 }
4691
4692 /**
4693 * Construct a RemoteViews for the smaller content view.
4694 *
4695 * @param increasedHeight true if this layout be created with an increased height. Some
4696 * styles may support showing more then just that basic 1U size
4697 * and the system may decide to render important notifications
4698 * slightly bigger even when collapsed.
4699 *
4700 * @hide
4701 */
4702 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004703 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004704 return mN.contentView;
4705 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004706 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004707 if (styleView != null) {
4708 return styleView;
4709 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004710 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004711 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004712 }
4713
Selim Cineka7679b62017-05-10 16:33:25 -07004714 private boolean useExistingRemoteView() {
4715 return mStyle == null || (!mStyle.displayCustomViewInline()
4716 && !mRebuildStyledRemoteViews);
4717 }
4718
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004719 /**
4720 * Construct a RemoteViews for the final big notification layout.
4721 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004722 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004723 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004724 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004725 return mN.bigContentView;
4726 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004727 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004728 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004729 } else if (mActions.size() != 0) {
4730 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004731 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004732 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004733 return result;
4734 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004735
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004736 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004737 * Construct a RemoteViews for the final notification header only. This will not be
4738 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004739 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004740 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004741 * @hide
4742 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004743 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004744 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4745 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004746 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004747 ambient ? R.layout.notification_template_ambient_header
4748 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004749 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004750 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004751 if (colorized != null) {
4752 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4753 } else {
4754 mN.extras.remove(EXTRA_COLORIZED);
4755 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004756 return header;
4757 }
4758
Adrian Roos487374f2017-01-11 15:48:14 -08004759 /**
4760 * Construct a RemoteViews for the ambient version of the notification.
4761 *
4762 * @hide
4763 */
4764 public RemoteViews makeAmbientNotification() {
4765 RemoteViews ambient = applyStandardTemplateWithActions(
4766 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004767 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004768 return ambient;
4769 }
4770
Selim Cinek29603462015-11-17 19:04:39 -08004771 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004772 if (result != null) {
4773 result.setViewVisibility(R.id.text_line_1, View.GONE);
4774 }
Selim Cinek29603462015-11-17 19:04:39 -08004775 }
4776
Selim Cinek6743c0b2017-01-18 18:24:01 -08004777 /**
4778 * Adapt the Notification header if this view is used as an expanded view.
4779 *
4780 * @hide
4781 */
4782 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004783 if (result != null) {
4784 result.setBoolean(R.id.notification_header, "setExpanded", true);
4785 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004786 }
4787
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004788 /**
4789 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004790 *
4791 * @param increasedHeight true if this layout be created with an increased height. Some
4792 * styles may support showing more then just that basic 1U size
4793 * and the system may decide to render important notifications
4794 * slightly bigger even when collapsed.
4795 *
4796 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004797 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004798 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004799 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004800 return mN.headsUpContentView;
4801 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004802 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4803 if (styleView != null) {
4804 return styleView;
4805 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004806 } else if (mActions.size() == 0) {
4807 return null;
4808 }
4809
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004810 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004811 }
4812
Selim Cinek624c02db2015-12-14 21:00:02 -08004813 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004814 * Construct a RemoteViews for the final heads-up notification layout.
4815 */
4816 public RemoteViews createHeadsUpContentView() {
4817 return createHeadsUpContentView(false /* useIncreasedHeight */);
4818 }
4819
4820 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004821 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4822 *
4823 * @hide
4824 */
4825 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004826 return makePublicView(false /* ambient */);
4827 }
4828
4829 /**
4830 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4831 *
4832 * @hide
4833 */
4834 public RemoteViews makePublicAmbientNotification() {
4835 return makePublicView(true /* ambient */);
4836 }
4837
4838 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004839 if (mN.publicVersion != null) {
4840 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004841 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004842 }
4843 Bundle savedBundle = mN.extras;
4844 Style style = mStyle;
4845 mStyle = null;
4846 Icon largeIcon = mN.mLargeIcon;
4847 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004848 Bitmap largeIconLegacy = mN.largeIcon;
4849 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004850 ArrayList<Action> actions = mActions;
4851 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004852 Bundle publicExtras = new Bundle();
4853 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4854 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4855 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4856 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004857 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4858 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004859 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004860 RemoteViews view;
4861 if (ambient) {
4862 publicExtras.putCharSequence(EXTRA_TITLE,
4863 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4864 view = makeAmbientNotification();
4865 } else{
4866 view = makeNotificationHeader(false /* ambient */);
4867 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4868 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004869 mN.extras = savedBundle;
4870 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004871 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004872 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004873 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004874 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004875 }
4876
Selim Cinek6743c0b2017-01-18 18:24:01 -08004877 /**
4878 * Construct a content view for the display when low - priority
4879 *
4880 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4881 * a new subtext is created consisting of the content of the
4882 * notification.
4883 * @hide
4884 */
4885 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4886 int color = mN.color;
4887 mN.color = COLOR_DEFAULT;
4888 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4889 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4890 CharSequence newSummary = createSummaryText();
4891 if (!TextUtils.isEmpty(newSummary)) {
4892 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4893 }
4894 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004895
Adrian Roos6f6e1592017-05-02 16:22:53 -07004896 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004897 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004898 if (summary != null) {
4899 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4900 } else {
4901 mN.extras.remove(EXTRA_SUB_TEXT);
4902 }
4903 mN.color = color;
4904 return header;
4905 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004906
Selim Cinek6743c0b2017-01-18 18:24:01 -08004907 private CharSequence createSummaryText() {
4908 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4909 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4910 return titleText;
4911 }
4912 SpannableStringBuilder summary = new SpannableStringBuilder();
4913 if (titleText == null) {
4914 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4915 }
4916 BidiFormatter bidi = BidiFormatter.getInstance();
4917 if (titleText != null) {
4918 summary.append(bidi.unicodeWrap(titleText));
4919 }
4920 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4921 if (titleText != null && contentText != null) {
4922 summary.append(bidi.unicodeWrap(mContext.getText(
4923 R.string.notification_header_divider_symbol_with_spaces)));
4924 }
4925 if (contentText != null) {
4926 summary.append(bidi.unicodeWrap(contentText));
4927 }
4928 return summary;
4929 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004930
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004931 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004932 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004933 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004934 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004935 emphazisedMode ? getEmphasizedActionLayoutResource()
4936 : tombstone ? getActionTombstoneLayoutResource()
4937 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004938 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004939 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004940 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004941 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004942 if (action.mRemoteInputs != null) {
4943 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4944 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004945 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004946 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004947 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004948 int bgColor;
4949 if (isColorized()) {
4950 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4951 } else {
4952 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4953 : R.color.notification_action_list_dark);
4954 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004955 button.setDrawableTint(R.id.button_holder, true,
4956 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004957 CharSequence title = action.title;
4958 ColorStateList[] outResultColor = null;
4959 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004960 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004961 } else {
4962 outResultColor = new ColorStateList[1];
4963 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4964 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004965 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004966 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004967 if (outResultColor != null && outResultColor[0] != null) {
4968 // We need to set the text color as well since changing a text to uppercase
4969 // clears its spans.
4970 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004971 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004972 button.setTextColor(R.id.action0,resolveContrastColor());
4973 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004974 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004975 button.setTextViewText(R.id.action0, processTextSpans(
4976 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004977 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004978 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004979 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004980 button.setTextColor(R.id.action0,
4981 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004982 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004983 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004984 return button;
4985 }
4986
Joe Onoratocb109a02011-01-18 17:57:41 -08004987 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004988 * Ensures contrast on color spans against a background color. also returns the color of the
4989 * text if a span was found that spans over the whole text.
4990 *
4991 * @param charSequence the charSequence on which the spans are
4992 * @param background the background color to ensure the contrast against
4993 * @param outResultColor an array in which a color will be returned as the first element if
4994 * there exists a full length color span.
4995 * @return the contrasted charSequence
4996 */
4997 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4998 ColorStateList[] outResultColor) {
4999 if (charSequence instanceof Spanned) {
5000 Spanned ss = (Spanned) charSequence;
5001 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5002 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5003 for (Object span : spans) {
5004 Object resultSpan = span;
5005 int spanStart = ss.getSpanStart(span);
5006 int spanEnd = ss.getSpanEnd(span);
5007 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5008 if (resultSpan instanceof CharacterStyle) {
5009 resultSpan = ((CharacterStyle) span).getUnderlying();
5010 }
5011 if (resultSpan instanceof TextAppearanceSpan) {
5012 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5013 ColorStateList textColor = originalSpan.getTextColor();
5014 if (textColor != null) {
5015 int[] colors = textColor.getColors();
5016 int[] newColors = new int[colors.length];
5017 for (int i = 0; i < newColors.length; i++) {
5018 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005019 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005020 }
5021 textColor = new ColorStateList(textColor.getStates().clone(),
5022 newColors);
5023 resultSpan = new TextAppearanceSpan(
5024 originalSpan.getFamily(),
5025 originalSpan.getTextStyle(),
5026 originalSpan.getTextSize(),
5027 textColor,
5028 originalSpan.getLinkTextColor());
5029 if (fullLength) {
5030 outResultColor[0] = new ColorStateList(
5031 textColor.getStates().clone(), newColors);
5032 }
5033 }
5034 } else if (resultSpan instanceof ForegroundColorSpan) {
5035 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5036 int foregroundColor = originalSpan.getForegroundColor();
5037 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005038 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005039 resultSpan = new ForegroundColorSpan(foregroundColor);
5040 if (fullLength) {
5041 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5042 }
5043 } else {
5044 resultSpan = span;
5045 }
5046
5047 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5048 }
5049 return builder;
5050 }
5051 return charSequence;
5052 }
5053
5054 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005055 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005056 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005057 */
5058 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005059 if (!mIsLegacyInitialized) {
5060 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5061 < Build.VERSION_CODES.LOLLIPOP;
5062 mIsLegacyInitialized = true;
5063 }
5064 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005065 }
5066
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005067 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005068 return processLegacyText(charSequence, false /* ambient */);
5069 }
5070
5071 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5072 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5073 boolean wantLightText = ambient;
5074 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005075 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005076 } else {
5077 return charSequence;
5078 }
5079 }
5080
Dan Sandler26e81cf2014-05-06 10:01:27 -04005081 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005082 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005083 */
Adrian Roos487374f2017-01-11 15:48:14 -08005084 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5085 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005086 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005087 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005088 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005089 contentView.setDrawableTint(R.id.icon, false, color,
5090 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005091
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005092 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005093 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005094 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005095 }
5096
Dan Sandler26e81cf2014-05-06 10:01:27 -04005097 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005098 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005099 * if it's grayscale).
5100 */
5101 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005102 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5103 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005104 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005105 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005106 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5107 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005108 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005109 }
5110
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005111 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005112 if (mN.color != COLOR_DEFAULT) {
5113 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005114 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005115 }
5116
Adrian Roos4ff3b122016-02-01 12:26:13 -08005117 int resolveContrastColor() {
5118 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5119 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005120 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005121
Selim Cinekac5f0272017-05-02 16:05:41 -07005122 int color;
5123 int background = mBackgroundColorHint;
5124 if (mBackgroundColorHint == COLOR_INVALID) {
5125 background = mContext.getColor(
5126 com.android.internal.R.color.notification_material_background_color);
5127 }
5128 if (mN.color == COLOR_DEFAULT) {
5129 ensureColors();
5130 color = mSecondaryTextColor;
5131 } else {
5132 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005133 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005134 }
5135 if (Color.alpha(color) < 255) {
5136 // alpha doesn't go well for color filters, so let's blend it manually
5137 color = NotificationColorUtil.compositeColors(color, background);
5138 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005139 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005140 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005141 }
5142
Adrian Roos487374f2017-01-11 15:48:14 -08005143 int resolveAmbientColor() {
5144 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5145 return mCachedAmbientColor;
5146 }
5147 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5148
5149 mCachedAmbientColorIsFor = mN.color;
5150 return mCachedAmbientColor = contrasted;
5151 }
5152
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005153 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005154 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005155 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005156 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005157 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005158 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005159 mN.actions = new Action[mActions.size()];
5160 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005161 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005162 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005163 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005164 }
Selim Cinek247fa012016-02-18 09:50:48 -08005165 if (mN.bigContentView != null || mN.contentView != null
5166 || mN.headsUpContentView != null) {
5167 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5168 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005169 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005170 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005171
Julia Reynolds3b848122016-02-26 10:45:32 -05005172 /**
5173 * Creates a Builder from an existing notification so further changes can be made.
5174 * @param context The context for your application / activity.
5175 * @param n The notification to create a Builder from.
5176 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005177 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005178 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005179 ApplicationInfo applicationInfo = n.extras.getParcelable(
5180 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005181 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005182 if (applicationInfo != null) {
5183 try {
5184 builderContext = context.createApplicationContext(applicationInfo,
5185 Context.CONTEXT_RESTRICTED);
5186 } catch (NameNotFoundException e) {
5187 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5188 builderContext = context; // try with our context
5189 }
5190 } else {
5191 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005192 }
5193
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005194 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005195 }
5196
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005197 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005198 * @deprecated Use {@link #build()} instead.
5199 */
5200 @Deprecated
5201 public Notification getNotification() {
5202 return build();
5203 }
5204
5205 /**
5206 * Combine all of the options that have been set and return a new {@link Notification}
5207 * object.
5208 */
5209 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005210 // first, add any extras from the calling code
5211 if (mUserExtras != null) {
5212 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005213 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005214
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005215 mN.creationTime = System.currentTimeMillis();
5216
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005217 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005218 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005219
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005220 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005221
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005222 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005223 mStyle.reduceImageSizes(mContext);
5224 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005225 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005226 }
Selim Cinekd0426622017-07-11 13:19:59 +02005227 mN.reduceImageSizes(mContext);
5228
Adrian Roos5081c0d2016-02-26 16:04:19 -08005229 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005230 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005231 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005232 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005233 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5234 mN.contentView.getSequenceNumber());
5235 }
5236 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005237 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005238 if (mN.bigContentView != null) {
5239 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5240 mN.bigContentView.getSequenceNumber());
5241 }
5242 }
5243 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005244 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005245 if (mN.headsUpContentView != null) {
5246 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5247 mN.headsUpContentView.getSequenceNumber());
5248 }
5249 }
5250 }
5251
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005252 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5253 mN.flags |= FLAG_SHOW_LIGHTS;
5254 }
5255
Adrian Roosfb921842017-10-26 14:49:56 +02005256 mN.allPendingIntents = null;
5257
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005258 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005259 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005260
5261 /**
5262 * Apply this Builder to an existing {@link Notification} object.
5263 *
5264 * @hide
5265 */
5266 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005267 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005268 return n;
5269 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005270
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005271 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005272 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005273 * change. Also removes extenders on low ram devices, as
5274 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005275 *
5276 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5277 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005278 * @hide
5279 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005280 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005281 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005282
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005283 // Only strip views for known Styles because we won't know how to
5284 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005285 if (!isLowRam
5286 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005287 && getNotificationStyleClass(templateClass) == null) {
5288 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005289 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005290
5291 // Only strip unmodified BuilderRemoteViews.
5292 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005293 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005294 n.contentView.getSequenceNumber();
5295 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005296 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005297 n.bigContentView.getSequenceNumber();
5298 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005299 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005300 n.headsUpContentView.getSequenceNumber();
5301
5302 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005303 if (!isLowRam
5304 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005305 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005306 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005307
5308 Notification clone = n.clone();
5309 if (stripContentView) {
5310 clone.contentView = null;
5311 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5312 }
5313 if (stripBigContentView) {
5314 clone.bigContentView = null;
5315 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5316 }
5317 if (stripHeadsUpContentView) {
5318 clone.headsUpContentView = null;
5319 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5320 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005321 if (isLowRam) {
5322 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5323 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5324 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5325 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005326 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005327 }
5328
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005329 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005330 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005331 }
5332
5333 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005334 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005335 }
5336
5337 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005338 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005339 }
5340
5341 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005342 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005343 }
5344
5345 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005346 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005347 }
5348
Adrian Roosc1a80b02016-04-05 14:54:55 -07005349 private int getMessagingLayoutResource() {
5350 return R.layout.notification_template_material_messaging;
5351 }
5352
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005353 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005354 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005355 }
5356
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005357 private int getEmphasizedActionLayoutResource() {
5358 return R.layout.notification_material_action_emphasized;
5359 }
5360
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005361 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005362 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005363 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005364
5365 private int getBackgroundColor() {
5366 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005367 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005368 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005369 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5370 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005371 }
5372 }
5373
5374 private boolean isColorized() {
5375 return mN.isColorized();
5376 }
Selim Cinek99104832017-01-25 14:47:33 -08005377
Anthony Chenad4d1582017-04-10 16:07:58 -07005378 private boolean shouldTintActionButtons() {
5379 return mTintActionButtons;
5380 }
5381
Selim Cinek99104832017-01-25 14:47:33 -08005382 private boolean textColorsNeedInversion() {
5383 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5384 return false;
5385 }
5386 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5387 return targetSdkVersion > Build.VERSION_CODES.M
5388 && targetSdkVersion < Build.VERSION_CODES.O;
5389 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005390
5391 /**
5392 * Set a color palette to be used as the background and textColors
5393 *
5394 * @param backgroundColor the color to be used as the background
5395 * @param foregroundColor the color to be used as the foreground
5396 *
5397 * @hide
5398 */
5399 public void setColorPalette(int backgroundColor, int foregroundColor) {
5400 mBackgroundColor = backgroundColor;
5401 mForegroundColor = foregroundColor;
5402 mTextColorsAreForBackground = COLOR_INVALID;
5403 ensureColors();
5404 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005405
5406 /**
5407 * Sets the background color for this notification to be a different one then the default.
5408 * This is mainly used to calculate contrast and won't necessarily be applied to the
5409 * background.
5410 *
5411 * @hide
5412 */
5413 public void setBackgroundColorHint(int backgroundColor) {
5414 mBackgroundColorHint = backgroundColor;
5415 }
Selim Cineka7679b62017-05-10 16:33:25 -07005416
5417
5418 /**
5419 * Forces all styled remoteViews to be built from scratch and not use any cached
5420 * RemoteViews.
5421 * This is needed for legacy apps that are baking in their remoteviews into the
5422 * notification.
5423 *
5424 * @hide
5425 */
5426 public void setRebuildStyledRemoteViews(boolean rebuild) {
5427 mRebuildStyledRemoteViews = rebuild;
5428 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005429 }
5430
5431 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005432 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5433 * remote views.
5434 *
5435 * @hide
5436 */
5437 void reduceImageSizes(Context context) {
5438 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5439 return;
5440 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005441 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005442 if (mLargeIcon != null || largeIcon != null) {
5443 Resources resources = context.getResources();
5444 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005445 int maxWidth = resources.getDimensionPixelSize(isLowRam
5446 ? R.dimen.notification_right_icon_size_low_ram
5447 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005448 int maxHeight = maxWidth;
5449 if (MediaStyle.class.equals(style)
5450 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005451 maxHeight = resources.getDimensionPixelSize(isLowRam
5452 ? R.dimen.notification_media_image_max_height_low_ram
5453 : R.dimen.notification_media_image_max_height);
5454 maxWidth = resources.getDimensionPixelSize(isLowRam
5455 ? R.dimen.notification_media_image_max_width_low_ram
5456 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005457 }
5458 if (mLargeIcon != null) {
5459 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5460 }
5461 if (largeIcon != null) {
5462 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5463 }
5464 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005465 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5466 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5467 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005468 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5469 }
5470
Selim Cineka8cb1262017-08-15 16:53:44 -07005471 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5472 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005473 if (remoteView != null) {
5474 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005475 int maxWidth = resources.getDimensionPixelSize(isLowRam
5476 ? R.dimen.notification_custom_view_max_image_width_low_ram
5477 : R.dimen.notification_custom_view_max_image_width);
5478 int maxHeight = resources.getDimensionPixelSize(isLowRam
5479 ? R.dimen.notification_custom_view_max_image_height_low_ram
5480 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005481 remoteView.reduceImageSizes(maxWidth, maxHeight);
5482 }
5483 }
5484
5485 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005486 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005487 */
Selim Cinek22714f12017-04-13 16:23:53 -07005488 private boolean isForegroundService() {
5489 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005490 }
5491
5492 /**
Selim Cinek99104832017-01-25 14:47:33 -08005493 * @return whether this notification has a media session attached
5494 * @hide
5495 */
5496 public boolean hasMediaSession() {
5497 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5498 }
5499
5500 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005501 * @return the style class of this notification
5502 * @hide
5503 */
5504 public Class<? extends Notification.Style> getNotificationStyle() {
5505 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5506
5507 if (!TextUtils.isEmpty(templateClass)) {
5508 return Notification.getNotificationStyleClass(templateClass);
5509 }
5510 return null;
5511 }
5512
5513 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005514 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005515 *
5516 * @hide
5517 */
5518 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005519 if (isColorizedMedia()) {
5520 return true;
5521 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005522 return extras.getBoolean(EXTRA_COLORIZED)
5523 && (hasColorizedPermission() || isForegroundService());
5524 }
5525
5526 /**
5527 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5528 * permission. The permission is checked when a notification is enqueued.
5529 */
5530 private boolean hasColorizedPermission() {
5531 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005532 }
5533
5534 /**
5535 * @return true if this notification is colorized and it is a media notification
5536 *
5537 * @hide
5538 */
5539 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005540 Class<? extends Style> style = getNotificationStyle();
5541 if (MediaStyle.class.equals(style)) {
5542 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5543 if ((colorized == null || colorized) && hasMediaSession()) {
5544 return true;
5545 }
5546 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5547 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5548 return true;
5549 }
5550 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005551 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005552 }
5553
Selim Cinek0847acd2017-04-24 19:48:29 -07005554
5555 /**
5556 * @return true if this is a media notification
5557 *
5558 * @hide
5559 */
5560 public boolean isMediaNotification() {
5561 Class<? extends Style> style = getNotificationStyle();
5562 if (MediaStyle.class.equals(style)) {
5563 return true;
5564 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5565 return true;
5566 }
5567 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005568 }
5569
Selim Cinek279fa862016-06-14 10:57:25 -07005570 private boolean hasLargeIcon() {
5571 return mLargeIcon != null || largeIcon != null;
5572 }
5573
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005574 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005575 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005576 * @hide
5577 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005578 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005579 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5580 }
5581
5582 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005583 * @return true if the notification will show a chronometer; false otherwise
5584 * @hide
5585 */
5586 public boolean showsChronometer() {
5587 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5588 }
5589
5590 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005591 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005592 */
5593 @SystemApi
5594 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5595 Class<? extends Style>[] classes = new Class[] {
5596 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5597 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5598 MessagingStyle.class };
5599 for (Class<? extends Style> innerClass : classes) {
5600 if (templateClass.equals(innerClass.getName())) {
5601 return innerClass;
5602 }
5603 }
5604 return null;
5605 }
5606
5607 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005608 * An object that can apply a rich notification style to a {@link Notification.Builder}
5609 * object.
5610 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005611 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005612 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005613
5614 /**
5615 * @hide
5616 */
5617 protected CharSequence mSummaryText = null;
5618
5619 /**
5620 * @hide
5621 */
5622 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005623
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005624 protected Builder mBuilder;
5625
Chris Wrend6297db2012-05-03 16:20:13 -04005626 /**
5627 * Overrides ContentTitle in the big form of the template.
5628 * This defaults to the value passed to setContentTitle().
5629 */
5630 protected void internalSetBigContentTitle(CharSequence title) {
5631 mBigContentTitle = title;
5632 }
5633
5634 /**
5635 * Set the first line of text after the detail section in the big form of the template.
5636 */
5637 protected void internalSetSummaryText(CharSequence cs) {
5638 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005639 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005640 }
5641
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005642 public void setBuilder(Builder builder) {
5643 if (mBuilder != builder) {
5644 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005645 if (mBuilder != null) {
5646 mBuilder.setStyle(this);
5647 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005648 }
5649 }
5650
Chris Wrend6297db2012-05-03 16:20:13 -04005651 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005652 if (mBuilder == null) {
5653 throw new IllegalArgumentException("Style requires a valid Builder object");
5654 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005655 }
Chris Wrend6297db2012-05-03 16:20:13 -04005656
5657 protected RemoteViews getStandardView(int layoutId) {
5658 checkBuilder();
5659
Christoph Studer4600f9b2014-07-22 22:44:43 +02005660 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005661 CharSequence oldBuilderContentTitle =
5662 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005663 if (mBigContentTitle != null) {
5664 mBuilder.setContentTitle(mBigContentTitle);
5665 }
5666
Chris Wrend6297db2012-05-03 16:20:13 -04005667 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5668
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005669 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005670
Chris Wrend6297db2012-05-03 16:20:13 -04005671 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5672 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005673 } else {
5674 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005675 }
5676
Chris Wrend6297db2012-05-03 16:20:13 -04005677 return contentView;
5678 }
5679
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005680 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005681 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005682 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005683 *
5684 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005685 * @hide
5686 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005687 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005688 return null;
5689 }
5690
5691 /**
5692 * Construct a Style-specific RemoteViews for the final big notification layout.
5693 * @hide
5694 */
5695 public RemoteViews makeBigContentView() {
5696 return null;
5697 }
5698
5699 /**
5700 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005701 *
5702 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005703 * @hide
5704 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005705 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005706 return null;
5707 }
5708
5709 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005710 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005711 * @hide
5712 */
5713 public void addExtras(Bundle extras) {
5714 if (mSummaryTextSet) {
5715 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5716 }
5717 if (mBigContentTitle != null) {
5718 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5719 }
Chris Wren91ad5632013-06-05 15:05:57 -04005720 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005721 }
5722
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005723 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005724 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005725 * @hide
5726 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005727 protected void restoreFromExtras(Bundle extras) {
5728 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5729 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5730 mSummaryTextSet = true;
5731 }
5732 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5733 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5734 }
5735 }
5736
5737
5738 /**
5739 * @hide
5740 */
5741 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005742 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005743 return wip;
5744 }
5745
Daniel Sandler0ec46202015-06-24 01:27:05 -04005746 /**
5747 * @hide
5748 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005749 public void purgeResources() {}
5750
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005751 /**
5752 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5753 * attached to.
5754 *
5755 * @return the fully constructed Notification.
5756 */
5757 public Notification build() {
5758 checkBuilder();
5759 return mBuilder.build();
5760 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005761
5762 /**
5763 * @hide
5764 * @return true if the style positions the progress bar on the second line; false if the
5765 * style hides the progress bar
5766 */
5767 protected boolean hasProgress() {
5768 return true;
5769 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005770
5771 /**
5772 * @hide
5773 * @return Whether we should put the summary be put into the notification header
5774 */
5775 public boolean hasSummaryInHeader() {
5776 return true;
5777 }
Selim Cinek593610c2016-02-16 18:42:57 -08005778
5779 /**
5780 * @hide
5781 * @return Whether custom content views are displayed inline in the style
5782 */
5783 public boolean displayCustomViewInline() {
5784 return false;
5785 }
Selim Cinekd0426622017-07-11 13:19:59 +02005786
5787 /**
5788 * Reduces the image sizes contained in this style.
5789 *
5790 * @hide
5791 */
5792 public void reduceImageSizes(Context context) {
5793 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005794 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005795
5796 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005797 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005798 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005799 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005800 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005801 * Notification notif = new Notification.Builder(mContext)
5802 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5803 * .setContentText(subject)
5804 * .setSmallIcon(R.drawable.new_post)
5805 * .setLargeIcon(aBitmap)
5806 * .setStyle(new Notification.BigPictureStyle()
5807 * .bigPicture(aBigBitmap))
5808 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005809 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005810 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005811 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005812 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005813 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005814 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005815 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005816 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005817
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005818 public BigPictureStyle() {
5819 }
5820
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005821 /**
5822 * @deprecated use {@code BigPictureStyle()}.
5823 */
5824 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005825 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005826 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005827 }
5828
Chris Wrend6297db2012-05-03 16:20:13 -04005829 /**
5830 * Overrides ContentTitle in the big form of the template.
5831 * This defaults to the value passed to setContentTitle().
5832 */
5833 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005834 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005835 return this;
5836 }
5837
5838 /**
5839 * Set the first line of text after the detail section in the big form of the template.
5840 */
5841 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005842 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005843 return this;
5844 }
5845
Chris Wren0bd664d2012-08-01 13:56:56 -04005846 /**
5847 * Provide the bitmap to be used as the payload for the BigPicture notification.
5848 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005849 public BigPictureStyle bigPicture(Bitmap b) {
5850 mPicture = b;
5851 return this;
5852 }
5853
Chris Wren3745a3d2012-05-22 15:11:52 -04005854 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005855 * Override the large icon when the big notification is shown.
5856 */
5857 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005858 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5859 }
5860
5861 /**
5862 * Override the large icon when the big notification is shown.
5863 */
5864 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005865 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005866 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005867 return this;
5868 }
5869
Riley Andrews0394a0c2015-11-03 23:36:52 -08005870 /** @hide */
5871 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5872
Daniel Sandler0ec46202015-06-24 01:27:05 -04005873 /**
5874 * @hide
5875 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005876 @Override
5877 public void purgeResources() {
5878 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005879 if (mPicture != null &&
5880 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005881 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005882 mPicture = mPicture.createAshmemBitmap();
5883 }
5884 if (mBigLargeIcon != null) {
5885 mBigLargeIcon.convertToAshmem();
5886 }
5887 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005888
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005889 /**
5890 * @hide
5891 */
Selim Cinekd0426622017-07-11 13:19:59 +02005892 @Override
5893 public void reduceImageSizes(Context context) {
5894 super.reduceImageSizes(context);
5895 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005896 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005897 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005898 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5899 ? R.dimen.notification_big_picture_max_height_low_ram
5900 : R.dimen.notification_big_picture_max_height);
5901 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5902 ? R.dimen.notification_big_picture_max_width_low_ram
5903 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005904 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5905 }
5906 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005907 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5908 ? R.dimen.notification_right_icon_size_low_ram
5909 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005910 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5911 }
5912 }
5913
5914 /**
5915 * @hide
5916 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005917 public RemoteViews makeBigContentView() {
5918 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005919 // This covers the following cases:
5920 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005921 // mN.mLargeIcon
5922 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005923 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005924 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005925 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005926 oldLargeIcon = mBuilder.mN.mLargeIcon;
5927 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005928 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5929 // replacement if the other one is null. Because we're restoring these legacy icons
5930 // for old listeners, this is in general non-null.
5931 largeIconLegacy = mBuilder.mN.largeIcon;
5932 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005933 }
5934
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005935 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005936 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005937 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5938 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005939 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005940 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005941 }
Selim Cinek279fa862016-06-14 10:57:25 -07005942 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005943
Christoph Studer5c510ee2014-12-15 16:32:27 +01005944 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005945 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005946 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005947 }
5948
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005949 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005950 return contentView;
5951 }
5952
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005953 /**
5954 * @hide
5955 */
5956 public void addExtras(Bundle extras) {
5957 super.addExtras(extras);
5958
5959 if (mBigLargeIconSet) {
5960 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5961 }
5962 extras.putParcelable(EXTRA_PICTURE, mPicture);
5963 }
5964
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005965 /**
5966 * @hide
5967 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005968 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005969 protected void restoreFromExtras(Bundle extras) {
5970 super.restoreFromExtras(extras);
5971
5972 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005973 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005974 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005975 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005976 mPicture = extras.getParcelable(EXTRA_PICTURE);
5977 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005978
5979 /**
5980 * @hide
5981 */
5982 @Override
5983 public boolean hasSummaryInHeader() {
5984 return false;
5985 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005986 }
5987
5988 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005989 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005990 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005991 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005992 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005993 * Notification notif = new Notification.Builder(mContext)
5994 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5995 * .setContentText(subject)
5996 * .setSmallIcon(R.drawable.new_mail)
5997 * .setLargeIcon(aBitmap)
5998 * .setStyle(new Notification.BigTextStyle()
5999 * .bigText(aVeryLongString))
6000 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006001 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006002 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006003 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006004 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006005 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006006
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006007 private CharSequence mBigText;
6008
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006009 public BigTextStyle() {
6010 }
6011
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006012 /**
6013 * @deprecated use {@code BigTextStyle()}.
6014 */
6015 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006016 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006017 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006018 }
6019
Chris Wrend6297db2012-05-03 16:20:13 -04006020 /**
6021 * Overrides ContentTitle in the big form of the template.
6022 * This defaults to the value passed to setContentTitle().
6023 */
6024 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006025 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006026 return this;
6027 }
6028
6029 /**
6030 * Set the first line of text after the detail section in the big form of the template.
6031 */
6032 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006033 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006034 return this;
6035 }
6036
Chris Wren0bd664d2012-08-01 13:56:56 -04006037 /**
6038 * Provide the longer text to be displayed in the big form of the
6039 * template in place of the content text.
6040 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006041 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006042 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006043 return this;
6044 }
6045
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006046 /**
6047 * @hide
6048 */
6049 public void addExtras(Bundle extras) {
6050 super.addExtras(extras);
6051
Christoph Studer4600f9b2014-07-22 22:44:43 +02006052 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6053 }
6054
6055 /**
6056 * @hide
6057 */
6058 @Override
6059 protected void restoreFromExtras(Bundle extras) {
6060 super.restoreFromExtras(extras);
6061
6062 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006063 }
6064
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006065 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006066 * @param increasedHeight true if this layout be created with an increased height.
6067 *
6068 * @hide
6069 */
6070 @Override
6071 public RemoteViews makeContentView(boolean increasedHeight) {
6072 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006073 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006074 mBuilder.mActions = new ArrayList<>();
6075 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006076 mBuilder.mActions = mBuilder.mOriginalActions;
6077 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006078 return remoteViews;
6079 }
6080 return super.makeContentView(increasedHeight);
6081 }
6082
6083 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006084 * @hide
6085 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006086 @Override
6087 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6088 if (increasedHeight && mBuilder.mActions.size() > 0) {
6089 return makeBigContentView();
6090 }
6091 return super.makeHeadsUpContentView(increasedHeight);
6092 }
6093
6094 /**
6095 * @hide
6096 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006097 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006098
6099 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006100 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006101 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006102
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006103 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006104
Selim Cinek75998782016-04-26 10:39:17 -07006105 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006106
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006107 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006108 if (TextUtils.isEmpty(bigTextText)) {
6109 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6110 // experience
6111 bigTextText = mBuilder.processLegacyText(text);
6112 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006113 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006114
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006115 return contentView;
6116 }
6117
Adrian Roosb1f427c2016-05-26 12:27:15 -07006118 static void applyBigTextContentView(Builder builder,
6119 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006120 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006121 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006122 contentView.setViewVisibility(R.id.big_text,
6123 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006124 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006125 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006126 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006127
6128 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006129 * Helper class for generating large-format notifications that include multiple back-and-forth
6130 * messages of varying types between any number of people.
6131 *
6132 * <br>
6133 * If the platform does not provide large-format notifications, this method has no effect. The
6134 * user will always see the normal notification view.
6135 * <br>
6136 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6137 * so:
6138 * <pre class="prettyprint">
6139 *
6140 * Notification noti = new Notification.Builder()
6141 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6142 * .setContentText(subject)
6143 * .setSmallIcon(R.drawable.new_message)
6144 * .setLargeIcon(aBitmap)
6145 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6146 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6147 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6148 * .build();
6149 * </pre>
6150 */
6151 public static class MessagingStyle extends Style {
6152
6153 /**
6154 * The maximum number of messages that will be retained in the Notification itself (the
6155 * number displayed is up to the platform).
6156 */
6157 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6158
Selim Cinekcb8b9852017-12-15 18:01:52 -08006159 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006160 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006161 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006162 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006163 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006164
6165 MessagingStyle() {
6166 }
6167
6168 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006169 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6170 * user before the posting app reposts the notification with those messages after they've
6171 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006172 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006173 *
6174 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006175 */
Alex Hillsfd590442016-10-07 09:52:44 -04006176 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006177 this(new Person().setName(userDisplayName));
6178 }
6179
6180 /**
6181 * @param user Required - The person displayed for any messages that are sent by the
6182 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6183 * who don't have a Person associated with it will be displayed as if they were sent
6184 * by this user. The user also needs to have a valid name associated with it.
6185 */
6186 public MessagingStyle(@NonNull Person user) {
6187 mUser = user;
6188 if (user == null || user.getName() == null) {
6189 throw new RuntimeException("user must be valid and have a name");
6190 }
6191 }
6192
6193 /**
6194 * @return the user to be displayed for any replies sent by the user
6195 */
6196 public Person getUser() {
6197 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006198 }
6199
6200 /**
6201 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006202 *
6203 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006204 */
6205 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006206 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006207 }
6208
6209 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006210 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6211 *
Kodlee Yin14656422017-12-22 17:00:46 -08006212 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6213 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6214 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6215 * {@code true} and passing {@code null} will make it return {@code false}. In
6216 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6217 * to set group conversation status.
6218 *
6219 * @param conversationTitle Title displayed for this conversation
6220 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006221 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006222 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006223 mConversationTitle = conversationTitle;
6224 return this;
6225 }
6226
6227 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006228 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006229 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006230 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006231 public CharSequence getConversationTitle() {
6232 return mConversationTitle;
6233 }
6234
6235 /**
6236 * Adds a message for display by this notification. Convenience call for a simple
6237 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6238 * @param text A {@link CharSequence} to be displayed as the message content
6239 * @param timestamp Time at which the message arrived
6240 * @param sender A {@link CharSequence} to be used for displaying the name of the
6241 * sender. Should be <code>null</code> for messages by the current user, in which case
6242 * the platform will insert {@link #getUserDisplayName()}.
6243 * Should be unique amongst all individuals in the conversation, and should be
6244 * consistent during re-posts of the notification.
6245 *
6246 * @see Message#Message(CharSequence, long, CharSequence)
6247 *
6248 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006249 *
6250 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006251 */
6252 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006253 return addMessage(text, timestamp,
6254 sender == null ? null : new Person().setName(sender));
6255 }
6256
6257 /**
6258 * Adds a message for display by this notification. Convenience call for a simple
6259 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6260 * @param text A {@link CharSequence} to be displayed as the message content
6261 * @param timestamp Time at which the message arrived
6262 * @param sender The {@link Person} who sent the message.
6263 * Should be <code>null</code> for messages by the current user, in which case
6264 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6265 *
6266 * @see Message#Message(CharSequence, long, CharSequence)
6267 *
6268 * @return this object for method chaining
6269 */
6270 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006271 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006272 }
6273
6274 /**
6275 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006276 *
6277 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6278 * the newest last.
6279 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006280 * @param message The {@link Message} to be displayed
6281 * @return this object for method chaining
6282 */
6283 public MessagingStyle addMessage(Message message) {
6284 mMessages.add(message);
6285 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6286 mMessages.remove(0);
6287 }
6288 return this;
6289 }
6290
6291 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006292 * Adds a {@link Message} for historic context in this notification.
6293 *
6294 * <p>Messages should be added as historic if they are not the main subject of the
6295 * notification but may give context to a conversation. The system may choose to present
6296 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6297 *
6298 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6299 * the newest last.
6300 *
6301 * @param message The historic {@link Message} to be added
6302 * @return this object for method chaining
6303 */
6304 public MessagingStyle addHistoricMessage(Message message) {
6305 mHistoricMessages.add(message);
6306 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6307 mHistoricMessages.remove(0);
6308 }
6309 return this;
6310 }
6311
6312 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006313 * Gets the list of {@code Message} objects that represent the notification
6314 */
6315 public List<Message> getMessages() {
6316 return mMessages;
6317 }
6318
6319 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006320 * Gets the list of historic {@code Message}s in the notification.
6321 */
6322 public List<Message> getHistoricMessages() {
6323 return mHistoricMessages;
6324 }
6325
6326 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006327 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006328 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006329 * @param isGroupConversation {@code true} if the conversation represents a group,
6330 * {@code false} otherwise.
6331 * @return this object for method chaining
6332 */
6333 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6334 mIsGroupConversation = isGroupConversation;
6335 return this;
6336 }
6337
6338 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006339 * Returns {@code true} if this notification represents a group conversation, otherwise
6340 * {@code false}.
6341 *
6342 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6343 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6344 * not the conversation title is set; returning {@code true} if the conversation title is
6345 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6346 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6347 * named, non-group conversations.
6348 *
6349 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006350 */
6351 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006352 // When target SDK version is < P, a non-null conversation title dictates if this is
6353 // as group conversation.
6354 if (mBuilder != null
6355 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6356 < Build.VERSION_CODES.P) {
6357 return mConversationTitle != null;
6358 }
6359
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006360 return mIsGroupConversation;
6361 }
6362
6363 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006364 * @hide
6365 */
6366 @Override
6367 public void addExtras(Bundle extras) {
6368 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006369 if (mUser != null) {
6370 // For legacy usages
6371 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6372 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006373 }
6374 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006375 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006376 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006377 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6378 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006379 }
Adrian Roos437cd562017-01-18 15:47:03 -08006380 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6381 Message.getBundleArrayForMessages(mHistoricMessages));
6382 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006383
6384 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006385 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006386 }
6387
6388 private void fixTitleAndTextExtras(Bundle extras) {
6389 Message m = findLatestIncomingMessage();
6390 CharSequence text = (m == null) ? null : m.mText;
6391 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006392 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6393 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006394 CharSequence title;
6395 if (!TextUtils.isEmpty(mConversationTitle)) {
6396 if (!TextUtils.isEmpty(sender)) {
6397 BidiFormatter bidi = BidiFormatter.getInstance();
6398 title = mBuilder.mContext.getString(
6399 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006400 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006401 } else {
6402 title = mConversationTitle;
6403 }
6404 } else {
6405 title = sender;
6406 }
6407
6408 if (title != null) {
6409 extras.putCharSequence(EXTRA_TITLE, title);
6410 }
6411 if (text != null) {
6412 extras.putCharSequence(EXTRA_TEXT, text);
6413 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006414 }
6415
6416 /**
6417 * @hide
6418 */
6419 @Override
6420 protected void restoreFromExtras(Bundle extras) {
6421 super.restoreFromExtras(extras);
6422
Selim Cinekcb8b9852017-12-15 18:01:52 -08006423 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6424 if (mUser == null) {
6425 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6426 mUser = new Person().setName(displayName);
6427 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006428 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006429 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006430 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006431 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006432 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006433 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006434 }
6435
6436 /**
6437 * @hide
6438 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006439 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006440 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006441 mBuilder.mOriginalActions = mBuilder.mActions;
6442 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006443 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006444 mBuilder.mActions = mBuilder.mOriginalActions;
6445 mBuilder.mOriginalActions = null;
6446 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006447 }
6448
6449 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006450 return findLatestIncomingMessage(mMessages);
6451 }
6452
6453 /**
6454 * @hide
6455 */
6456 @Nullable
6457 public static Message findLatestIncomingMessage(
6458 List<Message> messages) {
6459 for (int i = messages.size() - 1; i >= 0; i--) {
6460 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006461 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006462 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006463 return m;
6464 }
6465 }
Selim Cinek88188f22017-09-19 16:46:56 -07006466 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006467 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006468 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006469 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006470 return null;
6471 }
6472
6473 /**
6474 * @hide
6475 */
6476 @Override
6477 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006478 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006479 }
6480
6481 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006482 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006483 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006484 ? super.mBigContentTitle
6485 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006486 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006487 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006488 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006489 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006490 nameReplacement = conversationTitle;
6491 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006492 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006493 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006494 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006495 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006496 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006497 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006498 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006499 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006500 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006501 // also update the end margin if there is an image
6502 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006503 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006504 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6505 }
6506 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006507 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6508 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006509 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6510 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006511 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6512 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006513 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6514 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006515 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6516 isOneToOne);
6517 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6518 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006519 return contentView;
6520 }
6521
Selim Cinekf7409db2017-10-24 16:17:14 -07006522 private boolean hasOnlyWhiteSpaceSenders() {
6523 for (int i = 0; i < mMessages.size(); i++) {
6524 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006525 Person sender = m.getSenderPerson();
6526 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006527 return false;
6528 }
6529 }
6530 return true;
6531 }
6532
6533 private boolean isWhiteSpace(CharSequence sender) {
6534 if (TextUtils.isEmpty(sender)) {
6535 return true;
6536 }
6537 if (sender.toString().matches("^\\s*$")) {
6538 return true;
6539 }
6540 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6541 // For the presentation that we had.
6542 for (int i = 0; i < sender.length(); i++) {
6543 char c = sender.charAt(i);
6544 if (c != '\u200B') {
6545 return false;
6546 }
6547 }
6548 return true;
6549 }
6550
Selim Cinek88188f22017-09-19 16:46:56 -07006551 private CharSequence createConversationTitleFromMessages() {
6552 ArraySet<CharSequence> names = new ArraySet<>();
6553 for (int i = 0; i < mMessages.size(); i++) {
6554 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006555 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006556 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006557 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006558 }
6559 }
6560 SpannableStringBuilder title = new SpannableStringBuilder();
6561 int size = names.size();
6562 for (int i = 0; i < size; i++) {
6563 CharSequence name = names.valueAt(i);
6564 if (!TextUtils.isEmpty(title)) {
6565 title.append(", ");
6566 }
6567 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6568 }
6569 return title;
6570 }
6571
Adrian Roosdedd1df2016-04-26 16:38:47 -07006572 /**
6573 * @hide
6574 */
6575 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006576 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006577 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006578 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6579 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006580 }
6581
Adrian Roosc1a80b02016-04-05 14:54:55 -07006582 private static TextAppearanceSpan makeFontColorSpan(int color) {
6583 return new TextAppearanceSpan(null, 0, 0,
6584 ColorStateList.valueOf(color), null);
6585 }
6586
Alex Hillsd9b04d92016-04-11 16:38:16 -04006587 public static final class Message {
6588
6589 static final String KEY_TEXT = "text";
6590 static final String KEY_TIMESTAMP = "time";
6591 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006592 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006593 static final String KEY_DATA_MIME_TYPE = "type";
6594 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006595 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006596
6597 private final CharSequence mText;
6598 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006599 @Nullable
6600 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006601
Shane Brennan5a871862017-03-11 13:14:17 -08006602 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006603 private String mDataMimeType;
6604 private Uri mDataUri;
6605
6606 /**
6607 * Constructor
6608 * @param text A {@link CharSequence} to be displayed as the message content
6609 * @param timestamp Time at which the message arrived
6610 * @param sender A {@link CharSequence} to be used for displaying the name of the
6611 * sender. Should be <code>null</code> for messages by the current user, in which case
6612 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6613 * Should be unique amongst all individuals in the conversation, and should be
6614 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006615 *
6616 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006617 */
6618 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006619 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6620 }
6621
6622 /**
6623 * Constructor
6624 * @param text A {@link CharSequence} to be displayed as the message content
6625 * @param timestamp Time at which the message arrived
6626 * @param sender The {@link Person} who sent the message.
6627 * Should be <code>null</code> for messages by the current user, in which case
6628 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6629 * <p>
6630 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6631 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6632 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6633 * in order to differentiate between the different users.
6634 * </p>
6635 */
6636 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006637 mText = text;
6638 mTimestamp = timestamp;
6639 mSender = sender;
6640 }
6641
6642 /**
6643 * Sets a binary blob of data and an associated MIME type for a message. In the case
6644 * where the platform doesn't support the MIME type, the original text provided in the
6645 * constructor will be used.
6646 * @param dataMimeType The MIME type of the content. See
6647 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6648 * types on Android and Android Wear.
6649 * @param dataUri The uri containing the content whose type is given by the MIME type.
6650 * <p class="note">
6651 * <ol>
6652 * <li>Notification Listeners including the System UI need permission to access the
6653 * data the Uri points to. The recommended ways to do this are:</li>
6654 * <li>Store the data in your own ContentProvider, making sure that other apps have
6655 * the correct permission to access your provider. The preferred mechanism for
6656 * providing access is to use per-URI permissions which are temporary and only
6657 * grant access to the receiving application. An easy way to create a
6658 * ContentProvider like this is to use the FileProvider helper class.</li>
6659 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6660 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6661 * also store non-media types (see MediaStore.Files for more info). Files can be
6662 * inserted into the MediaStore using scanFile() after which a content:// style
6663 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6664 * Note that once added to the system MediaStore the content is accessible to any
6665 * app on the device.</li>
6666 * </ol>
6667 * @return this object for method chaining
6668 */
6669 public Message setData(String dataMimeType, Uri dataUri) {
6670 mDataMimeType = dataMimeType;
6671 mDataUri = dataUri;
6672 return this;
6673 }
6674
Alex Hillsfc737de2016-03-23 17:33:02 -04006675 /**
6676 * Get the text to be used for this message, or the fallback text if a type and content
6677 * Uri have been set
6678 */
6679 public CharSequence getText() {
6680 return mText;
6681 }
6682
6683 /**
6684 * Get the time at which this message arrived
6685 */
6686 public long getTimestamp() {
6687 return mTimestamp;
6688 }
6689
6690 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006691 * Get the extras Bundle for this message.
6692 */
6693 public Bundle getExtras() {
6694 return mExtras;
6695 }
6696
6697 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006698 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006699 *
6700 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006701 */
6702 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006703 return mSender == null ? null : mSender.getName();
6704 }
6705
6706 /**
6707 * Get the sender associated with this message.
6708 */
6709 @Nullable
6710 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006711 return mSender;
6712 }
6713
6714 /**
6715 * Get the MIME type of the data pointed to by the Uri
6716 */
6717 public String getDataMimeType() {
6718 return mDataMimeType;
6719 }
6720
6721 /**
6722 * Get the the Uri pointing to the content of the message. Can be null, in which case
6723 * {@see #getText()} is used.
6724 */
6725 public Uri getDataUri() {
6726 return mDataUri;
6727 }
6728
Alex Hillsd9b04d92016-04-11 16:38:16 -04006729 private Bundle toBundle() {
6730 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006731 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006732 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006733 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006734 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006736 // Legacy listeners need this
6737 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6738 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006739 }
6740 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006741 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006742 }
6743 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006744 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006745 }
Shane Brennan5a871862017-03-11 13:14:17 -08006746 if (mExtras != null) {
6747 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6748 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006749 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006750 }
6751
Alex Hillsd9b04d92016-04-11 16:38:16 -04006752 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6753 Bundle[] bundles = new Bundle[messages.size()];
6754 final int N = messages.size();
6755 for (int i = 0; i < N; i++) {
6756 bundles[i] = messages.get(i).toBundle();
6757 }
6758 return bundles;
6759 }
6760
Selim Cinek88188f22017-09-19 16:46:56 -07006761 /**
6762 * @return A list of messages read from the bundles.
6763 *
6764 * @hide
6765 */
6766 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6767 if (bundles == null) {
6768 return new ArrayList<>();
6769 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006770 List<Message> messages = new ArrayList<>(bundles.length);
6771 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006772 if (bundles[i] instanceof Bundle) {
6773 Message message = getMessageFromBundle((Bundle)bundles[i]);
6774 if (message != null) {
6775 messages.add(message);
6776 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006777 }
6778 }
6779 return messages;
6780 }
6781
6782 static Message getMessageFromBundle(Bundle bundle) {
6783 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006784 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006785 return null;
6786 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006787
6788 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6789 if (senderPerson == null) {
6790 // Legacy apps that use compat don't actually provide the sender objects
6791 // We need to fix the compat version to provide people / use
6792 // the native api instead
6793 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6794 if (senderName != null) {
6795 senderPerson = new Person().setName(senderName);
6796 }
6797 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006798 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006799 bundle.getLong(KEY_TIMESTAMP),
6800 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006801 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6802 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006803 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6804 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006805 }
Shane Brennan5a871862017-03-11 13:14:17 -08006806 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6807 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6808 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006809 return message;
6810 }
6811 } catch (ClassCastException e) {
6812 return null;
6813 }
6814 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006815 }
6816 }
6817
6818 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006819 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006820 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006821 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006822 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006823 * Notification notif = new Notification.Builder(mContext)
6824 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6825 * .setContentText(subject)
6826 * .setSmallIcon(R.drawable.new_mail)
6827 * .setLargeIcon(aBitmap)
6828 * .setStyle(new Notification.InboxStyle()
6829 * .addLine(str1)
6830 * .addLine(str2)
6831 * .setContentTitle(&quot;&quot;)
6832 * .setSummaryText(&quot;+3 more&quot;))
6833 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006834 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006835 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006836 * @see Notification#bigContentView
6837 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006838 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006839 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6840
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006841 public InboxStyle() {
6842 }
6843
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006844 /**
6845 * @deprecated use {@code InboxStyle()}.
6846 */
6847 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006848 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006849 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006850 }
6851
Chris Wrend6297db2012-05-03 16:20:13 -04006852 /**
6853 * Overrides ContentTitle in the big form of the template.
6854 * This defaults to the value passed to setContentTitle().
6855 */
6856 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006857 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006858 return this;
6859 }
6860
6861 /**
6862 * Set the first line of text after the detail section in the big form of the template.
6863 */
6864 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006865 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006866 return this;
6867 }
6868
Chris Wren0bd664d2012-08-01 13:56:56 -04006869 /**
6870 * Append a line to the digest section of the Inbox notification.
6871 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006872 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006873 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006874 return this;
6875 }
6876
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006877 /**
6878 * @hide
6879 */
6880 public void addExtras(Bundle extras) {
6881 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006882
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006883 CharSequence[] a = new CharSequence[mTexts.size()];
6884 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6885 }
6886
Christoph Studer4600f9b2014-07-22 22:44:43 +02006887 /**
6888 * @hide
6889 */
6890 @Override
6891 protected void restoreFromExtras(Bundle extras) {
6892 super.restoreFromExtras(extras);
6893
6894 mTexts.clear();
6895 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6896 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6897 }
6898 }
6899
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006900 /**
6901 * @hide
6902 */
6903 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006904 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006905 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006906 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6907 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006908
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006909 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006910
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006911 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006912
Chris Wrend6297db2012-05-03 16:20:13 -04006913 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 -04006914 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006915
Chris Wren4ed80d52012-05-17 09:30:03 -04006916 // Make sure all rows are gone in case we reuse a view.
6917 for (int rowId : rowIds) {
6918 contentView.setViewVisibility(rowId, View.GONE);
6919 }
6920
Daniel Sandler879c5e02012-04-17 16:46:51 -04006921 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006922 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6923 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006924 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006925 int onlyViewId = 0;
6926 int maxRows = rowIds.length;
6927 if (mBuilder.mActions.size() > 0) {
6928 maxRows--;
6929 }
6930 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006931 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006932 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006933 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006934 contentView.setTextViewText(rowIds[i],
6935 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006936 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006937 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006938 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006939 if (first) {
6940 onlyViewId = rowIds[i];
6941 } else {
6942 onlyViewId = 0;
6943 }
Selim Cinek247fa012016-02-18 09:50:48 -08006944 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006945 }
6946 i++;
6947 }
Selim Cinek07c80172016-04-21 16:40:47 -07006948 if (onlyViewId != 0) {
6949 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6950 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6951 R.dimen.notification_text_margin_top);
6952 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6953 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006954
Daniel Sandler879c5e02012-04-17 16:46:51 -04006955 return contentView;
6956 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006957
Selim Cinek247fa012016-02-18 09:50:48 -08006958 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006959 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006960 if (first) {
6961 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6962 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6963 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006964 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006965 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006966 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006967 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006968 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006969 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006970 }
Dan Sandler842dd772014-05-15 09:36:47 -04006971
6972 /**
6973 * Notification style for media playback notifications.
6974 *
6975 * In the expanded form, {@link Notification#bigContentView}, up to 5
6976 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006977 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006978 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6979 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6980 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006981 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006982 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6983 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006984 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006985 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006986 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006987 * Notifications created with MediaStyle will have their category set to
6988 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6989 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006990 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006991 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6992 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006993 * the System UI can identify this as a notification representing an active media session
6994 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6995 *
Selim Cinek99104832017-01-25 14:47:33 -08006996 * <p>
6997 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6998 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6999 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7000 * <p>
7001 *
Dan Sandler842dd772014-05-15 09:36:47 -04007002 * To use this style with your Notification, feed it to
7003 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7004 * <pre class="prettyprint">
7005 * Notification noti = new Notification.Builder()
7006 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007007 * .setContentTitle(&quot;Track title&quot;)
7008 * .setContentText(&quot;Artist - Album&quot;)
7009 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007010 * .setStyle(<b>new Notification.MediaStyle()</b>
7011 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007012 * .build();
7013 * </pre>
7014 *
7015 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007016 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007017 */
7018 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007019 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007020 static final int MAX_MEDIA_BUTTONS = 5;
7021
7022 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007023 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007024
7025 public MediaStyle() {
7026 }
7027
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007028 /**
7029 * @deprecated use {@code MediaStyle()}.
7030 */
7031 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007032 public MediaStyle(Builder builder) {
7033 setBuilder(builder);
7034 }
7035
7036 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007037 * 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 -04007038 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007039 *
7040 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007041 */
7042 public MediaStyle setShowActionsInCompactView(int...actions) {
7043 mActionsToShowInCompact = actions;
7044 return this;
7045 }
7046
7047 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007048 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7049 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007050 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007051 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007052 mToken = token;
7053 return this;
7054 }
7055
Christoph Studer4600f9b2014-07-22 22:44:43 +02007056 /**
7057 * @hide
7058 */
Dan Sandler842dd772014-05-15 09:36:47 -04007059 @Override
7060 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007061 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007062 if (wip.category == null) {
7063 wip.category = Notification.CATEGORY_TRANSPORT;
7064 }
Dan Sandler842dd772014-05-15 09:36:47 -04007065 return wip;
7066 }
7067
Christoph Studer4600f9b2014-07-22 22:44:43 +02007068 /**
7069 * @hide
7070 */
7071 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007072 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007073 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007074 }
7075
7076 /**
7077 * @hide
7078 */
7079 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007080 public RemoteViews makeBigContentView() {
7081 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007082 }
7083
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007084 /**
7085 * @hide
7086 */
7087 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007088 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007089 RemoteViews expanded = makeMediaBigContentView();
7090 return expanded != null ? expanded : makeMediaContentView();
7091 }
7092
Dan Sandler842dd772014-05-15 09:36:47 -04007093 /** @hide */
7094 @Override
7095 public void addExtras(Bundle extras) {
7096 super.addExtras(extras);
7097
7098 if (mToken != null) {
7099 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7100 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007101 if (mActionsToShowInCompact != null) {
7102 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7103 }
Dan Sandler842dd772014-05-15 09:36:47 -04007104 }
7105
Christoph Studer4600f9b2014-07-22 22:44:43 +02007106 /**
7107 * @hide
7108 */
7109 @Override
7110 protected void restoreFromExtras(Bundle extras) {
7111 super.restoreFromExtras(extras);
7112
7113 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7114 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7115 }
7116 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7117 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7118 }
7119 }
7120
Selim Cinek5bf069a2015-11-10 19:14:27 -05007121 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007122 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007123 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007124 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007125 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007126
7127 // If the action buttons should not be tinted, then just use the default
7128 // notification color. Otherwise, just use the passed-in color.
7129 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7130 ? color
7131 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7132 Notification.COLOR_DEFAULT);
7133
Sunny Goyal5b153922017-09-21 21:00:36 -07007134 button.setDrawableTint(R.id.action0, false, tintColor,
7135 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007136 if (!tombstone) {
7137 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7138 }
7139 button.setContentDescription(R.id.action0, action.title);
7140 return button;
7141 }
7142
7143 private RemoteViews makeMediaContentView() {
7144 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007145 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007146
7147 final int numActions = mBuilder.mActions.size();
7148 final int N = mActionsToShowInCompact == null
7149 ? 0
7150 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7151 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007152 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007153 for (int i = 0; i < N; i++) {
7154 if (i >= numActions) {
7155 throw new IllegalArgumentException(String.format(
7156 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7157 i, numActions - 1));
7158 }
7159
7160 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007161 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007162 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007163 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007164 }
7165 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007166 handleImage(view);
7167 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007168 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007169 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007170 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007171 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007172 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007173 return view;
7174 }
7175
Selim Cinek99104832017-01-25 14:47:33 -08007176 private int getPrimaryHighlightColor() {
7177 return mBuilder.getPrimaryHighlightColor();
7178 }
7179
Dan Sandler842dd772014-05-15 09:36:47 -04007180 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007181 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007182 // Dont add an expanded view if there is no more content to be revealed
7183 int actionsInCompact = mActionsToShowInCompact == null
7184 ? 0
7185 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007186 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007187 return null;
7188 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007189 RemoteViews big = mBuilder.applyStandardTemplate(
7190 R.layout.notification_template_material_big_media,
7191 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007192
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007193 if (actionCount > 0) {
7194 big.removeAllViews(com.android.internal.R.id.media_actions);
7195 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007196 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007197 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007198 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007199 }
7200 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007201 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007202 return big;
7203 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007204
Selim Cinek5bf069a2015-11-10 19:14:27 -05007205 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007206 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007207 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7208 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007209 }
7210 }
7211
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007212 /**
7213 * @hide
7214 */
7215 @Override
7216 protected boolean hasProgress() {
7217 return false;
7218 }
Dan Sandler842dd772014-05-15 09:36:47 -04007219 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007220
Selim Cinek593610c2016-02-16 18:42:57 -08007221 /**
7222 * Notification style for custom views that are decorated by the system
7223 *
7224 * <p>Instead of providing a notification that is completely custom, a developer can set this
7225 * style and still obtain system decorations like the notification header with the expand
7226 * affordance and actions.
7227 *
7228 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7229 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7230 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7231 * corresponding custom views to display.
7232 *
7233 * To use this style with your Notification, feed it to
7234 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7235 * <pre class="prettyprint">
7236 * Notification noti = new Notification.Builder()
7237 * .setSmallIcon(R.drawable.ic_stat_player)
7238 * .setLargeIcon(albumArtBitmap))
7239 * .setCustomContentView(contentView);
7240 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7241 * .build();
7242 * </pre>
7243 */
7244 public static class DecoratedCustomViewStyle extends Style {
7245
7246 public DecoratedCustomViewStyle() {
7247 }
7248
Selim Cinek593610c2016-02-16 18:42:57 -08007249 /**
7250 * @hide
7251 */
7252 public boolean displayCustomViewInline() {
7253 return true;
7254 }
7255
7256 /**
7257 * @hide
7258 */
7259 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007260 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007261 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7262 }
7263
7264 /**
7265 * @hide
7266 */
7267 @Override
7268 public RemoteViews makeBigContentView() {
7269 return makeDecoratedBigContentView();
7270 }
7271
7272 /**
7273 * @hide
7274 */
7275 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007276 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007277 return makeDecoratedHeadsUpContentView();
7278 }
7279
Selim Cinek593610c2016-02-16 18:42:57 -08007280 private RemoteViews makeDecoratedHeadsUpContentView() {
7281 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7282 ? mBuilder.mN.contentView
7283 : mBuilder.mN.headsUpContentView;
7284 if (mBuilder.mActions.size() == 0) {
7285 return makeStandardTemplateWithCustomContent(headsUpContentView);
7286 }
7287 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7288 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007289 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007290 return remoteViews;
7291 }
7292
Selim Cinek593610c2016-02-16 18:42:57 -08007293 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7294 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7295 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007296 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007297 return remoteViews;
7298 }
7299
Selim Cinek593610c2016-02-16 18:42:57 -08007300 private RemoteViews makeDecoratedBigContentView() {
7301 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7302 ? mBuilder.mN.contentView
7303 : mBuilder.mN.bigContentView;
7304 if (mBuilder.mActions.size() == 0) {
7305 return makeStandardTemplateWithCustomContent(bigContentView);
7306 }
7307 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7308 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007309 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007310 return remoteViews;
7311 }
Selim Cinek247fa012016-02-18 09:50:48 -08007312
7313 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7314 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007315 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007316 // Need to clone customContent before adding, because otherwise it can no longer be
7317 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007318 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007319 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7320 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007321 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007322 }
Selim Cinek247fa012016-02-18 09:50:48 -08007323 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007324 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007325 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007326 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007327 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007328 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007329 }
Selim Cinek593610c2016-02-16 18:42:57 -08007330 }
7331
Selim Cinek03eb3b72016-02-18 10:39:45 -08007332 /**
7333 * Notification style for media custom views that are decorated by the system
7334 *
7335 * <p>Instead of providing a media notification that is completely custom, a developer can set
7336 * this style and still obtain system decorations like the notification header with the expand
7337 * affordance and actions.
7338 *
7339 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7340 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7341 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7342 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007343 * <p>
7344 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7345 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7346 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007347 * To use this style with your Notification, feed it to
7348 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7349 * <pre class="prettyprint">
7350 * Notification noti = new Notification.Builder()
7351 * .setSmallIcon(R.drawable.ic_stat_player)
7352 * .setLargeIcon(albumArtBitmap))
7353 * .setCustomContentView(contentView);
7354 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7355 * .setMediaSession(mySession))
7356 * .build();
7357 * </pre>
7358 *
7359 * @see android.app.Notification.DecoratedCustomViewStyle
7360 * @see android.app.Notification.MediaStyle
7361 */
7362 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7363
7364 public DecoratedMediaCustomViewStyle() {
7365 }
7366
Selim Cinek03eb3b72016-02-18 10:39:45 -08007367 /**
7368 * @hide
7369 */
7370 public boolean displayCustomViewInline() {
7371 return true;
7372 }
7373
7374 /**
7375 * @hide
7376 */
7377 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007378 public RemoteViews makeContentView(boolean increasedHeight) {
7379 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007380 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7381 mBuilder.mN.contentView);
7382 }
7383
7384 /**
7385 * @hide
7386 */
7387 @Override
7388 public RemoteViews makeBigContentView() {
7389 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7390 ? mBuilder.mN.bigContentView
7391 : mBuilder.mN.contentView;
7392 return makeBigContentViewWithCustomContent(customRemoteView);
7393 }
7394
7395 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7396 RemoteViews remoteViews = super.makeBigContentView();
7397 if (remoteViews != null) {
7398 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7399 customRemoteView);
7400 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007401 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007402 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7403 customRemoteView);
7404 } else {
7405 return null;
7406 }
7407 }
7408
7409 /**
7410 * @hide
7411 */
7412 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007413 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007414 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7415 ? mBuilder.mN.headsUpContentView
7416 : mBuilder.mN.contentView;
7417 return makeBigContentViewWithCustomContent(customRemoteView);
7418 }
7419
7420 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7421 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007422 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007423 // Need to clone customContent before adding, because otherwise it can no longer be
7424 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007425 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007426 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007427 remoteViews.removeAllViews(id);
7428 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007429 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007430 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007431 return remoteViews;
7432 }
7433 }
7434
Selim Cineke7238dd2017-12-14 17:48:32 -08007435 /**
7436 * A Person associated with this Notification.
7437 */
7438 public static final class Person implements Parcelable {
7439 @Nullable private CharSequence mName;
7440 @Nullable private Icon mIcon;
7441 @Nullable private String mUri;
7442 @Nullable private String mKey;
7443
7444 protected Person(Parcel in) {
7445 mName = in.readCharSequence();
7446 if (in.readInt() != 0) {
7447 mIcon = Icon.CREATOR.createFromParcel(in);
7448 }
7449 mUri = in.readString();
7450 mKey = in.readString();
7451 }
7452
7453 /**
7454 * Create a new person.
7455 */
7456 public Person() {
7457 }
7458
7459 /**
7460 * Give this person a name.
7461 *
7462 * @param name the name of this person
7463 */
7464 public Person setName(@Nullable CharSequence name) {
7465 this.mName = name;
7466 return this;
7467 }
7468
7469 /**
7470 * Add an icon for this person.
7471 * <br />
7472 * This is currently only used for {@link MessagingStyle} notifications and should not be
7473 * provided otherwise, in order to save memory. The system will prefer this icon over any
7474 * images that are resolved from the URI.
7475 *
7476 * @param icon the icon of the person
7477 */
7478 public Person setIcon(@Nullable Icon icon) {
7479 this.mIcon = icon;
7480 return this;
7481 }
7482
7483 /**
7484 * Set a URI associated with this person.
7485 *
7486 * <P>
7487 * Depending on user preferences, adding a URI to a Person may allow the notification to
7488 * pass through interruption filters, if this notification is of
7489 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7490 * The addition of people may also cause this notification to appear more prominently in
7491 * the user interface.
7492 * </P>
7493 *
7494 * <P>
7495 * The person should be specified by the {@code String} representation of a
7496 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7497 * </P>
7498 *
7499 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7500 * URIs. The path part of these URIs must exist in the contacts database, in the
7501 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7502 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7503 * </P>
7504 *
7505 * @param uri a URI for the person
7506 */
7507 public Person setUri(@Nullable String uri) {
7508 mUri = uri;
7509 return this;
7510 }
7511
7512 /**
7513 * Add a key to this person in order to uniquely identify it.
7514 * This is especially useful if the name doesn't uniquely identify this person or if the
7515 * display name is a short handle of the actual name.
7516 *
7517 * <P>If no key is provided, the name serves as as the key for the purpose of
7518 * identification.</P>
7519 *
7520 * @param key the key that uniquely identifies this person
7521 */
7522 public Person setKey(@Nullable String key) {
7523 mKey = key;
7524 return this;
7525 }
7526
7527
7528 /**
7529 * @return the uri provided for this person or {@code null} if no Uri was provided
7530 */
7531 @Nullable
7532 public String getUri() {
7533 return mUri;
7534 }
7535
7536 /**
7537 * @return the name provided for this person or {@code null} if no name was provided
7538 */
7539 @Nullable
7540 public CharSequence getName() {
7541 return mName;
7542 }
7543
7544 /**
7545 * @return the icon provided for this person or {@code null} if no icon was provided
7546 */
7547 @Nullable
7548 public Icon getIcon() {
7549 return mIcon;
7550 }
7551
7552 /**
7553 * @return the key provided for this person or {@code null} if no key was provided
7554 */
7555 @Nullable
7556 public String getKey() {
7557 return mKey;
7558 }
7559
7560 /**
7561 * @return the URI associated with this person, or "name:mName" otherwise
7562 * @hide
7563 */
7564 public String resolveToLegacyUri() {
7565 if (mUri != null) {
7566 return mUri;
7567 }
7568 if (mName != null) {
7569 return "name:" + mName;
7570 }
7571 return "";
7572 }
7573
7574 @Override
7575 public int describeContents() {
7576 return 0;
7577 }
7578
7579 @Override
7580 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7581 dest.writeCharSequence(mName);
7582 if (mIcon != null) {
7583 dest.writeInt(1);
7584 mIcon.writeToParcel(dest, 0);
7585 } else {
7586 dest.writeInt(0);
7587 }
7588 dest.writeString(mUri);
7589 dest.writeString(mKey);
7590 }
7591
7592 public static final Creator<Person> CREATOR = new Creator<Person>() {
7593 @Override
7594 public Person createFromParcel(Parcel in) {
7595 return new Person(in);
7596 }
7597
7598 @Override
7599 public Person[] newArray(int size) {
7600 return new Person[size];
7601 }
7602 };
7603 }
7604
Christoph Studer4600f9b2014-07-22 22:44:43 +02007605 // When adding a new Style subclass here, don't forget to update
7606 // Builder.getNotificationStyleClass.
7607
Griff Hazen61a9e862014-05-22 16:05:19 -07007608 /**
7609 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7610 * metadata or change options on a notification builder.
7611 */
7612 public interface Extender {
7613 /**
7614 * Apply this extender to a notification builder.
7615 * @param builder the builder to be modified.
7616 * @return the build object for chaining.
7617 */
7618 public Builder extend(Builder builder);
7619 }
7620
7621 /**
7622 * Helper class to add wearable extensions to notifications.
7623 * <p class="note"> See
7624 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7625 * for Android Wear</a> for more information on how to use this class.
7626 * <p>
7627 * To create a notification with wearable extensions:
7628 * <ol>
7629 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7630 * properties.
7631 * <li>Create a {@link android.app.Notification.WearableExtender}.
7632 * <li>Set wearable-specific properties using the
7633 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7634 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7635 * notification.
7636 * <li>Post the notification to the notification system with the
7637 * {@code NotificationManager.notify(...)} methods.
7638 * </ol>
7639 *
7640 * <pre class="prettyprint">
7641 * Notification notif = new Notification.Builder(mContext)
7642 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7643 * .setContentText(subject)
7644 * .setSmallIcon(R.drawable.new_mail)
7645 * .extend(new Notification.WearableExtender()
7646 * .setContentIcon(R.drawable.new_mail))
7647 * .build();
7648 * NotificationManager notificationManger =
7649 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7650 * notificationManger.notify(0, notif);</pre>
7651 *
7652 * <p>Wearable extensions can be accessed on an existing notification by using the
7653 * {@code WearableExtender(Notification)} constructor,
7654 * and then using the {@code get} methods to access values.
7655 *
7656 * <pre class="prettyprint">
7657 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7658 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007659 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007660 */
7661 public static final class WearableExtender implements Extender {
7662 /**
7663 * Sentinel value for an action index that is unset.
7664 */
7665 public static final int UNSET_ACTION_INDEX = -1;
7666
7667 /**
7668 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7669 * default sizing.
7670 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007671 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007672 * on their content.
7673 */
7674 public static final int SIZE_DEFAULT = 0;
7675
7676 /**
7677 * Size value for use with {@link #setCustomSizePreset} to show this notification
7678 * with an extra small size.
7679 * <p>This value is only applicable for custom display notifications created using
7680 * {@link #setDisplayIntent}.
7681 */
7682 public static final int SIZE_XSMALL = 1;
7683
7684 /**
7685 * Size value for use with {@link #setCustomSizePreset} to show this notification
7686 * with a small size.
7687 * <p>This value is only applicable for custom display notifications created using
7688 * {@link #setDisplayIntent}.
7689 */
7690 public static final int SIZE_SMALL = 2;
7691
7692 /**
7693 * Size value for use with {@link #setCustomSizePreset} to show this notification
7694 * with a medium size.
7695 * <p>This value is only applicable for custom display notifications created using
7696 * {@link #setDisplayIntent}.
7697 */
7698 public static final int SIZE_MEDIUM = 3;
7699
7700 /**
7701 * Size value for use with {@link #setCustomSizePreset} to show this notification
7702 * with a large size.
7703 * <p>This value is only applicable for custom display notifications created using
7704 * {@link #setDisplayIntent}.
7705 */
7706 public static final int SIZE_LARGE = 4;
7707
Griff Hazend5f11f92014-05-27 15:40:09 -07007708 /**
7709 * Size value for use with {@link #setCustomSizePreset} to show this notification
7710 * full screen.
7711 * <p>This value is only applicable for custom display notifications created using
7712 * {@link #setDisplayIntent}.
7713 */
7714 public static final int SIZE_FULL_SCREEN = 5;
7715
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007716 /**
7717 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7718 * short amount of time when this notification is displayed on the screen. This
7719 * is the default value.
7720 */
7721 public static final int SCREEN_TIMEOUT_SHORT = 0;
7722
7723 /**
7724 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7725 * for a longer amount of time when this notification is displayed on the screen.
7726 */
7727 public static final int SCREEN_TIMEOUT_LONG = -1;
7728
Griff Hazen61a9e862014-05-22 16:05:19 -07007729 /** Notification extra which contains wearable extensions */
7730 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7731
Pete Gastaf6781d2014-10-07 15:17:05 -04007732 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007733 private static final String KEY_ACTIONS = "actions";
7734 private static final String KEY_FLAGS = "flags";
7735 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7736 private static final String KEY_PAGES = "pages";
7737 private static final String KEY_BACKGROUND = "background";
7738 private static final String KEY_CONTENT_ICON = "contentIcon";
7739 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7740 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7741 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7742 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7743 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007744 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007745 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007746 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007747
7748 // Flags bitwise-ored to mFlags
7749 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7750 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7751 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7752 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007753 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007754 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007755 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007756
7757 // Default value for flags integer
7758 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7759
7760 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7761 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7762
7763 private ArrayList<Action> mActions = new ArrayList<Action>();
7764 private int mFlags = DEFAULT_FLAGS;
7765 private PendingIntent mDisplayIntent;
7766 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7767 private Bitmap mBackground;
7768 private int mContentIcon;
7769 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7770 private int mContentActionIndex = UNSET_ACTION_INDEX;
7771 private int mCustomSizePreset = SIZE_DEFAULT;
7772 private int mCustomContentHeight;
7773 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007774 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007775 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007776 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007777
7778 /**
7779 * Create a {@link android.app.Notification.WearableExtender} with default
7780 * options.
7781 */
7782 public WearableExtender() {
7783 }
7784
7785 public WearableExtender(Notification notif) {
7786 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7787 if (wearableBundle != null) {
7788 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7789 if (actions != null) {
7790 mActions.addAll(actions);
7791 }
7792
7793 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7794 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7795
7796 Notification[] pages = getNotificationArrayFromBundle(
7797 wearableBundle, KEY_PAGES);
7798 if (pages != null) {
7799 Collections.addAll(mPages, pages);
7800 }
7801
7802 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7803 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7804 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7805 DEFAULT_CONTENT_ICON_GRAVITY);
7806 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7807 UNSET_ACTION_INDEX);
7808 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7809 SIZE_DEFAULT);
7810 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7811 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007812 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007813 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007814 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007815 }
7816 }
7817
7818 /**
7819 * Apply wearable extensions to a notification that is being built. This is typically
7820 * called by the {@link android.app.Notification.Builder#extend} method of
7821 * {@link android.app.Notification.Builder}.
7822 */
7823 @Override
7824 public Notification.Builder extend(Notification.Builder builder) {
7825 Bundle wearableBundle = new Bundle();
7826
7827 if (!mActions.isEmpty()) {
7828 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7829 }
7830 if (mFlags != DEFAULT_FLAGS) {
7831 wearableBundle.putInt(KEY_FLAGS, mFlags);
7832 }
7833 if (mDisplayIntent != null) {
7834 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7835 }
7836 if (!mPages.isEmpty()) {
7837 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7838 new Notification[mPages.size()]));
7839 }
7840 if (mBackground != null) {
7841 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7842 }
7843 if (mContentIcon != 0) {
7844 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7845 }
7846 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7847 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7848 }
7849 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7850 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7851 mContentActionIndex);
7852 }
7853 if (mCustomSizePreset != SIZE_DEFAULT) {
7854 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7855 }
7856 if (mCustomContentHeight != 0) {
7857 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7858 }
7859 if (mGravity != DEFAULT_GRAVITY) {
7860 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7861 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007862 if (mHintScreenTimeout != 0) {
7863 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7864 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007865 if (mDismissalId != null) {
7866 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7867 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007868 if (mBridgeTag != null) {
7869 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7870 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007871
7872 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7873 return builder;
7874 }
7875
7876 @Override
7877 public WearableExtender clone() {
7878 WearableExtender that = new WearableExtender();
7879 that.mActions = new ArrayList<Action>(this.mActions);
7880 that.mFlags = this.mFlags;
7881 that.mDisplayIntent = this.mDisplayIntent;
7882 that.mPages = new ArrayList<Notification>(this.mPages);
7883 that.mBackground = this.mBackground;
7884 that.mContentIcon = this.mContentIcon;
7885 that.mContentIconGravity = this.mContentIconGravity;
7886 that.mContentActionIndex = this.mContentActionIndex;
7887 that.mCustomSizePreset = this.mCustomSizePreset;
7888 that.mCustomContentHeight = this.mCustomContentHeight;
7889 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007890 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007891 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007892 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007893 return that;
7894 }
7895
7896 /**
7897 * Add a wearable action to this notification.
7898 *
7899 * <p>When wearable actions are added using this method, the set of actions that
7900 * show on a wearable device splits from devices that only show actions added
7901 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7902 * of which actions display on different devices.
7903 *
7904 * @param action the action to add to this notification
7905 * @return this object for method chaining
7906 * @see android.app.Notification.Action
7907 */
7908 public WearableExtender addAction(Action action) {
7909 mActions.add(action);
7910 return this;
7911 }
7912
7913 /**
7914 * Adds wearable actions to this notification.
7915 *
7916 * <p>When wearable actions are added using this method, the set of actions that
7917 * show on a wearable device splits from devices that only show actions added
7918 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7919 * of which actions display on different devices.
7920 *
7921 * @param actions the actions to add to this notification
7922 * @return this object for method chaining
7923 * @see android.app.Notification.Action
7924 */
7925 public WearableExtender addActions(List<Action> actions) {
7926 mActions.addAll(actions);
7927 return this;
7928 }
7929
7930 /**
7931 * Clear all wearable actions present on this builder.
7932 * @return this object for method chaining.
7933 * @see #addAction
7934 */
7935 public WearableExtender clearActions() {
7936 mActions.clear();
7937 return this;
7938 }
7939
7940 /**
7941 * Get the wearable actions present on this notification.
7942 */
7943 public List<Action> getActions() {
7944 return mActions;
7945 }
7946
7947 /**
7948 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007949 * this notification. The {@link PendingIntent} provided should be for an activity.
7950 *
7951 * <pre class="prettyprint">
7952 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7953 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7954 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7955 * Notification notif = new Notification.Builder(context)
7956 * .extend(new Notification.WearableExtender()
7957 * .setDisplayIntent(displayPendingIntent)
7958 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7959 * .build();</pre>
7960 *
7961 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007962 * should have an empty task affinity. It is also recommended to use the device
7963 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007964 *
7965 * <p>Example AndroidManifest.xml entry:
7966 * <pre class="prettyprint">
7967 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7968 * android:exported=&quot;true&quot;
7969 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007970 * android:taskAffinity=&quot;&quot;
7971 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007972 *
7973 * @param intent the {@link PendingIntent} for an activity
7974 * @return this object for method chaining
7975 * @see android.app.Notification.WearableExtender#getDisplayIntent
7976 */
7977 public WearableExtender setDisplayIntent(PendingIntent intent) {
7978 mDisplayIntent = intent;
7979 return this;
7980 }
7981
7982 /**
7983 * Get the intent to launch inside of an activity view when displaying this
7984 * notification. This {@code PendingIntent} should be for an activity.
7985 */
7986 public PendingIntent getDisplayIntent() {
7987 return mDisplayIntent;
7988 }
7989
7990 /**
7991 * Add an additional page of content to display with this notification. The current
7992 * notification forms the first page, and pages added using this function form
7993 * subsequent pages. This field can be used to separate a notification into multiple
7994 * sections.
7995 *
7996 * @param page the notification to add as another page
7997 * @return this object for method chaining
7998 * @see android.app.Notification.WearableExtender#getPages
7999 */
8000 public WearableExtender addPage(Notification page) {
8001 mPages.add(page);
8002 return this;
8003 }
8004
8005 /**
8006 * Add additional pages of content to display with this notification. The current
8007 * notification forms the first page, and pages added using this function form
8008 * subsequent pages. This field can be used to separate a notification into multiple
8009 * sections.
8010 *
8011 * @param pages a list of notifications
8012 * @return this object for method chaining
8013 * @see android.app.Notification.WearableExtender#getPages
8014 */
8015 public WearableExtender addPages(List<Notification> pages) {
8016 mPages.addAll(pages);
8017 return this;
8018 }
8019
8020 /**
8021 * Clear all additional pages present on this builder.
8022 * @return this object for method chaining.
8023 * @see #addPage
8024 */
8025 public WearableExtender clearPages() {
8026 mPages.clear();
8027 return this;
8028 }
8029
8030 /**
8031 * Get the array of additional pages of content for displaying this notification. The
8032 * current notification forms the first page, and elements within this array form
8033 * subsequent pages. This field can be used to separate a notification into multiple
8034 * sections.
8035 * @return the pages for this notification
8036 */
8037 public List<Notification> getPages() {
8038 return mPages;
8039 }
8040
8041 /**
8042 * Set a background image to be displayed behind the notification content.
8043 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8044 * will work with any notification style.
8045 *
8046 * @param background the background bitmap
8047 * @return this object for method chaining
8048 * @see android.app.Notification.WearableExtender#getBackground
8049 */
8050 public WearableExtender setBackground(Bitmap background) {
8051 mBackground = background;
8052 return this;
8053 }
8054
8055 /**
8056 * Get a background image to be displayed behind the notification content.
8057 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8058 * will work with any notification style.
8059 *
8060 * @return the background image
8061 * @see android.app.Notification.WearableExtender#setBackground
8062 */
8063 public Bitmap getBackground() {
8064 return mBackground;
8065 }
8066
8067 /**
8068 * Set an icon that goes with the content of this notification.
8069 */
8070 public WearableExtender setContentIcon(int icon) {
8071 mContentIcon = icon;
8072 return this;
8073 }
8074
8075 /**
8076 * Get an icon that goes with the content of this notification.
8077 */
8078 public int getContentIcon() {
8079 return mContentIcon;
8080 }
8081
8082 /**
8083 * Set the gravity that the content icon should have within the notification display.
8084 * Supported values include {@link android.view.Gravity#START} and
8085 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8086 * @see #setContentIcon
8087 */
8088 public WearableExtender setContentIconGravity(int contentIconGravity) {
8089 mContentIconGravity = contentIconGravity;
8090 return this;
8091 }
8092
8093 /**
8094 * Get the gravity that the content icon should have within the notification display.
8095 * Supported values include {@link android.view.Gravity#START} and
8096 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8097 * @see #getContentIcon
8098 */
8099 public int getContentIconGravity() {
8100 return mContentIconGravity;
8101 }
8102
8103 /**
8104 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008105 * this notification. This action will no longer display separately from the
8106 * notification's content.
8107 *
Griff Hazenca48d352014-05-28 22:37:13 -07008108 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008109 * set, although the list of available actions comes from the main notification and not
8110 * from the child page's notification.
8111 *
8112 * @param actionIndex The index of the action to hoist onto the current notification page.
8113 * If wearable actions were added to the main notification, this index
8114 * will apply to that list, otherwise it will apply to the regular
8115 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008116 */
8117 public WearableExtender setContentAction(int actionIndex) {
8118 mContentActionIndex = actionIndex;
8119 return this;
8120 }
8121
8122 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008123 * Get the index of the notification action, if any, that was specified as being clickable
8124 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008125 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008126 *
Griff Hazenca48d352014-05-28 22:37:13 -07008127 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008128 * set, although the list of available actions comes from the main notification and not
8129 * from the child page's notification.
8130 *
8131 * <p>If wearable specific actions were added to the main notification, this index will
8132 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008133 *
8134 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008135 */
8136 public int getContentAction() {
8137 return mContentActionIndex;
8138 }
8139
8140 /**
8141 * Set the gravity that this notification should have within the available viewport space.
8142 * Supported values include {@link android.view.Gravity#TOP},
8143 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8144 * The default value is {@link android.view.Gravity#BOTTOM}.
8145 */
8146 public WearableExtender setGravity(int gravity) {
8147 mGravity = gravity;
8148 return this;
8149 }
8150
8151 /**
8152 * Get the gravity that this notification should have within the available viewport space.
8153 * Supported values include {@link android.view.Gravity#TOP},
8154 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8155 * The default value is {@link android.view.Gravity#BOTTOM}.
8156 */
8157 public int getGravity() {
8158 return mGravity;
8159 }
8160
8161 /**
8162 * Set the custom size preset for the display of this notification out of the available
8163 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8164 * {@link #SIZE_LARGE}.
8165 * <p>Some custom size presets are only applicable for custom display notifications created
8166 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8167 * documentation for the preset in question. See also
8168 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8169 */
8170 public WearableExtender setCustomSizePreset(int sizePreset) {
8171 mCustomSizePreset = sizePreset;
8172 return this;
8173 }
8174
8175 /**
8176 * Get the custom size preset for the display of this notification out of the available
8177 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8178 * {@link #SIZE_LARGE}.
8179 * <p>Some custom size presets are only applicable for custom display notifications created
8180 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8181 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8182 */
8183 public int getCustomSizePreset() {
8184 return mCustomSizePreset;
8185 }
8186
8187 /**
8188 * Set the custom height in pixels for the display of this notification's content.
8189 * <p>This option is only available for custom display notifications created
8190 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8191 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8192 * {@link #getCustomContentHeight}.
8193 */
8194 public WearableExtender setCustomContentHeight(int height) {
8195 mCustomContentHeight = height;
8196 return this;
8197 }
8198
8199 /**
8200 * Get the custom height in pixels for the display of this notification's content.
8201 * <p>This option is only available for custom display notifications created
8202 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8203 * {@link #setCustomContentHeight}.
8204 */
8205 public int getCustomContentHeight() {
8206 return mCustomContentHeight;
8207 }
8208
8209 /**
8210 * Set whether the scrolling position for the contents of this notification should start
8211 * at the bottom of the contents instead of the top when the contents are too long to
8212 * display within the screen. Default is false (start scroll at the top).
8213 */
8214 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8215 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8216 return this;
8217 }
8218
8219 /**
8220 * Get whether the scrolling position for the contents of this notification should start
8221 * at the bottom of the contents instead of the top when the contents are too long to
8222 * display within the screen. Default is false (start scroll at the top).
8223 */
8224 public boolean getStartScrollBottom() {
8225 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8226 }
8227
8228 /**
8229 * Set whether the content intent is available when the wearable device is not connected
8230 * to a companion device. The user can still trigger this intent when the wearable device
8231 * is offline, but a visual hint will indicate that the content intent may not be available.
8232 * Defaults to true.
8233 */
8234 public WearableExtender setContentIntentAvailableOffline(
8235 boolean contentIntentAvailableOffline) {
8236 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8237 return this;
8238 }
8239
8240 /**
8241 * Get whether the content intent is available when the wearable device is not connected
8242 * to a companion device. The user can still trigger this intent when the wearable device
8243 * is offline, but a visual hint will indicate that the content intent may not be available.
8244 * Defaults to true.
8245 */
8246 public boolean getContentIntentAvailableOffline() {
8247 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8248 }
8249
8250 /**
8251 * Set a hint that this notification's icon should not be displayed.
8252 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8253 * @return this object for method chaining
8254 */
8255 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8256 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8257 return this;
8258 }
8259
8260 /**
8261 * Get a hint that this notification's icon should not be displayed.
8262 * @return {@code true} if this icon should not be displayed, false otherwise.
8263 * The default value is {@code false} if this was never set.
8264 */
8265 public boolean getHintHideIcon() {
8266 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8267 }
8268
8269 /**
8270 * Set a visual hint that only the background image of this notification should be
8271 * displayed, and other semantic content should be hidden. This hint is only applicable
8272 * to sub-pages added using {@link #addPage}.
8273 */
8274 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8275 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8276 return this;
8277 }
8278
8279 /**
8280 * Get a visual hint that only the background image of this notification should be
8281 * displayed, and other semantic content should be hidden. This hint is only applicable
8282 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8283 */
8284 public boolean getHintShowBackgroundOnly() {
8285 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8286 }
8287
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008288 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008289 * Set a hint that this notification's background should not be clipped if possible,
8290 * and should instead be resized to fully display on the screen, retaining the aspect
8291 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008292 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8293 * @return this object for method chaining
8294 */
8295 public WearableExtender setHintAvoidBackgroundClipping(
8296 boolean hintAvoidBackgroundClipping) {
8297 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8298 return this;
8299 }
8300
8301 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008302 * Get a hint that this notification's background should not be clipped if possible,
8303 * and should instead be resized to fully display on the screen, retaining the aspect
8304 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008305 * @return {@code true} if it's ok if the background is clipped on the screen, false
8306 * otherwise. The default value is {@code false} if this was never set.
8307 */
8308 public boolean getHintAvoidBackgroundClipping() {
8309 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8310 }
8311
8312 /**
8313 * Set a hint that the screen should remain on for at least this duration when
8314 * this notification is displayed on the screen.
8315 * @param timeout The requested screen timeout in milliseconds. Can also be either
8316 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8317 * @return this object for method chaining
8318 */
8319 public WearableExtender setHintScreenTimeout(int timeout) {
8320 mHintScreenTimeout = timeout;
8321 return this;
8322 }
8323
8324 /**
8325 * Get the duration, in milliseconds, that the screen should remain on for
8326 * when this notification is displayed.
8327 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8328 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8329 */
8330 public int getHintScreenTimeout() {
8331 return mHintScreenTimeout;
8332 }
8333
Alex Hills9ab3a232016-04-05 14:54:56 -04008334 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008335 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8336 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8337 * qr codes, as well as other simple black-and-white tickets.
8338 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8339 * @return this object for method chaining
8340 */
8341 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8342 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8343 return this;
8344 }
8345
8346 /**
8347 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8348 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8349 * qr codes, as well as other simple black-and-white tickets.
8350 * @return {@code true} if it should be displayed in ambient, false otherwise
8351 * otherwise. The default value is {@code false} if this was never set.
8352 */
8353 public boolean getHintAmbientBigPicture() {
8354 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8355 }
8356
8357 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008358 * Set a hint that this notification's content intent will launch an {@link Activity}
8359 * directly, telling the platform that it can generate the appropriate transitions.
8360 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8361 * an activity and transitions should be generated, false otherwise.
8362 * @return this object for method chaining
8363 */
8364 public WearableExtender setHintContentIntentLaunchesActivity(
8365 boolean hintContentIntentLaunchesActivity) {
8366 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8367 return this;
8368 }
8369
8370 /**
8371 * Get a hint that this notification's content intent will launch an {@link Activity}
8372 * directly, telling the platform that it can generate the appropriate transitions
8373 * @return {@code true} if the content intent will launch an activity and transitions should
8374 * be generated, false otherwise. The default value is {@code false} if this was never set.
8375 */
8376 public boolean getHintContentIntentLaunchesActivity() {
8377 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8378 }
8379
Nadia Benbernou948627e2016-04-14 14:41:08 -04008380 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008381 * Sets the dismissal id for this notification. If a notification is posted with a
8382 * dismissal id, then when that notification is canceled, notifications on other wearables
8383 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008384 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008385 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008386 * @param dismissalId the dismissal id of the notification.
8387 * @return this object for method chaining
8388 */
8389 public WearableExtender setDismissalId(String dismissalId) {
8390 mDismissalId = dismissalId;
8391 return this;
8392 }
8393
8394 /**
8395 * Returns the dismissal id of the notification.
8396 * @return the dismissal id of the notification or null if it has not been set.
8397 */
8398 public String getDismissalId() {
8399 return mDismissalId;
8400 }
8401
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008402 /**
8403 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8404 * posted from a phone to provide finer-grained control on what notifications are bridged
8405 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8406 * Features to Notifications</a> for more information.
8407 * @param bridgeTag the bridge tag of the notification.
8408 * @return this object for method chaining
8409 */
8410 public WearableExtender setBridgeTag(String bridgeTag) {
8411 mBridgeTag = bridgeTag;
8412 return this;
8413 }
8414
8415 /**
8416 * Returns the bridge tag of the notification.
8417 * @return the bridge tag or null if not present.
8418 */
8419 public String getBridgeTag() {
8420 return mBridgeTag;
8421 }
8422
Griff Hazen61a9e862014-05-22 16:05:19 -07008423 private void setFlag(int mask, boolean value) {
8424 if (value) {
8425 mFlags |= mask;
8426 } else {
8427 mFlags &= ~mask;
8428 }
8429 }
8430 }
8431
8432 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008433 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8434 * with car extensions:
8435 *
8436 * <ol>
8437 * <li>Create an {@link Notification.Builder}, setting any desired
8438 * properties.
8439 * <li>Create a {@link CarExtender}.
8440 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8441 * {@link CarExtender}.
8442 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8443 * to apply the extensions to a notification.
8444 * </ol>
8445 *
8446 * <pre class="prettyprint">
8447 * Notification notification = new Notification.Builder(context)
8448 * ...
8449 * .extend(new CarExtender()
8450 * .set*(...))
8451 * .build();
8452 * </pre>
8453 *
8454 * <p>Car extensions can be accessed on an existing notification by using the
8455 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8456 * to access values.
8457 */
8458 public static final class CarExtender implements Extender {
8459 private static final String TAG = "CarExtender";
8460
8461 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8462 private static final String EXTRA_LARGE_ICON = "large_icon";
8463 private static final String EXTRA_CONVERSATION = "car_conversation";
8464 private static final String EXTRA_COLOR = "app_color";
8465
8466 private Bitmap mLargeIcon;
8467 private UnreadConversation mUnreadConversation;
8468 private int mColor = Notification.COLOR_DEFAULT;
8469
8470 /**
8471 * Create a {@link CarExtender} with default options.
8472 */
8473 public CarExtender() {
8474 }
8475
8476 /**
8477 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8478 *
8479 * @param notif The notification from which to copy options.
8480 */
8481 public CarExtender(Notification notif) {
8482 Bundle carBundle = notif.extras == null ?
8483 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8484 if (carBundle != null) {
8485 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8486 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8487
8488 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8489 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8490 }
8491 }
8492
8493 /**
8494 * Apply car extensions to a notification that is being built. This is typically called by
8495 * the {@link Notification.Builder#extend(Notification.Extender)}
8496 * method of {@link Notification.Builder}.
8497 */
8498 @Override
8499 public Notification.Builder extend(Notification.Builder builder) {
8500 Bundle carExtensions = new Bundle();
8501
8502 if (mLargeIcon != null) {
8503 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8504 }
8505 if (mColor != Notification.COLOR_DEFAULT) {
8506 carExtensions.putInt(EXTRA_COLOR, mColor);
8507 }
8508
8509 if (mUnreadConversation != null) {
8510 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8511 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8512 }
8513
8514 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8515 return builder;
8516 }
8517
8518 /**
8519 * Sets the accent color to use when Android Auto presents the notification.
8520 *
8521 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8522 * to accent the displayed notification. However, not all colors are acceptable in an
8523 * automotive setting. This method can be used to override the color provided in the
8524 * notification in such a situation.
8525 */
Tor Norbye80756e32015-03-02 09:39:27 -08008526 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008527 mColor = color;
8528 return this;
8529 }
8530
8531 /**
8532 * Gets the accent color.
8533 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008534 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008535 */
Tor Norbye80756e32015-03-02 09:39:27 -08008536 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008537 public int getColor() {
8538 return mColor;
8539 }
8540
8541 /**
8542 * Sets the large icon of the car notification.
8543 *
8544 * If no large icon is set in the extender, Android Auto will display the icon
8545 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8546 *
8547 * @param largeIcon The large icon to use in the car notification.
8548 * @return This object for method chaining.
8549 */
8550 public CarExtender setLargeIcon(Bitmap largeIcon) {
8551 mLargeIcon = largeIcon;
8552 return this;
8553 }
8554
8555 /**
8556 * Gets the large icon used in this car notification, or null if no icon has been set.
8557 *
8558 * @return The large icon for the car notification.
8559 * @see CarExtender#setLargeIcon
8560 */
8561 public Bitmap getLargeIcon() {
8562 return mLargeIcon;
8563 }
8564
8565 /**
8566 * Sets the unread conversation in a message notification.
8567 *
8568 * @param unreadConversation The unread part of the conversation this notification conveys.
8569 * @return This object for method chaining.
8570 */
8571 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8572 mUnreadConversation = unreadConversation;
8573 return this;
8574 }
8575
8576 /**
8577 * Returns the unread conversation conveyed by this notification.
8578 * @see #setUnreadConversation(UnreadConversation)
8579 */
8580 public UnreadConversation getUnreadConversation() {
8581 return mUnreadConversation;
8582 }
8583
8584 /**
8585 * A class which holds the unread messages from a conversation.
8586 */
8587 public static class UnreadConversation {
8588 private static final String KEY_AUTHOR = "author";
8589 private static final String KEY_TEXT = "text";
8590 private static final String KEY_MESSAGES = "messages";
8591 private static final String KEY_REMOTE_INPUT = "remote_input";
8592 private static final String KEY_ON_REPLY = "on_reply";
8593 private static final String KEY_ON_READ = "on_read";
8594 private static final String KEY_PARTICIPANTS = "participants";
8595 private static final String KEY_TIMESTAMP = "timestamp";
8596
8597 private final String[] mMessages;
8598 private final RemoteInput mRemoteInput;
8599 private final PendingIntent mReplyPendingIntent;
8600 private final PendingIntent mReadPendingIntent;
8601 private final String[] mParticipants;
8602 private final long mLatestTimestamp;
8603
8604 UnreadConversation(String[] messages, RemoteInput remoteInput,
8605 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8606 String[] participants, long latestTimestamp) {
8607 mMessages = messages;
8608 mRemoteInput = remoteInput;
8609 mReadPendingIntent = readPendingIntent;
8610 mReplyPendingIntent = replyPendingIntent;
8611 mParticipants = participants;
8612 mLatestTimestamp = latestTimestamp;
8613 }
8614
8615 /**
8616 * Gets the list of messages conveyed by this notification.
8617 */
8618 public String[] getMessages() {
8619 return mMessages;
8620 }
8621
8622 /**
8623 * Gets the remote input that will be used to convey the response to a message list, or
8624 * null if no such remote input exists.
8625 */
8626 public RemoteInput getRemoteInput() {
8627 return mRemoteInput;
8628 }
8629
8630 /**
8631 * Gets the pending intent that will be triggered when the user replies to this
8632 * notification.
8633 */
8634 public PendingIntent getReplyPendingIntent() {
8635 return mReplyPendingIntent;
8636 }
8637
8638 /**
8639 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8640 * in this object's message list.
8641 */
8642 public PendingIntent getReadPendingIntent() {
8643 return mReadPendingIntent;
8644 }
8645
8646 /**
8647 * Gets the participants in the conversation.
8648 */
8649 public String[] getParticipants() {
8650 return mParticipants;
8651 }
8652
8653 /**
8654 * Gets the firs participant in the conversation.
8655 */
8656 public String getParticipant() {
8657 return mParticipants.length > 0 ? mParticipants[0] : null;
8658 }
8659
8660 /**
8661 * Gets the timestamp of the conversation.
8662 */
8663 public long getLatestTimestamp() {
8664 return mLatestTimestamp;
8665 }
8666
8667 Bundle getBundleForUnreadConversation() {
8668 Bundle b = new Bundle();
8669 String author = null;
8670 if (mParticipants != null && mParticipants.length > 1) {
8671 author = mParticipants[0];
8672 }
8673 Parcelable[] messages = new Parcelable[mMessages.length];
8674 for (int i = 0; i < messages.length; i++) {
8675 Bundle m = new Bundle();
8676 m.putString(KEY_TEXT, mMessages[i]);
8677 m.putString(KEY_AUTHOR, author);
8678 messages[i] = m;
8679 }
8680 b.putParcelableArray(KEY_MESSAGES, messages);
8681 if (mRemoteInput != null) {
8682 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8683 }
8684 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8685 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8686 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8687 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8688 return b;
8689 }
8690
8691 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8692 if (b == null) {
8693 return null;
8694 }
8695 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8696 String[] messages = null;
8697 if (parcelableMessages != null) {
8698 String[] tmp = new String[parcelableMessages.length];
8699 boolean success = true;
8700 for (int i = 0; i < tmp.length; i++) {
8701 if (!(parcelableMessages[i] instanceof Bundle)) {
8702 success = false;
8703 break;
8704 }
8705 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8706 if (tmp[i] == null) {
8707 success = false;
8708 break;
8709 }
8710 }
8711 if (success) {
8712 messages = tmp;
8713 } else {
8714 return null;
8715 }
8716 }
8717
8718 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8719 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8720
8721 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8722
8723 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8724 if (participants == null || participants.length != 1) {
8725 return null;
8726 }
8727
8728 return new UnreadConversation(messages,
8729 remoteInput,
8730 onReply,
8731 onRead,
8732 participants, b.getLong(KEY_TIMESTAMP));
8733 }
8734 };
8735
8736 /**
8737 * Builder class for {@link CarExtender.UnreadConversation} objects.
8738 */
8739 public static class Builder {
8740 private final List<String> mMessages = new ArrayList<String>();
8741 private final String mParticipant;
8742 private RemoteInput mRemoteInput;
8743 private PendingIntent mReadPendingIntent;
8744 private PendingIntent mReplyPendingIntent;
8745 private long mLatestTimestamp;
8746
8747 /**
8748 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8749 *
8750 * @param name The name of the other participant in the conversation.
8751 */
8752 public Builder(String name) {
8753 mParticipant = name;
8754 }
8755
8756 /**
8757 * Appends a new unread message to the list of messages for this conversation.
8758 *
8759 * The messages should be added from oldest to newest.
8760 *
8761 * @param message The text of the new unread message.
8762 * @return This object for method chaining.
8763 */
8764 public Builder addMessage(String message) {
8765 mMessages.add(message);
8766 return this;
8767 }
8768
8769 /**
8770 * Sets the pending intent and remote input which will convey the reply to this
8771 * notification.
8772 *
8773 * @param pendingIntent The pending intent which will be triggered on a reply.
8774 * @param remoteInput The remote input parcelable which will carry the reply.
8775 * @return This object for method chaining.
8776 *
8777 * @see CarExtender.UnreadConversation#getRemoteInput
8778 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8779 */
8780 public Builder setReplyAction(
8781 PendingIntent pendingIntent, RemoteInput remoteInput) {
8782 mRemoteInput = remoteInput;
8783 mReplyPendingIntent = pendingIntent;
8784
8785 return this;
8786 }
8787
8788 /**
8789 * Sets the pending intent that will be sent once the messages in this notification
8790 * are read.
8791 *
8792 * @param pendingIntent The pending intent to use.
8793 * @return This object for method chaining.
8794 */
8795 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8796 mReadPendingIntent = pendingIntent;
8797 return this;
8798 }
8799
8800 /**
8801 * Sets the timestamp of the most recent message in an unread conversation.
8802 *
8803 * If a messaging notification has been posted by your application and has not
8804 * yet been cancelled, posting a later notification with the same id and tag
8805 * but without a newer timestamp may result in Android Auto not displaying a
8806 * heads up notification for the later notification.
8807 *
8808 * @param timestamp The timestamp of the most recent message in the conversation.
8809 * @return This object for method chaining.
8810 */
8811 public Builder setLatestTimestamp(long timestamp) {
8812 mLatestTimestamp = timestamp;
8813 return this;
8814 }
8815
8816 /**
8817 * Builds a new unread conversation object.
8818 *
8819 * @return The new unread conversation object.
8820 */
8821 public UnreadConversation build() {
8822 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8823 String[] participants = { mParticipant };
8824 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8825 mReadPendingIntent, participants, mLatestTimestamp);
8826 }
8827 }
8828 }
8829
8830 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008831 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8832 * with a TV extension:
8833 *
8834 * <ol>
8835 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8836 * <li>Create a {@link TvExtender}.
8837 * <li>Set TV-specific properties using the {@code set} methods of
8838 * {@link TvExtender}.
8839 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8840 * to apply the extension to a notification.
8841 * </ol>
8842 *
8843 * <pre class="prettyprint">
8844 * Notification notification = new Notification.Builder(context)
8845 * ...
8846 * .extend(new TvExtender()
8847 * .set*(...))
8848 * .build();
8849 * </pre>
8850 *
8851 * <p>TV extensions can be accessed on an existing notification by using the
8852 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8853 * to access values.
8854 *
8855 * @hide
8856 */
8857 @SystemApi
8858 public static final class TvExtender implements Extender {
8859 private static final String TAG = "TvExtender";
8860
8861 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8862 private static final String EXTRA_FLAGS = "flags";
8863 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8864 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008865 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008866
8867 // Flags bitwise-ored to mFlags
8868 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8869
8870 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008871 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008872 private PendingIntent mContentIntent;
8873 private PendingIntent mDeleteIntent;
8874
8875 /**
8876 * Create a {@link TvExtender} with default options.
8877 */
8878 public TvExtender() {
8879 mFlags = FLAG_AVAILABLE_ON_TV;
8880 }
8881
8882 /**
8883 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8884 *
8885 * @param notif The notification from which to copy options.
8886 */
8887 public TvExtender(Notification notif) {
8888 Bundle bundle = notif.extras == null ?
8889 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8890 if (bundle != null) {
8891 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008892 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008893 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8894 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8895 }
8896 }
8897
8898 /**
8899 * Apply a TV extension to a notification that is being built. This is typically called by
8900 * the {@link Notification.Builder#extend(Notification.Extender)}
8901 * method of {@link Notification.Builder}.
8902 */
8903 @Override
8904 public Notification.Builder extend(Notification.Builder builder) {
8905 Bundle bundle = new Bundle();
8906
8907 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008908 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008909 if (mContentIntent != null) {
8910 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8911 }
8912
8913 if (mDeleteIntent != null) {
8914 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8915 }
8916
8917 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8918 return builder;
8919 }
8920
8921 /**
8922 * Returns true if this notification should be shown on TV. This method return true
8923 * if the notification was extended with a TvExtender.
8924 */
8925 public boolean isAvailableOnTv() {
8926 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8927 }
8928
8929 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008930 * Specifies the channel the notification should be delivered on when shown on TV.
8931 * It can be different from the channel that the notification is delivered to when
8932 * posting on a non-TV device.
8933 */
8934 public TvExtender setChannel(String channelId) {
8935 mChannelId = channelId;
8936 return this;
8937 }
8938
8939 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008940 * Specifies the channel the notification should be delivered on when shown on TV.
8941 * It can be different from the channel that the notification is delivered to when
8942 * posting on a non-TV device.
8943 */
8944 public TvExtender setChannelId(String channelId) {
8945 mChannelId = channelId;
8946 return this;
8947 }
8948
Jeff Sharkey000ce802017-04-29 13:13:27 -06008949 /** @removed */
8950 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008951 public String getChannel() {
8952 return mChannelId;
8953 }
8954
8955 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008956 * Returns the id of the channel this notification posts to on TV.
8957 */
8958 public String getChannelId() {
8959 return mChannelId;
8960 }
8961
8962 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008963 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8964 * If provided, it is used instead of the content intent specified
8965 * at the level of Notification.
8966 */
8967 public TvExtender setContentIntent(PendingIntent intent) {
8968 mContentIntent = intent;
8969 return this;
8970 }
8971
8972 /**
8973 * Returns the TV-specific content intent. If this method returns null, the
8974 * main content intent on the notification should be used.
8975 *
8976 * @see {@link Notification#contentIntent}
8977 */
8978 public PendingIntent getContentIntent() {
8979 return mContentIntent;
8980 }
8981
8982 /**
8983 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8984 * by the user on TV. If provided, it is used instead of the delete intent specified
8985 * at the level of Notification.
8986 */
8987 public TvExtender setDeleteIntent(PendingIntent intent) {
8988 mDeleteIntent = intent;
8989 return this;
8990 }
8991
8992 /**
8993 * Returns the TV-specific delete intent. If this method returns null, the
8994 * main delete intent on the notification should be used.
8995 *
8996 * @see {@link Notification#deleteIntent}
8997 */
8998 public PendingIntent getDeleteIntent() {
8999 return mDeleteIntent;
9000 }
9001 }
9002
9003 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009004 * Get an array of Notification objects from a parcelable array bundle field.
9005 * Update the bundle to have a typed array so fetches in the future don't need
9006 * to do an array copy.
9007 */
9008 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9009 Parcelable[] array = bundle.getParcelableArray(key);
9010 if (array instanceof Notification[] || array == null) {
9011 return (Notification[]) array;
9012 }
9013 Notification[] typedArray = Arrays.copyOf(array, array.length,
9014 Notification[].class);
9015 bundle.putParcelableArray(key, typedArray);
9016 return typedArray;
9017 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009018
9019 private static class BuilderRemoteViews extends RemoteViews {
9020 public BuilderRemoteViews(Parcel parcel) {
9021 super(parcel);
9022 }
9023
Kenny Guy77320062014-08-27 21:37:15 +01009024 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9025 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009026 }
9027
9028 @Override
9029 public BuilderRemoteViews clone() {
9030 Parcel p = Parcel.obtain();
9031 writeToParcel(p, 0);
9032 p.setDataPosition(0);
9033 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9034 p.recycle();
9035 return brv;
9036 }
9037 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009038
9039 private static class StandardTemplateParams {
9040 boolean hasProgress = true;
9041 boolean ambient = false;
9042 CharSequence title;
9043 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009044 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009045 boolean hideLargeIcon;
9046 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009047
9048 final StandardTemplateParams reset() {
9049 hasProgress = true;
9050 ambient = false;
9051 title = null;
9052 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009053 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009054 return this;
9055 }
9056
9057 final StandardTemplateParams hasProgress(boolean hasProgress) {
9058 this.hasProgress = hasProgress;
9059 return this;
9060 }
9061
9062 final StandardTemplateParams title(CharSequence title) {
9063 this.title = title;
9064 return this;
9065 }
9066
9067 final StandardTemplateParams text(CharSequence text) {
9068 this.text = text;
9069 return this;
9070 }
9071
Selim Cinekafeed292017-12-12 17:32:44 -08009072 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9073 this.headerTextSecondary = text;
9074 return this;
9075 }
9076
Selim Cinek88188f22017-09-19 16:46:56 -07009077 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9078 this.alwaysShowReply = alwaysShowReply;
9079 return this;
9080 }
9081
9082 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9083 this.hideLargeIcon = hideLargeIcon;
9084 return this;
9085 }
9086
Adrian Roos70d7aa32017-01-11 15:39:06 -08009087 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009088 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009089 this.ambient = ambient;
9090 return this;
9091 }
9092
9093 final StandardTemplateParams fillTextsFrom(Builder b) {
9094 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009095 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9096
9097 // Big text notifications should contain their content when viewed in ambient mode.
9098 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9099 if (!ambient || TextUtils.isEmpty(text)) {
9100 text = extras.getCharSequence(EXTRA_TEXT);
9101 }
9102 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009103 return this;
9104 }
9105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106}