blob: 8393caa33e315fad04e7465fc2bc766a64bf0b76 [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
Ricardo Loo Forondaf8f6d0a2018-01-25 08:42:43 -0800496 * you pass are honored exactly. Use the system defaults if possible
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 * 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
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001058 * in the background when the notification is selected. Used on television platforms.
1059 * The URI must point to an image stream suitable for passing into
Jose Limae9e3b3b2014-05-18 23:44:50 -07001060 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001061 * BitmapFactory.decodeStream}; all other content types will be ignored.
Jose Limae9e3b3b2014-05-18 23:44:50 -07001062 */
1063 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1064
1065 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001066 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001067 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001068 * {@link android.app.Notification.MediaStyle} notification.
1069 */
1070 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1071
1072 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001073 * {@link #extras} key: the indices of actions to be shown in the compact view,
1074 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1075 */
1076 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1077
Christoph Studer943aa672014-08-03 20:31:16 +02001078 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001079 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1080 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001081 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1082 * {@link CharSequence}
Selim Cinekcb8b9852017-12-15 18:01:52 -08001083 *
1084 * @deprecated use {@link #EXTRA_MESSAGING_PERSON}
Alex Hillsfc737de2016-03-23 17:33:02 -04001085 */
1086 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1087
1088 /**
Selim Cinekcb8b9852017-12-15 18:01:52 -08001089 * {@link #extras} key: the person to be displayed for all messages sent by the user including
1090 * direct replies
1091 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1092 * {@link Person}
1093 */
1094 public static final String EXTRA_MESSAGING_PERSON = "android.messagingUser";
1095
1096 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001097 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001098 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001099 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001100 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001101
1102 /**
1103 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1104 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001105 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1106 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001107 */
1108 public static final String EXTRA_MESSAGES = "android.messages";
1109
1110 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001111 * {@link #extras} key: an array of
1112 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1113 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1114 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1115 * array of bundles.
1116 */
1117 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1118
1119 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08001120 * {@link #extras} key: whether the {@link android.app.Notification.MessagingStyle} notification
1121 * represents a group conversation.
1122 */
1123 public static final String EXTRA_IS_GROUP_CONVERSATION = "android.isGroupConversation";
1124
1125 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001126 * {@link #extras} key: whether the notification should be colorized as
1127 * supplied to {@link Builder#setColorized(boolean)}}.
1128 */
1129 public static final String EXTRA_COLORIZED = "android.colorized";
1130
1131 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001132 * @hide
1133 */
1134 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1135
Selim Cinek247fa012016-02-18 09:50:48 -08001136 /**
1137 * @hide
1138 */
1139 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1140
Shane Brennan472a3b32016-12-12 15:28:10 -08001141 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001142 * @hide
1143 */
1144 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1145
1146 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001147 * {@link #extras} key: the audio contents of this notification.
1148 *
1149 * This is for use when rendering the notification on an audio-focused interface;
1150 * the audio contents are a complete sound sample that contains the contents/body of the
1151 * notification. This may be used in substitute of a Text-to-Speech reading of the
1152 * notification. For example if the notification represents a voice message this should point
1153 * to the audio of that message.
1154 *
1155 * The data stored under this key should be a String representation of a Uri that contains the
1156 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1157 *
1158 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1159 * has a field for holding data URI. That field can be used for audio.
1160 * See {@code Message#setData}.
1161 *
1162 * Example usage:
1163 * <pre>
1164 * {@code
1165 * Notification.Builder myBuilder = (build your Notification as normal);
1166 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1167 * }
1168 * </pre>
1169 */
1170 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1171
Dan Sandler80eaa592016-04-14 23:34:54 -04001172 /** @hide */
1173 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001174 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001175 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1176
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001177 /**
1178 * This is set on the notification shown by the activity manager about all apps
1179 * running in the background. It indicates that the notification should be shown
1180 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1181 * notification currently visible to the user. This is a string array of all
1182 * package names of the apps.
1183 * @hide
1184 */
1185 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1186
Dan Sandlerd63f9322015-05-06 15:18:49 -04001187 private Icon mSmallIcon;
1188 private Icon mLargeIcon;
1189
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001190 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001191 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001192
Julia Reynolds13d898c2017-02-02 12:22:05 -05001193 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001194 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001195
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001196 /** @hide */
1197 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1198 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1199 })
1200 @Retention(RetentionPolicy.SOURCE)
1201 public @interface GroupAlertBehavior {}
1202
1203 /**
1204 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1205 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1206 * notification will not be muted when it is in a group.
1207 */
1208 public static final int GROUP_ALERT_ALL = 0;
1209
1210 /**
1211 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1212 * notification in a group should be silenced (no sound or vibration) even if they are posted
1213 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001214 * mute this notification if this notification is a group child. This must be applied to all
1215 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001216 *
1217 * <p> For example, you might want to use this constant if you post a number of children
1218 * notifications at once (say, after a periodic sync), and only need to notify the user
1219 * audibly once.
1220 */
1221 public static final int GROUP_ALERT_SUMMARY = 1;
1222
1223 /**
1224 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1225 * notification in a group should be silenced (no sound or vibration) even if they are
1226 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1227 * to mute this notification if this notification is a group summary.
1228 *
1229 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001230 * in your group have content and the summary is only used to visually group notifications
1231 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001232 */
1233 public static final int GROUP_ALERT_CHILDREN = 2;
1234
Julia Reynolds2f431e22017-06-07 14:12:09 +00001235 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001236
Julia Reynolds13d898c2017-02-02 12:22:05 -05001237 /**
1238 * If this notification is being shown as a badge, always show as a number.
1239 */
1240 public static final int BADGE_ICON_NONE = 0;
1241
1242 /**
1243 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1244 * represent this notification.
1245 */
1246 public static final int BADGE_ICON_SMALL = 1;
1247
1248 /**
1249 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1250 * represent this notification.
1251 */
1252 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001253 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001254
Chris Wren51c75102013-07-16 20:49:17 -04001255 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001256 * Structure to encapsulate a named action that can be shown as part of this notification.
1257 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1258 * selected by the user.
1259 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001260 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1261 * or {@link Notification.Builder#addAction(Notification.Action)}
1262 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001264 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001265 /**
1266 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1267 * {@link RemoteInput}s.
1268 *
1269 * This is intended for {@link RemoteInput}s that only accept data, meaning
1270 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1271 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1272 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1273 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1274 *
1275 * You can test if a RemoteInput matches these constraints using
1276 * {@link RemoteInput#isDataOnly}.
1277 */
1278 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1279
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001280 /**
1281 * {@link }: No semantic action defined.
1282 */
1283 public static final int SEMANTIC_ACTION_NONE = 0;
1284
1285 /**
1286 * {@code SemanticAction}: Reply to a conversation, chat, group, or wherever replies
1287 * may be appropriate.
1288 */
1289 public static final int SEMANTIC_ACTION_REPLY = 1;
1290
1291 /**
1292 * {@code SemanticAction}: Mark content as read.
1293 */
1294 public static final int SEMANTIC_ACTION_MARK_AS_READ = 2;
1295
1296 /**
1297 * {@code SemanticAction}: Mark content as unread.
1298 */
1299 public static final int SEMANTIC_ACTION_MARK_AS_UNREAD = 3;
1300
1301 /**
1302 * {@code SemanticAction}: Delete the content associated with the notification. This
1303 * could mean deleting an email, message, etc.
1304 */
1305 public static final int SEMANTIC_ACTION_DELETE = 4;
1306
1307 /**
1308 * {@code SemanticAction}: Archive the content associated with the notification. This
1309 * could mean archiving an email, message, etc.
1310 */
1311 public static final int SEMANTIC_ACTION_ARCHIVE = 5;
1312
1313 /**
1314 * {@code SemanticAction}: Mute the content associated with the notification. This could
1315 * mean silencing a conversation or currently playing media.
1316 */
1317 public static final int SEMANTIC_ACTION_MUTE = 6;
1318
1319 /**
1320 * {@code SemanticAction}: Unmute the content associated with the notification. This could
1321 * mean un-silencing a conversation or currently playing media.
1322 */
1323 public static final int SEMANTIC_ACTION_UNMUTE = 7;
1324
1325 /**
1326 * {@code SemanticAction}: Mark content with a thumbs up.
1327 */
1328 public static final int SEMANTIC_ACTION_THUMBS_UP = 8;
1329
1330 /**
1331 * {@code SemanticAction}: Mark content with a thumbs down.
1332 */
1333 public static final int SEMANTIC_ACTION_THUMBS_DOWN = 9;
1334
1335
Griff Hazen959591e2014-05-15 22:26:18 -07001336 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001337 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001338 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001339 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001340 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001341
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001342 /**
1343 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001344 *
1345 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 */
Dan Sandler86647982015-05-13 23:41:13 -04001347 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001348 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001349
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001350 /**
1351 * Title of the action.
1352 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001353 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001354
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001355 /**
1356 * Intent to send when the user invokes this action. May be null, in which case the action
1357 * may be rendered in a disabled presentation by the system UI.
1358 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001359 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001360
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001361 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001362 if (in.readInt() != 0) {
1363 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001364 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1365 icon = mIcon.getResId();
1366 }
Dan Sandler86647982015-05-13 23:41:13 -04001367 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001368 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1369 if (in.readInt() == 1) {
1370 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1371 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001372 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001373 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001374 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001375 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001376 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001377
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001378 /**
Dan Sandler86647982015-05-13 23:41:13 -04001379 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001380 */
Dan Sandler86647982015-05-13 23:41:13 -04001381 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001382 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001383 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1384 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001385 }
1386
Adrian Roos7af53622016-10-12 13:44:05 -07001387 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001388 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001389 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1390 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001391 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001392 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1393 this.icon = icon.getResId();
1394 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001395 this.title = title;
1396 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001397 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001398 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001399 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001400 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001401 }
1402
1403 /**
Dan Sandler86647982015-05-13 23:41:13 -04001404 * Return an icon representing the action.
1405 */
1406 public Icon getIcon() {
1407 if (mIcon == null && icon != 0) {
1408 // you snuck an icon in here without using the builder; let's try to keep it
1409 mIcon = Icon.createWithResource("", icon);
1410 }
1411 return mIcon;
1412 }
1413
1414 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001415 * Get additional metadata carried around with this Action.
1416 */
1417 public Bundle getExtras() {
1418 return mExtras;
1419 }
1420
1421 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001422 * Return whether the platform should automatically generate possible replies for this
1423 * {@link Action}
1424 */
1425 public boolean getAllowGeneratedReplies() {
1426 return mAllowGeneratedReplies;
1427 }
1428
1429 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001430 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001431 * May return null if no remote inputs were added. Only returns inputs which accept
1432 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001433 */
1434 public RemoteInput[] getRemoteInputs() {
1435 return mRemoteInputs;
1436 }
1437
1438 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001439 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1440 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1441 * (eg. reply, mark as read, delete, etc).
1442 */
1443 public @SemanticAction int getSemanticAction() {
1444 return mSemanticAction;
1445 }
1446
1447 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001448 * Get the list of inputs to be collected from the user that ONLY accept data when this
1449 * action is sent. These remote inputs are guaranteed to return true on a call to
1450 * {@link RemoteInput#isDataOnly}.
1451 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001452 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001453 *
1454 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1455 * of non-textual RemoteInputs do not access these remote inputs.
1456 */
1457 public RemoteInput[] getDataOnlyRemoteInputs() {
1458 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1459 }
1460
1461 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001462 * Builder class for {@link Action} objects.
1463 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001464 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001465 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001466 private final CharSequence mTitle;
1467 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001468 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001469 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001470 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001471 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001472
1473 /**
1474 * Construct a new builder for {@link Action} object.
1475 * @param icon icon to show for this action
1476 * @param title the title of the action
1477 * @param intent the {@link PendingIntent} to fire when users trigger this action
1478 */
Dan Sandler86647982015-05-13 23:41:13 -04001479 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001480 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001481 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001482 }
1483
1484 /**
1485 * Construct a new builder for {@link Action} object.
1486 * @param icon icon to show for this action
1487 * @param title the title of the action
1488 * @param intent the {@link PendingIntent} to fire when users trigger this action
1489 */
1490 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001491 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001492 }
1493
1494 /**
1495 * Construct a new builder for {@link Action} object using the fields from an
1496 * {@link Action}.
1497 * @param action the action to read fields from.
1498 */
1499 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001500 this(action.getIcon(), action.title, action.actionIntent,
1501 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001502 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001503 }
1504
Dan Sandler86647982015-05-13 23:41:13 -04001505 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001506 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1507 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001508 mIcon = icon;
1509 mTitle = title;
1510 mIntent = intent;
1511 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001512 if (remoteInputs != null) {
1513 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1514 Collections.addAll(mRemoteInputs, remoteInputs);
1515 }
Adrian Roos7af53622016-10-12 13:44:05 -07001516 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001517 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001518 }
1519
1520 /**
1521 * Merge additional metadata into this builder.
1522 *
1523 * <p>Values within the Bundle will replace existing extras values in this Builder.
1524 *
1525 * @see Notification.Action#extras
1526 */
1527 public Builder addExtras(Bundle extras) {
1528 if (extras != null) {
1529 mExtras.putAll(extras);
1530 }
1531 return this;
1532 }
1533
1534 /**
1535 * Get the metadata Bundle used by this Builder.
1536 *
1537 * <p>The returned Bundle is shared with this Builder.
1538 */
1539 public Bundle getExtras() {
1540 return mExtras;
1541 }
1542
1543 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001544 * Add an input to be collected from the user when this action is sent.
1545 * Response values can be retrieved from the fired intent by using the
1546 * {@link RemoteInput#getResultsFromIntent} function.
1547 * @param remoteInput a {@link RemoteInput} to add to the action
1548 * @return this object for method chaining
1549 */
1550 public Builder addRemoteInput(RemoteInput remoteInput) {
1551 if (mRemoteInputs == null) {
1552 mRemoteInputs = new ArrayList<RemoteInput>();
1553 }
1554 mRemoteInputs.add(remoteInput);
1555 return this;
1556 }
1557
1558 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001559 * Set whether the platform should automatically generate possible replies to add to
1560 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1561 * {@link RemoteInput}, this has no effect.
1562 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1563 * otherwise
1564 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001565 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001566 */
1567 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1568 mAllowGeneratedReplies = allowGeneratedReplies;
1569 return this;
1570 }
1571
1572 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001573 * Sets the {@code SemanticAction} for this {@link Action}. A
1574 * {@code SemanticAction} denotes what an {@link Action}'s
1575 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1576 * @param semanticAction a SemanticAction defined within {@link Action} with
1577 * {@code SEMANTIC_ACTION_} prefixes
1578 * @return this object for method chaining
1579 */
1580 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1581 mSemanticAction = semanticAction;
1582 return this;
1583 }
1584
1585 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001586 * Apply an extender to this action builder. Extenders may be used to add
1587 * metadata or change options on this builder.
1588 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001589 public Builder extend(Extender extender) {
1590 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001591 return this;
1592 }
1593
1594 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001595 * Combine all of the options that have been set and return a new {@link Action}
1596 * object.
1597 * @return the built action
1598 */
1599 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001600 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1601 RemoteInput[] previousDataInputs =
1602 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001603 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001604 for (RemoteInput input : previousDataInputs) {
1605 dataOnlyInputs.add(input);
1606 }
1607 }
1608 List<RemoteInput> textInputs = new ArrayList<>();
1609 if (mRemoteInputs != null) {
1610 for (RemoteInput input : mRemoteInputs) {
1611 if (input.isDataOnly()) {
1612 dataOnlyInputs.add(input);
1613 } else {
1614 textInputs.add(input);
1615 }
1616 }
1617 }
1618 if (!dataOnlyInputs.isEmpty()) {
1619 RemoteInput[] dataInputsArr =
1620 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1621 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1622 }
1623 RemoteInput[] textInputsArr = textInputs.isEmpty()
1624 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1625 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001626 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001627 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001628 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001629
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001630 @Override
1631 public Action clone() {
1632 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001633 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001634 title,
1635 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001636 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001637 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001638 getAllowGeneratedReplies(),
1639 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001640 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001641
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001642 @Override
1643 public int describeContents() {
1644 return 0;
1645 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001646
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001647 @Override
1648 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001649 final Icon ic = getIcon();
1650 if (ic != null) {
1651 out.writeInt(1);
1652 ic.writeToParcel(out, 0);
1653 } else {
1654 out.writeInt(0);
1655 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001656 TextUtils.writeToParcel(title, out, flags);
1657 if (actionIntent != null) {
1658 out.writeInt(1);
1659 actionIntent.writeToParcel(out, flags);
1660 } else {
1661 out.writeInt(0);
1662 }
Griff Hazen959591e2014-05-15 22:26:18 -07001663 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001664 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001665 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001666 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001667 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001668
Griff Hazen959591e2014-05-15 22:26:18 -07001669 public static final Parcelable.Creator<Action> CREATOR =
1670 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001671 public Action createFromParcel(Parcel in) {
1672 return new Action(in);
1673 }
1674 public Action[] newArray(int size) {
1675 return new Action[size];
1676 }
1677 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001678
1679 /**
1680 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1681 * metadata or change options on an action builder.
1682 */
1683 public interface Extender {
1684 /**
1685 * Apply this extender to a notification action builder.
1686 * @param builder the builder to be modified.
1687 * @return the build object for chaining.
1688 */
1689 public Builder extend(Builder builder);
1690 }
1691
1692 /**
1693 * Wearable extender for notification actions. To add extensions to an action,
1694 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1695 * the {@code WearableExtender()} constructor and apply it to a
1696 * {@link android.app.Notification.Action.Builder} using
1697 * {@link android.app.Notification.Action.Builder#extend}.
1698 *
1699 * <pre class="prettyprint">
1700 * Notification.Action action = new Notification.Action.Builder(
1701 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001702 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001703 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001704 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001705 */
1706 public static final class WearableExtender implements Extender {
1707 /** Notification action extra which contains wearable extensions */
1708 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1709
Pete Gastaf6781d2014-10-07 15:17:05 -04001710 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001711 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001712 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1713 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1714 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001715
1716 // Flags bitwise-ored to mFlags
1717 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001718 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001719 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001720
1721 // Default value for flags integer
1722 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1723
1724 private int mFlags = DEFAULT_FLAGS;
1725
Pete Gastaf6781d2014-10-07 15:17:05 -04001726 private CharSequence mInProgressLabel;
1727 private CharSequence mConfirmLabel;
1728 private CharSequence mCancelLabel;
1729
Griff Hazen61a9e862014-05-22 16:05:19 -07001730 /**
1731 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1732 * options.
1733 */
1734 public WearableExtender() {
1735 }
1736
1737 /**
1738 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1739 * wearable options present in an existing notification action.
1740 * @param action the notification action to inspect.
1741 */
1742 public WearableExtender(Action action) {
1743 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1744 if (wearableBundle != null) {
1745 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001746 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1747 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1748 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001749 }
1750 }
1751
1752 /**
1753 * Apply wearable extensions to a notification action that is being built. This is
1754 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1755 * method of {@link android.app.Notification.Action.Builder}.
1756 */
1757 @Override
1758 public Action.Builder extend(Action.Builder builder) {
1759 Bundle wearableBundle = new Bundle();
1760
1761 if (mFlags != DEFAULT_FLAGS) {
1762 wearableBundle.putInt(KEY_FLAGS, mFlags);
1763 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001764 if (mInProgressLabel != null) {
1765 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1766 }
1767 if (mConfirmLabel != null) {
1768 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1769 }
1770 if (mCancelLabel != null) {
1771 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1772 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001773
1774 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1775 return builder;
1776 }
1777
1778 @Override
1779 public WearableExtender clone() {
1780 WearableExtender that = new WearableExtender();
1781 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001782 that.mInProgressLabel = this.mInProgressLabel;
1783 that.mConfirmLabel = this.mConfirmLabel;
1784 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001785 return that;
1786 }
1787
1788 /**
1789 * Set whether this action is available when the wearable device is not connected to
1790 * a companion device. The user can still trigger this action when the wearable device is
1791 * offline, but a visual hint will indicate that the action may not be available.
1792 * Defaults to true.
1793 */
1794 public WearableExtender setAvailableOffline(boolean availableOffline) {
1795 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1796 return this;
1797 }
1798
1799 /**
1800 * Get whether this action is available when the wearable device is not connected to
1801 * a companion device. The user can still trigger this action when the wearable device is
1802 * offline, but a visual hint will indicate that the action may not be available.
1803 * Defaults to true.
1804 */
1805 public boolean isAvailableOffline() {
1806 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1807 }
1808
1809 private void setFlag(int mask, boolean value) {
1810 if (value) {
1811 mFlags |= mask;
1812 } else {
1813 mFlags &= ~mask;
1814 }
1815 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001816
1817 /**
1818 * Set a label to display while the wearable is preparing to automatically execute the
1819 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1820 *
1821 * @param label the label to display while the action is being prepared to execute
1822 * @return this object for method chaining
1823 */
1824 public WearableExtender setInProgressLabel(CharSequence label) {
1825 mInProgressLabel = label;
1826 return this;
1827 }
1828
1829 /**
1830 * Get the label to display while the wearable is preparing to automatically execute
1831 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1832 *
1833 * @return the label to display while the action is being prepared to execute
1834 */
1835 public CharSequence getInProgressLabel() {
1836 return mInProgressLabel;
1837 }
1838
1839 /**
1840 * Set a label to display to confirm that the action should be executed.
1841 * This is usually an imperative verb like "Send".
1842 *
1843 * @param label the label to confirm the action should be executed
1844 * @return this object for method chaining
1845 */
1846 public WearableExtender setConfirmLabel(CharSequence label) {
1847 mConfirmLabel = label;
1848 return this;
1849 }
1850
1851 /**
1852 * Get the label to display to confirm that the action should be executed.
1853 * This is usually an imperative verb like "Send".
1854 *
1855 * @return the label to confirm the action should be executed
1856 */
1857 public CharSequence getConfirmLabel() {
1858 return mConfirmLabel;
1859 }
1860
1861 /**
1862 * Set a label to display to cancel the action.
1863 * This is usually an imperative verb, like "Cancel".
1864 *
1865 * @param label the label to display to cancel the action
1866 * @return this object for method chaining
1867 */
1868 public WearableExtender setCancelLabel(CharSequence label) {
1869 mCancelLabel = label;
1870 return this;
1871 }
1872
1873 /**
1874 * Get the label to display to cancel the action.
1875 * This is usually an imperative verb like "Cancel".
1876 *
1877 * @return the label to display to cancel the action
1878 */
1879 public CharSequence getCancelLabel() {
1880 return mCancelLabel;
1881 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001882
1883 /**
1884 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1885 * platform that it can generate the appropriate transitions.
1886 * @param hintLaunchesActivity {@code true} if the content intent will launch
1887 * an activity and transitions should be generated, false otherwise.
1888 * @return this object for method chaining
1889 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001890 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001891 boolean hintLaunchesActivity) {
1892 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1893 return this;
1894 }
1895
1896 /**
1897 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1898 * platform that it can generate the appropriate transitions
1899 * @return {@code true} if the content intent will launch an activity and transitions
1900 * should be generated, false otherwise. The default value is {@code false} if this was
1901 * never set.
1902 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001903 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001904 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1905 }
Alex Hills9f087612016-06-07 09:08:59 -04001906
1907 /**
1908 * Set a hint that this Action should be displayed inline.
1909 *
1910 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1911 * otherwise
1912 * @return this object for method chaining
1913 */
1914 public WearableExtender setHintDisplayActionInline(
1915 boolean hintDisplayInline) {
1916 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1917 return this;
1918 }
1919
1920 /**
1921 * Get a hint that this Action should be displayed inline.
1922 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001923 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001924 * otherwise. The default value is {@code false} if this was never set.
1925 */
1926 public boolean getHintDisplayActionInline() {
1927 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1928 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001929 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001930
1931 /**
1932 * Provides meaning to an {@link Action} that hints at what the associated
1933 * {@link PendingIntent} will do. For example, an {@link Action} with a
1934 * {@link PendingIntent} that replies to a text message notification may have the
1935 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1936 *
1937 * @hide
1938 */
1939 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1940 SEMANTIC_ACTION_NONE,
1941 SEMANTIC_ACTION_REPLY,
1942 SEMANTIC_ACTION_MARK_AS_READ,
1943 SEMANTIC_ACTION_MARK_AS_UNREAD,
1944 SEMANTIC_ACTION_DELETE,
1945 SEMANTIC_ACTION_ARCHIVE,
1946 SEMANTIC_ACTION_MUTE,
1947 SEMANTIC_ACTION_UNMUTE,
1948 SEMANTIC_ACTION_THUMBS_UP,
1949 SEMANTIC_ACTION_THUMBS_DOWN
1950 })
1951 @Retention(RetentionPolicy.SOURCE)
1952 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001953 }
1954
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001955 /**
1956 * Array of all {@link Action} structures attached to this notification by
1957 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1958 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1959 * interface for invoking actions.
1960 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001961 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001962
1963 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001964 * Replacement version of this notification whose content will be shown
1965 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1966 * and {@link #VISIBILITY_PUBLIC}.
1967 */
1968 public Notification publicVersion;
1969
1970 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001971 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001972 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 */
1974 public Notification()
1975 {
1976 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001977 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001978 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980
1981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 * @hide
1983 */
1984 public Notification(Context context, int icon, CharSequence tickerText, long when,
1985 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1986 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001987 new Builder(context)
1988 .setWhen(when)
1989 .setSmallIcon(icon)
1990 .setTicker(tickerText)
1991 .setContentTitle(contentTitle)
1992 .setContentText(contentText)
1993 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1994 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996
1997 /**
1998 * Constructs a Notification object with the information needed to
1999 * have a status bar icon without the standard expanded view.
2000 *
2001 * @param icon The resource id of the icon to put in the status bar.
2002 * @param tickerText The text that flows by in the status bar when the notification first
2003 * activates.
2004 * @param when The time to show in the time field. In the System.currentTimeMillis
2005 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002006 *
2007 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002009 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 public Notification(int icon, CharSequence tickerText, long when)
2011 {
2012 this.icon = icon;
2013 this.tickerText = tickerText;
2014 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002015 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 }
2017
2018 /**
2019 * Unflatten the notification from a parcel.
2020 */
Svet Ganovddb94882016-06-23 19:55:24 -07002021 @SuppressWarnings("unchecked")
2022 public Notification(Parcel parcel) {
2023 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2024 // intents in extras are always written as the last entry.
2025 readFromParcelImpl(parcel);
2026 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002027 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002028 }
2029
2030 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 {
2032 int version = parcel.readInt();
2033
Adrian Roosfb921842017-10-26 14:49:56 +02002034 mWhitelistToken = parcel.readStrongBinder();
2035 if (mWhitelistToken == null) {
2036 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002037 }
2038 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002039 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002042 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002043 if (parcel.readInt() != 0) {
2044 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002045 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2046 icon = mSmallIcon.getResId();
2047 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 number = parcel.readInt();
2050 if (parcel.readInt() != 0) {
2051 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2052 }
2053 if (parcel.readInt() != 0) {
2054 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2055 }
2056 if (parcel.readInt() != 0) {
2057 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2058 }
2059 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002060 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002061 }
2062 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2064 }
Joe Onorato561d3852010-11-20 18:09:34 -08002065 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002066 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 defaults = parcel.readInt();
2069 flags = parcel.readInt();
2070 if (parcel.readInt() != 0) {
2071 sound = Uri.CREATOR.createFromParcel(parcel);
2072 }
2073
2074 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002075 if (parcel.readInt() != 0) {
2076 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 vibrate = parcel.createLongArray();
2079 ledARGB = parcel.readInt();
2080 ledOnMS = parcel.readInt();
2081 ledOffMS = parcel.readInt();
2082 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002083
2084 if (parcel.readInt() != 0) {
2085 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2086 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087
2088 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002089
John Spurlockfd7f1e02014-03-18 16:41:57 -04002090 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002091
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002092 mGroupKey = parcel.readString();
2093
2094 mSortKey = parcel.readString();
2095
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002096 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002097 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002098
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002099 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2100
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002101 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002102 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2103 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002104
Chris Wren8fd39ec2014-02-27 17:43:26 -05002105 if (parcel.readInt() != 0) {
2106 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2107 }
2108
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002109 visibility = parcel.readInt();
2110
2111 if (parcel.readInt() != 0) {
2112 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2113 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002114
2115 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002116
2117 if (parcel.readInt() != 0) {
2118 mChannelId = parcel.readString();
2119 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002120 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002121
2122 if (parcel.readInt() != 0) {
2123 mShortcutId = parcel.readString();
2124 }
2125
2126 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002127
2128 if (parcel.readInt() != 0) {
2129 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2130 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002131
2132 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
2134
Andy Stadler110988c2010-12-03 14:29:16 -08002135 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002136 public Notification clone() {
2137 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002138 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002139 return that;
2140 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002141
Daniel Sandler1a497d32013-04-18 14:52:45 -04002142 /**
2143 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2144 * of this into that.
2145 * @hide
2146 */
2147 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002148 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002149 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002150 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002151 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002152 that.number = this.number;
2153
2154 // PendingIntents are global, so there's no reason (or way) to clone them.
2155 that.contentIntent = this.contentIntent;
2156 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002157 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002158
2159 if (this.tickerText != null) {
2160 that.tickerText = this.tickerText.toString();
2161 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002162 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002163 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002164 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002165 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002166 that.contentView = this.contentView.clone();
2167 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002168 if (heavy && this.mLargeIcon != null) {
2169 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002170 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002171 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002172 that.sound = this.sound; // android.net.Uri is immutable
2173 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002174 if (this.audioAttributes != null) {
2175 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2176 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002177
2178 final long[] vibrate = this.vibrate;
2179 if (vibrate != null) {
2180 final int N = vibrate.length;
2181 final long[] vib = that.vibrate = new long[N];
2182 System.arraycopy(vibrate, 0, vib, 0, N);
2183 }
2184
2185 that.ledARGB = this.ledARGB;
2186 that.ledOnMS = this.ledOnMS;
2187 that.ledOffMS = this.ledOffMS;
2188 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002189
Joe Onorato18e69df2010-05-17 22:26:12 -07002190 that.flags = this.flags;
2191
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002192 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002193
John Spurlockfd7f1e02014-03-18 16:41:57 -04002194 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002195
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002196 that.mGroupKey = this.mGroupKey;
2197
2198 that.mSortKey = this.mSortKey;
2199
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002200 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002201 try {
2202 that.extras = new Bundle(this.extras);
2203 // will unparcel
2204 that.extras.size();
2205 } catch (BadParcelableException e) {
2206 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2207 that.extras = null;
2208 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002209 }
2210
Felipe Lemedd85da62016-06-28 11:29:54 -07002211 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2212 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002213 }
2214
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002215 if (this.actions != null) {
2216 that.actions = new Action[this.actions.length];
2217 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002218 if ( this.actions[i] != null) {
2219 that.actions[i] = this.actions[i].clone();
2220 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002221 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002222 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002223
Daniel Sandler1a497d32013-04-18 14:52:45 -04002224 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002225 that.bigContentView = this.bigContentView.clone();
2226 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002227
Chris Wren8fd39ec2014-02-27 17:43:26 -05002228 if (heavy && this.headsUpContentView != null) {
2229 that.headsUpContentView = this.headsUpContentView.clone();
2230 }
2231
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002232 that.visibility = this.visibility;
2233
2234 if (this.publicVersion != null) {
2235 that.publicVersion = new Notification();
2236 this.publicVersion.cloneInto(that.publicVersion, heavy);
2237 }
2238
Dan Sandler26e81cf2014-05-06 10:01:27 -04002239 that.color = this.color;
2240
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002241 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002242 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002243 that.mShortcutId = this.mShortcutId;
2244 that.mBadgeIcon = this.mBadgeIcon;
2245 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002246 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002247
Daniel Sandler1a497d32013-04-18 14:52:45 -04002248 if (!heavy) {
2249 that.lightenPayload(); // will clean out extras
2250 }
2251 }
2252
2253 /**
2254 * Removes heavyweight parts of the Notification object for archival or for sending to
2255 * listeners when the full contents are not necessary.
2256 * @hide
2257 */
2258 public final void lightenPayload() {
2259 tickerView = null;
2260 contentView = null;
2261 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002262 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002263 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002264 if (extras != null && !extras.isEmpty()) {
2265 final Set<String> keyset = extras.keySet();
2266 final int N = keyset.size();
2267 final String[] keys = keyset.toArray(new String[N]);
2268 for (int i=0; i<N; i++) {
2269 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002270 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2271 continue;
2272 }
Dan Sandler50128532015-12-08 15:42:41 -05002273 final Object obj = extras.get(key);
2274 if (obj != null &&
2275 ( obj instanceof Parcelable
2276 || obj instanceof Parcelable[]
2277 || obj instanceof SparseArray
2278 || obj instanceof ArrayList)) {
2279 extras.remove(key);
2280 }
2281 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002282 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002283 }
2284
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002285 /**
2286 * Make sure this CharSequence is safe to put into a bundle, which basically
2287 * means it had better not be some custom Parcelable implementation.
2288 * @hide
2289 */
2290 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002291 if (cs == null) return cs;
2292 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2293 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2294 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002295 if (cs instanceof Parcelable) {
2296 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2297 + " instance is a custom Parcelable and not allowed in Notification");
2298 return cs.toString();
2299 }
Selim Cinek60a54252016-02-26 17:03:25 -08002300 return removeTextSizeSpans(cs);
2301 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002302
Selim Cinek60a54252016-02-26 17:03:25 -08002303 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2304 if (charSequence instanceof Spanned) {
2305 Spanned ss = (Spanned) charSequence;
2306 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2307 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2308 for (Object span : spans) {
2309 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002310 if (resultSpan instanceof CharacterStyle) {
2311 resultSpan = ((CharacterStyle) span).getUnderlying();
2312 }
2313 if (resultSpan instanceof TextAppearanceSpan) {
2314 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002315 resultSpan = new TextAppearanceSpan(
2316 originalSpan.getFamily(),
2317 originalSpan.getTextStyle(),
2318 -1,
2319 originalSpan.getTextColor(),
2320 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002321 } else if (resultSpan instanceof RelativeSizeSpan
2322 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002323 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002324 } else {
2325 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002326 }
2327 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2328 ss.getSpanFlags(span));
2329 }
2330 return builder;
2331 }
2332 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002333 }
2334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public int describeContents() {
2336 return 0;
2337 }
2338
2339 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002340 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 */
Svet Ganovddb94882016-06-23 19:55:24 -07002342 public void writeToParcel(Parcel parcel, int flags) {
2343 // We need to mark all pending intents getting into the notification
2344 // system as being put there to later allow the notification ranker
2345 // to launch them and by doing so add the app to the battery saver white
2346 // list for a short period of time. The problem is that the system
2347 // cannot look into the extras as there may be parcelables there that
2348 // the platform does not know how to handle. To go around that we have
2349 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002350 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002351 if (collectPendingIntents) {
2352 PendingIntent.setOnMarshaledListener(
2353 (PendingIntent intent, Parcel out, int outFlags) -> {
2354 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002355 if (allPendingIntents == null) {
2356 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002357 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002358 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002359 }
2360 });
2361 }
2362 try {
2363 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002364 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002365 writeToParcelImpl(parcel, flags);
2366 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002367 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002368 } finally {
2369 if (collectPendingIntents) {
2370 PendingIntent.setOnMarshaledListener(null);
2371 }
2372 }
2373 }
2374
2375 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 parcel.writeInt(1);
2377
Adrian Roosfb921842017-10-26 14:49:56 +02002378 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002380 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002381 if (mSmallIcon == null && icon != 0) {
2382 // you snuck an icon in here without using the builder; let's try to keep it
2383 mSmallIcon = Icon.createWithResource("", icon);
2384 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002385 if (mSmallIcon != null) {
2386 parcel.writeInt(1);
2387 mSmallIcon.writeToParcel(parcel, 0);
2388 } else {
2389 parcel.writeInt(0);
2390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 parcel.writeInt(number);
2392 if (contentIntent != null) {
2393 parcel.writeInt(1);
2394 contentIntent.writeToParcel(parcel, 0);
2395 } else {
2396 parcel.writeInt(0);
2397 }
2398 if (deleteIntent != null) {
2399 parcel.writeInt(1);
2400 deleteIntent.writeToParcel(parcel, 0);
2401 } else {
2402 parcel.writeInt(0);
2403 }
2404 if (tickerText != null) {
2405 parcel.writeInt(1);
2406 TextUtils.writeToParcel(tickerText, parcel, flags);
2407 } else {
2408 parcel.writeInt(0);
2409 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002410 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002411 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002412 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002413 } else {
2414 parcel.writeInt(0);
2415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 if (contentView != null) {
2417 parcel.writeInt(1);
2418 contentView.writeToParcel(parcel, 0);
2419 } else {
2420 parcel.writeInt(0);
2421 }
Selim Cinek279fa862016-06-14 10:57:25 -07002422 if (mLargeIcon == null && largeIcon != null) {
2423 // you snuck an icon in here without using the builder; let's try to keep it
2424 mLargeIcon = Icon.createWithBitmap(largeIcon);
2425 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002426 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002427 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002428 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002429 } else {
2430 parcel.writeInt(0);
2431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
2433 parcel.writeInt(defaults);
2434 parcel.writeInt(this.flags);
2435
2436 if (sound != null) {
2437 parcel.writeInt(1);
2438 sound.writeToParcel(parcel, 0);
2439 } else {
2440 parcel.writeInt(0);
2441 }
2442 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002443
2444 if (audioAttributes != null) {
2445 parcel.writeInt(1);
2446 audioAttributes.writeToParcel(parcel, 0);
2447 } else {
2448 parcel.writeInt(0);
2449 }
2450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 parcel.writeLongArray(vibrate);
2452 parcel.writeInt(ledARGB);
2453 parcel.writeInt(ledOnMS);
2454 parcel.writeInt(ledOffMS);
2455 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002456
2457 if (fullScreenIntent != null) {
2458 parcel.writeInt(1);
2459 fullScreenIntent.writeToParcel(parcel, 0);
2460 } else {
2461 parcel.writeInt(0);
2462 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002463
2464 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002465
John Spurlockfd7f1e02014-03-18 16:41:57 -04002466 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002467
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002468 parcel.writeString(mGroupKey);
2469
2470 parcel.writeString(mSortKey);
2471
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002472 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002473
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002474 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002475
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002476 if (bigContentView != null) {
2477 parcel.writeInt(1);
2478 bigContentView.writeToParcel(parcel, 0);
2479 } else {
2480 parcel.writeInt(0);
2481 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002482
Chris Wren8fd39ec2014-02-27 17:43:26 -05002483 if (headsUpContentView != null) {
2484 parcel.writeInt(1);
2485 headsUpContentView.writeToParcel(parcel, 0);
2486 } else {
2487 parcel.writeInt(0);
2488 }
2489
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002490 parcel.writeInt(visibility);
2491
2492 if (publicVersion != null) {
2493 parcel.writeInt(1);
2494 publicVersion.writeToParcel(parcel, 0);
2495 } else {
2496 parcel.writeInt(0);
2497 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002498
2499 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002500
2501 if (mChannelId != null) {
2502 parcel.writeInt(1);
2503 parcel.writeString(mChannelId);
2504 } else {
2505 parcel.writeInt(0);
2506 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002507 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002508
2509 if (mShortcutId != null) {
2510 parcel.writeInt(1);
2511 parcel.writeString(mShortcutId);
2512 } else {
2513 parcel.writeInt(0);
2514 }
2515
2516 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002517
2518 if (mSettingsText != null) {
2519 parcel.writeInt(1);
2520 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2521 } else {
2522 parcel.writeInt(0);
2523 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002524
2525 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
2527
2528 /**
2529 * Parcelable.Creator that instantiates Notification objects
2530 */
2531 public static final Parcelable.Creator<Notification> CREATOR
2532 = new Parcelable.Creator<Notification>()
2533 {
2534 public Notification createFromParcel(Parcel parcel)
2535 {
2536 return new Notification(parcel);
2537 }
2538
2539 public Notification[] newArray(int size)
2540 {
2541 return new Notification[size];
2542 }
2543 };
2544
2545 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002546 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2547 * <p>
2548 * For backwards compatibility {@code extras} holds some references to "real" member data such
2549 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2550 * fine as long as the object stays in one process.
2551 * <p>
2552 * However, once the notification goes into a parcel each reference gets marshalled separately,
2553 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2554 */
2555 private void fixDuplicateExtras() {
2556 if (extras != null) {
2557 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2558 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2559 }
2560 }
2561
2562 /**
2563 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2564 * separate object, replace it with the field's version to avoid holding duplicate copies.
2565 */
2566 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2567 if (original != null && extras.getParcelable(extraName) != null) {
2568 extras.putParcelable(extraName, original);
2569 }
2570 }
2571
2572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2574 * layout.
2575 *
2576 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2577 * in the view.</p>
2578 * @param context The context for your application / activity.
2579 * @param contentTitle The title that goes in the expanded entry.
2580 * @param contentText The text that goes in the expanded entry.
2581 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2582 * If this is an activity, it must include the
2583 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002584 * that you take care of task management as described in the
2585 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2586 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002587 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002588 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002589 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002591 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 public void setLatestEventInfo(Context context,
2593 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002594 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2595 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2596 new Throwable());
2597 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002598
Selim Cinek4ac6f602016-06-13 15:47:03 -07002599 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2600 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2601 }
2602
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002603 // ensure that any information already set directly is preserved
2604 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002605
2606 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002608 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
2610 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002611 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002613 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002614
2615 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
Julia Reynoldsda303542015-11-23 14:00:20 -05002618 /**
2619 * @hide
2620 */
2621 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002622 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002623 }
2624
2625 /**
2626 * @hide
2627 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002628 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002629 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002630 }
2631
Yi Jin6b514142017-10-30 14:54:12 -07002632 /**
2633 * @hide
2634 */
2635 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2636 long token = proto.start(fieldId);
2637 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2638 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2639 proto.write(NotificationProto.FLAGS, this.flags);
2640 proto.write(NotificationProto.COLOR, this.color);
2641 proto.write(NotificationProto.CATEGORY, this.category);
2642 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2643 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2644 if (this.actions != null) {
2645 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2646 }
2647 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2648 proto.write(NotificationProto.VISIBILITY, this.visibility);
2649 }
2650 if (publicVersion != null) {
2651 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2652 }
2653 proto.end(token);
2654 }
2655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 @Override
2657 public String toString() {
2658 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002659 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002660 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002661 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662 sb.append(priority);
2663 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002664 if (contentView != null) {
2665 sb.append(contentView.getPackage());
2666 sb.append("/0x");
2667 sb.append(Integer.toHexString(contentView.getLayoutId()));
2668 } else {
2669 sb.append("null");
2670 }
2671 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002672 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2673 sb.append("default");
2674 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 int N = this.vibrate.length-1;
2676 sb.append("[");
2677 for (int i=0; i<N; i++) {
2678 sb.append(this.vibrate[i]);
2679 sb.append(',');
2680 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002681 if (N != -1) {
2682 sb.append(this.vibrate[N]);
2683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 } else {
2686 sb.append("null");
2687 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002688 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002689 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002691 } else if (this.sound != null) {
2692 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 } else {
2694 sb.append("null");
2695 }
Chris Wren365b6d32015-07-16 10:39:26 -04002696 if (this.tickerText != null) {
2697 sb.append(" tick");
2698 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002701 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002702 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002703 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002704 if (this.category != null) {
2705 sb.append(" category=");
2706 sb.append(this.category);
2707 }
2708 if (this.mGroupKey != null) {
2709 sb.append(" groupKey=");
2710 sb.append(this.mGroupKey);
2711 }
2712 if (this.mSortKey != null) {
2713 sb.append(" sortKey=");
2714 sb.append(this.mSortKey);
2715 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002716 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002717 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002718 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002719 }
2720 sb.append(" vis=");
2721 sb.append(visibilityToString(this.visibility));
2722 if (this.publicVersion != null) {
2723 sb.append(" publicVersion=");
2724 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002725 }
2726 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 return sb.toString();
2728 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002729
Dan Sandler1b718782014-07-18 12:43:45 -04002730 /**
2731 * {@hide}
2732 */
2733 public static String visibilityToString(int vis) {
2734 switch (vis) {
2735 case VISIBILITY_PRIVATE:
2736 return "PRIVATE";
2737 case VISIBILITY_PUBLIC:
2738 return "PUBLIC";
2739 case VISIBILITY_SECRET:
2740 return "SECRET";
2741 default:
2742 return "UNKNOWN(" + String.valueOf(vis) + ")";
2743 }
2744 }
2745
Joe Onoratocb109a02011-01-18 17:57:41 -08002746 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002747 * {@hide}
2748 */
2749 public static String priorityToString(@Priority int pri) {
2750 switch (pri) {
2751 case PRIORITY_MIN:
2752 return "MIN";
2753 case PRIORITY_LOW:
2754 return "LOW";
2755 case PRIORITY_DEFAULT:
2756 return "DEFAULT";
2757 case PRIORITY_HIGH:
2758 return "HIGH";
2759 case PRIORITY_MAX:
2760 return "MAX";
2761 default:
2762 return "UNKNOWN(" + String.valueOf(pri) + ")";
2763 }
2764 }
2765
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002766 /**
2767 * @hide
2768 */
2769 public boolean hasCompletedProgress() {
2770 // not a progress notification; can't be complete
2771 if (!extras.containsKey(EXTRA_PROGRESS)
2772 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2773 return false;
2774 }
2775 // many apps use max 0 for 'indeterminate'; not complete
2776 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2777 return false;
2778 }
2779 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2780 }
2781
Jeff Sharkey000ce802017-04-29 13:13:27 -06002782 /** @removed */
2783 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002784 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002785 return mChannelId;
2786 }
2787
2788 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002789 * Returns the id of the channel this notification posts to.
2790 */
2791 public String getChannelId() {
2792 return mChannelId;
2793 }
2794
Jeff Sharkey000ce802017-04-29 13:13:27 -06002795 /** @removed */
2796 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002797 public long getTimeout() {
2798 return mTimeout;
2799 }
2800
2801 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002802 * Returns the duration from posting after which this notification should be canceled by the
2803 * system, if it's not canceled already.
2804 */
2805 public long getTimeoutAfter() {
2806 return mTimeout;
2807 }
2808
2809 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002810 * Returns what icon should be shown for this notification if it is being displayed in a
2811 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2812 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2813 */
2814 public int getBadgeIconType() {
2815 return mBadgeIcon;
2816 }
2817
2818 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002819 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002820 *
2821 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2822 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002823 */
2824 public String getShortcutId() {
2825 return mShortcutId;
2826 }
2827
Julia Reynolds3aedded2017-03-31 14:42:09 -04002828
2829 /**
2830 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2831 */
2832 public CharSequence getSettingsText() {
2833 return mSettingsText;
2834 }
2835
Julia Reynolds13d898c2017-02-02 12:22:05 -05002836 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002837 * Returns which type of notifications in a group are responsible for audibly alerting the
2838 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2839 * {@link #GROUP_ALERT_SUMMARY}.
2840 */
2841 public @GroupAlertBehavior int getGroupAlertBehavior() {
2842 return mGroupAlertBehavior;
2843 }
2844
2845 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002846 * The small icon representing this notification in the status bar and content view.
2847 *
2848 * @return the small icon representing this notification.
2849 *
2850 * @see Builder#getSmallIcon()
2851 * @see Builder#setSmallIcon(Icon)
2852 */
2853 public Icon getSmallIcon() {
2854 return mSmallIcon;
2855 }
2856
2857 /**
2858 * Used when notifying to clean up legacy small icons.
2859 * @hide
2860 */
2861 public void setSmallIcon(Icon icon) {
2862 mSmallIcon = icon;
2863 }
2864
2865 /**
2866 * The large icon shown in this notification's content view.
2867 * @see Builder#getLargeIcon()
2868 * @see Builder#setLargeIcon(Icon)
2869 */
2870 public Icon getLargeIcon() {
2871 return mLargeIcon;
2872 }
2873
2874 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002875 * @hide
2876 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002877 public boolean isGroupSummary() {
2878 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2879 }
2880
2881 /**
2882 * @hide
2883 */
2884 public boolean isGroupChild() {
2885 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2886 }
2887
2888 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002889 * @hide
2890 */
2891 public boolean suppressAlertingDueToGrouping() {
2892 if (isGroupSummary()
2893 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2894 return true;
2895 } else if (isGroupChild()
2896 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2897 return true;
2898 }
2899 return false;
2900 }
2901
2902 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002903 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002904 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002905 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002906 * content views using the platform's notification layout template. If your app supports
2907 * versions of Android as old as API level 4, you can instead use
2908 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2909 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2910 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002911 *
Scott Main183bf112012-08-13 19:12:13 -07002912 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002913 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002914 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002915 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002916 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2917 * .setContentText(subject)
2918 * .setSmallIcon(R.drawable.new_mail)
2919 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002920 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002921 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002922 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002923 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002924 /**
2925 * @hide
2926 */
2927 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2928 "android.rebuild.contentViewActionCount";
2929 /**
2930 * @hide
2931 */
2932 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2933 = "android.rebuild.bigViewActionCount";
2934 /**
2935 * @hide
2936 */
2937 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2938 = "android.rebuild.hudViewActionCount";
2939
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002940 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002941
Selim Cinek6743c0b2017-01-18 18:24:01 -08002942 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2943 SystemProperties.getBoolean("notifications.only_title", true);
2944
Selim Cinek389edcd2017-05-11 19:16:44 -07002945 /**
2946 * The lightness difference that has to be added to the primary text color to obtain the
2947 * secondary text color when the background is light.
2948 */
2949 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2950
2951 /**
2952 * The lightness difference that has to be added to the primary text color to obtain the
2953 * secondary text color when the background is dark.
2954 * A bit less then the above value, since it looks better on dark backgrounds.
2955 */
2956 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2957
Joe Onorato46439ce2010-11-19 13:56:21 -08002958 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002959 private Notification mN;
2960 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002961 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002962 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002963 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002965 private boolean mIsLegacy;
2966 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002967
2968 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002969 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2970 */
2971 private int mCachedContrastColor = COLOR_INVALID;
2972 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002973 /**
2974 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2975 */
2976 private int mCachedAmbientColor = COLOR_INVALID;
2977 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002978
2979 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002980 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2981 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2982 */
2983 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002984 private int mTextColorsAreForBackground = COLOR_INVALID;
2985 private int mPrimaryTextColor = COLOR_INVALID;
2986 private int mSecondaryTextColor = COLOR_INVALID;
2987 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002988 private int mBackgroundColor = COLOR_INVALID;
2989 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002990 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002991 /**
2992 * A temporary location where actions are stored. If != null the view originally has action
2993 * but doesn't have any for this inflation.
2994 */
2995 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002996 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002997
Anthony Chenad4d1582017-04-10 16:07:58 -07002998 private boolean mTintActionButtons;
2999 private boolean mInNightMode;
3000
Adrian Roos70d7aa32017-01-11 15:39:06 -08003001 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003002 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003003 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003004 * @param context
3005 * A {@link Context} that will be used by the Builder to construct the
3006 * RemoteViews. The Context will not be held past the lifetime of this Builder
3007 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003008 * @param channelId
3009 * The constructed Notification will be posted on this
3010 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3011 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003012 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003013 public Builder(Context context, String channelId) {
3014 this(context, (Notification) null);
3015 mN.mChannelId = channelId;
3016 }
3017
3018 /**
3019 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3020 * instead. All posted Notifications must specify a NotificationChannel Id.
3021 */
3022 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003023 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003024 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003025 }
3026
Joe Onoratocb109a02011-01-18 17:57:41 -08003027 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003028 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003029 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003030 public Builder(Context context, Notification toAdopt) {
3031 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003032 Resources res = mContext.getResources();
3033 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3034
3035 if (res.getBoolean(R.bool.config_enableNightMode)) {
3036 Configuration currentConfig = res.getConfiguration();
3037 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3038 == Configuration.UI_MODE_NIGHT_YES;
3039 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003040
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003041 if (toAdopt == null) {
3042 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003043 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3044 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3045 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003046 mN.priority = PRIORITY_DEFAULT;
3047 mN.visibility = VISIBILITY_PRIVATE;
3048 } else {
3049 mN = toAdopt;
3050 if (mN.actions != null) {
3051 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003052 }
3053
Selim Cineke7238dd2017-12-14 17:48:32 -08003054 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3055 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3056 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003057 }
3058
Selim Cinek4ac6f602016-06-13 15:47:03 -07003059 if (mN.getSmallIcon() == null && mN.icon != 0) {
3060 setSmallIcon(mN.icon);
3061 }
3062
3063 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3064 setLargeIcon(mN.largeIcon);
3065 }
3066
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003067 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3068 if (!TextUtils.isEmpty(templateClass)) {
3069 final Class<? extends Style> styleClass
3070 = getNotificationStyleClass(templateClass);
3071 if (styleClass == null) {
3072 Log.d(TAG, "Unknown style class: " + templateClass);
3073 } else {
3074 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003075 final Constructor<? extends Style> ctor =
3076 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003077 ctor.setAccessible(true);
3078 final Style style = ctor.newInstance();
3079 style.restoreFromExtras(mN.extras);
3080
3081 if (style != null) {
3082 setStyle(style);
3083 }
3084 } catch (Throwable t) {
3085 Log.e(TAG, "Could not create Style", t);
3086 }
3087 }
3088 }
3089
3090 }
3091 }
3092
3093 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003094 if (mColorUtil == null) {
3095 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003096 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003097 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003098 }
3099
3100 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003101 * If this notification is duplicative of a Launcher shortcut, sets the
3102 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3103 * the shortcut.
3104 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003105 * This field will be ignored by Launchers that don't support badging, don't show
3106 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003107 *
3108 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3109 * supersedes
3110 */
3111 public Builder setShortcutId(String shortcutId) {
3112 mN.mShortcutId = shortcutId;
3113 return this;
3114 }
3115
3116 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003117 * Sets which icon to display as a badge for this notification.
3118 *
3119 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3120 * {@link #BADGE_ICON_LARGE}.
3121 *
3122 * Note: This value might be ignored, for launchers that don't support badge icons.
3123 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003124 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003125 mN.mBadgeIcon = icon;
3126 return this;
3127 }
3128
3129 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003130 * Sets the group alert behavior for this notification. Use this method to mute this
3131 * notification if alerts for this notification's group should be handled by a different
3132 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003133 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3134 * mute. For example, if you want only the summary of your group to make noise, all
3135 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003136 *
3137 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3138 */
3139 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3140 mN.mGroupAlertBehavior = groupAlertBehavior;
3141 return this;
3142 }
3143
Jeff Sharkey000ce802017-04-29 13:13:27 -06003144 /** @removed */
3145 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003146 public Builder setChannel(String channelId) {
3147 mN.mChannelId = channelId;
3148 return this;
3149 }
3150
3151 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003152 * Specifies the channel the notification should be delivered on.
3153 */
3154 public Builder setChannelId(String channelId) {
3155 mN.mChannelId = channelId;
3156 return this;
3157 }
3158
Jeff Sharkey000ce802017-04-29 13:13:27 -06003159 /** @removed */
3160 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003161 public Builder setTimeout(long durationMs) {
3162 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003163 return this;
3164 }
3165
3166 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003167 * Specifies a duration in milliseconds after which this notification should be canceled,
3168 * if it is not already canceled.
3169 */
3170 public Builder setTimeoutAfter(long durationMs) {
3171 mN.mTimeout = durationMs;
3172 return this;
3173 }
3174
3175 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003176 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003177 *
3178 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3179 * shown anymore by default and must be opted into by using
3180 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003181 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003182 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003183 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003184 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003185 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003186 return this;
3187 }
3188
Joe Onoratocb109a02011-01-18 17:57:41 -08003189 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003190 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003191 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003192 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3193 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003194 */
3195 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003196 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003197 return this;
3198 }
3199
3200 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003201 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003202 *
3203 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003204 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003205 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003206 * Useful when showing an elapsed time (like an ongoing phone call).
3207 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003208 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003209 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003210 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003211 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003212 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003213 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003214 */
3215 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003216 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003217 return this;
3218 }
3219
3220 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003221 * Sets the Chronometer to count down instead of counting up.
3222 *
3223 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3224 * If it isn't set the chronometer will count up.
3225 *
3226 * @see #setUsesChronometer(boolean)
3227 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003228 public Builder setChronometerCountDown(boolean countDown) {
3229 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003230 return this;
3231 }
3232
3233 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003234 * Set the small icon resource, which will be used to represent the notification in the
3235 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003237
3238 * The platform template for the expanded view will draw this icon in the left, unless a
3239 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3240 * icon will be moved to the right-hand side.
3241 *
3242
3243 * @param icon
3244 * A resource ID in the application's package of the drawable to use.
3245 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003246 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003247 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003248 return setSmallIcon(icon != 0
3249 ? Icon.createWithResource(mContext, icon)
3250 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003251 }
3252
Joe Onoratocb109a02011-01-18 17:57:41 -08003253 /**
3254 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3255 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3256 * LevelListDrawable}.
3257 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003258 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003259 * @param level The level to use for the icon.
3260 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003261 * @see Notification#icon
3262 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003263 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003264 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003265 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003266 return setSmallIcon(icon);
3267 }
3268
3269 /**
3270 * Set the small icon, which will be used to represent the notification in the
3271 * status bar and content view (unless overriden there by a
3272 * {@link #setLargeIcon(Bitmap) large icon}).
3273 *
3274 * @param icon An Icon object to use.
3275 * @see Notification#icon
3276 */
3277 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003278 mN.setSmallIcon(icon);
3279 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3280 mN.icon = icon.getResId();
3281 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003282 return this;
3283 }
3284
Joe Onoratocb109a02011-01-18 17:57:41 -08003285 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003287 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003288 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003289 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003290 return this;
3291 }
3292
Joe Onoratocb109a02011-01-18 17:57:41 -08003293 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003294 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003295 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003296 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003297 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003298 return this;
3299 }
3300
Joe Onoratocb109a02011-01-18 17:57:41 -08003301 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003302 * This provides some additional information that is displayed in the notification. No
3303 * guarantees are given where exactly it is displayed.
3304 *
3305 * <p>This information should only be provided if it provides an essential
3306 * benefit to the understanding of the notification. The more text you provide the
3307 * less readable it becomes. For example, an email client should only provide the account
3308 * name here if more than one email account has been added.</p>
3309 *
3310 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3311 * notification header area.
3312 *
3313 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3314 * this will be shown in the third line of text in the platform notification template.
3315 * You should not be using {@link #setProgress(int, int, boolean)} at the
3316 * same time on those versions; they occupy the same place.
3317 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003318 */
3319 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003320 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003321 return this;
3322 }
3323
3324 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003325 * Provides text that will appear as a link to your application's settings.
3326 *
3327 * <p>This text does not appear within notification {@link Style templates} but may
3328 * appear when the user uses an affordance to learn more about the notification.
3329 * Additionally, this text will not appear unless you provide a valid link target by
3330 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3331 *
3332 * <p>This text is meant to be concise description about what the user can customize
3333 * when they click on this link. The recommended maximum length is 40 characters.
3334 * @param text
3335 * @return
3336 */
3337 public Builder setSettingsText(CharSequence text) {
3338 mN.mSettingsText = safeCharSequence(text);
3339 return this;
3340 }
3341
3342 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003343 * Set the remote input history.
3344 *
3345 * This should be set to the most recent inputs that have been sent
3346 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3347 * longer relevant (e.g. for chat notifications once the other party has responded).
3348 *
3349 * The most recent input must be stored at the 0 index, the second most recent at the
3350 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3351 * and how much of each individual input is shown.
3352 *
3353 * <p>Note: The reply text will only be shown on notifications that have least one action
3354 * with a {@code RemoteInput}.</p>
3355 */
3356 public Builder setRemoteInputHistory(CharSequence[] text) {
3357 if (text == null) {
3358 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3359 } else {
3360 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3361 CharSequence[] safe = new CharSequence[N];
3362 for (int i = 0; i < N; i++) {
3363 safe[i] = safeCharSequence(text[i]);
3364 }
3365 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3366 }
3367 return this;
3368 }
3369
3370 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003371 * Sets the number of items this notification represents. May be displayed as a badge count
3372 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003373 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003374 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003375 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003376 return this;
3377 }
3378
Joe Onoratocb109a02011-01-18 17:57:41 -08003379 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003380 * A small piece of additional information pertaining to this notification.
3381 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003382 * The platform template will draw this on the last line of the notification, at the far
3383 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003384 *
3385 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3386 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3387 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003388 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003389 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003390 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003392 return this;
3393 }
3394
Joe Onoratocb109a02011-01-18 17:57:41 -08003395 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003396 * Set the progress this notification represents.
3397 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003398 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003399 */
3400 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003401 mN.extras.putInt(EXTRA_PROGRESS, progress);
3402 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3403 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003404 return this;
3405 }
3406
3407 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003408 * Supply a custom RemoteViews to use instead of the platform template.
3409 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003410 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003411 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003412 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003413 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003414 return setCustomContentView(views);
3415 }
3416
3417 /**
3418 * Supply custom RemoteViews to use instead of the platform template.
3419 *
3420 * This will override the layout that would otherwise be constructed by this Builder
3421 * object.
3422 */
3423 public Builder setCustomContentView(RemoteViews contentView) {
3424 mN.contentView = contentView;
3425 return this;
3426 }
3427
3428 /**
3429 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3430 *
3431 * This will override the expanded layout that would otherwise be constructed by this
3432 * Builder object.
3433 */
3434 public Builder setCustomBigContentView(RemoteViews contentView) {
3435 mN.bigContentView = contentView;
3436 return this;
3437 }
3438
3439 /**
3440 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3441 *
3442 * This will override the heads-up layout that would otherwise be constructed by this
3443 * Builder object.
3444 */
3445 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3446 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003447 return this;
3448 }
3449
Joe Onoratocb109a02011-01-18 17:57:41 -08003450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003451 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3452 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003453 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3454 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3455 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003456 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003457 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003458 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003459 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003460 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003461 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003462 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003463 return this;
3464 }
3465
Joe Onoratocb109a02011-01-18 17:57:41 -08003466 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003467 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3468 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003470 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003471 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003472 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003473 return this;
3474 }
3475
Joe Onoratocb109a02011-01-18 17:57:41 -08003476 /**
3477 * An intent to launch instead of posting the notification to the status bar.
3478 * Only for use with extremely high-priority notifications demanding the user's
3479 * <strong>immediate</strong> attention, such as an incoming phone call or
3480 * alarm clock that the user has explicitly set to a particular time.
3481 * If this facility is used for something else, please give the user an option
3482 * to turn it off and use a normal notification, as this can be extremely
3483 * disruptive.
3484 *
Chris Wren47c20a12014-06-18 17:27:29 -04003485 * <p>
3486 * The system UI may choose to display a heads-up notification, instead of
3487 * launching this intent, while the user is using the device.
3488 * </p>
3489 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003490 * @param intent The pending intent to launch.
3491 * @param highPriority Passing true will cause this notification to be sent
3492 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003493 *
3494 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003495 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003496 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003498 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3499 return this;
3500 }
3501
Joe Onoratocb109a02011-01-18 17:57:41 -08003502 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003503 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003504 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003505 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003507 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003508 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003509 return this;
3510 }
3511
Joe Onoratocb109a02011-01-18 17:57:41 -08003512 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003513 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003514 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003515 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003516 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003517 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 setTicker(tickerText);
3519 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003520 return this;
3521 }
3522
Joe Onoratocb109a02011-01-18 17:57:41 -08003523 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003524 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003525 *
3526 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003527 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3528 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003529 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003530 public Builder setLargeIcon(Bitmap b) {
3531 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3532 }
3533
3534 /**
3535 * Add a large icon to the notification content view.
3536 *
3537 * In the platform template, this image will be shown on the left of the notification view
3538 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3539 * badge atop the large icon).
3540 */
3541 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003542 mN.mLargeIcon = icon;
3543 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003544 return this;
3545 }
3546
Joe Onoratocb109a02011-01-18 17:57:41 -08003547 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003548 * Set the sound to play.
3549 *
John Spurlockc0650f022014-07-19 13:22:39 -04003550 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3551 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003552 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003553 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003554 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003555 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003556 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003557 mN.sound = sound;
3558 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003559 return this;
3560 }
3561
Joe Onoratocb109a02011-01-18 17:57:41 -08003562 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003563 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003564 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003565 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3566 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003567 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003568 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003569 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003570 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003571 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003572 mN.sound = sound;
3573 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003574 return this;
3575 }
3576
Joe Onoratocb109a02011-01-18 17:57:41 -08003577 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003578 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3579 * use during playback.
3580 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003581 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003582 * @see Notification#sound
3583 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003584 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003585 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003586 mN.sound = sound;
3587 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003588 return this;
3589 }
3590
3591 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003592 * Set the vibration pattern to use.
3593 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003594 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3595 * <code>pattern</code> parameter.
3596 *
Chris Wren47c20a12014-06-18 17:27:29 -04003597 * <p>
3598 * A notification that vibrates is more likely to be presented as a heads-up notification.
3599 * </p>
3600 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003601 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003602 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003603 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003604 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003605 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003606 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003607 return this;
3608 }
3609
Joe Onoratocb109a02011-01-18 17:57:41 -08003610 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003611 * Set the desired color for the indicator LED on the device, as well as the
3612 * blink duty cycle (specified in milliseconds).
3613 *
3614
3615 * Not all devices will honor all (or even any) of these values.
3616 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003617 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003618 * @see Notification#ledARGB
3619 * @see Notification#ledOnMS
3620 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003621 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003622 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003623 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003624 mN.ledARGB = argb;
3625 mN.ledOnMS = onMs;
3626 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003627 if (onMs != 0 || offMs != 0) {
3628 mN.flags |= FLAG_SHOW_LIGHTS;
3629 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003630 return this;
3631 }
3632
Joe Onoratocb109a02011-01-18 17:57:41 -08003633 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003634 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003635 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003636
3637 * Ongoing notifications cannot be dismissed by the user, so your application or service
3638 * must take care of canceling them.
3639 *
3640
3641 * They are typically used to indicate a background task that the user is actively engaged
3642 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3643 * (e.g., a file download, sync operation, active network connection).
3644 *
3645
3646 * @see Notification#FLAG_ONGOING_EVENT
3647 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003648 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003649 public Builder setOngoing(boolean ongoing) {
3650 setFlag(FLAG_ONGOING_EVENT, ongoing);
3651 return this;
3652 }
3653
Joe Onoratocb109a02011-01-18 17:57:41 -08003654 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003655 * Set whether this notification should be colorized. When set, the color set with
3656 * {@link #setColor(int)} will be used as the background color of this notification.
3657 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003658 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3659 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003660 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003661 * For most styles, the coloring will only be applied if the notification is for a
3662 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003663 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003664 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003665 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003666 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003667 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003668 */
3669 public Builder setColorized(boolean colorize) {
3670 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3671 return this;
3672 }
3673
3674 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003675 * Set this flag if you would only like the sound, vibrate
3676 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003677 *
3678 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003679 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003680 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3681 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3682 return this;
3683 }
3684
Joe Onoratocb109a02011-01-18 17:57:41 -08003685 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003686 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003687 *
3688 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003689 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003690 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003691 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003692 return this;
3693 }
3694
Joe Onoratocb109a02011-01-18 17:57:41 -08003695 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003696 * Set whether or not this notification should not bridge to other devices.
3697 *
3698 * <p>Some notifications can be bridged to other devices for remote display.
3699 * This hint can be set to recommend this notification not be bridged.
3700 */
3701 public Builder setLocalOnly(boolean localOnly) {
3702 setFlag(FLAG_LOCAL_ONLY, localOnly);
3703 return this;
3704 }
3705
3706 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003707 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003708 * <p>
3709 * The value should be one or more of the following fields combined with
3710 * bitwise-or:
3711 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3712 * <p>
3713 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003714 *
3715 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003716 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003717 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003718 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003719 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003720 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003721 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003722 return this;
3723 }
3724
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003725 /**
3726 * Set the priority of this notification.
3727 *
3728 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003729 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003730 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003731 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003732 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003733 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003734 return this;
3735 }
Joe Malin8d40d042012-11-05 11:36:40 -08003736
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003737 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003738 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003739 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003740 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003741 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003742 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003743 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003744 return this;
3745 }
3746
3747 /**
Chris Wrendde75302014-03-26 17:24:15 -04003748 * Add a person that is relevant to this notification.
3749 *
Chris Wrene6c48932014-09-29 17:19:27 -04003750 * <P>
3751 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003752 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3753 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3754 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003755 * </P>
3756 *
3757 * <P>
3758 * The person should be specified by the {@code String} representation of a
3759 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3760 * </P>
3761 *
3762 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3763 * URIs. The path part of these URIs must exist in the contacts database, in the
3764 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3765 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003766 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3767 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003768 * </P>
3769 *
3770 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003771 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003772 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003773 */
Chris Wrene6c48932014-09-29 17:19:27 -04003774 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003775 addPerson(new Person().setUri(uri));
3776 return this;
3777 }
3778
3779 /**
3780 * Add a person that is relevant to this notification.
3781 *
3782 * <P>
3783 * Depending on user preferences, this annotation may allow the notification to pass
3784 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3785 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3786 * appear more prominently in the user interface.
3787 * </P>
3788 *
3789 * <P>
3790 * A person should usually contain a uri in order to benefit from the ranking boost.
3791 * However, even if no uri is provided, it's beneficial to provide other people in the
3792 * notification, such that listeners and voice only devices can announce and handle them
3793 * properly.
3794 * </P>
3795 *
3796 * @param person the person to add.
3797 * @see Notification#EXTRA_PEOPLE_LIST
3798 */
3799 public Builder addPerson(Person person) {
3800 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003801 return this;
3802 }
3803
3804 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003805 * Set this notification to be part of a group of notifications sharing the same key.
3806 * Grouped notifications may display in a cluster or stack on devices which
3807 * support such rendering.
3808 *
3809 * <p>To make this notification the summary for its group, also call
3810 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3811 * {@link #setSortKey}.
3812 * @param groupKey The group key of the group.
3813 * @return this object for method chaining
3814 */
3815 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003816 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003817 return this;
3818 }
3819
3820 /**
3821 * Set this notification to be the group summary for a group of notifications.
3822 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003823 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3824 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003825 * @param isGroupSummary Whether this notification should be a group summary.
3826 * @return this object for method chaining
3827 */
3828 public Builder setGroupSummary(boolean isGroupSummary) {
3829 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3830 return this;
3831 }
3832
3833 /**
3834 * Set a sort key that orders this notification among other notifications from the
3835 * same package. This can be useful if an external sort was already applied and an app
3836 * would like to preserve this. Notifications will be sorted lexicographically using this
3837 * value, although providing different priorities in addition to providing sort key may
3838 * cause this value to be ignored.
3839 *
3840 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003841 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003842 *
3843 * @see String#compareTo(String)
3844 */
3845 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003846 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003847 return this;
3848 }
3849
3850 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003851 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003852 *
Griff Hazen720042b2014-02-24 15:46:56 -08003853 * <p>Values within the Bundle will replace existing extras values in this Builder.
3854 *
3855 * @see Notification#extras
3856 */
Griff Hazen959591e2014-05-15 22:26:18 -07003857 public Builder addExtras(Bundle extras) {
3858 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003859 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003860 }
3861 return this;
3862 }
3863
3864 /**
3865 * Set metadata for this notification.
3866 *
3867 * <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 -04003868 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003869 * called.
3870 *
Griff Hazen720042b2014-02-24 15:46:56 -08003871 * <p>Replaces any existing extras values with those from the provided Bundle.
3872 * Use {@link #addExtras} to merge in metadata instead.
3873 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003874 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003875 */
Griff Hazen959591e2014-05-15 22:26:18 -07003876 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003877 if (extras != null) {
3878 mUserExtras = extras;
3879 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 return this;
3881 }
3882
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003883 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003884 * Get the current metadata Bundle used by this notification Builder.
3885 *
3886 * <p>The returned Bundle is shared with this Builder.
3887 *
3888 * <p>The current contents of this Bundle are copied into the Notification each time
3889 * {@link #build()} is called.
3890 *
3891 * @see Notification#extras
3892 */
3893 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003894 return mUserExtras;
3895 }
3896
3897 private Bundle getAllExtras() {
3898 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3899 saveExtras.putAll(mN.extras);
3900 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003901 }
3902
3903 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003904 * Add an action to this notification. Actions are typically displayed by
3905 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003906 * <p>
3907 * Every action must have an icon (32dp square and matching the
3908 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3909 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3910 * <p>
3911 * A notification in its expanded form can display up to 3 actions, from left to right in
3912 * the order they were added. Actions will not be displayed when the notification is
3913 * collapsed, however, so be sure that any essential functions may be accessed by the user
3914 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003915 *
3916 * @param icon Resource ID of a drawable that represents the action.
3917 * @param title Text describing the action.
3918 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003919 *
3920 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003921 */
Dan Sandler86647982015-05-13 23:41:13 -04003922 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003923 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003924 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003925 return this;
3926 }
3927
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003928 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003929 * Add an action to this notification. Actions are typically displayed by
3930 * the system as a button adjacent to the notification content.
3931 * <p>
3932 * Every action must have an icon (32dp square and matching the
3933 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3934 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3935 * <p>
3936 * A notification in its expanded form can display up to 3 actions, from left to right in
3937 * the order they were added. Actions will not be displayed when the notification is
3938 * collapsed, however, so be sure that any essential functions may be accessed by the user
3939 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3940 *
3941 * @param action The action to add.
3942 */
3943 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003944 if (action != null) {
3945 mActions.add(action);
3946 }
Griff Hazen959591e2014-05-15 22:26:18 -07003947 return this;
3948 }
3949
3950 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003951 * Alter the complete list of actions attached to this notification.
3952 * @see #addAction(Action).
3953 *
3954 * @param actions
3955 * @return
3956 */
3957 public Builder setActions(Action... actions) {
3958 mActions.clear();
3959 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003960 if (actions[i] != null) {
3961 mActions.add(actions[i]);
3962 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003963 }
3964 return this;
3965 }
3966
3967 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003968 * Add a rich notification style to be applied at build time.
3969 *
3970 * @param style Object responsible for modifying the notification style.
3971 */
3972 public Builder setStyle(Style style) {
3973 if (mStyle != style) {
3974 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003975 if (mStyle != null) {
3976 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003977 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3978 } else {
3979 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003980 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003981 }
3982 return this;
3983 }
3984
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003985 /**
3986 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003987 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003988 * @return The same Builder.
3989 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003990 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003991 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003992 return this;
3993 }
3994
3995 /**
3996 * Supply a replacement Notification whose contents should be shown in insecure contexts
3997 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3998 * @param n A replacement notification, presumably with some or all info redacted.
3999 * @return The same Builder.
4000 */
4001 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004002 if (n != null) {
4003 mN.publicVersion = new Notification();
4004 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4005 } else {
4006 mN.publicVersion = null;
4007 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004008 return this;
4009 }
4010
Griff Hazenb720abe2014-05-20 13:15:30 -07004011 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004012 * Apply an extender to this notification builder. Extenders may be used to add
4013 * metadata or change options on this builder.
4014 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004015 public Builder extend(Extender extender) {
4016 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004017 return this;
4018 }
4019
Dan Sandler4e787062015-06-17 15:09:48 -04004020 /**
4021 * @hide
4022 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004023 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004024 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004025 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004026 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004027 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004028 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004029 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004030 }
4031
Dan Sandler26e81cf2014-05-06 10:01:27 -04004032 /**
4033 * Sets {@link Notification#color}.
4034 *
4035 * @param argb The accent color to use
4036 *
4037 * @return The same Builder.
4038 */
Tor Norbye80756e32015-03-02 09:39:27 -08004039 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004040 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004041 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004042 return this;
4043 }
4044
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004045 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004046 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4047 // This user can never be a badged profile,
4048 // and also includes USER_ALL system notifications.
4049 return null;
4050 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004051 // Note: This assumes that the current user can read the profile badge of the
4052 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004053 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004054 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004055 }
4056
4057 private Bitmap getProfileBadge() {
4058 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004059 if (badge == null) {
4060 return null;
4061 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004062 final int size = mContext.getResources().getDimensionPixelSize(
4063 R.dimen.notification_badge_size);
4064 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004065 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004066 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004067 badge.draw(canvas);
4068 return bitmap;
4069 }
4070
Selim Cinekc848c3a2016-01-13 15:27:30 -08004071 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004072 Bitmap profileBadge = getProfileBadge();
4073
Kenny Guy98193ea2014-07-24 19:54:37 +01004074 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004075 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4076 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004077 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004078 contentView.setDrawableTint(R.id.profile_badge, false,
4079 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004080 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004081 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004082 }
4083
Christoph Studerfe718432014-09-01 18:21:18 +02004084 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004085 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004086 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004087 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004088 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004089 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004090 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004091 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004092 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004093 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004094 }
4095
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004096 /**
4097 * Resets the notification header to its original state
4098 */
4099 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004100 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4101 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004102 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004103 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004104 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004105 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004106 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004107 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4108 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004109 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004110 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004111 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004112 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004113 contentView.setImageViewIcon(R.id.profile_badge, null);
4114 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004115 }
4116
4117 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004118 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4119 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004120 }
4121
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004122 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004123 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004124 }
4125
4126 /**
4127 * @param hasProgress whether the progress bar should be shown and set
4128 */
4129 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004130 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4131 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004132 }
4133
Adrian Roos70d7aa32017-01-11 15:39:06 -08004134 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004135 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004136
Christoph Studerfe718432014-09-01 18:21:18 +02004137 resetStandardTemplate(contentView);
4138
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004139 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004140 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004141 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004142 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004143 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4144 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004145 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004146 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004147 if (!p.ambient) {
4148 setTextViewColorPrimary(contentView, R.id.title);
4149 }
Selim Cinek954cc232016-05-20 13:29:23 -07004150 contentView.setViewLayoutWidth(R.id.title, showProgress
4151 ? ViewGroup.LayoutParams.WRAP_CONTENT
4152 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004153 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004154 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004155 int textId = showProgress ? com.android.internal.R.id.text_line_1
4156 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004157 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004158 if (!p.ambient) {
4159 setTextViewColorSecondary(contentView, textId);
4160 }
Selim Cinek41598732016-01-11 16:58:37 -08004161 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004162 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004163
Selim Cinek279fa862016-06-14 10:57:25 -07004164 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004165
Selim Cinek29603462015-11-17 19:04:39 -08004166 return contentView;
4167 }
4168
Selim Cinek48f66b72017-08-18 16:17:51 -07004169 private CharSequence processTextSpans(CharSequence text) {
4170 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004171 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004172 }
4173 return text;
4174 }
4175
Selim Cinek7b9605b2017-01-19 17:36:00 -08004176 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4177 ensureColors();
4178 contentView.setTextColor(id, mPrimaryTextColor);
4179 }
4180
Selim Cinek48f66b72017-08-18 16:17:51 -07004181 private boolean hasForegroundColor() {
4182 return mForegroundColor != COLOR_INVALID;
4183 }
4184
Selim Cinek389edcd2017-05-11 19:16:44 -07004185 /**
4186 * @return the primary text color
4187 * @hide
4188 */
4189 @VisibleForTesting
4190 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004191 ensureColors();
4192 return mPrimaryTextColor;
4193 }
4194
Selim Cinek389edcd2017-05-11 19:16:44 -07004195 /**
4196 * @return the secondary text color
4197 * @hide
4198 */
4199 @VisibleForTesting
4200 public int getSecondaryTextColor() {
4201 ensureColors();
4202 return mSecondaryTextColor;
4203 }
4204
Selim Cinek7b9605b2017-01-19 17:36:00 -08004205 private int getActionBarColor() {
4206 ensureColors();
4207 return mActionBarColor;
4208 }
4209
Selim Cinek622c64a2017-04-17 17:10:05 -07004210 private int getActionBarColorDeEmphasized() {
4211 int backgroundColor = getBackgroundColor();
4212 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4213 }
4214
Selim Cinek7b9605b2017-01-19 17:36:00 -08004215 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4216 ensureColors();
4217 contentView.setTextColor(id, mSecondaryTextColor);
4218 }
4219
4220 private void ensureColors() {
4221 int backgroundColor = getBackgroundColor();
4222 if (mPrimaryTextColor == COLOR_INVALID
4223 || mSecondaryTextColor == COLOR_INVALID
4224 || mActionBarColor == COLOR_INVALID
4225 || mTextColorsAreForBackground != backgroundColor) {
4226 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004227 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004228 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4229 backgroundColor);
4230 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4231 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004232 if (backgroundColor != COLOR_DEFAULT
4233 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4234 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4235 mPrimaryTextColor, backgroundColor, 4.5);
4236 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4237 mSecondaryTextColor, backgroundColor, 4.5);
4238 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004239 } else {
4240 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4241 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4242 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4243 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004244 // We only respect the given colors if worst case Black or White still has
4245 // contrast
4246 boolean backgroundLight = backLum > textLum
4247 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4248 || backLum <= textLum
4249 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004250 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004251 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004252 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4253 mForegroundColor,
4254 backgroundColor,
4255 true /* findFG */,
4256 4.5f);
4257 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004258 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004259 } else {
4260 mSecondaryTextColor =
4261 NotificationColorUtil.findContrastColorAgainstDark(
4262 mForegroundColor,
4263 backgroundColor,
4264 true /* findFG */,
4265 4.5f);
4266 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004267 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004268 }
4269 } else {
4270 mPrimaryTextColor = mForegroundColor;
4271 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004272 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4273 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004274 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4275 backgroundColor) < 4.5f) {
4276 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004277 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004278 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4279 mSecondaryTextColor,
4280 backgroundColor,
4281 true /* findFG */,
4282 4.5f);
4283 } else {
4284 mSecondaryTextColor
4285 = NotificationColorUtil.findContrastColorAgainstDark(
4286 mSecondaryTextColor,
4287 backgroundColor,
4288 true /* findFG */,
4289 4.5f);
4290 }
4291 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004292 mSecondaryTextColor, backgroundLight
4293 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4294 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004295 }
4296 }
4297 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004298 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4299 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004300 }
4301 }
4302
4303 private void updateBackgroundColor(RemoteViews contentView) {
4304 if (isColorized()) {
4305 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4306 getBackgroundColor());
4307 } else {
4308 // Clear it!
4309 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4310 0);
4311 }
4312 }
4313
Selim Cinek860b6da2015-12-16 19:02:19 -08004314 /**
4315 * @param remoteView the remote view to update the minheight in
4316 * @param hasMinHeight does it have a mimHeight
4317 * @hide
4318 */
4319 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4320 int minHeight = 0;
4321 if (hasMinHeight) {
4322 // we need to set the minHeight of the notification
4323 minHeight = mContext.getResources().getDimensionPixelSize(
4324 com.android.internal.R.dimen.notification_min_content_height);
4325 }
4326 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4327 }
4328
Selim Cinek29603462015-11-17 19:04:39 -08004329 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004330 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4331 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4332 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4333 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004334 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004335 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004336 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004337 contentView.setProgressBackgroundTintList(
4338 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4339 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004340 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004341 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004342 contentView.setProgressTintList(R.id.progress, colorStateList);
4343 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004344 }
Selim Cinek29603462015-11-17 19:04:39 -08004345 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004346 } else {
4347 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004348 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004349 }
Joe Onorato561d3852010-11-20 18:09:34 -08004350 }
4351
Selim Cinek88188f22017-09-19 16:46:56 -07004352 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4353 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004354 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4355 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4356 }
Selim Cinek88188f22017-09-19 16:46:56 -07004357 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4358 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004359 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4360 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4361 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004362 int endMargin = R.dimen.notification_content_picture_margin;
4363 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4364 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4365 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004366 }
4367 // Bind the reply action
4368 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004369
Selim Cinek88188f22017-09-19 16:46:56 -07004370 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4371 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4372 if (actionVisible) {
4373 // We're only showing the icon as big if we're hiding the large icon
4374 int contrastColor = resolveContrastColor();
4375 int iconColor;
4376 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004377 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004378 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004379 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004380 contentView.setOnClickPendingIntent(R.id.right_icon,
4381 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004382 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004383 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4384 ? Color.BLACK : Color.WHITE;
4385 } else {
4386 contentView.setImageViewResource(R.id.right_icon,
4387 R.drawable.ic_reply_notification_large);
4388 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4389 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004390 }
Selim Cinek88188f22017-09-19 16:46:56 -07004391 contentView.setDrawableTint(replyId,
4392 false /* targetBackground */,
4393 iconColor,
4394 PorterDuff.Mode.SRC_ATOP);
4395 contentView.setOnClickPendingIntent(replyId,
4396 action.actionIntent);
4397 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4398 } else {
4399 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004400 }
Selim Cinek88188f22017-09-19 16:46:56 -07004401 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4402 ? View.VISIBLE
4403 : View.GONE);
4404 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004405 ? View.VISIBLE
4406 : View.GONE);
4407 }
4408
4409 private Action findReplyAction() {
4410 ArrayList<Action> actions = mActions;
4411 if (mOriginalActions != null) {
4412 actions = mOriginalActions;
4413 }
4414 int numActions = actions.size();
4415 for (int i = 0; i < numActions; i++) {
4416 Action action = actions.get(i);
4417 if (hasValidRemoteInput(action)) {
4418 return action;
4419 }
4420 }
4421 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004422 }
4423
Selim Cinekafeed292017-12-12 17:32:44 -08004424 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4425 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004426 bindSmallIcon(contentView, ambient);
4427 bindHeaderAppName(contentView, ambient);
4428 if (!ambient) {
4429 // Ambient view does not have these
4430 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004431 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004432 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004433 bindProfileBadge(contentView);
4434 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004435 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004436 }
4437
4438 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004439 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004440 contentView.setDrawableTint(R.id.expand_button, false, color,
4441 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004442 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004443 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004444 }
4445
Selim Cinek99104832017-01-25 14:47:33 -08004446 /**
4447 * @return the color that is used as the first primary highlight color. This is applied
4448 * in several places like the action buttons or the app name in the header.
4449 */
4450 private int getPrimaryHighlightColor() {
4451 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4452 }
4453
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004454 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4455 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004456 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004457 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004458 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4459 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4460 contentView.setLong(R.id.chronometer, "setBase",
4461 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4462 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004463 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004464 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004465 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004466 } else {
4467 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4468 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004469 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004470 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004471 } else {
4472 // We still want a time to be set but gone, such that we can show and hide it
4473 // on demand in case it's a child notification without anything in the header
4474 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004475 }
4476 }
4477
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004478 private void bindHeaderText(RemoteViews contentView) {
4479 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4480 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004481 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004482 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004483 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004484 if (headerText == null
4485 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4486 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4487 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4488 }
4489 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004490 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004491 contentView.setTextViewText(R.id.header_text, processTextSpans(
4492 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004493 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004494 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4495 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004496 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004497 }
4498 }
4499
Selim Cinekafeed292017-12-12 17:32:44 -08004500 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4501 if (!TextUtils.isEmpty(secondaryText)) {
4502 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4503 processLegacyText(secondaryText)));
4504 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4505 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4506 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4507 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4508 }
4509 }
4510
Adrian Rooseba05822016-04-22 17:09:27 -07004511 /**
4512 * @hide
4513 */
4514 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004515 CharSequence name = null;
4516 final PackageManager pm = mContext.getPackageManager();
4517 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4518 // only system packages which lump together a bunch of unrelated stuff
4519 // may substitute a different name to make the purpose of the
4520 // notification more clear. the correct package label should always
4521 // be accessible via SystemUI.
4522 final String pkg = mContext.getPackageName();
4523 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4524 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4525 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4526 name = subName;
4527 } else {
4528 Log.w(TAG, "warning: pkg "
4529 + pkg + " attempting to substitute app name '" + subName
4530 + "' without holding perm "
4531 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4532 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004533 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004534 if (TextUtils.isEmpty(name)) {
4535 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4536 }
4537 if (TextUtils.isEmpty(name)) {
4538 // still nothing?
4539 return null;
4540 }
4541
4542 return String.valueOf(name);
4543 }
Adrian Roos487374f2017-01-11 15:48:14 -08004544 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004545 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004546 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004547 setTextViewColorPrimary(contentView, R.id.app_name_text);
4548 } else {
4549 contentView.setTextColor(R.id.app_name_text,
4550 ambient ? resolveAmbientColor() : resolveContrastColor());
4551 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004552 }
4553
Adrian Roos487374f2017-01-11 15:48:14 -08004554 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004555 if (mN.mSmallIcon == null && mN.icon != 0) {
4556 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4557 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004558 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004559 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004560 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004561 }
4562
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004563 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004564 * @return true if the built notification will show the time or the chronometer; false
4565 * otherwise
4566 */
4567 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004568 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004569 }
4570
Christoph Studerfe718432014-09-01 18:21:18 +02004571 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004572 // actions_container is only reset when there are no actions to avoid focus issues with
4573 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004574 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004575 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004576
4577 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4578 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4579
4580 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4581 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4582 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4583 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004584
4585 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004586 }
4587
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004588 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004589 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004590 }
4591
Adrian Roos70d7aa32017-01-11 15:39:06 -08004592 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4593 StandardTemplateParams p) {
4594 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004595
Christoph Studerfe718432014-09-01 18:21:18 +02004596 resetStandardTemplateWithActions(big);
4597
Adrian Roose458aa82015-12-08 16:17:19 -08004598 boolean validRemoteInput = false;
4599
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004600 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004601 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004602 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004603 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004604 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004605 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004606 if (p.ambient) {
4607 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004608 } else if (isColorized()) {
4609 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4610 } else {
4611 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4612 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004613 }
Adrian Roosf852a422016-06-03 13:33:43 -07004614 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4615 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004616 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004617 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004618 Action action = mActions.get(i);
4619 validRemoteInput |= hasValidRemoteInput(action);
4620
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004621 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004622 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004623 big.addView(R.id.actions, button);
4624 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004625 } else {
4626 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004627 }
Adrian Roose458aa82015-12-08 16:17:19 -08004628
4629 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004630 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004631 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4632 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004633 big.setTextViewText(R.id.notification_material_reply_text_1,
4634 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004635 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004636
4637 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4638 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004639 big.setTextViewText(R.id.notification_material_reply_text_2,
4640 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004641 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004642
4643 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4644 big.setViewVisibility(
4645 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004646 big.setTextViewText(R.id.notification_material_reply_text_3,
4647 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004648 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004649 }
4650 }
4651 }
4652
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004653 return big;
4654 }
4655
Adrian Roose458aa82015-12-08 16:17:19 -08004656 private boolean hasValidRemoteInput(Action action) {
4657 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4658 // Weird actions
4659 return false;
4660 }
4661
4662 RemoteInput[] remoteInputs = action.getRemoteInputs();
4663 if (remoteInputs == null) {
4664 return false;
4665 }
4666
4667 for (RemoteInput r : remoteInputs) {
4668 CharSequence[] choices = r.getChoices();
4669 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4670 return true;
4671 }
4672 }
4673 return false;
4674 }
4675
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004676 /**
4677 * Construct a RemoteViews for the final 1U notification layout. In order:
4678 * 1. Custom contentView from the caller
4679 * 2. Style's proposed content view
4680 * 3. Standard template view
4681 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004682 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004683 return createContentView(false /* increasedheight */ );
4684 }
4685
4686 /**
4687 * Construct a RemoteViews for the smaller content view.
4688 *
4689 * @param increasedHeight true if this layout be created with an increased height. Some
4690 * styles may support showing more then just that basic 1U size
4691 * and the system may decide to render important notifications
4692 * slightly bigger even when collapsed.
4693 *
4694 * @hide
4695 */
4696 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004697 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004698 return mN.contentView;
4699 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004700 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004701 if (styleView != null) {
4702 return styleView;
4703 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004704 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004705 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004706 }
4707
Selim Cineka7679b62017-05-10 16:33:25 -07004708 private boolean useExistingRemoteView() {
4709 return mStyle == null || (!mStyle.displayCustomViewInline()
4710 && !mRebuildStyledRemoteViews);
4711 }
4712
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004713 /**
4714 * Construct a RemoteViews for the final big notification layout.
4715 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004716 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004717 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004718 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004719 return mN.bigContentView;
4720 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004721 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004722 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004723 } else if (mActions.size() != 0) {
4724 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004725 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004726 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004727 return result;
4728 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004729
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004730 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004731 * Construct a RemoteViews for the final notification header only. This will not be
4732 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004733 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004734 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004735 * @hide
4736 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004737 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004738 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4739 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004740 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004741 ambient ? R.layout.notification_template_ambient_header
4742 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004743 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004744 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004745 if (colorized != null) {
4746 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4747 } else {
4748 mN.extras.remove(EXTRA_COLORIZED);
4749 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004750 return header;
4751 }
4752
Adrian Roos487374f2017-01-11 15:48:14 -08004753 /**
4754 * Construct a RemoteViews for the ambient version of the notification.
4755 *
4756 * @hide
4757 */
4758 public RemoteViews makeAmbientNotification() {
4759 RemoteViews ambient = applyStandardTemplateWithActions(
4760 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004761 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004762 return ambient;
4763 }
4764
Selim Cinek29603462015-11-17 19:04:39 -08004765 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004766 if (result != null) {
4767 result.setViewVisibility(R.id.text_line_1, View.GONE);
4768 }
Selim Cinek29603462015-11-17 19:04:39 -08004769 }
4770
Selim Cinek6743c0b2017-01-18 18:24:01 -08004771 /**
4772 * Adapt the Notification header if this view is used as an expanded view.
4773 *
4774 * @hide
4775 */
4776 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004777 if (result != null) {
4778 result.setBoolean(R.id.notification_header, "setExpanded", true);
4779 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004780 }
4781
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004782 /**
4783 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004784 *
4785 * @param increasedHeight true if this layout be created with an increased height. Some
4786 * styles may support showing more then just that basic 1U size
4787 * and the system may decide to render important notifications
4788 * slightly bigger even when collapsed.
4789 *
4790 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004791 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004792 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004793 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004794 return mN.headsUpContentView;
4795 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004796 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4797 if (styleView != null) {
4798 return styleView;
4799 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004800 } else if (mActions.size() == 0) {
4801 return null;
4802 }
4803
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004804 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004805 }
4806
Selim Cinek624c02db2015-12-14 21:00:02 -08004807 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004808 * Construct a RemoteViews for the final heads-up notification layout.
4809 */
4810 public RemoteViews createHeadsUpContentView() {
4811 return createHeadsUpContentView(false /* useIncreasedHeight */);
4812 }
4813
4814 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004815 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4816 *
4817 * @hide
4818 */
4819 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004820 return makePublicView(false /* ambient */);
4821 }
4822
4823 /**
4824 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4825 *
4826 * @hide
4827 */
4828 public RemoteViews makePublicAmbientNotification() {
4829 return makePublicView(true /* ambient */);
4830 }
4831
4832 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004833 if (mN.publicVersion != null) {
4834 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004835 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004836 }
4837 Bundle savedBundle = mN.extras;
4838 Style style = mStyle;
4839 mStyle = null;
4840 Icon largeIcon = mN.mLargeIcon;
4841 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004842 Bitmap largeIconLegacy = mN.largeIcon;
4843 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004844 ArrayList<Action> actions = mActions;
4845 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004846 Bundle publicExtras = new Bundle();
4847 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4848 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4849 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4850 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004851 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4852 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004853 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004854 RemoteViews view;
4855 if (ambient) {
4856 publicExtras.putCharSequence(EXTRA_TITLE,
4857 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4858 view = makeAmbientNotification();
4859 } else{
4860 view = makeNotificationHeader(false /* ambient */);
4861 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4862 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004863 mN.extras = savedBundle;
4864 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004865 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004866 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004867 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004868 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004869 }
4870
Selim Cinek6743c0b2017-01-18 18:24:01 -08004871 /**
4872 * Construct a content view for the display when low - priority
4873 *
4874 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4875 * a new subtext is created consisting of the content of the
4876 * notification.
4877 * @hide
4878 */
4879 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4880 int color = mN.color;
4881 mN.color = COLOR_DEFAULT;
4882 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4883 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4884 CharSequence newSummary = createSummaryText();
4885 if (!TextUtils.isEmpty(newSummary)) {
4886 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4887 }
4888 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004889
Adrian Roos6f6e1592017-05-02 16:22:53 -07004890 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004891 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004892 if (summary != null) {
4893 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4894 } else {
4895 mN.extras.remove(EXTRA_SUB_TEXT);
4896 }
4897 mN.color = color;
4898 return header;
4899 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004900
Selim Cinek6743c0b2017-01-18 18:24:01 -08004901 private CharSequence createSummaryText() {
4902 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4903 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4904 return titleText;
4905 }
4906 SpannableStringBuilder summary = new SpannableStringBuilder();
4907 if (titleText == null) {
4908 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4909 }
4910 BidiFormatter bidi = BidiFormatter.getInstance();
4911 if (titleText != null) {
4912 summary.append(bidi.unicodeWrap(titleText));
4913 }
4914 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4915 if (titleText != null && contentText != null) {
4916 summary.append(bidi.unicodeWrap(mContext.getText(
4917 R.string.notification_header_divider_symbol_with_spaces)));
4918 }
4919 if (contentText != null) {
4920 summary.append(bidi.unicodeWrap(contentText));
4921 }
4922 return summary;
4923 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004924
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004925 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004926 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004927 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004928 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004929 emphazisedMode ? getEmphasizedActionLayoutResource()
4930 : tombstone ? getActionTombstoneLayoutResource()
4931 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004932 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004933 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004934 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004935 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004936 if (action.mRemoteInputs != null) {
4937 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4938 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004939 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004940 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004941 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004942 int bgColor;
4943 if (isColorized()) {
4944 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4945 } else {
4946 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4947 : R.color.notification_action_list_dark);
4948 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004949 button.setDrawableTint(R.id.button_holder, true,
4950 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004951 CharSequence title = action.title;
4952 ColorStateList[] outResultColor = null;
4953 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004954 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004955 } else {
4956 outResultColor = new ColorStateList[1];
4957 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4958 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004959 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004960 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004961 if (outResultColor != null && outResultColor[0] != null) {
4962 // We need to set the text color as well since changing a text to uppercase
4963 // clears its spans.
4964 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004965 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004966 button.setTextColor(R.id.action0,resolveContrastColor());
4967 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004968 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004969 button.setTextViewText(R.id.action0, processTextSpans(
4970 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004971 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004972 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004973 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004974 button.setTextColor(R.id.action0,
4975 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004976 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004977 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004978 return button;
4979 }
4980
Joe Onoratocb109a02011-01-18 17:57:41 -08004981 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004982 * Ensures contrast on color spans against a background color. also returns the color of the
4983 * text if a span was found that spans over the whole text.
4984 *
4985 * @param charSequence the charSequence on which the spans are
4986 * @param background the background color to ensure the contrast against
4987 * @param outResultColor an array in which a color will be returned as the first element if
4988 * there exists a full length color span.
4989 * @return the contrasted charSequence
4990 */
4991 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4992 ColorStateList[] outResultColor) {
4993 if (charSequence instanceof Spanned) {
4994 Spanned ss = (Spanned) charSequence;
4995 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4996 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4997 for (Object span : spans) {
4998 Object resultSpan = span;
4999 int spanStart = ss.getSpanStart(span);
5000 int spanEnd = ss.getSpanEnd(span);
5001 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5002 if (resultSpan instanceof CharacterStyle) {
5003 resultSpan = ((CharacterStyle) span).getUnderlying();
5004 }
5005 if (resultSpan instanceof TextAppearanceSpan) {
5006 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5007 ColorStateList textColor = originalSpan.getTextColor();
5008 if (textColor != null) {
5009 int[] colors = textColor.getColors();
5010 int[] newColors = new int[colors.length];
5011 for (int i = 0; i < newColors.length; i++) {
5012 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005013 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005014 }
5015 textColor = new ColorStateList(textColor.getStates().clone(),
5016 newColors);
5017 resultSpan = new TextAppearanceSpan(
5018 originalSpan.getFamily(),
5019 originalSpan.getTextStyle(),
5020 originalSpan.getTextSize(),
5021 textColor,
5022 originalSpan.getLinkTextColor());
5023 if (fullLength) {
5024 outResultColor[0] = new ColorStateList(
5025 textColor.getStates().clone(), newColors);
5026 }
5027 }
5028 } else if (resultSpan instanceof ForegroundColorSpan) {
5029 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5030 int foregroundColor = originalSpan.getForegroundColor();
5031 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005032 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005033 resultSpan = new ForegroundColorSpan(foregroundColor);
5034 if (fullLength) {
5035 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5036 }
5037 } else {
5038 resultSpan = span;
5039 }
5040
5041 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5042 }
5043 return builder;
5044 }
5045 return charSequence;
5046 }
5047
5048 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005049 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005050 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005051 */
5052 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005053 if (!mIsLegacyInitialized) {
5054 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5055 < Build.VERSION_CODES.LOLLIPOP;
5056 mIsLegacyInitialized = true;
5057 }
5058 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005059 }
5060
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005061 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005062 return processLegacyText(charSequence, false /* ambient */);
5063 }
5064
5065 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5066 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5067 boolean wantLightText = ambient;
5068 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005069 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005070 } else {
5071 return charSequence;
5072 }
5073 }
5074
Dan Sandler26e81cf2014-05-06 10:01:27 -04005075 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005076 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005077 */
Adrian Roos487374f2017-01-11 15:48:14 -08005078 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5079 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005080 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005081 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005082 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005083 contentView.setDrawableTint(R.id.icon, false, color,
5084 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005085
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005086 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005087 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005088 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005089 }
5090
Dan Sandler26e81cf2014-05-06 10:01:27 -04005091 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005092 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005093 * if it's grayscale).
5094 */
5095 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005096 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5097 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005098 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005099 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005100 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5101 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005102 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005103 }
5104
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005105 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005106 if (mN.color != COLOR_DEFAULT) {
5107 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005108 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005109 }
5110
Adrian Roos4ff3b122016-02-01 12:26:13 -08005111 int resolveContrastColor() {
5112 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5113 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005114 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005115
Selim Cinekac5f0272017-05-02 16:05:41 -07005116 int color;
5117 int background = mBackgroundColorHint;
5118 if (mBackgroundColorHint == COLOR_INVALID) {
5119 background = mContext.getColor(
5120 com.android.internal.R.color.notification_material_background_color);
5121 }
5122 if (mN.color == COLOR_DEFAULT) {
5123 ensureColors();
5124 color = mSecondaryTextColor;
5125 } else {
5126 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005127 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005128 }
5129 if (Color.alpha(color) < 255) {
5130 // alpha doesn't go well for color filters, so let's blend it manually
5131 color = NotificationColorUtil.compositeColors(color, background);
5132 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005133 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005134 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005135 }
5136
Adrian Roos487374f2017-01-11 15:48:14 -08005137 int resolveAmbientColor() {
5138 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5139 return mCachedAmbientColor;
5140 }
5141 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5142
5143 mCachedAmbientColorIsFor = mN.color;
5144 return mCachedAmbientColor = contrasted;
5145 }
5146
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005147 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005148 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005149 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005150 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005151 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005152 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005153 mN.actions = new Action[mActions.size()];
5154 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005155 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005156 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005157 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005158 }
Selim Cinek247fa012016-02-18 09:50:48 -08005159 if (mN.bigContentView != null || mN.contentView != null
5160 || mN.headsUpContentView != null) {
5161 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5162 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005163 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005164 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005165
Julia Reynolds3b848122016-02-26 10:45:32 -05005166 /**
5167 * Creates a Builder from an existing notification so further changes can be made.
5168 * @param context The context for your application / activity.
5169 * @param n The notification to create a Builder from.
5170 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005171 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005172 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005173 ApplicationInfo applicationInfo = n.extras.getParcelable(
5174 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005175 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005176 if (applicationInfo != null) {
5177 try {
5178 builderContext = context.createApplicationContext(applicationInfo,
5179 Context.CONTEXT_RESTRICTED);
5180 } catch (NameNotFoundException e) {
5181 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5182 builderContext = context; // try with our context
5183 }
5184 } else {
5185 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005186 }
5187
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005188 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005189 }
5190
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005191 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005192 * @deprecated Use {@link #build()} instead.
5193 */
5194 @Deprecated
5195 public Notification getNotification() {
5196 return build();
5197 }
5198
5199 /**
5200 * Combine all of the options that have been set and return a new {@link Notification}
5201 * object.
5202 */
5203 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005204 // first, add any extras from the calling code
5205 if (mUserExtras != null) {
5206 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005207 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005208
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005209 mN.creationTime = System.currentTimeMillis();
5210
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005211 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005212 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005213
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005214 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005215
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005216 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005217 mStyle.reduceImageSizes(mContext);
5218 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005219 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005220 }
Selim Cinekd0426622017-07-11 13:19:59 +02005221 mN.reduceImageSizes(mContext);
5222
Adrian Roos5081c0d2016-02-26 16:04:19 -08005223 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005224 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005225 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005226 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005227 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5228 mN.contentView.getSequenceNumber());
5229 }
5230 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005231 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005232 if (mN.bigContentView != null) {
5233 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5234 mN.bigContentView.getSequenceNumber());
5235 }
5236 }
5237 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005238 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005239 if (mN.headsUpContentView != null) {
5240 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5241 mN.headsUpContentView.getSequenceNumber());
5242 }
5243 }
5244 }
5245
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005246 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5247 mN.flags |= FLAG_SHOW_LIGHTS;
5248 }
5249
Adrian Roosfb921842017-10-26 14:49:56 +02005250 mN.allPendingIntents = null;
5251
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005252 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005253 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005254
5255 /**
5256 * Apply this Builder to an existing {@link Notification} object.
5257 *
5258 * @hide
5259 */
5260 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005261 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005262 return n;
5263 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005264
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005265 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005266 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005267 * change. Also removes extenders on low ram devices, as
5268 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005269 *
5270 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5271 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005272 * @hide
5273 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005274 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005275 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005276
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005277 // Only strip views for known Styles because we won't know how to
5278 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005279 if (!isLowRam
5280 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005281 && getNotificationStyleClass(templateClass) == null) {
5282 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005283 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005284
5285 // Only strip unmodified BuilderRemoteViews.
5286 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005287 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005288 n.contentView.getSequenceNumber();
5289 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005290 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005291 n.bigContentView.getSequenceNumber();
5292 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005293 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005294 n.headsUpContentView.getSequenceNumber();
5295
5296 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005297 if (!isLowRam
5298 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005299 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005300 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005301
5302 Notification clone = n.clone();
5303 if (stripContentView) {
5304 clone.contentView = null;
5305 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5306 }
5307 if (stripBigContentView) {
5308 clone.bigContentView = null;
5309 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5310 }
5311 if (stripHeadsUpContentView) {
5312 clone.headsUpContentView = null;
5313 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5314 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005315 if (isLowRam) {
5316 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5317 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5318 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5319 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005320 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005321 }
5322
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005323 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005324 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005325 }
5326
5327 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005328 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005329 }
5330
5331 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005332 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005333 }
5334
5335 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005336 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005337 }
5338
5339 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005340 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005341 }
5342
Adrian Roosc1a80b02016-04-05 14:54:55 -07005343 private int getMessagingLayoutResource() {
5344 return R.layout.notification_template_material_messaging;
5345 }
5346
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005347 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005348 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005349 }
5350
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005351 private int getEmphasizedActionLayoutResource() {
5352 return R.layout.notification_material_action_emphasized;
5353 }
5354
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005355 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005356 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005357 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005358
5359 private int getBackgroundColor() {
5360 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005361 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005362 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005363 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5364 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005365 }
5366 }
5367
5368 private boolean isColorized() {
5369 return mN.isColorized();
5370 }
Selim Cinek99104832017-01-25 14:47:33 -08005371
Anthony Chenad4d1582017-04-10 16:07:58 -07005372 private boolean shouldTintActionButtons() {
5373 return mTintActionButtons;
5374 }
5375
Selim Cinek99104832017-01-25 14:47:33 -08005376 private boolean textColorsNeedInversion() {
5377 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5378 return false;
5379 }
5380 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5381 return targetSdkVersion > Build.VERSION_CODES.M
5382 && targetSdkVersion < Build.VERSION_CODES.O;
5383 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005384
5385 /**
5386 * Set a color palette to be used as the background and textColors
5387 *
5388 * @param backgroundColor the color to be used as the background
5389 * @param foregroundColor the color to be used as the foreground
5390 *
5391 * @hide
5392 */
5393 public void setColorPalette(int backgroundColor, int foregroundColor) {
5394 mBackgroundColor = backgroundColor;
5395 mForegroundColor = foregroundColor;
5396 mTextColorsAreForBackground = COLOR_INVALID;
5397 ensureColors();
5398 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005399
5400 /**
5401 * Sets the background color for this notification to be a different one then the default.
5402 * This is mainly used to calculate contrast and won't necessarily be applied to the
5403 * background.
5404 *
5405 * @hide
5406 */
5407 public void setBackgroundColorHint(int backgroundColor) {
5408 mBackgroundColorHint = backgroundColor;
5409 }
Selim Cineka7679b62017-05-10 16:33:25 -07005410
5411
5412 /**
5413 * Forces all styled remoteViews to be built from scratch and not use any cached
5414 * RemoteViews.
5415 * This is needed for legacy apps that are baking in their remoteviews into the
5416 * notification.
5417 *
5418 * @hide
5419 */
5420 public void setRebuildStyledRemoteViews(boolean rebuild) {
5421 mRebuildStyledRemoteViews = rebuild;
5422 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005423 }
5424
5425 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005426 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5427 * remote views.
5428 *
5429 * @hide
5430 */
5431 void reduceImageSizes(Context context) {
5432 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5433 return;
5434 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005435 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005436 if (mLargeIcon != null || largeIcon != null) {
5437 Resources resources = context.getResources();
5438 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005439 int maxWidth = resources.getDimensionPixelSize(isLowRam
5440 ? R.dimen.notification_right_icon_size_low_ram
5441 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005442 int maxHeight = maxWidth;
5443 if (MediaStyle.class.equals(style)
5444 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005445 maxHeight = resources.getDimensionPixelSize(isLowRam
5446 ? R.dimen.notification_media_image_max_height_low_ram
5447 : R.dimen.notification_media_image_max_height);
5448 maxWidth = resources.getDimensionPixelSize(isLowRam
5449 ? R.dimen.notification_media_image_max_width_low_ram
5450 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005451 }
5452 if (mLargeIcon != null) {
5453 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5454 }
5455 if (largeIcon != null) {
5456 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5457 }
5458 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005459 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5460 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5461 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005462 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5463 }
5464
Selim Cineka8cb1262017-08-15 16:53:44 -07005465 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5466 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005467 if (remoteView != null) {
5468 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005469 int maxWidth = resources.getDimensionPixelSize(isLowRam
5470 ? R.dimen.notification_custom_view_max_image_width_low_ram
5471 : R.dimen.notification_custom_view_max_image_width);
5472 int maxHeight = resources.getDimensionPixelSize(isLowRam
5473 ? R.dimen.notification_custom_view_max_image_height_low_ram
5474 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005475 remoteView.reduceImageSizes(maxWidth, maxHeight);
5476 }
5477 }
5478
5479 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005480 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005481 */
Selim Cinek22714f12017-04-13 16:23:53 -07005482 private boolean isForegroundService() {
5483 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005484 }
5485
5486 /**
Selim Cinek99104832017-01-25 14:47:33 -08005487 * @return whether this notification has a media session attached
5488 * @hide
5489 */
5490 public boolean hasMediaSession() {
5491 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5492 }
5493
5494 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005495 * @return the style class of this notification
5496 * @hide
5497 */
5498 public Class<? extends Notification.Style> getNotificationStyle() {
5499 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5500
5501 if (!TextUtils.isEmpty(templateClass)) {
5502 return Notification.getNotificationStyleClass(templateClass);
5503 }
5504 return null;
5505 }
5506
5507 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005508 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005509 *
5510 * @hide
5511 */
5512 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005513 if (isColorizedMedia()) {
5514 return true;
5515 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005516 return extras.getBoolean(EXTRA_COLORIZED)
5517 && (hasColorizedPermission() || isForegroundService());
5518 }
5519
5520 /**
5521 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5522 * permission. The permission is checked when a notification is enqueued.
5523 */
5524 private boolean hasColorizedPermission() {
5525 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005526 }
5527
5528 /**
5529 * @return true if this notification is colorized and it is a media notification
5530 *
5531 * @hide
5532 */
5533 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005534 Class<? extends Style> style = getNotificationStyle();
5535 if (MediaStyle.class.equals(style)) {
5536 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5537 if ((colorized == null || colorized) && hasMediaSession()) {
5538 return true;
5539 }
5540 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5541 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5542 return true;
5543 }
5544 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005545 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005546 }
5547
Selim Cinek0847acd2017-04-24 19:48:29 -07005548
5549 /**
5550 * @return true if this is a media notification
5551 *
5552 * @hide
5553 */
5554 public boolean isMediaNotification() {
5555 Class<? extends Style> style = getNotificationStyle();
5556 if (MediaStyle.class.equals(style)) {
5557 return true;
5558 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5559 return true;
5560 }
5561 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005562 }
5563
Selim Cinek279fa862016-06-14 10:57:25 -07005564 private boolean hasLargeIcon() {
5565 return mLargeIcon != null || largeIcon != null;
5566 }
5567
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005568 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005569 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005570 * @hide
5571 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005572 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005573 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5574 }
5575
5576 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005577 * @return true if the notification will show a chronometer; false otherwise
5578 * @hide
5579 */
5580 public boolean showsChronometer() {
5581 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5582 }
5583
5584 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005585 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005586 */
5587 @SystemApi
5588 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5589 Class<? extends Style>[] classes = new Class[] {
5590 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5591 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5592 MessagingStyle.class };
5593 for (Class<? extends Style> innerClass : classes) {
5594 if (templateClass.equals(innerClass.getName())) {
5595 return innerClass;
5596 }
5597 }
5598 return null;
5599 }
5600
5601 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005602 * An object that can apply a rich notification style to a {@link Notification.Builder}
5603 * object.
5604 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005605 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005606 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005607
5608 /**
5609 * @hide
5610 */
5611 protected CharSequence mSummaryText = null;
5612
5613 /**
5614 * @hide
5615 */
5616 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005617
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005618 protected Builder mBuilder;
5619
Chris Wrend6297db2012-05-03 16:20:13 -04005620 /**
5621 * Overrides ContentTitle in the big form of the template.
5622 * This defaults to the value passed to setContentTitle().
5623 */
5624 protected void internalSetBigContentTitle(CharSequence title) {
5625 mBigContentTitle = title;
5626 }
5627
5628 /**
5629 * Set the first line of text after the detail section in the big form of the template.
5630 */
5631 protected void internalSetSummaryText(CharSequence cs) {
5632 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005633 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005634 }
5635
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005636 public void setBuilder(Builder builder) {
5637 if (mBuilder != builder) {
5638 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005639 if (mBuilder != null) {
5640 mBuilder.setStyle(this);
5641 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005642 }
5643 }
5644
Chris Wrend6297db2012-05-03 16:20:13 -04005645 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005646 if (mBuilder == null) {
5647 throw new IllegalArgumentException("Style requires a valid Builder object");
5648 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005649 }
Chris Wrend6297db2012-05-03 16:20:13 -04005650
5651 protected RemoteViews getStandardView(int layoutId) {
5652 checkBuilder();
5653
Christoph Studer4600f9b2014-07-22 22:44:43 +02005654 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005655 CharSequence oldBuilderContentTitle =
5656 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005657 if (mBigContentTitle != null) {
5658 mBuilder.setContentTitle(mBigContentTitle);
5659 }
5660
Chris Wrend6297db2012-05-03 16:20:13 -04005661 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5662
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005663 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005664
Chris Wrend6297db2012-05-03 16:20:13 -04005665 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5666 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005667 } else {
5668 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005669 }
5670
Chris Wrend6297db2012-05-03 16:20:13 -04005671 return contentView;
5672 }
5673
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005674 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005675 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005676 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005677 *
5678 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005679 * @hide
5680 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005681 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005682 return null;
5683 }
5684
5685 /**
5686 * Construct a Style-specific RemoteViews for the final big notification layout.
5687 * @hide
5688 */
5689 public RemoteViews makeBigContentView() {
5690 return null;
5691 }
5692
5693 /**
5694 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005695 *
5696 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005697 * @hide
5698 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005699 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005700 return null;
5701 }
5702
5703 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005704 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005705 * @hide
5706 */
5707 public void addExtras(Bundle extras) {
5708 if (mSummaryTextSet) {
5709 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5710 }
5711 if (mBigContentTitle != null) {
5712 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5713 }
Chris Wren91ad5632013-06-05 15:05:57 -04005714 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005715 }
5716
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005717 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005718 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005719 * @hide
5720 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005721 protected void restoreFromExtras(Bundle extras) {
5722 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5723 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5724 mSummaryTextSet = true;
5725 }
5726 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5727 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5728 }
5729 }
5730
5731
5732 /**
5733 * @hide
5734 */
5735 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005736 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005737 return wip;
5738 }
5739
Daniel Sandler0ec46202015-06-24 01:27:05 -04005740 /**
5741 * @hide
5742 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005743 public void purgeResources() {}
5744
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005745 /**
5746 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5747 * attached to.
5748 *
5749 * @return the fully constructed Notification.
5750 */
5751 public Notification build() {
5752 checkBuilder();
5753 return mBuilder.build();
5754 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005755
5756 /**
5757 * @hide
5758 * @return true if the style positions the progress bar on the second line; false if the
5759 * style hides the progress bar
5760 */
5761 protected boolean hasProgress() {
5762 return true;
5763 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005764
5765 /**
5766 * @hide
5767 * @return Whether we should put the summary be put into the notification header
5768 */
5769 public boolean hasSummaryInHeader() {
5770 return true;
5771 }
Selim Cinek593610c2016-02-16 18:42:57 -08005772
5773 /**
5774 * @hide
5775 * @return Whether custom content views are displayed inline in the style
5776 */
5777 public boolean displayCustomViewInline() {
5778 return false;
5779 }
Selim Cinekd0426622017-07-11 13:19:59 +02005780
5781 /**
5782 * Reduces the image sizes contained in this style.
5783 *
5784 * @hide
5785 */
5786 public void reduceImageSizes(Context context) {
5787 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005788 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005789
5790 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005791 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005792 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005793 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005794 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005795 * Notification notif = new Notification.Builder(mContext)
5796 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5797 * .setContentText(subject)
5798 * .setSmallIcon(R.drawable.new_post)
5799 * .setLargeIcon(aBitmap)
5800 * .setStyle(new Notification.BigPictureStyle()
5801 * .bigPicture(aBigBitmap))
5802 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005803 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005804 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005805 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005806 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005807 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005808 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005809 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005810 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005811
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005812 public BigPictureStyle() {
5813 }
5814
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005815 /**
5816 * @deprecated use {@code BigPictureStyle()}.
5817 */
5818 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005819 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005820 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005821 }
5822
Chris Wrend6297db2012-05-03 16:20:13 -04005823 /**
5824 * Overrides ContentTitle in the big form of the template.
5825 * This defaults to the value passed to setContentTitle().
5826 */
5827 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005828 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005829 return this;
5830 }
5831
5832 /**
5833 * Set the first line of text after the detail section in the big form of the template.
5834 */
5835 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005836 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005837 return this;
5838 }
5839
Chris Wren0bd664d2012-08-01 13:56:56 -04005840 /**
5841 * Provide the bitmap to be used as the payload for the BigPicture notification.
5842 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005843 public BigPictureStyle bigPicture(Bitmap b) {
5844 mPicture = b;
5845 return this;
5846 }
5847
Chris Wren3745a3d2012-05-22 15:11:52 -04005848 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005849 * Override the large icon when the big notification is shown.
5850 */
5851 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005852 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5853 }
5854
5855 /**
5856 * Override the large icon when the big notification is shown.
5857 */
5858 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005859 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005860 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005861 return this;
5862 }
5863
Riley Andrews0394a0c2015-11-03 23:36:52 -08005864 /** @hide */
5865 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5866
Daniel Sandler0ec46202015-06-24 01:27:05 -04005867 /**
5868 * @hide
5869 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005870 @Override
5871 public void purgeResources() {
5872 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005873 if (mPicture != null &&
5874 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005875 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005876 mPicture = mPicture.createAshmemBitmap();
5877 }
5878 if (mBigLargeIcon != null) {
5879 mBigLargeIcon.convertToAshmem();
5880 }
5881 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005882
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005883 /**
5884 * @hide
5885 */
Selim Cinekd0426622017-07-11 13:19:59 +02005886 @Override
5887 public void reduceImageSizes(Context context) {
5888 super.reduceImageSizes(context);
5889 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005890 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005891 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005892 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5893 ? R.dimen.notification_big_picture_max_height_low_ram
5894 : R.dimen.notification_big_picture_max_height);
5895 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5896 ? R.dimen.notification_big_picture_max_width_low_ram
5897 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005898 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5899 }
5900 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005901 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5902 ? R.dimen.notification_right_icon_size_low_ram
5903 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005904 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5905 }
5906 }
5907
5908 /**
5909 * @hide
5910 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005911 public RemoteViews makeBigContentView() {
5912 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005913 // This covers the following cases:
5914 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005915 // mN.mLargeIcon
5916 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005917 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005918 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005919 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005920 oldLargeIcon = mBuilder.mN.mLargeIcon;
5921 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005922 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5923 // replacement if the other one is null. Because we're restoring these legacy icons
5924 // for old listeners, this is in general non-null.
5925 largeIconLegacy = mBuilder.mN.largeIcon;
5926 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005927 }
5928
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005929 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005930 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005931 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5932 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005933 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005934 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005935 }
Selim Cinek279fa862016-06-14 10:57:25 -07005936 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005937
Christoph Studer5c510ee2014-12-15 16:32:27 +01005938 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005939 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005940 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005941 }
5942
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005943 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005944 return contentView;
5945 }
5946
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005947 /**
5948 * @hide
5949 */
5950 public void addExtras(Bundle extras) {
5951 super.addExtras(extras);
5952
5953 if (mBigLargeIconSet) {
5954 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5955 }
5956 extras.putParcelable(EXTRA_PICTURE, mPicture);
5957 }
5958
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005959 /**
5960 * @hide
5961 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005962 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005963 protected void restoreFromExtras(Bundle extras) {
5964 super.restoreFromExtras(extras);
5965
5966 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005967 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005968 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005969 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005970 mPicture = extras.getParcelable(EXTRA_PICTURE);
5971 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005972
5973 /**
5974 * @hide
5975 */
5976 @Override
5977 public boolean hasSummaryInHeader() {
5978 return false;
5979 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005980 }
5981
5982 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005983 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005984 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005985 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005986 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005987 * Notification notif = new Notification.Builder(mContext)
5988 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5989 * .setContentText(subject)
5990 * .setSmallIcon(R.drawable.new_mail)
5991 * .setLargeIcon(aBitmap)
5992 * .setStyle(new Notification.BigTextStyle()
5993 * .bigText(aVeryLongString))
5994 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005995 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005996 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005997 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005998 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005999 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006000
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006001 private CharSequence mBigText;
6002
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006003 public BigTextStyle() {
6004 }
6005
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006006 /**
6007 * @deprecated use {@code BigTextStyle()}.
6008 */
6009 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006010 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006011 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006012 }
6013
Chris Wrend6297db2012-05-03 16:20:13 -04006014 /**
6015 * Overrides ContentTitle in the big form of the template.
6016 * This defaults to the value passed to setContentTitle().
6017 */
6018 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006019 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006020 return this;
6021 }
6022
6023 /**
6024 * Set the first line of text after the detail section in the big form of the template.
6025 */
6026 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006027 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006028 return this;
6029 }
6030
Chris Wren0bd664d2012-08-01 13:56:56 -04006031 /**
6032 * Provide the longer text to be displayed in the big form of the
6033 * template in place of the content text.
6034 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006035 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006036 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006037 return this;
6038 }
6039
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006040 /**
6041 * @hide
6042 */
6043 public void addExtras(Bundle extras) {
6044 super.addExtras(extras);
6045
Christoph Studer4600f9b2014-07-22 22:44:43 +02006046 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6047 }
6048
6049 /**
6050 * @hide
6051 */
6052 @Override
6053 protected void restoreFromExtras(Bundle extras) {
6054 super.restoreFromExtras(extras);
6055
6056 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006057 }
6058
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006059 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006060 * @param increasedHeight true if this layout be created with an increased height.
6061 *
6062 * @hide
6063 */
6064 @Override
6065 public RemoteViews makeContentView(boolean increasedHeight) {
6066 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006067 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006068 mBuilder.mActions = new ArrayList<>();
6069 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006070 mBuilder.mActions = mBuilder.mOriginalActions;
6071 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006072 return remoteViews;
6073 }
6074 return super.makeContentView(increasedHeight);
6075 }
6076
6077 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006078 * @hide
6079 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006080 @Override
6081 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6082 if (increasedHeight && mBuilder.mActions.size() > 0) {
6083 return makeBigContentView();
6084 }
6085 return super.makeHeadsUpContentView(increasedHeight);
6086 }
6087
6088 /**
6089 * @hide
6090 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006091 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006092
6093 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006094 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006095 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006096
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006097 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006098
Selim Cinek75998782016-04-26 10:39:17 -07006099 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006100
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006101 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006102 if (TextUtils.isEmpty(bigTextText)) {
6103 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6104 // experience
6105 bigTextText = mBuilder.processLegacyText(text);
6106 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006107 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006108
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006109 return contentView;
6110 }
6111
Adrian Roosb1f427c2016-05-26 12:27:15 -07006112 static void applyBigTextContentView(Builder builder,
6113 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006114 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006115 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006116 contentView.setViewVisibility(R.id.big_text,
6117 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006118 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006119 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006120 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006121
6122 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006123 * Helper class for generating large-format notifications that include multiple back-and-forth
6124 * messages of varying types between any number of people.
6125 *
6126 * <br>
6127 * If the platform does not provide large-format notifications, this method has no effect. The
6128 * user will always see the normal notification view.
6129 * <br>
6130 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6131 * so:
6132 * <pre class="prettyprint">
6133 *
6134 * Notification noti = new Notification.Builder()
6135 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6136 * .setContentText(subject)
6137 * .setSmallIcon(R.drawable.new_message)
6138 * .setLargeIcon(aBitmap)
6139 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6140 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6141 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6142 * .build();
6143 * </pre>
6144 */
6145 public static class MessagingStyle extends Style {
6146
6147 /**
6148 * The maximum number of messages that will be retained in the Notification itself (the
6149 * number displayed is up to the platform).
6150 */
6151 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6152
Selim Cinekcb8b9852017-12-15 18:01:52 -08006153 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006154 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006155 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006156 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006157 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006158
6159 MessagingStyle() {
6160 }
6161
6162 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006163 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6164 * user before the posting app reposts the notification with those messages after they've
6165 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006166 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006167 *
6168 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006169 */
Alex Hillsfd590442016-10-07 09:52:44 -04006170 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006171 this(new Person().setName(userDisplayName));
6172 }
6173
6174 /**
6175 * @param user Required - The person displayed for any messages that are sent by the
6176 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6177 * who don't have a Person associated with it will be displayed as if they were sent
6178 * by this user. The user also needs to have a valid name associated with it.
6179 */
6180 public MessagingStyle(@NonNull Person user) {
6181 mUser = user;
6182 if (user == null || user.getName() == null) {
6183 throw new RuntimeException("user must be valid and have a name");
6184 }
6185 }
6186
6187 /**
6188 * @return the user to be displayed for any replies sent by the user
6189 */
6190 public Person getUser() {
6191 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006192 }
6193
6194 /**
6195 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006196 *
6197 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006198 */
6199 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006200 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006201 }
6202
6203 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006204 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6205 *
Kodlee Yin14656422017-12-22 17:00:46 -08006206 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6207 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6208 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6209 * {@code true} and passing {@code null} will make it return {@code false}. In
6210 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6211 * to set group conversation status.
6212 *
6213 * @param conversationTitle Title displayed for this conversation
6214 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006215 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006216 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006217 mConversationTitle = conversationTitle;
6218 return this;
6219 }
6220
6221 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006222 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006223 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006224 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006225 public CharSequence getConversationTitle() {
6226 return mConversationTitle;
6227 }
6228
6229 /**
6230 * Adds a message for display by this notification. Convenience call for a simple
6231 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6232 * @param text A {@link CharSequence} to be displayed as the message content
6233 * @param timestamp Time at which the message arrived
6234 * @param sender A {@link CharSequence} to be used for displaying the name of the
6235 * sender. Should be <code>null</code> for messages by the current user, in which case
6236 * the platform will insert {@link #getUserDisplayName()}.
6237 * Should be unique amongst all individuals in the conversation, and should be
6238 * consistent during re-posts of the notification.
6239 *
6240 * @see Message#Message(CharSequence, long, CharSequence)
6241 *
6242 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006243 *
6244 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006245 */
6246 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006247 return addMessage(text, timestamp,
6248 sender == null ? null : new Person().setName(sender));
6249 }
6250
6251 /**
6252 * Adds a message for display by this notification. Convenience call for a simple
6253 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6254 * @param text A {@link CharSequence} to be displayed as the message content
6255 * @param timestamp Time at which the message arrived
6256 * @param sender The {@link Person} who sent the message.
6257 * Should be <code>null</code> for messages by the current user, in which case
6258 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6259 *
6260 * @see Message#Message(CharSequence, long, CharSequence)
6261 *
6262 * @return this object for method chaining
6263 */
6264 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006265 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006266 }
6267
6268 /**
6269 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006270 *
6271 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6272 * the newest last.
6273 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006274 * @param message The {@link Message} to be displayed
6275 * @return this object for method chaining
6276 */
6277 public MessagingStyle addMessage(Message message) {
6278 mMessages.add(message);
6279 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6280 mMessages.remove(0);
6281 }
6282 return this;
6283 }
6284
6285 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006286 * Adds a {@link Message} for historic context in this notification.
6287 *
6288 * <p>Messages should be added as historic if they are not the main subject of the
6289 * notification but may give context to a conversation. The system may choose to present
6290 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6291 *
6292 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6293 * the newest last.
6294 *
6295 * @param message The historic {@link Message} to be added
6296 * @return this object for method chaining
6297 */
6298 public MessagingStyle addHistoricMessage(Message message) {
6299 mHistoricMessages.add(message);
6300 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6301 mHistoricMessages.remove(0);
6302 }
6303 return this;
6304 }
6305
6306 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006307 * Gets the list of {@code Message} objects that represent the notification
6308 */
6309 public List<Message> getMessages() {
6310 return mMessages;
6311 }
6312
6313 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006314 * Gets the list of historic {@code Message}s in the notification.
6315 */
6316 public List<Message> getHistoricMessages() {
6317 return mHistoricMessages;
6318 }
6319
6320 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006321 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006322 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006323 * @param isGroupConversation {@code true} if the conversation represents a group,
6324 * {@code false} otherwise.
6325 * @return this object for method chaining
6326 */
6327 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6328 mIsGroupConversation = isGroupConversation;
6329 return this;
6330 }
6331
6332 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006333 * Returns {@code true} if this notification represents a group conversation, otherwise
6334 * {@code false}.
6335 *
6336 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6337 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6338 * not the conversation title is set; returning {@code true} if the conversation title is
6339 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6340 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6341 * named, non-group conversations.
6342 *
6343 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006344 */
6345 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006346 // When target SDK version is < P, a non-null conversation title dictates if this is
6347 // as group conversation.
6348 if (mBuilder != null
6349 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6350 < Build.VERSION_CODES.P) {
6351 return mConversationTitle != null;
6352 }
6353
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006354 return mIsGroupConversation;
6355 }
6356
6357 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006358 * @hide
6359 */
6360 @Override
6361 public void addExtras(Bundle extras) {
6362 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006363 if (mUser != null) {
6364 // For legacy usages
6365 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6366 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006367 }
6368 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006369 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006370 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006371 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6372 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006373 }
Adrian Roos437cd562017-01-18 15:47:03 -08006374 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6375 Message.getBundleArrayForMessages(mHistoricMessages));
6376 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006377
6378 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006379 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006380 }
6381
6382 private void fixTitleAndTextExtras(Bundle extras) {
6383 Message m = findLatestIncomingMessage();
6384 CharSequence text = (m == null) ? null : m.mText;
6385 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006386 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6387 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006388 CharSequence title;
6389 if (!TextUtils.isEmpty(mConversationTitle)) {
6390 if (!TextUtils.isEmpty(sender)) {
6391 BidiFormatter bidi = BidiFormatter.getInstance();
6392 title = mBuilder.mContext.getString(
6393 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006394 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006395 } else {
6396 title = mConversationTitle;
6397 }
6398 } else {
6399 title = sender;
6400 }
6401
6402 if (title != null) {
6403 extras.putCharSequence(EXTRA_TITLE, title);
6404 }
6405 if (text != null) {
6406 extras.putCharSequence(EXTRA_TEXT, text);
6407 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006408 }
6409
6410 /**
6411 * @hide
6412 */
6413 @Override
6414 protected void restoreFromExtras(Bundle extras) {
6415 super.restoreFromExtras(extras);
6416
Selim Cinekcb8b9852017-12-15 18:01:52 -08006417 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6418 if (mUser == null) {
6419 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6420 mUser = new Person().setName(displayName);
6421 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006422 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006423 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006424 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006425 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006426 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006427 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006428 }
6429
6430 /**
6431 * @hide
6432 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006433 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006434 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006435 mBuilder.mOriginalActions = mBuilder.mActions;
6436 mBuilder.mActions = new ArrayList<>();
Selim Cinekafeed292017-12-12 17:32:44 -08006437 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006438 mBuilder.mActions = mBuilder.mOriginalActions;
6439 mBuilder.mOriginalActions = null;
6440 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006441 }
6442
6443 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006444 return findLatestIncomingMessage(mMessages);
6445 }
6446
6447 /**
6448 * @hide
6449 */
6450 @Nullable
6451 public static Message findLatestIncomingMessage(
6452 List<Message> messages) {
6453 for (int i = messages.size() - 1; i >= 0; i--) {
6454 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006455 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006456 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006457 return m;
6458 }
6459 }
Selim Cinek88188f22017-09-19 16:46:56 -07006460 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006461 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006462 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006463 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006464 return null;
6465 }
6466
6467 /**
6468 * @hide
6469 */
6470 @Override
6471 public RemoteViews makeBigContentView() {
Selim Cinekafeed292017-12-12 17:32:44 -08006472 return makeBigContentView(false /* showRightIcon */);
6473 }
6474
6475 @NonNull
6476 private RemoteViews makeBigContentView(boolean showRightIcon) {
Selim Cinek88188f22017-09-19 16:46:56 -07006477 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006478 ? super.mBigContentTitle
6479 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006480 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006481 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006482 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006483 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006484 nameReplacement = conversationTitle;
6485 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006486 }
Adrian Roos48d746a2016-04-12 14:57:28 -07006487 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006488 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006489 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinekafeed292017-12-12 17:32:44 -08006490 .hideLargeIcon(!showRightIcon || isOneToOne)
6491 .headerTextSecondary(conversationTitle)
6492 .alwaysShowReply(showRightIcon));
Selim Cinek88188f22017-09-19 16:46:56 -07006493 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006494 // also update the end margin if there is an image
6495 int endMargin = R.dimen.notification_content_margin_end;
6496 if (mBuilder.mN.hasLargeIcon() && showRightIcon) {
6497 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6498 }
6499 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006500 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6501 mBuilder.resolveContrastColor());
6502 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6503 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006504 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6505 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006506 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6507 isOneToOne);
6508 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6509 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006510 return contentView;
6511 }
6512
Selim Cinekf7409db2017-10-24 16:17:14 -07006513 private boolean hasOnlyWhiteSpaceSenders() {
6514 for (int i = 0; i < mMessages.size(); i++) {
6515 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006516 Person sender = m.getSenderPerson();
6517 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006518 return false;
6519 }
6520 }
6521 return true;
6522 }
6523
6524 private boolean isWhiteSpace(CharSequence sender) {
6525 if (TextUtils.isEmpty(sender)) {
6526 return true;
6527 }
6528 if (sender.toString().matches("^\\s*$")) {
6529 return true;
6530 }
6531 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6532 // For the presentation that we had.
6533 for (int i = 0; i < sender.length(); i++) {
6534 char c = sender.charAt(i);
6535 if (c != '\u200B') {
6536 return false;
6537 }
6538 }
6539 return true;
6540 }
6541
Selim Cinek88188f22017-09-19 16:46:56 -07006542 private CharSequence createConversationTitleFromMessages() {
6543 ArraySet<CharSequence> names = new ArraySet<>();
6544 for (int i = 0; i < mMessages.size(); i++) {
6545 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006546 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006547 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006548 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006549 }
6550 }
6551 SpannableStringBuilder title = new SpannableStringBuilder();
6552 int size = names.size();
6553 for (int i = 0; i < size; i++) {
6554 CharSequence name = names.valueAt(i);
6555 if (!TextUtils.isEmpty(title)) {
6556 title.append(", ");
6557 }
6558 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6559 }
6560 return title;
6561 }
6562
Adrian Roosdedd1df2016-04-26 16:38:47 -07006563 /**
6564 * @hide
6565 */
6566 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006567 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekafeed292017-12-12 17:32:44 -08006568 RemoteViews remoteViews = makeBigContentView(true /* showRightIcon */);
Selim Cineke62255c2017-09-28 18:23:23 -07006569 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6570 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006571 }
6572
Adrian Roosc1a80b02016-04-05 14:54:55 -07006573 private static TextAppearanceSpan makeFontColorSpan(int color) {
6574 return new TextAppearanceSpan(null, 0, 0,
6575 ColorStateList.valueOf(color), null);
6576 }
6577
Alex Hillsd9b04d92016-04-11 16:38:16 -04006578 public static final class Message {
6579
6580 static final String KEY_TEXT = "text";
6581 static final String KEY_TIMESTAMP = "time";
6582 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006583 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006584 static final String KEY_DATA_MIME_TYPE = "type";
6585 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006586 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006587
6588 private final CharSequence mText;
6589 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006590 @Nullable
6591 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006592
Shane Brennan5a871862017-03-11 13:14:17 -08006593 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006594 private String mDataMimeType;
6595 private Uri mDataUri;
6596
6597 /**
6598 * Constructor
6599 * @param text A {@link CharSequence} to be displayed as the message content
6600 * @param timestamp Time at which the message arrived
6601 * @param sender A {@link CharSequence} to be used for displaying the name of the
6602 * sender. Should be <code>null</code> for messages by the current user, in which case
6603 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6604 * Should be unique amongst all individuals in the conversation, and should be
6605 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006606 *
6607 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006608 */
6609 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006610 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6611 }
6612
6613 /**
6614 * Constructor
6615 * @param text A {@link CharSequence} to be displayed as the message content
6616 * @param timestamp Time at which the message arrived
6617 * @param sender The {@link Person} who sent the message.
6618 * Should be <code>null</code> for messages by the current user, in which case
6619 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6620 * <p>
6621 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6622 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6623 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6624 * in order to differentiate between the different users.
6625 * </p>
6626 */
6627 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006628 mText = text;
6629 mTimestamp = timestamp;
6630 mSender = sender;
6631 }
6632
6633 /**
6634 * Sets a binary blob of data and an associated MIME type for a message. In the case
6635 * where the platform doesn't support the MIME type, the original text provided in the
6636 * constructor will be used.
6637 * @param dataMimeType The MIME type of the content. See
6638 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6639 * types on Android and Android Wear.
6640 * @param dataUri The uri containing the content whose type is given by the MIME type.
6641 * <p class="note">
6642 * <ol>
6643 * <li>Notification Listeners including the System UI need permission to access the
6644 * data the Uri points to. The recommended ways to do this are:</li>
6645 * <li>Store the data in your own ContentProvider, making sure that other apps have
6646 * the correct permission to access your provider. The preferred mechanism for
6647 * providing access is to use per-URI permissions which are temporary and only
6648 * grant access to the receiving application. An easy way to create a
6649 * ContentProvider like this is to use the FileProvider helper class.</li>
6650 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6651 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6652 * also store non-media types (see MediaStore.Files for more info). Files can be
6653 * inserted into the MediaStore using scanFile() after which a content:// style
6654 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6655 * Note that once added to the system MediaStore the content is accessible to any
6656 * app on the device.</li>
6657 * </ol>
6658 * @return this object for method chaining
6659 */
6660 public Message setData(String dataMimeType, Uri dataUri) {
6661 mDataMimeType = dataMimeType;
6662 mDataUri = dataUri;
6663 return this;
6664 }
6665
Alex Hillsfc737de2016-03-23 17:33:02 -04006666 /**
6667 * Get the text to be used for this message, or the fallback text if a type and content
6668 * Uri have been set
6669 */
6670 public CharSequence getText() {
6671 return mText;
6672 }
6673
6674 /**
6675 * Get the time at which this message arrived
6676 */
6677 public long getTimestamp() {
6678 return mTimestamp;
6679 }
6680
6681 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006682 * Get the extras Bundle for this message.
6683 */
6684 public Bundle getExtras() {
6685 return mExtras;
6686 }
6687
6688 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006689 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006690 *
6691 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006692 */
6693 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006694 return mSender == null ? null : mSender.getName();
6695 }
6696
6697 /**
6698 * Get the sender associated with this message.
6699 */
6700 @Nullable
6701 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006702 return mSender;
6703 }
6704
6705 /**
6706 * Get the MIME type of the data pointed to by the Uri
6707 */
6708 public String getDataMimeType() {
6709 return mDataMimeType;
6710 }
6711
6712 /**
6713 * Get the the Uri pointing to the content of the message. Can be null, in which case
6714 * {@see #getText()} is used.
6715 */
6716 public Uri getDataUri() {
6717 return mDataUri;
6718 }
6719
Alex Hillsd9b04d92016-04-11 16:38:16 -04006720 private Bundle toBundle() {
6721 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006722 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006723 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006724 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006725 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006726 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006727 // Legacy listeners need this
6728 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6729 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006730 }
6731 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006732 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006733 }
6734 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006735 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006736 }
Shane Brennan5a871862017-03-11 13:14:17 -08006737 if (mExtras != null) {
6738 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6739 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006740 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006741 }
6742
Alex Hillsd9b04d92016-04-11 16:38:16 -04006743 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6744 Bundle[] bundles = new Bundle[messages.size()];
6745 final int N = messages.size();
6746 for (int i = 0; i < N; i++) {
6747 bundles[i] = messages.get(i).toBundle();
6748 }
6749 return bundles;
6750 }
6751
Selim Cinek88188f22017-09-19 16:46:56 -07006752 /**
6753 * @return A list of messages read from the bundles.
6754 *
6755 * @hide
6756 */
6757 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6758 if (bundles == null) {
6759 return new ArrayList<>();
6760 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006761 List<Message> messages = new ArrayList<>(bundles.length);
6762 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006763 if (bundles[i] instanceof Bundle) {
6764 Message message = getMessageFromBundle((Bundle)bundles[i]);
6765 if (message != null) {
6766 messages.add(message);
6767 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006768 }
6769 }
6770 return messages;
6771 }
6772
6773 static Message getMessageFromBundle(Bundle bundle) {
6774 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006775 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006776 return null;
6777 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006778
6779 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6780 if (senderPerson == null) {
6781 // Legacy apps that use compat don't actually provide the sender objects
6782 // We need to fix the compat version to provide people / use
6783 // the native api instead
6784 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6785 if (senderName != null) {
6786 senderPerson = new Person().setName(senderName);
6787 }
6788 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006789 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006790 bundle.getLong(KEY_TIMESTAMP),
6791 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006792 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6793 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006794 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6795 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006796 }
Shane Brennan5a871862017-03-11 13:14:17 -08006797 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6798 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6799 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006800 return message;
6801 }
6802 } catch (ClassCastException e) {
6803 return null;
6804 }
6805 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006806 }
6807 }
6808
6809 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006810 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006811 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006812 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006813 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006814 * Notification notif = new Notification.Builder(mContext)
6815 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6816 * .setContentText(subject)
6817 * .setSmallIcon(R.drawable.new_mail)
6818 * .setLargeIcon(aBitmap)
6819 * .setStyle(new Notification.InboxStyle()
6820 * .addLine(str1)
6821 * .addLine(str2)
6822 * .setContentTitle(&quot;&quot;)
6823 * .setSummaryText(&quot;+3 more&quot;))
6824 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006825 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006826 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006827 * @see Notification#bigContentView
6828 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006829 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006830 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6831
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006832 public InboxStyle() {
6833 }
6834
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006835 /**
6836 * @deprecated use {@code InboxStyle()}.
6837 */
6838 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006839 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006840 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006841 }
6842
Chris Wrend6297db2012-05-03 16:20:13 -04006843 /**
6844 * Overrides ContentTitle in the big form of the template.
6845 * This defaults to the value passed to setContentTitle().
6846 */
6847 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006848 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006849 return this;
6850 }
6851
6852 /**
6853 * Set the first line of text after the detail section in the big form of the template.
6854 */
6855 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006856 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006857 return this;
6858 }
6859
Chris Wren0bd664d2012-08-01 13:56:56 -04006860 /**
6861 * Append a line to the digest section of the Inbox notification.
6862 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006863 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006864 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006865 return this;
6866 }
6867
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006868 /**
6869 * @hide
6870 */
6871 public void addExtras(Bundle extras) {
6872 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006873
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006874 CharSequence[] a = new CharSequence[mTexts.size()];
6875 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6876 }
6877
Christoph Studer4600f9b2014-07-22 22:44:43 +02006878 /**
6879 * @hide
6880 */
6881 @Override
6882 protected void restoreFromExtras(Bundle extras) {
6883 super.restoreFromExtras(extras);
6884
6885 mTexts.clear();
6886 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6887 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6888 }
6889 }
6890
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006891 /**
6892 * @hide
6893 */
6894 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006895 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006896 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006897 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6898 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006899
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006900 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006901
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006902 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006903
Chris Wrend6297db2012-05-03 16:20:13 -04006904 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 -04006905 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006906
Chris Wren4ed80d52012-05-17 09:30:03 -04006907 // Make sure all rows are gone in case we reuse a view.
6908 for (int rowId : rowIds) {
6909 contentView.setViewVisibility(rowId, View.GONE);
6910 }
6911
Daniel Sandler879c5e02012-04-17 16:46:51 -04006912 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006913 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6914 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006915 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006916 int onlyViewId = 0;
6917 int maxRows = rowIds.length;
6918 if (mBuilder.mActions.size() > 0) {
6919 maxRows--;
6920 }
6921 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006922 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006923 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006924 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006925 contentView.setTextViewText(rowIds[i],
6926 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006927 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006928 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006929 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006930 if (first) {
6931 onlyViewId = rowIds[i];
6932 } else {
6933 onlyViewId = 0;
6934 }
Selim Cinek247fa012016-02-18 09:50:48 -08006935 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006936 }
6937 i++;
6938 }
Selim Cinek07c80172016-04-21 16:40:47 -07006939 if (onlyViewId != 0) {
6940 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6941 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6942 R.dimen.notification_text_margin_top);
6943 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6944 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006945
Daniel Sandler879c5e02012-04-17 16:46:51 -04006946 return contentView;
6947 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006948
Selim Cinek247fa012016-02-18 09:50:48 -08006949 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006950 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006951 if (first) {
6952 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6953 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6954 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006955 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006956 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006957 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006958 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006959 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006960 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006961 }
Dan Sandler842dd772014-05-15 09:36:47 -04006962
6963 /**
6964 * Notification style for media playback notifications.
6965 *
6966 * In the expanded form, {@link Notification#bigContentView}, up to 5
6967 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006968 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006969 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6970 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6971 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006972 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006973 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6974 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006975 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006976 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006977 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006978 * Notifications created with MediaStyle will have their category set to
6979 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6980 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006981 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006982 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6983 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006984 * the System UI can identify this as a notification representing an active media session
6985 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6986 *
Selim Cinek99104832017-01-25 14:47:33 -08006987 * <p>
6988 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6989 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6990 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6991 * <p>
6992 *
Dan Sandler842dd772014-05-15 09:36:47 -04006993 * To use this style with your Notification, feed it to
6994 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6995 * <pre class="prettyprint">
6996 * Notification noti = new Notification.Builder()
6997 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006998 * .setContentTitle(&quot;Track title&quot;)
6999 * .setContentText(&quot;Artist - Album&quot;)
7000 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007001 * .setStyle(<b>new Notification.MediaStyle()</b>
7002 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007003 * .build();
7004 * </pre>
7005 *
7006 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007007 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007008 */
7009 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007010 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007011 static final int MAX_MEDIA_BUTTONS = 5;
7012
7013 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007014 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007015
7016 public MediaStyle() {
7017 }
7018
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007019 /**
7020 * @deprecated use {@code MediaStyle()}.
7021 */
7022 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007023 public MediaStyle(Builder builder) {
7024 setBuilder(builder);
7025 }
7026
7027 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007028 * 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 -04007029 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007030 *
7031 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007032 */
7033 public MediaStyle setShowActionsInCompactView(int...actions) {
7034 mActionsToShowInCompact = actions;
7035 return this;
7036 }
7037
7038 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007039 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7040 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007041 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007042 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007043 mToken = token;
7044 return this;
7045 }
7046
Christoph Studer4600f9b2014-07-22 22:44:43 +02007047 /**
7048 * @hide
7049 */
Dan Sandler842dd772014-05-15 09:36:47 -04007050 @Override
7051 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007052 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007053 if (wip.category == null) {
7054 wip.category = Notification.CATEGORY_TRANSPORT;
7055 }
Dan Sandler842dd772014-05-15 09:36:47 -04007056 return wip;
7057 }
7058
Christoph Studer4600f9b2014-07-22 22:44:43 +02007059 /**
7060 * @hide
7061 */
7062 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007063 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007064 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007065 }
7066
7067 /**
7068 * @hide
7069 */
7070 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007071 public RemoteViews makeBigContentView() {
7072 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007073 }
7074
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007075 /**
7076 * @hide
7077 */
7078 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007079 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007080 RemoteViews expanded = makeMediaBigContentView();
7081 return expanded != null ? expanded : makeMediaContentView();
7082 }
7083
Dan Sandler842dd772014-05-15 09:36:47 -04007084 /** @hide */
7085 @Override
7086 public void addExtras(Bundle extras) {
7087 super.addExtras(extras);
7088
7089 if (mToken != null) {
7090 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7091 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007092 if (mActionsToShowInCompact != null) {
7093 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7094 }
Dan Sandler842dd772014-05-15 09:36:47 -04007095 }
7096
Christoph Studer4600f9b2014-07-22 22:44:43 +02007097 /**
7098 * @hide
7099 */
7100 @Override
7101 protected void restoreFromExtras(Bundle extras) {
7102 super.restoreFromExtras(extras);
7103
7104 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7105 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7106 }
7107 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7108 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7109 }
7110 }
7111
Selim Cinek5bf069a2015-11-10 19:14:27 -05007112 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007113 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007114 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007115 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007116 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007117
7118 // If the action buttons should not be tinted, then just use the default
7119 // notification color. Otherwise, just use the passed-in color.
7120 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7121 ? color
7122 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7123 Notification.COLOR_DEFAULT);
7124
Sunny Goyal5b153922017-09-21 21:00:36 -07007125 button.setDrawableTint(R.id.action0, false, tintColor,
7126 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007127 if (!tombstone) {
7128 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7129 }
7130 button.setContentDescription(R.id.action0, action.title);
7131 return button;
7132 }
7133
7134 private RemoteViews makeMediaContentView() {
7135 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007136 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007137
7138 final int numActions = mBuilder.mActions.size();
7139 final int N = mActionsToShowInCompact == null
7140 ? 0
7141 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7142 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007143 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007144 for (int i = 0; i < N; i++) {
7145 if (i >= numActions) {
7146 throw new IllegalArgumentException(String.format(
7147 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7148 i, numActions - 1));
7149 }
7150
7151 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007152 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007153 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007154 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007155 }
7156 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007157 handleImage(view);
7158 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007159 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007160 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007161 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007162 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007163 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007164 return view;
7165 }
7166
Selim Cinek99104832017-01-25 14:47:33 -08007167 private int getPrimaryHighlightColor() {
7168 return mBuilder.getPrimaryHighlightColor();
7169 }
7170
Dan Sandler842dd772014-05-15 09:36:47 -04007171 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007172 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007173 // Dont add an expanded view if there is no more content to be revealed
7174 int actionsInCompact = mActionsToShowInCompact == null
7175 ? 0
7176 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007177 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007178 return null;
7179 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007180 RemoteViews big = mBuilder.applyStandardTemplate(
7181 R.layout.notification_template_material_big_media,
7182 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007183
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007184 if (actionCount > 0) {
7185 big.removeAllViews(com.android.internal.R.id.media_actions);
7186 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007187 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007188 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007189 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007190 }
7191 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007192 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007193 return big;
7194 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007195
Selim Cinek5bf069a2015-11-10 19:14:27 -05007196 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007197 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007198 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7199 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007200 }
7201 }
7202
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007203 /**
7204 * @hide
7205 */
7206 @Override
7207 protected boolean hasProgress() {
7208 return false;
7209 }
Dan Sandler842dd772014-05-15 09:36:47 -04007210 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007211
Selim Cinek593610c2016-02-16 18:42:57 -08007212 /**
7213 * Notification style for custom views that are decorated by the system
7214 *
7215 * <p>Instead of providing a notification that is completely custom, a developer can set this
7216 * style and still obtain system decorations like the notification header with the expand
7217 * affordance and actions.
7218 *
7219 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7220 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7221 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7222 * corresponding custom views to display.
7223 *
7224 * To use this style with your Notification, feed it to
7225 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7226 * <pre class="prettyprint">
7227 * Notification noti = new Notification.Builder()
7228 * .setSmallIcon(R.drawable.ic_stat_player)
7229 * .setLargeIcon(albumArtBitmap))
7230 * .setCustomContentView(contentView);
7231 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7232 * .build();
7233 * </pre>
7234 */
7235 public static class DecoratedCustomViewStyle extends Style {
7236
7237 public DecoratedCustomViewStyle() {
7238 }
7239
Selim Cinek593610c2016-02-16 18:42:57 -08007240 /**
7241 * @hide
7242 */
7243 public boolean displayCustomViewInline() {
7244 return true;
7245 }
7246
7247 /**
7248 * @hide
7249 */
7250 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007251 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007252 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7253 }
7254
7255 /**
7256 * @hide
7257 */
7258 @Override
7259 public RemoteViews makeBigContentView() {
7260 return makeDecoratedBigContentView();
7261 }
7262
7263 /**
7264 * @hide
7265 */
7266 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007267 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007268 return makeDecoratedHeadsUpContentView();
7269 }
7270
Selim Cinek593610c2016-02-16 18:42:57 -08007271 private RemoteViews makeDecoratedHeadsUpContentView() {
7272 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7273 ? mBuilder.mN.contentView
7274 : mBuilder.mN.headsUpContentView;
7275 if (mBuilder.mActions.size() == 0) {
7276 return makeStandardTemplateWithCustomContent(headsUpContentView);
7277 }
7278 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7279 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007280 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007281 return remoteViews;
7282 }
7283
Selim Cinek593610c2016-02-16 18:42:57 -08007284 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7285 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7286 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007287 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007288 return remoteViews;
7289 }
7290
Selim Cinek593610c2016-02-16 18:42:57 -08007291 private RemoteViews makeDecoratedBigContentView() {
7292 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7293 ? mBuilder.mN.contentView
7294 : mBuilder.mN.bigContentView;
7295 if (mBuilder.mActions.size() == 0) {
7296 return makeStandardTemplateWithCustomContent(bigContentView);
7297 }
7298 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7299 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007300 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007301 return remoteViews;
7302 }
Selim Cinek247fa012016-02-18 09:50:48 -08007303
7304 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7305 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007306 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007307 // Need to clone customContent before adding, because otherwise it can no longer be
7308 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007309 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007310 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7311 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007312 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007313 }
Selim Cinek247fa012016-02-18 09:50:48 -08007314 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007315 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007316 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007317 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007318 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007319 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007320 }
Selim Cinek593610c2016-02-16 18:42:57 -08007321 }
7322
Selim Cinek03eb3b72016-02-18 10:39:45 -08007323 /**
7324 * Notification style for media custom views that are decorated by the system
7325 *
7326 * <p>Instead of providing a media notification that is completely custom, a developer can set
7327 * this style and still obtain system decorations like the notification header with the expand
7328 * affordance and actions.
7329 *
7330 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7331 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7332 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7333 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007334 * <p>
7335 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7336 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7337 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007338 * To use this style with your Notification, feed it to
7339 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7340 * <pre class="prettyprint">
7341 * Notification noti = new Notification.Builder()
7342 * .setSmallIcon(R.drawable.ic_stat_player)
7343 * .setLargeIcon(albumArtBitmap))
7344 * .setCustomContentView(contentView);
7345 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7346 * .setMediaSession(mySession))
7347 * .build();
7348 * </pre>
7349 *
7350 * @see android.app.Notification.DecoratedCustomViewStyle
7351 * @see android.app.Notification.MediaStyle
7352 */
7353 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7354
7355 public DecoratedMediaCustomViewStyle() {
7356 }
7357
Selim Cinek03eb3b72016-02-18 10:39:45 -08007358 /**
7359 * @hide
7360 */
7361 public boolean displayCustomViewInline() {
7362 return true;
7363 }
7364
7365 /**
7366 * @hide
7367 */
7368 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007369 public RemoteViews makeContentView(boolean increasedHeight) {
7370 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007371 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7372 mBuilder.mN.contentView);
7373 }
7374
7375 /**
7376 * @hide
7377 */
7378 @Override
7379 public RemoteViews makeBigContentView() {
7380 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7381 ? mBuilder.mN.bigContentView
7382 : mBuilder.mN.contentView;
7383 return makeBigContentViewWithCustomContent(customRemoteView);
7384 }
7385
7386 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7387 RemoteViews remoteViews = super.makeBigContentView();
7388 if (remoteViews != null) {
7389 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7390 customRemoteView);
7391 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007392 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007393 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7394 customRemoteView);
7395 } else {
7396 return null;
7397 }
7398 }
7399
7400 /**
7401 * @hide
7402 */
7403 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007404 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007405 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7406 ? mBuilder.mN.headsUpContentView
7407 : mBuilder.mN.contentView;
7408 return makeBigContentViewWithCustomContent(customRemoteView);
7409 }
7410
7411 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7412 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007413 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007414 // Need to clone customContent before adding, because otherwise it can no longer be
7415 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007416 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007417 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007418 remoteViews.removeAllViews(id);
7419 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007420 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007421 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007422 return remoteViews;
7423 }
7424 }
7425
Selim Cineke7238dd2017-12-14 17:48:32 -08007426 /**
7427 * A Person associated with this Notification.
7428 */
7429 public static final class Person implements Parcelable {
7430 @Nullable private CharSequence mName;
7431 @Nullable private Icon mIcon;
7432 @Nullable private String mUri;
7433 @Nullable private String mKey;
7434
7435 protected Person(Parcel in) {
7436 mName = in.readCharSequence();
7437 if (in.readInt() != 0) {
7438 mIcon = Icon.CREATOR.createFromParcel(in);
7439 }
7440 mUri = in.readString();
7441 mKey = in.readString();
7442 }
7443
7444 /**
7445 * Create a new person.
7446 */
7447 public Person() {
7448 }
7449
7450 /**
7451 * Give this person a name.
7452 *
7453 * @param name the name of this person
7454 */
7455 public Person setName(@Nullable CharSequence name) {
7456 this.mName = name;
7457 return this;
7458 }
7459
7460 /**
7461 * Add an icon for this person.
7462 * <br />
7463 * This is currently only used for {@link MessagingStyle} notifications and should not be
7464 * provided otherwise, in order to save memory. The system will prefer this icon over any
7465 * images that are resolved from the URI.
7466 *
7467 * @param icon the icon of the person
7468 */
7469 public Person setIcon(@Nullable Icon icon) {
7470 this.mIcon = icon;
7471 return this;
7472 }
7473
7474 /**
7475 * Set a URI associated with this person.
7476 *
7477 * <P>
7478 * Depending on user preferences, adding a URI to a Person may allow the notification to
7479 * pass through interruption filters, if this notification is of
7480 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7481 * The addition of people may also cause this notification to appear more prominently in
7482 * the user interface.
7483 * </P>
7484 *
7485 * <P>
7486 * The person should be specified by the {@code String} representation of a
7487 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7488 * </P>
7489 *
7490 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7491 * URIs. The path part of these URIs must exist in the contacts database, in the
7492 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7493 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7494 * </P>
7495 *
7496 * @param uri a URI for the person
7497 */
7498 public Person setUri(@Nullable String uri) {
7499 mUri = uri;
7500 return this;
7501 }
7502
7503 /**
7504 * Add a key to this person in order to uniquely identify it.
7505 * This is especially useful if the name doesn't uniquely identify this person or if the
7506 * display name is a short handle of the actual name.
7507 *
7508 * <P>If no key is provided, the name serves as as the key for the purpose of
7509 * identification.</P>
7510 *
7511 * @param key the key that uniquely identifies this person
7512 */
7513 public Person setKey(@Nullable String key) {
7514 mKey = key;
7515 return this;
7516 }
7517
7518
7519 /**
7520 * @return the uri provided for this person or {@code null} if no Uri was provided
7521 */
7522 @Nullable
7523 public String getUri() {
7524 return mUri;
7525 }
7526
7527 /**
7528 * @return the name provided for this person or {@code null} if no name was provided
7529 */
7530 @Nullable
7531 public CharSequence getName() {
7532 return mName;
7533 }
7534
7535 /**
7536 * @return the icon provided for this person or {@code null} if no icon was provided
7537 */
7538 @Nullable
7539 public Icon getIcon() {
7540 return mIcon;
7541 }
7542
7543 /**
7544 * @return the key provided for this person or {@code null} if no key was provided
7545 */
7546 @Nullable
7547 public String getKey() {
7548 return mKey;
7549 }
7550
7551 /**
7552 * @return the URI associated with this person, or "name:mName" otherwise
7553 * @hide
7554 */
7555 public String resolveToLegacyUri() {
7556 if (mUri != null) {
7557 return mUri;
7558 }
7559 if (mName != null) {
7560 return "name:" + mName;
7561 }
7562 return "";
7563 }
7564
7565 @Override
7566 public int describeContents() {
7567 return 0;
7568 }
7569
7570 @Override
7571 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7572 dest.writeCharSequence(mName);
7573 if (mIcon != null) {
7574 dest.writeInt(1);
7575 mIcon.writeToParcel(dest, 0);
7576 } else {
7577 dest.writeInt(0);
7578 }
7579 dest.writeString(mUri);
7580 dest.writeString(mKey);
7581 }
7582
7583 public static final Creator<Person> CREATOR = new Creator<Person>() {
7584 @Override
7585 public Person createFromParcel(Parcel in) {
7586 return new Person(in);
7587 }
7588
7589 @Override
7590 public Person[] newArray(int size) {
7591 return new Person[size];
7592 }
7593 };
7594 }
7595
Christoph Studer4600f9b2014-07-22 22:44:43 +02007596 // When adding a new Style subclass here, don't forget to update
7597 // Builder.getNotificationStyleClass.
7598
Griff Hazen61a9e862014-05-22 16:05:19 -07007599 /**
7600 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7601 * metadata or change options on a notification builder.
7602 */
7603 public interface Extender {
7604 /**
7605 * Apply this extender to a notification builder.
7606 * @param builder the builder to be modified.
7607 * @return the build object for chaining.
7608 */
7609 public Builder extend(Builder builder);
7610 }
7611
7612 /**
7613 * Helper class to add wearable extensions to notifications.
7614 * <p class="note"> See
7615 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7616 * for Android Wear</a> for more information on how to use this class.
7617 * <p>
7618 * To create a notification with wearable extensions:
7619 * <ol>
7620 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7621 * properties.
7622 * <li>Create a {@link android.app.Notification.WearableExtender}.
7623 * <li>Set wearable-specific properties using the
7624 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7625 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7626 * notification.
7627 * <li>Post the notification to the notification system with the
7628 * {@code NotificationManager.notify(...)} methods.
7629 * </ol>
7630 *
7631 * <pre class="prettyprint">
7632 * Notification notif = new Notification.Builder(mContext)
7633 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7634 * .setContentText(subject)
7635 * .setSmallIcon(R.drawable.new_mail)
7636 * .extend(new Notification.WearableExtender()
7637 * .setContentIcon(R.drawable.new_mail))
7638 * .build();
7639 * NotificationManager notificationManger =
7640 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7641 * notificationManger.notify(0, notif);</pre>
7642 *
7643 * <p>Wearable extensions can be accessed on an existing notification by using the
7644 * {@code WearableExtender(Notification)} constructor,
7645 * and then using the {@code get} methods to access values.
7646 *
7647 * <pre class="prettyprint">
7648 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7649 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007650 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007651 */
7652 public static final class WearableExtender implements Extender {
7653 /**
7654 * Sentinel value for an action index that is unset.
7655 */
7656 public static final int UNSET_ACTION_INDEX = -1;
7657
7658 /**
7659 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7660 * default sizing.
7661 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007662 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007663 * on their content.
7664 */
7665 public static final int SIZE_DEFAULT = 0;
7666
7667 /**
7668 * Size value for use with {@link #setCustomSizePreset} to show this notification
7669 * with an extra small size.
7670 * <p>This value is only applicable for custom display notifications created using
7671 * {@link #setDisplayIntent}.
7672 */
7673 public static final int SIZE_XSMALL = 1;
7674
7675 /**
7676 * Size value for use with {@link #setCustomSizePreset} to show this notification
7677 * with a small size.
7678 * <p>This value is only applicable for custom display notifications created using
7679 * {@link #setDisplayIntent}.
7680 */
7681 public static final int SIZE_SMALL = 2;
7682
7683 /**
7684 * Size value for use with {@link #setCustomSizePreset} to show this notification
7685 * with a medium size.
7686 * <p>This value is only applicable for custom display notifications created using
7687 * {@link #setDisplayIntent}.
7688 */
7689 public static final int SIZE_MEDIUM = 3;
7690
7691 /**
7692 * Size value for use with {@link #setCustomSizePreset} to show this notification
7693 * with a large size.
7694 * <p>This value is only applicable for custom display notifications created using
7695 * {@link #setDisplayIntent}.
7696 */
7697 public static final int SIZE_LARGE = 4;
7698
Griff Hazend5f11f92014-05-27 15:40:09 -07007699 /**
7700 * Size value for use with {@link #setCustomSizePreset} to show this notification
7701 * full screen.
7702 * <p>This value is only applicable for custom display notifications created using
7703 * {@link #setDisplayIntent}.
7704 */
7705 public static final int SIZE_FULL_SCREEN = 5;
7706
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007707 /**
7708 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7709 * short amount of time when this notification is displayed on the screen. This
7710 * is the default value.
7711 */
7712 public static final int SCREEN_TIMEOUT_SHORT = 0;
7713
7714 /**
7715 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7716 * for a longer amount of time when this notification is displayed on the screen.
7717 */
7718 public static final int SCREEN_TIMEOUT_LONG = -1;
7719
Griff Hazen61a9e862014-05-22 16:05:19 -07007720 /** Notification extra which contains wearable extensions */
7721 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7722
Pete Gastaf6781d2014-10-07 15:17:05 -04007723 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007724 private static final String KEY_ACTIONS = "actions";
7725 private static final String KEY_FLAGS = "flags";
7726 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7727 private static final String KEY_PAGES = "pages";
7728 private static final String KEY_BACKGROUND = "background";
7729 private static final String KEY_CONTENT_ICON = "contentIcon";
7730 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7731 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7732 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7733 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7734 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007735 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007736 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007737 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007738
7739 // Flags bitwise-ored to mFlags
7740 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7741 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7742 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7743 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007744 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007745 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007746 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007747
7748 // Default value for flags integer
7749 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7750
7751 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7752 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7753
7754 private ArrayList<Action> mActions = new ArrayList<Action>();
7755 private int mFlags = DEFAULT_FLAGS;
7756 private PendingIntent mDisplayIntent;
7757 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7758 private Bitmap mBackground;
7759 private int mContentIcon;
7760 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7761 private int mContentActionIndex = UNSET_ACTION_INDEX;
7762 private int mCustomSizePreset = SIZE_DEFAULT;
7763 private int mCustomContentHeight;
7764 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007765 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007766 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007767 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007768
7769 /**
7770 * Create a {@link android.app.Notification.WearableExtender} with default
7771 * options.
7772 */
7773 public WearableExtender() {
7774 }
7775
7776 public WearableExtender(Notification notif) {
7777 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7778 if (wearableBundle != null) {
7779 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7780 if (actions != null) {
7781 mActions.addAll(actions);
7782 }
7783
7784 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7785 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7786
7787 Notification[] pages = getNotificationArrayFromBundle(
7788 wearableBundle, KEY_PAGES);
7789 if (pages != null) {
7790 Collections.addAll(mPages, pages);
7791 }
7792
7793 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7794 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7795 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7796 DEFAULT_CONTENT_ICON_GRAVITY);
7797 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7798 UNSET_ACTION_INDEX);
7799 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7800 SIZE_DEFAULT);
7801 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7802 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007803 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007804 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007805 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007806 }
7807 }
7808
7809 /**
7810 * Apply wearable extensions to a notification that is being built. This is typically
7811 * called by the {@link android.app.Notification.Builder#extend} method of
7812 * {@link android.app.Notification.Builder}.
7813 */
7814 @Override
7815 public Notification.Builder extend(Notification.Builder builder) {
7816 Bundle wearableBundle = new Bundle();
7817
7818 if (!mActions.isEmpty()) {
7819 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7820 }
7821 if (mFlags != DEFAULT_FLAGS) {
7822 wearableBundle.putInt(KEY_FLAGS, mFlags);
7823 }
7824 if (mDisplayIntent != null) {
7825 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7826 }
7827 if (!mPages.isEmpty()) {
7828 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7829 new Notification[mPages.size()]));
7830 }
7831 if (mBackground != null) {
7832 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7833 }
7834 if (mContentIcon != 0) {
7835 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7836 }
7837 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7838 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7839 }
7840 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7841 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7842 mContentActionIndex);
7843 }
7844 if (mCustomSizePreset != SIZE_DEFAULT) {
7845 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7846 }
7847 if (mCustomContentHeight != 0) {
7848 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7849 }
7850 if (mGravity != DEFAULT_GRAVITY) {
7851 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7852 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007853 if (mHintScreenTimeout != 0) {
7854 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7855 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007856 if (mDismissalId != null) {
7857 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7858 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007859 if (mBridgeTag != null) {
7860 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7861 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007862
7863 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7864 return builder;
7865 }
7866
7867 @Override
7868 public WearableExtender clone() {
7869 WearableExtender that = new WearableExtender();
7870 that.mActions = new ArrayList<Action>(this.mActions);
7871 that.mFlags = this.mFlags;
7872 that.mDisplayIntent = this.mDisplayIntent;
7873 that.mPages = new ArrayList<Notification>(this.mPages);
7874 that.mBackground = this.mBackground;
7875 that.mContentIcon = this.mContentIcon;
7876 that.mContentIconGravity = this.mContentIconGravity;
7877 that.mContentActionIndex = this.mContentActionIndex;
7878 that.mCustomSizePreset = this.mCustomSizePreset;
7879 that.mCustomContentHeight = this.mCustomContentHeight;
7880 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007881 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007882 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007883 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007884 return that;
7885 }
7886
7887 /**
7888 * Add a wearable action to this notification.
7889 *
7890 * <p>When wearable actions are added using this method, the set of actions that
7891 * show on a wearable device splits from devices that only show actions added
7892 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7893 * of which actions display on different devices.
7894 *
7895 * @param action the action to add to this notification
7896 * @return this object for method chaining
7897 * @see android.app.Notification.Action
7898 */
7899 public WearableExtender addAction(Action action) {
7900 mActions.add(action);
7901 return this;
7902 }
7903
7904 /**
7905 * Adds wearable actions to this notification.
7906 *
7907 * <p>When wearable actions are added using this method, the set of actions that
7908 * show on a wearable device splits from devices that only show actions added
7909 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7910 * of which actions display on different devices.
7911 *
7912 * @param actions the actions to add to this notification
7913 * @return this object for method chaining
7914 * @see android.app.Notification.Action
7915 */
7916 public WearableExtender addActions(List<Action> actions) {
7917 mActions.addAll(actions);
7918 return this;
7919 }
7920
7921 /**
7922 * Clear all wearable actions present on this builder.
7923 * @return this object for method chaining.
7924 * @see #addAction
7925 */
7926 public WearableExtender clearActions() {
7927 mActions.clear();
7928 return this;
7929 }
7930
7931 /**
7932 * Get the wearable actions present on this notification.
7933 */
7934 public List<Action> getActions() {
7935 return mActions;
7936 }
7937
7938 /**
7939 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007940 * this notification. The {@link PendingIntent} provided should be for an activity.
7941 *
7942 * <pre class="prettyprint">
7943 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7944 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7945 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7946 * Notification notif = new Notification.Builder(context)
7947 * .extend(new Notification.WearableExtender()
7948 * .setDisplayIntent(displayPendingIntent)
7949 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7950 * .build();</pre>
7951 *
7952 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007953 * should have an empty task affinity. It is also recommended to use the device
7954 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007955 *
7956 * <p>Example AndroidManifest.xml entry:
7957 * <pre class="prettyprint">
7958 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7959 * android:exported=&quot;true&quot;
7960 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007961 * android:taskAffinity=&quot;&quot;
7962 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007963 *
7964 * @param intent the {@link PendingIntent} for an activity
7965 * @return this object for method chaining
7966 * @see android.app.Notification.WearableExtender#getDisplayIntent
7967 */
7968 public WearableExtender setDisplayIntent(PendingIntent intent) {
7969 mDisplayIntent = intent;
7970 return this;
7971 }
7972
7973 /**
7974 * Get the intent to launch inside of an activity view when displaying this
7975 * notification. This {@code PendingIntent} should be for an activity.
7976 */
7977 public PendingIntent getDisplayIntent() {
7978 return mDisplayIntent;
7979 }
7980
7981 /**
7982 * Add an additional page of content to display with this notification. The current
7983 * notification forms the first page, and pages added using this function form
7984 * subsequent pages. This field can be used to separate a notification into multiple
7985 * sections.
7986 *
7987 * @param page the notification to add as another page
7988 * @return this object for method chaining
7989 * @see android.app.Notification.WearableExtender#getPages
7990 */
7991 public WearableExtender addPage(Notification page) {
7992 mPages.add(page);
7993 return this;
7994 }
7995
7996 /**
7997 * Add additional pages of content to display with this notification. The current
7998 * notification forms the first page, and pages added using this function form
7999 * subsequent pages. This field can be used to separate a notification into multiple
8000 * sections.
8001 *
8002 * @param pages a list of notifications
8003 * @return this object for method chaining
8004 * @see android.app.Notification.WearableExtender#getPages
8005 */
8006 public WearableExtender addPages(List<Notification> pages) {
8007 mPages.addAll(pages);
8008 return this;
8009 }
8010
8011 /**
8012 * Clear all additional pages present on this builder.
8013 * @return this object for method chaining.
8014 * @see #addPage
8015 */
8016 public WearableExtender clearPages() {
8017 mPages.clear();
8018 return this;
8019 }
8020
8021 /**
8022 * Get the array of additional pages of content for displaying this notification. The
8023 * current notification forms the first page, and elements within this array form
8024 * subsequent pages. This field can be used to separate a notification into multiple
8025 * sections.
8026 * @return the pages for this notification
8027 */
8028 public List<Notification> getPages() {
8029 return mPages;
8030 }
8031
8032 /**
8033 * Set a background image to be displayed behind the notification content.
8034 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8035 * will work with any notification style.
8036 *
8037 * @param background the background bitmap
8038 * @return this object for method chaining
8039 * @see android.app.Notification.WearableExtender#getBackground
8040 */
8041 public WearableExtender setBackground(Bitmap background) {
8042 mBackground = background;
8043 return this;
8044 }
8045
8046 /**
8047 * Get a background image to be displayed behind the notification content.
8048 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8049 * will work with any notification style.
8050 *
8051 * @return the background image
8052 * @see android.app.Notification.WearableExtender#setBackground
8053 */
8054 public Bitmap getBackground() {
8055 return mBackground;
8056 }
8057
8058 /**
8059 * Set an icon that goes with the content of this notification.
8060 */
8061 public WearableExtender setContentIcon(int icon) {
8062 mContentIcon = icon;
8063 return this;
8064 }
8065
8066 /**
8067 * Get an icon that goes with the content of this notification.
8068 */
8069 public int getContentIcon() {
8070 return mContentIcon;
8071 }
8072
8073 /**
8074 * Set the gravity that the content icon should have within the notification display.
8075 * Supported values include {@link android.view.Gravity#START} and
8076 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8077 * @see #setContentIcon
8078 */
8079 public WearableExtender setContentIconGravity(int contentIconGravity) {
8080 mContentIconGravity = contentIconGravity;
8081 return this;
8082 }
8083
8084 /**
8085 * Get the gravity that the content icon should have within the notification display.
8086 * Supported values include {@link android.view.Gravity#START} and
8087 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8088 * @see #getContentIcon
8089 */
8090 public int getContentIconGravity() {
8091 return mContentIconGravity;
8092 }
8093
8094 /**
8095 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008096 * this notification. This action will no longer display separately from the
8097 * notification's content.
8098 *
Griff Hazenca48d352014-05-28 22:37:13 -07008099 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008100 * set, although the list of available actions comes from the main notification and not
8101 * from the child page's notification.
8102 *
8103 * @param actionIndex The index of the action to hoist onto the current notification page.
8104 * If wearable actions were added to the main notification, this index
8105 * will apply to that list, otherwise it will apply to the regular
8106 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008107 */
8108 public WearableExtender setContentAction(int actionIndex) {
8109 mContentActionIndex = actionIndex;
8110 return this;
8111 }
8112
8113 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008114 * Get the index of the notification action, if any, that was specified as being clickable
8115 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008116 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008117 *
Griff Hazenca48d352014-05-28 22:37:13 -07008118 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008119 * set, although the list of available actions comes from the main notification and not
8120 * from the child page's notification.
8121 *
8122 * <p>If wearable specific actions were added to the main notification, this index will
8123 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008124 *
8125 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008126 */
8127 public int getContentAction() {
8128 return mContentActionIndex;
8129 }
8130
8131 /**
8132 * Set the gravity that this notification should have within the available viewport space.
8133 * Supported values include {@link android.view.Gravity#TOP},
8134 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8135 * The default value is {@link android.view.Gravity#BOTTOM}.
8136 */
8137 public WearableExtender setGravity(int gravity) {
8138 mGravity = gravity;
8139 return this;
8140 }
8141
8142 /**
8143 * Get the gravity that this notification should have within the available viewport space.
8144 * Supported values include {@link android.view.Gravity#TOP},
8145 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8146 * The default value is {@link android.view.Gravity#BOTTOM}.
8147 */
8148 public int getGravity() {
8149 return mGravity;
8150 }
8151
8152 /**
8153 * Set the custom size preset for the display of this notification out of the available
8154 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8155 * {@link #SIZE_LARGE}.
8156 * <p>Some custom size presets are only applicable for custom display notifications created
8157 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8158 * documentation for the preset in question. See also
8159 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8160 */
8161 public WearableExtender setCustomSizePreset(int sizePreset) {
8162 mCustomSizePreset = sizePreset;
8163 return this;
8164 }
8165
8166 /**
8167 * Get the custom size preset for the display of this notification out of the available
8168 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8169 * {@link #SIZE_LARGE}.
8170 * <p>Some custom size presets are only applicable for custom display notifications created
8171 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8172 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8173 */
8174 public int getCustomSizePreset() {
8175 return mCustomSizePreset;
8176 }
8177
8178 /**
8179 * Set the custom height in pixels for the display of this notification's content.
8180 * <p>This option is only available for custom display notifications created
8181 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8182 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8183 * {@link #getCustomContentHeight}.
8184 */
8185 public WearableExtender setCustomContentHeight(int height) {
8186 mCustomContentHeight = height;
8187 return this;
8188 }
8189
8190 /**
8191 * Get the custom height in pixels for the display of this notification's content.
8192 * <p>This option is only available for custom display notifications created
8193 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8194 * {@link #setCustomContentHeight}.
8195 */
8196 public int getCustomContentHeight() {
8197 return mCustomContentHeight;
8198 }
8199
8200 /**
8201 * Set whether the scrolling position for the contents of this notification should start
8202 * at the bottom of the contents instead of the top when the contents are too long to
8203 * display within the screen. Default is false (start scroll at the top).
8204 */
8205 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8206 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8207 return this;
8208 }
8209
8210 /**
8211 * Get whether the scrolling position for the contents of this notification should start
8212 * at the bottom of the contents instead of the top when the contents are too long to
8213 * display within the screen. Default is false (start scroll at the top).
8214 */
8215 public boolean getStartScrollBottom() {
8216 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8217 }
8218
8219 /**
8220 * Set whether the content intent is available when the wearable device is not connected
8221 * to a companion device. The user can still trigger this intent when the wearable device
8222 * is offline, but a visual hint will indicate that the content intent may not be available.
8223 * Defaults to true.
8224 */
8225 public WearableExtender setContentIntentAvailableOffline(
8226 boolean contentIntentAvailableOffline) {
8227 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8228 return this;
8229 }
8230
8231 /**
8232 * Get whether the content intent is available when the wearable device is not connected
8233 * to a companion device. The user can still trigger this intent when the wearable device
8234 * is offline, but a visual hint will indicate that the content intent may not be available.
8235 * Defaults to true.
8236 */
8237 public boolean getContentIntentAvailableOffline() {
8238 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8239 }
8240
8241 /**
8242 * Set a hint that this notification's icon should not be displayed.
8243 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8244 * @return this object for method chaining
8245 */
8246 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8247 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8248 return this;
8249 }
8250
8251 /**
8252 * Get a hint that this notification's icon should not be displayed.
8253 * @return {@code true} if this icon should not be displayed, false otherwise.
8254 * The default value is {@code false} if this was never set.
8255 */
8256 public boolean getHintHideIcon() {
8257 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8258 }
8259
8260 /**
8261 * Set a visual hint that only the background image of this notification should be
8262 * displayed, and other semantic content should be hidden. This hint is only applicable
8263 * to sub-pages added using {@link #addPage}.
8264 */
8265 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8266 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8267 return this;
8268 }
8269
8270 /**
8271 * Get a visual hint that only the background image of this notification should be
8272 * displayed, and other semantic content should be hidden. This hint is only applicable
8273 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8274 */
8275 public boolean getHintShowBackgroundOnly() {
8276 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8277 }
8278
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008279 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008280 * Set a hint that this notification's background should not be clipped if possible,
8281 * and should instead be resized to fully display on the screen, retaining the aspect
8282 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008283 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8284 * @return this object for method chaining
8285 */
8286 public WearableExtender setHintAvoidBackgroundClipping(
8287 boolean hintAvoidBackgroundClipping) {
8288 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8289 return this;
8290 }
8291
8292 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008293 * Get a hint that this notification's background should not be clipped if possible,
8294 * and should instead be resized to fully display on the screen, retaining the aspect
8295 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008296 * @return {@code true} if it's ok if the background is clipped on the screen, false
8297 * otherwise. The default value is {@code false} if this was never set.
8298 */
8299 public boolean getHintAvoidBackgroundClipping() {
8300 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8301 }
8302
8303 /**
8304 * Set a hint that the screen should remain on for at least this duration when
8305 * this notification is displayed on the screen.
8306 * @param timeout The requested screen timeout in milliseconds. Can also be either
8307 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8308 * @return this object for method chaining
8309 */
8310 public WearableExtender setHintScreenTimeout(int timeout) {
8311 mHintScreenTimeout = timeout;
8312 return this;
8313 }
8314
8315 /**
8316 * Get the duration, in milliseconds, that the screen should remain on for
8317 * when this notification is displayed.
8318 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8319 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8320 */
8321 public int getHintScreenTimeout() {
8322 return mHintScreenTimeout;
8323 }
8324
Alex Hills9ab3a232016-04-05 14:54:56 -04008325 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008326 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8327 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8328 * qr codes, as well as other simple black-and-white tickets.
8329 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8330 * @return this object for method chaining
8331 */
8332 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8333 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8334 return this;
8335 }
8336
8337 /**
8338 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8339 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8340 * qr codes, as well as other simple black-and-white tickets.
8341 * @return {@code true} if it should be displayed in ambient, false otherwise
8342 * otherwise. The default value is {@code false} if this was never set.
8343 */
8344 public boolean getHintAmbientBigPicture() {
8345 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8346 }
8347
8348 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008349 * Set a hint that this notification's content intent will launch an {@link Activity}
8350 * directly, telling the platform that it can generate the appropriate transitions.
8351 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8352 * an activity and transitions should be generated, false otherwise.
8353 * @return this object for method chaining
8354 */
8355 public WearableExtender setHintContentIntentLaunchesActivity(
8356 boolean hintContentIntentLaunchesActivity) {
8357 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8358 return this;
8359 }
8360
8361 /**
8362 * Get a hint that this notification's content intent will launch an {@link Activity}
8363 * directly, telling the platform that it can generate the appropriate transitions
8364 * @return {@code true} if the content intent will launch an activity and transitions should
8365 * be generated, false otherwise. The default value is {@code false} if this was never set.
8366 */
8367 public boolean getHintContentIntentLaunchesActivity() {
8368 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8369 }
8370
Nadia Benbernou948627e2016-04-14 14:41:08 -04008371 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008372 * Sets the dismissal id for this notification. If a notification is posted with a
8373 * dismissal id, then when that notification is canceled, notifications on other wearables
8374 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008375 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008376 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008377 * @param dismissalId the dismissal id of the notification.
8378 * @return this object for method chaining
8379 */
8380 public WearableExtender setDismissalId(String dismissalId) {
8381 mDismissalId = dismissalId;
8382 return this;
8383 }
8384
8385 /**
8386 * Returns the dismissal id of the notification.
8387 * @return the dismissal id of the notification or null if it has not been set.
8388 */
8389 public String getDismissalId() {
8390 return mDismissalId;
8391 }
8392
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008393 /**
8394 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8395 * posted from a phone to provide finer-grained control on what notifications are bridged
8396 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8397 * Features to Notifications</a> for more information.
8398 * @param bridgeTag the bridge tag of the notification.
8399 * @return this object for method chaining
8400 */
8401 public WearableExtender setBridgeTag(String bridgeTag) {
8402 mBridgeTag = bridgeTag;
8403 return this;
8404 }
8405
8406 /**
8407 * Returns the bridge tag of the notification.
8408 * @return the bridge tag or null if not present.
8409 */
8410 public String getBridgeTag() {
8411 return mBridgeTag;
8412 }
8413
Griff Hazen61a9e862014-05-22 16:05:19 -07008414 private void setFlag(int mask, boolean value) {
8415 if (value) {
8416 mFlags |= mask;
8417 } else {
8418 mFlags &= ~mask;
8419 }
8420 }
8421 }
8422
8423 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008424 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8425 * with car extensions:
8426 *
8427 * <ol>
8428 * <li>Create an {@link Notification.Builder}, setting any desired
8429 * properties.
8430 * <li>Create a {@link CarExtender}.
8431 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8432 * {@link CarExtender}.
8433 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8434 * to apply the extensions to a notification.
8435 * </ol>
8436 *
8437 * <pre class="prettyprint">
8438 * Notification notification = new Notification.Builder(context)
8439 * ...
8440 * .extend(new CarExtender()
8441 * .set*(...))
8442 * .build();
8443 * </pre>
8444 *
8445 * <p>Car extensions can be accessed on an existing notification by using the
8446 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8447 * to access values.
8448 */
8449 public static final class CarExtender implements Extender {
8450 private static final String TAG = "CarExtender";
8451
8452 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8453 private static final String EXTRA_LARGE_ICON = "large_icon";
8454 private static final String EXTRA_CONVERSATION = "car_conversation";
8455 private static final String EXTRA_COLOR = "app_color";
8456
8457 private Bitmap mLargeIcon;
8458 private UnreadConversation mUnreadConversation;
8459 private int mColor = Notification.COLOR_DEFAULT;
8460
8461 /**
8462 * Create a {@link CarExtender} with default options.
8463 */
8464 public CarExtender() {
8465 }
8466
8467 /**
8468 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8469 *
8470 * @param notif The notification from which to copy options.
8471 */
8472 public CarExtender(Notification notif) {
8473 Bundle carBundle = notif.extras == null ?
8474 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8475 if (carBundle != null) {
8476 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8477 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8478
8479 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8480 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8481 }
8482 }
8483
8484 /**
8485 * Apply car extensions to a notification that is being built. This is typically called by
8486 * the {@link Notification.Builder#extend(Notification.Extender)}
8487 * method of {@link Notification.Builder}.
8488 */
8489 @Override
8490 public Notification.Builder extend(Notification.Builder builder) {
8491 Bundle carExtensions = new Bundle();
8492
8493 if (mLargeIcon != null) {
8494 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8495 }
8496 if (mColor != Notification.COLOR_DEFAULT) {
8497 carExtensions.putInt(EXTRA_COLOR, mColor);
8498 }
8499
8500 if (mUnreadConversation != null) {
8501 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8502 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8503 }
8504
8505 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8506 return builder;
8507 }
8508
8509 /**
8510 * Sets the accent color to use when Android Auto presents the notification.
8511 *
8512 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8513 * to accent the displayed notification. However, not all colors are acceptable in an
8514 * automotive setting. This method can be used to override the color provided in the
8515 * notification in such a situation.
8516 */
Tor Norbye80756e32015-03-02 09:39:27 -08008517 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008518 mColor = color;
8519 return this;
8520 }
8521
8522 /**
8523 * Gets the accent color.
8524 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008525 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008526 */
Tor Norbye80756e32015-03-02 09:39:27 -08008527 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008528 public int getColor() {
8529 return mColor;
8530 }
8531
8532 /**
8533 * Sets the large icon of the car notification.
8534 *
8535 * If no large icon is set in the extender, Android Auto will display the icon
8536 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8537 *
8538 * @param largeIcon The large icon to use in the car notification.
8539 * @return This object for method chaining.
8540 */
8541 public CarExtender setLargeIcon(Bitmap largeIcon) {
8542 mLargeIcon = largeIcon;
8543 return this;
8544 }
8545
8546 /**
8547 * Gets the large icon used in this car notification, or null if no icon has been set.
8548 *
8549 * @return The large icon for the car notification.
8550 * @see CarExtender#setLargeIcon
8551 */
8552 public Bitmap getLargeIcon() {
8553 return mLargeIcon;
8554 }
8555
8556 /**
8557 * Sets the unread conversation in a message notification.
8558 *
8559 * @param unreadConversation The unread part of the conversation this notification conveys.
8560 * @return This object for method chaining.
8561 */
8562 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8563 mUnreadConversation = unreadConversation;
8564 return this;
8565 }
8566
8567 /**
8568 * Returns the unread conversation conveyed by this notification.
8569 * @see #setUnreadConversation(UnreadConversation)
8570 */
8571 public UnreadConversation getUnreadConversation() {
8572 return mUnreadConversation;
8573 }
8574
8575 /**
8576 * A class which holds the unread messages from a conversation.
8577 */
8578 public static class UnreadConversation {
8579 private static final String KEY_AUTHOR = "author";
8580 private static final String KEY_TEXT = "text";
8581 private static final String KEY_MESSAGES = "messages";
8582 private static final String KEY_REMOTE_INPUT = "remote_input";
8583 private static final String KEY_ON_REPLY = "on_reply";
8584 private static final String KEY_ON_READ = "on_read";
8585 private static final String KEY_PARTICIPANTS = "participants";
8586 private static final String KEY_TIMESTAMP = "timestamp";
8587
8588 private final String[] mMessages;
8589 private final RemoteInput mRemoteInput;
8590 private final PendingIntent mReplyPendingIntent;
8591 private final PendingIntent mReadPendingIntent;
8592 private final String[] mParticipants;
8593 private final long mLatestTimestamp;
8594
8595 UnreadConversation(String[] messages, RemoteInput remoteInput,
8596 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8597 String[] participants, long latestTimestamp) {
8598 mMessages = messages;
8599 mRemoteInput = remoteInput;
8600 mReadPendingIntent = readPendingIntent;
8601 mReplyPendingIntent = replyPendingIntent;
8602 mParticipants = participants;
8603 mLatestTimestamp = latestTimestamp;
8604 }
8605
8606 /**
8607 * Gets the list of messages conveyed by this notification.
8608 */
8609 public String[] getMessages() {
8610 return mMessages;
8611 }
8612
8613 /**
8614 * Gets the remote input that will be used to convey the response to a message list, or
8615 * null if no such remote input exists.
8616 */
8617 public RemoteInput getRemoteInput() {
8618 return mRemoteInput;
8619 }
8620
8621 /**
8622 * Gets the pending intent that will be triggered when the user replies to this
8623 * notification.
8624 */
8625 public PendingIntent getReplyPendingIntent() {
8626 return mReplyPendingIntent;
8627 }
8628
8629 /**
8630 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8631 * in this object's message list.
8632 */
8633 public PendingIntent getReadPendingIntent() {
8634 return mReadPendingIntent;
8635 }
8636
8637 /**
8638 * Gets the participants in the conversation.
8639 */
8640 public String[] getParticipants() {
8641 return mParticipants;
8642 }
8643
8644 /**
8645 * Gets the firs participant in the conversation.
8646 */
8647 public String getParticipant() {
8648 return mParticipants.length > 0 ? mParticipants[0] : null;
8649 }
8650
8651 /**
8652 * Gets the timestamp of the conversation.
8653 */
8654 public long getLatestTimestamp() {
8655 return mLatestTimestamp;
8656 }
8657
8658 Bundle getBundleForUnreadConversation() {
8659 Bundle b = new Bundle();
8660 String author = null;
8661 if (mParticipants != null && mParticipants.length > 1) {
8662 author = mParticipants[0];
8663 }
8664 Parcelable[] messages = new Parcelable[mMessages.length];
8665 for (int i = 0; i < messages.length; i++) {
8666 Bundle m = new Bundle();
8667 m.putString(KEY_TEXT, mMessages[i]);
8668 m.putString(KEY_AUTHOR, author);
8669 messages[i] = m;
8670 }
8671 b.putParcelableArray(KEY_MESSAGES, messages);
8672 if (mRemoteInput != null) {
8673 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8674 }
8675 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8676 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8677 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8678 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8679 return b;
8680 }
8681
8682 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8683 if (b == null) {
8684 return null;
8685 }
8686 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8687 String[] messages = null;
8688 if (parcelableMessages != null) {
8689 String[] tmp = new String[parcelableMessages.length];
8690 boolean success = true;
8691 for (int i = 0; i < tmp.length; i++) {
8692 if (!(parcelableMessages[i] instanceof Bundle)) {
8693 success = false;
8694 break;
8695 }
8696 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8697 if (tmp[i] == null) {
8698 success = false;
8699 break;
8700 }
8701 }
8702 if (success) {
8703 messages = tmp;
8704 } else {
8705 return null;
8706 }
8707 }
8708
8709 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8710 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8711
8712 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8713
8714 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8715 if (participants == null || participants.length != 1) {
8716 return null;
8717 }
8718
8719 return new UnreadConversation(messages,
8720 remoteInput,
8721 onReply,
8722 onRead,
8723 participants, b.getLong(KEY_TIMESTAMP));
8724 }
8725 };
8726
8727 /**
8728 * Builder class for {@link CarExtender.UnreadConversation} objects.
8729 */
8730 public static class Builder {
8731 private final List<String> mMessages = new ArrayList<String>();
8732 private final String mParticipant;
8733 private RemoteInput mRemoteInput;
8734 private PendingIntent mReadPendingIntent;
8735 private PendingIntent mReplyPendingIntent;
8736 private long mLatestTimestamp;
8737
8738 /**
8739 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8740 *
8741 * @param name The name of the other participant in the conversation.
8742 */
8743 public Builder(String name) {
8744 mParticipant = name;
8745 }
8746
8747 /**
8748 * Appends a new unread message to the list of messages for this conversation.
8749 *
8750 * The messages should be added from oldest to newest.
8751 *
8752 * @param message The text of the new unread message.
8753 * @return This object for method chaining.
8754 */
8755 public Builder addMessage(String message) {
8756 mMessages.add(message);
8757 return this;
8758 }
8759
8760 /**
8761 * Sets the pending intent and remote input which will convey the reply to this
8762 * notification.
8763 *
8764 * @param pendingIntent The pending intent which will be triggered on a reply.
8765 * @param remoteInput The remote input parcelable which will carry the reply.
8766 * @return This object for method chaining.
8767 *
8768 * @see CarExtender.UnreadConversation#getRemoteInput
8769 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8770 */
8771 public Builder setReplyAction(
8772 PendingIntent pendingIntent, RemoteInput remoteInput) {
8773 mRemoteInput = remoteInput;
8774 mReplyPendingIntent = pendingIntent;
8775
8776 return this;
8777 }
8778
8779 /**
8780 * Sets the pending intent that will be sent once the messages in this notification
8781 * are read.
8782 *
8783 * @param pendingIntent The pending intent to use.
8784 * @return This object for method chaining.
8785 */
8786 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8787 mReadPendingIntent = pendingIntent;
8788 return this;
8789 }
8790
8791 /**
8792 * Sets the timestamp of the most recent message in an unread conversation.
8793 *
8794 * If a messaging notification has been posted by your application and has not
8795 * yet been cancelled, posting a later notification with the same id and tag
8796 * but without a newer timestamp may result in Android Auto not displaying a
8797 * heads up notification for the later notification.
8798 *
8799 * @param timestamp The timestamp of the most recent message in the conversation.
8800 * @return This object for method chaining.
8801 */
8802 public Builder setLatestTimestamp(long timestamp) {
8803 mLatestTimestamp = timestamp;
8804 return this;
8805 }
8806
8807 /**
8808 * Builds a new unread conversation object.
8809 *
8810 * @return The new unread conversation object.
8811 */
8812 public UnreadConversation build() {
8813 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8814 String[] participants = { mParticipant };
8815 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8816 mReadPendingIntent, participants, mLatestTimestamp);
8817 }
8818 }
8819 }
8820
8821 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008822 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8823 * with a TV extension:
8824 *
8825 * <ol>
8826 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8827 * <li>Create a {@link TvExtender}.
8828 * <li>Set TV-specific properties using the {@code set} methods of
8829 * {@link TvExtender}.
8830 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8831 * to apply the extension to a notification.
8832 * </ol>
8833 *
8834 * <pre class="prettyprint">
8835 * Notification notification = new Notification.Builder(context)
8836 * ...
8837 * .extend(new TvExtender()
8838 * .set*(...))
8839 * .build();
8840 * </pre>
8841 *
8842 * <p>TV extensions can be accessed on an existing notification by using the
8843 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8844 * to access values.
8845 *
8846 * @hide
8847 */
8848 @SystemApi
8849 public static final class TvExtender implements Extender {
8850 private static final String TAG = "TvExtender";
8851
8852 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8853 private static final String EXTRA_FLAGS = "flags";
8854 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8855 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008856 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008857
8858 // Flags bitwise-ored to mFlags
8859 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8860
8861 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008862 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008863 private PendingIntent mContentIntent;
8864 private PendingIntent mDeleteIntent;
8865
8866 /**
8867 * Create a {@link TvExtender} with default options.
8868 */
8869 public TvExtender() {
8870 mFlags = FLAG_AVAILABLE_ON_TV;
8871 }
8872
8873 /**
8874 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8875 *
8876 * @param notif The notification from which to copy options.
8877 */
8878 public TvExtender(Notification notif) {
8879 Bundle bundle = notif.extras == null ?
8880 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8881 if (bundle != null) {
8882 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008883 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008884 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8885 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8886 }
8887 }
8888
8889 /**
8890 * Apply a TV extension to a notification that is being built. This is typically called by
8891 * the {@link Notification.Builder#extend(Notification.Extender)}
8892 * method of {@link Notification.Builder}.
8893 */
8894 @Override
8895 public Notification.Builder extend(Notification.Builder builder) {
8896 Bundle bundle = new Bundle();
8897
8898 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008899 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008900 if (mContentIntent != null) {
8901 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8902 }
8903
8904 if (mDeleteIntent != null) {
8905 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8906 }
8907
8908 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8909 return builder;
8910 }
8911
8912 /**
8913 * Returns true if this notification should be shown on TV. This method return true
8914 * if the notification was extended with a TvExtender.
8915 */
8916 public boolean isAvailableOnTv() {
8917 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8918 }
8919
8920 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008921 * Specifies the channel the notification should be delivered on when shown on TV.
8922 * It can be different from the channel that the notification is delivered to when
8923 * posting on a non-TV device.
8924 */
8925 public TvExtender setChannel(String channelId) {
8926 mChannelId = channelId;
8927 return this;
8928 }
8929
8930 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008931 * Specifies the channel the notification should be delivered on when shown on TV.
8932 * It can be different from the channel that the notification is delivered to when
8933 * posting on a non-TV device.
8934 */
8935 public TvExtender setChannelId(String channelId) {
8936 mChannelId = channelId;
8937 return this;
8938 }
8939
Jeff Sharkey000ce802017-04-29 13:13:27 -06008940 /** @removed */
8941 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008942 public String getChannel() {
8943 return mChannelId;
8944 }
8945
8946 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008947 * Returns the id of the channel this notification posts to on TV.
8948 */
8949 public String getChannelId() {
8950 return mChannelId;
8951 }
8952
8953 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008954 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8955 * If provided, it is used instead of the content intent specified
8956 * at the level of Notification.
8957 */
8958 public TvExtender setContentIntent(PendingIntent intent) {
8959 mContentIntent = intent;
8960 return this;
8961 }
8962
8963 /**
8964 * Returns the TV-specific content intent. If this method returns null, the
8965 * main content intent on the notification should be used.
8966 *
8967 * @see {@link Notification#contentIntent}
8968 */
8969 public PendingIntent getContentIntent() {
8970 return mContentIntent;
8971 }
8972
8973 /**
8974 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8975 * by the user on TV. If provided, it is used instead of the delete intent specified
8976 * at the level of Notification.
8977 */
8978 public TvExtender setDeleteIntent(PendingIntent intent) {
8979 mDeleteIntent = intent;
8980 return this;
8981 }
8982
8983 /**
8984 * Returns the TV-specific delete intent. If this method returns null, the
8985 * main delete intent on the notification should be used.
8986 *
8987 * @see {@link Notification#deleteIntent}
8988 */
8989 public PendingIntent getDeleteIntent() {
8990 return mDeleteIntent;
8991 }
8992 }
8993
8994 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008995 * Get an array of Notification objects from a parcelable array bundle field.
8996 * Update the bundle to have a typed array so fetches in the future don't need
8997 * to do an array copy.
8998 */
8999 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9000 Parcelable[] array = bundle.getParcelableArray(key);
9001 if (array instanceof Notification[] || array == null) {
9002 return (Notification[]) array;
9003 }
9004 Notification[] typedArray = Arrays.copyOf(array, array.length,
9005 Notification[].class);
9006 bundle.putParcelableArray(key, typedArray);
9007 return typedArray;
9008 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009009
9010 private static class BuilderRemoteViews extends RemoteViews {
9011 public BuilderRemoteViews(Parcel parcel) {
9012 super(parcel);
9013 }
9014
Kenny Guy77320062014-08-27 21:37:15 +01009015 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9016 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009017 }
9018
9019 @Override
9020 public BuilderRemoteViews clone() {
9021 Parcel p = Parcel.obtain();
9022 writeToParcel(p, 0);
9023 p.setDataPosition(0);
9024 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9025 p.recycle();
9026 return brv;
9027 }
9028 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009029
9030 private static class StandardTemplateParams {
9031 boolean hasProgress = true;
9032 boolean ambient = false;
9033 CharSequence title;
9034 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009035 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009036 boolean hideLargeIcon;
9037 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009038
9039 final StandardTemplateParams reset() {
9040 hasProgress = true;
9041 ambient = false;
9042 title = null;
9043 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009044 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009045 return this;
9046 }
9047
9048 final StandardTemplateParams hasProgress(boolean hasProgress) {
9049 this.hasProgress = hasProgress;
9050 return this;
9051 }
9052
9053 final StandardTemplateParams title(CharSequence title) {
9054 this.title = title;
9055 return this;
9056 }
9057
9058 final StandardTemplateParams text(CharSequence text) {
9059 this.text = text;
9060 return this;
9061 }
9062
Selim Cinekafeed292017-12-12 17:32:44 -08009063 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9064 this.headerTextSecondary = text;
9065 return this;
9066 }
9067
Selim Cinek88188f22017-09-19 16:46:56 -07009068 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9069 this.alwaysShowReply = alwaysShowReply;
9070 return this;
9071 }
9072
9073 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9074 this.hideLargeIcon = hideLargeIcon;
9075 return this;
9076 }
9077
Adrian Roos70d7aa32017-01-11 15:39:06 -08009078 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009079 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009080 this.ambient = ambient;
9081 return this;
9082 }
9083
9084 final StandardTemplateParams fillTextsFrom(Builder b) {
9085 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009086 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9087
9088 // Big text notifications should contain their content when viewed in ambient mode.
9089 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9090 if (!ambient || TextUtils.isEmpty(text)) {
9091 text = extras.getCharSequence(EXTRA_TEXT);
9092 }
9093 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009094 return this;
9095 }
9096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097}