blob: 28ecd9803d28cd1267b01860a3f1e18b8dc3f4ac [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
Kodlee Yincda5b092018-02-15 15:34:53 -08001335 /**
1336 * {@code SemanticAction}: Call a contact, group, etc.
1337 */
1338 public static final int SEMANTIC_ACTION_CALL = 10;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001339
Griff Hazen959591e2014-05-15 22:26:18 -07001340 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001341 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001342 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001343 private boolean mAllowGeneratedReplies = true;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001344 private final @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001345
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001346 /**
1347 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001348 *
1349 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001350 */
Dan Sandler86647982015-05-13 23:41:13 -04001351 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001352 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001353
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001354 /**
1355 * Title of the action.
1356 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001357 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001358
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001359 /**
1360 * Intent to send when the user invokes this action. May be null, in which case the action
1361 * may be rendered in a disabled presentation by the system UI.
1362 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001363 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001364
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001365 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001366 if (in.readInt() != 0) {
1367 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001368 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1369 icon = mIcon.getResId();
1370 }
Dan Sandler86647982015-05-13 23:41:13 -04001371 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001372 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1373 if (in.readInt() == 1) {
1374 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1375 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001376 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001377 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001378 mAllowGeneratedReplies = in.readInt() == 1;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001379 mSemanticAction = in.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001380 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001381
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001382 /**
Dan Sandler86647982015-05-13 23:41:13 -04001383 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001384 */
Dan Sandler86647982015-05-13 23:41:13 -04001385 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001386 public Action(int icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001387 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true,
1388 SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001389 }
1390
Adrian Roos7af53622016-10-12 13:44:05 -07001391 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001392 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001393 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1394 @SemanticAction int semanticAction) {
Dan Sandler86647982015-05-13 23:41:13 -04001395 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001396 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1397 this.icon = icon.getResId();
1398 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001399 this.title = title;
1400 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001401 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001402 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001403 this.mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001404 this.mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001405 }
1406
1407 /**
Dan Sandler86647982015-05-13 23:41:13 -04001408 * Return an icon representing the action.
1409 */
1410 public Icon getIcon() {
1411 if (mIcon == null && icon != 0) {
1412 // you snuck an icon in here without using the builder; let's try to keep it
1413 mIcon = Icon.createWithResource("", icon);
1414 }
1415 return mIcon;
1416 }
1417
1418 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001419 * Get additional metadata carried around with this Action.
1420 */
1421 public Bundle getExtras() {
1422 return mExtras;
1423 }
1424
1425 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001426 * Return whether the platform should automatically generate possible replies for this
1427 * {@link Action}
1428 */
1429 public boolean getAllowGeneratedReplies() {
1430 return mAllowGeneratedReplies;
1431 }
1432
1433 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001434 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001435 * May return null if no remote inputs were added. Only returns inputs which accept
1436 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001437 */
1438 public RemoteInput[] getRemoteInputs() {
1439 return mRemoteInputs;
1440 }
1441
1442 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001443 * Returns the {@code SemanticAction} associated with this {@link Action}. A
1444 * {@code SemanticAction} denotes what an {@link Action}'s {@link PendingIntent} will do
1445 * (eg. reply, mark as read, delete, etc).
1446 */
1447 public @SemanticAction int getSemanticAction() {
1448 return mSemanticAction;
1449 }
1450
1451 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001452 * Get the list of inputs to be collected from the user that ONLY accept data when this
1453 * action is sent. These remote inputs are guaranteed to return true on a call to
1454 * {@link RemoteInput#isDataOnly}.
1455 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001456 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001457 *
1458 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1459 * of non-textual RemoteInputs do not access these remote inputs.
1460 */
1461 public RemoteInput[] getDataOnlyRemoteInputs() {
1462 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1463 }
1464
1465 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001466 * Builder class for {@link Action} objects.
1467 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001468 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001469 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001470 private final CharSequence mTitle;
1471 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001472 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001473 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001474 private ArrayList<RemoteInput> mRemoteInputs;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001475 private @SemanticAction int mSemanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001476
1477 /**
1478 * Construct a new builder for {@link Action} object.
1479 * @param icon icon to show for this action
1480 * @param title the title of the action
1481 * @param intent the {@link PendingIntent} to fire when users trigger this action
1482 */
Dan Sandler86647982015-05-13 23:41:13 -04001483 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001484 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001485 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001486 }
1487
1488 /**
1489 * Construct a new builder for {@link Action} object.
1490 * @param icon icon to show for this action
1491 * @param title the title of the action
1492 * @param intent the {@link PendingIntent} to fire when users trigger this action
1493 */
1494 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001495 this(icon, title, intent, new Bundle(), null, true, SEMANTIC_ACTION_NONE);
Griff Hazen959591e2014-05-15 22:26:18 -07001496 }
1497
1498 /**
1499 * Construct a new builder for {@link Action} object using the fields from an
1500 * {@link Action}.
1501 * @param action the action to read fields from.
1502 */
1503 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001504 this(action.getIcon(), action.title, action.actionIntent,
1505 new Bundle(action.mExtras), action.getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001506 action.getAllowGeneratedReplies(), action.getSemanticAction());
Griff Hazen959591e2014-05-15 22:26:18 -07001507 }
1508
Dan Sandler86647982015-05-13 23:41:13 -04001509 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001510 RemoteInput[] remoteInputs, boolean allowGeneratedReplies,
1511 @SemanticAction int semanticAction) {
Griff Hazen959591e2014-05-15 22:26:18 -07001512 mIcon = icon;
1513 mTitle = title;
1514 mIntent = intent;
1515 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001516 if (remoteInputs != null) {
1517 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1518 Collections.addAll(mRemoteInputs, remoteInputs);
1519 }
Adrian Roos7af53622016-10-12 13:44:05 -07001520 mAllowGeneratedReplies = allowGeneratedReplies;
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001521 mSemanticAction = semanticAction;
Griff Hazen959591e2014-05-15 22:26:18 -07001522 }
1523
1524 /**
1525 * Merge additional metadata into this builder.
1526 *
1527 * <p>Values within the Bundle will replace existing extras values in this Builder.
1528 *
1529 * @see Notification.Action#extras
1530 */
1531 public Builder addExtras(Bundle extras) {
1532 if (extras != null) {
1533 mExtras.putAll(extras);
1534 }
1535 return this;
1536 }
1537
1538 /**
1539 * Get the metadata Bundle used by this Builder.
1540 *
1541 * <p>The returned Bundle is shared with this Builder.
1542 */
1543 public Bundle getExtras() {
1544 return mExtras;
1545 }
1546
1547 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001548 * Add an input to be collected from the user when this action is sent.
1549 * Response values can be retrieved from the fired intent by using the
1550 * {@link RemoteInput#getResultsFromIntent} function.
1551 * @param remoteInput a {@link RemoteInput} to add to the action
1552 * @return this object for method chaining
1553 */
1554 public Builder addRemoteInput(RemoteInput remoteInput) {
1555 if (mRemoteInputs == null) {
1556 mRemoteInputs = new ArrayList<RemoteInput>();
1557 }
1558 mRemoteInputs.add(remoteInput);
1559 return this;
1560 }
1561
1562 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001563 * Set whether the platform should automatically generate possible replies to add to
1564 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1565 * {@link RemoteInput}, this has no effect.
1566 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1567 * otherwise
1568 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001569 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001570 */
1571 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1572 mAllowGeneratedReplies = allowGeneratedReplies;
1573 return this;
1574 }
1575
1576 /**
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001577 * Sets the {@code SemanticAction} for this {@link Action}. A
1578 * {@code SemanticAction} denotes what an {@link Action}'s
1579 * {@link PendingIntent} will do (eg. reply, mark as read, delete, etc).
1580 * @param semanticAction a SemanticAction defined within {@link Action} with
1581 * {@code SEMANTIC_ACTION_} prefixes
1582 * @return this object for method chaining
1583 */
1584 public Builder setSemanticAction(@SemanticAction int semanticAction) {
1585 mSemanticAction = semanticAction;
1586 return this;
1587 }
1588
1589 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001590 * Apply an extender to this action builder. Extenders may be used to add
1591 * metadata or change options on this builder.
1592 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001593 public Builder extend(Extender extender) {
1594 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001595 return this;
1596 }
1597
1598 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001599 * Combine all of the options that have been set and return a new {@link Action}
1600 * object.
1601 * @return the built action
1602 */
1603 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001604 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1605 RemoteInput[] previousDataInputs =
1606 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001607 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001608 for (RemoteInput input : previousDataInputs) {
1609 dataOnlyInputs.add(input);
1610 }
1611 }
1612 List<RemoteInput> textInputs = new ArrayList<>();
1613 if (mRemoteInputs != null) {
1614 for (RemoteInput input : mRemoteInputs) {
1615 if (input.isDataOnly()) {
1616 dataOnlyInputs.add(input);
1617 } else {
1618 textInputs.add(input);
1619 }
1620 }
1621 }
1622 if (!dataOnlyInputs.isEmpty()) {
1623 RemoteInput[] dataInputsArr =
1624 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1625 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1626 }
1627 RemoteInput[] textInputsArr = textInputs.isEmpty()
1628 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1629 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001630 mAllowGeneratedReplies, mSemanticAction);
Griff Hazen959591e2014-05-15 22:26:18 -07001631 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001632 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001633
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001634 @Override
1635 public Action clone() {
1636 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001637 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001638 title,
1639 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001640 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001641 getRemoteInputs(),
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001642 getAllowGeneratedReplies(),
1643 getSemanticAction());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001644 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001645
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001646 @Override
1647 public int describeContents() {
1648 return 0;
1649 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001650
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001651 @Override
1652 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001653 final Icon ic = getIcon();
1654 if (ic != null) {
1655 out.writeInt(1);
1656 ic.writeToParcel(out, 0);
1657 } else {
1658 out.writeInt(0);
1659 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001660 TextUtils.writeToParcel(title, out, flags);
1661 if (actionIntent != null) {
1662 out.writeInt(1);
1663 actionIntent.writeToParcel(out, flags);
1664 } else {
1665 out.writeInt(0);
1666 }
Griff Hazen959591e2014-05-15 22:26:18 -07001667 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001668 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001669 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001670 out.writeInt(mSemanticAction);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001671 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001672
Griff Hazen959591e2014-05-15 22:26:18 -07001673 public static final Parcelable.Creator<Action> CREATOR =
1674 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001675 public Action createFromParcel(Parcel in) {
1676 return new Action(in);
1677 }
1678 public Action[] newArray(int size) {
1679 return new Action[size];
1680 }
1681 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001682
1683 /**
1684 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1685 * metadata or change options on an action builder.
1686 */
1687 public interface Extender {
1688 /**
1689 * Apply this extender to a notification action builder.
1690 * @param builder the builder to be modified.
1691 * @return the build object for chaining.
1692 */
1693 public Builder extend(Builder builder);
1694 }
1695
1696 /**
1697 * Wearable extender for notification actions. To add extensions to an action,
1698 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1699 * the {@code WearableExtender()} constructor and apply it to a
1700 * {@link android.app.Notification.Action.Builder} using
1701 * {@link android.app.Notification.Action.Builder#extend}.
1702 *
1703 * <pre class="prettyprint">
1704 * Notification.Action action = new Notification.Action.Builder(
1705 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001706 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001707 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001708 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001709 */
1710 public static final class WearableExtender implements Extender {
1711 /** Notification action extra which contains wearable extensions */
1712 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1713
Pete Gastaf6781d2014-10-07 15:17:05 -04001714 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001715 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001716 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1717 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1718 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001719
1720 // Flags bitwise-ored to mFlags
1721 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001722 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001723 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001724
1725 // Default value for flags integer
1726 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1727
1728 private int mFlags = DEFAULT_FLAGS;
1729
Pete Gastaf6781d2014-10-07 15:17:05 -04001730 private CharSequence mInProgressLabel;
1731 private CharSequence mConfirmLabel;
1732 private CharSequence mCancelLabel;
1733
Griff Hazen61a9e862014-05-22 16:05:19 -07001734 /**
1735 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1736 * options.
1737 */
1738 public WearableExtender() {
1739 }
1740
1741 /**
1742 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1743 * wearable options present in an existing notification action.
1744 * @param action the notification action to inspect.
1745 */
1746 public WearableExtender(Action action) {
1747 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1748 if (wearableBundle != null) {
1749 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001750 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1751 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1752 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001753 }
1754 }
1755
1756 /**
1757 * Apply wearable extensions to a notification action that is being built. This is
1758 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1759 * method of {@link android.app.Notification.Action.Builder}.
1760 */
1761 @Override
1762 public Action.Builder extend(Action.Builder builder) {
1763 Bundle wearableBundle = new Bundle();
1764
1765 if (mFlags != DEFAULT_FLAGS) {
1766 wearableBundle.putInt(KEY_FLAGS, mFlags);
1767 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001768 if (mInProgressLabel != null) {
1769 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1770 }
1771 if (mConfirmLabel != null) {
1772 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1773 }
1774 if (mCancelLabel != null) {
1775 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1776 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001777
1778 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1779 return builder;
1780 }
1781
1782 @Override
1783 public WearableExtender clone() {
1784 WearableExtender that = new WearableExtender();
1785 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001786 that.mInProgressLabel = this.mInProgressLabel;
1787 that.mConfirmLabel = this.mConfirmLabel;
1788 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001789 return that;
1790 }
1791
1792 /**
1793 * Set whether this action is available when the wearable device is not connected to
1794 * a companion device. The user can still trigger this action when the wearable device is
1795 * offline, but a visual hint will indicate that the action may not be available.
1796 * Defaults to true.
1797 */
1798 public WearableExtender setAvailableOffline(boolean availableOffline) {
1799 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1800 return this;
1801 }
1802
1803 /**
1804 * Get whether this action is available when the wearable device is not connected to
1805 * a companion device. The user can still trigger this action when the wearable device is
1806 * offline, but a visual hint will indicate that the action may not be available.
1807 * Defaults to true.
1808 */
1809 public boolean isAvailableOffline() {
1810 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1811 }
1812
1813 private void setFlag(int mask, boolean value) {
1814 if (value) {
1815 mFlags |= mask;
1816 } else {
1817 mFlags &= ~mask;
1818 }
1819 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001820
1821 /**
1822 * Set a label to display while the wearable is preparing to automatically execute the
1823 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1824 *
1825 * @param label the label to display while the action is being prepared to execute
1826 * @return this object for method chaining
1827 */
1828 public WearableExtender setInProgressLabel(CharSequence label) {
1829 mInProgressLabel = label;
1830 return this;
1831 }
1832
1833 /**
1834 * Get the label to display while the wearable is preparing to automatically execute
1835 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1836 *
1837 * @return the label to display while the action is being prepared to execute
1838 */
1839 public CharSequence getInProgressLabel() {
1840 return mInProgressLabel;
1841 }
1842
1843 /**
1844 * Set a label to display to confirm that the action should be executed.
1845 * This is usually an imperative verb like "Send".
1846 *
1847 * @param label the label to confirm the action should be executed
1848 * @return this object for method chaining
1849 */
1850 public WearableExtender setConfirmLabel(CharSequence label) {
1851 mConfirmLabel = label;
1852 return this;
1853 }
1854
1855 /**
1856 * Get the label to display to confirm that the action should be executed.
1857 * This is usually an imperative verb like "Send".
1858 *
1859 * @return the label to confirm the action should be executed
1860 */
1861 public CharSequence getConfirmLabel() {
1862 return mConfirmLabel;
1863 }
1864
1865 /**
1866 * Set a label to display to cancel the action.
1867 * This is usually an imperative verb, like "Cancel".
1868 *
1869 * @param label the label to display to cancel the action
1870 * @return this object for method chaining
1871 */
1872 public WearableExtender setCancelLabel(CharSequence label) {
1873 mCancelLabel = label;
1874 return this;
1875 }
1876
1877 /**
1878 * Get the label to display to cancel the action.
1879 * This is usually an imperative verb like "Cancel".
1880 *
1881 * @return the label to display to cancel the action
1882 */
1883 public CharSequence getCancelLabel() {
1884 return mCancelLabel;
1885 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001886
1887 /**
1888 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1889 * platform that it can generate the appropriate transitions.
1890 * @param hintLaunchesActivity {@code true} if the content intent will launch
1891 * an activity and transitions should be generated, false otherwise.
1892 * @return this object for method chaining
1893 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001894 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001895 boolean hintLaunchesActivity) {
1896 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1897 return this;
1898 }
1899
1900 /**
1901 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1902 * platform that it can generate the appropriate transitions
1903 * @return {@code true} if the content intent will launch an activity and transitions
1904 * should be generated, false otherwise. The default value is {@code false} if this was
1905 * never set.
1906 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001907 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001908 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1909 }
Alex Hills9f087612016-06-07 09:08:59 -04001910
1911 /**
1912 * Set a hint that this Action should be displayed inline.
1913 *
1914 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1915 * otherwise
1916 * @return this object for method chaining
1917 */
1918 public WearableExtender setHintDisplayActionInline(
1919 boolean hintDisplayInline) {
1920 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1921 return this;
1922 }
1923
1924 /**
1925 * Get a hint that this Action should be displayed inline.
1926 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001927 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001928 * otherwise. The default value is {@code false} if this was never set.
1929 */
1930 public boolean getHintDisplayActionInline() {
1931 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1932 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001933 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001934
1935 /**
1936 * Provides meaning to an {@link Action} that hints at what the associated
1937 * {@link PendingIntent} will do. For example, an {@link Action} with a
1938 * {@link PendingIntent} that replies to a text message notification may have the
1939 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1940 *
1941 * @hide
1942 */
1943 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1944 SEMANTIC_ACTION_NONE,
1945 SEMANTIC_ACTION_REPLY,
1946 SEMANTIC_ACTION_MARK_AS_READ,
1947 SEMANTIC_ACTION_MARK_AS_UNREAD,
1948 SEMANTIC_ACTION_DELETE,
1949 SEMANTIC_ACTION_ARCHIVE,
1950 SEMANTIC_ACTION_MUTE,
1951 SEMANTIC_ACTION_UNMUTE,
1952 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08001953 SEMANTIC_ACTION_THUMBS_DOWN,
1954 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001955 })
1956 @Retention(RetentionPolicy.SOURCE)
1957 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001958 }
1959
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001960 /**
1961 * Array of all {@link Action} structures attached to this notification by
1962 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1963 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1964 * interface for invoking actions.
1965 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001966 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001967
1968 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001969 * Replacement version of this notification whose content will be shown
1970 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1971 * and {@link #VISIBILITY_PUBLIC}.
1972 */
1973 public Notification publicVersion;
1974
1975 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001976 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001977 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 */
1979 public Notification()
1980 {
1981 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001982 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001983 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985
1986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 * @hide
1988 */
1989 public Notification(Context context, int icon, CharSequence tickerText, long when,
1990 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1991 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001992 new Builder(context)
1993 .setWhen(when)
1994 .setSmallIcon(icon)
1995 .setTicker(tickerText)
1996 .setContentTitle(contentTitle)
1997 .setContentText(contentText)
1998 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1999 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001
2002 /**
2003 * Constructs a Notification object with the information needed to
2004 * have a status bar icon without the standard expanded view.
2005 *
2006 * @param icon The resource id of the icon to put in the status bar.
2007 * @param tickerText The text that flows by in the status bar when the notification first
2008 * activates.
2009 * @param when The time to show in the time field. In the System.currentTimeMillis
2010 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002011 *
2012 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002014 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 public Notification(int icon, CharSequence tickerText, long when)
2016 {
2017 this.icon = icon;
2018 this.tickerText = tickerText;
2019 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002020 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022
2023 /**
2024 * Unflatten the notification from a parcel.
2025 */
Svet Ganovddb94882016-06-23 19:55:24 -07002026 @SuppressWarnings("unchecked")
2027 public Notification(Parcel parcel) {
2028 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2029 // intents in extras are always written as the last entry.
2030 readFromParcelImpl(parcel);
2031 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002032 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002033 }
2034
2035 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 {
2037 int version = parcel.readInt();
2038
Adrian Roosfb921842017-10-26 14:49:56 +02002039 mWhitelistToken = parcel.readStrongBinder();
2040 if (mWhitelistToken == null) {
2041 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002042 }
2043 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002044 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002047 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002048 if (parcel.readInt() != 0) {
2049 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002050 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2051 icon = mSmallIcon.getResId();
2052 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 number = parcel.readInt();
2055 if (parcel.readInt() != 0) {
2056 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2057 }
2058 if (parcel.readInt() != 0) {
2059 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2060 }
2061 if (parcel.readInt() != 0) {
2062 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2063 }
2064 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002065 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002066 }
2067 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2069 }
Joe Onorato561d3852010-11-20 18:09:34 -08002070 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002071 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 defaults = parcel.readInt();
2074 flags = parcel.readInt();
2075 if (parcel.readInt() != 0) {
2076 sound = Uri.CREATOR.createFromParcel(parcel);
2077 }
2078
2079 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002080 if (parcel.readInt() != 0) {
2081 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 vibrate = parcel.createLongArray();
2084 ledARGB = parcel.readInt();
2085 ledOnMS = parcel.readInt();
2086 ledOffMS = parcel.readInt();
2087 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002088
2089 if (parcel.readInt() != 0) {
2090 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2091 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002092
2093 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002094
John Spurlockfd7f1e02014-03-18 16:41:57 -04002095 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002096
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002097 mGroupKey = parcel.readString();
2098
2099 mSortKey = parcel.readString();
2100
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002101 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002102 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002103
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002104 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2105
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002106 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002107 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2108 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002109
Chris Wren8fd39ec2014-02-27 17:43:26 -05002110 if (parcel.readInt() != 0) {
2111 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2112 }
2113
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002114 visibility = parcel.readInt();
2115
2116 if (parcel.readInt() != 0) {
2117 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2118 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002119
2120 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002121
2122 if (parcel.readInt() != 0) {
2123 mChannelId = parcel.readString();
2124 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002125 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002126
2127 if (parcel.readInt() != 0) {
2128 mShortcutId = parcel.readString();
2129 }
2130
2131 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002132
2133 if (parcel.readInt() != 0) {
2134 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2135 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002136
2137 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139
Andy Stadler110988c2010-12-03 14:29:16 -08002140 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002141 public Notification clone() {
2142 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002143 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002144 return that;
2145 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002146
Daniel Sandler1a497d32013-04-18 14:52:45 -04002147 /**
2148 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2149 * of this into that.
2150 * @hide
2151 */
2152 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002153 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002154 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002155 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002156 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002157 that.number = this.number;
2158
2159 // PendingIntents are global, so there's no reason (or way) to clone them.
2160 that.contentIntent = this.contentIntent;
2161 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002162 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002163
2164 if (this.tickerText != null) {
2165 that.tickerText = this.tickerText.toString();
2166 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002167 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002168 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002169 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002170 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002171 that.contentView = this.contentView.clone();
2172 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002173 if (heavy && this.mLargeIcon != null) {
2174 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002175 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002176 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002177 that.sound = this.sound; // android.net.Uri is immutable
2178 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002179 if (this.audioAttributes != null) {
2180 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2181 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002182
2183 final long[] vibrate = this.vibrate;
2184 if (vibrate != null) {
2185 final int N = vibrate.length;
2186 final long[] vib = that.vibrate = new long[N];
2187 System.arraycopy(vibrate, 0, vib, 0, N);
2188 }
2189
2190 that.ledARGB = this.ledARGB;
2191 that.ledOnMS = this.ledOnMS;
2192 that.ledOffMS = this.ledOffMS;
2193 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002194
Joe Onorato18e69df2010-05-17 22:26:12 -07002195 that.flags = this.flags;
2196
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002197 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002198
John Spurlockfd7f1e02014-03-18 16:41:57 -04002199 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002200
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002201 that.mGroupKey = this.mGroupKey;
2202
2203 that.mSortKey = this.mSortKey;
2204
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002205 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002206 try {
2207 that.extras = new Bundle(this.extras);
2208 // will unparcel
2209 that.extras.size();
2210 } catch (BadParcelableException e) {
2211 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2212 that.extras = null;
2213 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002214 }
2215
Felipe Lemedd85da62016-06-28 11:29:54 -07002216 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2217 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002218 }
2219
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002220 if (this.actions != null) {
2221 that.actions = new Action[this.actions.length];
2222 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002223 if ( this.actions[i] != null) {
2224 that.actions[i] = this.actions[i].clone();
2225 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002226 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002227 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002228
Daniel Sandler1a497d32013-04-18 14:52:45 -04002229 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002230 that.bigContentView = this.bigContentView.clone();
2231 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002232
Chris Wren8fd39ec2014-02-27 17:43:26 -05002233 if (heavy && this.headsUpContentView != null) {
2234 that.headsUpContentView = this.headsUpContentView.clone();
2235 }
2236
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002237 that.visibility = this.visibility;
2238
2239 if (this.publicVersion != null) {
2240 that.publicVersion = new Notification();
2241 this.publicVersion.cloneInto(that.publicVersion, heavy);
2242 }
2243
Dan Sandler26e81cf2014-05-06 10:01:27 -04002244 that.color = this.color;
2245
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002246 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002247 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002248 that.mShortcutId = this.mShortcutId;
2249 that.mBadgeIcon = this.mBadgeIcon;
2250 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002251 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002252
Daniel Sandler1a497d32013-04-18 14:52:45 -04002253 if (!heavy) {
2254 that.lightenPayload(); // will clean out extras
2255 }
2256 }
2257
2258 /**
2259 * Removes heavyweight parts of the Notification object for archival or for sending to
2260 * listeners when the full contents are not necessary.
2261 * @hide
2262 */
2263 public final void lightenPayload() {
2264 tickerView = null;
2265 contentView = null;
2266 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002267 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002268 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002269 if (extras != null && !extras.isEmpty()) {
2270 final Set<String> keyset = extras.keySet();
2271 final int N = keyset.size();
2272 final String[] keys = keyset.toArray(new String[N]);
2273 for (int i=0; i<N; i++) {
2274 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002275 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2276 continue;
2277 }
Dan Sandler50128532015-12-08 15:42:41 -05002278 final Object obj = extras.get(key);
2279 if (obj != null &&
2280 ( obj instanceof Parcelable
2281 || obj instanceof Parcelable[]
2282 || obj instanceof SparseArray
2283 || obj instanceof ArrayList)) {
2284 extras.remove(key);
2285 }
2286 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002287 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002288 }
2289
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002290 /**
2291 * Make sure this CharSequence is safe to put into a bundle, which basically
2292 * means it had better not be some custom Parcelable implementation.
2293 * @hide
2294 */
2295 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002296 if (cs == null) return cs;
2297 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2298 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2299 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002300 if (cs instanceof Parcelable) {
2301 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2302 + " instance is a custom Parcelable and not allowed in Notification");
2303 return cs.toString();
2304 }
Selim Cinek60a54252016-02-26 17:03:25 -08002305 return removeTextSizeSpans(cs);
2306 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002307
Selim Cinek60a54252016-02-26 17:03:25 -08002308 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2309 if (charSequence instanceof Spanned) {
2310 Spanned ss = (Spanned) charSequence;
2311 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2312 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2313 for (Object span : spans) {
2314 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002315 if (resultSpan instanceof CharacterStyle) {
2316 resultSpan = ((CharacterStyle) span).getUnderlying();
2317 }
2318 if (resultSpan instanceof TextAppearanceSpan) {
2319 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002320 resultSpan = new TextAppearanceSpan(
2321 originalSpan.getFamily(),
2322 originalSpan.getTextStyle(),
2323 -1,
2324 originalSpan.getTextColor(),
2325 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002326 } else if (resultSpan instanceof RelativeSizeSpan
2327 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002328 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002329 } else {
2330 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002331 }
2332 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2333 ss.getSpanFlags(span));
2334 }
2335 return builder;
2336 }
2337 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002338 }
2339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 public int describeContents() {
2341 return 0;
2342 }
2343
2344 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002345 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 */
Svet Ganovddb94882016-06-23 19:55:24 -07002347 public void writeToParcel(Parcel parcel, int flags) {
2348 // We need to mark all pending intents getting into the notification
2349 // system as being put there to later allow the notification ranker
2350 // to launch them and by doing so add the app to the battery saver white
2351 // list for a short period of time. The problem is that the system
2352 // cannot look into the extras as there may be parcelables there that
2353 // the platform does not know how to handle. To go around that we have
2354 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002355 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002356 if (collectPendingIntents) {
2357 PendingIntent.setOnMarshaledListener(
2358 (PendingIntent intent, Parcel out, int outFlags) -> {
2359 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002360 if (allPendingIntents == null) {
2361 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002362 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002363 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002364 }
2365 });
2366 }
2367 try {
2368 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002369 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002370 writeToParcelImpl(parcel, flags);
2371 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002372 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002373 } finally {
2374 if (collectPendingIntents) {
2375 PendingIntent.setOnMarshaledListener(null);
2376 }
2377 }
2378 }
2379
2380 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 parcel.writeInt(1);
2382
Adrian Roosfb921842017-10-26 14:49:56 +02002383 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002385 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002386 if (mSmallIcon == null && icon != 0) {
2387 // you snuck an icon in here without using the builder; let's try to keep it
2388 mSmallIcon = Icon.createWithResource("", icon);
2389 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002390 if (mSmallIcon != null) {
2391 parcel.writeInt(1);
2392 mSmallIcon.writeToParcel(parcel, 0);
2393 } else {
2394 parcel.writeInt(0);
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 parcel.writeInt(number);
2397 if (contentIntent != null) {
2398 parcel.writeInt(1);
2399 contentIntent.writeToParcel(parcel, 0);
2400 } else {
2401 parcel.writeInt(0);
2402 }
2403 if (deleteIntent != null) {
2404 parcel.writeInt(1);
2405 deleteIntent.writeToParcel(parcel, 0);
2406 } else {
2407 parcel.writeInt(0);
2408 }
2409 if (tickerText != null) {
2410 parcel.writeInt(1);
2411 TextUtils.writeToParcel(tickerText, parcel, flags);
2412 } else {
2413 parcel.writeInt(0);
2414 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002415 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002416 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002417 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002418 } else {
2419 parcel.writeInt(0);
2420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 if (contentView != null) {
2422 parcel.writeInt(1);
2423 contentView.writeToParcel(parcel, 0);
2424 } else {
2425 parcel.writeInt(0);
2426 }
Selim Cinek279fa862016-06-14 10:57:25 -07002427 if (mLargeIcon == null && largeIcon != null) {
2428 // you snuck an icon in here without using the builder; let's try to keep it
2429 mLargeIcon = Icon.createWithBitmap(largeIcon);
2430 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002431 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002432 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002433 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002434 } else {
2435 parcel.writeInt(0);
2436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437
2438 parcel.writeInt(defaults);
2439 parcel.writeInt(this.flags);
2440
2441 if (sound != null) {
2442 parcel.writeInt(1);
2443 sound.writeToParcel(parcel, 0);
2444 } else {
2445 parcel.writeInt(0);
2446 }
2447 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002448
2449 if (audioAttributes != null) {
2450 parcel.writeInt(1);
2451 audioAttributes.writeToParcel(parcel, 0);
2452 } else {
2453 parcel.writeInt(0);
2454 }
2455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 parcel.writeLongArray(vibrate);
2457 parcel.writeInt(ledARGB);
2458 parcel.writeInt(ledOnMS);
2459 parcel.writeInt(ledOffMS);
2460 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002461
2462 if (fullScreenIntent != null) {
2463 parcel.writeInt(1);
2464 fullScreenIntent.writeToParcel(parcel, 0);
2465 } else {
2466 parcel.writeInt(0);
2467 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002468
2469 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002470
John Spurlockfd7f1e02014-03-18 16:41:57 -04002471 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002472
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002473 parcel.writeString(mGroupKey);
2474
2475 parcel.writeString(mSortKey);
2476
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002477 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002478
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002479 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002480
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002481 if (bigContentView != null) {
2482 parcel.writeInt(1);
2483 bigContentView.writeToParcel(parcel, 0);
2484 } else {
2485 parcel.writeInt(0);
2486 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002487
Chris Wren8fd39ec2014-02-27 17:43:26 -05002488 if (headsUpContentView != null) {
2489 parcel.writeInt(1);
2490 headsUpContentView.writeToParcel(parcel, 0);
2491 } else {
2492 parcel.writeInt(0);
2493 }
2494
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002495 parcel.writeInt(visibility);
2496
2497 if (publicVersion != null) {
2498 parcel.writeInt(1);
2499 publicVersion.writeToParcel(parcel, 0);
2500 } else {
2501 parcel.writeInt(0);
2502 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002503
2504 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002505
2506 if (mChannelId != null) {
2507 parcel.writeInt(1);
2508 parcel.writeString(mChannelId);
2509 } else {
2510 parcel.writeInt(0);
2511 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002512 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002513
2514 if (mShortcutId != null) {
2515 parcel.writeInt(1);
2516 parcel.writeString(mShortcutId);
2517 } else {
2518 parcel.writeInt(0);
2519 }
2520
2521 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002522
2523 if (mSettingsText != null) {
2524 parcel.writeInt(1);
2525 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2526 } else {
2527 parcel.writeInt(0);
2528 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002529
2530 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
2532
2533 /**
2534 * Parcelable.Creator that instantiates Notification objects
2535 */
2536 public static final Parcelable.Creator<Notification> CREATOR
2537 = new Parcelable.Creator<Notification>()
2538 {
2539 public Notification createFromParcel(Parcel parcel)
2540 {
2541 return new Notification(parcel);
2542 }
2543
2544 public Notification[] newArray(int size)
2545 {
2546 return new Notification[size];
2547 }
2548 };
2549
2550 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002551 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2552 * <p>
2553 * For backwards compatibility {@code extras} holds some references to "real" member data such
2554 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2555 * fine as long as the object stays in one process.
2556 * <p>
2557 * However, once the notification goes into a parcel each reference gets marshalled separately,
2558 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2559 */
2560 private void fixDuplicateExtras() {
2561 if (extras != null) {
2562 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2563 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2564 }
2565 }
2566
2567 /**
2568 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2569 * separate object, replace it with the field's version to avoid holding duplicate copies.
2570 */
2571 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2572 if (original != null && extras.getParcelable(extraName) != null) {
2573 extras.putParcelable(extraName, original);
2574 }
2575 }
2576
2577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2579 * layout.
2580 *
2581 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2582 * in the view.</p>
2583 * @param context The context for your application / activity.
2584 * @param contentTitle The title that goes in the expanded entry.
2585 * @param contentText The text that goes in the expanded entry.
2586 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2587 * If this is an activity, it must include the
2588 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002589 * that you take care of task management as described in the
2590 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2591 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002592 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002593 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002594 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002596 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 public void setLatestEventInfo(Context context,
2598 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002599 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2600 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2601 new Throwable());
2602 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002603
Selim Cinek4ac6f602016-06-13 15:47:03 -07002604 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2605 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2606 }
2607
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002608 // ensure that any information already set directly is preserved
2609 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002610
2611 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002613 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002616 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002618 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002619
2620 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
2622
Julia Reynoldsda303542015-11-23 14:00:20 -05002623 /**
2624 * @hide
2625 */
2626 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002627 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002628 }
2629
2630 /**
2631 * @hide
2632 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002633 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002634 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002635 }
2636
Yi Jin6b514142017-10-30 14:54:12 -07002637 /**
2638 * @hide
2639 */
2640 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2641 long token = proto.start(fieldId);
2642 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2643 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2644 proto.write(NotificationProto.FLAGS, this.flags);
2645 proto.write(NotificationProto.COLOR, this.color);
2646 proto.write(NotificationProto.CATEGORY, this.category);
2647 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2648 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2649 if (this.actions != null) {
2650 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2651 }
2652 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2653 proto.write(NotificationProto.VISIBILITY, this.visibility);
2654 }
2655 if (publicVersion != null) {
2656 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2657 }
2658 proto.end(token);
2659 }
2660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 @Override
2662 public String toString() {
2663 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002664 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002665 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002666 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002667 sb.append(priority);
2668 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002669 if (contentView != null) {
2670 sb.append(contentView.getPackage());
2671 sb.append("/0x");
2672 sb.append(Integer.toHexString(contentView.getLayoutId()));
2673 } else {
2674 sb.append("null");
2675 }
2676 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002677 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2678 sb.append("default");
2679 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 int N = this.vibrate.length-1;
2681 sb.append("[");
2682 for (int i=0; i<N; i++) {
2683 sb.append(this.vibrate[i]);
2684 sb.append(',');
2685 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002686 if (N != -1) {
2687 sb.append(this.vibrate[N]);
2688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 } else {
2691 sb.append("null");
2692 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002693 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002694 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002696 } else if (this.sound != null) {
2697 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 } else {
2699 sb.append("null");
2700 }
Chris Wren365b6d32015-07-16 10:39:26 -04002701 if (this.tickerText != null) {
2702 sb.append(" tick");
2703 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002704 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002706 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002707 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002708 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002709 if (this.category != null) {
2710 sb.append(" category=");
2711 sb.append(this.category);
2712 }
2713 if (this.mGroupKey != null) {
2714 sb.append(" groupKey=");
2715 sb.append(this.mGroupKey);
2716 }
2717 if (this.mSortKey != null) {
2718 sb.append(" sortKey=");
2719 sb.append(this.mSortKey);
2720 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002721 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002722 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002723 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002724 }
2725 sb.append(" vis=");
2726 sb.append(visibilityToString(this.visibility));
2727 if (this.publicVersion != null) {
2728 sb.append(" publicVersion=");
2729 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002730 }
2731 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 return sb.toString();
2733 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002734
Dan Sandler1b718782014-07-18 12:43:45 -04002735 /**
2736 * {@hide}
2737 */
2738 public static String visibilityToString(int vis) {
2739 switch (vis) {
2740 case VISIBILITY_PRIVATE:
2741 return "PRIVATE";
2742 case VISIBILITY_PUBLIC:
2743 return "PUBLIC";
2744 case VISIBILITY_SECRET:
2745 return "SECRET";
2746 default:
2747 return "UNKNOWN(" + String.valueOf(vis) + ")";
2748 }
2749 }
2750
Joe Onoratocb109a02011-01-18 17:57:41 -08002751 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002752 * {@hide}
2753 */
2754 public static String priorityToString(@Priority int pri) {
2755 switch (pri) {
2756 case PRIORITY_MIN:
2757 return "MIN";
2758 case PRIORITY_LOW:
2759 return "LOW";
2760 case PRIORITY_DEFAULT:
2761 return "DEFAULT";
2762 case PRIORITY_HIGH:
2763 return "HIGH";
2764 case PRIORITY_MAX:
2765 return "MAX";
2766 default:
2767 return "UNKNOWN(" + String.valueOf(pri) + ")";
2768 }
2769 }
2770
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002771 /**
2772 * @hide
2773 */
2774 public boolean hasCompletedProgress() {
2775 // not a progress notification; can't be complete
2776 if (!extras.containsKey(EXTRA_PROGRESS)
2777 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2778 return false;
2779 }
2780 // many apps use max 0 for 'indeterminate'; not complete
2781 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2782 return false;
2783 }
2784 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2785 }
2786
Jeff Sharkey000ce802017-04-29 13:13:27 -06002787 /** @removed */
2788 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002789 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002790 return mChannelId;
2791 }
2792
2793 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002794 * Returns the id of the channel this notification posts to.
2795 */
2796 public String getChannelId() {
2797 return mChannelId;
2798 }
2799
Jeff Sharkey000ce802017-04-29 13:13:27 -06002800 /** @removed */
2801 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002802 public long getTimeout() {
2803 return mTimeout;
2804 }
2805
2806 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002807 * Returns the duration from posting after which this notification should be canceled by the
2808 * system, if it's not canceled already.
2809 */
2810 public long getTimeoutAfter() {
2811 return mTimeout;
2812 }
2813
2814 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002815 * Returns what icon should be shown for this notification if it is being displayed in a
2816 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2817 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2818 */
2819 public int getBadgeIconType() {
2820 return mBadgeIcon;
2821 }
2822
2823 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002824 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002825 *
2826 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2827 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002828 */
2829 public String getShortcutId() {
2830 return mShortcutId;
2831 }
2832
Julia Reynolds3aedded2017-03-31 14:42:09 -04002833
2834 /**
2835 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2836 */
2837 public CharSequence getSettingsText() {
2838 return mSettingsText;
2839 }
2840
Julia Reynolds13d898c2017-02-02 12:22:05 -05002841 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002842 * Returns which type of notifications in a group are responsible for audibly alerting the
2843 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2844 * {@link #GROUP_ALERT_SUMMARY}.
2845 */
2846 public @GroupAlertBehavior int getGroupAlertBehavior() {
2847 return mGroupAlertBehavior;
2848 }
2849
2850 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002851 * The small icon representing this notification in the status bar and content view.
2852 *
2853 * @return the small icon representing this notification.
2854 *
2855 * @see Builder#getSmallIcon()
2856 * @see Builder#setSmallIcon(Icon)
2857 */
2858 public Icon getSmallIcon() {
2859 return mSmallIcon;
2860 }
2861
2862 /**
2863 * Used when notifying to clean up legacy small icons.
2864 * @hide
2865 */
2866 public void setSmallIcon(Icon icon) {
2867 mSmallIcon = icon;
2868 }
2869
2870 /**
2871 * The large icon shown in this notification's content view.
2872 * @see Builder#getLargeIcon()
2873 * @see Builder#setLargeIcon(Icon)
2874 */
2875 public Icon getLargeIcon() {
2876 return mLargeIcon;
2877 }
2878
2879 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002880 * @hide
2881 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002882 public boolean isGroupSummary() {
2883 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2884 }
2885
2886 /**
2887 * @hide
2888 */
2889 public boolean isGroupChild() {
2890 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2891 }
2892
2893 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002894 * @hide
2895 */
2896 public boolean suppressAlertingDueToGrouping() {
2897 if (isGroupSummary()
2898 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2899 return true;
2900 } else if (isGroupChild()
2901 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2902 return true;
2903 }
2904 return false;
2905 }
2906
2907 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002908 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002909 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002910 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002911 * content views using the platform's notification layout template. If your app supports
2912 * versions of Android as old as API level 4, you can instead use
2913 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2914 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2915 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002916 *
Scott Main183bf112012-08-13 19:12:13 -07002917 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002918 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002919 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002920 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002921 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2922 * .setContentText(subject)
2923 * .setSmallIcon(R.drawable.new_mail)
2924 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002925 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002926 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002927 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002928 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002929 /**
2930 * @hide
2931 */
2932 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2933 "android.rebuild.contentViewActionCount";
2934 /**
2935 * @hide
2936 */
2937 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2938 = "android.rebuild.bigViewActionCount";
2939 /**
2940 * @hide
2941 */
2942 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2943 = "android.rebuild.hudViewActionCount";
2944
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002945 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002946
Selim Cinek6743c0b2017-01-18 18:24:01 -08002947 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2948 SystemProperties.getBoolean("notifications.only_title", true);
2949
Selim Cinek389edcd2017-05-11 19:16:44 -07002950 /**
2951 * The lightness difference that has to be added to the primary text color to obtain the
2952 * secondary text color when the background is light.
2953 */
2954 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2955
2956 /**
2957 * The lightness difference that has to be added to the primary text color to obtain the
2958 * secondary text color when the background is dark.
2959 * A bit less then the above value, since it looks better on dark backgrounds.
2960 */
2961 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2962
Joe Onorato46439ce2010-11-19 13:56:21 -08002963 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 private Notification mN;
2965 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002966 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002967 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002968 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002969 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002970 private boolean mIsLegacy;
2971 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002972
2973 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002974 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2975 */
2976 private int mCachedContrastColor = COLOR_INVALID;
2977 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002978 /**
2979 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2980 */
2981 private int mCachedAmbientColor = COLOR_INVALID;
2982 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002983
2984 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002985 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2986 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2987 */
2988 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002989 private int mTextColorsAreForBackground = COLOR_INVALID;
2990 private int mPrimaryTextColor = COLOR_INVALID;
2991 private int mSecondaryTextColor = COLOR_INVALID;
2992 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002993 private int mBackgroundColor = COLOR_INVALID;
2994 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002995 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002996 /**
2997 * A temporary location where actions are stored. If != null the view originally has action
2998 * but doesn't have any for this inflation.
2999 */
3000 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003001 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003002
Anthony Chenad4d1582017-04-10 16:07:58 -07003003 private boolean mTintActionButtons;
3004 private boolean mInNightMode;
3005
Adrian Roos70d7aa32017-01-11 15:39:06 -08003006 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003007 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003008 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003009 * @param context
3010 * A {@link Context} that will be used by the Builder to construct the
3011 * RemoteViews. The Context will not be held past the lifetime of this Builder
3012 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003013 * @param channelId
3014 * The constructed Notification will be posted on this
3015 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3016 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003017 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003018 public Builder(Context context, String channelId) {
3019 this(context, (Notification) null);
3020 mN.mChannelId = channelId;
3021 }
3022
3023 /**
3024 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3025 * instead. All posted Notifications must specify a NotificationChannel Id.
3026 */
3027 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003028 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003029 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003030 }
3031
Joe Onoratocb109a02011-01-18 17:57:41 -08003032 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003033 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003034 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003035 public Builder(Context context, Notification toAdopt) {
3036 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003037 Resources res = mContext.getResources();
3038 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3039
3040 if (res.getBoolean(R.bool.config_enableNightMode)) {
3041 Configuration currentConfig = res.getConfiguration();
3042 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3043 == Configuration.UI_MODE_NIGHT_YES;
3044 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003045
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003046 if (toAdopt == null) {
3047 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003048 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3049 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3050 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003051 mN.priority = PRIORITY_DEFAULT;
3052 mN.visibility = VISIBILITY_PRIVATE;
3053 } else {
3054 mN = toAdopt;
3055 if (mN.actions != null) {
3056 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003057 }
3058
Selim Cineke7238dd2017-12-14 17:48:32 -08003059 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3060 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3061 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 }
3063
Selim Cinek4ac6f602016-06-13 15:47:03 -07003064 if (mN.getSmallIcon() == null && mN.icon != 0) {
3065 setSmallIcon(mN.icon);
3066 }
3067
3068 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3069 setLargeIcon(mN.largeIcon);
3070 }
3071
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003072 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3073 if (!TextUtils.isEmpty(templateClass)) {
3074 final Class<? extends Style> styleClass
3075 = getNotificationStyleClass(templateClass);
3076 if (styleClass == null) {
3077 Log.d(TAG, "Unknown style class: " + templateClass);
3078 } else {
3079 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003080 final Constructor<? extends Style> ctor =
3081 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003082 ctor.setAccessible(true);
3083 final Style style = ctor.newInstance();
3084 style.restoreFromExtras(mN.extras);
3085
3086 if (style != null) {
3087 setStyle(style);
3088 }
3089 } catch (Throwable t) {
3090 Log.e(TAG, "Could not create Style", t);
3091 }
3092 }
3093 }
3094
3095 }
3096 }
3097
3098 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003099 if (mColorUtil == null) {
3100 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003101 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003102 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003103 }
3104
3105 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003106 * If this notification is duplicative of a Launcher shortcut, sets the
3107 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3108 * the shortcut.
3109 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003110 * This field will be ignored by Launchers that don't support badging, don't show
3111 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003112 *
3113 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3114 * supersedes
3115 */
3116 public Builder setShortcutId(String shortcutId) {
3117 mN.mShortcutId = shortcutId;
3118 return this;
3119 }
3120
3121 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003122 * Sets which icon to display as a badge for this notification.
3123 *
3124 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3125 * {@link #BADGE_ICON_LARGE}.
3126 *
3127 * Note: This value might be ignored, for launchers that don't support badge icons.
3128 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003129 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003130 mN.mBadgeIcon = icon;
3131 return this;
3132 }
3133
3134 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003135 * Sets the group alert behavior for this notification. Use this method to mute this
3136 * notification if alerts for this notification's group should be handled by a different
3137 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003138 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3139 * mute. For example, if you want only the summary of your group to make noise, all
3140 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003141 *
3142 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3143 */
3144 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3145 mN.mGroupAlertBehavior = groupAlertBehavior;
3146 return this;
3147 }
3148
Jeff Sharkey000ce802017-04-29 13:13:27 -06003149 /** @removed */
3150 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003151 public Builder setChannel(String channelId) {
3152 mN.mChannelId = channelId;
3153 return this;
3154 }
3155
3156 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003157 * Specifies the channel the notification should be delivered on.
3158 */
3159 public Builder setChannelId(String channelId) {
3160 mN.mChannelId = channelId;
3161 return this;
3162 }
3163
Jeff Sharkey000ce802017-04-29 13:13:27 -06003164 /** @removed */
3165 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003166 public Builder setTimeout(long durationMs) {
3167 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003168 return this;
3169 }
3170
3171 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003172 * Specifies a duration in milliseconds after which this notification should be canceled,
3173 * if it is not already canceled.
3174 */
3175 public Builder setTimeoutAfter(long durationMs) {
3176 mN.mTimeout = durationMs;
3177 return this;
3178 }
3179
3180 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003181 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003182 *
3183 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3184 * shown anymore by default and must be opted into by using
3185 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003186 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003187 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003188 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003189 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003190 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003191 return this;
3192 }
3193
Joe Onoratocb109a02011-01-18 17:57:41 -08003194 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003195 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003196 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003197 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3198 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003199 */
3200 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003201 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003202 return this;
3203 }
3204
3205 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003206 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003207 *
3208 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003209 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003210 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003211 * Useful when showing an elapsed time (like an ongoing phone call).
3212 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003213 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003214 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003215 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003216 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003217 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003218 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003219 */
3220 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003221 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003222 return this;
3223 }
3224
3225 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003226 * Sets the Chronometer to count down instead of counting up.
3227 *
3228 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3229 * If it isn't set the chronometer will count up.
3230 *
3231 * @see #setUsesChronometer(boolean)
3232 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003233 public Builder setChronometerCountDown(boolean countDown) {
3234 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003235 return this;
3236 }
3237
3238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003239 * Set the small icon resource, which will be used to represent the notification in the
3240 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003241 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003242
3243 * The platform template for the expanded view will draw this icon in the left, unless a
3244 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3245 * icon will be moved to the right-hand side.
3246 *
3247
3248 * @param icon
3249 * A resource ID in the application's package of the drawable to use.
3250 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003251 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003252 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003253 return setSmallIcon(icon != 0
3254 ? Icon.createWithResource(mContext, icon)
3255 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003256 }
3257
Joe Onoratocb109a02011-01-18 17:57:41 -08003258 /**
3259 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3260 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3261 * LevelListDrawable}.
3262 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003263 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003264 * @param level The level to use for the icon.
3265 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003266 * @see Notification#icon
3267 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003268 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003269 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003270 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003271 return setSmallIcon(icon);
3272 }
3273
3274 /**
3275 * Set the small icon, which will be used to represent the notification in the
3276 * status bar and content view (unless overriden there by a
3277 * {@link #setLargeIcon(Bitmap) large icon}).
3278 *
3279 * @param icon An Icon object to use.
3280 * @see Notification#icon
3281 */
3282 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003283 mN.setSmallIcon(icon);
3284 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3285 mN.icon = icon.getResId();
3286 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003287 return this;
3288 }
3289
Joe Onoratocb109a02011-01-18 17:57:41 -08003290 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003291 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003292 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003293 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003294 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003295 return this;
3296 }
3297
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003299 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003300 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003301 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003302 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003303 return this;
3304 }
3305
Joe Onoratocb109a02011-01-18 17:57:41 -08003306 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003307 * This provides some additional information that is displayed in the notification. No
3308 * guarantees are given where exactly it is displayed.
3309 *
3310 * <p>This information should only be provided if it provides an essential
3311 * benefit to the understanding of the notification. The more text you provide the
3312 * less readable it becomes. For example, an email client should only provide the account
3313 * name here if more than one email account has been added.</p>
3314 *
3315 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3316 * notification header area.
3317 *
3318 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3319 * this will be shown in the third line of text in the platform notification template.
3320 * You should not be using {@link #setProgress(int, int, boolean)} at the
3321 * same time on those versions; they occupy the same place.
3322 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003323 */
3324 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003325 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003326 return this;
3327 }
3328
3329 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003330 * Provides text that will appear as a link to your application's settings.
3331 *
3332 * <p>This text does not appear within notification {@link Style templates} but may
3333 * appear when the user uses an affordance to learn more about the notification.
3334 * Additionally, this text will not appear unless you provide a valid link target by
3335 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3336 *
3337 * <p>This text is meant to be concise description about what the user can customize
3338 * when they click on this link. The recommended maximum length is 40 characters.
3339 * @param text
3340 * @return
3341 */
3342 public Builder setSettingsText(CharSequence text) {
3343 mN.mSettingsText = safeCharSequence(text);
3344 return this;
3345 }
3346
3347 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003348 * Set the remote input history.
3349 *
3350 * This should be set to the most recent inputs that have been sent
3351 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3352 * longer relevant (e.g. for chat notifications once the other party has responded).
3353 *
3354 * The most recent input must be stored at the 0 index, the second most recent at the
3355 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3356 * and how much of each individual input is shown.
3357 *
3358 * <p>Note: The reply text will only be shown on notifications that have least one action
3359 * with a {@code RemoteInput}.</p>
3360 */
3361 public Builder setRemoteInputHistory(CharSequence[] text) {
3362 if (text == null) {
3363 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3364 } else {
3365 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3366 CharSequence[] safe = new CharSequence[N];
3367 for (int i = 0; i < N; i++) {
3368 safe[i] = safeCharSequence(text[i]);
3369 }
3370 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3371 }
3372 return this;
3373 }
3374
3375 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003376 * Sets the number of items this notification represents. May be displayed as a badge count
3377 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003378 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003379 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003380 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003381 return this;
3382 }
3383
Joe Onoratocb109a02011-01-18 17:57:41 -08003384 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003385 * A small piece of additional information pertaining to this notification.
3386 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003387 * The platform template will draw this on the last line of the notification, at the far
3388 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003389 *
3390 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3391 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3392 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003393 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003394 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003395 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003396 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003397 return this;
3398 }
3399
Joe Onoratocb109a02011-01-18 17:57:41 -08003400 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003401 * Set the progress this notification represents.
3402 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003403 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003404 */
3405 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003406 mN.extras.putInt(EXTRA_PROGRESS, progress);
3407 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3408 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003409 return this;
3410 }
3411
3412 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003413 * Supply a custom RemoteViews to use instead of the platform template.
3414 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003416 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003417 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003418 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003419 return setCustomContentView(views);
3420 }
3421
3422 /**
3423 * Supply custom RemoteViews to use instead of the platform template.
3424 *
3425 * This will override the layout that would otherwise be constructed by this Builder
3426 * object.
3427 */
3428 public Builder setCustomContentView(RemoteViews contentView) {
3429 mN.contentView = contentView;
3430 return this;
3431 }
3432
3433 /**
3434 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3435 *
3436 * This will override the expanded layout that would otherwise be constructed by this
3437 * Builder object.
3438 */
3439 public Builder setCustomBigContentView(RemoteViews contentView) {
3440 mN.bigContentView = contentView;
3441 return this;
3442 }
3443
3444 /**
3445 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3446 *
3447 * This will override the heads-up layout that would otherwise be constructed by this
3448 * Builder object.
3449 */
3450 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3451 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003452 return this;
3453 }
3454
Joe Onoratocb109a02011-01-18 17:57:41 -08003455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003456 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3457 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003458 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3459 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3460 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003461 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003462 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003463 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003464 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003465 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003466 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003468 return this;
3469 }
3470
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003472 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3473 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003474 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003475 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003476 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003477 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003478 return this;
3479 }
3480
Joe Onoratocb109a02011-01-18 17:57:41 -08003481 /**
3482 * An intent to launch instead of posting the notification to the status bar.
3483 * Only for use with extremely high-priority notifications demanding the user's
3484 * <strong>immediate</strong> attention, such as an incoming phone call or
3485 * alarm clock that the user has explicitly set to a particular time.
3486 * If this facility is used for something else, please give the user an option
3487 * to turn it off and use a normal notification, as this can be extremely
3488 * disruptive.
3489 *
Chris Wren47c20a12014-06-18 17:27:29 -04003490 * <p>
3491 * The system UI may choose to display a heads-up notification, instead of
3492 * launching this intent, while the user is using the device.
3493 * </p>
3494 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003495 * @param intent The pending intent to launch.
3496 * @param highPriority Passing true will cause this notification to be sent
3497 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003498 *
3499 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003500 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003501 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003502 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003503 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3504 return this;
3505 }
3506
Joe Onoratocb109a02011-01-18 17:57:41 -08003507 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003508 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003509 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003510 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003511 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003512 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003513 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003514 return this;
3515 }
3516
Joe Onoratocb109a02011-01-18 17:57:41 -08003517 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003518 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003519 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003520 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003521 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003522 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003523 setTicker(tickerText);
3524 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003525 return this;
3526 }
3527
Joe Onoratocb109a02011-01-18 17:57:41 -08003528 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003529 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003530 *
3531 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003532 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3533 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003534 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003535 public Builder setLargeIcon(Bitmap b) {
3536 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3537 }
3538
3539 /**
3540 * Add a large icon to the notification content view.
3541 *
3542 * In the platform template, this image will be shown on the left of the notification view
3543 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3544 * badge atop the large icon).
3545 */
3546 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003547 mN.mLargeIcon = icon;
3548 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003549 return this;
3550 }
3551
Joe Onoratocb109a02011-01-18 17:57:41 -08003552 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003553 * Set the sound to play.
3554 *
John Spurlockc0650f022014-07-19 13:22:39 -04003555 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3556 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003557 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003558 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003559 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003560 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003561 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003562 mN.sound = sound;
3563 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003564 return this;
3565 }
3566
Joe Onoratocb109a02011-01-18 17:57:41 -08003567 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003568 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003569 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003570 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3571 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003572 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003573 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003574 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003575 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003576 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003577 mN.sound = sound;
3578 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003579 return this;
3580 }
3581
Joe Onoratocb109a02011-01-18 17:57:41 -08003582 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003583 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3584 * use during playback.
3585 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003586 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003587 * @see Notification#sound
3588 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003589 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003590 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003591 mN.sound = sound;
3592 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003593 return this;
3594 }
3595
3596 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003597 * Set the vibration pattern to use.
3598 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003599 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3600 * <code>pattern</code> parameter.
3601 *
Chris Wren47c20a12014-06-18 17:27:29 -04003602 * <p>
3603 * A notification that vibrates is more likely to be presented as a heads-up notification.
3604 * </p>
3605 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003606 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003607 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003608 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003609 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003610 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003611 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003612 return this;
3613 }
3614
Joe Onoratocb109a02011-01-18 17:57:41 -08003615 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003616 * Set the desired color for the indicator LED on the device, as well as the
3617 * blink duty cycle (specified in milliseconds).
3618 *
3619
3620 * Not all devices will honor all (or even any) of these values.
3621 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003622 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003623 * @see Notification#ledARGB
3624 * @see Notification#ledOnMS
3625 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003626 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003627 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003628 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003629 mN.ledARGB = argb;
3630 mN.ledOnMS = onMs;
3631 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003632 if (onMs != 0 || offMs != 0) {
3633 mN.flags |= FLAG_SHOW_LIGHTS;
3634 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003635 return this;
3636 }
3637
Joe Onoratocb109a02011-01-18 17:57:41 -08003638 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003639 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003640 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003641
3642 * Ongoing notifications cannot be dismissed by the user, so your application or service
3643 * must take care of canceling them.
3644 *
3645
3646 * They are typically used to indicate a background task that the user is actively engaged
3647 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3648 * (e.g., a file download, sync operation, active network connection).
3649 *
3650
3651 * @see Notification#FLAG_ONGOING_EVENT
3652 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003653 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003654 public Builder setOngoing(boolean ongoing) {
3655 setFlag(FLAG_ONGOING_EVENT, ongoing);
3656 return this;
3657 }
3658
Joe Onoratocb109a02011-01-18 17:57:41 -08003659 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003660 * Set whether this notification should be colorized. When set, the color set with
3661 * {@link #setColor(int)} will be used as the background color of this notification.
3662 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003663 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3664 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003665 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003666 * For most styles, the coloring will only be applied if the notification is for a
3667 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003668 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003669 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003670 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003671 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003672 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003673 */
3674 public Builder setColorized(boolean colorize) {
3675 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3676 return this;
3677 }
3678
3679 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003680 * Set this flag if you would only like the sound, vibrate
3681 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003682 *
3683 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003684 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003685 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3686 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3687 return this;
3688 }
3689
Joe Onoratocb109a02011-01-18 17:57:41 -08003690 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003691 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003692 *
3693 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003694 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003695 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003696 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003697 return this;
3698 }
3699
Joe Onoratocb109a02011-01-18 17:57:41 -08003700 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003701 * Set whether or not this notification should not bridge to other devices.
3702 *
3703 * <p>Some notifications can be bridged to other devices for remote display.
3704 * This hint can be set to recommend this notification not be bridged.
3705 */
3706 public Builder setLocalOnly(boolean localOnly) {
3707 setFlag(FLAG_LOCAL_ONLY, localOnly);
3708 return this;
3709 }
3710
3711 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003712 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003713 * <p>
3714 * The value should be one or more of the following fields combined with
3715 * bitwise-or:
3716 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3717 * <p>
3718 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003719 *
3720 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003721 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003722 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003723 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003724 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003725 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003726 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003727 return this;
3728 }
3729
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003730 /**
3731 * Set the priority of this notification.
3732 *
3733 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003734 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003735 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003736 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003737 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003738 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003739 return this;
3740 }
Joe Malin8d40d042012-11-05 11:36:40 -08003741
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003742 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003743 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003744 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003745 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003746 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003747 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003748 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003749 return this;
3750 }
3751
3752 /**
Chris Wrendde75302014-03-26 17:24:15 -04003753 * Add a person that is relevant to this notification.
3754 *
Chris Wrene6c48932014-09-29 17:19:27 -04003755 * <P>
3756 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003757 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3758 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3759 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003760 * </P>
3761 *
3762 * <P>
3763 * The person should be specified by the {@code String} representation of a
3764 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3765 * </P>
3766 *
3767 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3768 * URIs. The path part of these URIs must exist in the contacts database, in the
3769 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3770 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003771 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3772 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003773 * </P>
3774 *
3775 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003776 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003777 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003778 */
Chris Wrene6c48932014-09-29 17:19:27 -04003779 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003780 addPerson(new Person().setUri(uri));
3781 return this;
3782 }
3783
3784 /**
3785 * Add a person that is relevant to this notification.
3786 *
3787 * <P>
3788 * Depending on user preferences, this annotation may allow the notification to pass
3789 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3790 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3791 * appear more prominently in the user interface.
3792 * </P>
3793 *
3794 * <P>
3795 * A person should usually contain a uri in order to benefit from the ranking boost.
3796 * However, even if no uri is provided, it's beneficial to provide other people in the
3797 * notification, such that listeners and voice only devices can announce and handle them
3798 * properly.
3799 * </P>
3800 *
3801 * @param person the person to add.
3802 * @see Notification#EXTRA_PEOPLE_LIST
3803 */
3804 public Builder addPerson(Person person) {
3805 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003806 return this;
3807 }
3808
3809 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003810 * Set this notification to be part of a group of notifications sharing the same key.
3811 * Grouped notifications may display in a cluster or stack on devices which
3812 * support such rendering.
3813 *
3814 * <p>To make this notification the summary for its group, also call
3815 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3816 * {@link #setSortKey}.
3817 * @param groupKey The group key of the group.
3818 * @return this object for method chaining
3819 */
3820 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003821 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003822 return this;
3823 }
3824
3825 /**
3826 * Set this notification to be the group summary for a group of notifications.
3827 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003828 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3829 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003830 * @param isGroupSummary Whether this notification should be a group summary.
3831 * @return this object for method chaining
3832 */
3833 public Builder setGroupSummary(boolean isGroupSummary) {
3834 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3835 return this;
3836 }
3837
3838 /**
3839 * Set a sort key that orders this notification among other notifications from the
3840 * same package. This can be useful if an external sort was already applied and an app
3841 * would like to preserve this. Notifications will be sorted lexicographically using this
3842 * value, although providing different priorities in addition to providing sort key may
3843 * cause this value to be ignored.
3844 *
3845 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003846 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003847 *
3848 * @see String#compareTo(String)
3849 */
3850 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003851 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003852 return this;
3853 }
3854
3855 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003856 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003857 *
Griff Hazen720042b2014-02-24 15:46:56 -08003858 * <p>Values within the Bundle will replace existing extras values in this Builder.
3859 *
3860 * @see Notification#extras
3861 */
Griff Hazen959591e2014-05-15 22:26:18 -07003862 public Builder addExtras(Bundle extras) {
3863 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003864 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003865 }
3866 return this;
3867 }
3868
3869 /**
3870 * Set metadata for this notification.
3871 *
3872 * <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 -04003873 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003874 * called.
3875 *
Griff Hazen720042b2014-02-24 15:46:56 -08003876 * <p>Replaces any existing extras values with those from the provided Bundle.
3877 * Use {@link #addExtras} to merge in metadata instead.
3878 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003879 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 */
Griff Hazen959591e2014-05-15 22:26:18 -07003881 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003882 if (extras != null) {
3883 mUserExtras = extras;
3884 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003885 return this;
3886 }
3887
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003888 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003889 * Get the current metadata Bundle used by this notification Builder.
3890 *
3891 * <p>The returned Bundle is shared with this Builder.
3892 *
3893 * <p>The current contents of this Bundle are copied into the Notification each time
3894 * {@link #build()} is called.
3895 *
3896 * @see Notification#extras
3897 */
3898 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003899 return mUserExtras;
3900 }
3901
3902 private Bundle getAllExtras() {
3903 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3904 saveExtras.putAll(mN.extras);
3905 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003906 }
3907
3908 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003909 * Add an action to this notification. Actions are typically displayed by
3910 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003911 * <p>
3912 * Every action must have an icon (32dp square and matching the
3913 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3914 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3915 * <p>
3916 * A notification in its expanded form can display up to 3 actions, from left to right in
3917 * the order they were added. Actions will not be displayed when the notification is
3918 * collapsed, however, so be sure that any essential functions may be accessed by the user
3919 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003920 *
3921 * @param icon Resource ID of a drawable that represents the action.
3922 * @param title Text describing the action.
3923 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003924 *
3925 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003926 */
Dan Sandler86647982015-05-13 23:41:13 -04003927 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003928 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003929 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003930 return this;
3931 }
3932
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003933 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003934 * Add an action to this notification. Actions are typically displayed by
3935 * the system as a button adjacent to the notification content.
3936 * <p>
3937 * Every action must have an icon (32dp square and matching the
3938 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3939 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3940 * <p>
3941 * A notification in its expanded form can display up to 3 actions, from left to right in
3942 * the order they were added. Actions will not be displayed when the notification is
3943 * collapsed, however, so be sure that any essential functions may be accessed by the user
3944 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3945 *
3946 * @param action The action to add.
3947 */
3948 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003949 if (action != null) {
3950 mActions.add(action);
3951 }
Griff Hazen959591e2014-05-15 22:26:18 -07003952 return this;
3953 }
3954
3955 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003956 * Alter the complete list of actions attached to this notification.
3957 * @see #addAction(Action).
3958 *
3959 * @param actions
3960 * @return
3961 */
3962 public Builder setActions(Action... actions) {
3963 mActions.clear();
3964 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003965 if (actions[i] != null) {
3966 mActions.add(actions[i]);
3967 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003968 }
3969 return this;
3970 }
3971
3972 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003973 * Add a rich notification style to be applied at build time.
3974 *
3975 * @param style Object responsible for modifying the notification style.
3976 */
3977 public Builder setStyle(Style style) {
3978 if (mStyle != style) {
3979 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003980 if (mStyle != null) {
3981 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3983 } else {
3984 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003985 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003986 }
3987 return this;
3988 }
3989
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003990 /**
3991 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003992 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003993 * @return The same Builder.
3994 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003995 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003996 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003997 return this;
3998 }
3999
4000 /**
4001 * Supply a replacement Notification whose contents should be shown in insecure contexts
4002 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4003 * @param n A replacement notification, presumably with some or all info redacted.
4004 * @return The same Builder.
4005 */
4006 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004007 if (n != null) {
4008 mN.publicVersion = new Notification();
4009 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4010 } else {
4011 mN.publicVersion = null;
4012 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004013 return this;
4014 }
4015
Griff Hazenb720abe2014-05-20 13:15:30 -07004016 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004017 * Apply an extender to this notification builder. Extenders may be used to add
4018 * metadata or change options on this builder.
4019 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004020 public Builder extend(Extender extender) {
4021 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004022 return this;
4023 }
4024
Dan Sandler4e787062015-06-17 15:09:48 -04004025 /**
4026 * @hide
4027 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004028 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004029 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004030 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004031 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004032 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004033 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004034 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004035 }
4036
Dan Sandler26e81cf2014-05-06 10:01:27 -04004037 /**
4038 * Sets {@link Notification#color}.
4039 *
4040 * @param argb The accent color to use
4041 *
4042 * @return The same Builder.
4043 */
Tor Norbye80756e32015-03-02 09:39:27 -08004044 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004045 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004046 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004047 return this;
4048 }
4049
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004050 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004051 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4052 // This user can never be a badged profile,
4053 // and also includes USER_ALL system notifications.
4054 return null;
4055 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004056 // Note: This assumes that the current user can read the profile badge of the
4057 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004058 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004059 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004060 }
4061
4062 private Bitmap getProfileBadge() {
4063 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004064 if (badge == null) {
4065 return null;
4066 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004067 final int size = mContext.getResources().getDimensionPixelSize(
4068 R.dimen.notification_badge_size);
4069 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004070 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004071 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004072 badge.draw(canvas);
4073 return bitmap;
4074 }
4075
Selim Cinekc848c3a2016-01-13 15:27:30 -08004076 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004077 Bitmap profileBadge = getProfileBadge();
4078
Kenny Guy98193ea2014-07-24 19:54:37 +01004079 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004080 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4081 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004082 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004083 contentView.setDrawableTint(R.id.profile_badge, false,
4084 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004085 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004086 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004087 }
4088
Christoph Studerfe718432014-09-01 18:21:18 +02004089 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004090 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004091 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004092 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004093 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004094 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004095 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004096 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004097 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004098 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004099 }
4100
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004101 /**
4102 * Resets the notification header to its original state
4103 */
4104 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004105 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4106 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004107 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004108 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004109 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004110 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004111 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004112 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4113 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004114 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004115 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004116 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004117 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004118 contentView.setImageViewIcon(R.id.profile_badge, null);
4119 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004120 }
4121
4122 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004123 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4124 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004125 }
4126
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004127 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004128 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004129 }
4130
4131 /**
4132 * @param hasProgress whether the progress bar should be shown and set
4133 */
4134 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004135 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4136 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004137 }
4138
Adrian Roos70d7aa32017-01-11 15:39:06 -08004139 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004140 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004141
Christoph Studerfe718432014-09-01 18:21:18 +02004142 resetStandardTemplate(contentView);
4143
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004144 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004145 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004146 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004147 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004148 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4149 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004150 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004151 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004152 if (!p.ambient) {
4153 setTextViewColorPrimary(contentView, R.id.title);
4154 }
Selim Cinek954cc232016-05-20 13:29:23 -07004155 contentView.setViewLayoutWidth(R.id.title, showProgress
4156 ? ViewGroup.LayoutParams.WRAP_CONTENT
4157 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004158 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004159 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004160 int textId = showProgress ? com.android.internal.R.id.text_line_1
4161 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004162 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004163 if (!p.ambient) {
4164 setTextViewColorSecondary(contentView, textId);
4165 }
Selim Cinek41598732016-01-11 16:58:37 -08004166 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004167 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004168
Selim Cinek279fa862016-06-14 10:57:25 -07004169 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004170
Selim Cinek29603462015-11-17 19:04:39 -08004171 return contentView;
4172 }
4173
Selim Cinek48f66b72017-08-18 16:17:51 -07004174 private CharSequence processTextSpans(CharSequence text) {
4175 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004176 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004177 }
4178 return text;
4179 }
4180
Selim Cinek7b9605b2017-01-19 17:36:00 -08004181 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4182 ensureColors();
4183 contentView.setTextColor(id, mPrimaryTextColor);
4184 }
4185
Selim Cinek48f66b72017-08-18 16:17:51 -07004186 private boolean hasForegroundColor() {
4187 return mForegroundColor != COLOR_INVALID;
4188 }
4189
Selim Cinek389edcd2017-05-11 19:16:44 -07004190 /**
4191 * @return the primary text color
4192 * @hide
4193 */
4194 @VisibleForTesting
4195 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004196 ensureColors();
4197 return mPrimaryTextColor;
4198 }
4199
Selim Cinek389edcd2017-05-11 19:16:44 -07004200 /**
4201 * @return the secondary text color
4202 * @hide
4203 */
4204 @VisibleForTesting
4205 public int getSecondaryTextColor() {
4206 ensureColors();
4207 return mSecondaryTextColor;
4208 }
4209
Selim Cinek7b9605b2017-01-19 17:36:00 -08004210 private int getActionBarColor() {
4211 ensureColors();
4212 return mActionBarColor;
4213 }
4214
Selim Cinek622c64a2017-04-17 17:10:05 -07004215 private int getActionBarColorDeEmphasized() {
4216 int backgroundColor = getBackgroundColor();
4217 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4218 }
4219
Selim Cinek7b9605b2017-01-19 17:36:00 -08004220 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4221 ensureColors();
4222 contentView.setTextColor(id, mSecondaryTextColor);
4223 }
4224
4225 private void ensureColors() {
4226 int backgroundColor = getBackgroundColor();
4227 if (mPrimaryTextColor == COLOR_INVALID
4228 || mSecondaryTextColor == COLOR_INVALID
4229 || mActionBarColor == COLOR_INVALID
4230 || mTextColorsAreForBackground != backgroundColor) {
4231 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004232 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004233 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4234 backgroundColor);
4235 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4236 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004237 if (backgroundColor != COLOR_DEFAULT
4238 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4239 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4240 mPrimaryTextColor, backgroundColor, 4.5);
4241 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4242 mSecondaryTextColor, backgroundColor, 4.5);
4243 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004244 } else {
4245 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4246 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4247 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4248 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004249 // We only respect the given colors if worst case Black or White still has
4250 // contrast
4251 boolean backgroundLight = backLum > textLum
4252 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4253 || backLum <= textLum
4254 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004255 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004256 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004257 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4258 mForegroundColor,
4259 backgroundColor,
4260 true /* findFG */,
4261 4.5f);
4262 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004263 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004264 } else {
4265 mSecondaryTextColor =
4266 NotificationColorUtil.findContrastColorAgainstDark(
4267 mForegroundColor,
4268 backgroundColor,
4269 true /* findFG */,
4270 4.5f);
4271 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004272 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004273 }
4274 } else {
4275 mPrimaryTextColor = mForegroundColor;
4276 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004277 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4278 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004279 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4280 backgroundColor) < 4.5f) {
4281 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004282 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004283 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4284 mSecondaryTextColor,
4285 backgroundColor,
4286 true /* findFG */,
4287 4.5f);
4288 } else {
4289 mSecondaryTextColor
4290 = NotificationColorUtil.findContrastColorAgainstDark(
4291 mSecondaryTextColor,
4292 backgroundColor,
4293 true /* findFG */,
4294 4.5f);
4295 }
4296 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004297 mSecondaryTextColor, backgroundLight
4298 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4299 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004300 }
4301 }
4302 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004303 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4304 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004305 }
4306 }
4307
4308 private void updateBackgroundColor(RemoteViews contentView) {
4309 if (isColorized()) {
4310 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4311 getBackgroundColor());
4312 } else {
4313 // Clear it!
4314 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4315 0);
4316 }
4317 }
4318
Selim Cinek860b6da2015-12-16 19:02:19 -08004319 /**
4320 * @param remoteView the remote view to update the minheight in
4321 * @param hasMinHeight does it have a mimHeight
4322 * @hide
4323 */
4324 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4325 int minHeight = 0;
4326 if (hasMinHeight) {
4327 // we need to set the minHeight of the notification
4328 minHeight = mContext.getResources().getDimensionPixelSize(
4329 com.android.internal.R.dimen.notification_min_content_height);
4330 }
4331 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4332 }
4333
Selim Cinek29603462015-11-17 19:04:39 -08004334 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004335 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4336 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4337 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4338 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004339 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004340 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004341 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004342 contentView.setProgressBackgroundTintList(
4343 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4344 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004345 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004346 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004347 contentView.setProgressTintList(R.id.progress, colorStateList);
4348 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004349 }
Selim Cinek29603462015-11-17 19:04:39 -08004350 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004351 } else {
4352 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004353 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004354 }
Joe Onorato561d3852010-11-20 18:09:34 -08004355 }
4356
Selim Cinek88188f22017-09-19 16:46:56 -07004357 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4358 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004359 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4360 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4361 }
Selim Cinek88188f22017-09-19 16:46:56 -07004362 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4363 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004364 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4365 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4366 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004367 int endMargin = R.dimen.notification_content_picture_margin;
4368 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4369 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4370 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004371 }
4372 // Bind the reply action
4373 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004374
Selim Cinek88188f22017-09-19 16:46:56 -07004375 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4376 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4377 if (actionVisible) {
4378 // We're only showing the icon as big if we're hiding the large icon
4379 int contrastColor = resolveContrastColor();
4380 int iconColor;
4381 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004382 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004383 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004384 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004385 contentView.setOnClickPendingIntent(R.id.right_icon,
4386 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004387 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004388 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4389 ? Color.BLACK : Color.WHITE;
4390 } else {
4391 contentView.setImageViewResource(R.id.right_icon,
4392 R.drawable.ic_reply_notification_large);
4393 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4394 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004395 }
Selim Cinek88188f22017-09-19 16:46:56 -07004396 contentView.setDrawableTint(replyId,
4397 false /* targetBackground */,
4398 iconColor,
4399 PorterDuff.Mode.SRC_ATOP);
4400 contentView.setOnClickPendingIntent(replyId,
4401 action.actionIntent);
4402 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4403 } else {
4404 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004405 }
Selim Cinek88188f22017-09-19 16:46:56 -07004406 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4407 ? View.VISIBLE
4408 : View.GONE);
4409 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004410 ? View.VISIBLE
4411 : View.GONE);
4412 }
4413
4414 private Action findReplyAction() {
4415 ArrayList<Action> actions = mActions;
4416 if (mOriginalActions != null) {
4417 actions = mOriginalActions;
4418 }
4419 int numActions = actions.size();
4420 for (int i = 0; i < numActions; i++) {
4421 Action action = actions.get(i);
4422 if (hasValidRemoteInput(action)) {
4423 return action;
4424 }
4425 }
4426 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004427 }
4428
Selim Cinekafeed292017-12-12 17:32:44 -08004429 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4430 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004431 bindSmallIcon(contentView, ambient);
4432 bindHeaderAppName(contentView, ambient);
4433 if (!ambient) {
4434 // Ambient view does not have these
4435 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004436 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004437 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004438 bindProfileBadge(contentView);
4439 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004440 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004441 }
4442
4443 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004444 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004445 contentView.setDrawableTint(R.id.expand_button, false, color,
4446 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004447 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004448 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004449 }
4450
Selim Cinek99104832017-01-25 14:47:33 -08004451 /**
4452 * @return the color that is used as the first primary highlight color. This is applied
4453 * in several places like the action buttons or the app name in the header.
4454 */
4455 private int getPrimaryHighlightColor() {
4456 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4457 }
4458
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004459 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4460 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004461 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004462 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004463 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4464 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4465 contentView.setLong(R.id.chronometer, "setBase",
4466 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4467 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004468 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004469 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004470 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004471 } else {
4472 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4473 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004474 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004475 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004476 } else {
4477 // We still want a time to be set but gone, such that we can show and hide it
4478 // on demand in case it's a child notification without anything in the header
4479 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004480 }
4481 }
4482
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004483 private void bindHeaderText(RemoteViews contentView) {
4484 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4485 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004486 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004487 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004488 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004489 if (headerText == null
4490 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4491 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4492 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4493 }
4494 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004495 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004496 contentView.setTextViewText(R.id.header_text, processTextSpans(
4497 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004498 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004499 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4500 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004501 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004502 }
4503 }
4504
Selim Cinekafeed292017-12-12 17:32:44 -08004505 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4506 if (!TextUtils.isEmpty(secondaryText)) {
4507 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4508 processLegacyText(secondaryText)));
4509 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4510 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4511 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4512 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4513 }
4514 }
4515
Adrian Rooseba05822016-04-22 17:09:27 -07004516 /**
4517 * @hide
4518 */
4519 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004520 CharSequence name = null;
4521 final PackageManager pm = mContext.getPackageManager();
4522 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4523 // only system packages which lump together a bunch of unrelated stuff
4524 // may substitute a different name to make the purpose of the
4525 // notification more clear. the correct package label should always
4526 // be accessible via SystemUI.
4527 final String pkg = mContext.getPackageName();
4528 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4529 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4530 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4531 name = subName;
4532 } else {
4533 Log.w(TAG, "warning: pkg "
4534 + pkg + " attempting to substitute app name '" + subName
4535 + "' without holding perm "
4536 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4537 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004538 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004539 if (TextUtils.isEmpty(name)) {
4540 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4541 }
4542 if (TextUtils.isEmpty(name)) {
4543 // still nothing?
4544 return null;
4545 }
4546
4547 return String.valueOf(name);
4548 }
Adrian Roos487374f2017-01-11 15:48:14 -08004549 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004550 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004551 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004552 setTextViewColorPrimary(contentView, R.id.app_name_text);
4553 } else {
4554 contentView.setTextColor(R.id.app_name_text,
4555 ambient ? resolveAmbientColor() : resolveContrastColor());
4556 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004557 }
4558
Adrian Roos487374f2017-01-11 15:48:14 -08004559 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004560 if (mN.mSmallIcon == null && mN.icon != 0) {
4561 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4562 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004563 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004564 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004565 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004566 }
4567
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004568 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004569 * @return true if the built notification will show the time or the chronometer; false
4570 * otherwise
4571 */
4572 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004573 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004574 }
4575
Christoph Studerfe718432014-09-01 18:21:18 +02004576 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004577 // actions_container is only reset when there are no actions to avoid focus issues with
4578 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004579 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004580 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004581
4582 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4583 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4584
4585 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4586 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4587 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4588 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004589
4590 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004591 }
4592
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004593 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004594 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004595 }
4596
Adrian Roos70d7aa32017-01-11 15:39:06 -08004597 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4598 StandardTemplateParams p) {
4599 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004600
Christoph Studerfe718432014-09-01 18:21:18 +02004601 resetStandardTemplateWithActions(big);
4602
Adrian Roose458aa82015-12-08 16:17:19 -08004603 boolean validRemoteInput = false;
4604
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004605 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004606 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004607 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004608 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004609 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004610 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004611 if (p.ambient) {
4612 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004613 } else if (isColorized()) {
4614 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4615 } else {
4616 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4617 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004618 }
Adrian Roosf852a422016-06-03 13:33:43 -07004619 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4620 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004621 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004622 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004623 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004624 boolean actionHasValidInput = hasValidRemoteInput(action);
4625 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004626
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004627 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004628 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004629 if (actionHasValidInput) {
4630 // Clear the drawable
4631 button.setInt(R.id.action0, "setBackgroundResource", 0);
4632 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004633 big.addView(R.id.actions, button);
4634 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004635 } else {
4636 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004637 }
Adrian Roose458aa82015-12-08 16:17:19 -08004638
4639 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004640 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004641 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4642 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004643 big.setTextViewText(R.id.notification_material_reply_text_1,
4644 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004645 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004646
4647 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4648 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004649 big.setTextViewText(R.id.notification_material_reply_text_2,
4650 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004651 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004652
4653 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4654 big.setViewVisibility(
4655 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004656 big.setTextViewText(R.id.notification_material_reply_text_3,
4657 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004658 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004659 }
4660 }
4661 }
4662
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004663 return big;
4664 }
4665
Adrian Roose458aa82015-12-08 16:17:19 -08004666 private boolean hasValidRemoteInput(Action action) {
4667 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4668 // Weird actions
4669 return false;
4670 }
4671
4672 RemoteInput[] remoteInputs = action.getRemoteInputs();
4673 if (remoteInputs == null) {
4674 return false;
4675 }
4676
4677 for (RemoteInput r : remoteInputs) {
4678 CharSequence[] choices = r.getChoices();
4679 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4680 return true;
4681 }
4682 }
4683 return false;
4684 }
4685
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004686 /**
4687 * Construct a RemoteViews for the final 1U notification layout. In order:
4688 * 1. Custom contentView from the caller
4689 * 2. Style's proposed content view
4690 * 3. Standard template view
4691 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004692 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004693 return createContentView(false /* increasedheight */ );
4694 }
4695
4696 /**
4697 * Construct a RemoteViews for the smaller content view.
4698 *
4699 * @param increasedHeight true if this layout be created with an increased height. Some
4700 * styles may support showing more then just that basic 1U size
4701 * and the system may decide to render important notifications
4702 * slightly bigger even when collapsed.
4703 *
4704 * @hide
4705 */
4706 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004707 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004708 return mN.contentView;
4709 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004710 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004711 if (styleView != null) {
4712 return styleView;
4713 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004714 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004715 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004716 }
4717
Selim Cineka7679b62017-05-10 16:33:25 -07004718 private boolean useExistingRemoteView() {
4719 return mStyle == null || (!mStyle.displayCustomViewInline()
4720 && !mRebuildStyledRemoteViews);
4721 }
4722
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004723 /**
4724 * Construct a RemoteViews for the final big notification layout.
4725 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004726 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004727 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004728 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004729 return mN.bigContentView;
4730 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004731 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004732 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004733 } else if (mActions.size() != 0) {
4734 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004735 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004736 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004737 return result;
4738 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004739
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004740 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004741 * Construct a RemoteViews for the final notification header only. This will not be
4742 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004743 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004744 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004745 * @hide
4746 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004747 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004748 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4749 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004750 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004751 ambient ? R.layout.notification_template_ambient_header
4752 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004753 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004754 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004755 if (colorized != null) {
4756 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4757 } else {
4758 mN.extras.remove(EXTRA_COLORIZED);
4759 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004760 return header;
4761 }
4762
Adrian Roos487374f2017-01-11 15:48:14 -08004763 /**
4764 * Construct a RemoteViews for the ambient version of the notification.
4765 *
4766 * @hide
4767 */
4768 public RemoteViews makeAmbientNotification() {
4769 RemoteViews ambient = applyStandardTemplateWithActions(
4770 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004771 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004772 return ambient;
4773 }
4774
Selim Cinek29603462015-11-17 19:04:39 -08004775 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004776 if (result != null) {
4777 result.setViewVisibility(R.id.text_line_1, View.GONE);
4778 }
Selim Cinek29603462015-11-17 19:04:39 -08004779 }
4780
Selim Cinek6743c0b2017-01-18 18:24:01 -08004781 /**
4782 * Adapt the Notification header if this view is used as an expanded view.
4783 *
4784 * @hide
4785 */
4786 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004787 if (result != null) {
4788 result.setBoolean(R.id.notification_header, "setExpanded", true);
4789 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004790 }
4791
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004792 /**
4793 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004794 *
4795 * @param increasedHeight true if this layout be created with an increased height. Some
4796 * styles may support showing more then just that basic 1U size
4797 * and the system may decide to render important notifications
4798 * slightly bigger even when collapsed.
4799 *
4800 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004801 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004802 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004803 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004804 return mN.headsUpContentView;
4805 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004806 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4807 if (styleView != null) {
4808 return styleView;
4809 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004810 } else if (mActions.size() == 0) {
4811 return null;
4812 }
4813
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004814 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004815 }
4816
Selim Cinek624c02db2015-12-14 21:00:02 -08004817 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004818 * Construct a RemoteViews for the final heads-up notification layout.
4819 */
4820 public RemoteViews createHeadsUpContentView() {
4821 return createHeadsUpContentView(false /* useIncreasedHeight */);
4822 }
4823
4824 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004825 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4826 *
4827 * @hide
4828 */
4829 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004830 return makePublicView(false /* ambient */);
4831 }
4832
4833 /**
4834 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4835 *
4836 * @hide
4837 */
4838 public RemoteViews makePublicAmbientNotification() {
4839 return makePublicView(true /* ambient */);
4840 }
4841
4842 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004843 if (mN.publicVersion != null) {
4844 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004845 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004846 }
4847 Bundle savedBundle = mN.extras;
4848 Style style = mStyle;
4849 mStyle = null;
4850 Icon largeIcon = mN.mLargeIcon;
4851 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004852 Bitmap largeIconLegacy = mN.largeIcon;
4853 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004854 ArrayList<Action> actions = mActions;
4855 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004856 Bundle publicExtras = new Bundle();
4857 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4858 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4859 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4860 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004861 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4862 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004863 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004864 RemoteViews view;
4865 if (ambient) {
4866 publicExtras.putCharSequence(EXTRA_TITLE,
4867 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4868 view = makeAmbientNotification();
4869 } else{
4870 view = makeNotificationHeader(false /* ambient */);
4871 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4872 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004873 mN.extras = savedBundle;
4874 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004875 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004876 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004877 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004878 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004879 }
4880
Selim Cinek6743c0b2017-01-18 18:24:01 -08004881 /**
4882 * Construct a content view for the display when low - priority
4883 *
4884 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4885 * a new subtext is created consisting of the content of the
4886 * notification.
4887 * @hide
4888 */
4889 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4890 int color = mN.color;
4891 mN.color = COLOR_DEFAULT;
4892 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4893 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4894 CharSequence newSummary = createSummaryText();
4895 if (!TextUtils.isEmpty(newSummary)) {
4896 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4897 }
4898 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004899
Adrian Roos6f6e1592017-05-02 16:22:53 -07004900 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004901 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004902 if (summary != null) {
4903 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4904 } else {
4905 mN.extras.remove(EXTRA_SUB_TEXT);
4906 }
4907 mN.color = color;
4908 return header;
4909 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004910
Selim Cinek6743c0b2017-01-18 18:24:01 -08004911 private CharSequence createSummaryText() {
4912 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4913 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4914 return titleText;
4915 }
4916 SpannableStringBuilder summary = new SpannableStringBuilder();
4917 if (titleText == null) {
4918 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4919 }
4920 BidiFormatter bidi = BidiFormatter.getInstance();
4921 if (titleText != null) {
4922 summary.append(bidi.unicodeWrap(titleText));
4923 }
4924 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4925 if (titleText != null && contentText != null) {
4926 summary.append(bidi.unicodeWrap(mContext.getText(
4927 R.string.notification_header_divider_symbol_with_spaces)));
4928 }
4929 if (contentText != null) {
4930 summary.append(bidi.unicodeWrap(contentText));
4931 }
4932 return summary;
4933 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004934
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004935 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004936 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004937 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004938 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004939 emphazisedMode ? getEmphasizedActionLayoutResource()
4940 : tombstone ? getActionTombstoneLayoutResource()
4941 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004942 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004943 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004944 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004945 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004946 if (action.mRemoteInputs != null) {
4947 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4948 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004949 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004950 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004951 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004952 int bgColor;
4953 if (isColorized()) {
4954 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4955 } else {
4956 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4957 : R.color.notification_action_list_dark);
4958 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004959 button.setDrawableTint(R.id.button_holder, true,
4960 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004961 CharSequence title = action.title;
4962 ColorStateList[] outResultColor = null;
4963 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004964 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004965 } else {
4966 outResultColor = new ColorStateList[1];
4967 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4968 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004969 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004970 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004971 if (outResultColor != null && outResultColor[0] != null) {
4972 // We need to set the text color as well since changing a text to uppercase
4973 // clears its spans.
4974 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004975 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004976 button.setTextColor(R.id.action0,resolveContrastColor());
4977 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004978 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004979 button.setTextViewText(R.id.action0, processTextSpans(
4980 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004981 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004982 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004983 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004984 button.setTextColor(R.id.action0,
4985 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004986 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004987 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004988 return button;
4989 }
4990
Joe Onoratocb109a02011-01-18 17:57:41 -08004991 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004992 * Ensures contrast on color spans against a background color. also returns the color of the
4993 * text if a span was found that spans over the whole text.
4994 *
4995 * @param charSequence the charSequence on which the spans are
4996 * @param background the background color to ensure the contrast against
4997 * @param outResultColor an array in which a color will be returned as the first element if
4998 * there exists a full length color span.
4999 * @return the contrasted charSequence
5000 */
5001 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5002 ColorStateList[] outResultColor) {
5003 if (charSequence instanceof Spanned) {
5004 Spanned ss = (Spanned) charSequence;
5005 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5006 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5007 for (Object span : spans) {
5008 Object resultSpan = span;
5009 int spanStart = ss.getSpanStart(span);
5010 int spanEnd = ss.getSpanEnd(span);
5011 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5012 if (resultSpan instanceof CharacterStyle) {
5013 resultSpan = ((CharacterStyle) span).getUnderlying();
5014 }
5015 if (resultSpan instanceof TextAppearanceSpan) {
5016 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5017 ColorStateList textColor = originalSpan.getTextColor();
5018 if (textColor != null) {
5019 int[] colors = textColor.getColors();
5020 int[] newColors = new int[colors.length];
5021 for (int i = 0; i < newColors.length; i++) {
5022 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005023 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005024 }
5025 textColor = new ColorStateList(textColor.getStates().clone(),
5026 newColors);
5027 resultSpan = new TextAppearanceSpan(
5028 originalSpan.getFamily(),
5029 originalSpan.getTextStyle(),
5030 originalSpan.getTextSize(),
5031 textColor,
5032 originalSpan.getLinkTextColor());
5033 if (fullLength) {
5034 outResultColor[0] = new ColorStateList(
5035 textColor.getStates().clone(), newColors);
5036 }
5037 }
5038 } else if (resultSpan instanceof ForegroundColorSpan) {
5039 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5040 int foregroundColor = originalSpan.getForegroundColor();
5041 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005042 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005043 resultSpan = new ForegroundColorSpan(foregroundColor);
5044 if (fullLength) {
5045 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5046 }
5047 } else {
5048 resultSpan = span;
5049 }
5050
5051 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5052 }
5053 return builder;
5054 }
5055 return charSequence;
5056 }
5057
5058 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005059 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005060 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005061 */
5062 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005063 if (!mIsLegacyInitialized) {
5064 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5065 < Build.VERSION_CODES.LOLLIPOP;
5066 mIsLegacyInitialized = true;
5067 }
5068 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005069 }
5070
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005071 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005072 return processLegacyText(charSequence, false /* ambient */);
5073 }
5074
5075 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5076 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5077 boolean wantLightText = ambient;
5078 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005079 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005080 } else {
5081 return charSequence;
5082 }
5083 }
5084
Dan Sandler26e81cf2014-05-06 10:01:27 -04005085 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005086 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005087 */
Adrian Roos487374f2017-01-11 15:48:14 -08005088 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5089 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005090 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005091 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005092 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005093 contentView.setDrawableTint(R.id.icon, false, color,
5094 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005095
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005096 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005097 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005098 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005099 }
5100
Dan Sandler26e81cf2014-05-06 10:01:27 -04005101 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005102 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005103 * if it's grayscale).
5104 */
5105 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005106 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5107 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005108 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005109 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005110 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5111 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005112 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005113 }
5114
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005115 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005116 if (mN.color != COLOR_DEFAULT) {
5117 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005118 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005119 }
5120
Adrian Roos4ff3b122016-02-01 12:26:13 -08005121 int resolveContrastColor() {
5122 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5123 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005124 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005125
Selim Cinekac5f0272017-05-02 16:05:41 -07005126 int color;
5127 int background = mBackgroundColorHint;
5128 if (mBackgroundColorHint == COLOR_INVALID) {
5129 background = mContext.getColor(
5130 com.android.internal.R.color.notification_material_background_color);
5131 }
5132 if (mN.color == COLOR_DEFAULT) {
5133 ensureColors();
5134 color = mSecondaryTextColor;
5135 } else {
5136 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005137 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005138 }
5139 if (Color.alpha(color) < 255) {
5140 // alpha doesn't go well for color filters, so let's blend it manually
5141 color = NotificationColorUtil.compositeColors(color, background);
5142 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005143 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005144 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005145 }
5146
Adrian Roos487374f2017-01-11 15:48:14 -08005147 int resolveAmbientColor() {
5148 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5149 return mCachedAmbientColor;
5150 }
5151 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5152
5153 mCachedAmbientColorIsFor = mN.color;
5154 return mCachedAmbientColor = contrasted;
5155 }
5156
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005157 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005158 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005159 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005160 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005161 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005162 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005163 mN.actions = new Action[mActions.size()];
5164 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005165 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005166 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005167 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005168 }
Selim Cinek247fa012016-02-18 09:50:48 -08005169 if (mN.bigContentView != null || mN.contentView != null
5170 || mN.headsUpContentView != null) {
5171 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5172 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005173 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005174 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005175
Julia Reynolds3b848122016-02-26 10:45:32 -05005176 /**
5177 * Creates a Builder from an existing notification so further changes can be made.
5178 * @param context The context for your application / activity.
5179 * @param n The notification to create a Builder from.
5180 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005181 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005182 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005183 ApplicationInfo applicationInfo = n.extras.getParcelable(
5184 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005185 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005186 if (applicationInfo != null) {
5187 try {
5188 builderContext = context.createApplicationContext(applicationInfo,
5189 Context.CONTEXT_RESTRICTED);
5190 } catch (NameNotFoundException e) {
5191 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5192 builderContext = context; // try with our context
5193 }
5194 } else {
5195 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005196 }
5197
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005198 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005199 }
5200
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005201 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005202 * @deprecated Use {@link #build()} instead.
5203 */
5204 @Deprecated
5205 public Notification getNotification() {
5206 return build();
5207 }
5208
5209 /**
5210 * Combine all of the options that have been set and return a new {@link Notification}
5211 * object.
5212 */
5213 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005214 // first, add any extras from the calling code
5215 if (mUserExtras != null) {
5216 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005217 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005218
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005219 mN.creationTime = System.currentTimeMillis();
5220
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005221 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005222 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005223
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005224 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005225
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005226 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005227 mStyle.reduceImageSizes(mContext);
5228 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005229 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005230 }
Selim Cinekd0426622017-07-11 13:19:59 +02005231 mN.reduceImageSizes(mContext);
5232
Adrian Roos5081c0d2016-02-26 16:04:19 -08005233 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005234 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005235 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005236 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005237 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5238 mN.contentView.getSequenceNumber());
5239 }
5240 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005241 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005242 if (mN.bigContentView != null) {
5243 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5244 mN.bigContentView.getSequenceNumber());
5245 }
5246 }
5247 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005248 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005249 if (mN.headsUpContentView != null) {
5250 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5251 mN.headsUpContentView.getSequenceNumber());
5252 }
5253 }
5254 }
5255
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005256 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5257 mN.flags |= FLAG_SHOW_LIGHTS;
5258 }
5259
Adrian Roosfb921842017-10-26 14:49:56 +02005260 mN.allPendingIntents = null;
5261
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005262 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005263 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005264
5265 /**
5266 * Apply this Builder to an existing {@link Notification} object.
5267 *
5268 * @hide
5269 */
5270 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005271 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005272 return n;
5273 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005274
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005275 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005276 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005277 * change. Also removes extenders on low ram devices, as
5278 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005279 *
5280 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5281 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005282 * @hide
5283 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005284 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005285 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005286
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005287 // Only strip views for known Styles because we won't know how to
5288 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005289 if (!isLowRam
5290 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005291 && getNotificationStyleClass(templateClass) == null) {
5292 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005293 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005294
5295 // Only strip unmodified BuilderRemoteViews.
5296 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005297 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005298 n.contentView.getSequenceNumber();
5299 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005300 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005301 n.bigContentView.getSequenceNumber();
5302 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005303 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005304 n.headsUpContentView.getSequenceNumber();
5305
5306 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005307 if (!isLowRam
5308 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005309 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005310 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005311
5312 Notification clone = n.clone();
5313 if (stripContentView) {
5314 clone.contentView = null;
5315 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5316 }
5317 if (stripBigContentView) {
5318 clone.bigContentView = null;
5319 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5320 }
5321 if (stripHeadsUpContentView) {
5322 clone.headsUpContentView = null;
5323 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5324 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005325 if (isLowRam) {
5326 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5327 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5328 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5329 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005330 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005331 }
5332
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005333 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005334 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005335 }
5336
5337 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005338 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005339 }
5340
5341 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005342 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005343 }
5344
5345 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005346 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005347 }
5348
5349 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005350 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005351 }
5352
Adrian Roosc1a80b02016-04-05 14:54:55 -07005353 private int getMessagingLayoutResource() {
5354 return R.layout.notification_template_material_messaging;
5355 }
5356
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005357 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005358 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005359 }
5360
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005361 private int getEmphasizedActionLayoutResource() {
5362 return R.layout.notification_material_action_emphasized;
5363 }
5364
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005365 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005366 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005367 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005368
5369 private int getBackgroundColor() {
5370 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005371 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005372 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005373 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5374 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005375 }
5376 }
5377
5378 private boolean isColorized() {
5379 return mN.isColorized();
5380 }
Selim Cinek99104832017-01-25 14:47:33 -08005381
Anthony Chenad4d1582017-04-10 16:07:58 -07005382 private boolean shouldTintActionButtons() {
5383 return mTintActionButtons;
5384 }
5385
Selim Cinek99104832017-01-25 14:47:33 -08005386 private boolean textColorsNeedInversion() {
5387 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5388 return false;
5389 }
5390 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5391 return targetSdkVersion > Build.VERSION_CODES.M
5392 && targetSdkVersion < Build.VERSION_CODES.O;
5393 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005394
5395 /**
5396 * Set a color palette to be used as the background and textColors
5397 *
5398 * @param backgroundColor the color to be used as the background
5399 * @param foregroundColor the color to be used as the foreground
5400 *
5401 * @hide
5402 */
5403 public void setColorPalette(int backgroundColor, int foregroundColor) {
5404 mBackgroundColor = backgroundColor;
5405 mForegroundColor = foregroundColor;
5406 mTextColorsAreForBackground = COLOR_INVALID;
5407 ensureColors();
5408 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005409
5410 /**
5411 * Sets the background color for this notification to be a different one then the default.
5412 * This is mainly used to calculate contrast and won't necessarily be applied to the
5413 * background.
5414 *
5415 * @hide
5416 */
5417 public void setBackgroundColorHint(int backgroundColor) {
5418 mBackgroundColorHint = backgroundColor;
5419 }
Selim Cineka7679b62017-05-10 16:33:25 -07005420
5421
5422 /**
5423 * Forces all styled remoteViews to be built from scratch and not use any cached
5424 * RemoteViews.
5425 * This is needed for legacy apps that are baking in their remoteviews into the
5426 * notification.
5427 *
5428 * @hide
5429 */
5430 public void setRebuildStyledRemoteViews(boolean rebuild) {
5431 mRebuildStyledRemoteViews = rebuild;
5432 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005433 }
5434
5435 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005436 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5437 * remote views.
5438 *
5439 * @hide
5440 */
5441 void reduceImageSizes(Context context) {
5442 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5443 return;
5444 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005445 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005446 if (mLargeIcon != null || largeIcon != null) {
5447 Resources resources = context.getResources();
5448 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005449 int maxWidth = resources.getDimensionPixelSize(isLowRam
5450 ? R.dimen.notification_right_icon_size_low_ram
5451 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005452 int maxHeight = maxWidth;
5453 if (MediaStyle.class.equals(style)
5454 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005455 maxHeight = resources.getDimensionPixelSize(isLowRam
5456 ? R.dimen.notification_media_image_max_height_low_ram
5457 : R.dimen.notification_media_image_max_height);
5458 maxWidth = resources.getDimensionPixelSize(isLowRam
5459 ? R.dimen.notification_media_image_max_width_low_ram
5460 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005461 }
5462 if (mLargeIcon != null) {
5463 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5464 }
5465 if (largeIcon != null) {
5466 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5467 }
5468 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005469 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5470 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5471 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005472 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5473 }
5474
Selim Cineka8cb1262017-08-15 16:53:44 -07005475 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5476 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005477 if (remoteView != null) {
5478 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005479 int maxWidth = resources.getDimensionPixelSize(isLowRam
5480 ? R.dimen.notification_custom_view_max_image_width_low_ram
5481 : R.dimen.notification_custom_view_max_image_width);
5482 int maxHeight = resources.getDimensionPixelSize(isLowRam
5483 ? R.dimen.notification_custom_view_max_image_height_low_ram
5484 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005485 remoteView.reduceImageSizes(maxWidth, maxHeight);
5486 }
5487 }
5488
5489 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005490 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005491 */
Selim Cinek22714f12017-04-13 16:23:53 -07005492 private boolean isForegroundService() {
5493 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005494 }
5495
5496 /**
Selim Cinek99104832017-01-25 14:47:33 -08005497 * @return whether this notification has a media session attached
5498 * @hide
5499 */
5500 public boolean hasMediaSession() {
5501 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5502 }
5503
5504 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005505 * @return the style class of this notification
5506 * @hide
5507 */
5508 public Class<? extends Notification.Style> getNotificationStyle() {
5509 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5510
5511 if (!TextUtils.isEmpty(templateClass)) {
5512 return Notification.getNotificationStyleClass(templateClass);
5513 }
5514 return null;
5515 }
5516
5517 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005518 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005519 *
5520 * @hide
5521 */
5522 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005523 if (isColorizedMedia()) {
5524 return true;
5525 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005526 return extras.getBoolean(EXTRA_COLORIZED)
5527 && (hasColorizedPermission() || isForegroundService());
5528 }
5529
5530 /**
5531 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5532 * permission. The permission is checked when a notification is enqueued.
5533 */
5534 private boolean hasColorizedPermission() {
5535 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005536 }
5537
5538 /**
5539 * @return true if this notification is colorized and it is a media notification
5540 *
5541 * @hide
5542 */
5543 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005544 Class<? extends Style> style = getNotificationStyle();
5545 if (MediaStyle.class.equals(style)) {
5546 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5547 if ((colorized == null || colorized) && hasMediaSession()) {
5548 return true;
5549 }
5550 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5551 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5552 return true;
5553 }
5554 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005555 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005556 }
5557
Selim Cinek0847acd2017-04-24 19:48:29 -07005558
5559 /**
5560 * @return true if this is a media notification
5561 *
5562 * @hide
5563 */
5564 public boolean isMediaNotification() {
5565 Class<? extends Style> style = getNotificationStyle();
5566 if (MediaStyle.class.equals(style)) {
5567 return true;
5568 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5569 return true;
5570 }
5571 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005572 }
5573
Selim Cinek279fa862016-06-14 10:57:25 -07005574 private boolean hasLargeIcon() {
5575 return mLargeIcon != null || largeIcon != null;
5576 }
5577
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005578 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005579 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005580 * @hide
5581 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005582 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005583 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5584 }
5585
5586 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005587 * @return true if the notification will show a chronometer; false otherwise
5588 * @hide
5589 */
5590 public boolean showsChronometer() {
5591 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5592 }
5593
5594 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005595 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005596 */
5597 @SystemApi
5598 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5599 Class<? extends Style>[] classes = new Class[] {
5600 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5601 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5602 MessagingStyle.class };
5603 for (Class<? extends Style> innerClass : classes) {
5604 if (templateClass.equals(innerClass.getName())) {
5605 return innerClass;
5606 }
5607 }
5608 return null;
5609 }
5610
5611 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005612 * An object that can apply a rich notification style to a {@link Notification.Builder}
5613 * object.
5614 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005615 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005616 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005617
5618 /**
5619 * @hide
5620 */
5621 protected CharSequence mSummaryText = null;
5622
5623 /**
5624 * @hide
5625 */
5626 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005627
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005628 protected Builder mBuilder;
5629
Chris Wrend6297db2012-05-03 16:20:13 -04005630 /**
5631 * Overrides ContentTitle in the big form of the template.
5632 * This defaults to the value passed to setContentTitle().
5633 */
5634 protected void internalSetBigContentTitle(CharSequence title) {
5635 mBigContentTitle = title;
5636 }
5637
5638 /**
5639 * Set the first line of text after the detail section in the big form of the template.
5640 */
5641 protected void internalSetSummaryText(CharSequence cs) {
5642 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005643 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005644 }
5645
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005646 public void setBuilder(Builder builder) {
5647 if (mBuilder != builder) {
5648 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005649 if (mBuilder != null) {
5650 mBuilder.setStyle(this);
5651 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005652 }
5653 }
5654
Chris Wrend6297db2012-05-03 16:20:13 -04005655 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005656 if (mBuilder == null) {
5657 throw new IllegalArgumentException("Style requires a valid Builder object");
5658 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005659 }
Chris Wrend6297db2012-05-03 16:20:13 -04005660
5661 protected RemoteViews getStandardView(int layoutId) {
5662 checkBuilder();
5663
Christoph Studer4600f9b2014-07-22 22:44:43 +02005664 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005665 CharSequence oldBuilderContentTitle =
5666 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005667 if (mBigContentTitle != null) {
5668 mBuilder.setContentTitle(mBigContentTitle);
5669 }
5670
Chris Wrend6297db2012-05-03 16:20:13 -04005671 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5672
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005673 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005674
Chris Wrend6297db2012-05-03 16:20:13 -04005675 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5676 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005677 } else {
5678 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005679 }
5680
Chris Wrend6297db2012-05-03 16:20:13 -04005681 return contentView;
5682 }
5683
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005684 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005685 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005686 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005687 *
5688 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005689 * @hide
5690 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005691 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005692 return null;
5693 }
5694
5695 /**
5696 * Construct a Style-specific RemoteViews for the final big notification layout.
5697 * @hide
5698 */
5699 public RemoteViews makeBigContentView() {
5700 return null;
5701 }
5702
5703 /**
5704 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005705 *
5706 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005707 * @hide
5708 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005709 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005710 return null;
5711 }
5712
5713 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005714 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005715 * @hide
5716 */
5717 public void addExtras(Bundle extras) {
5718 if (mSummaryTextSet) {
5719 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5720 }
5721 if (mBigContentTitle != null) {
5722 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5723 }
Chris Wren91ad5632013-06-05 15:05:57 -04005724 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005725 }
5726
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005727 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005728 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005729 * @hide
5730 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005731 protected void restoreFromExtras(Bundle extras) {
5732 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5733 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5734 mSummaryTextSet = true;
5735 }
5736 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5737 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5738 }
5739 }
5740
5741
5742 /**
5743 * @hide
5744 */
5745 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005746 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005747 return wip;
5748 }
5749
Daniel Sandler0ec46202015-06-24 01:27:05 -04005750 /**
5751 * @hide
5752 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005753 public void purgeResources() {}
5754
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005755 /**
5756 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5757 * attached to.
5758 *
5759 * @return the fully constructed Notification.
5760 */
5761 public Notification build() {
5762 checkBuilder();
5763 return mBuilder.build();
5764 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005765
5766 /**
5767 * @hide
5768 * @return true if the style positions the progress bar on the second line; false if the
5769 * style hides the progress bar
5770 */
5771 protected boolean hasProgress() {
5772 return true;
5773 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005774
5775 /**
5776 * @hide
5777 * @return Whether we should put the summary be put into the notification header
5778 */
5779 public boolean hasSummaryInHeader() {
5780 return true;
5781 }
Selim Cinek593610c2016-02-16 18:42:57 -08005782
5783 /**
5784 * @hide
5785 * @return Whether custom content views are displayed inline in the style
5786 */
5787 public boolean displayCustomViewInline() {
5788 return false;
5789 }
Selim Cinekd0426622017-07-11 13:19:59 +02005790
5791 /**
5792 * Reduces the image sizes contained in this style.
5793 *
5794 * @hide
5795 */
5796 public void reduceImageSizes(Context context) {
5797 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005798 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005799
5800 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005801 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005802 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005803 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005804 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005805 * Notification notif = new Notification.Builder(mContext)
5806 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5807 * .setContentText(subject)
5808 * .setSmallIcon(R.drawable.new_post)
5809 * .setLargeIcon(aBitmap)
5810 * .setStyle(new Notification.BigPictureStyle()
5811 * .bigPicture(aBigBitmap))
5812 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005813 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005814 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005815 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005816 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005817 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005818 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005819 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005820 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005821
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005822 public BigPictureStyle() {
5823 }
5824
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005825 /**
5826 * @deprecated use {@code BigPictureStyle()}.
5827 */
5828 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005829 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005830 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005831 }
5832
Chris Wrend6297db2012-05-03 16:20:13 -04005833 /**
5834 * Overrides ContentTitle in the big form of the template.
5835 * This defaults to the value passed to setContentTitle().
5836 */
5837 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005838 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005839 return this;
5840 }
5841
5842 /**
5843 * Set the first line of text after the detail section in the big form of the template.
5844 */
5845 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005846 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005847 return this;
5848 }
5849
Chris Wren0bd664d2012-08-01 13:56:56 -04005850 /**
5851 * Provide the bitmap to be used as the payload for the BigPicture notification.
5852 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005853 public BigPictureStyle bigPicture(Bitmap b) {
5854 mPicture = b;
5855 return this;
5856 }
5857
Chris Wren3745a3d2012-05-22 15:11:52 -04005858 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005859 * Override the large icon when the big notification is shown.
5860 */
5861 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005862 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5863 }
5864
5865 /**
5866 * Override the large icon when the big notification is shown.
5867 */
5868 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005869 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005870 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005871 return this;
5872 }
5873
Riley Andrews0394a0c2015-11-03 23:36:52 -08005874 /** @hide */
5875 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5876
Daniel Sandler0ec46202015-06-24 01:27:05 -04005877 /**
5878 * @hide
5879 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005880 @Override
5881 public void purgeResources() {
5882 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005883 if (mPicture != null &&
5884 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005885 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005886 mPicture = mPicture.createAshmemBitmap();
5887 }
5888 if (mBigLargeIcon != null) {
5889 mBigLargeIcon.convertToAshmem();
5890 }
5891 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005892
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005893 /**
5894 * @hide
5895 */
Selim Cinekd0426622017-07-11 13:19:59 +02005896 @Override
5897 public void reduceImageSizes(Context context) {
5898 super.reduceImageSizes(context);
5899 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005900 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005901 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005902 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5903 ? R.dimen.notification_big_picture_max_height_low_ram
5904 : R.dimen.notification_big_picture_max_height);
5905 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5906 ? R.dimen.notification_big_picture_max_width_low_ram
5907 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005908 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5909 }
5910 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005911 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5912 ? R.dimen.notification_right_icon_size_low_ram
5913 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005914 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5915 }
5916 }
5917
5918 /**
5919 * @hide
5920 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005921 public RemoteViews makeBigContentView() {
5922 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005923 // This covers the following cases:
5924 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005925 // mN.mLargeIcon
5926 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005927 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005928 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005929 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005930 oldLargeIcon = mBuilder.mN.mLargeIcon;
5931 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005932 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5933 // replacement if the other one is null. Because we're restoring these legacy icons
5934 // for old listeners, this is in general non-null.
5935 largeIconLegacy = mBuilder.mN.largeIcon;
5936 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005937 }
5938
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005939 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005940 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005941 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5942 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005943 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005944 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005945 }
Selim Cinek279fa862016-06-14 10:57:25 -07005946 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005947
Christoph Studer5c510ee2014-12-15 16:32:27 +01005948 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005949 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005950 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005951 }
5952
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005953 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005954 return contentView;
5955 }
5956
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005957 /**
5958 * @hide
5959 */
5960 public void addExtras(Bundle extras) {
5961 super.addExtras(extras);
5962
5963 if (mBigLargeIconSet) {
5964 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5965 }
5966 extras.putParcelable(EXTRA_PICTURE, mPicture);
5967 }
5968
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005969 /**
5970 * @hide
5971 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005972 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005973 protected void restoreFromExtras(Bundle extras) {
5974 super.restoreFromExtras(extras);
5975
5976 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005977 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005978 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005979 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005980 mPicture = extras.getParcelable(EXTRA_PICTURE);
5981 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005982
5983 /**
5984 * @hide
5985 */
5986 @Override
5987 public boolean hasSummaryInHeader() {
5988 return false;
5989 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005990 }
5991
5992 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005993 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005994 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005995 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005996 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005997 * Notification notif = new Notification.Builder(mContext)
5998 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5999 * .setContentText(subject)
6000 * .setSmallIcon(R.drawable.new_mail)
6001 * .setLargeIcon(aBitmap)
6002 * .setStyle(new Notification.BigTextStyle()
6003 * .bigText(aVeryLongString))
6004 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006005 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006006 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006007 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006008 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006009 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006010
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006011 private CharSequence mBigText;
6012
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006013 public BigTextStyle() {
6014 }
6015
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006016 /**
6017 * @deprecated use {@code BigTextStyle()}.
6018 */
6019 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006020 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006021 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006022 }
6023
Chris Wrend6297db2012-05-03 16:20:13 -04006024 /**
6025 * Overrides ContentTitle in the big form of the template.
6026 * This defaults to the value passed to setContentTitle().
6027 */
6028 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006029 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006030 return this;
6031 }
6032
6033 /**
6034 * Set the first line of text after the detail section in the big form of the template.
6035 */
6036 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006037 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006038 return this;
6039 }
6040
Chris Wren0bd664d2012-08-01 13:56:56 -04006041 /**
6042 * Provide the longer text to be displayed in the big form of the
6043 * template in place of the content text.
6044 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006045 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006046 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006047 return this;
6048 }
6049
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006050 /**
6051 * @hide
6052 */
6053 public void addExtras(Bundle extras) {
6054 super.addExtras(extras);
6055
Christoph Studer4600f9b2014-07-22 22:44:43 +02006056 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6057 }
6058
6059 /**
6060 * @hide
6061 */
6062 @Override
6063 protected void restoreFromExtras(Bundle extras) {
6064 super.restoreFromExtras(extras);
6065
6066 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006067 }
6068
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006069 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006070 * @param increasedHeight true if this layout be created with an increased height.
6071 *
6072 * @hide
6073 */
6074 @Override
6075 public RemoteViews makeContentView(boolean increasedHeight) {
6076 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006077 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006078 mBuilder.mActions = new ArrayList<>();
6079 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006080 mBuilder.mActions = mBuilder.mOriginalActions;
6081 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006082 return remoteViews;
6083 }
6084 return super.makeContentView(increasedHeight);
6085 }
6086
6087 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006088 * @hide
6089 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006090 @Override
6091 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6092 if (increasedHeight && mBuilder.mActions.size() > 0) {
6093 return makeBigContentView();
6094 }
6095 return super.makeHeadsUpContentView(increasedHeight);
6096 }
6097
6098 /**
6099 * @hide
6100 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006101 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006102
6103 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006104 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006105 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006106
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006107 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006108
Selim Cinek75998782016-04-26 10:39:17 -07006109 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006110
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006111 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006112 if (TextUtils.isEmpty(bigTextText)) {
6113 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6114 // experience
6115 bigTextText = mBuilder.processLegacyText(text);
6116 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006117 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006118
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006119 return contentView;
6120 }
6121
Adrian Roosb1f427c2016-05-26 12:27:15 -07006122 static void applyBigTextContentView(Builder builder,
6123 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006124 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006125 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006126 contentView.setViewVisibility(R.id.big_text,
6127 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006128 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006129 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006130 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006131
6132 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006133 * Helper class for generating large-format notifications that include multiple back-and-forth
6134 * messages of varying types between any number of people.
6135 *
6136 * <br>
6137 * If the platform does not provide large-format notifications, this method has no effect. The
6138 * user will always see the normal notification view.
6139 * <br>
6140 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6141 * so:
6142 * <pre class="prettyprint">
6143 *
6144 * Notification noti = new Notification.Builder()
6145 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6146 * .setContentText(subject)
6147 * .setSmallIcon(R.drawable.new_message)
6148 * .setLargeIcon(aBitmap)
6149 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6150 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6151 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6152 * .build();
6153 * </pre>
6154 */
6155 public static class MessagingStyle extends Style {
6156
6157 /**
6158 * The maximum number of messages that will be retained in the Notification itself (the
6159 * number displayed is up to the platform).
6160 */
6161 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6162
Selim Cinekcb8b9852017-12-15 18:01:52 -08006163 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006164 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006165 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006166 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006167 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006168
6169 MessagingStyle() {
6170 }
6171
6172 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006173 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6174 * user before the posting app reposts the notification with those messages after they've
6175 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006176 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006177 *
6178 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006179 */
Alex Hillsfd590442016-10-07 09:52:44 -04006180 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006181 this(new Person().setName(userDisplayName));
6182 }
6183
6184 /**
6185 * @param user Required - The person displayed for any messages that are sent by the
6186 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6187 * who don't have a Person associated with it will be displayed as if they were sent
6188 * by this user. The user also needs to have a valid name associated with it.
6189 */
6190 public MessagingStyle(@NonNull Person user) {
6191 mUser = user;
6192 if (user == null || user.getName() == null) {
6193 throw new RuntimeException("user must be valid and have a name");
6194 }
6195 }
6196
6197 /**
6198 * @return the user to be displayed for any replies sent by the user
6199 */
6200 public Person getUser() {
6201 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006202 }
6203
6204 /**
6205 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006206 *
6207 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006208 */
6209 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006210 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006211 }
6212
6213 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006214 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6215 *
Kodlee Yin14656422017-12-22 17:00:46 -08006216 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6217 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6218 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6219 * {@code true} and passing {@code null} will make it return {@code false}. In
6220 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6221 * to set group conversation status.
6222 *
6223 * @param conversationTitle Title displayed for this conversation
6224 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006225 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006226 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006227 mConversationTitle = conversationTitle;
6228 return this;
6229 }
6230
6231 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006232 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006233 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006234 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006235 public CharSequence getConversationTitle() {
6236 return mConversationTitle;
6237 }
6238
6239 /**
6240 * Adds a message for display by this notification. Convenience call for a simple
6241 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6242 * @param text A {@link CharSequence} to be displayed as the message content
6243 * @param timestamp Time at which the message arrived
6244 * @param sender A {@link CharSequence} to be used for displaying the name of the
6245 * sender. Should be <code>null</code> for messages by the current user, in which case
6246 * the platform will insert {@link #getUserDisplayName()}.
6247 * Should be unique amongst all individuals in the conversation, and should be
6248 * consistent during re-posts of the notification.
6249 *
6250 * @see Message#Message(CharSequence, long, CharSequence)
6251 *
6252 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006253 *
6254 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006255 */
6256 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006257 return addMessage(text, timestamp,
6258 sender == null ? null : new Person().setName(sender));
6259 }
6260
6261 /**
6262 * Adds a message for display by this notification. Convenience call for a simple
6263 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6264 * @param text A {@link CharSequence} to be displayed as the message content
6265 * @param timestamp Time at which the message arrived
6266 * @param sender The {@link Person} who sent the message.
6267 * Should be <code>null</code> for messages by the current user, in which case
6268 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6269 *
6270 * @see Message#Message(CharSequence, long, CharSequence)
6271 *
6272 * @return this object for method chaining
6273 */
6274 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006275 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006276 }
6277
6278 /**
6279 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006280 *
6281 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6282 * the newest last.
6283 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006284 * @param message The {@link Message} to be displayed
6285 * @return this object for method chaining
6286 */
6287 public MessagingStyle addMessage(Message message) {
6288 mMessages.add(message);
6289 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6290 mMessages.remove(0);
6291 }
6292 return this;
6293 }
6294
6295 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006296 * Adds a {@link Message} for historic context in this notification.
6297 *
6298 * <p>Messages should be added as historic if they are not the main subject of the
6299 * notification but may give context to a conversation. The system may choose to present
6300 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6301 *
6302 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6303 * the newest last.
6304 *
6305 * @param message The historic {@link Message} to be added
6306 * @return this object for method chaining
6307 */
6308 public MessagingStyle addHistoricMessage(Message message) {
6309 mHistoricMessages.add(message);
6310 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6311 mHistoricMessages.remove(0);
6312 }
6313 return this;
6314 }
6315
6316 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006317 * Gets the list of {@code Message} objects that represent the notification
6318 */
6319 public List<Message> getMessages() {
6320 return mMessages;
6321 }
6322
6323 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006324 * Gets the list of historic {@code Message}s in the notification.
6325 */
6326 public List<Message> getHistoricMessages() {
6327 return mHistoricMessages;
6328 }
6329
6330 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006331 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006332 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006333 * @param isGroupConversation {@code true} if the conversation represents a group,
6334 * {@code false} otherwise.
6335 * @return this object for method chaining
6336 */
6337 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6338 mIsGroupConversation = isGroupConversation;
6339 return this;
6340 }
6341
6342 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006343 * Returns {@code true} if this notification represents a group conversation, otherwise
6344 * {@code false}.
6345 *
6346 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6347 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6348 * not the conversation title is set; returning {@code true} if the conversation title is
6349 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6350 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6351 * named, non-group conversations.
6352 *
6353 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006354 */
6355 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006356 // When target SDK version is < P, a non-null conversation title dictates if this is
6357 // as group conversation.
6358 if (mBuilder != null
6359 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6360 < Build.VERSION_CODES.P) {
6361 return mConversationTitle != null;
6362 }
6363
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006364 return mIsGroupConversation;
6365 }
6366
6367 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006368 * @hide
6369 */
6370 @Override
6371 public void addExtras(Bundle extras) {
6372 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006373 if (mUser != null) {
6374 // For legacy usages
6375 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6376 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006377 }
6378 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006379 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006380 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006381 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6382 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006383 }
Adrian Roos437cd562017-01-18 15:47:03 -08006384 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6385 Message.getBundleArrayForMessages(mHistoricMessages));
6386 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006387
6388 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006389 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006390 }
6391
6392 private void fixTitleAndTextExtras(Bundle extras) {
6393 Message m = findLatestIncomingMessage();
6394 CharSequence text = (m == null) ? null : m.mText;
6395 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006396 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6397 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006398 CharSequence title;
6399 if (!TextUtils.isEmpty(mConversationTitle)) {
6400 if (!TextUtils.isEmpty(sender)) {
6401 BidiFormatter bidi = BidiFormatter.getInstance();
6402 title = mBuilder.mContext.getString(
6403 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006404 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006405 } else {
6406 title = mConversationTitle;
6407 }
6408 } else {
6409 title = sender;
6410 }
6411
6412 if (title != null) {
6413 extras.putCharSequence(EXTRA_TITLE, title);
6414 }
6415 if (text != null) {
6416 extras.putCharSequence(EXTRA_TEXT, text);
6417 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006418 }
6419
6420 /**
6421 * @hide
6422 */
6423 @Override
6424 protected void restoreFromExtras(Bundle extras) {
6425 super.restoreFromExtras(extras);
6426
Selim Cinekcb8b9852017-12-15 18:01:52 -08006427 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6428 if (mUser == null) {
6429 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6430 mUser = new Person().setName(displayName);
6431 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006432 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006433 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006434 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006435 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006436 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006437 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006438 }
6439
6440 /**
6441 * @hide
6442 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006443 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006444 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006445 mBuilder.mOriginalActions = mBuilder.mActions;
6446 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006447 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006448 mBuilder.mActions = mBuilder.mOriginalActions;
6449 mBuilder.mOriginalActions = null;
6450 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006451 }
6452
6453 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006454 return findLatestIncomingMessage(mMessages);
6455 }
6456
6457 /**
6458 * @hide
6459 */
6460 @Nullable
6461 public static Message findLatestIncomingMessage(
6462 List<Message> messages) {
6463 for (int i = messages.size() - 1; i >= 0; i--) {
6464 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006465 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006466 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006467 return m;
6468 }
6469 }
Selim Cinek88188f22017-09-19 16:46:56 -07006470 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006471 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006472 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006473 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006474 return null;
6475 }
6476
6477 /**
6478 * @hide
6479 */
6480 @Override
6481 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006482 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006483 }
6484
6485 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006486 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006487 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006488 ? super.mBigContentTitle
6489 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006490 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006491 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006492 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006493 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006494 nameReplacement = conversationTitle;
6495 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006496 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006497 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006498 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006499 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006500 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006501 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006502 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006503 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006504 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006505 // also update the end margin if there is an image
6506 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006507 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006508 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6509 }
6510 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006511 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6512 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006513 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6514 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006515 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6516 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006517 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6518 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006519 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6520 isOneToOne);
6521 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6522 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006523 return contentView;
6524 }
6525
Selim Cinekf7409db2017-10-24 16:17:14 -07006526 private boolean hasOnlyWhiteSpaceSenders() {
6527 for (int i = 0; i < mMessages.size(); i++) {
6528 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006529 Person sender = m.getSenderPerson();
6530 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006531 return false;
6532 }
6533 }
6534 return true;
6535 }
6536
6537 private boolean isWhiteSpace(CharSequence sender) {
6538 if (TextUtils.isEmpty(sender)) {
6539 return true;
6540 }
6541 if (sender.toString().matches("^\\s*$")) {
6542 return true;
6543 }
6544 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6545 // For the presentation that we had.
6546 for (int i = 0; i < sender.length(); i++) {
6547 char c = sender.charAt(i);
6548 if (c != '\u200B') {
6549 return false;
6550 }
6551 }
6552 return true;
6553 }
6554
Selim Cinek88188f22017-09-19 16:46:56 -07006555 private CharSequence createConversationTitleFromMessages() {
6556 ArraySet<CharSequence> names = new ArraySet<>();
6557 for (int i = 0; i < mMessages.size(); i++) {
6558 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006559 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006560 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006561 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006562 }
6563 }
6564 SpannableStringBuilder title = new SpannableStringBuilder();
6565 int size = names.size();
6566 for (int i = 0; i < size; i++) {
6567 CharSequence name = names.valueAt(i);
6568 if (!TextUtils.isEmpty(title)) {
6569 title.append(", ");
6570 }
6571 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6572 }
6573 return title;
6574 }
6575
Adrian Roosdedd1df2016-04-26 16:38:47 -07006576 /**
6577 * @hide
6578 */
6579 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006580 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006581 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006582 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6583 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006584 }
6585
Adrian Roosc1a80b02016-04-05 14:54:55 -07006586 private static TextAppearanceSpan makeFontColorSpan(int color) {
6587 return new TextAppearanceSpan(null, 0, 0,
6588 ColorStateList.valueOf(color), null);
6589 }
6590
Alex Hillsd9b04d92016-04-11 16:38:16 -04006591 public static final class Message {
6592
6593 static final String KEY_TEXT = "text";
6594 static final String KEY_TIMESTAMP = "time";
6595 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006596 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006597 static final String KEY_DATA_MIME_TYPE = "type";
6598 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006599 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006600
6601 private final CharSequence mText;
6602 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006603 @Nullable
6604 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006605
Shane Brennan5a871862017-03-11 13:14:17 -08006606 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006607 private String mDataMimeType;
6608 private Uri mDataUri;
6609
6610 /**
6611 * Constructor
6612 * @param text A {@link CharSequence} to be displayed as the message content
6613 * @param timestamp Time at which the message arrived
6614 * @param sender A {@link CharSequence} to be used for displaying the name of the
6615 * sender. Should be <code>null</code> for messages by the current user, in which case
6616 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6617 * Should be unique amongst all individuals in the conversation, and should be
6618 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006619 *
6620 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006621 */
6622 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006623 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6624 }
6625
6626 /**
6627 * Constructor
6628 * @param text A {@link CharSequence} to be displayed as the message content
6629 * @param timestamp Time at which the message arrived
6630 * @param sender The {@link Person} who sent the message.
6631 * Should be <code>null</code> for messages by the current user, in which case
6632 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6633 * <p>
6634 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6635 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6636 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6637 * in order to differentiate between the different users.
6638 * </p>
6639 */
6640 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006641 mText = text;
6642 mTimestamp = timestamp;
6643 mSender = sender;
6644 }
6645
6646 /**
6647 * Sets a binary blob of data and an associated MIME type for a message. In the case
6648 * where the platform doesn't support the MIME type, the original text provided in the
6649 * constructor will be used.
6650 * @param dataMimeType The MIME type of the content. See
6651 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6652 * types on Android and Android Wear.
6653 * @param dataUri The uri containing the content whose type is given by the MIME type.
6654 * <p class="note">
6655 * <ol>
6656 * <li>Notification Listeners including the System UI need permission to access the
6657 * data the Uri points to. The recommended ways to do this are:</li>
6658 * <li>Store the data in your own ContentProvider, making sure that other apps have
6659 * the correct permission to access your provider. The preferred mechanism for
6660 * providing access is to use per-URI permissions which are temporary and only
6661 * grant access to the receiving application. An easy way to create a
6662 * ContentProvider like this is to use the FileProvider helper class.</li>
6663 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6664 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6665 * also store non-media types (see MediaStore.Files for more info). Files can be
6666 * inserted into the MediaStore using scanFile() after which a content:// style
6667 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6668 * Note that once added to the system MediaStore the content is accessible to any
6669 * app on the device.</li>
6670 * </ol>
6671 * @return this object for method chaining
6672 */
6673 public Message setData(String dataMimeType, Uri dataUri) {
6674 mDataMimeType = dataMimeType;
6675 mDataUri = dataUri;
6676 return this;
6677 }
6678
Alex Hillsfc737de2016-03-23 17:33:02 -04006679 /**
6680 * Get the text to be used for this message, or the fallback text if a type and content
6681 * Uri have been set
6682 */
6683 public CharSequence getText() {
6684 return mText;
6685 }
6686
6687 /**
6688 * Get the time at which this message arrived
6689 */
6690 public long getTimestamp() {
6691 return mTimestamp;
6692 }
6693
6694 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006695 * Get the extras Bundle for this message.
6696 */
6697 public Bundle getExtras() {
6698 return mExtras;
6699 }
6700
6701 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006702 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006703 *
6704 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006705 */
6706 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006707 return mSender == null ? null : mSender.getName();
6708 }
6709
6710 /**
6711 * Get the sender associated with this message.
6712 */
6713 @Nullable
6714 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006715 return mSender;
6716 }
6717
6718 /**
6719 * Get the MIME type of the data pointed to by the Uri
6720 */
6721 public String getDataMimeType() {
6722 return mDataMimeType;
6723 }
6724
6725 /**
6726 * Get the the Uri pointing to the content of the message. Can be null, in which case
6727 * {@see #getText()} is used.
6728 */
6729 public Uri getDataUri() {
6730 return mDataUri;
6731 }
6732
Alex Hillsd9b04d92016-04-11 16:38:16 -04006733 private Bundle toBundle() {
6734 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006735 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006736 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006737 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006738 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006739 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006740 // Legacy listeners need this
6741 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6742 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006743 }
6744 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006745 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006746 }
6747 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006748 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006749 }
Shane Brennan5a871862017-03-11 13:14:17 -08006750 if (mExtras != null) {
6751 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6752 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006753 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006754 }
6755
Alex Hillsd9b04d92016-04-11 16:38:16 -04006756 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6757 Bundle[] bundles = new Bundle[messages.size()];
6758 final int N = messages.size();
6759 for (int i = 0; i < N; i++) {
6760 bundles[i] = messages.get(i).toBundle();
6761 }
6762 return bundles;
6763 }
6764
Selim Cinek88188f22017-09-19 16:46:56 -07006765 /**
6766 * @return A list of messages read from the bundles.
6767 *
6768 * @hide
6769 */
6770 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6771 if (bundles == null) {
6772 return new ArrayList<>();
6773 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006774 List<Message> messages = new ArrayList<>(bundles.length);
6775 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006776 if (bundles[i] instanceof Bundle) {
6777 Message message = getMessageFromBundle((Bundle)bundles[i]);
6778 if (message != null) {
6779 messages.add(message);
6780 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006781 }
6782 }
6783 return messages;
6784 }
6785
6786 static Message getMessageFromBundle(Bundle bundle) {
6787 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006788 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006789 return null;
6790 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006791
6792 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6793 if (senderPerson == null) {
6794 // Legacy apps that use compat don't actually provide the sender objects
6795 // We need to fix the compat version to provide people / use
6796 // the native api instead
6797 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6798 if (senderName != null) {
6799 senderPerson = new Person().setName(senderName);
6800 }
6801 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006802 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006803 bundle.getLong(KEY_TIMESTAMP),
6804 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006805 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6806 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006807 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6808 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006809 }
Shane Brennan5a871862017-03-11 13:14:17 -08006810 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6811 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6812 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006813 return message;
6814 }
6815 } catch (ClassCastException e) {
6816 return null;
6817 }
6818 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006819 }
6820 }
6821
6822 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006823 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006824 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006825 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006826 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006827 * Notification notif = new Notification.Builder(mContext)
6828 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6829 * .setContentText(subject)
6830 * .setSmallIcon(R.drawable.new_mail)
6831 * .setLargeIcon(aBitmap)
6832 * .setStyle(new Notification.InboxStyle()
6833 * .addLine(str1)
6834 * .addLine(str2)
6835 * .setContentTitle(&quot;&quot;)
6836 * .setSummaryText(&quot;+3 more&quot;))
6837 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006838 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006839 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006840 * @see Notification#bigContentView
6841 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006842 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006843 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6844
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006845 public InboxStyle() {
6846 }
6847
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006848 /**
6849 * @deprecated use {@code InboxStyle()}.
6850 */
6851 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006852 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006853 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006854 }
6855
Chris Wrend6297db2012-05-03 16:20:13 -04006856 /**
6857 * Overrides ContentTitle in the big form of the template.
6858 * This defaults to the value passed to setContentTitle().
6859 */
6860 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006861 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006862 return this;
6863 }
6864
6865 /**
6866 * Set the first line of text after the detail section in the big form of the template.
6867 */
6868 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006869 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006870 return this;
6871 }
6872
Chris Wren0bd664d2012-08-01 13:56:56 -04006873 /**
6874 * Append a line to the digest section of the Inbox notification.
6875 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006876 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006877 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006878 return this;
6879 }
6880
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006881 /**
6882 * @hide
6883 */
6884 public void addExtras(Bundle extras) {
6885 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006886
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006887 CharSequence[] a = new CharSequence[mTexts.size()];
6888 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6889 }
6890
Christoph Studer4600f9b2014-07-22 22:44:43 +02006891 /**
6892 * @hide
6893 */
6894 @Override
6895 protected void restoreFromExtras(Bundle extras) {
6896 super.restoreFromExtras(extras);
6897
6898 mTexts.clear();
6899 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6900 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6901 }
6902 }
6903
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006904 /**
6905 * @hide
6906 */
6907 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006908 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006909 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006910 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6911 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006912
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006913 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006914
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006915 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006916
Chris Wrend6297db2012-05-03 16:20:13 -04006917 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 -04006918 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006919
Chris Wren4ed80d52012-05-17 09:30:03 -04006920 // Make sure all rows are gone in case we reuse a view.
6921 for (int rowId : rowIds) {
6922 contentView.setViewVisibility(rowId, View.GONE);
6923 }
6924
Daniel Sandler879c5e02012-04-17 16:46:51 -04006925 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006926 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6927 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006928 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006929 int onlyViewId = 0;
6930 int maxRows = rowIds.length;
6931 if (mBuilder.mActions.size() > 0) {
6932 maxRows--;
6933 }
6934 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006935 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006936 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006937 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006938 contentView.setTextViewText(rowIds[i],
6939 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006940 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006941 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006942 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006943 if (first) {
6944 onlyViewId = rowIds[i];
6945 } else {
6946 onlyViewId = 0;
6947 }
Selim Cinek247fa012016-02-18 09:50:48 -08006948 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006949 }
6950 i++;
6951 }
Selim Cinek07c80172016-04-21 16:40:47 -07006952 if (onlyViewId != 0) {
6953 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6954 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6955 R.dimen.notification_text_margin_top);
6956 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6957 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006958
Daniel Sandler879c5e02012-04-17 16:46:51 -04006959 return contentView;
6960 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006961
Selim Cinek247fa012016-02-18 09:50:48 -08006962 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006963 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006964 if (first) {
6965 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6966 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6967 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006968 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006969 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006970 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006971 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006972 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006973 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006974 }
Dan Sandler842dd772014-05-15 09:36:47 -04006975
6976 /**
6977 * Notification style for media playback notifications.
6978 *
6979 * In the expanded form, {@link Notification#bigContentView}, up to 5
6980 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006981 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006982 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6983 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6984 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006985 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006986 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6987 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006988 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006989 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006990 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006991 * Notifications created with MediaStyle will have their category set to
6992 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6993 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006994 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006995 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6996 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006997 * the System UI can identify this as a notification representing an active media session
6998 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6999 *
Selim Cinek99104832017-01-25 14:47:33 -08007000 * <p>
7001 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7002 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7003 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7004 * <p>
7005 *
Dan Sandler842dd772014-05-15 09:36:47 -04007006 * To use this style with your Notification, feed it to
7007 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7008 * <pre class="prettyprint">
7009 * Notification noti = new Notification.Builder()
7010 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007011 * .setContentTitle(&quot;Track title&quot;)
7012 * .setContentText(&quot;Artist - Album&quot;)
7013 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007014 * .setStyle(<b>new Notification.MediaStyle()</b>
7015 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007016 * .build();
7017 * </pre>
7018 *
7019 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007020 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007021 */
7022 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007023 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007024 static final int MAX_MEDIA_BUTTONS = 5;
7025
7026 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007027 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007028
7029 public MediaStyle() {
7030 }
7031
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007032 /**
7033 * @deprecated use {@code MediaStyle()}.
7034 */
7035 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007036 public MediaStyle(Builder builder) {
7037 setBuilder(builder);
7038 }
7039
7040 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007041 * 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 -04007042 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007043 *
7044 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007045 */
7046 public MediaStyle setShowActionsInCompactView(int...actions) {
7047 mActionsToShowInCompact = actions;
7048 return this;
7049 }
7050
7051 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007052 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7053 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007054 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007055 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007056 mToken = token;
7057 return this;
7058 }
7059
Christoph Studer4600f9b2014-07-22 22:44:43 +02007060 /**
7061 * @hide
7062 */
Dan Sandler842dd772014-05-15 09:36:47 -04007063 @Override
7064 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007065 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007066 if (wip.category == null) {
7067 wip.category = Notification.CATEGORY_TRANSPORT;
7068 }
Dan Sandler842dd772014-05-15 09:36:47 -04007069 return wip;
7070 }
7071
Christoph Studer4600f9b2014-07-22 22:44:43 +02007072 /**
7073 * @hide
7074 */
7075 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007076 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007077 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007078 }
7079
7080 /**
7081 * @hide
7082 */
7083 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007084 public RemoteViews makeBigContentView() {
7085 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007086 }
7087
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007088 /**
7089 * @hide
7090 */
7091 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007092 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007093 RemoteViews expanded = makeMediaBigContentView();
7094 return expanded != null ? expanded : makeMediaContentView();
7095 }
7096
Dan Sandler842dd772014-05-15 09:36:47 -04007097 /** @hide */
7098 @Override
7099 public void addExtras(Bundle extras) {
7100 super.addExtras(extras);
7101
7102 if (mToken != null) {
7103 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7104 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007105 if (mActionsToShowInCompact != null) {
7106 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7107 }
Dan Sandler842dd772014-05-15 09:36:47 -04007108 }
7109
Christoph Studer4600f9b2014-07-22 22:44:43 +02007110 /**
7111 * @hide
7112 */
7113 @Override
7114 protected void restoreFromExtras(Bundle extras) {
7115 super.restoreFromExtras(extras);
7116
7117 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7118 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7119 }
7120 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7121 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7122 }
7123 }
7124
Selim Cinek5bf069a2015-11-10 19:14:27 -05007125 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007126 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007127 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007128 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007129 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007130
7131 // If the action buttons should not be tinted, then just use the default
7132 // notification color. Otherwise, just use the passed-in color.
7133 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7134 ? color
7135 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7136 Notification.COLOR_DEFAULT);
7137
Sunny Goyal5b153922017-09-21 21:00:36 -07007138 button.setDrawableTint(R.id.action0, false, tintColor,
7139 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007140 if (!tombstone) {
7141 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7142 }
7143 button.setContentDescription(R.id.action0, action.title);
7144 return button;
7145 }
7146
7147 private RemoteViews makeMediaContentView() {
7148 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007149 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007150
7151 final int numActions = mBuilder.mActions.size();
7152 final int N = mActionsToShowInCompact == null
7153 ? 0
7154 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7155 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007156 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007157 for (int i = 0; i < N; i++) {
7158 if (i >= numActions) {
7159 throw new IllegalArgumentException(String.format(
7160 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7161 i, numActions - 1));
7162 }
7163
7164 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007165 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007166 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007167 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007168 }
7169 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007170 handleImage(view);
7171 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007172 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007173 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007174 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007175 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007176 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007177 return view;
7178 }
7179
Selim Cinek99104832017-01-25 14:47:33 -08007180 private int getPrimaryHighlightColor() {
7181 return mBuilder.getPrimaryHighlightColor();
7182 }
7183
Dan Sandler842dd772014-05-15 09:36:47 -04007184 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007185 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007186 // Dont add an expanded view if there is no more content to be revealed
7187 int actionsInCompact = mActionsToShowInCompact == null
7188 ? 0
7189 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007190 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007191 return null;
7192 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007193 RemoteViews big = mBuilder.applyStandardTemplate(
7194 R.layout.notification_template_material_big_media,
7195 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007196
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007197 if (actionCount > 0) {
7198 big.removeAllViews(com.android.internal.R.id.media_actions);
7199 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007200 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007201 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007202 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007203 }
7204 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007205 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007206 return big;
7207 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007208
Selim Cinek5bf069a2015-11-10 19:14:27 -05007209 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007210 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007211 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7212 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007213 }
7214 }
7215
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007216 /**
7217 * @hide
7218 */
7219 @Override
7220 protected boolean hasProgress() {
7221 return false;
7222 }
Dan Sandler842dd772014-05-15 09:36:47 -04007223 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007224
Selim Cinek593610c2016-02-16 18:42:57 -08007225 /**
7226 * Notification style for custom views that are decorated by the system
7227 *
7228 * <p>Instead of providing a notification that is completely custom, a developer can set this
7229 * style and still obtain system decorations like the notification header with the expand
7230 * affordance and actions.
7231 *
7232 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7233 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7234 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7235 * corresponding custom views to display.
7236 *
7237 * To use this style with your Notification, feed it to
7238 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7239 * <pre class="prettyprint">
7240 * Notification noti = new Notification.Builder()
7241 * .setSmallIcon(R.drawable.ic_stat_player)
7242 * .setLargeIcon(albumArtBitmap))
7243 * .setCustomContentView(contentView);
7244 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7245 * .build();
7246 * </pre>
7247 */
7248 public static class DecoratedCustomViewStyle extends Style {
7249
7250 public DecoratedCustomViewStyle() {
7251 }
7252
Selim Cinek593610c2016-02-16 18:42:57 -08007253 /**
7254 * @hide
7255 */
7256 public boolean displayCustomViewInline() {
7257 return true;
7258 }
7259
7260 /**
7261 * @hide
7262 */
7263 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007264 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007265 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7266 }
7267
7268 /**
7269 * @hide
7270 */
7271 @Override
7272 public RemoteViews makeBigContentView() {
7273 return makeDecoratedBigContentView();
7274 }
7275
7276 /**
7277 * @hide
7278 */
7279 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007280 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007281 return makeDecoratedHeadsUpContentView();
7282 }
7283
Selim Cinek593610c2016-02-16 18:42:57 -08007284 private RemoteViews makeDecoratedHeadsUpContentView() {
7285 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7286 ? mBuilder.mN.contentView
7287 : mBuilder.mN.headsUpContentView;
7288 if (mBuilder.mActions.size() == 0) {
7289 return makeStandardTemplateWithCustomContent(headsUpContentView);
7290 }
7291 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7292 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007293 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007294 return remoteViews;
7295 }
7296
Selim Cinek593610c2016-02-16 18:42:57 -08007297 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7298 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7299 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007300 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007301 return remoteViews;
7302 }
7303
Selim Cinek593610c2016-02-16 18:42:57 -08007304 private RemoteViews makeDecoratedBigContentView() {
7305 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7306 ? mBuilder.mN.contentView
7307 : mBuilder.mN.bigContentView;
7308 if (mBuilder.mActions.size() == 0) {
7309 return makeStandardTemplateWithCustomContent(bigContentView);
7310 }
7311 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7312 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007313 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007314 return remoteViews;
7315 }
Selim Cinek247fa012016-02-18 09:50:48 -08007316
7317 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7318 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007319 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007320 // Need to clone customContent before adding, because otherwise it can no longer be
7321 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007322 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007323 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7324 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007325 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007326 }
Selim Cinek247fa012016-02-18 09:50:48 -08007327 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007328 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007329 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007330 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007331 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007332 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007333 }
Selim Cinek593610c2016-02-16 18:42:57 -08007334 }
7335
Selim Cinek03eb3b72016-02-18 10:39:45 -08007336 /**
7337 * Notification style for media custom views that are decorated by the system
7338 *
7339 * <p>Instead of providing a media notification that is completely custom, a developer can set
7340 * this style and still obtain system decorations like the notification header with the expand
7341 * affordance and actions.
7342 *
7343 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7344 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7345 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7346 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007347 * <p>
7348 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7349 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7350 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007351 * To use this style with your Notification, feed it to
7352 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7353 * <pre class="prettyprint">
7354 * Notification noti = new Notification.Builder()
7355 * .setSmallIcon(R.drawable.ic_stat_player)
7356 * .setLargeIcon(albumArtBitmap))
7357 * .setCustomContentView(contentView);
7358 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7359 * .setMediaSession(mySession))
7360 * .build();
7361 * </pre>
7362 *
7363 * @see android.app.Notification.DecoratedCustomViewStyle
7364 * @see android.app.Notification.MediaStyle
7365 */
7366 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7367
7368 public DecoratedMediaCustomViewStyle() {
7369 }
7370
Selim Cinek03eb3b72016-02-18 10:39:45 -08007371 /**
7372 * @hide
7373 */
7374 public boolean displayCustomViewInline() {
7375 return true;
7376 }
7377
7378 /**
7379 * @hide
7380 */
7381 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007382 public RemoteViews makeContentView(boolean increasedHeight) {
7383 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007384 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7385 mBuilder.mN.contentView);
7386 }
7387
7388 /**
7389 * @hide
7390 */
7391 @Override
7392 public RemoteViews makeBigContentView() {
7393 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7394 ? mBuilder.mN.bigContentView
7395 : mBuilder.mN.contentView;
7396 return makeBigContentViewWithCustomContent(customRemoteView);
7397 }
7398
7399 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7400 RemoteViews remoteViews = super.makeBigContentView();
7401 if (remoteViews != null) {
7402 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7403 customRemoteView);
7404 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007405 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007406 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7407 customRemoteView);
7408 } else {
7409 return null;
7410 }
7411 }
7412
7413 /**
7414 * @hide
7415 */
7416 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007417 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007418 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7419 ? mBuilder.mN.headsUpContentView
7420 : mBuilder.mN.contentView;
7421 return makeBigContentViewWithCustomContent(customRemoteView);
7422 }
7423
7424 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7425 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007426 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007427 // Need to clone customContent before adding, because otherwise it can no longer be
7428 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007429 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007430 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007431 remoteViews.removeAllViews(id);
7432 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007433 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007434 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007435 return remoteViews;
7436 }
7437 }
7438
Selim Cineke7238dd2017-12-14 17:48:32 -08007439 /**
7440 * A Person associated with this Notification.
7441 */
7442 public static final class Person implements Parcelable {
7443 @Nullable private CharSequence mName;
7444 @Nullable private Icon mIcon;
7445 @Nullable private String mUri;
7446 @Nullable private String mKey;
7447
7448 protected Person(Parcel in) {
7449 mName = in.readCharSequence();
7450 if (in.readInt() != 0) {
7451 mIcon = Icon.CREATOR.createFromParcel(in);
7452 }
7453 mUri = in.readString();
7454 mKey = in.readString();
7455 }
7456
7457 /**
7458 * Create a new person.
7459 */
7460 public Person() {
7461 }
7462
7463 /**
7464 * Give this person a name.
7465 *
7466 * @param name the name of this person
7467 */
7468 public Person setName(@Nullable CharSequence name) {
7469 this.mName = name;
7470 return this;
7471 }
7472
7473 /**
7474 * Add an icon for this person.
7475 * <br />
7476 * This is currently only used for {@link MessagingStyle} notifications and should not be
7477 * provided otherwise, in order to save memory. The system will prefer this icon over any
7478 * images that are resolved from the URI.
7479 *
7480 * @param icon the icon of the person
7481 */
7482 public Person setIcon(@Nullable Icon icon) {
7483 this.mIcon = icon;
7484 return this;
7485 }
7486
7487 /**
7488 * Set a URI associated with this person.
7489 *
7490 * <P>
7491 * Depending on user preferences, adding a URI to a Person may allow the notification to
7492 * pass through interruption filters, if this notification is of
7493 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7494 * The addition of people may also cause this notification to appear more prominently in
7495 * the user interface.
7496 * </P>
7497 *
7498 * <P>
7499 * The person should be specified by the {@code String} representation of a
7500 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7501 * </P>
7502 *
7503 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7504 * URIs. The path part of these URIs must exist in the contacts database, in the
7505 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7506 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7507 * </P>
7508 *
7509 * @param uri a URI for the person
7510 */
7511 public Person setUri(@Nullable String uri) {
7512 mUri = uri;
7513 return this;
7514 }
7515
7516 /**
7517 * Add a key to this person in order to uniquely identify it.
7518 * This is especially useful if the name doesn't uniquely identify this person or if the
7519 * display name is a short handle of the actual name.
7520 *
7521 * <P>If no key is provided, the name serves as as the key for the purpose of
7522 * identification.</P>
7523 *
7524 * @param key the key that uniquely identifies this person
7525 */
7526 public Person setKey(@Nullable String key) {
7527 mKey = key;
7528 return this;
7529 }
7530
7531
7532 /**
7533 * @return the uri provided for this person or {@code null} if no Uri was provided
7534 */
7535 @Nullable
7536 public String getUri() {
7537 return mUri;
7538 }
7539
7540 /**
7541 * @return the name provided for this person or {@code null} if no name was provided
7542 */
7543 @Nullable
7544 public CharSequence getName() {
7545 return mName;
7546 }
7547
7548 /**
7549 * @return the icon provided for this person or {@code null} if no icon was provided
7550 */
7551 @Nullable
7552 public Icon getIcon() {
7553 return mIcon;
7554 }
7555
7556 /**
7557 * @return the key provided for this person or {@code null} if no key was provided
7558 */
7559 @Nullable
7560 public String getKey() {
7561 return mKey;
7562 }
7563
7564 /**
7565 * @return the URI associated with this person, or "name:mName" otherwise
7566 * @hide
7567 */
7568 public String resolveToLegacyUri() {
7569 if (mUri != null) {
7570 return mUri;
7571 }
7572 if (mName != null) {
7573 return "name:" + mName;
7574 }
7575 return "";
7576 }
7577
7578 @Override
7579 public int describeContents() {
7580 return 0;
7581 }
7582
7583 @Override
7584 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7585 dest.writeCharSequence(mName);
7586 if (mIcon != null) {
7587 dest.writeInt(1);
7588 mIcon.writeToParcel(dest, 0);
7589 } else {
7590 dest.writeInt(0);
7591 }
7592 dest.writeString(mUri);
7593 dest.writeString(mKey);
7594 }
7595
7596 public static final Creator<Person> CREATOR = new Creator<Person>() {
7597 @Override
7598 public Person createFromParcel(Parcel in) {
7599 return new Person(in);
7600 }
7601
7602 @Override
7603 public Person[] newArray(int size) {
7604 return new Person[size];
7605 }
7606 };
7607 }
7608
Christoph Studer4600f9b2014-07-22 22:44:43 +02007609 // When adding a new Style subclass here, don't forget to update
7610 // Builder.getNotificationStyleClass.
7611
Griff Hazen61a9e862014-05-22 16:05:19 -07007612 /**
7613 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7614 * metadata or change options on a notification builder.
7615 */
7616 public interface Extender {
7617 /**
7618 * Apply this extender to a notification builder.
7619 * @param builder the builder to be modified.
7620 * @return the build object for chaining.
7621 */
7622 public Builder extend(Builder builder);
7623 }
7624
7625 /**
7626 * Helper class to add wearable extensions to notifications.
7627 * <p class="note"> See
7628 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7629 * for Android Wear</a> for more information on how to use this class.
7630 * <p>
7631 * To create a notification with wearable extensions:
7632 * <ol>
7633 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7634 * properties.
7635 * <li>Create a {@link android.app.Notification.WearableExtender}.
7636 * <li>Set wearable-specific properties using the
7637 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7638 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7639 * notification.
7640 * <li>Post the notification to the notification system with the
7641 * {@code NotificationManager.notify(...)} methods.
7642 * </ol>
7643 *
7644 * <pre class="prettyprint">
7645 * Notification notif = new Notification.Builder(mContext)
7646 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7647 * .setContentText(subject)
7648 * .setSmallIcon(R.drawable.new_mail)
7649 * .extend(new Notification.WearableExtender()
7650 * .setContentIcon(R.drawable.new_mail))
7651 * .build();
7652 * NotificationManager notificationManger =
7653 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7654 * notificationManger.notify(0, notif);</pre>
7655 *
7656 * <p>Wearable extensions can be accessed on an existing notification by using the
7657 * {@code WearableExtender(Notification)} constructor,
7658 * and then using the {@code get} methods to access values.
7659 *
7660 * <pre class="prettyprint">
7661 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7662 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007663 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007664 */
7665 public static final class WearableExtender implements Extender {
7666 /**
7667 * Sentinel value for an action index that is unset.
7668 */
7669 public static final int UNSET_ACTION_INDEX = -1;
7670
7671 /**
7672 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7673 * default sizing.
7674 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007675 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007676 * on their content.
7677 */
7678 public static final int SIZE_DEFAULT = 0;
7679
7680 /**
7681 * Size value for use with {@link #setCustomSizePreset} to show this notification
7682 * with an extra small size.
7683 * <p>This value is only applicable for custom display notifications created using
7684 * {@link #setDisplayIntent}.
7685 */
7686 public static final int SIZE_XSMALL = 1;
7687
7688 /**
7689 * Size value for use with {@link #setCustomSizePreset} to show this notification
7690 * with a small size.
7691 * <p>This value is only applicable for custom display notifications created using
7692 * {@link #setDisplayIntent}.
7693 */
7694 public static final int SIZE_SMALL = 2;
7695
7696 /**
7697 * Size value for use with {@link #setCustomSizePreset} to show this notification
7698 * with a medium size.
7699 * <p>This value is only applicable for custom display notifications created using
7700 * {@link #setDisplayIntent}.
7701 */
7702 public static final int SIZE_MEDIUM = 3;
7703
7704 /**
7705 * Size value for use with {@link #setCustomSizePreset} to show this notification
7706 * with a large size.
7707 * <p>This value is only applicable for custom display notifications created using
7708 * {@link #setDisplayIntent}.
7709 */
7710 public static final int SIZE_LARGE = 4;
7711
Griff Hazend5f11f92014-05-27 15:40:09 -07007712 /**
7713 * Size value for use with {@link #setCustomSizePreset} to show this notification
7714 * full screen.
7715 * <p>This value is only applicable for custom display notifications created using
7716 * {@link #setDisplayIntent}.
7717 */
7718 public static final int SIZE_FULL_SCREEN = 5;
7719
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007720 /**
7721 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7722 * short amount of time when this notification is displayed on the screen. This
7723 * is the default value.
7724 */
7725 public static final int SCREEN_TIMEOUT_SHORT = 0;
7726
7727 /**
7728 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7729 * for a longer amount of time when this notification is displayed on the screen.
7730 */
7731 public static final int SCREEN_TIMEOUT_LONG = -1;
7732
Griff Hazen61a9e862014-05-22 16:05:19 -07007733 /** Notification extra which contains wearable extensions */
7734 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7735
Pete Gastaf6781d2014-10-07 15:17:05 -04007736 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007737 private static final String KEY_ACTIONS = "actions";
7738 private static final String KEY_FLAGS = "flags";
7739 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7740 private static final String KEY_PAGES = "pages";
7741 private static final String KEY_BACKGROUND = "background";
7742 private static final String KEY_CONTENT_ICON = "contentIcon";
7743 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7744 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7745 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7746 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7747 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007748 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007749 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007750 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007751
7752 // Flags bitwise-ored to mFlags
7753 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7754 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7755 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7756 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007757 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007758 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007759 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007760
7761 // Default value for flags integer
7762 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7763
7764 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7765 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7766
7767 private ArrayList<Action> mActions = new ArrayList<Action>();
7768 private int mFlags = DEFAULT_FLAGS;
7769 private PendingIntent mDisplayIntent;
7770 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7771 private Bitmap mBackground;
7772 private int mContentIcon;
7773 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7774 private int mContentActionIndex = UNSET_ACTION_INDEX;
7775 private int mCustomSizePreset = SIZE_DEFAULT;
7776 private int mCustomContentHeight;
7777 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007778 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007779 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007780 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007781
7782 /**
7783 * Create a {@link android.app.Notification.WearableExtender} with default
7784 * options.
7785 */
7786 public WearableExtender() {
7787 }
7788
7789 public WearableExtender(Notification notif) {
7790 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7791 if (wearableBundle != null) {
7792 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7793 if (actions != null) {
7794 mActions.addAll(actions);
7795 }
7796
7797 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7798 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7799
7800 Notification[] pages = getNotificationArrayFromBundle(
7801 wearableBundle, KEY_PAGES);
7802 if (pages != null) {
7803 Collections.addAll(mPages, pages);
7804 }
7805
7806 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7807 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7808 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7809 DEFAULT_CONTENT_ICON_GRAVITY);
7810 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7811 UNSET_ACTION_INDEX);
7812 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7813 SIZE_DEFAULT);
7814 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7815 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007816 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007817 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007818 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007819 }
7820 }
7821
7822 /**
7823 * Apply wearable extensions to a notification that is being built. This is typically
7824 * called by the {@link android.app.Notification.Builder#extend} method of
7825 * {@link android.app.Notification.Builder}.
7826 */
7827 @Override
7828 public Notification.Builder extend(Notification.Builder builder) {
7829 Bundle wearableBundle = new Bundle();
7830
7831 if (!mActions.isEmpty()) {
7832 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7833 }
7834 if (mFlags != DEFAULT_FLAGS) {
7835 wearableBundle.putInt(KEY_FLAGS, mFlags);
7836 }
7837 if (mDisplayIntent != null) {
7838 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7839 }
7840 if (!mPages.isEmpty()) {
7841 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7842 new Notification[mPages.size()]));
7843 }
7844 if (mBackground != null) {
7845 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7846 }
7847 if (mContentIcon != 0) {
7848 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7849 }
7850 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7851 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7852 }
7853 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7854 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7855 mContentActionIndex);
7856 }
7857 if (mCustomSizePreset != SIZE_DEFAULT) {
7858 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7859 }
7860 if (mCustomContentHeight != 0) {
7861 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7862 }
7863 if (mGravity != DEFAULT_GRAVITY) {
7864 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7865 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007866 if (mHintScreenTimeout != 0) {
7867 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7868 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007869 if (mDismissalId != null) {
7870 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7871 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007872 if (mBridgeTag != null) {
7873 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7874 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007875
7876 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7877 return builder;
7878 }
7879
7880 @Override
7881 public WearableExtender clone() {
7882 WearableExtender that = new WearableExtender();
7883 that.mActions = new ArrayList<Action>(this.mActions);
7884 that.mFlags = this.mFlags;
7885 that.mDisplayIntent = this.mDisplayIntent;
7886 that.mPages = new ArrayList<Notification>(this.mPages);
7887 that.mBackground = this.mBackground;
7888 that.mContentIcon = this.mContentIcon;
7889 that.mContentIconGravity = this.mContentIconGravity;
7890 that.mContentActionIndex = this.mContentActionIndex;
7891 that.mCustomSizePreset = this.mCustomSizePreset;
7892 that.mCustomContentHeight = this.mCustomContentHeight;
7893 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007894 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007895 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007896 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007897 return that;
7898 }
7899
7900 /**
7901 * Add a wearable action to this notification.
7902 *
7903 * <p>When wearable actions are added using this method, the set of actions that
7904 * show on a wearable device splits from devices that only show actions added
7905 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7906 * of which actions display on different devices.
7907 *
7908 * @param action the action to add to this notification
7909 * @return this object for method chaining
7910 * @see android.app.Notification.Action
7911 */
7912 public WearableExtender addAction(Action action) {
7913 mActions.add(action);
7914 return this;
7915 }
7916
7917 /**
7918 * Adds wearable actions to this notification.
7919 *
7920 * <p>When wearable actions are added using this method, the set of actions that
7921 * show on a wearable device splits from devices that only show actions added
7922 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7923 * of which actions display on different devices.
7924 *
7925 * @param actions the actions to add to this notification
7926 * @return this object for method chaining
7927 * @see android.app.Notification.Action
7928 */
7929 public WearableExtender addActions(List<Action> actions) {
7930 mActions.addAll(actions);
7931 return this;
7932 }
7933
7934 /**
7935 * Clear all wearable actions present on this builder.
7936 * @return this object for method chaining.
7937 * @see #addAction
7938 */
7939 public WearableExtender clearActions() {
7940 mActions.clear();
7941 return this;
7942 }
7943
7944 /**
7945 * Get the wearable actions present on this notification.
7946 */
7947 public List<Action> getActions() {
7948 return mActions;
7949 }
7950
7951 /**
7952 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007953 * this notification. The {@link PendingIntent} provided should be for an activity.
7954 *
7955 * <pre class="prettyprint">
7956 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7957 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7958 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7959 * Notification notif = new Notification.Builder(context)
7960 * .extend(new Notification.WearableExtender()
7961 * .setDisplayIntent(displayPendingIntent)
7962 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7963 * .build();</pre>
7964 *
7965 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007966 * should have an empty task affinity. It is also recommended to use the device
7967 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007968 *
7969 * <p>Example AndroidManifest.xml entry:
7970 * <pre class="prettyprint">
7971 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7972 * android:exported=&quot;true&quot;
7973 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007974 * android:taskAffinity=&quot;&quot;
7975 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007976 *
7977 * @param intent the {@link PendingIntent} for an activity
7978 * @return this object for method chaining
7979 * @see android.app.Notification.WearableExtender#getDisplayIntent
7980 */
7981 public WearableExtender setDisplayIntent(PendingIntent intent) {
7982 mDisplayIntent = intent;
7983 return this;
7984 }
7985
7986 /**
7987 * Get the intent to launch inside of an activity view when displaying this
7988 * notification. This {@code PendingIntent} should be for an activity.
7989 */
7990 public PendingIntent getDisplayIntent() {
7991 return mDisplayIntent;
7992 }
7993
7994 /**
7995 * Add an additional page of content to display with this notification. The current
7996 * notification forms the first page, and pages added using this function form
7997 * subsequent pages. This field can be used to separate a notification into multiple
7998 * sections.
7999 *
8000 * @param page the notification to add as another page
8001 * @return this object for method chaining
8002 * @see android.app.Notification.WearableExtender#getPages
8003 */
8004 public WearableExtender addPage(Notification page) {
8005 mPages.add(page);
8006 return this;
8007 }
8008
8009 /**
8010 * Add additional pages of content to display with this notification. The current
8011 * notification forms the first page, and pages added using this function form
8012 * subsequent pages. This field can be used to separate a notification into multiple
8013 * sections.
8014 *
8015 * @param pages a list of notifications
8016 * @return this object for method chaining
8017 * @see android.app.Notification.WearableExtender#getPages
8018 */
8019 public WearableExtender addPages(List<Notification> pages) {
8020 mPages.addAll(pages);
8021 return this;
8022 }
8023
8024 /**
8025 * Clear all additional pages present on this builder.
8026 * @return this object for method chaining.
8027 * @see #addPage
8028 */
8029 public WearableExtender clearPages() {
8030 mPages.clear();
8031 return this;
8032 }
8033
8034 /**
8035 * Get the array of additional pages of content for displaying this notification. The
8036 * current notification forms the first page, and elements within this array form
8037 * subsequent pages. This field can be used to separate a notification into multiple
8038 * sections.
8039 * @return the pages for this notification
8040 */
8041 public List<Notification> getPages() {
8042 return mPages;
8043 }
8044
8045 /**
8046 * Set a background image to be displayed behind the notification content.
8047 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8048 * will work with any notification style.
8049 *
8050 * @param background the background bitmap
8051 * @return this object for method chaining
8052 * @see android.app.Notification.WearableExtender#getBackground
8053 */
8054 public WearableExtender setBackground(Bitmap background) {
8055 mBackground = background;
8056 return this;
8057 }
8058
8059 /**
8060 * Get a background image to be displayed behind the notification content.
8061 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8062 * will work with any notification style.
8063 *
8064 * @return the background image
8065 * @see android.app.Notification.WearableExtender#setBackground
8066 */
8067 public Bitmap getBackground() {
8068 return mBackground;
8069 }
8070
8071 /**
8072 * Set an icon that goes with the content of this notification.
8073 */
8074 public WearableExtender setContentIcon(int icon) {
8075 mContentIcon = icon;
8076 return this;
8077 }
8078
8079 /**
8080 * Get an icon that goes with the content of this notification.
8081 */
8082 public int getContentIcon() {
8083 return mContentIcon;
8084 }
8085
8086 /**
8087 * Set the gravity that the content icon should have within the notification display.
8088 * Supported values include {@link android.view.Gravity#START} and
8089 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8090 * @see #setContentIcon
8091 */
8092 public WearableExtender setContentIconGravity(int contentIconGravity) {
8093 mContentIconGravity = contentIconGravity;
8094 return this;
8095 }
8096
8097 /**
8098 * Get the gravity that the content icon should have within the notification display.
8099 * Supported values include {@link android.view.Gravity#START} and
8100 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8101 * @see #getContentIcon
8102 */
8103 public int getContentIconGravity() {
8104 return mContentIconGravity;
8105 }
8106
8107 /**
8108 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008109 * this notification. This action will no longer display separately from the
8110 * notification's content.
8111 *
Griff Hazenca48d352014-05-28 22:37:13 -07008112 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008113 * set, although the list of available actions comes from the main notification and not
8114 * from the child page's notification.
8115 *
8116 * @param actionIndex The index of the action to hoist onto the current notification page.
8117 * If wearable actions were added to the main notification, this index
8118 * will apply to that list, otherwise it will apply to the regular
8119 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008120 */
8121 public WearableExtender setContentAction(int actionIndex) {
8122 mContentActionIndex = actionIndex;
8123 return this;
8124 }
8125
8126 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008127 * Get the index of the notification action, if any, that was specified as being clickable
8128 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008129 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008130 *
Griff Hazenca48d352014-05-28 22:37:13 -07008131 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008132 * set, although the list of available actions comes from the main notification and not
8133 * from the child page's notification.
8134 *
8135 * <p>If wearable specific actions were added to the main notification, this index will
8136 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008137 *
8138 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008139 */
8140 public int getContentAction() {
8141 return mContentActionIndex;
8142 }
8143
8144 /**
8145 * Set the gravity that this notification should have within the available viewport space.
8146 * Supported values include {@link android.view.Gravity#TOP},
8147 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8148 * The default value is {@link android.view.Gravity#BOTTOM}.
8149 */
8150 public WearableExtender setGravity(int gravity) {
8151 mGravity = gravity;
8152 return this;
8153 }
8154
8155 /**
8156 * Get the gravity that this notification should have within the available viewport space.
8157 * Supported values include {@link android.view.Gravity#TOP},
8158 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8159 * The default value is {@link android.view.Gravity#BOTTOM}.
8160 */
8161 public int getGravity() {
8162 return mGravity;
8163 }
8164
8165 /**
8166 * Set the custom size preset for the display of this notification out of the available
8167 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8168 * {@link #SIZE_LARGE}.
8169 * <p>Some custom size presets are only applicable for custom display notifications created
8170 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8171 * documentation for the preset in question. See also
8172 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8173 */
8174 public WearableExtender setCustomSizePreset(int sizePreset) {
8175 mCustomSizePreset = sizePreset;
8176 return this;
8177 }
8178
8179 /**
8180 * Get the custom size preset for the display of this notification out of the available
8181 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8182 * {@link #SIZE_LARGE}.
8183 * <p>Some custom size presets are only applicable for custom display notifications created
8184 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8185 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8186 */
8187 public int getCustomSizePreset() {
8188 return mCustomSizePreset;
8189 }
8190
8191 /**
8192 * Set the custom height in pixels for the display of this notification's content.
8193 * <p>This option is only available for custom display notifications created
8194 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8195 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8196 * {@link #getCustomContentHeight}.
8197 */
8198 public WearableExtender setCustomContentHeight(int height) {
8199 mCustomContentHeight = height;
8200 return this;
8201 }
8202
8203 /**
8204 * Get the custom height in pixels for the display of this notification's content.
8205 * <p>This option is only available for custom display notifications created
8206 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8207 * {@link #setCustomContentHeight}.
8208 */
8209 public int getCustomContentHeight() {
8210 return mCustomContentHeight;
8211 }
8212
8213 /**
8214 * Set whether the scrolling position for the contents of this notification should start
8215 * at the bottom of the contents instead of the top when the contents are too long to
8216 * display within the screen. Default is false (start scroll at the top).
8217 */
8218 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8219 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8220 return this;
8221 }
8222
8223 /**
8224 * Get whether the scrolling position for the contents of this notification should start
8225 * at the bottom of the contents instead of the top when the contents are too long to
8226 * display within the screen. Default is false (start scroll at the top).
8227 */
8228 public boolean getStartScrollBottom() {
8229 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8230 }
8231
8232 /**
8233 * Set whether the content intent is available when the wearable device is not connected
8234 * to a companion device. The user can still trigger this intent when the wearable device
8235 * is offline, but a visual hint will indicate that the content intent may not be available.
8236 * Defaults to true.
8237 */
8238 public WearableExtender setContentIntentAvailableOffline(
8239 boolean contentIntentAvailableOffline) {
8240 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8241 return this;
8242 }
8243
8244 /**
8245 * Get whether the content intent is available when the wearable device is not connected
8246 * to a companion device. The user can still trigger this intent when the wearable device
8247 * is offline, but a visual hint will indicate that the content intent may not be available.
8248 * Defaults to true.
8249 */
8250 public boolean getContentIntentAvailableOffline() {
8251 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8252 }
8253
8254 /**
8255 * Set a hint that this notification's icon should not be displayed.
8256 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8257 * @return this object for method chaining
8258 */
8259 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8260 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8261 return this;
8262 }
8263
8264 /**
8265 * Get a hint that this notification's icon should not be displayed.
8266 * @return {@code true} if this icon should not be displayed, false otherwise.
8267 * The default value is {@code false} if this was never set.
8268 */
8269 public boolean getHintHideIcon() {
8270 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8271 }
8272
8273 /**
8274 * Set a visual hint that only the background image of this notification should be
8275 * displayed, and other semantic content should be hidden. This hint is only applicable
8276 * to sub-pages added using {@link #addPage}.
8277 */
8278 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8279 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8280 return this;
8281 }
8282
8283 /**
8284 * Get a visual hint that only the background image of this notification should be
8285 * displayed, and other semantic content should be hidden. This hint is only applicable
8286 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8287 */
8288 public boolean getHintShowBackgroundOnly() {
8289 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8290 }
8291
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008292 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008293 * Set 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 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8297 * @return this object for method chaining
8298 */
8299 public WearableExtender setHintAvoidBackgroundClipping(
8300 boolean hintAvoidBackgroundClipping) {
8301 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8302 return this;
8303 }
8304
8305 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008306 * Get a hint that this notification's background should not be clipped if possible,
8307 * and should instead be resized to fully display on the screen, retaining the aspect
8308 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008309 * @return {@code true} if it's ok if the background is clipped on the screen, false
8310 * otherwise. The default value is {@code false} if this was never set.
8311 */
8312 public boolean getHintAvoidBackgroundClipping() {
8313 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8314 }
8315
8316 /**
8317 * Set a hint that the screen should remain on for at least this duration when
8318 * this notification is displayed on the screen.
8319 * @param timeout The requested screen timeout in milliseconds. Can also be either
8320 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8321 * @return this object for method chaining
8322 */
8323 public WearableExtender setHintScreenTimeout(int timeout) {
8324 mHintScreenTimeout = timeout;
8325 return this;
8326 }
8327
8328 /**
8329 * Get the duration, in milliseconds, that the screen should remain on for
8330 * when this notification is displayed.
8331 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8332 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8333 */
8334 public int getHintScreenTimeout() {
8335 return mHintScreenTimeout;
8336 }
8337
Alex Hills9ab3a232016-04-05 14:54:56 -04008338 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008339 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8340 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8341 * qr codes, as well as other simple black-and-white tickets.
8342 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8343 * @return this object for method chaining
8344 */
8345 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8346 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8347 return this;
8348 }
8349
8350 /**
8351 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8352 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8353 * qr codes, as well as other simple black-and-white tickets.
8354 * @return {@code true} if it should be displayed in ambient, false otherwise
8355 * otherwise. The default value is {@code false} if this was never set.
8356 */
8357 public boolean getHintAmbientBigPicture() {
8358 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8359 }
8360
8361 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008362 * Set 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 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8365 * an activity and transitions should be generated, false otherwise.
8366 * @return this object for method chaining
8367 */
8368 public WearableExtender setHintContentIntentLaunchesActivity(
8369 boolean hintContentIntentLaunchesActivity) {
8370 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8371 return this;
8372 }
8373
8374 /**
8375 * Get a hint that this notification's content intent will launch an {@link Activity}
8376 * directly, telling the platform that it can generate the appropriate transitions
8377 * @return {@code true} if the content intent will launch an activity and transitions should
8378 * be generated, false otherwise. The default value is {@code false} if this was never set.
8379 */
8380 public boolean getHintContentIntentLaunchesActivity() {
8381 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8382 }
8383
Nadia Benbernou948627e2016-04-14 14:41:08 -04008384 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008385 * Sets the dismissal id for this notification. If a notification is posted with a
8386 * dismissal id, then when that notification is canceled, notifications on other wearables
8387 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008388 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008389 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008390 * @param dismissalId the dismissal id of the notification.
8391 * @return this object for method chaining
8392 */
8393 public WearableExtender setDismissalId(String dismissalId) {
8394 mDismissalId = dismissalId;
8395 return this;
8396 }
8397
8398 /**
8399 * Returns the dismissal id of the notification.
8400 * @return the dismissal id of the notification or null if it has not been set.
8401 */
8402 public String getDismissalId() {
8403 return mDismissalId;
8404 }
8405
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008406 /**
8407 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8408 * posted from a phone to provide finer-grained control on what notifications are bridged
8409 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8410 * Features to Notifications</a> for more information.
8411 * @param bridgeTag the bridge tag of the notification.
8412 * @return this object for method chaining
8413 */
8414 public WearableExtender setBridgeTag(String bridgeTag) {
8415 mBridgeTag = bridgeTag;
8416 return this;
8417 }
8418
8419 /**
8420 * Returns the bridge tag of the notification.
8421 * @return the bridge tag or null if not present.
8422 */
8423 public String getBridgeTag() {
8424 return mBridgeTag;
8425 }
8426
Griff Hazen61a9e862014-05-22 16:05:19 -07008427 private void setFlag(int mask, boolean value) {
8428 if (value) {
8429 mFlags |= mask;
8430 } else {
8431 mFlags &= ~mask;
8432 }
8433 }
8434 }
8435
8436 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008437 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8438 * with car extensions:
8439 *
8440 * <ol>
8441 * <li>Create an {@link Notification.Builder}, setting any desired
8442 * properties.
8443 * <li>Create a {@link CarExtender}.
8444 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8445 * {@link CarExtender}.
8446 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8447 * to apply the extensions to a notification.
8448 * </ol>
8449 *
8450 * <pre class="prettyprint">
8451 * Notification notification = new Notification.Builder(context)
8452 * ...
8453 * .extend(new CarExtender()
8454 * .set*(...))
8455 * .build();
8456 * </pre>
8457 *
8458 * <p>Car extensions can be accessed on an existing notification by using the
8459 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8460 * to access values.
8461 */
8462 public static final class CarExtender implements Extender {
8463 private static final String TAG = "CarExtender";
8464
8465 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8466 private static final String EXTRA_LARGE_ICON = "large_icon";
8467 private static final String EXTRA_CONVERSATION = "car_conversation";
8468 private static final String EXTRA_COLOR = "app_color";
8469
8470 private Bitmap mLargeIcon;
8471 private UnreadConversation mUnreadConversation;
8472 private int mColor = Notification.COLOR_DEFAULT;
8473
8474 /**
8475 * Create a {@link CarExtender} with default options.
8476 */
8477 public CarExtender() {
8478 }
8479
8480 /**
8481 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8482 *
8483 * @param notif The notification from which to copy options.
8484 */
8485 public CarExtender(Notification notif) {
8486 Bundle carBundle = notif.extras == null ?
8487 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8488 if (carBundle != null) {
8489 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8490 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8491
8492 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8493 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8494 }
8495 }
8496
8497 /**
8498 * Apply car extensions to a notification that is being built. This is typically called by
8499 * the {@link Notification.Builder#extend(Notification.Extender)}
8500 * method of {@link Notification.Builder}.
8501 */
8502 @Override
8503 public Notification.Builder extend(Notification.Builder builder) {
8504 Bundle carExtensions = new Bundle();
8505
8506 if (mLargeIcon != null) {
8507 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8508 }
8509 if (mColor != Notification.COLOR_DEFAULT) {
8510 carExtensions.putInt(EXTRA_COLOR, mColor);
8511 }
8512
8513 if (mUnreadConversation != null) {
8514 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8515 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8516 }
8517
8518 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8519 return builder;
8520 }
8521
8522 /**
8523 * Sets the accent color to use when Android Auto presents the notification.
8524 *
8525 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8526 * to accent the displayed notification. However, not all colors are acceptable in an
8527 * automotive setting. This method can be used to override the color provided in the
8528 * notification in such a situation.
8529 */
Tor Norbye80756e32015-03-02 09:39:27 -08008530 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008531 mColor = color;
8532 return this;
8533 }
8534
8535 /**
8536 * Gets the accent color.
8537 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008538 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008539 */
Tor Norbye80756e32015-03-02 09:39:27 -08008540 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008541 public int getColor() {
8542 return mColor;
8543 }
8544
8545 /**
8546 * Sets the large icon of the car notification.
8547 *
8548 * If no large icon is set in the extender, Android Auto will display the icon
8549 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8550 *
8551 * @param largeIcon The large icon to use in the car notification.
8552 * @return This object for method chaining.
8553 */
8554 public CarExtender setLargeIcon(Bitmap largeIcon) {
8555 mLargeIcon = largeIcon;
8556 return this;
8557 }
8558
8559 /**
8560 * Gets the large icon used in this car notification, or null if no icon has been set.
8561 *
8562 * @return The large icon for the car notification.
8563 * @see CarExtender#setLargeIcon
8564 */
8565 public Bitmap getLargeIcon() {
8566 return mLargeIcon;
8567 }
8568
8569 /**
8570 * Sets the unread conversation in a message notification.
8571 *
8572 * @param unreadConversation The unread part of the conversation this notification conveys.
8573 * @return This object for method chaining.
8574 */
8575 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8576 mUnreadConversation = unreadConversation;
8577 return this;
8578 }
8579
8580 /**
8581 * Returns the unread conversation conveyed by this notification.
8582 * @see #setUnreadConversation(UnreadConversation)
8583 */
8584 public UnreadConversation getUnreadConversation() {
8585 return mUnreadConversation;
8586 }
8587
8588 /**
8589 * A class which holds the unread messages from a conversation.
8590 */
8591 public static class UnreadConversation {
8592 private static final String KEY_AUTHOR = "author";
8593 private static final String KEY_TEXT = "text";
8594 private static final String KEY_MESSAGES = "messages";
8595 private static final String KEY_REMOTE_INPUT = "remote_input";
8596 private static final String KEY_ON_REPLY = "on_reply";
8597 private static final String KEY_ON_READ = "on_read";
8598 private static final String KEY_PARTICIPANTS = "participants";
8599 private static final String KEY_TIMESTAMP = "timestamp";
8600
8601 private final String[] mMessages;
8602 private final RemoteInput mRemoteInput;
8603 private final PendingIntent mReplyPendingIntent;
8604 private final PendingIntent mReadPendingIntent;
8605 private final String[] mParticipants;
8606 private final long mLatestTimestamp;
8607
8608 UnreadConversation(String[] messages, RemoteInput remoteInput,
8609 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8610 String[] participants, long latestTimestamp) {
8611 mMessages = messages;
8612 mRemoteInput = remoteInput;
8613 mReadPendingIntent = readPendingIntent;
8614 mReplyPendingIntent = replyPendingIntent;
8615 mParticipants = participants;
8616 mLatestTimestamp = latestTimestamp;
8617 }
8618
8619 /**
8620 * Gets the list of messages conveyed by this notification.
8621 */
8622 public String[] getMessages() {
8623 return mMessages;
8624 }
8625
8626 /**
8627 * Gets the remote input that will be used to convey the response to a message list, or
8628 * null if no such remote input exists.
8629 */
8630 public RemoteInput getRemoteInput() {
8631 return mRemoteInput;
8632 }
8633
8634 /**
8635 * Gets the pending intent that will be triggered when the user replies to this
8636 * notification.
8637 */
8638 public PendingIntent getReplyPendingIntent() {
8639 return mReplyPendingIntent;
8640 }
8641
8642 /**
8643 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8644 * in this object's message list.
8645 */
8646 public PendingIntent getReadPendingIntent() {
8647 return mReadPendingIntent;
8648 }
8649
8650 /**
8651 * Gets the participants in the conversation.
8652 */
8653 public String[] getParticipants() {
8654 return mParticipants;
8655 }
8656
8657 /**
8658 * Gets the firs participant in the conversation.
8659 */
8660 public String getParticipant() {
8661 return mParticipants.length > 0 ? mParticipants[0] : null;
8662 }
8663
8664 /**
8665 * Gets the timestamp of the conversation.
8666 */
8667 public long getLatestTimestamp() {
8668 return mLatestTimestamp;
8669 }
8670
8671 Bundle getBundleForUnreadConversation() {
8672 Bundle b = new Bundle();
8673 String author = null;
8674 if (mParticipants != null && mParticipants.length > 1) {
8675 author = mParticipants[0];
8676 }
8677 Parcelable[] messages = new Parcelable[mMessages.length];
8678 for (int i = 0; i < messages.length; i++) {
8679 Bundle m = new Bundle();
8680 m.putString(KEY_TEXT, mMessages[i]);
8681 m.putString(KEY_AUTHOR, author);
8682 messages[i] = m;
8683 }
8684 b.putParcelableArray(KEY_MESSAGES, messages);
8685 if (mRemoteInput != null) {
8686 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8687 }
8688 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8689 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8690 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8691 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8692 return b;
8693 }
8694
8695 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8696 if (b == null) {
8697 return null;
8698 }
8699 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8700 String[] messages = null;
8701 if (parcelableMessages != null) {
8702 String[] tmp = new String[parcelableMessages.length];
8703 boolean success = true;
8704 for (int i = 0; i < tmp.length; i++) {
8705 if (!(parcelableMessages[i] instanceof Bundle)) {
8706 success = false;
8707 break;
8708 }
8709 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8710 if (tmp[i] == null) {
8711 success = false;
8712 break;
8713 }
8714 }
8715 if (success) {
8716 messages = tmp;
8717 } else {
8718 return null;
8719 }
8720 }
8721
8722 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8723 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8724
8725 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8726
8727 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8728 if (participants == null || participants.length != 1) {
8729 return null;
8730 }
8731
8732 return new UnreadConversation(messages,
8733 remoteInput,
8734 onReply,
8735 onRead,
8736 participants, b.getLong(KEY_TIMESTAMP));
8737 }
8738 };
8739
8740 /**
8741 * Builder class for {@link CarExtender.UnreadConversation} objects.
8742 */
8743 public static class Builder {
8744 private final List<String> mMessages = new ArrayList<String>();
8745 private final String mParticipant;
8746 private RemoteInput mRemoteInput;
8747 private PendingIntent mReadPendingIntent;
8748 private PendingIntent mReplyPendingIntent;
8749 private long mLatestTimestamp;
8750
8751 /**
8752 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8753 *
8754 * @param name The name of the other participant in the conversation.
8755 */
8756 public Builder(String name) {
8757 mParticipant = name;
8758 }
8759
8760 /**
8761 * Appends a new unread message to the list of messages for this conversation.
8762 *
8763 * The messages should be added from oldest to newest.
8764 *
8765 * @param message The text of the new unread message.
8766 * @return This object for method chaining.
8767 */
8768 public Builder addMessage(String message) {
8769 mMessages.add(message);
8770 return this;
8771 }
8772
8773 /**
8774 * Sets the pending intent and remote input which will convey the reply to this
8775 * notification.
8776 *
8777 * @param pendingIntent The pending intent which will be triggered on a reply.
8778 * @param remoteInput The remote input parcelable which will carry the reply.
8779 * @return This object for method chaining.
8780 *
8781 * @see CarExtender.UnreadConversation#getRemoteInput
8782 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8783 */
8784 public Builder setReplyAction(
8785 PendingIntent pendingIntent, RemoteInput remoteInput) {
8786 mRemoteInput = remoteInput;
8787 mReplyPendingIntent = pendingIntent;
8788
8789 return this;
8790 }
8791
8792 /**
8793 * Sets the pending intent that will be sent once the messages in this notification
8794 * are read.
8795 *
8796 * @param pendingIntent The pending intent to use.
8797 * @return This object for method chaining.
8798 */
8799 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8800 mReadPendingIntent = pendingIntent;
8801 return this;
8802 }
8803
8804 /**
8805 * Sets the timestamp of the most recent message in an unread conversation.
8806 *
8807 * If a messaging notification has been posted by your application and has not
8808 * yet been cancelled, posting a later notification with the same id and tag
8809 * but without a newer timestamp may result in Android Auto not displaying a
8810 * heads up notification for the later notification.
8811 *
8812 * @param timestamp The timestamp of the most recent message in the conversation.
8813 * @return This object for method chaining.
8814 */
8815 public Builder setLatestTimestamp(long timestamp) {
8816 mLatestTimestamp = timestamp;
8817 return this;
8818 }
8819
8820 /**
8821 * Builds a new unread conversation object.
8822 *
8823 * @return The new unread conversation object.
8824 */
8825 public UnreadConversation build() {
8826 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8827 String[] participants = { mParticipant };
8828 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8829 mReadPendingIntent, participants, mLatestTimestamp);
8830 }
8831 }
8832 }
8833
8834 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008835 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8836 * with a TV extension:
8837 *
8838 * <ol>
8839 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8840 * <li>Create a {@link TvExtender}.
8841 * <li>Set TV-specific properties using the {@code set} methods of
8842 * {@link TvExtender}.
8843 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8844 * to apply the extension to a notification.
8845 * </ol>
8846 *
8847 * <pre class="prettyprint">
8848 * Notification notification = new Notification.Builder(context)
8849 * ...
8850 * .extend(new TvExtender()
8851 * .set*(...))
8852 * .build();
8853 * </pre>
8854 *
8855 * <p>TV extensions can be accessed on an existing notification by using the
8856 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8857 * to access values.
8858 *
8859 * @hide
8860 */
8861 @SystemApi
8862 public static final class TvExtender implements Extender {
8863 private static final String TAG = "TvExtender";
8864
8865 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8866 private static final String EXTRA_FLAGS = "flags";
8867 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8868 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008869 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08008870 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008871
8872 // Flags bitwise-ored to mFlags
8873 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8874
8875 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008876 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008877 private PendingIntent mContentIntent;
8878 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08008879 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008880
8881 /**
8882 * Create a {@link TvExtender} with default options.
8883 */
8884 public TvExtender() {
8885 mFlags = FLAG_AVAILABLE_ON_TV;
8886 }
8887
8888 /**
8889 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8890 *
8891 * @param notif The notification from which to copy options.
8892 */
8893 public TvExtender(Notification notif) {
8894 Bundle bundle = notif.extras == null ?
8895 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8896 if (bundle != null) {
8897 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008898 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008899 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008900 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8901 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8902 }
8903 }
8904
8905 /**
8906 * Apply a TV extension to a notification that is being built. This is typically called by
8907 * the {@link Notification.Builder#extend(Notification.Extender)}
8908 * method of {@link Notification.Builder}.
8909 */
8910 @Override
8911 public Notification.Builder extend(Notification.Builder builder) {
8912 Bundle bundle = new Bundle();
8913
8914 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008915 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008916 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008917 if (mContentIntent != null) {
8918 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8919 }
8920
8921 if (mDeleteIntent != null) {
8922 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8923 }
8924
8925 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8926 return builder;
8927 }
8928
8929 /**
8930 * Returns true if this notification should be shown on TV. This method return true
8931 * if the notification was extended with a TvExtender.
8932 */
8933 public boolean isAvailableOnTv() {
8934 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8935 }
8936
8937 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008938 * Specifies the channel the notification should be delivered on when shown on TV.
8939 * It can be different from the channel that the notification is delivered to when
8940 * posting on a non-TV device.
8941 */
8942 public TvExtender setChannel(String channelId) {
8943 mChannelId = channelId;
8944 return this;
8945 }
8946
8947 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008948 * Specifies the channel the notification should be delivered on when shown on TV.
8949 * It can be different from the channel that the notification is delivered to when
8950 * posting on a non-TV device.
8951 */
8952 public TvExtender setChannelId(String channelId) {
8953 mChannelId = channelId;
8954 return this;
8955 }
8956
Jeff Sharkey000ce802017-04-29 13:13:27 -06008957 /** @removed */
8958 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008959 public String getChannel() {
8960 return mChannelId;
8961 }
8962
8963 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008964 * Returns the id of the channel this notification posts to on TV.
8965 */
8966 public String getChannelId() {
8967 return mChannelId;
8968 }
8969
8970 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008971 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8972 * If provided, it is used instead of the content intent specified
8973 * at the level of Notification.
8974 */
8975 public TvExtender setContentIntent(PendingIntent intent) {
8976 mContentIntent = intent;
8977 return this;
8978 }
8979
8980 /**
8981 * Returns the TV-specific content intent. If this method returns null, the
8982 * main content intent on the notification should be used.
8983 *
8984 * @see {@link Notification#contentIntent}
8985 */
8986 public PendingIntent getContentIntent() {
8987 return mContentIntent;
8988 }
8989
8990 /**
8991 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8992 * by the user on TV. If provided, it is used instead of the delete intent specified
8993 * at the level of Notification.
8994 */
8995 public TvExtender setDeleteIntent(PendingIntent intent) {
8996 mDeleteIntent = intent;
8997 return this;
8998 }
8999
9000 /**
9001 * Returns the TV-specific delete intent. If this method returns null, the
9002 * main delete intent on the notification should be used.
9003 *
9004 * @see {@link Notification#deleteIntent}
9005 */
9006 public PendingIntent getDeleteIntent() {
9007 return mDeleteIntent;
9008 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009009
9010 /**
9011 * Specifies whether this notification should suppress showing a message over top of apps
9012 * outside of the launcher.
9013 */
9014 public TvExtender setSuppressShowOverApps(boolean suppress) {
9015 mSuppressShowOverApps = suppress;
9016 return this;
9017 }
9018
9019 /**
9020 * Returns true if this notification should not show messages over top of apps
9021 * outside of the launcher.
9022 */
9023 public boolean getSuppressShowOverApps() {
9024 return mSuppressShowOverApps;
9025 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009026 }
9027
9028 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009029 * Get an array of Notification objects from a parcelable array bundle field.
9030 * Update the bundle to have a typed array so fetches in the future don't need
9031 * to do an array copy.
9032 */
9033 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9034 Parcelable[] array = bundle.getParcelableArray(key);
9035 if (array instanceof Notification[] || array == null) {
9036 return (Notification[]) array;
9037 }
9038 Notification[] typedArray = Arrays.copyOf(array, array.length,
9039 Notification[].class);
9040 bundle.putParcelableArray(key, typedArray);
9041 return typedArray;
9042 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009043
9044 private static class BuilderRemoteViews extends RemoteViews {
9045 public BuilderRemoteViews(Parcel parcel) {
9046 super(parcel);
9047 }
9048
Kenny Guy77320062014-08-27 21:37:15 +01009049 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9050 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009051 }
9052
9053 @Override
9054 public BuilderRemoteViews clone() {
9055 Parcel p = Parcel.obtain();
9056 writeToParcel(p, 0);
9057 p.setDataPosition(0);
9058 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9059 p.recycle();
9060 return brv;
9061 }
9062 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009063
9064 private static class StandardTemplateParams {
9065 boolean hasProgress = true;
9066 boolean ambient = false;
9067 CharSequence title;
9068 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009069 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009070 boolean hideLargeIcon;
9071 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009072
9073 final StandardTemplateParams reset() {
9074 hasProgress = true;
9075 ambient = false;
9076 title = null;
9077 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009078 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009079 return this;
9080 }
9081
9082 final StandardTemplateParams hasProgress(boolean hasProgress) {
9083 this.hasProgress = hasProgress;
9084 return this;
9085 }
9086
9087 final StandardTemplateParams title(CharSequence title) {
9088 this.title = title;
9089 return this;
9090 }
9091
9092 final StandardTemplateParams text(CharSequence text) {
9093 this.text = text;
9094 return this;
9095 }
9096
Selim Cinekafeed292017-12-12 17:32:44 -08009097 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9098 this.headerTextSecondary = text;
9099 return this;
9100 }
9101
Selim Cinek88188f22017-09-19 16:46:56 -07009102 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9103 this.alwaysShowReply = alwaysShowReply;
9104 return this;
9105 }
9106
9107 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9108 this.hideLargeIcon = hideLargeIcon;
9109 return this;
9110 }
9111
Adrian Roos70d7aa32017-01-11 15:39:06 -08009112 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009113 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009114 this.ambient = ambient;
9115 return this;
9116 }
9117
9118 final StandardTemplateParams fillTextsFrom(Builder b) {
9119 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009120 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9121
9122 // Big text notifications should contain their content when viewed in ambient mode.
9123 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9124 if (!ambient || TextUtils.isEmpty(text)) {
9125 text = extras.getCharSequence(EXTRA_TEXT);
9126 }
9127 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009128 return this;
9129 }
9130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009131}