blob: 3e1895dc836fc10820ad3a426ff86795f2862560 [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 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001828 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001829 public WearableExtender setInProgressLabel(CharSequence label) {
1830 mInProgressLabel = label;
1831 return this;
1832 }
1833
1834 /**
1835 * Get the label to display while the wearable is preparing to automatically execute
1836 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1837 *
1838 * @return the label to display while the action is being prepared to execute
1839 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001840 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001841 public CharSequence getInProgressLabel() {
1842 return mInProgressLabel;
1843 }
1844
1845 /**
1846 * Set a label to display to confirm that the action should be executed.
1847 * This is usually an imperative verb like "Send".
1848 *
1849 * @param label the label to confirm the action should be executed
1850 * @return this object for method chaining
1851 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001852 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001853 public WearableExtender setConfirmLabel(CharSequence label) {
1854 mConfirmLabel = label;
1855 return this;
1856 }
1857
1858 /**
1859 * Get the label to display to confirm that the action should be executed.
1860 * This is usually an imperative verb like "Send".
1861 *
1862 * @return the label to confirm the action should be executed
1863 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001864 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001865 public CharSequence getConfirmLabel() {
1866 return mConfirmLabel;
1867 }
1868
1869 /**
1870 * Set a label to display to cancel the action.
1871 * This is usually an imperative verb, like "Cancel".
1872 *
1873 * @param label the label to display to cancel the action
1874 * @return this object for method chaining
1875 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001876 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001877 public WearableExtender setCancelLabel(CharSequence label) {
1878 mCancelLabel = label;
1879 return this;
1880 }
1881
1882 /**
1883 * Get the label to display to cancel the action.
1884 * This is usually an imperative verb like "Cancel".
1885 *
1886 * @return the label to display to cancel the action
1887 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05001888 @Deprecated
Pete Gastaf6781d2014-10-07 15:17:05 -04001889 public CharSequence getCancelLabel() {
1890 return mCancelLabel;
1891 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001892
1893 /**
1894 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1895 * platform that it can generate the appropriate transitions.
1896 * @param hintLaunchesActivity {@code true} if the content intent will launch
1897 * an activity and transitions should be generated, false otherwise.
1898 * @return this object for method chaining
1899 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001900 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001901 boolean hintLaunchesActivity) {
1902 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1903 return this;
1904 }
1905
1906 /**
1907 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1908 * platform that it can generate the appropriate transitions
1909 * @return {@code true} if the content intent will launch an activity and transitions
1910 * should be generated, false otherwise. The default value is {@code false} if this was
1911 * never set.
1912 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001913 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001914 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1915 }
Alex Hills9f087612016-06-07 09:08:59 -04001916
1917 /**
1918 * Set a hint that this Action should be displayed inline.
1919 *
1920 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1921 * otherwise
1922 * @return this object for method chaining
1923 */
1924 public WearableExtender setHintDisplayActionInline(
1925 boolean hintDisplayInline) {
1926 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1927 return this;
1928 }
1929
1930 /**
1931 * Get a hint that this Action should be displayed inline.
1932 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001933 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001934 * otherwise. The default value is {@code false} if this was never set.
1935 */
1936 public boolean getHintDisplayActionInline() {
1937 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1938 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001939 }
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001940
1941 /**
1942 * Provides meaning to an {@link Action} that hints at what the associated
1943 * {@link PendingIntent} will do. For example, an {@link Action} with a
1944 * {@link PendingIntent} that replies to a text message notification may have the
1945 * {@link #SEMANTIC_ACTION_REPLY} {@code SemanticAction} set within it.
1946 *
1947 * @hide
1948 */
1949 @IntDef(prefix = { "SEMANTIC_ACTION_" }, value = {
1950 SEMANTIC_ACTION_NONE,
1951 SEMANTIC_ACTION_REPLY,
1952 SEMANTIC_ACTION_MARK_AS_READ,
1953 SEMANTIC_ACTION_MARK_AS_UNREAD,
1954 SEMANTIC_ACTION_DELETE,
1955 SEMANTIC_ACTION_ARCHIVE,
1956 SEMANTIC_ACTION_MUTE,
1957 SEMANTIC_ACTION_UNMUTE,
1958 SEMANTIC_ACTION_THUMBS_UP,
Kodlee Yincda5b092018-02-15 15:34:53 -08001959 SEMANTIC_ACTION_THUMBS_DOWN,
1960 SEMANTIC_ACTION_CALL
Kodlee Yinc72c44d2017-12-21 22:07:15 +00001961 })
1962 @Retention(RetentionPolicy.SOURCE)
1963 public @interface SemanticAction {}
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001964 }
1965
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001966 /**
1967 * Array of all {@link Action} structures attached to this notification by
1968 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1969 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1970 * interface for invoking actions.
1971 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001972 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001973
1974 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001975 * Replacement version of this notification whose content will be shown
1976 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1977 * and {@link #VISIBILITY_PUBLIC}.
1978 */
1979 public Notification publicVersion;
1980
1981 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001982 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001983 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 */
1985 public Notification()
1986 {
1987 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001988 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001989 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991
1992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 * @hide
1994 */
1995 public Notification(Context context, int icon, CharSequence tickerText, long when,
1996 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1997 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001998 new Builder(context)
1999 .setWhen(when)
2000 .setSmallIcon(icon)
2001 .setTicker(tickerText)
2002 .setContentTitle(contentTitle)
2003 .setContentText(contentText)
2004 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
2005 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
2007
2008 /**
2009 * Constructs a Notification object with the information needed to
2010 * have a status bar icon without the standard expanded view.
2011 *
2012 * @param icon The resource id of the icon to put in the status bar.
2013 * @param tickerText The text that flows by in the status bar when the notification first
2014 * activates.
2015 * @param when The time to show in the time field. In the System.currentTimeMillis
2016 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08002017 *
2018 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002020 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 public Notification(int icon, CharSequence tickerText, long when)
2022 {
2023 this.icon = icon;
2024 this.tickerText = tickerText;
2025 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002026 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028
2029 /**
2030 * Unflatten the notification from a parcel.
2031 */
Svet Ganovddb94882016-06-23 19:55:24 -07002032 @SuppressWarnings("unchecked")
2033 public Notification(Parcel parcel) {
2034 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
2035 // intents in extras are always written as the last entry.
2036 readFromParcelImpl(parcel);
2037 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002038 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07002039 }
2040
2041 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 {
2043 int version = parcel.readInt();
2044
Adrian Roosfb921842017-10-26 14:49:56 +02002045 mWhitelistToken = parcel.readStrongBinder();
2046 if (mWhitelistToken == null) {
2047 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07002048 }
2049 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02002050 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07002051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002053 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04002054 if (parcel.readInt() != 0) {
2055 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04002056 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
2057 icon = mSmallIcon.getResId();
2058 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 number = parcel.readInt();
2061 if (parcel.readInt() != 0) {
2062 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2063 }
2064 if (parcel.readInt() != 0) {
2065 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2066 }
2067 if (parcel.readInt() != 0) {
2068 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2069 }
2070 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002071 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002072 }
2073 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
2075 }
Joe Onorato561d3852010-11-20 18:09:34 -08002076 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002077 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08002078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 defaults = parcel.readInt();
2080 flags = parcel.readInt();
2081 if (parcel.readInt() != 0) {
2082 sound = Uri.CREATOR.createFromParcel(parcel);
2083 }
2084
2085 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04002086 if (parcel.readInt() != 0) {
2087 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
2088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 vibrate = parcel.createLongArray();
2090 ledARGB = parcel.readInt();
2091 ledOnMS = parcel.readInt();
2092 ledOffMS = parcel.readInt();
2093 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002094
2095 if (parcel.readInt() != 0) {
2096 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
2097 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002098
2099 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002100
John Spurlockfd7f1e02014-03-18 16:41:57 -04002101 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002102
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002103 mGroupKey = parcel.readString();
2104
2105 mSortKey = parcel.readString();
2106
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06002107 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Robin Leead7e72a2017-12-04 15:45:46 +01002108 fixDuplicateExtras();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002109
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002110 actions = parcel.createTypedArray(Action.CREATOR); // may be null
2111
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002112 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002113 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2114 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002115
Chris Wren8fd39ec2014-02-27 17:43:26 -05002116 if (parcel.readInt() != 0) {
2117 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
2118 }
2119
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002120 visibility = parcel.readInt();
2121
2122 if (parcel.readInt() != 0) {
2123 publicVersion = Notification.CREATOR.createFromParcel(parcel);
2124 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002125
2126 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002127
2128 if (parcel.readInt() != 0) {
2129 mChannelId = parcel.readString();
2130 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002131 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05002132
2133 if (parcel.readInt() != 0) {
2134 mShortcutId = parcel.readString();
2135 }
2136
2137 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04002138
2139 if (parcel.readInt() != 0) {
2140 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
2141 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002142
2143 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145
Andy Stadler110988c2010-12-03 14:29:16 -08002146 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07002147 public Notification clone() {
2148 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04002149 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002150 return that;
2151 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002152
Daniel Sandler1a497d32013-04-18 14:52:45 -04002153 /**
2154 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
2155 * of this into that.
2156 * @hide
2157 */
2158 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02002159 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07002160 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002161 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002162 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07002163 that.number = this.number;
2164
2165 // PendingIntents are global, so there's no reason (or way) to clone them.
2166 that.contentIntent = this.contentIntent;
2167 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002168 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002169
2170 if (this.tickerText != null) {
2171 that.tickerText = this.tickerText.toString();
2172 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002173 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002174 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002175 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002176 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002177 that.contentView = this.contentView.clone();
2178 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002179 if (heavy && this.mLargeIcon != null) {
2180 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002181 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002182 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002183 that.sound = this.sound; // android.net.Uri is immutable
2184 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002185 if (this.audioAttributes != null) {
2186 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2187 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002188
2189 final long[] vibrate = this.vibrate;
2190 if (vibrate != null) {
2191 final int N = vibrate.length;
2192 final long[] vib = that.vibrate = new long[N];
2193 System.arraycopy(vibrate, 0, vib, 0, N);
2194 }
2195
2196 that.ledARGB = this.ledARGB;
2197 that.ledOnMS = this.ledOnMS;
2198 that.ledOffMS = this.ledOffMS;
2199 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002200
Joe Onorato18e69df2010-05-17 22:26:12 -07002201 that.flags = this.flags;
2202
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002203 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002204
John Spurlockfd7f1e02014-03-18 16:41:57 -04002205 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002206
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002207 that.mGroupKey = this.mGroupKey;
2208
2209 that.mSortKey = this.mSortKey;
2210
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002211 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002212 try {
2213 that.extras = new Bundle(this.extras);
2214 // will unparcel
2215 that.extras.size();
2216 } catch (BadParcelableException e) {
2217 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2218 that.extras = null;
2219 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002220 }
2221
Felipe Lemedd85da62016-06-28 11:29:54 -07002222 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2223 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002224 }
2225
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002226 if (this.actions != null) {
2227 that.actions = new Action[this.actions.length];
2228 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002229 if ( this.actions[i] != null) {
2230 that.actions[i] = this.actions[i].clone();
2231 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002232 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002233 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002234
Daniel Sandler1a497d32013-04-18 14:52:45 -04002235 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002236 that.bigContentView = this.bigContentView.clone();
2237 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002238
Chris Wren8fd39ec2014-02-27 17:43:26 -05002239 if (heavy && this.headsUpContentView != null) {
2240 that.headsUpContentView = this.headsUpContentView.clone();
2241 }
2242
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002243 that.visibility = this.visibility;
2244
2245 if (this.publicVersion != null) {
2246 that.publicVersion = new Notification();
2247 this.publicVersion.cloneInto(that.publicVersion, heavy);
2248 }
2249
Dan Sandler26e81cf2014-05-06 10:01:27 -04002250 that.color = this.color;
2251
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002252 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002253 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002254 that.mShortcutId = this.mShortcutId;
2255 that.mBadgeIcon = this.mBadgeIcon;
2256 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002257 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002258
Daniel Sandler1a497d32013-04-18 14:52:45 -04002259 if (!heavy) {
2260 that.lightenPayload(); // will clean out extras
2261 }
2262 }
2263
2264 /**
2265 * Removes heavyweight parts of the Notification object for archival or for sending to
2266 * listeners when the full contents are not necessary.
2267 * @hide
2268 */
2269 public final void lightenPayload() {
2270 tickerView = null;
2271 contentView = null;
2272 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002273 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002274 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002275 if (extras != null && !extras.isEmpty()) {
2276 final Set<String> keyset = extras.keySet();
2277 final int N = keyset.size();
2278 final String[] keys = keyset.toArray(new String[N]);
2279 for (int i=0; i<N; i++) {
2280 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002281 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2282 continue;
2283 }
Dan Sandler50128532015-12-08 15:42:41 -05002284 final Object obj = extras.get(key);
2285 if (obj != null &&
2286 ( obj instanceof Parcelable
2287 || obj instanceof Parcelable[]
2288 || obj instanceof SparseArray
2289 || obj instanceof ArrayList)) {
2290 extras.remove(key);
2291 }
2292 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002293 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002294 }
2295
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002296 /**
2297 * Make sure this CharSequence is safe to put into a bundle, which basically
2298 * means it had better not be some custom Parcelable implementation.
2299 * @hide
2300 */
2301 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002302 if (cs == null) return cs;
2303 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2304 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2305 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002306 if (cs instanceof Parcelable) {
2307 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2308 + " instance is a custom Parcelable and not allowed in Notification");
2309 return cs.toString();
2310 }
Selim Cinek60a54252016-02-26 17:03:25 -08002311 return removeTextSizeSpans(cs);
2312 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002313
Selim Cinek60a54252016-02-26 17:03:25 -08002314 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2315 if (charSequence instanceof Spanned) {
2316 Spanned ss = (Spanned) charSequence;
2317 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2318 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2319 for (Object span : spans) {
2320 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002321 if (resultSpan instanceof CharacterStyle) {
2322 resultSpan = ((CharacterStyle) span).getUnderlying();
2323 }
2324 if (resultSpan instanceof TextAppearanceSpan) {
2325 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002326 resultSpan = new TextAppearanceSpan(
2327 originalSpan.getFamily(),
2328 originalSpan.getTextStyle(),
2329 -1,
2330 originalSpan.getTextColor(),
2331 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002332 } else if (resultSpan instanceof RelativeSizeSpan
2333 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002334 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002335 } else {
2336 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002337 }
2338 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2339 ss.getSpanFlags(span));
2340 }
2341 return builder;
2342 }
2343 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002344 }
2345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 public int describeContents() {
2347 return 0;
2348 }
2349
2350 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002351 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 */
Svet Ganovddb94882016-06-23 19:55:24 -07002353 public void writeToParcel(Parcel parcel, int flags) {
2354 // We need to mark all pending intents getting into the notification
2355 // system as being put there to later allow the notification ranker
2356 // to launch them and by doing so add the app to the battery saver white
2357 // list for a short period of time. The problem is that the system
2358 // cannot look into the extras as there may be parcelables there that
2359 // the platform does not know how to handle. To go around that we have
2360 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002361 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002362 if (collectPendingIntents) {
2363 PendingIntent.setOnMarshaledListener(
2364 (PendingIntent intent, Parcel out, int outFlags) -> {
2365 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002366 if (allPendingIntents == null) {
2367 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002368 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002369 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002370 }
2371 });
2372 }
2373 try {
2374 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002375 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002376 writeToParcelImpl(parcel, flags);
2377 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002378 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002379 } finally {
2380 if (collectPendingIntents) {
2381 PendingIntent.setOnMarshaledListener(null);
2382 }
2383 }
2384 }
2385
2386 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 parcel.writeInt(1);
2388
Adrian Roosfb921842017-10-26 14:49:56 +02002389 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002391 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002392 if (mSmallIcon == null && icon != 0) {
2393 // you snuck an icon in here without using the builder; let's try to keep it
2394 mSmallIcon = Icon.createWithResource("", icon);
2395 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002396 if (mSmallIcon != null) {
2397 parcel.writeInt(1);
2398 mSmallIcon.writeToParcel(parcel, 0);
2399 } else {
2400 parcel.writeInt(0);
2401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 parcel.writeInt(number);
2403 if (contentIntent != null) {
2404 parcel.writeInt(1);
2405 contentIntent.writeToParcel(parcel, 0);
2406 } else {
2407 parcel.writeInt(0);
2408 }
2409 if (deleteIntent != null) {
2410 parcel.writeInt(1);
2411 deleteIntent.writeToParcel(parcel, 0);
2412 } else {
2413 parcel.writeInt(0);
2414 }
2415 if (tickerText != null) {
2416 parcel.writeInt(1);
2417 TextUtils.writeToParcel(tickerText, parcel, flags);
2418 } else {
2419 parcel.writeInt(0);
2420 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002421 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002422 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002423 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002424 } else {
2425 parcel.writeInt(0);
2426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 if (contentView != null) {
2428 parcel.writeInt(1);
2429 contentView.writeToParcel(parcel, 0);
2430 } else {
2431 parcel.writeInt(0);
2432 }
Selim Cinek279fa862016-06-14 10:57:25 -07002433 if (mLargeIcon == null && largeIcon != null) {
2434 // you snuck an icon in here without using the builder; let's try to keep it
2435 mLargeIcon = Icon.createWithBitmap(largeIcon);
2436 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002437 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002438 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002439 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002440 } else {
2441 parcel.writeInt(0);
2442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443
2444 parcel.writeInt(defaults);
2445 parcel.writeInt(this.flags);
2446
2447 if (sound != null) {
2448 parcel.writeInt(1);
2449 sound.writeToParcel(parcel, 0);
2450 } else {
2451 parcel.writeInt(0);
2452 }
2453 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002454
2455 if (audioAttributes != null) {
2456 parcel.writeInt(1);
2457 audioAttributes.writeToParcel(parcel, 0);
2458 } else {
2459 parcel.writeInt(0);
2460 }
2461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 parcel.writeLongArray(vibrate);
2463 parcel.writeInt(ledARGB);
2464 parcel.writeInt(ledOnMS);
2465 parcel.writeInt(ledOffMS);
2466 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002467
2468 if (fullScreenIntent != null) {
2469 parcel.writeInt(1);
2470 fullScreenIntent.writeToParcel(parcel, 0);
2471 } else {
2472 parcel.writeInt(0);
2473 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474
2475 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002476
John Spurlockfd7f1e02014-03-18 16:41:57 -04002477 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002478
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002479 parcel.writeString(mGroupKey);
2480
2481 parcel.writeString(mSortKey);
2482
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002483 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002484
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002485 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002486
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002487 if (bigContentView != null) {
2488 parcel.writeInt(1);
2489 bigContentView.writeToParcel(parcel, 0);
2490 } else {
2491 parcel.writeInt(0);
2492 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002493
Chris Wren8fd39ec2014-02-27 17:43:26 -05002494 if (headsUpContentView != null) {
2495 parcel.writeInt(1);
2496 headsUpContentView.writeToParcel(parcel, 0);
2497 } else {
2498 parcel.writeInt(0);
2499 }
2500
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002501 parcel.writeInt(visibility);
2502
2503 if (publicVersion != null) {
2504 parcel.writeInt(1);
2505 publicVersion.writeToParcel(parcel, 0);
2506 } else {
2507 parcel.writeInt(0);
2508 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002509
2510 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002511
2512 if (mChannelId != null) {
2513 parcel.writeInt(1);
2514 parcel.writeString(mChannelId);
2515 } else {
2516 parcel.writeInt(0);
2517 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002518 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002519
2520 if (mShortcutId != null) {
2521 parcel.writeInt(1);
2522 parcel.writeString(mShortcutId);
2523 } else {
2524 parcel.writeInt(0);
2525 }
2526
2527 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002528
2529 if (mSettingsText != null) {
2530 parcel.writeInt(1);
2531 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2532 } else {
2533 parcel.writeInt(0);
2534 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002535
2536 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538
2539 /**
2540 * Parcelable.Creator that instantiates Notification objects
2541 */
2542 public static final Parcelable.Creator<Notification> CREATOR
2543 = new Parcelable.Creator<Notification>()
2544 {
2545 public Notification createFromParcel(Parcel parcel)
2546 {
2547 return new Notification(parcel);
2548 }
2549
2550 public Notification[] newArray(int size)
2551 {
2552 return new Notification[size];
2553 }
2554 };
2555
2556 /**
Robin Leead7e72a2017-12-04 15:45:46 +01002557 * Parcelling creates multiple copies of objects in {@code extras}. Fix them.
2558 * <p>
2559 * For backwards compatibility {@code extras} holds some references to "real" member data such
2560 * as {@link getLargeIcon()} which is mirrored by {@link #EXTRA_LARGE_ICON}. This is mostly
2561 * fine as long as the object stays in one process.
2562 * <p>
2563 * However, once the notification goes into a parcel each reference gets marshalled separately,
2564 * wasting memory. Especially with large images on Auto and TV, this is worth fixing.
2565 */
2566 private void fixDuplicateExtras() {
2567 if (extras != null) {
2568 fixDuplicateExtra(mSmallIcon, EXTRA_SMALL_ICON);
2569 fixDuplicateExtra(mLargeIcon, EXTRA_LARGE_ICON);
2570 }
2571 }
2572
2573 /**
2574 * If we find an extra that's exactly the same as one of the "real" fields but refers to a
2575 * separate object, replace it with the field's version to avoid holding duplicate copies.
2576 */
2577 private void fixDuplicateExtra(@Nullable Parcelable original, @NonNull String extraName) {
2578 if (original != null && extras.getParcelable(extraName) != null) {
2579 extras.putParcelable(extraName, original);
2580 }
2581 }
2582
2583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2585 * layout.
2586 *
2587 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2588 * in the view.</p>
2589 * @param context The context for your application / activity.
2590 * @param contentTitle The title that goes in the expanded entry.
2591 * @param contentText The text that goes in the expanded entry.
2592 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2593 * If this is an activity, it must include the
2594 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002595 * that you take care of task management as described in the
2596 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2597 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002598 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002599 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002600 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002602 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 public void setLatestEventInfo(Context context,
2604 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002605 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2606 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2607 new Throwable());
2608 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002609
Selim Cinek4ac6f602016-06-13 15:47:03 -07002610 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2611 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2612 }
2613
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002614 // ensure that any information already set directly is preserved
2615 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002616
2617 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002619 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
2621 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002622 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002624 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002625
2626 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 }
2628
Julia Reynoldsda303542015-11-23 14:00:20 -05002629 /**
2630 * @hide
2631 */
2632 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002633 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002634 }
2635
2636 /**
2637 * @hide
2638 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002639 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002640 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002641 }
2642
Yi Jin6b514142017-10-30 14:54:12 -07002643 /**
2644 * @hide
2645 */
2646 public void writeToProto(ProtoOutputStream proto, long fieldId) {
2647 long token = proto.start(fieldId);
2648 proto.write(NotificationProto.CHANNEL_ID, getChannelId());
2649 proto.write(NotificationProto.HAS_TICKER_TEXT, this.tickerText != null);
2650 proto.write(NotificationProto.FLAGS, this.flags);
2651 proto.write(NotificationProto.COLOR, this.color);
2652 proto.write(NotificationProto.CATEGORY, this.category);
2653 proto.write(NotificationProto.GROUP_KEY, this.mGroupKey);
2654 proto.write(NotificationProto.SORT_KEY, this.mSortKey);
2655 if (this.actions != null) {
2656 proto.write(NotificationProto.ACTION_LENGTH, this.actions.length);
2657 }
2658 if (this.visibility >= VISIBILITY_SECRET && this.visibility <= VISIBILITY_PUBLIC) {
2659 proto.write(NotificationProto.VISIBILITY, this.visibility);
2660 }
2661 if (publicVersion != null) {
2662 publicVersion.writeToProto(proto, NotificationProto.PUBLIC_VERSION);
2663 }
2664 proto.end(token);
2665 }
2666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 @Override
2668 public String toString() {
2669 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002670 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002671 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002672 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002673 sb.append(priority);
2674 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002675 if (contentView != null) {
2676 sb.append(contentView.getPackage());
2677 sb.append("/0x");
2678 sb.append(Integer.toHexString(contentView.getLayoutId()));
2679 } else {
2680 sb.append("null");
2681 }
2682 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002683 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2684 sb.append("default");
2685 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 int N = this.vibrate.length-1;
2687 sb.append("[");
2688 for (int i=0; i<N; i++) {
2689 sb.append(this.vibrate[i]);
2690 sb.append(',');
2691 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002692 if (N != -1) {
2693 sb.append(this.vibrate[N]);
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 } else {
2697 sb.append("null");
2698 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002700 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002702 } else if (this.sound != null) {
2703 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 } else {
2705 sb.append("null");
2706 }
Chris Wren365b6d32015-07-16 10:39:26 -04002707 if (this.tickerText != null) {
2708 sb.append(" tick");
2709 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002710 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002712 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002713 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002714 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002715 if (this.category != null) {
2716 sb.append(" category=");
2717 sb.append(this.category);
2718 }
2719 if (this.mGroupKey != null) {
2720 sb.append(" groupKey=");
2721 sb.append(this.mGroupKey);
2722 }
2723 if (this.mSortKey != null) {
2724 sb.append(" sortKey=");
2725 sb.append(this.mSortKey);
2726 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002727 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002728 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002729 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002730 }
2731 sb.append(" vis=");
2732 sb.append(visibilityToString(this.visibility));
2733 if (this.publicVersion != null) {
2734 sb.append(" publicVersion=");
2735 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002736 }
2737 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 return sb.toString();
2739 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002740
Dan Sandler1b718782014-07-18 12:43:45 -04002741 /**
2742 * {@hide}
2743 */
2744 public static String visibilityToString(int vis) {
2745 switch (vis) {
2746 case VISIBILITY_PRIVATE:
2747 return "PRIVATE";
2748 case VISIBILITY_PUBLIC:
2749 return "PUBLIC";
2750 case VISIBILITY_SECRET:
2751 return "SECRET";
2752 default:
2753 return "UNKNOWN(" + String.valueOf(vis) + ")";
2754 }
2755 }
2756
Joe Onoratocb109a02011-01-18 17:57:41 -08002757 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002758 * {@hide}
2759 */
2760 public static String priorityToString(@Priority int pri) {
2761 switch (pri) {
2762 case PRIORITY_MIN:
2763 return "MIN";
2764 case PRIORITY_LOW:
2765 return "LOW";
2766 case PRIORITY_DEFAULT:
2767 return "DEFAULT";
2768 case PRIORITY_HIGH:
2769 return "HIGH";
2770 case PRIORITY_MAX:
2771 return "MAX";
2772 default:
2773 return "UNKNOWN(" + String.valueOf(pri) + ")";
2774 }
2775 }
2776
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002777 /**
2778 * @hide
2779 */
2780 public boolean hasCompletedProgress() {
2781 // not a progress notification; can't be complete
2782 if (!extras.containsKey(EXTRA_PROGRESS)
2783 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2784 return false;
2785 }
2786 // many apps use max 0 for 'indeterminate'; not complete
2787 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2788 return false;
2789 }
2790 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2791 }
2792
Jeff Sharkey000ce802017-04-29 13:13:27 -06002793 /** @removed */
2794 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002795 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002796 return mChannelId;
2797 }
2798
2799 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002800 * Returns the id of the channel this notification posts to.
2801 */
2802 public String getChannelId() {
2803 return mChannelId;
2804 }
2805
Jeff Sharkey000ce802017-04-29 13:13:27 -06002806 /** @removed */
2807 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002808 public long getTimeout() {
2809 return mTimeout;
2810 }
2811
2812 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002813 * Returns the duration from posting after which this notification should be canceled by the
2814 * system, if it's not canceled already.
2815 */
2816 public long getTimeoutAfter() {
2817 return mTimeout;
2818 }
2819
2820 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002821 * Returns what icon should be shown for this notification if it is being displayed in a
2822 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2823 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2824 */
2825 public int getBadgeIconType() {
2826 return mBadgeIcon;
2827 }
2828
2829 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002830 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002831 *
2832 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2833 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002834 */
2835 public String getShortcutId() {
2836 return mShortcutId;
2837 }
2838
Julia Reynolds3aedded2017-03-31 14:42:09 -04002839
2840 /**
2841 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2842 */
2843 public CharSequence getSettingsText() {
2844 return mSettingsText;
2845 }
2846
Julia Reynolds13d898c2017-02-02 12:22:05 -05002847 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002848 * Returns which type of notifications in a group are responsible for audibly alerting the
2849 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2850 * {@link #GROUP_ALERT_SUMMARY}.
2851 */
2852 public @GroupAlertBehavior int getGroupAlertBehavior() {
2853 return mGroupAlertBehavior;
2854 }
2855
2856 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002857 * The small icon representing this notification in the status bar and content view.
2858 *
2859 * @return the small icon representing this notification.
2860 *
2861 * @see Builder#getSmallIcon()
2862 * @see Builder#setSmallIcon(Icon)
2863 */
2864 public Icon getSmallIcon() {
2865 return mSmallIcon;
2866 }
2867
2868 /**
2869 * Used when notifying to clean up legacy small icons.
2870 * @hide
2871 */
2872 public void setSmallIcon(Icon icon) {
2873 mSmallIcon = icon;
2874 }
2875
2876 /**
2877 * The large icon shown in this notification's content view.
2878 * @see Builder#getLargeIcon()
2879 * @see Builder#setLargeIcon(Icon)
2880 */
2881 public Icon getLargeIcon() {
2882 return mLargeIcon;
2883 }
2884
2885 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002886 * @hide
2887 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002888 public boolean isGroupSummary() {
2889 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2890 }
2891
2892 /**
2893 * @hide
2894 */
2895 public boolean isGroupChild() {
2896 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2897 }
2898
2899 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002900 * @hide
2901 */
2902 public boolean suppressAlertingDueToGrouping() {
2903 if (isGroupSummary()
2904 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2905 return true;
2906 } else if (isGroupChild()
2907 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2908 return true;
2909 }
2910 return false;
2911 }
2912
2913 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002914 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002915 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002916 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002917 * content views using the platform's notification layout template. If your app supports
2918 * versions of Android as old as API level 4, you can instead use
2919 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2920 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2921 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002922 *
Scott Main183bf112012-08-13 19:12:13 -07002923 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002924 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002925 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002926 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002927 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2928 * .setContentText(subject)
2929 * .setSmallIcon(R.drawable.new_mail)
2930 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002931 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002932 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002933 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002934 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002935 /**
2936 * @hide
2937 */
2938 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2939 "android.rebuild.contentViewActionCount";
2940 /**
2941 * @hide
2942 */
2943 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2944 = "android.rebuild.bigViewActionCount";
2945 /**
2946 * @hide
2947 */
2948 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2949 = "android.rebuild.hudViewActionCount";
2950
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002951 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002952
Selim Cinek6743c0b2017-01-18 18:24:01 -08002953 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2954 SystemProperties.getBoolean("notifications.only_title", true);
2955
Selim Cinek389edcd2017-05-11 19:16:44 -07002956 /**
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 light.
2959 */
2960 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2961
2962 /**
2963 * The lightness difference that has to be added to the primary text color to obtain the
2964 * secondary text color when the background is dark.
2965 * A bit less then the above value, since it looks better on dark backgrounds.
2966 */
2967 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2968
Joe Onorato46439ce2010-11-19 13:56:21 -08002969 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002970 private Notification mN;
2971 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002972 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002973 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Selim Cineke7238dd2017-12-14 17:48:32 -08002974 private ArrayList<Person> mPersonList = new ArrayList<>();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002975 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002976 private boolean mIsLegacy;
2977 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002978
2979 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002980 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2981 */
2982 private int mCachedContrastColor = COLOR_INVALID;
2983 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002984 /**
2985 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2986 */
2987 private int mCachedAmbientColor = COLOR_INVALID;
2988 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002989
2990 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002991 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2992 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2993 */
2994 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002995 private int mTextColorsAreForBackground = COLOR_INVALID;
2996 private int mPrimaryTextColor = COLOR_INVALID;
2997 private int mSecondaryTextColor = COLOR_INVALID;
2998 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002999 private int mBackgroundColor = COLOR_INVALID;
3000 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07003001 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07003002 /**
3003 * A temporary location where actions are stored. If != null the view originally has action
3004 * but doesn't have any for this inflation.
3005 */
3006 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07003007 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003008
Anthony Chenad4d1582017-04-10 16:07:58 -07003009 private boolean mTintActionButtons;
3010 private boolean mInNightMode;
3011
Adrian Roos70d7aa32017-01-11 15:39:06 -08003012 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003013 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08003014 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003015 * @param context
3016 * A {@link Context} that will be used by the Builder to construct the
3017 * RemoteViews. The Context will not be held past the lifetime of this Builder
3018 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003019 * @param channelId
3020 * The constructed Notification will be posted on this
3021 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
3022 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003023 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003024 public Builder(Context context, String channelId) {
3025 this(context, (Notification) null);
3026 mN.mChannelId = channelId;
3027 }
3028
3029 /**
3030 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
3031 * instead. All posted Notifications must specify a NotificationChannel Id.
3032 */
3033 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003034 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05003035 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003036 }
3037
Joe Onoratocb109a02011-01-18 17:57:41 -08003038 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003039 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02003040 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003041 public Builder(Context context, Notification toAdopt) {
3042 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07003043 Resources res = mContext.getResources();
3044 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
3045
3046 if (res.getBoolean(R.bool.config_enableNightMode)) {
3047 Configuration currentConfig = res.getConfiguration();
3048 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
3049 == Configuration.UI_MODE_NIGHT_YES;
3050 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003051
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003052 if (toAdopt == null) {
3053 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003054 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3055 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
3056 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003057 mN.priority = PRIORITY_DEFAULT;
3058 mN.visibility = VISIBILITY_PRIVATE;
3059 } else {
3060 mN = toAdopt;
3061 if (mN.actions != null) {
3062 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003063 }
3064
Selim Cineke7238dd2017-12-14 17:48:32 -08003065 if (mN.extras.containsKey(EXTRA_PEOPLE_LIST)) {
3066 ArrayList<Person> people = mN.extras.getParcelableArrayList(EXTRA_PEOPLE_LIST);
3067 mPersonList.addAll(people);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003068 }
3069
Selim Cinek4ac6f602016-06-13 15:47:03 -07003070 if (mN.getSmallIcon() == null && mN.icon != 0) {
3071 setSmallIcon(mN.icon);
3072 }
3073
3074 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
3075 setLargeIcon(mN.largeIcon);
3076 }
3077
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
3079 if (!TextUtils.isEmpty(templateClass)) {
3080 final Class<? extends Style> styleClass
3081 = getNotificationStyleClass(templateClass);
3082 if (styleClass == null) {
3083 Log.d(TAG, "Unknown style class: " + templateClass);
3084 } else {
3085 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003086 final Constructor<? extends Style> ctor =
3087 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003088 ctor.setAccessible(true);
3089 final Style style = ctor.newInstance();
3090 style.restoreFromExtras(mN.extras);
3091
3092 if (style != null) {
3093 setStyle(style);
3094 }
3095 } catch (Throwable t) {
3096 Log.e(TAG, "Could not create Style", t);
3097 }
3098 }
3099 }
3100
3101 }
3102 }
3103
3104 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08003105 if (mColorUtil == null) {
3106 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003107 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003108 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003109 }
3110
3111 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003112 * If this notification is duplicative of a Launcher shortcut, sets the
3113 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
3114 * the shortcut.
3115 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04003116 * This field will be ignored by Launchers that don't support badging, don't show
3117 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05003118 *
3119 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
3120 * supersedes
3121 */
3122 public Builder setShortcutId(String shortcutId) {
3123 mN.mShortcutId = shortcutId;
3124 return this;
3125 }
3126
3127 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04003128 * Sets which icon to display as a badge for this notification.
3129 *
3130 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
3131 * {@link #BADGE_ICON_LARGE}.
3132 *
3133 * Note: This value might be ignored, for launchers that don't support badge icons.
3134 */
Julia Reynolds612beb22017-03-30 10:48:30 -04003135 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04003136 mN.mBadgeIcon = icon;
3137 return this;
3138 }
3139
3140 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003141 * Sets the group alert behavior for this notification. Use this method to mute this
3142 * notification if alerts for this notification's group should be handled by a different
3143 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04003144 * {@link #setGroup(String) group}. This must be called on all notifications you want to
3145 * mute. For example, if you want only the summary of your group to make noise, all
3146 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04003147 *
3148 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
3149 */
3150 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
3151 mN.mGroupAlertBehavior = groupAlertBehavior;
3152 return this;
3153 }
3154
Jeff Sharkey000ce802017-04-29 13:13:27 -06003155 /** @removed */
3156 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04003157 public Builder setChannel(String channelId) {
3158 mN.mChannelId = channelId;
3159 return this;
3160 }
3161
3162 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003163 * Specifies the channel the notification should be delivered on.
3164 */
3165 public Builder setChannelId(String channelId) {
3166 mN.mChannelId = channelId;
3167 return this;
3168 }
3169
Jeff Sharkey000ce802017-04-29 13:13:27 -06003170 /** @removed */
3171 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05003172 public Builder setTimeout(long durationMs) {
3173 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05003174 return this;
3175 }
3176
3177 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04003178 * Specifies a duration in milliseconds after which this notification should be canceled,
3179 * if it is not already canceled.
3180 */
3181 public Builder setTimeoutAfter(long durationMs) {
3182 mN.mTimeout = durationMs;
3183 return this;
3184 }
3185
3186 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003187 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003188 *
3189 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
3190 * shown anymore by default and must be opted into by using
3191 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003192 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003193 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08003194 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003195 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003196 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08003197 return this;
3198 }
3199
Joe Onoratocb109a02011-01-18 17:57:41 -08003200 /**
Griff Hazen50c11652014-05-16 09:46:31 -07003201 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07003202 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07003203 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
3204 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07003205 */
3206 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003207 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07003208 return this;
3209 }
3210
3211 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003212 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08003213 *
3214 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003215 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003216 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003217 * Useful when showing an elapsed time (like an ongoing phone call).
3218 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003219 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003220 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003221 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003222 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003223 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003224 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003225 */
3226 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003227 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003228 return this;
3229 }
3230
3231 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003232 * Sets the Chronometer to count down instead of counting up.
3233 *
3234 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3235 * If it isn't set the chronometer will count up.
3236 *
3237 * @see #setUsesChronometer(boolean)
3238 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003239 public Builder setChronometerCountDown(boolean countDown) {
3240 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003241 return this;
3242 }
3243
3244 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003245 * Set the small icon resource, which will be used to represent the notification in the
3246 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003247 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003248
3249 * The platform template for the expanded view will draw this icon in the left, unless a
3250 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3251 * icon will be moved to the right-hand side.
3252 *
3253
3254 * @param icon
3255 * A resource ID in the application's package of the drawable to use.
3256 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003257 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003258 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003259 return setSmallIcon(icon != 0
3260 ? Icon.createWithResource(mContext, icon)
3261 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003262 }
3263
Joe Onoratocb109a02011-01-18 17:57:41 -08003264 /**
3265 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3266 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3267 * LevelListDrawable}.
3268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003269 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003270 * @param level The level to use for the icon.
3271 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003272 * @see Notification#icon
3273 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003274 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003275 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003276 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003277 return setSmallIcon(icon);
3278 }
3279
3280 /**
3281 * Set the small icon, which will be used to represent the notification in the
3282 * status bar and content view (unless overriden there by a
3283 * {@link #setLargeIcon(Bitmap) large icon}).
3284 *
3285 * @param icon An Icon object to use.
3286 * @see Notification#icon
3287 */
3288 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003289 mN.setSmallIcon(icon);
3290 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3291 mN.icon = icon.getResId();
3292 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003293 return this;
3294 }
3295
Joe Onoratocb109a02011-01-18 17:57:41 -08003296 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003297 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003299 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003300 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003301 return this;
3302 }
3303
Joe Onoratocb109a02011-01-18 17:57:41 -08003304 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003305 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003306 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003307 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003308 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003309 return this;
3310 }
3311
Joe Onoratocb109a02011-01-18 17:57:41 -08003312 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003313 * This provides some additional information that is displayed in the notification. No
3314 * guarantees are given where exactly it is displayed.
3315 *
3316 * <p>This information should only be provided if it provides an essential
3317 * benefit to the understanding of the notification. The more text you provide the
3318 * less readable it becomes. For example, an email client should only provide the account
3319 * name here if more than one email account has been added.</p>
3320 *
3321 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3322 * notification header area.
3323 *
3324 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3325 * this will be shown in the third line of text in the platform notification template.
3326 * You should not be using {@link #setProgress(int, int, boolean)} at the
3327 * same time on those versions; they occupy the same place.
3328 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003329 */
3330 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003331 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003332 return this;
3333 }
3334
3335 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003336 * Provides text that will appear as a link to your application's settings.
3337 *
3338 * <p>This text does not appear within notification {@link Style templates} but may
3339 * appear when the user uses an affordance to learn more about the notification.
3340 * Additionally, this text will not appear unless you provide a valid link target by
3341 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3342 *
3343 * <p>This text is meant to be concise description about what the user can customize
3344 * when they click on this link. The recommended maximum length is 40 characters.
3345 * @param text
3346 * @return
3347 */
3348 public Builder setSettingsText(CharSequence text) {
3349 mN.mSettingsText = safeCharSequence(text);
3350 return this;
3351 }
3352
3353 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003354 * Set the remote input history.
3355 *
3356 * This should be set to the most recent inputs that have been sent
3357 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3358 * longer relevant (e.g. for chat notifications once the other party has responded).
3359 *
3360 * The most recent input must be stored at the 0 index, the second most recent at the
3361 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3362 * and how much of each individual input is shown.
3363 *
3364 * <p>Note: The reply text will only be shown on notifications that have least one action
3365 * with a {@code RemoteInput}.</p>
3366 */
3367 public Builder setRemoteInputHistory(CharSequence[] text) {
3368 if (text == null) {
3369 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3370 } else {
3371 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3372 CharSequence[] safe = new CharSequence[N];
3373 for (int i = 0; i < N; i++) {
3374 safe[i] = safeCharSequence(text[i]);
3375 }
3376 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3377 }
3378 return this;
3379 }
3380
3381 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003382 * Sets the number of items this notification represents. May be displayed as a badge count
3383 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003384 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003385 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003386 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003387 return this;
3388 }
3389
Joe Onoratocb109a02011-01-18 17:57:41 -08003390 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003391 * A small piece of additional information pertaining to this notification.
3392 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003393 * The platform template will draw this on the last line of the notification, at the far
3394 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003395 *
3396 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3397 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3398 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003399 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003400 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003401 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003402 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003403 return this;
3404 }
3405
Joe Onoratocb109a02011-01-18 17:57:41 -08003406 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003407 * Set the progress this notification represents.
3408 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003409 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003410 */
3411 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003412 mN.extras.putInt(EXTRA_PROGRESS, progress);
3413 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3414 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003415 return this;
3416 }
3417
3418 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003419 * Supply a custom RemoteViews to use instead of the platform template.
3420 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003421 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003422 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003423 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003424 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003425 return setCustomContentView(views);
3426 }
3427
3428 /**
3429 * Supply custom RemoteViews to use instead of the platform template.
3430 *
3431 * This will override the layout that would otherwise be constructed by this Builder
3432 * object.
3433 */
3434 public Builder setCustomContentView(RemoteViews contentView) {
3435 mN.contentView = contentView;
3436 return this;
3437 }
3438
3439 /**
3440 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3441 *
3442 * This will override the expanded layout that would otherwise be constructed by this
3443 * Builder object.
3444 */
3445 public Builder setCustomBigContentView(RemoteViews contentView) {
3446 mN.bigContentView = contentView;
3447 return this;
3448 }
3449
3450 /**
3451 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3452 *
3453 * This will override the heads-up layout that would otherwise be constructed by this
3454 * Builder object.
3455 */
3456 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3457 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003458 return this;
3459 }
3460
Joe Onoratocb109a02011-01-18 17:57:41 -08003461 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003462 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3463 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003464 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3465 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3466 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003467 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003468 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003469 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003470 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003471 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003472 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003473 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003474 return this;
3475 }
3476
Joe Onoratocb109a02011-01-18 17:57:41 -08003477 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003478 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3479 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003480 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003481 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003482 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003483 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003484 return this;
3485 }
3486
Joe Onoratocb109a02011-01-18 17:57:41 -08003487 /**
3488 * An intent to launch instead of posting the notification to the status bar.
3489 * Only for use with extremely high-priority notifications demanding the user's
3490 * <strong>immediate</strong> attention, such as an incoming phone call or
3491 * alarm clock that the user has explicitly set to a particular time.
3492 * If this facility is used for something else, please give the user an option
3493 * to turn it off and use a normal notification, as this can be extremely
3494 * disruptive.
3495 *
Chris Wren47c20a12014-06-18 17:27:29 -04003496 * <p>
3497 * The system UI may choose to display a heads-up notification, instead of
3498 * launching this intent, while the user is using the device.
3499 * </p>
3500 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003501 * @param intent The pending intent to launch.
3502 * @param highPriority Passing true will cause this notification to be sent
3503 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003504 *
3505 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003506 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003507 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003508 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003509 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3510 return this;
3511 }
3512
Joe Onoratocb109a02011-01-18 17:57:41 -08003513 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003514 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003515 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003516 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003517 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003518 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003519 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003520 return this;
3521 }
3522
Joe Onoratocb109a02011-01-18 17:57:41 -08003523 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003524 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003525 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003526 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003527 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003528 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003529 setTicker(tickerText);
3530 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003531 return this;
3532 }
3533
Joe Onoratocb109a02011-01-18 17:57:41 -08003534 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003535 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003536 *
3537 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003538 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3539 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003540 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003541 public Builder setLargeIcon(Bitmap b) {
3542 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3543 }
3544
3545 /**
3546 * Add a large icon to the notification content view.
3547 *
3548 * In the platform template, this image will be shown on the left of the notification view
3549 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3550 * badge atop the large icon).
3551 */
3552 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003553 mN.mLargeIcon = icon;
3554 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003555 return this;
3556 }
3557
Joe Onoratocb109a02011-01-18 17:57:41 -08003558 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003559 * Set the sound to play.
3560 *
John Spurlockc0650f022014-07-19 13:22:39 -04003561 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3562 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003563 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003564 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003565 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003566 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003567 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003568 mN.sound = sound;
3569 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003570 return this;
3571 }
3572
Joe Onoratocb109a02011-01-18 17:57:41 -08003573 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003574 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003575 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003576 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3577 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003578 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003579 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003580 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003581 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003582 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003583 mN.sound = sound;
3584 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003585 return this;
3586 }
3587
Joe Onoratocb109a02011-01-18 17:57:41 -08003588 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003589 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3590 * use during playback.
3591 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003592 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003593 * @see Notification#sound
3594 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003595 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003596 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003597 mN.sound = sound;
3598 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003599 return this;
3600 }
3601
3602 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003603 * Set the vibration pattern to use.
3604 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003605 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3606 * <code>pattern</code> parameter.
3607 *
Chris Wren47c20a12014-06-18 17:27:29 -04003608 * <p>
3609 * A notification that vibrates is more likely to be presented as a heads-up notification.
3610 * </p>
3611 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003612 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003613 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003614 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003615 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003616 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003617 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003618 return this;
3619 }
3620
Joe Onoratocb109a02011-01-18 17:57:41 -08003621 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003622 * Set the desired color for the indicator LED on the device, as well as the
3623 * blink duty cycle (specified in milliseconds).
3624 *
3625
3626 * Not all devices will honor all (or even any) of these values.
3627 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003628 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003629 * @see Notification#ledARGB
3630 * @see Notification#ledOnMS
3631 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003632 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003633 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003634 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 mN.ledARGB = argb;
3636 mN.ledOnMS = onMs;
3637 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003638 if (onMs != 0 || offMs != 0) {
3639 mN.flags |= FLAG_SHOW_LIGHTS;
3640 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003641 return this;
3642 }
3643
Joe Onoratocb109a02011-01-18 17:57:41 -08003644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003645 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003646 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003647
3648 * Ongoing notifications cannot be dismissed by the user, so your application or service
3649 * must take care of canceling them.
3650 *
3651
3652 * They are typically used to indicate a background task that the user is actively engaged
3653 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3654 * (e.g., a file download, sync operation, active network connection).
3655 *
3656
3657 * @see Notification#FLAG_ONGOING_EVENT
3658 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003659 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003660 public Builder setOngoing(boolean ongoing) {
3661 setFlag(FLAG_ONGOING_EVENT, ongoing);
3662 return this;
3663 }
3664
Joe Onoratocb109a02011-01-18 17:57:41 -08003665 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003666 * Set whether this notification should be colorized. When set, the color set with
3667 * {@link #setColor(int)} will be used as the background color of this notification.
3668 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003669 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3670 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003671 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003672 * For most styles, the coloring will only be applied if the notification is for a
3673 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003674 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003675 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003676 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003677 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003678 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003679 */
3680 public Builder setColorized(boolean colorize) {
3681 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3682 return this;
3683 }
3684
3685 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003686 * Set this flag if you would only like the sound, vibrate
3687 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003688 *
3689 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003690 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003691 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3692 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3693 return this;
3694 }
3695
Joe Onoratocb109a02011-01-18 17:57:41 -08003696 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003697 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003698 *
3699 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003700 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003701 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003702 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003703 return this;
3704 }
3705
Joe Onoratocb109a02011-01-18 17:57:41 -08003706 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003707 * Set whether or not this notification should not bridge to other devices.
3708 *
3709 * <p>Some notifications can be bridged to other devices for remote display.
3710 * This hint can be set to recommend this notification not be bridged.
3711 */
3712 public Builder setLocalOnly(boolean localOnly) {
3713 setFlag(FLAG_LOCAL_ONLY, localOnly);
3714 return this;
3715 }
3716
3717 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003718 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003719 * <p>
3720 * The value should be one or more of the following fields combined with
3721 * bitwise-or:
3722 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3723 * <p>
3724 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003725 *
3726 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003727 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003728 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003729 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003730 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003731 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003732 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003733 return this;
3734 }
3735
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003736 /**
3737 * Set the priority of this notification.
3738 *
3739 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003740 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003741 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003742 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003743 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003744 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003745 return this;
3746 }
Joe Malin8d40d042012-11-05 11:36:40 -08003747
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003748 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003749 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003750 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003751 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003752 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003753 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003754 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003755 return this;
3756 }
3757
3758 /**
Chris Wrendde75302014-03-26 17:24:15 -04003759 * Add a person that is relevant to this notification.
3760 *
Chris Wrene6c48932014-09-29 17:19:27 -04003761 * <P>
3762 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003763 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3764 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3765 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003766 * </P>
3767 *
3768 * <P>
3769 * The person should be specified by the {@code String} representation of a
3770 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3771 * </P>
3772 *
3773 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3774 * URIs. The path part of these URIs must exist in the contacts database, in the
3775 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3776 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
Selim Cineke7238dd2017-12-14 17:48:32 -08003777 * It is also possible to provide a URI with the schema {@code name:} in order to uniquely
3778 * identify a person without an entry in the contacts database.
Chris Wrene6c48932014-09-29 17:19:27 -04003779 * </P>
3780 *
3781 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003782 * @see Notification#EXTRA_PEOPLE
Selim Cineke7238dd2017-12-14 17:48:32 -08003783 * @deprecated use {@link #addPerson(Person)}
Chris Wrendde75302014-03-26 17:24:15 -04003784 */
Chris Wrene6c48932014-09-29 17:19:27 -04003785 public Builder addPerson(String uri) {
Selim Cineke7238dd2017-12-14 17:48:32 -08003786 addPerson(new Person().setUri(uri));
3787 return this;
3788 }
3789
3790 /**
3791 * Add a person that is relevant to this notification.
3792 *
3793 * <P>
3794 * Depending on user preferences, this annotation may allow the notification to pass
3795 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3796 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3797 * appear more prominently in the user interface.
3798 * </P>
3799 *
3800 * <P>
3801 * A person should usually contain a uri in order to benefit from the ranking boost.
3802 * However, even if no uri is provided, it's beneficial to provide other people in the
3803 * notification, such that listeners and voice only devices can announce and handle them
3804 * properly.
3805 * </P>
3806 *
3807 * @param person the person to add.
3808 * @see Notification#EXTRA_PEOPLE_LIST
3809 */
3810 public Builder addPerson(Person person) {
3811 mPersonList.add(person);
Chris Wrendde75302014-03-26 17:24:15 -04003812 return this;
3813 }
3814
3815 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003816 * Set this notification to be part of a group of notifications sharing the same key.
3817 * Grouped notifications may display in a cluster or stack on devices which
3818 * support such rendering.
3819 *
3820 * <p>To make this notification the summary for its group, also call
3821 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3822 * {@link #setSortKey}.
3823 * @param groupKey The group key of the group.
3824 * @return this object for method chaining
3825 */
3826 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003827 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003828 return this;
3829 }
3830
3831 /**
3832 * Set this notification to be the group summary for a group of notifications.
3833 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003834 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3835 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003836 * @param isGroupSummary Whether this notification should be a group summary.
3837 * @return this object for method chaining
3838 */
3839 public Builder setGroupSummary(boolean isGroupSummary) {
3840 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3841 return this;
3842 }
3843
3844 /**
3845 * Set a sort key that orders this notification among other notifications from the
3846 * same package. This can be useful if an external sort was already applied and an app
3847 * would like to preserve this. Notifications will be sorted lexicographically using this
3848 * value, although providing different priorities in addition to providing sort key may
3849 * cause this value to be ignored.
3850 *
3851 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003852 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003853 *
3854 * @see String#compareTo(String)
3855 */
3856 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003857 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003858 return this;
3859 }
3860
3861 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003862 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003863 *
Griff Hazen720042b2014-02-24 15:46:56 -08003864 * <p>Values within the Bundle will replace existing extras values in this Builder.
3865 *
3866 * @see Notification#extras
3867 */
Griff Hazen959591e2014-05-15 22:26:18 -07003868 public Builder addExtras(Bundle extras) {
3869 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003870 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003871 }
3872 return this;
3873 }
3874
3875 /**
3876 * Set metadata for this notification.
3877 *
3878 * <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 -04003879 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003880 * called.
3881 *
Griff Hazen720042b2014-02-24 15:46:56 -08003882 * <p>Replaces any existing extras values with those from the provided Bundle.
3883 * Use {@link #addExtras} to merge in metadata instead.
3884 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003885 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003886 */
Griff Hazen959591e2014-05-15 22:26:18 -07003887 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003888 if (extras != null) {
3889 mUserExtras = extras;
3890 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003891 return this;
3892 }
3893
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003894 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003895 * Get the current metadata Bundle used by this notification Builder.
3896 *
3897 * <p>The returned Bundle is shared with this Builder.
3898 *
3899 * <p>The current contents of this Bundle are copied into the Notification each time
3900 * {@link #build()} is called.
3901 *
3902 * @see Notification#extras
3903 */
3904 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003905 return mUserExtras;
3906 }
3907
3908 private Bundle getAllExtras() {
3909 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3910 saveExtras.putAll(mN.extras);
3911 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003912 }
3913
3914 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003915 * Add an action to this notification. Actions are typically displayed by
3916 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003917 * <p>
3918 * Every action must have an icon (32dp square and matching the
3919 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3920 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3921 * <p>
3922 * A notification in its expanded form can display up to 3 actions, from left to right in
3923 * the order they were added. Actions will not be displayed when the notification is
3924 * collapsed, however, so be sure that any essential functions may be accessed by the user
3925 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003926 *
3927 * @param icon Resource ID of a drawable that represents the action.
3928 * @param title Text describing the action.
3929 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003930 *
3931 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003932 */
Dan Sandler86647982015-05-13 23:41:13 -04003933 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003934 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003935 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003936 return this;
3937 }
3938
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003939 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003940 * Add an action to this notification. Actions are typically displayed by
3941 * the system as a button adjacent to the notification content.
3942 * <p>
3943 * Every action must have an icon (32dp square and matching the
3944 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3945 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3946 * <p>
3947 * A notification in its expanded form can display up to 3 actions, from left to right in
3948 * the order they were added. Actions will not be displayed when the notification is
3949 * collapsed, however, so be sure that any essential functions may be accessed by the user
3950 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3951 *
3952 * @param action The action to add.
3953 */
3954 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003955 if (action != null) {
3956 mActions.add(action);
3957 }
Griff Hazen959591e2014-05-15 22:26:18 -07003958 return this;
3959 }
3960
3961 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003962 * Alter the complete list of actions attached to this notification.
3963 * @see #addAction(Action).
3964 *
3965 * @param actions
3966 * @return
3967 */
3968 public Builder setActions(Action... actions) {
3969 mActions.clear();
3970 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003971 if (actions[i] != null) {
3972 mActions.add(actions[i]);
3973 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003974 }
3975 return this;
3976 }
3977
3978 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003979 * Add a rich notification style to be applied at build time.
3980 *
3981 * @param style Object responsible for modifying the notification style.
3982 */
3983 public Builder setStyle(Style style) {
3984 if (mStyle != style) {
3985 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003986 if (mStyle != null) {
3987 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003988 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3989 } else {
3990 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003991 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003992 }
3993 return this;
3994 }
3995
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003996 /**
3997 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003998 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003999 * @return The same Builder.
4000 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06004001 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004002 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004003 return this;
4004 }
4005
4006 /**
4007 * Supply a replacement Notification whose contents should be shown in insecure contexts
4008 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
4009 * @param n A replacement notification, presumably with some or all info redacted.
4010 * @return The same Builder.
4011 */
4012 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004013 if (n != null) {
4014 mN.publicVersion = new Notification();
4015 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
4016 } else {
4017 mN.publicVersion = null;
4018 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004019 return this;
4020 }
4021
Griff Hazenb720abe2014-05-20 13:15:30 -07004022 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004023 * Apply an extender to this notification builder. Extenders may be used to add
4024 * metadata or change options on this builder.
4025 */
Griff Hazen61a9e862014-05-22 16:05:19 -07004026 public Builder extend(Extender extender) {
4027 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07004028 return this;
4029 }
4030
Dan Sandler4e787062015-06-17 15:09:48 -04004031 /**
4032 * @hide
4033 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04004034 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08004035 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004036 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004037 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004038 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08004039 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04004040 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08004041 }
4042
Dan Sandler26e81cf2014-05-06 10:01:27 -04004043 /**
4044 * Sets {@link Notification#color}.
4045 *
4046 * @param argb The accent color to use
4047 *
4048 * @return The same Builder.
4049 */
Tor Norbye80756e32015-03-02 09:39:27 -08004050 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004051 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004052 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04004053 return this;
4054 }
4055
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004056 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04004057 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
4058 // This user can never be a badged profile,
4059 // and also includes USER_ALL system notifications.
4060 return null;
4061 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02004062 // Note: This assumes that the current user can read the profile badge of the
4063 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08004064 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05004065 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004066 }
4067
4068 private Bitmap getProfileBadge() {
4069 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01004070 if (badge == null) {
4071 return null;
4072 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004073 final int size = mContext.getResources().getDimensionPixelSize(
4074 R.dimen.notification_badge_size);
4075 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004076 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02004077 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01004078 badge.draw(canvas);
4079 return bitmap;
4080 }
4081
Selim Cinekc848c3a2016-01-13 15:27:30 -08004082 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01004083 Bitmap profileBadge = getProfileBadge();
4084
Kenny Guy98193ea2014-07-24 19:54:37 +01004085 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004086 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
4087 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004088 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004089 contentView.setDrawableTint(R.id.profile_badge, false,
4090 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004091 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004092 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004093 }
4094
Christoph Studerfe718432014-09-01 18:21:18 +02004095 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004096 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004097 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02004098 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08004099 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004100 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08004101 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004102 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08004103 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08004104 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004105 }
4106
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004107 /**
4108 * Resets the notification header to its original state
4109 */
4110 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07004111 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
4112 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08004113 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004114 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02004115 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004116 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07004117 contentView.setTextViewText(R.id.header_text, null);
Selim Cinekafeed292017-12-12 17:32:44 -08004118 contentView.setViewVisibility(R.id.header_text_secondary, View.GONE);
4119 contentView.setTextViewText(R.id.header_text_secondary, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004120 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinekafeed292017-12-12 17:32:44 -08004121 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004122 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004123 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004124 contentView.setImageViewIcon(R.id.profile_badge, null);
4125 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004126 }
4127
4128 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004129 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
4130 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004131 }
4132
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004133 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004134 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004135 }
4136
4137 /**
4138 * @param hasProgress whether the progress bar should be shown and set
4139 */
4140 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004141 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
4142 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004143 }
4144
Adrian Roos70d7aa32017-01-11 15:39:06 -08004145 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01004146 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04004147
Christoph Studerfe718432014-09-01 18:21:18 +02004148 resetStandardTemplate(contentView);
4149
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004150 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08004151 updateBackgroundColor(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004152 bindNotificationHeader(contentView, p.ambient, p.headerTextSecondary);
Lucas Dupin90158d02018-01-23 16:36:12 -08004153 bindLargeIcon(contentView, p.hideLargeIcon || p.ambient, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08004154 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
4155 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08004156 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004157 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08004158 if (!p.ambient) {
4159 setTextViewColorPrimary(contentView, R.id.title);
4160 }
Selim Cinek954cc232016-05-20 13:29:23 -07004161 contentView.setViewLayoutWidth(R.id.title, showProgress
4162 ? ViewGroup.LayoutParams.WRAP_CONTENT
4163 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08004164 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08004165 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08004166 int textId = showProgress ? com.android.internal.R.id.text_line_1
4167 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07004168 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08004169 if (!p.ambient) {
4170 setTextViewColorSecondary(contentView, textId);
4171 }
Selim Cinek41598732016-01-11 16:58:37 -08004172 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08004173 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08004174
Selim Cinek279fa862016-06-14 10:57:25 -07004175 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004176
Selim Cinek29603462015-11-17 19:04:39 -08004177 return contentView;
4178 }
4179
Selim Cinek48f66b72017-08-18 16:17:51 -07004180 private CharSequence processTextSpans(CharSequence text) {
4181 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004182 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07004183 }
4184 return text;
4185 }
4186
Selim Cinek7b9605b2017-01-19 17:36:00 -08004187 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
4188 ensureColors();
4189 contentView.setTextColor(id, mPrimaryTextColor);
4190 }
4191
Selim Cinek48f66b72017-08-18 16:17:51 -07004192 private boolean hasForegroundColor() {
4193 return mForegroundColor != COLOR_INVALID;
4194 }
4195
Selim Cinek389edcd2017-05-11 19:16:44 -07004196 /**
4197 * @return the primary text color
4198 * @hide
4199 */
4200 @VisibleForTesting
4201 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004202 ensureColors();
4203 return mPrimaryTextColor;
4204 }
4205
Selim Cinek389edcd2017-05-11 19:16:44 -07004206 /**
4207 * @return the secondary text color
4208 * @hide
4209 */
4210 @VisibleForTesting
4211 public int getSecondaryTextColor() {
4212 ensureColors();
4213 return mSecondaryTextColor;
4214 }
4215
Selim Cinek7b9605b2017-01-19 17:36:00 -08004216 private int getActionBarColor() {
4217 ensureColors();
4218 return mActionBarColor;
4219 }
4220
Selim Cinek622c64a2017-04-17 17:10:05 -07004221 private int getActionBarColorDeEmphasized() {
4222 int backgroundColor = getBackgroundColor();
4223 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
4224 }
4225
Selim Cinek7b9605b2017-01-19 17:36:00 -08004226 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
4227 ensureColors();
4228 contentView.setTextColor(id, mSecondaryTextColor);
4229 }
4230
4231 private void ensureColors() {
4232 int backgroundColor = getBackgroundColor();
4233 if (mPrimaryTextColor == COLOR_INVALID
4234 || mSecondaryTextColor == COLOR_INVALID
4235 || mActionBarColor == COLOR_INVALID
4236 || mTextColorsAreForBackground != backgroundColor) {
4237 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07004238 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004239 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
4240 backgroundColor);
4241 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
4242 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07004243 if (backgroundColor != COLOR_DEFAULT
4244 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
4245 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4246 mPrimaryTextColor, backgroundColor, 4.5);
4247 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
4248 mSecondaryTextColor, backgroundColor, 4.5);
4249 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004250 } else {
4251 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4252 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4253 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4254 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004255 // We only respect the given colors if worst case Black or White still has
4256 // contrast
4257 boolean backgroundLight = backLum > textLum
4258 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4259 || backLum <= textLum
4260 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004261 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004262 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004263 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4264 mForegroundColor,
4265 backgroundColor,
4266 true /* findFG */,
4267 4.5f);
4268 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004269 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004270 } else {
4271 mSecondaryTextColor =
4272 NotificationColorUtil.findContrastColorAgainstDark(
4273 mForegroundColor,
4274 backgroundColor,
4275 true /* findFG */,
4276 4.5f);
4277 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004278 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004279 }
4280 } else {
4281 mPrimaryTextColor = mForegroundColor;
4282 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004283 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4284 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004285 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4286 backgroundColor) < 4.5f) {
4287 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004288 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004289 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4290 mSecondaryTextColor,
4291 backgroundColor,
4292 true /* findFG */,
4293 4.5f);
4294 } else {
4295 mSecondaryTextColor
4296 = NotificationColorUtil.findContrastColorAgainstDark(
4297 mSecondaryTextColor,
4298 backgroundColor,
4299 true /* findFG */,
4300 4.5f);
4301 }
4302 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004303 mSecondaryTextColor, backgroundLight
4304 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4305 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004306 }
4307 }
4308 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004309 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4310 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004311 }
4312 }
4313
4314 private void updateBackgroundColor(RemoteViews contentView) {
4315 if (isColorized()) {
4316 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4317 getBackgroundColor());
4318 } else {
4319 // Clear it!
4320 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4321 0);
4322 }
4323 }
4324
Selim Cinek860b6da2015-12-16 19:02:19 -08004325 /**
4326 * @param remoteView the remote view to update the minheight in
4327 * @param hasMinHeight does it have a mimHeight
4328 * @hide
4329 */
4330 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4331 int minHeight = 0;
4332 if (hasMinHeight) {
4333 // we need to set the minHeight of the notification
4334 minHeight = mContext.getResources().getDimensionPixelSize(
4335 com.android.internal.R.dimen.notification_min_content_height);
4336 }
4337 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4338 }
4339
Selim Cinek29603462015-11-17 19:04:39 -08004340 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004341 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4342 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4343 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4344 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004345 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004346 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004347 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004348 contentView.setProgressBackgroundTintList(
4349 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4350 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004351 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004352 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004353 contentView.setProgressTintList(R.id.progress, colorStateList);
4354 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004355 }
Selim Cinek29603462015-11-17 19:04:39 -08004356 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004357 } else {
4358 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004359 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004360 }
Joe Onorato561d3852010-11-20 18:09:34 -08004361 }
4362
Selim Cinek88188f22017-09-19 16:46:56 -07004363 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4364 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004365 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4366 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4367 }
Selim Cinek88188f22017-09-19 16:46:56 -07004368 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4369 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004370 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4371 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4372 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004373 int endMargin = R.dimen.notification_content_picture_margin;
4374 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4375 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4376 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004377 }
4378 // Bind the reply action
4379 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004380
Selim Cinek88188f22017-09-19 16:46:56 -07004381 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4382 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4383 if (actionVisible) {
4384 // We're only showing the icon as big if we're hiding the large icon
4385 int contrastColor = resolveContrastColor();
4386 int iconColor;
4387 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004388 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004389 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004390 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004391 contentView.setOnClickPendingIntent(R.id.right_icon,
4392 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004393 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004394 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4395 ? Color.BLACK : Color.WHITE;
4396 } else {
4397 contentView.setImageViewResource(R.id.right_icon,
4398 R.drawable.ic_reply_notification_large);
4399 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4400 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004401 }
Selim Cinek88188f22017-09-19 16:46:56 -07004402 contentView.setDrawableTint(replyId,
4403 false /* targetBackground */,
4404 iconColor,
4405 PorterDuff.Mode.SRC_ATOP);
4406 contentView.setOnClickPendingIntent(replyId,
4407 action.actionIntent);
4408 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4409 } else {
4410 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004411 }
Selim Cinek88188f22017-09-19 16:46:56 -07004412 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4413 ? View.VISIBLE
4414 : View.GONE);
4415 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004416 ? View.VISIBLE
4417 : View.GONE);
4418 }
4419
4420 private Action findReplyAction() {
4421 ArrayList<Action> actions = mActions;
4422 if (mOriginalActions != null) {
4423 actions = mOriginalActions;
4424 }
4425 int numActions = actions.size();
4426 for (int i = 0; i < numActions; i++) {
4427 Action action = actions.get(i);
4428 if (hasValidRemoteInput(action)) {
4429 return action;
4430 }
4431 }
4432 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004433 }
4434
Selim Cinekafeed292017-12-12 17:32:44 -08004435 private void bindNotificationHeader(RemoteViews contentView, boolean ambient,
4436 CharSequence secondaryHeaderText) {
Adrian Roos487374f2017-01-11 15:48:14 -08004437 bindSmallIcon(contentView, ambient);
4438 bindHeaderAppName(contentView, ambient);
4439 if (!ambient) {
4440 // Ambient view does not have these
4441 bindHeaderText(contentView);
Selim Cinekafeed292017-12-12 17:32:44 -08004442 bindHeaderTextSecondary(contentView, secondaryHeaderText);
Adrian Roos487374f2017-01-11 15:48:14 -08004443 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004444 bindProfileBadge(contentView);
4445 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004446 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004447 }
4448
4449 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004450 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004451 contentView.setDrawableTint(R.id.expand_button, false, color,
4452 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004453 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004454 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004455 }
4456
Selim Cinek99104832017-01-25 14:47:33 -08004457 /**
4458 * @return the color that is used as the first primary highlight color. This is applied
4459 * in several places like the action buttons or the app name in the header.
4460 */
4461 private int getPrimaryHighlightColor() {
4462 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4463 }
4464
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004465 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4466 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004467 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004468 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004469 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4470 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4471 contentView.setLong(R.id.chronometer, "setBase",
4472 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4473 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004474 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004475 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004476 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004477 } else {
4478 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4479 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004480 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004481 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004482 } else {
4483 // We still want a time to be set but gone, such that we can show and hide it
4484 // on demand in case it's a child notification without anything in the header
4485 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004486 }
4487 }
4488
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004489 private void bindHeaderText(RemoteViews contentView) {
4490 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4491 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004492 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004493 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004494 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004495 if (headerText == null
4496 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4497 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4498 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4499 }
4500 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004501 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004502 contentView.setTextViewText(R.id.header_text, processTextSpans(
4503 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004504 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004505 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4506 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004507 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004508 }
4509 }
4510
Selim Cinekafeed292017-12-12 17:32:44 -08004511 private void bindHeaderTextSecondary(RemoteViews contentView, CharSequence secondaryText) {
4512 if (!TextUtils.isEmpty(secondaryText)) {
4513 contentView.setTextViewText(R.id.header_text_secondary, processTextSpans(
4514 processLegacyText(secondaryText)));
4515 setTextViewColorSecondary(contentView, R.id.header_text_secondary);
4516 contentView.setViewVisibility(R.id.header_text_secondary, View.VISIBLE);
4517 contentView.setViewVisibility(R.id.header_text_secondary_divider, View.VISIBLE);
4518 setTextViewColorSecondary(contentView, R.id.header_text_secondary_divider);
4519 }
4520 }
4521
Adrian Rooseba05822016-04-22 17:09:27 -07004522 /**
4523 * @hide
4524 */
4525 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004526 CharSequence name = null;
4527 final PackageManager pm = mContext.getPackageManager();
4528 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4529 // only system packages which lump together a bunch of unrelated stuff
4530 // may substitute a different name to make the purpose of the
4531 // notification more clear. the correct package label should always
4532 // be accessible via SystemUI.
4533 final String pkg = mContext.getPackageName();
4534 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4535 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4536 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4537 name = subName;
4538 } else {
4539 Log.w(TAG, "warning: pkg "
4540 + pkg + " attempting to substitute app name '" + subName
4541 + "' without holding perm "
4542 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4543 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004544 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004545 if (TextUtils.isEmpty(name)) {
4546 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4547 }
4548 if (TextUtils.isEmpty(name)) {
4549 // still nothing?
4550 return null;
4551 }
4552
4553 return String.valueOf(name);
4554 }
Adrian Roos487374f2017-01-11 15:48:14 -08004555 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004556 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004557 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004558 setTextViewColorPrimary(contentView, R.id.app_name_text);
4559 } else {
4560 contentView.setTextColor(R.id.app_name_text,
4561 ambient ? resolveAmbientColor() : resolveContrastColor());
4562 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004563 }
4564
Adrian Roos487374f2017-01-11 15:48:14 -08004565 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004566 if (mN.mSmallIcon == null && mN.icon != 0) {
4567 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4568 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004569 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004570 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004571 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004572 }
4573
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004574 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004575 * @return true if the built notification will show the time or the chronometer; false
4576 * otherwise
4577 */
4578 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004579 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004580 }
4581
Christoph Studerfe718432014-09-01 18:21:18 +02004582 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004583 // actions_container is only reset when there are no actions to avoid focus issues with
4584 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004585 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004586 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004587
4588 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4589 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4590
4591 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4592 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4593 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4594 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004595
4596 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004597 }
4598
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004599 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004600 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004601 }
4602
Adrian Roos70d7aa32017-01-11 15:39:06 -08004603 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4604 StandardTemplateParams p) {
4605 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004606
Christoph Studerfe718432014-09-01 18:21:18 +02004607 resetStandardTemplateWithActions(big);
4608
Adrian Roose458aa82015-12-08 16:17:19 -08004609 boolean validRemoteInput = false;
4610
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004611 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004612 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004613 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004614 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004615 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004616 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004617 if (p.ambient) {
4618 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004619 } else if (isColorized()) {
4620 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4621 } else {
4622 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4623 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004624 }
Adrian Roosf852a422016-06-03 13:33:43 -07004625 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4626 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004627 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004628 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004629 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004630 boolean actionHasValidInput = hasValidRemoteInput(action);
4631 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004632
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004633 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004634 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004635 if (actionHasValidInput) {
4636 // Clear the drawable
4637 button.setInt(R.id.action0, "setBackgroundResource", 0);
4638 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004639 big.addView(R.id.actions, button);
4640 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004641 } else {
4642 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004643 }
Adrian Roose458aa82015-12-08 16:17:19 -08004644
4645 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004646 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004647 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4648 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004649 big.setTextViewText(R.id.notification_material_reply_text_1,
4650 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004651 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004652
4653 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4654 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004655 big.setTextViewText(R.id.notification_material_reply_text_2,
4656 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004657 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004658
4659 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4660 big.setViewVisibility(
4661 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004662 big.setTextViewText(R.id.notification_material_reply_text_3,
4663 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004664 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004665 }
4666 }
4667 }
4668
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004669 return big;
4670 }
4671
Adrian Roose458aa82015-12-08 16:17:19 -08004672 private boolean hasValidRemoteInput(Action action) {
4673 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4674 // Weird actions
4675 return false;
4676 }
4677
4678 RemoteInput[] remoteInputs = action.getRemoteInputs();
4679 if (remoteInputs == null) {
4680 return false;
4681 }
4682
4683 for (RemoteInput r : remoteInputs) {
4684 CharSequence[] choices = r.getChoices();
4685 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4686 return true;
4687 }
4688 }
4689 return false;
4690 }
4691
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004692 /**
4693 * Construct a RemoteViews for the final 1U notification layout. In order:
4694 * 1. Custom contentView from the caller
4695 * 2. Style's proposed content view
4696 * 3. Standard template view
4697 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004698 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004699 return createContentView(false /* increasedheight */ );
4700 }
4701
4702 /**
4703 * Construct a RemoteViews for the smaller content view.
4704 *
4705 * @param increasedHeight true if this layout be created with an increased height. Some
4706 * styles may support showing more then just that basic 1U size
4707 * and the system may decide to render important notifications
4708 * slightly bigger even when collapsed.
4709 *
4710 * @hide
4711 */
4712 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004713 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004714 return mN.contentView;
4715 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004716 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004717 if (styleView != null) {
4718 return styleView;
4719 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004720 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004721 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004722 }
4723
Selim Cineka7679b62017-05-10 16:33:25 -07004724 private boolean useExistingRemoteView() {
4725 return mStyle == null || (!mStyle.displayCustomViewInline()
4726 && !mRebuildStyledRemoteViews);
4727 }
4728
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004729 /**
4730 * Construct a RemoteViews for the final big notification layout.
4731 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004732 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004733 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004734 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004735 return mN.bigContentView;
4736 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004737 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004738 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004739 } else if (mActions.size() != 0) {
4740 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004741 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004742 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004743 return result;
4744 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004745
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004746 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004747 * Construct a RemoteViews for the final notification header only. This will not be
4748 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004749 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004750 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004751 * @hide
4752 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004753 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004754 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4755 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004756 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004757 ambient ? R.layout.notification_template_ambient_header
4758 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004759 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004760 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004761 if (colorized != null) {
4762 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4763 } else {
4764 mN.extras.remove(EXTRA_COLORIZED);
4765 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004766 return header;
4767 }
4768
Adrian Roos487374f2017-01-11 15:48:14 -08004769 /**
4770 * Construct a RemoteViews for the ambient version of the notification.
4771 *
4772 * @hide
4773 */
4774 public RemoteViews makeAmbientNotification() {
4775 RemoteViews ambient = applyStandardTemplateWithActions(
4776 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004777 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004778 return ambient;
4779 }
4780
Selim Cinek29603462015-11-17 19:04:39 -08004781 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004782 if (result != null) {
4783 result.setViewVisibility(R.id.text_line_1, View.GONE);
4784 }
Selim Cinek29603462015-11-17 19:04:39 -08004785 }
4786
Selim Cinek6743c0b2017-01-18 18:24:01 -08004787 /**
4788 * Adapt the Notification header if this view is used as an expanded view.
4789 *
4790 * @hide
4791 */
4792 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004793 if (result != null) {
4794 result.setBoolean(R.id.notification_header, "setExpanded", true);
4795 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004796 }
4797
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004798 /**
4799 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004800 *
4801 * @param increasedHeight true if this layout be created with an increased height. Some
4802 * styles may support showing more then just that basic 1U size
4803 * and the system may decide to render important notifications
4804 * slightly bigger even when collapsed.
4805 *
4806 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004807 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004808 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004809 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004810 return mN.headsUpContentView;
4811 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004812 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4813 if (styleView != null) {
4814 return styleView;
4815 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004816 } else if (mActions.size() == 0) {
4817 return null;
4818 }
4819
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004820 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004821 }
4822
Selim Cinek624c02db2015-12-14 21:00:02 -08004823 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004824 * Construct a RemoteViews for the final heads-up notification layout.
4825 */
4826 public RemoteViews createHeadsUpContentView() {
4827 return createHeadsUpContentView(false /* useIncreasedHeight */);
4828 }
4829
4830 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004831 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4832 *
4833 * @hide
4834 */
4835 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004836 return makePublicView(false /* ambient */);
4837 }
4838
4839 /**
4840 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4841 *
4842 * @hide
4843 */
4844 public RemoteViews makePublicAmbientNotification() {
4845 return makePublicView(true /* ambient */);
4846 }
4847
4848 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004849 if (mN.publicVersion != null) {
4850 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004851 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004852 }
4853 Bundle savedBundle = mN.extras;
4854 Style style = mStyle;
4855 mStyle = null;
4856 Icon largeIcon = mN.mLargeIcon;
4857 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004858 Bitmap largeIconLegacy = mN.largeIcon;
4859 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004860 ArrayList<Action> actions = mActions;
4861 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004862 Bundle publicExtras = new Bundle();
4863 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4864 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4865 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4866 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004867 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4868 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004869 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004870 RemoteViews view;
4871 if (ambient) {
4872 publicExtras.putCharSequence(EXTRA_TITLE,
4873 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4874 view = makeAmbientNotification();
4875 } else{
4876 view = makeNotificationHeader(false /* ambient */);
4877 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4878 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004879 mN.extras = savedBundle;
4880 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004881 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004882 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004883 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004884 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004885 }
4886
Selim Cinek6743c0b2017-01-18 18:24:01 -08004887 /**
4888 * Construct a content view for the display when low - priority
4889 *
4890 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4891 * a new subtext is created consisting of the content of the
4892 * notification.
4893 * @hide
4894 */
4895 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4896 int color = mN.color;
4897 mN.color = COLOR_DEFAULT;
4898 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4899 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4900 CharSequence newSummary = createSummaryText();
4901 if (!TextUtils.isEmpty(newSummary)) {
4902 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4903 }
4904 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004905
Adrian Roos6f6e1592017-05-02 16:22:53 -07004906 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004907 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004908 if (summary != null) {
4909 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4910 } else {
4911 mN.extras.remove(EXTRA_SUB_TEXT);
4912 }
4913 mN.color = color;
4914 return header;
4915 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004916
Selim Cinek6743c0b2017-01-18 18:24:01 -08004917 private CharSequence createSummaryText() {
4918 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4919 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4920 return titleText;
4921 }
4922 SpannableStringBuilder summary = new SpannableStringBuilder();
4923 if (titleText == null) {
4924 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4925 }
4926 BidiFormatter bidi = BidiFormatter.getInstance();
4927 if (titleText != null) {
4928 summary.append(bidi.unicodeWrap(titleText));
4929 }
4930 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4931 if (titleText != null && contentText != null) {
4932 summary.append(bidi.unicodeWrap(mContext.getText(
4933 R.string.notification_header_divider_symbol_with_spaces)));
4934 }
4935 if (contentText != null) {
4936 summary.append(bidi.unicodeWrap(contentText));
4937 }
4938 return summary;
4939 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004940
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004941 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004942 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004943 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004944 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004945 emphazisedMode ? getEmphasizedActionLayoutResource()
4946 : tombstone ? getActionTombstoneLayoutResource()
4947 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004948 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004949 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004950 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004951 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004952 if (action.mRemoteInputs != null) {
4953 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4954 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004955 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004956 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004957 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004958 int bgColor;
4959 if (isColorized()) {
4960 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4961 } else {
4962 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4963 : R.color.notification_action_list_dark);
4964 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004965 button.setDrawableTint(R.id.button_holder, true,
4966 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004967 CharSequence title = action.title;
4968 ColorStateList[] outResultColor = null;
4969 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004970 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004971 } else {
4972 outResultColor = new ColorStateList[1];
4973 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4974 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004975 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004976 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004977 if (outResultColor != null && outResultColor[0] != null) {
4978 // We need to set the text color as well since changing a text to uppercase
4979 // clears its spans.
4980 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004981 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004982 button.setTextColor(R.id.action0,resolveContrastColor());
4983 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004984 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004985 button.setTextViewText(R.id.action0, processTextSpans(
4986 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004987 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004988 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004989 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004990 button.setTextColor(R.id.action0,
4991 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004992 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004993 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004994 return button;
4995 }
4996
Joe Onoratocb109a02011-01-18 17:57:41 -08004997 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004998 * Ensures contrast on color spans against a background color. also returns the color of the
4999 * text if a span was found that spans over the whole text.
5000 *
5001 * @param charSequence the charSequence on which the spans are
5002 * @param background the background color to ensure the contrast against
5003 * @param outResultColor an array in which a color will be returned as the first element if
5004 * there exists a full length color span.
5005 * @return the contrasted charSequence
5006 */
5007 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5008 ColorStateList[] outResultColor) {
5009 if (charSequence instanceof Spanned) {
5010 Spanned ss = (Spanned) charSequence;
5011 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5012 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5013 for (Object span : spans) {
5014 Object resultSpan = span;
5015 int spanStart = ss.getSpanStart(span);
5016 int spanEnd = ss.getSpanEnd(span);
5017 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5018 if (resultSpan instanceof CharacterStyle) {
5019 resultSpan = ((CharacterStyle) span).getUnderlying();
5020 }
5021 if (resultSpan instanceof TextAppearanceSpan) {
5022 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5023 ColorStateList textColor = originalSpan.getTextColor();
5024 if (textColor != null) {
5025 int[] colors = textColor.getColors();
5026 int[] newColors = new int[colors.length];
5027 for (int i = 0; i < newColors.length; i++) {
5028 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005029 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005030 }
5031 textColor = new ColorStateList(textColor.getStates().clone(),
5032 newColors);
5033 resultSpan = new TextAppearanceSpan(
5034 originalSpan.getFamily(),
5035 originalSpan.getTextStyle(),
5036 originalSpan.getTextSize(),
5037 textColor,
5038 originalSpan.getLinkTextColor());
5039 if (fullLength) {
5040 outResultColor[0] = new ColorStateList(
5041 textColor.getStates().clone(), newColors);
5042 }
5043 }
5044 } else if (resultSpan instanceof ForegroundColorSpan) {
5045 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5046 int foregroundColor = originalSpan.getForegroundColor();
5047 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005048 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005049 resultSpan = new ForegroundColorSpan(foregroundColor);
5050 if (fullLength) {
5051 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5052 }
5053 } else {
5054 resultSpan = span;
5055 }
5056
5057 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5058 }
5059 return builder;
5060 }
5061 return charSequence;
5062 }
5063
5064 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005065 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005066 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005067 */
5068 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005069 if (!mIsLegacyInitialized) {
5070 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5071 < Build.VERSION_CODES.LOLLIPOP;
5072 mIsLegacyInitialized = true;
5073 }
5074 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005075 }
5076
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005077 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005078 return processLegacyText(charSequence, false /* ambient */);
5079 }
5080
5081 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5082 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5083 boolean wantLightText = ambient;
5084 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005085 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005086 } else {
5087 return charSequence;
5088 }
5089 }
5090
Dan Sandler26e81cf2014-05-06 10:01:27 -04005091 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005092 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005093 */
Adrian Roos487374f2017-01-11 15:48:14 -08005094 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5095 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005096 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005097 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005098 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005099 contentView.setDrawableTint(R.id.icon, false, color,
5100 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005101
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005102 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005103 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005104 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005105 }
5106
Dan Sandler26e81cf2014-05-06 10:01:27 -04005107 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005108 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005109 * if it's grayscale).
5110 */
5111 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005112 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5113 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005114 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005115 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005116 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5117 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005118 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005119 }
5120
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005121 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005122 if (mN.color != COLOR_DEFAULT) {
5123 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005124 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005125 }
5126
Adrian Roos4ff3b122016-02-01 12:26:13 -08005127 int resolveContrastColor() {
5128 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5129 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005130 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005131
Selim Cinekac5f0272017-05-02 16:05:41 -07005132 int color;
5133 int background = mBackgroundColorHint;
5134 if (mBackgroundColorHint == COLOR_INVALID) {
5135 background = mContext.getColor(
5136 com.android.internal.R.color.notification_material_background_color);
5137 }
5138 if (mN.color == COLOR_DEFAULT) {
5139 ensureColors();
5140 color = mSecondaryTextColor;
5141 } else {
5142 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005143 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005144 }
5145 if (Color.alpha(color) < 255) {
5146 // alpha doesn't go well for color filters, so let's blend it manually
5147 color = NotificationColorUtil.compositeColors(color, background);
5148 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005149 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005150 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005151 }
5152
Adrian Roos487374f2017-01-11 15:48:14 -08005153 int resolveAmbientColor() {
5154 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5155 return mCachedAmbientColor;
5156 }
5157 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5158
5159 mCachedAmbientColorIsFor = mN.color;
5160 return mCachedAmbientColor = contrasted;
5161 }
5162
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005163 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005164 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005165 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005166 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005167 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005168 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005169 mN.actions = new Action[mActions.size()];
5170 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005171 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005172 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005173 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005174 }
Selim Cinek247fa012016-02-18 09:50:48 -08005175 if (mN.bigContentView != null || mN.contentView != null
5176 || mN.headsUpContentView != null) {
5177 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5178 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005179 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005180 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005181
Julia Reynolds3b848122016-02-26 10:45:32 -05005182 /**
5183 * Creates a Builder from an existing notification so further changes can be made.
5184 * @param context The context for your application / activity.
5185 * @param n The notification to create a Builder from.
5186 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005187 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005188 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005189 ApplicationInfo applicationInfo = n.extras.getParcelable(
5190 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005191 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005192 if (applicationInfo != null) {
5193 try {
5194 builderContext = context.createApplicationContext(applicationInfo,
5195 Context.CONTEXT_RESTRICTED);
5196 } catch (NameNotFoundException e) {
5197 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5198 builderContext = context; // try with our context
5199 }
5200 } else {
5201 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005202 }
5203
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005204 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005205 }
5206
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005207 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005208 * @deprecated Use {@link #build()} instead.
5209 */
5210 @Deprecated
5211 public Notification getNotification() {
5212 return build();
5213 }
5214
5215 /**
5216 * Combine all of the options that have been set and return a new {@link Notification}
5217 * object.
5218 */
5219 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005220 // first, add any extras from the calling code
5221 if (mUserExtras != null) {
5222 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005223 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005224
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005225 mN.creationTime = System.currentTimeMillis();
5226
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005227 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005228 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005229
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005230 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005231
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005232 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005233 mStyle.reduceImageSizes(mContext);
5234 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005235 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005236 }
Selim Cinekd0426622017-07-11 13:19:59 +02005237 mN.reduceImageSizes(mContext);
5238
Adrian Roos5081c0d2016-02-26 16:04:19 -08005239 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005240 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005241 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005242 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005243 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5244 mN.contentView.getSequenceNumber());
5245 }
5246 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005247 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005248 if (mN.bigContentView != null) {
5249 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5250 mN.bigContentView.getSequenceNumber());
5251 }
5252 }
5253 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005254 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005255 if (mN.headsUpContentView != null) {
5256 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5257 mN.headsUpContentView.getSequenceNumber());
5258 }
5259 }
5260 }
5261
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005262 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5263 mN.flags |= FLAG_SHOW_LIGHTS;
5264 }
5265
Adrian Roosfb921842017-10-26 14:49:56 +02005266 mN.allPendingIntents = null;
5267
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005268 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005269 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005270
5271 /**
5272 * Apply this Builder to an existing {@link Notification} object.
5273 *
5274 * @hide
5275 */
5276 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005277 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005278 return n;
5279 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005280
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005281 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005282 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005283 * change. Also removes extenders on low ram devices, as
5284 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005285 *
5286 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5287 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005288 * @hide
5289 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005290 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005291 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005292
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005293 // Only strip views for known Styles because we won't know how to
5294 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005295 if (!isLowRam
5296 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005297 && getNotificationStyleClass(templateClass) == null) {
5298 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005299 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005300
5301 // Only strip unmodified BuilderRemoteViews.
5302 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005303 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005304 n.contentView.getSequenceNumber();
5305 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005306 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005307 n.bigContentView.getSequenceNumber();
5308 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005309 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005310 n.headsUpContentView.getSequenceNumber();
5311
5312 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005313 if (!isLowRam
5314 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005315 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005316 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005317
5318 Notification clone = n.clone();
5319 if (stripContentView) {
5320 clone.contentView = null;
5321 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5322 }
5323 if (stripBigContentView) {
5324 clone.bigContentView = null;
5325 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5326 }
5327 if (stripHeadsUpContentView) {
5328 clone.headsUpContentView = null;
5329 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5330 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005331 if (isLowRam) {
5332 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5333 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5334 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5335 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005336 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005337 }
5338
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005339 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005340 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005341 }
5342
5343 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005344 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005345 }
5346
5347 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005348 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005349 }
5350
5351 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005352 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005353 }
5354
5355 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005356 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005357 }
5358
Adrian Roosc1a80b02016-04-05 14:54:55 -07005359 private int getMessagingLayoutResource() {
5360 return R.layout.notification_template_material_messaging;
5361 }
5362
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005363 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005364 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005365 }
5366
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005367 private int getEmphasizedActionLayoutResource() {
5368 return R.layout.notification_material_action_emphasized;
5369 }
5370
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005371 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005372 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005373 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005374
5375 private int getBackgroundColor() {
5376 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005377 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005378 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005379 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5380 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005381 }
5382 }
5383
5384 private boolean isColorized() {
5385 return mN.isColorized();
5386 }
Selim Cinek99104832017-01-25 14:47:33 -08005387
Anthony Chenad4d1582017-04-10 16:07:58 -07005388 private boolean shouldTintActionButtons() {
5389 return mTintActionButtons;
5390 }
5391
Selim Cinek99104832017-01-25 14:47:33 -08005392 private boolean textColorsNeedInversion() {
5393 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5394 return false;
5395 }
5396 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5397 return targetSdkVersion > Build.VERSION_CODES.M
5398 && targetSdkVersion < Build.VERSION_CODES.O;
5399 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005400
5401 /**
5402 * Set a color palette to be used as the background and textColors
5403 *
5404 * @param backgroundColor the color to be used as the background
5405 * @param foregroundColor the color to be used as the foreground
5406 *
5407 * @hide
5408 */
5409 public void setColorPalette(int backgroundColor, int foregroundColor) {
5410 mBackgroundColor = backgroundColor;
5411 mForegroundColor = foregroundColor;
5412 mTextColorsAreForBackground = COLOR_INVALID;
5413 ensureColors();
5414 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005415
5416 /**
5417 * Sets the background color for this notification to be a different one then the default.
5418 * This is mainly used to calculate contrast and won't necessarily be applied to the
5419 * background.
5420 *
5421 * @hide
5422 */
5423 public void setBackgroundColorHint(int backgroundColor) {
5424 mBackgroundColorHint = backgroundColor;
5425 }
Selim Cineka7679b62017-05-10 16:33:25 -07005426
5427
5428 /**
5429 * Forces all styled remoteViews to be built from scratch and not use any cached
5430 * RemoteViews.
5431 * This is needed for legacy apps that are baking in their remoteviews into the
5432 * notification.
5433 *
5434 * @hide
5435 */
5436 public void setRebuildStyledRemoteViews(boolean rebuild) {
5437 mRebuildStyledRemoteViews = rebuild;
5438 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005439 }
5440
5441 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005442 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5443 * remote views.
5444 *
5445 * @hide
5446 */
5447 void reduceImageSizes(Context context) {
5448 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5449 return;
5450 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005451 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005452 if (mLargeIcon != null || largeIcon != null) {
5453 Resources resources = context.getResources();
5454 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005455 int maxWidth = resources.getDimensionPixelSize(isLowRam
5456 ? R.dimen.notification_right_icon_size_low_ram
5457 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005458 int maxHeight = maxWidth;
5459 if (MediaStyle.class.equals(style)
5460 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005461 maxHeight = resources.getDimensionPixelSize(isLowRam
5462 ? R.dimen.notification_media_image_max_height_low_ram
5463 : R.dimen.notification_media_image_max_height);
5464 maxWidth = resources.getDimensionPixelSize(isLowRam
5465 ? R.dimen.notification_media_image_max_width_low_ram
5466 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005467 }
5468 if (mLargeIcon != null) {
5469 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5470 }
5471 if (largeIcon != null) {
5472 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5473 }
5474 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005475 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5476 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5477 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005478 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5479 }
5480
Selim Cineka8cb1262017-08-15 16:53:44 -07005481 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5482 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005483 if (remoteView != null) {
5484 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005485 int maxWidth = resources.getDimensionPixelSize(isLowRam
5486 ? R.dimen.notification_custom_view_max_image_width_low_ram
5487 : R.dimen.notification_custom_view_max_image_width);
5488 int maxHeight = resources.getDimensionPixelSize(isLowRam
5489 ? R.dimen.notification_custom_view_max_image_height_low_ram
5490 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005491 remoteView.reduceImageSizes(maxWidth, maxHeight);
5492 }
5493 }
5494
5495 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005496 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005497 */
Selim Cinek22714f12017-04-13 16:23:53 -07005498 private boolean isForegroundService() {
5499 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005500 }
5501
5502 /**
Selim Cinek99104832017-01-25 14:47:33 -08005503 * @return whether this notification has a media session attached
5504 * @hide
5505 */
5506 public boolean hasMediaSession() {
5507 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5508 }
5509
5510 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005511 * @return the style class of this notification
5512 * @hide
5513 */
5514 public Class<? extends Notification.Style> getNotificationStyle() {
5515 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5516
5517 if (!TextUtils.isEmpty(templateClass)) {
5518 return Notification.getNotificationStyleClass(templateClass);
5519 }
5520 return null;
5521 }
5522
5523 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005524 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005525 *
5526 * @hide
5527 */
5528 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005529 if (isColorizedMedia()) {
5530 return true;
5531 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005532 return extras.getBoolean(EXTRA_COLORIZED)
5533 && (hasColorizedPermission() || isForegroundService());
5534 }
5535
5536 /**
5537 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5538 * permission. The permission is checked when a notification is enqueued.
5539 */
5540 private boolean hasColorizedPermission() {
5541 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005542 }
5543
5544 /**
5545 * @return true if this notification is colorized and it is a media notification
5546 *
5547 * @hide
5548 */
5549 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005550 Class<? extends Style> style = getNotificationStyle();
5551 if (MediaStyle.class.equals(style)) {
5552 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5553 if ((colorized == null || colorized) && hasMediaSession()) {
5554 return true;
5555 }
5556 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5557 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5558 return true;
5559 }
5560 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005561 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005562 }
5563
Selim Cinek0847acd2017-04-24 19:48:29 -07005564
5565 /**
5566 * @return true if this is a media notification
5567 *
5568 * @hide
5569 */
5570 public boolean isMediaNotification() {
5571 Class<? extends Style> style = getNotificationStyle();
5572 if (MediaStyle.class.equals(style)) {
5573 return true;
5574 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5575 return true;
5576 }
5577 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005578 }
5579
Selim Cinek279fa862016-06-14 10:57:25 -07005580 private boolean hasLargeIcon() {
5581 return mLargeIcon != null || largeIcon != null;
5582 }
5583
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005584 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005585 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005586 * @hide
5587 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005588 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005589 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5590 }
5591
5592 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005593 * @return true if the notification will show a chronometer; false otherwise
5594 * @hide
5595 */
5596 public boolean showsChronometer() {
5597 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5598 }
5599
5600 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005601 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005602 */
5603 @SystemApi
5604 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5605 Class<? extends Style>[] classes = new Class[] {
5606 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5607 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5608 MessagingStyle.class };
5609 for (Class<? extends Style> innerClass : classes) {
5610 if (templateClass.equals(innerClass.getName())) {
5611 return innerClass;
5612 }
5613 }
5614 return null;
5615 }
5616
5617 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005618 * An object that can apply a rich notification style to a {@link Notification.Builder}
5619 * object.
5620 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005621 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005622 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005623
5624 /**
5625 * @hide
5626 */
5627 protected CharSequence mSummaryText = null;
5628
5629 /**
5630 * @hide
5631 */
5632 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005633
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005634 protected Builder mBuilder;
5635
Chris Wrend6297db2012-05-03 16:20:13 -04005636 /**
5637 * Overrides ContentTitle in the big form of the template.
5638 * This defaults to the value passed to setContentTitle().
5639 */
5640 protected void internalSetBigContentTitle(CharSequence title) {
5641 mBigContentTitle = title;
5642 }
5643
5644 /**
5645 * Set the first line of text after the detail section in the big form of the template.
5646 */
5647 protected void internalSetSummaryText(CharSequence cs) {
5648 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005649 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005650 }
5651
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005652 public void setBuilder(Builder builder) {
5653 if (mBuilder != builder) {
5654 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005655 if (mBuilder != null) {
5656 mBuilder.setStyle(this);
5657 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005658 }
5659 }
5660
Chris Wrend6297db2012-05-03 16:20:13 -04005661 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005662 if (mBuilder == null) {
5663 throw new IllegalArgumentException("Style requires a valid Builder object");
5664 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005665 }
Chris Wrend6297db2012-05-03 16:20:13 -04005666
5667 protected RemoteViews getStandardView(int layoutId) {
5668 checkBuilder();
5669
Christoph Studer4600f9b2014-07-22 22:44:43 +02005670 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005671 CharSequence oldBuilderContentTitle =
5672 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005673 if (mBigContentTitle != null) {
5674 mBuilder.setContentTitle(mBigContentTitle);
5675 }
5676
Chris Wrend6297db2012-05-03 16:20:13 -04005677 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5678
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005679 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005680
Chris Wrend6297db2012-05-03 16:20:13 -04005681 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5682 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005683 } else {
5684 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005685 }
5686
Chris Wrend6297db2012-05-03 16:20:13 -04005687 return contentView;
5688 }
5689
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005690 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005691 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005692 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005693 *
5694 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005695 * @hide
5696 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005697 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005698 return null;
5699 }
5700
5701 /**
5702 * Construct a Style-specific RemoteViews for the final big notification layout.
5703 * @hide
5704 */
5705 public RemoteViews makeBigContentView() {
5706 return null;
5707 }
5708
5709 /**
5710 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005711 *
5712 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005713 * @hide
5714 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005715 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005716 return null;
5717 }
5718
5719 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005720 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005721 * @hide
5722 */
5723 public void addExtras(Bundle extras) {
5724 if (mSummaryTextSet) {
5725 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5726 }
5727 if (mBigContentTitle != null) {
5728 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5729 }
Chris Wren91ad5632013-06-05 15:05:57 -04005730 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005731 }
5732
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005733 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005734 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005735 * @hide
5736 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005737 protected void restoreFromExtras(Bundle extras) {
5738 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5739 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5740 mSummaryTextSet = true;
5741 }
5742 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5743 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5744 }
5745 }
5746
5747
5748 /**
5749 * @hide
5750 */
5751 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005752 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005753 return wip;
5754 }
5755
Daniel Sandler0ec46202015-06-24 01:27:05 -04005756 /**
5757 * @hide
5758 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005759 public void purgeResources() {}
5760
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005761 /**
5762 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5763 * attached to.
5764 *
5765 * @return the fully constructed Notification.
5766 */
5767 public Notification build() {
5768 checkBuilder();
5769 return mBuilder.build();
5770 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005771
5772 /**
5773 * @hide
5774 * @return true if the style positions the progress bar on the second line; false if the
5775 * style hides the progress bar
5776 */
5777 protected boolean hasProgress() {
5778 return true;
5779 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005780
5781 /**
5782 * @hide
5783 * @return Whether we should put the summary be put into the notification header
5784 */
5785 public boolean hasSummaryInHeader() {
5786 return true;
5787 }
Selim Cinek593610c2016-02-16 18:42:57 -08005788
5789 /**
5790 * @hide
5791 * @return Whether custom content views are displayed inline in the style
5792 */
5793 public boolean displayCustomViewInline() {
5794 return false;
5795 }
Selim Cinekd0426622017-07-11 13:19:59 +02005796
5797 /**
5798 * Reduces the image sizes contained in this style.
5799 *
5800 * @hide
5801 */
5802 public void reduceImageSizes(Context context) {
5803 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005804 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005805
5806 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005807 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005808 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005809 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005810 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005811 * Notification notif = new Notification.Builder(mContext)
5812 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5813 * .setContentText(subject)
5814 * .setSmallIcon(R.drawable.new_post)
5815 * .setLargeIcon(aBitmap)
5816 * .setStyle(new Notification.BigPictureStyle()
5817 * .bigPicture(aBigBitmap))
5818 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005819 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005820 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005821 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005822 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005823 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005824 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005825 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005826 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005827
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005828 public BigPictureStyle() {
5829 }
5830
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005831 /**
5832 * @deprecated use {@code BigPictureStyle()}.
5833 */
5834 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005835 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005836 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005837 }
5838
Chris Wrend6297db2012-05-03 16:20:13 -04005839 /**
5840 * Overrides ContentTitle in the big form of the template.
5841 * This defaults to the value passed to setContentTitle().
5842 */
5843 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005844 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005845 return this;
5846 }
5847
5848 /**
5849 * Set the first line of text after the detail section in the big form of the template.
5850 */
5851 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005852 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005853 return this;
5854 }
5855
Chris Wren0bd664d2012-08-01 13:56:56 -04005856 /**
5857 * Provide the bitmap to be used as the payload for the BigPicture notification.
5858 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005859 public BigPictureStyle bigPicture(Bitmap b) {
5860 mPicture = b;
5861 return this;
5862 }
5863
Chris Wren3745a3d2012-05-22 15:11:52 -04005864 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005865 * Override the large icon when the big notification is shown.
5866 */
5867 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005868 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5869 }
5870
5871 /**
5872 * Override the large icon when the big notification is shown.
5873 */
5874 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005875 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005876 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005877 return this;
5878 }
5879
Riley Andrews0394a0c2015-11-03 23:36:52 -08005880 /** @hide */
5881 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5882
Daniel Sandler0ec46202015-06-24 01:27:05 -04005883 /**
5884 * @hide
5885 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005886 @Override
5887 public void purgeResources() {
5888 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005889 if (mPicture != null &&
5890 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005891 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005892 mPicture = mPicture.createAshmemBitmap();
5893 }
5894 if (mBigLargeIcon != null) {
5895 mBigLargeIcon.convertToAshmem();
5896 }
5897 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005898
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005899 /**
5900 * @hide
5901 */
Selim Cinekd0426622017-07-11 13:19:59 +02005902 @Override
5903 public void reduceImageSizes(Context context) {
5904 super.reduceImageSizes(context);
5905 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005906 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005907 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005908 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5909 ? R.dimen.notification_big_picture_max_height_low_ram
5910 : R.dimen.notification_big_picture_max_height);
5911 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5912 ? R.dimen.notification_big_picture_max_width_low_ram
5913 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005914 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5915 }
5916 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005917 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5918 ? R.dimen.notification_right_icon_size_low_ram
5919 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005920 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5921 }
5922 }
5923
5924 /**
5925 * @hide
5926 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005927 public RemoteViews makeBigContentView() {
5928 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005929 // This covers the following cases:
5930 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005931 // mN.mLargeIcon
5932 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005933 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005934 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005935 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005936 oldLargeIcon = mBuilder.mN.mLargeIcon;
5937 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005938 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5939 // replacement if the other one is null. Because we're restoring these legacy icons
5940 // for old listeners, this is in general non-null.
5941 largeIconLegacy = mBuilder.mN.largeIcon;
5942 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005943 }
5944
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005945 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005946 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005947 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5948 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005949 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005950 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005951 }
Selim Cinek279fa862016-06-14 10:57:25 -07005952 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005953
Christoph Studer5c510ee2014-12-15 16:32:27 +01005954 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005955 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005956 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005957 }
5958
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005959 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005960 return contentView;
5961 }
5962
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005963 /**
5964 * @hide
5965 */
5966 public void addExtras(Bundle extras) {
5967 super.addExtras(extras);
5968
5969 if (mBigLargeIconSet) {
5970 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5971 }
5972 extras.putParcelable(EXTRA_PICTURE, mPicture);
5973 }
5974
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005975 /**
5976 * @hide
5977 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005978 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005979 protected void restoreFromExtras(Bundle extras) {
5980 super.restoreFromExtras(extras);
5981
5982 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005983 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005984 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005985 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005986 mPicture = extras.getParcelable(EXTRA_PICTURE);
5987 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005988
5989 /**
5990 * @hide
5991 */
5992 @Override
5993 public boolean hasSummaryInHeader() {
5994 return false;
5995 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005996 }
5997
5998 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005999 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08006000 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006001 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006002 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006003 * Notification notif = new Notification.Builder(mContext)
6004 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6005 * .setContentText(subject)
6006 * .setSmallIcon(R.drawable.new_mail)
6007 * .setLargeIcon(aBitmap)
6008 * .setStyle(new Notification.BigTextStyle()
6009 * .bigText(aVeryLongString))
6010 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006011 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006012 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04006013 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006014 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006015 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02006016
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006017 private CharSequence mBigText;
6018
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006019 public BigTextStyle() {
6020 }
6021
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006022 /**
6023 * @deprecated use {@code BigTextStyle()}.
6024 */
6025 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006026 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006027 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006028 }
6029
Chris Wrend6297db2012-05-03 16:20:13 -04006030 /**
6031 * Overrides ContentTitle in the big form of the template.
6032 * This defaults to the value passed to setContentTitle().
6033 */
6034 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006035 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006036 return this;
6037 }
6038
6039 /**
6040 * Set the first line of text after the detail section in the big form of the template.
6041 */
6042 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006043 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006044 return this;
6045 }
6046
Chris Wren0bd664d2012-08-01 13:56:56 -04006047 /**
6048 * Provide the longer text to be displayed in the big form of the
6049 * template in place of the content text.
6050 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006051 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006052 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006053 return this;
6054 }
6055
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006056 /**
6057 * @hide
6058 */
6059 public void addExtras(Bundle extras) {
6060 super.addExtras(extras);
6061
Christoph Studer4600f9b2014-07-22 22:44:43 +02006062 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
6063 }
6064
6065 /**
6066 * @hide
6067 */
6068 @Override
6069 protected void restoreFromExtras(Bundle extras) {
6070 super.restoreFromExtras(extras);
6071
6072 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006073 }
6074
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006075 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08006076 * @param increasedHeight true if this layout be created with an increased height.
6077 *
6078 * @hide
6079 */
6080 @Override
6081 public RemoteViews makeContentView(boolean increasedHeight) {
6082 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006083 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006084 mBuilder.mActions = new ArrayList<>();
6085 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07006086 mBuilder.mActions = mBuilder.mOriginalActions;
6087 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08006088 return remoteViews;
6089 }
6090 return super.makeContentView(increasedHeight);
6091 }
6092
6093 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006094 * @hide
6095 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08006096 @Override
6097 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
6098 if (increasedHeight && mBuilder.mActions.size() > 0) {
6099 return makeBigContentView();
6100 }
6101 return super.makeHeadsUpContentView(increasedHeight);
6102 }
6103
6104 /**
6105 * @hide
6106 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006107 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006108
6109 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07006110 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006111 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04006112
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006113 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08006114
Selim Cinek75998782016-04-26 10:39:17 -07006115 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006116
Selim Cinek3a2c4b92015-12-17 17:01:17 -08006117 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07006118 if (TextUtils.isEmpty(bigTextText)) {
6119 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
6120 // experience
6121 bigTextText = mBuilder.processLegacyText(text);
6122 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07006123 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08006124
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006125 return contentView;
6126 }
6127
Adrian Roosb1f427c2016-05-26 12:27:15 -07006128 static void applyBigTextContentView(Builder builder,
6129 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07006130 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006131 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07006132 contentView.setViewVisibility(R.id.big_text,
6133 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07006134 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07006135 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04006136 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006137
6138 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006139 * Helper class for generating large-format notifications that include multiple back-and-forth
6140 * messages of varying types between any number of people.
6141 *
6142 * <br>
6143 * If the platform does not provide large-format notifications, this method has no effect. The
6144 * user will always see the normal notification view.
6145 * <br>
6146 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
6147 * so:
6148 * <pre class="prettyprint">
6149 *
6150 * Notification noti = new Notification.Builder()
6151 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
6152 * .setContentText(subject)
6153 * .setSmallIcon(R.drawable.new_message)
6154 * .setLargeIcon(aBitmap)
6155 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
6156 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
6157 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
6158 * .build();
6159 * </pre>
6160 */
6161 public static class MessagingStyle extends Style {
6162
6163 /**
6164 * The maximum number of messages that will be retained in the Notification itself (the
6165 * number displayed is up to the platform).
6166 */
6167 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
6168
Selim Cinekcb8b9852017-12-15 18:01:52 -08006169 @NonNull Person mUser;
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006170 @Nullable CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04006171 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08006172 List<Message> mHistoricMessages = new ArrayList<>();
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006173 boolean mIsGroupConversation;
Alex Hillsfc737de2016-03-23 17:33:02 -04006174
6175 MessagingStyle() {
6176 }
6177
6178 /**
Alex Hillsfd590442016-10-07 09:52:44 -04006179 * @param userDisplayName Required - the name to be displayed for any replies sent by the
6180 * user before the posting app reposts the notification with those messages after they've
6181 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04006182 * {@link #addMessage(Notification.MessagingStyle.Message)}
Selim Cinekcb8b9852017-12-15 18:01:52 -08006183 *
6184 * @deprecated use {@code MessagingStyle(Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006185 */
Alex Hillsfd590442016-10-07 09:52:44 -04006186 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006187 this(new Person().setName(userDisplayName));
6188 }
6189
6190 /**
6191 * @param user Required - The person displayed for any messages that are sent by the
6192 * user. Any messages added with {@link #addMessage(Notification.MessagingStyle.Message)}
6193 * who don't have a Person associated with it will be displayed as if they were sent
6194 * by this user. The user also needs to have a valid name associated with it.
6195 */
6196 public MessagingStyle(@NonNull Person user) {
6197 mUser = user;
6198 if (user == null || user.getName() == null) {
6199 throw new RuntimeException("user must be valid and have a name");
6200 }
6201 }
6202
6203 /**
6204 * @return the user to be displayed for any replies sent by the user
6205 */
6206 public Person getUser() {
6207 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006208 }
6209
6210 /**
6211 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006212 *
6213 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006214 */
6215 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006216 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006217 }
6218
6219 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006220 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6221 *
Kodlee Yin14656422017-12-22 17:00:46 -08006222 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6223 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6224 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6225 * {@code true} and passing {@code null} will make it return {@code false}. In
6226 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6227 * to set group conversation status.
6228 *
6229 * @param conversationTitle Title displayed for this conversation
6230 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006231 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006232 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006233 mConversationTitle = conversationTitle;
6234 return this;
6235 }
6236
6237 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006238 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006239 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006240 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006241 public CharSequence getConversationTitle() {
6242 return mConversationTitle;
6243 }
6244
6245 /**
6246 * Adds a message for display by this notification. Convenience call for a simple
6247 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6248 * @param text A {@link CharSequence} to be displayed as the message content
6249 * @param timestamp Time at which the message arrived
6250 * @param sender A {@link CharSequence} to be used for displaying the name of the
6251 * sender. Should be <code>null</code> for messages by the current user, in which case
6252 * the platform will insert {@link #getUserDisplayName()}.
6253 * Should be unique amongst all individuals in the conversation, and should be
6254 * consistent during re-posts of the notification.
6255 *
6256 * @see Message#Message(CharSequence, long, CharSequence)
6257 *
6258 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006259 *
6260 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006261 */
6262 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006263 return addMessage(text, timestamp,
6264 sender == null ? null : new Person().setName(sender));
6265 }
6266
6267 /**
6268 * Adds a message for display by this notification. Convenience call for a simple
6269 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6270 * @param text A {@link CharSequence} to be displayed as the message content
6271 * @param timestamp Time at which the message arrived
6272 * @param sender The {@link Person} who sent the message.
6273 * Should be <code>null</code> for messages by the current user, in which case
6274 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6275 *
6276 * @see Message#Message(CharSequence, long, CharSequence)
6277 *
6278 * @return this object for method chaining
6279 */
6280 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006281 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006282 }
6283
6284 /**
6285 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006286 *
6287 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6288 * the newest last.
6289 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006290 * @param message The {@link Message} to be displayed
6291 * @return this object for method chaining
6292 */
6293 public MessagingStyle addMessage(Message message) {
6294 mMessages.add(message);
6295 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6296 mMessages.remove(0);
6297 }
6298 return this;
6299 }
6300
6301 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006302 * Adds a {@link Message} for historic context in this notification.
6303 *
6304 * <p>Messages should be added as historic if they are not the main subject of the
6305 * notification but may give context to a conversation. The system may choose to present
6306 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6307 *
6308 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6309 * the newest last.
6310 *
6311 * @param message The historic {@link Message} to be added
6312 * @return this object for method chaining
6313 */
6314 public MessagingStyle addHistoricMessage(Message message) {
6315 mHistoricMessages.add(message);
6316 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6317 mHistoricMessages.remove(0);
6318 }
6319 return this;
6320 }
6321
6322 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006323 * Gets the list of {@code Message} objects that represent the notification
6324 */
6325 public List<Message> getMessages() {
6326 return mMessages;
6327 }
6328
6329 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006330 * Gets the list of historic {@code Message}s in the notification.
6331 */
6332 public List<Message> getHistoricMessages() {
6333 return mHistoricMessages;
6334 }
6335
6336 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006337 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006338 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006339 * @param isGroupConversation {@code true} if the conversation represents a group,
6340 * {@code false} otherwise.
6341 * @return this object for method chaining
6342 */
6343 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6344 mIsGroupConversation = isGroupConversation;
6345 return this;
6346 }
6347
6348 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006349 * Returns {@code true} if this notification represents a group conversation, otherwise
6350 * {@code false}.
6351 *
6352 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6353 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6354 * not the conversation title is set; returning {@code true} if the conversation title is
6355 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6356 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6357 * named, non-group conversations.
6358 *
6359 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006360 */
6361 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006362 // When target SDK version is < P, a non-null conversation title dictates if this is
6363 // as group conversation.
6364 if (mBuilder != null
6365 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6366 < Build.VERSION_CODES.P) {
6367 return mConversationTitle != null;
6368 }
6369
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006370 return mIsGroupConversation;
6371 }
6372
6373 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006374 * @hide
6375 */
6376 @Override
6377 public void addExtras(Bundle extras) {
6378 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006379 if (mUser != null) {
6380 // For legacy usages
6381 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6382 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006383 }
6384 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006385 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006386 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006387 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6388 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006389 }
Adrian Roos437cd562017-01-18 15:47:03 -08006390 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6391 Message.getBundleArrayForMessages(mHistoricMessages));
6392 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006393
6394 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006395 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006396 }
6397
6398 private void fixTitleAndTextExtras(Bundle extras) {
6399 Message m = findLatestIncomingMessage();
6400 CharSequence text = (m == null) ? null : m.mText;
6401 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006402 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6403 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006404 CharSequence title;
6405 if (!TextUtils.isEmpty(mConversationTitle)) {
6406 if (!TextUtils.isEmpty(sender)) {
6407 BidiFormatter bidi = BidiFormatter.getInstance();
6408 title = mBuilder.mContext.getString(
6409 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006410 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006411 } else {
6412 title = mConversationTitle;
6413 }
6414 } else {
6415 title = sender;
6416 }
6417
6418 if (title != null) {
6419 extras.putCharSequence(EXTRA_TITLE, title);
6420 }
6421 if (text != null) {
6422 extras.putCharSequence(EXTRA_TEXT, text);
6423 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006424 }
6425
6426 /**
6427 * @hide
6428 */
6429 @Override
6430 protected void restoreFromExtras(Bundle extras) {
6431 super.restoreFromExtras(extras);
6432
Selim Cinekcb8b9852017-12-15 18:01:52 -08006433 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6434 if (mUser == null) {
6435 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6436 mUser = new Person().setName(displayName);
6437 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006438 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006439 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006440 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006441 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006442 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006443 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006444 }
6445
6446 /**
6447 * @hide
6448 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006449 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006450 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006451 mBuilder.mOriginalActions = mBuilder.mActions;
6452 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006453 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006454 mBuilder.mActions = mBuilder.mOriginalActions;
6455 mBuilder.mOriginalActions = null;
6456 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006457 }
6458
6459 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006460 return findLatestIncomingMessage(mMessages);
6461 }
6462
6463 /**
6464 * @hide
6465 */
6466 @Nullable
6467 public static Message findLatestIncomingMessage(
6468 List<Message> messages) {
6469 for (int i = messages.size() - 1; i >= 0; i--) {
6470 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006471 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006472 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006473 return m;
6474 }
6475 }
Selim Cinek88188f22017-09-19 16:46:56 -07006476 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006477 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006478 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006479 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006480 return null;
6481 }
6482
6483 /**
6484 * @hide
6485 */
6486 @Override
6487 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006488 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006489 }
6490
6491 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006492 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006493 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006494 ? super.mBigContentTitle
6495 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006496 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006497 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006498 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006499 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006500 nameReplacement = conversationTitle;
6501 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006502 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006503 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006504 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006505 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006506 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006507 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006508 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006509 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006510 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006511 // also update the end margin if there is an image
6512 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006513 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006514 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6515 }
6516 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006517 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6518 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006519 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6520 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006521 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6522 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006523 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6524 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006525 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6526 isOneToOne);
6527 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6528 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006529 return contentView;
6530 }
6531
Selim Cinekf7409db2017-10-24 16:17:14 -07006532 private boolean hasOnlyWhiteSpaceSenders() {
6533 for (int i = 0; i < mMessages.size(); i++) {
6534 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006535 Person sender = m.getSenderPerson();
6536 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006537 return false;
6538 }
6539 }
6540 return true;
6541 }
6542
6543 private boolean isWhiteSpace(CharSequence sender) {
6544 if (TextUtils.isEmpty(sender)) {
6545 return true;
6546 }
6547 if (sender.toString().matches("^\\s*$")) {
6548 return true;
6549 }
6550 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6551 // For the presentation that we had.
6552 for (int i = 0; i < sender.length(); i++) {
6553 char c = sender.charAt(i);
6554 if (c != '\u200B') {
6555 return false;
6556 }
6557 }
6558 return true;
6559 }
6560
Selim Cinek88188f22017-09-19 16:46:56 -07006561 private CharSequence createConversationTitleFromMessages() {
6562 ArraySet<CharSequence> names = new ArraySet<>();
6563 for (int i = 0; i < mMessages.size(); i++) {
6564 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006565 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006566 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006567 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006568 }
6569 }
6570 SpannableStringBuilder title = new SpannableStringBuilder();
6571 int size = names.size();
6572 for (int i = 0; i < size; i++) {
6573 CharSequence name = names.valueAt(i);
6574 if (!TextUtils.isEmpty(title)) {
6575 title.append(", ");
6576 }
6577 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6578 }
6579 return title;
6580 }
6581
Adrian Roosdedd1df2016-04-26 16:38:47 -07006582 /**
6583 * @hide
6584 */
6585 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006586 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006587 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006588 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6589 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006590 }
6591
Adrian Roosc1a80b02016-04-05 14:54:55 -07006592 private static TextAppearanceSpan makeFontColorSpan(int color) {
6593 return new TextAppearanceSpan(null, 0, 0,
6594 ColorStateList.valueOf(color), null);
6595 }
6596
Alex Hillsd9b04d92016-04-11 16:38:16 -04006597 public static final class Message {
6598
6599 static final String KEY_TEXT = "text";
6600 static final String KEY_TIMESTAMP = "time";
6601 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006602 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006603 static final String KEY_DATA_MIME_TYPE = "type";
6604 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006605 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006606
6607 private final CharSequence mText;
6608 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006609 @Nullable
6610 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006611
Shane Brennan5a871862017-03-11 13:14:17 -08006612 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006613 private String mDataMimeType;
6614 private Uri mDataUri;
6615
6616 /**
6617 * Constructor
6618 * @param text A {@link CharSequence} to be displayed as the message content
6619 * @param timestamp Time at which the message arrived
6620 * @param sender A {@link CharSequence} to be used for displaying the name of the
6621 * sender. Should be <code>null</code> for messages by the current user, in which case
6622 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6623 * Should be unique amongst all individuals in the conversation, and should be
6624 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006625 *
6626 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006627 */
6628 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006629 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6630 }
6631
6632 /**
6633 * Constructor
6634 * @param text A {@link CharSequence} to be displayed as the message content
6635 * @param timestamp Time at which the message arrived
6636 * @param sender The {@link Person} who sent the message.
6637 * Should be <code>null</code> for messages by the current user, in which case
6638 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6639 * <p>
6640 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6641 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6642 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6643 * in order to differentiate between the different users.
6644 * </p>
6645 */
6646 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006647 mText = text;
6648 mTimestamp = timestamp;
6649 mSender = sender;
6650 }
6651
6652 /**
6653 * Sets a binary blob of data and an associated MIME type for a message. In the case
6654 * where the platform doesn't support the MIME type, the original text provided in the
6655 * constructor will be used.
6656 * @param dataMimeType The MIME type of the content. See
6657 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6658 * types on Android and Android Wear.
6659 * @param dataUri The uri containing the content whose type is given by the MIME type.
6660 * <p class="note">
6661 * <ol>
6662 * <li>Notification Listeners including the System UI need permission to access the
6663 * data the Uri points to. The recommended ways to do this are:</li>
6664 * <li>Store the data in your own ContentProvider, making sure that other apps have
6665 * the correct permission to access your provider. The preferred mechanism for
6666 * providing access is to use per-URI permissions which are temporary and only
6667 * grant access to the receiving application. An easy way to create a
6668 * ContentProvider like this is to use the FileProvider helper class.</li>
6669 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6670 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6671 * also store non-media types (see MediaStore.Files for more info). Files can be
6672 * inserted into the MediaStore using scanFile() after which a content:// style
6673 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6674 * Note that once added to the system MediaStore the content is accessible to any
6675 * app on the device.</li>
6676 * </ol>
6677 * @return this object for method chaining
6678 */
6679 public Message setData(String dataMimeType, Uri dataUri) {
6680 mDataMimeType = dataMimeType;
6681 mDataUri = dataUri;
6682 return this;
6683 }
6684
Alex Hillsfc737de2016-03-23 17:33:02 -04006685 /**
6686 * Get the text to be used for this message, or the fallback text if a type and content
6687 * Uri have been set
6688 */
6689 public CharSequence getText() {
6690 return mText;
6691 }
6692
6693 /**
6694 * Get the time at which this message arrived
6695 */
6696 public long getTimestamp() {
6697 return mTimestamp;
6698 }
6699
6700 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006701 * Get the extras Bundle for this message.
6702 */
6703 public Bundle getExtras() {
6704 return mExtras;
6705 }
6706
6707 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006708 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006709 *
6710 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006711 */
6712 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006713 return mSender == null ? null : mSender.getName();
6714 }
6715
6716 /**
6717 * Get the sender associated with this message.
6718 */
6719 @Nullable
6720 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006721 return mSender;
6722 }
6723
6724 /**
6725 * Get the MIME type of the data pointed to by the Uri
6726 */
6727 public String getDataMimeType() {
6728 return mDataMimeType;
6729 }
6730
6731 /**
6732 * Get the the Uri pointing to the content of the message. Can be null, in which case
6733 * {@see #getText()} is used.
6734 */
6735 public Uri getDataUri() {
6736 return mDataUri;
6737 }
6738
Alex Hillsd9b04d92016-04-11 16:38:16 -04006739 private Bundle toBundle() {
6740 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006741 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006742 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006743 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006744 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006745 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006746 // Legacy listeners need this
6747 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6748 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006749 }
6750 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006751 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006752 }
6753 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006754 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006755 }
Shane Brennan5a871862017-03-11 13:14:17 -08006756 if (mExtras != null) {
6757 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6758 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006759 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006760 }
6761
Alex Hillsd9b04d92016-04-11 16:38:16 -04006762 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6763 Bundle[] bundles = new Bundle[messages.size()];
6764 final int N = messages.size();
6765 for (int i = 0; i < N; i++) {
6766 bundles[i] = messages.get(i).toBundle();
6767 }
6768 return bundles;
6769 }
6770
Selim Cinek88188f22017-09-19 16:46:56 -07006771 /**
6772 * @return A list of messages read from the bundles.
6773 *
6774 * @hide
6775 */
6776 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6777 if (bundles == null) {
6778 return new ArrayList<>();
6779 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006780 List<Message> messages = new ArrayList<>(bundles.length);
6781 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006782 if (bundles[i] instanceof Bundle) {
6783 Message message = getMessageFromBundle((Bundle)bundles[i]);
6784 if (message != null) {
6785 messages.add(message);
6786 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006787 }
6788 }
6789 return messages;
6790 }
6791
6792 static Message getMessageFromBundle(Bundle bundle) {
6793 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006794 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006795 return null;
6796 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006797
6798 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6799 if (senderPerson == null) {
6800 // Legacy apps that use compat don't actually provide the sender objects
6801 // We need to fix the compat version to provide people / use
6802 // the native api instead
6803 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6804 if (senderName != null) {
6805 senderPerson = new Person().setName(senderName);
6806 }
6807 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006808 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006809 bundle.getLong(KEY_TIMESTAMP),
6810 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006811 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6812 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006813 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6814 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006815 }
Shane Brennan5a871862017-03-11 13:14:17 -08006816 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6817 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6818 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006819 return message;
6820 }
6821 } catch (ClassCastException e) {
6822 return null;
6823 }
6824 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006825 }
6826 }
6827
6828 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006829 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006830 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006831 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006832 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006833 * Notification notif = new Notification.Builder(mContext)
6834 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6835 * .setContentText(subject)
6836 * .setSmallIcon(R.drawable.new_mail)
6837 * .setLargeIcon(aBitmap)
6838 * .setStyle(new Notification.InboxStyle()
6839 * .addLine(str1)
6840 * .addLine(str2)
6841 * .setContentTitle(&quot;&quot;)
6842 * .setSummaryText(&quot;+3 more&quot;))
6843 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006844 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006845 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006846 * @see Notification#bigContentView
6847 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006848 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006849 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6850
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006851 public InboxStyle() {
6852 }
6853
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006854 /**
6855 * @deprecated use {@code InboxStyle()}.
6856 */
6857 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006858 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006859 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006860 }
6861
Chris Wrend6297db2012-05-03 16:20:13 -04006862 /**
6863 * Overrides ContentTitle in the big form of the template.
6864 * This defaults to the value passed to setContentTitle().
6865 */
6866 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006867 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006868 return this;
6869 }
6870
6871 /**
6872 * Set the first line of text after the detail section in the big form of the template.
6873 */
6874 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006875 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006876 return this;
6877 }
6878
Chris Wren0bd664d2012-08-01 13:56:56 -04006879 /**
6880 * Append a line to the digest section of the Inbox notification.
6881 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006882 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006883 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006884 return this;
6885 }
6886
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006887 /**
6888 * @hide
6889 */
6890 public void addExtras(Bundle extras) {
6891 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006892
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006893 CharSequence[] a = new CharSequence[mTexts.size()];
6894 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6895 }
6896
Christoph Studer4600f9b2014-07-22 22:44:43 +02006897 /**
6898 * @hide
6899 */
6900 @Override
6901 protected void restoreFromExtras(Bundle extras) {
6902 super.restoreFromExtras(extras);
6903
6904 mTexts.clear();
6905 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6906 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6907 }
6908 }
6909
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006910 /**
6911 * @hide
6912 */
6913 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006914 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006915 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006916 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6917 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006918
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006919 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006920
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006921 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006922
Chris Wrend6297db2012-05-03 16:20:13 -04006923 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 -04006924 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006925
Chris Wren4ed80d52012-05-17 09:30:03 -04006926 // Make sure all rows are gone in case we reuse a view.
6927 for (int rowId : rowIds) {
6928 contentView.setViewVisibility(rowId, View.GONE);
6929 }
6930
Daniel Sandler879c5e02012-04-17 16:46:51 -04006931 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006932 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6933 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006934 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006935 int onlyViewId = 0;
6936 int maxRows = rowIds.length;
6937 if (mBuilder.mActions.size() > 0) {
6938 maxRows--;
6939 }
6940 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006941 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006942 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006943 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006944 contentView.setTextViewText(rowIds[i],
6945 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006946 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006947 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006948 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006949 if (first) {
6950 onlyViewId = rowIds[i];
6951 } else {
6952 onlyViewId = 0;
6953 }
Selim Cinek247fa012016-02-18 09:50:48 -08006954 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006955 }
6956 i++;
6957 }
Selim Cinek07c80172016-04-21 16:40:47 -07006958 if (onlyViewId != 0) {
6959 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6960 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6961 R.dimen.notification_text_margin_top);
6962 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6963 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006964
Daniel Sandler879c5e02012-04-17 16:46:51 -04006965 return contentView;
6966 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006967
Selim Cinek247fa012016-02-18 09:50:48 -08006968 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006969 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006970 if (first) {
6971 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6972 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6973 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006974 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006975 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006976 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006977 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006978 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006979 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006980 }
Dan Sandler842dd772014-05-15 09:36:47 -04006981
6982 /**
6983 * Notification style for media playback notifications.
6984 *
6985 * In the expanded form, {@link Notification#bigContentView}, up to 5
6986 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006987 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006988 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6989 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6990 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006991 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006992 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6993 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006994 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006995 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006996 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006997 * Notifications created with MediaStyle will have their category set to
6998 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6999 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007000 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007001 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7002 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007003 * the System UI can identify this as a notification representing an active media session
7004 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7005 *
Selim Cinek99104832017-01-25 14:47:33 -08007006 * <p>
7007 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7008 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7009 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7010 * <p>
7011 *
Dan Sandler842dd772014-05-15 09:36:47 -04007012 * To use this style with your Notification, feed it to
7013 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7014 * <pre class="prettyprint">
7015 * Notification noti = new Notification.Builder()
7016 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007017 * .setContentTitle(&quot;Track title&quot;)
7018 * .setContentText(&quot;Artist - Album&quot;)
7019 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007020 * .setStyle(<b>new Notification.MediaStyle()</b>
7021 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007022 * .build();
7023 * </pre>
7024 *
7025 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007026 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007027 */
7028 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007029 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007030 static final int MAX_MEDIA_BUTTONS = 5;
7031
7032 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007033 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007034
7035 public MediaStyle() {
7036 }
7037
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007038 /**
7039 * @deprecated use {@code MediaStyle()}.
7040 */
7041 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007042 public MediaStyle(Builder builder) {
7043 setBuilder(builder);
7044 }
7045
7046 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007047 * 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 -04007048 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007049 *
7050 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007051 */
7052 public MediaStyle setShowActionsInCompactView(int...actions) {
7053 mActionsToShowInCompact = actions;
7054 return this;
7055 }
7056
7057 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007058 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7059 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007060 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007061 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007062 mToken = token;
7063 return this;
7064 }
7065
Christoph Studer4600f9b2014-07-22 22:44:43 +02007066 /**
7067 * @hide
7068 */
Dan Sandler842dd772014-05-15 09:36:47 -04007069 @Override
7070 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007071 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007072 if (wip.category == null) {
7073 wip.category = Notification.CATEGORY_TRANSPORT;
7074 }
Dan Sandler842dd772014-05-15 09:36:47 -04007075 return wip;
7076 }
7077
Christoph Studer4600f9b2014-07-22 22:44:43 +02007078 /**
7079 * @hide
7080 */
7081 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007082 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007083 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007084 }
7085
7086 /**
7087 * @hide
7088 */
7089 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007090 public RemoteViews makeBigContentView() {
7091 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007092 }
7093
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007094 /**
7095 * @hide
7096 */
7097 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007098 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007099 RemoteViews expanded = makeMediaBigContentView();
7100 return expanded != null ? expanded : makeMediaContentView();
7101 }
7102
Dan Sandler842dd772014-05-15 09:36:47 -04007103 /** @hide */
7104 @Override
7105 public void addExtras(Bundle extras) {
7106 super.addExtras(extras);
7107
7108 if (mToken != null) {
7109 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7110 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007111 if (mActionsToShowInCompact != null) {
7112 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7113 }
Dan Sandler842dd772014-05-15 09:36:47 -04007114 }
7115
Christoph Studer4600f9b2014-07-22 22:44:43 +02007116 /**
7117 * @hide
7118 */
7119 @Override
7120 protected void restoreFromExtras(Bundle extras) {
7121 super.restoreFromExtras(extras);
7122
7123 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7124 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7125 }
7126 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7127 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7128 }
7129 }
7130
Selim Cinek5bf069a2015-11-10 19:14:27 -05007131 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007132 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007133 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007134 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007135 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007136
7137 // If the action buttons should not be tinted, then just use the default
7138 // notification color. Otherwise, just use the passed-in color.
7139 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7140 ? color
7141 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7142 Notification.COLOR_DEFAULT);
7143
Sunny Goyal5b153922017-09-21 21:00:36 -07007144 button.setDrawableTint(R.id.action0, false, tintColor,
7145 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007146 if (!tombstone) {
7147 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7148 }
7149 button.setContentDescription(R.id.action0, action.title);
7150 return button;
7151 }
7152
7153 private RemoteViews makeMediaContentView() {
7154 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007155 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007156
7157 final int numActions = mBuilder.mActions.size();
7158 final int N = mActionsToShowInCompact == null
7159 ? 0
7160 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7161 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007162 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007163 for (int i = 0; i < N; i++) {
7164 if (i >= numActions) {
7165 throw new IllegalArgumentException(String.format(
7166 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7167 i, numActions - 1));
7168 }
7169
7170 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007171 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007172 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007173 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007174 }
7175 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007176 handleImage(view);
7177 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007178 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007179 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007180 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007181 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007182 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007183 return view;
7184 }
7185
Selim Cinek99104832017-01-25 14:47:33 -08007186 private int getPrimaryHighlightColor() {
7187 return mBuilder.getPrimaryHighlightColor();
7188 }
7189
Dan Sandler842dd772014-05-15 09:36:47 -04007190 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007191 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007192 // Dont add an expanded view if there is no more content to be revealed
7193 int actionsInCompact = mActionsToShowInCompact == null
7194 ? 0
7195 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007196 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007197 return null;
7198 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007199 RemoteViews big = mBuilder.applyStandardTemplate(
7200 R.layout.notification_template_material_big_media,
7201 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007202
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007203 if (actionCount > 0) {
7204 big.removeAllViews(com.android.internal.R.id.media_actions);
7205 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007206 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007207 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007208 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007209 }
7210 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007211 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007212 return big;
7213 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007214
Selim Cinek5bf069a2015-11-10 19:14:27 -05007215 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007216 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007217 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7218 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007219 }
7220 }
7221
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007222 /**
7223 * @hide
7224 */
7225 @Override
7226 protected boolean hasProgress() {
7227 return false;
7228 }
Dan Sandler842dd772014-05-15 09:36:47 -04007229 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007230
Selim Cinek593610c2016-02-16 18:42:57 -08007231 /**
7232 * Notification style for custom views that are decorated by the system
7233 *
7234 * <p>Instead of providing a notification that is completely custom, a developer can set this
7235 * style and still obtain system decorations like the notification header with the expand
7236 * affordance and actions.
7237 *
7238 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7239 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7240 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7241 * corresponding custom views to display.
7242 *
7243 * To use this style with your Notification, feed it to
7244 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7245 * <pre class="prettyprint">
7246 * Notification noti = new Notification.Builder()
7247 * .setSmallIcon(R.drawable.ic_stat_player)
7248 * .setLargeIcon(albumArtBitmap))
7249 * .setCustomContentView(contentView);
7250 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7251 * .build();
7252 * </pre>
7253 */
7254 public static class DecoratedCustomViewStyle extends Style {
7255
7256 public DecoratedCustomViewStyle() {
7257 }
7258
Selim Cinek593610c2016-02-16 18:42:57 -08007259 /**
7260 * @hide
7261 */
7262 public boolean displayCustomViewInline() {
7263 return true;
7264 }
7265
7266 /**
7267 * @hide
7268 */
7269 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007270 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007271 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7272 }
7273
7274 /**
7275 * @hide
7276 */
7277 @Override
7278 public RemoteViews makeBigContentView() {
7279 return makeDecoratedBigContentView();
7280 }
7281
7282 /**
7283 * @hide
7284 */
7285 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007286 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007287 return makeDecoratedHeadsUpContentView();
7288 }
7289
Selim Cinek593610c2016-02-16 18:42:57 -08007290 private RemoteViews makeDecoratedHeadsUpContentView() {
7291 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7292 ? mBuilder.mN.contentView
7293 : mBuilder.mN.headsUpContentView;
7294 if (mBuilder.mActions.size() == 0) {
7295 return makeStandardTemplateWithCustomContent(headsUpContentView);
7296 }
7297 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7298 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007299 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007300 return remoteViews;
7301 }
7302
Selim Cinek593610c2016-02-16 18:42:57 -08007303 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7304 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7305 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007306 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007307 return remoteViews;
7308 }
7309
Selim Cinek593610c2016-02-16 18:42:57 -08007310 private RemoteViews makeDecoratedBigContentView() {
7311 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7312 ? mBuilder.mN.contentView
7313 : mBuilder.mN.bigContentView;
7314 if (mBuilder.mActions.size() == 0) {
7315 return makeStandardTemplateWithCustomContent(bigContentView);
7316 }
7317 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7318 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007319 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007320 return remoteViews;
7321 }
Selim Cinek247fa012016-02-18 09:50:48 -08007322
7323 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7324 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007325 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007326 // Need to clone customContent before adding, because otherwise it can no longer be
7327 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007328 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007329 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7330 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007331 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007332 }
Selim Cinek247fa012016-02-18 09:50:48 -08007333 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007334 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007335 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007336 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007337 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007338 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007339 }
Selim Cinek593610c2016-02-16 18:42:57 -08007340 }
7341
Selim Cinek03eb3b72016-02-18 10:39:45 -08007342 /**
7343 * Notification style for media custom views that are decorated by the system
7344 *
7345 * <p>Instead of providing a media notification that is completely custom, a developer can set
7346 * this style and still obtain system decorations like the notification header with the expand
7347 * affordance and actions.
7348 *
7349 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7350 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7351 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7352 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007353 * <p>
7354 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7355 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7356 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007357 * To use this style with your Notification, feed it to
7358 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7359 * <pre class="prettyprint">
7360 * Notification noti = new Notification.Builder()
7361 * .setSmallIcon(R.drawable.ic_stat_player)
7362 * .setLargeIcon(albumArtBitmap))
7363 * .setCustomContentView(contentView);
7364 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7365 * .setMediaSession(mySession))
7366 * .build();
7367 * </pre>
7368 *
7369 * @see android.app.Notification.DecoratedCustomViewStyle
7370 * @see android.app.Notification.MediaStyle
7371 */
7372 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7373
7374 public DecoratedMediaCustomViewStyle() {
7375 }
7376
Selim Cinek03eb3b72016-02-18 10:39:45 -08007377 /**
7378 * @hide
7379 */
7380 public boolean displayCustomViewInline() {
7381 return true;
7382 }
7383
7384 /**
7385 * @hide
7386 */
7387 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007388 public RemoteViews makeContentView(boolean increasedHeight) {
7389 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007390 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7391 mBuilder.mN.contentView);
7392 }
7393
7394 /**
7395 * @hide
7396 */
7397 @Override
7398 public RemoteViews makeBigContentView() {
7399 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7400 ? mBuilder.mN.bigContentView
7401 : mBuilder.mN.contentView;
7402 return makeBigContentViewWithCustomContent(customRemoteView);
7403 }
7404
7405 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7406 RemoteViews remoteViews = super.makeBigContentView();
7407 if (remoteViews != null) {
7408 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7409 customRemoteView);
7410 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007411 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007412 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7413 customRemoteView);
7414 } else {
7415 return null;
7416 }
7417 }
7418
7419 /**
7420 * @hide
7421 */
7422 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007423 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007424 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7425 ? mBuilder.mN.headsUpContentView
7426 : mBuilder.mN.contentView;
7427 return makeBigContentViewWithCustomContent(customRemoteView);
7428 }
7429
7430 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7431 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007432 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007433 // Need to clone customContent before adding, because otherwise it can no longer be
7434 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007435 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007436 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007437 remoteViews.removeAllViews(id);
7438 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007439 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007440 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007441 return remoteViews;
7442 }
7443 }
7444
Selim Cineke7238dd2017-12-14 17:48:32 -08007445 /**
7446 * A Person associated with this Notification.
7447 */
7448 public static final class Person implements Parcelable {
7449 @Nullable private CharSequence mName;
7450 @Nullable private Icon mIcon;
7451 @Nullable private String mUri;
7452 @Nullable private String mKey;
7453
7454 protected Person(Parcel in) {
7455 mName = in.readCharSequence();
7456 if (in.readInt() != 0) {
7457 mIcon = Icon.CREATOR.createFromParcel(in);
7458 }
7459 mUri = in.readString();
7460 mKey = in.readString();
7461 }
7462
7463 /**
7464 * Create a new person.
7465 */
7466 public Person() {
7467 }
7468
7469 /**
7470 * Give this person a name.
7471 *
7472 * @param name the name of this person
7473 */
7474 public Person setName(@Nullable CharSequence name) {
7475 this.mName = name;
7476 return this;
7477 }
7478
7479 /**
7480 * Add an icon for this person.
7481 * <br />
7482 * This is currently only used for {@link MessagingStyle} notifications and should not be
7483 * provided otherwise, in order to save memory. The system will prefer this icon over any
7484 * images that are resolved from the URI.
7485 *
7486 * @param icon the icon of the person
7487 */
7488 public Person setIcon(@Nullable Icon icon) {
7489 this.mIcon = icon;
7490 return this;
7491 }
7492
7493 /**
7494 * Set a URI associated with this person.
7495 *
7496 * <P>
7497 * Depending on user preferences, adding a URI to a Person may allow the notification to
7498 * pass through interruption filters, if this notification is of
7499 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7500 * The addition of people may also cause this notification to appear more prominently in
7501 * the user interface.
7502 * </P>
7503 *
7504 * <P>
7505 * The person should be specified by the {@code String} representation of a
7506 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7507 * </P>
7508 *
7509 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7510 * URIs. The path part of these URIs must exist in the contacts database, in the
7511 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7512 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7513 * </P>
7514 *
7515 * @param uri a URI for the person
7516 */
7517 public Person setUri(@Nullable String uri) {
7518 mUri = uri;
7519 return this;
7520 }
7521
7522 /**
7523 * Add a key to this person in order to uniquely identify it.
7524 * This is especially useful if the name doesn't uniquely identify this person or if the
7525 * display name is a short handle of the actual name.
7526 *
7527 * <P>If no key is provided, the name serves as as the key for the purpose of
7528 * identification.</P>
7529 *
7530 * @param key the key that uniquely identifies this person
7531 */
7532 public Person setKey(@Nullable String key) {
7533 mKey = key;
7534 return this;
7535 }
7536
7537
7538 /**
7539 * @return the uri provided for this person or {@code null} if no Uri was provided
7540 */
7541 @Nullable
7542 public String getUri() {
7543 return mUri;
7544 }
7545
7546 /**
7547 * @return the name provided for this person or {@code null} if no name was provided
7548 */
7549 @Nullable
7550 public CharSequence getName() {
7551 return mName;
7552 }
7553
7554 /**
7555 * @return the icon provided for this person or {@code null} if no icon was provided
7556 */
7557 @Nullable
7558 public Icon getIcon() {
7559 return mIcon;
7560 }
7561
7562 /**
7563 * @return the key provided for this person or {@code null} if no key was provided
7564 */
7565 @Nullable
7566 public String getKey() {
7567 return mKey;
7568 }
7569
7570 /**
7571 * @return the URI associated with this person, or "name:mName" otherwise
7572 * @hide
7573 */
7574 public String resolveToLegacyUri() {
7575 if (mUri != null) {
7576 return mUri;
7577 }
7578 if (mName != null) {
7579 return "name:" + mName;
7580 }
7581 return "";
7582 }
7583
7584 @Override
7585 public int describeContents() {
7586 return 0;
7587 }
7588
7589 @Override
7590 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7591 dest.writeCharSequence(mName);
7592 if (mIcon != null) {
7593 dest.writeInt(1);
7594 mIcon.writeToParcel(dest, 0);
7595 } else {
7596 dest.writeInt(0);
7597 }
7598 dest.writeString(mUri);
7599 dest.writeString(mKey);
7600 }
7601
7602 public static final Creator<Person> CREATOR = new Creator<Person>() {
7603 @Override
7604 public Person createFromParcel(Parcel in) {
7605 return new Person(in);
7606 }
7607
7608 @Override
7609 public Person[] newArray(int size) {
7610 return new Person[size];
7611 }
7612 };
7613 }
7614
Christoph Studer4600f9b2014-07-22 22:44:43 +02007615 // When adding a new Style subclass here, don't forget to update
7616 // Builder.getNotificationStyleClass.
7617
Griff Hazen61a9e862014-05-22 16:05:19 -07007618 /**
7619 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7620 * metadata or change options on a notification builder.
7621 */
7622 public interface Extender {
7623 /**
7624 * Apply this extender to a notification builder.
7625 * @param builder the builder to be modified.
7626 * @return the build object for chaining.
7627 */
7628 public Builder extend(Builder builder);
7629 }
7630
7631 /**
7632 * Helper class to add wearable extensions to notifications.
7633 * <p class="note"> See
7634 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7635 * for Android Wear</a> for more information on how to use this class.
7636 * <p>
7637 * To create a notification with wearable extensions:
7638 * <ol>
7639 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7640 * properties.
7641 * <li>Create a {@link android.app.Notification.WearableExtender}.
7642 * <li>Set wearable-specific properties using the
7643 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7644 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7645 * notification.
7646 * <li>Post the notification to the notification system with the
7647 * {@code NotificationManager.notify(...)} methods.
7648 * </ol>
7649 *
7650 * <pre class="prettyprint">
7651 * Notification notif = new Notification.Builder(mContext)
7652 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7653 * .setContentText(subject)
7654 * .setSmallIcon(R.drawable.new_mail)
7655 * .extend(new Notification.WearableExtender()
7656 * .setContentIcon(R.drawable.new_mail))
7657 * .build();
7658 * NotificationManager notificationManger =
7659 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7660 * notificationManger.notify(0, notif);</pre>
7661 *
7662 * <p>Wearable extensions can be accessed on an existing notification by using the
7663 * {@code WearableExtender(Notification)} constructor,
7664 * and then using the {@code get} methods to access values.
7665 *
7666 * <pre class="prettyprint">
7667 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7668 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007669 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007670 */
7671 public static final class WearableExtender implements Extender {
7672 /**
7673 * Sentinel value for an action index that is unset.
7674 */
7675 public static final int UNSET_ACTION_INDEX = -1;
7676
7677 /**
7678 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7679 * default sizing.
7680 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007681 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007682 * on their content.
7683 */
7684 public static final int SIZE_DEFAULT = 0;
7685
7686 /**
7687 * Size value for use with {@link #setCustomSizePreset} to show this notification
7688 * with an extra small size.
7689 * <p>This value is only applicable for custom display notifications created using
7690 * {@link #setDisplayIntent}.
7691 */
7692 public static final int SIZE_XSMALL = 1;
7693
7694 /**
7695 * Size value for use with {@link #setCustomSizePreset} to show this notification
7696 * with a small size.
7697 * <p>This value is only applicable for custom display notifications created using
7698 * {@link #setDisplayIntent}.
7699 */
7700 public static final int SIZE_SMALL = 2;
7701
7702 /**
7703 * Size value for use with {@link #setCustomSizePreset} to show this notification
7704 * with a medium size.
7705 * <p>This value is only applicable for custom display notifications created using
7706 * {@link #setDisplayIntent}.
7707 */
7708 public static final int SIZE_MEDIUM = 3;
7709
7710 /**
7711 * Size value for use with {@link #setCustomSizePreset} to show this notification
7712 * with a large size.
7713 * <p>This value is only applicable for custom display notifications created using
7714 * {@link #setDisplayIntent}.
7715 */
7716 public static final int SIZE_LARGE = 4;
7717
Griff Hazend5f11f92014-05-27 15:40:09 -07007718 /**
7719 * Size value for use with {@link #setCustomSizePreset} to show this notification
7720 * full screen.
7721 * <p>This value is only applicable for custom display notifications created using
7722 * {@link #setDisplayIntent}.
7723 */
7724 public static final int SIZE_FULL_SCREEN = 5;
7725
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007726 /**
7727 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7728 * short amount of time when this notification is displayed on the screen. This
7729 * is the default value.
7730 */
7731 public static final int SCREEN_TIMEOUT_SHORT = 0;
7732
7733 /**
7734 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7735 * for a longer amount of time when this notification is displayed on the screen.
7736 */
7737 public static final int SCREEN_TIMEOUT_LONG = -1;
7738
Griff Hazen61a9e862014-05-22 16:05:19 -07007739 /** Notification extra which contains wearable extensions */
7740 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7741
Pete Gastaf6781d2014-10-07 15:17:05 -04007742 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007743 private static final String KEY_ACTIONS = "actions";
7744 private static final String KEY_FLAGS = "flags";
7745 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7746 private static final String KEY_PAGES = "pages";
7747 private static final String KEY_BACKGROUND = "background";
7748 private static final String KEY_CONTENT_ICON = "contentIcon";
7749 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7750 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7751 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7752 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7753 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007754 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007755 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007756 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007757
7758 // Flags bitwise-ored to mFlags
7759 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7760 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7761 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7762 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007763 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007764 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007765 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007766
7767 // Default value for flags integer
7768 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7769
7770 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7771 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7772
7773 private ArrayList<Action> mActions = new ArrayList<Action>();
7774 private int mFlags = DEFAULT_FLAGS;
7775 private PendingIntent mDisplayIntent;
7776 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7777 private Bitmap mBackground;
7778 private int mContentIcon;
7779 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7780 private int mContentActionIndex = UNSET_ACTION_INDEX;
7781 private int mCustomSizePreset = SIZE_DEFAULT;
7782 private int mCustomContentHeight;
7783 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007784 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007785 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007786 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007787
7788 /**
7789 * Create a {@link android.app.Notification.WearableExtender} with default
7790 * options.
7791 */
7792 public WearableExtender() {
7793 }
7794
7795 public WearableExtender(Notification notif) {
7796 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7797 if (wearableBundle != null) {
7798 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7799 if (actions != null) {
7800 mActions.addAll(actions);
7801 }
7802
7803 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7804 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7805
7806 Notification[] pages = getNotificationArrayFromBundle(
7807 wearableBundle, KEY_PAGES);
7808 if (pages != null) {
7809 Collections.addAll(mPages, pages);
7810 }
7811
7812 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7813 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7814 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7815 DEFAULT_CONTENT_ICON_GRAVITY);
7816 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7817 UNSET_ACTION_INDEX);
7818 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7819 SIZE_DEFAULT);
7820 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7821 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007822 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007823 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007824 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007825 }
7826 }
7827
7828 /**
7829 * Apply wearable extensions to a notification that is being built. This is typically
7830 * called by the {@link android.app.Notification.Builder#extend} method of
7831 * {@link android.app.Notification.Builder}.
7832 */
7833 @Override
7834 public Notification.Builder extend(Notification.Builder builder) {
7835 Bundle wearableBundle = new Bundle();
7836
7837 if (!mActions.isEmpty()) {
7838 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7839 }
7840 if (mFlags != DEFAULT_FLAGS) {
7841 wearableBundle.putInt(KEY_FLAGS, mFlags);
7842 }
7843 if (mDisplayIntent != null) {
7844 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7845 }
7846 if (!mPages.isEmpty()) {
7847 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7848 new Notification[mPages.size()]));
7849 }
7850 if (mBackground != null) {
7851 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7852 }
7853 if (mContentIcon != 0) {
7854 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7855 }
7856 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7857 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7858 }
7859 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7860 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7861 mContentActionIndex);
7862 }
7863 if (mCustomSizePreset != SIZE_DEFAULT) {
7864 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7865 }
7866 if (mCustomContentHeight != 0) {
7867 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7868 }
7869 if (mGravity != DEFAULT_GRAVITY) {
7870 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7871 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007872 if (mHintScreenTimeout != 0) {
7873 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7874 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007875 if (mDismissalId != null) {
7876 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7877 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007878 if (mBridgeTag != null) {
7879 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7880 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007881
7882 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7883 return builder;
7884 }
7885
7886 @Override
7887 public WearableExtender clone() {
7888 WearableExtender that = new WearableExtender();
7889 that.mActions = new ArrayList<Action>(this.mActions);
7890 that.mFlags = this.mFlags;
7891 that.mDisplayIntent = this.mDisplayIntent;
7892 that.mPages = new ArrayList<Notification>(this.mPages);
7893 that.mBackground = this.mBackground;
7894 that.mContentIcon = this.mContentIcon;
7895 that.mContentIconGravity = this.mContentIconGravity;
7896 that.mContentActionIndex = this.mContentActionIndex;
7897 that.mCustomSizePreset = this.mCustomSizePreset;
7898 that.mCustomContentHeight = this.mCustomContentHeight;
7899 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007900 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007901 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007902 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007903 return that;
7904 }
7905
7906 /**
7907 * Add a wearable action to this notification.
7908 *
7909 * <p>When wearable actions are added using this method, the set of actions that
7910 * show on a wearable device splits from devices that only show actions added
7911 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7912 * of which actions display on different devices.
7913 *
7914 * @param action the action to add to this notification
7915 * @return this object for method chaining
7916 * @see android.app.Notification.Action
7917 */
7918 public WearableExtender addAction(Action action) {
7919 mActions.add(action);
7920 return this;
7921 }
7922
7923 /**
7924 * Adds wearable actions to this notification.
7925 *
7926 * <p>When wearable actions are added using this method, the set of actions that
7927 * show on a wearable device splits from devices that only show actions added
7928 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7929 * of which actions display on different devices.
7930 *
7931 * @param actions the actions to add to this notification
7932 * @return this object for method chaining
7933 * @see android.app.Notification.Action
7934 */
7935 public WearableExtender addActions(List<Action> actions) {
7936 mActions.addAll(actions);
7937 return this;
7938 }
7939
7940 /**
7941 * Clear all wearable actions present on this builder.
7942 * @return this object for method chaining.
7943 * @see #addAction
7944 */
7945 public WearableExtender clearActions() {
7946 mActions.clear();
7947 return this;
7948 }
7949
7950 /**
7951 * Get the wearable actions present on this notification.
7952 */
7953 public List<Action> getActions() {
7954 return mActions;
7955 }
7956
7957 /**
7958 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007959 * this notification. The {@link PendingIntent} provided should be for an activity.
7960 *
7961 * <pre class="prettyprint">
7962 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7963 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7964 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7965 * Notification notif = new Notification.Builder(context)
7966 * .extend(new Notification.WearableExtender()
7967 * .setDisplayIntent(displayPendingIntent)
7968 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7969 * .build();</pre>
7970 *
7971 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007972 * should have an empty task affinity. It is also recommended to use the device
7973 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007974 *
7975 * <p>Example AndroidManifest.xml entry:
7976 * <pre class="prettyprint">
7977 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7978 * android:exported=&quot;true&quot;
7979 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007980 * android:taskAffinity=&quot;&quot;
7981 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007982 *
7983 * @param intent the {@link PendingIntent} for an activity
7984 * @return this object for method chaining
7985 * @see android.app.Notification.WearableExtender#getDisplayIntent
7986 */
7987 public WearableExtender setDisplayIntent(PendingIntent intent) {
7988 mDisplayIntent = intent;
7989 return this;
7990 }
7991
7992 /**
7993 * Get the intent to launch inside of an activity view when displaying this
7994 * notification. This {@code PendingIntent} should be for an activity.
7995 */
7996 public PendingIntent getDisplayIntent() {
7997 return mDisplayIntent;
7998 }
7999
8000 /**
8001 * Add an additional page of content to display with this notification. The current
8002 * notification forms the first page, and pages added using this function form
8003 * subsequent pages. This field can be used to separate a notification into multiple
8004 * sections.
8005 *
8006 * @param page the notification to add as another page
8007 * @return this object for method chaining
8008 * @see android.app.Notification.WearableExtender#getPages
8009 */
8010 public WearableExtender addPage(Notification page) {
8011 mPages.add(page);
8012 return this;
8013 }
8014
8015 /**
8016 * Add additional pages of content to display with this notification. The current
8017 * notification forms the first page, and pages added using this function form
8018 * subsequent pages. This field can be used to separate a notification into multiple
8019 * sections.
8020 *
8021 * @param pages a list of notifications
8022 * @return this object for method chaining
8023 * @see android.app.Notification.WearableExtender#getPages
8024 */
8025 public WearableExtender addPages(List<Notification> pages) {
8026 mPages.addAll(pages);
8027 return this;
8028 }
8029
8030 /**
8031 * Clear all additional pages present on this builder.
8032 * @return this object for method chaining.
8033 * @see #addPage
8034 */
8035 public WearableExtender clearPages() {
8036 mPages.clear();
8037 return this;
8038 }
8039
8040 /**
8041 * Get the array of additional pages of content for displaying this notification. The
8042 * current notification forms the first page, and elements within this array form
8043 * subsequent pages. This field can be used to separate a notification into multiple
8044 * sections.
8045 * @return the pages for this notification
8046 */
8047 public List<Notification> getPages() {
8048 return mPages;
8049 }
8050
8051 /**
8052 * Set a background image to be displayed behind the notification content.
8053 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8054 * will work with any notification style.
8055 *
8056 * @param background the background bitmap
8057 * @return this object for method chaining
8058 * @see android.app.Notification.WearableExtender#getBackground
8059 */
8060 public WearableExtender setBackground(Bitmap background) {
8061 mBackground = background;
8062 return this;
8063 }
8064
8065 /**
8066 * Get a background image to be displayed behind the notification content.
8067 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8068 * will work with any notification style.
8069 *
8070 * @return the background image
8071 * @see android.app.Notification.WearableExtender#setBackground
8072 */
8073 public Bitmap getBackground() {
8074 return mBackground;
8075 }
8076
8077 /**
8078 * Set an icon that goes with the content of this notification.
8079 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008080 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008081 public WearableExtender setContentIcon(int icon) {
8082 mContentIcon = icon;
8083 return this;
8084 }
8085
8086 /**
8087 * Get an icon that goes with the content of this notification.
8088 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008089 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008090 public int getContentIcon() {
8091 return mContentIcon;
8092 }
8093
8094 /**
8095 * Set the gravity that the content icon should have within the notification display.
8096 * Supported values include {@link android.view.Gravity#START} and
8097 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8098 * @see #setContentIcon
8099 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008100 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008101 public WearableExtender setContentIconGravity(int contentIconGravity) {
8102 mContentIconGravity = contentIconGravity;
8103 return this;
8104 }
8105
8106 /**
8107 * Get the gravity that the content icon should have within the notification display.
8108 * Supported values include {@link android.view.Gravity#START} and
8109 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8110 * @see #getContentIcon
8111 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008112 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008113 public int getContentIconGravity() {
8114 return mContentIconGravity;
8115 }
8116
8117 /**
8118 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008119 * this notification. This action will no longer display separately from the
8120 * notification's content.
8121 *
Griff Hazenca48d352014-05-28 22:37:13 -07008122 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008123 * set, although the list of available actions comes from the main notification and not
8124 * from the child page's notification.
8125 *
8126 * @param actionIndex The index of the action to hoist onto the current notification page.
8127 * If wearable actions were added to the main notification, this index
8128 * will apply to that list, otherwise it will apply to the regular
8129 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008130 */
8131 public WearableExtender setContentAction(int actionIndex) {
8132 mContentActionIndex = actionIndex;
8133 return this;
8134 }
8135
8136 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008137 * Get the index of the notification action, if any, that was specified as being clickable
8138 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008139 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008140 *
Griff Hazenca48d352014-05-28 22:37:13 -07008141 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008142 * set, although the list of available actions comes from the main notification and not
8143 * from the child page's notification.
8144 *
8145 * <p>If wearable specific actions were added to the main notification, this index will
8146 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008147 *
8148 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008149 */
8150 public int getContentAction() {
8151 return mContentActionIndex;
8152 }
8153
8154 /**
8155 * Set the gravity that this notification should have within the available viewport space.
8156 * Supported values include {@link android.view.Gravity#TOP},
8157 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8158 * The default value is {@link android.view.Gravity#BOTTOM}.
8159 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008160 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008161 public WearableExtender setGravity(int gravity) {
8162 mGravity = gravity;
8163 return this;
8164 }
8165
8166 /**
8167 * Get the gravity that this notification should have within the available viewport space.
8168 * Supported values include {@link android.view.Gravity#TOP},
8169 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8170 * The default value is {@link android.view.Gravity#BOTTOM}.
8171 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008172 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008173 public int getGravity() {
8174 return mGravity;
8175 }
8176
8177 /**
8178 * Set the custom size preset for the display of this notification out of the available
8179 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8180 * {@link #SIZE_LARGE}.
8181 * <p>Some custom size presets are only applicable for custom display notifications created
8182 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8183 * documentation for the preset in question. See also
8184 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8185 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008186 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008187 public WearableExtender setCustomSizePreset(int sizePreset) {
8188 mCustomSizePreset = sizePreset;
8189 return this;
8190 }
8191
8192 /**
8193 * Get the custom size preset for the display of this notification out of the available
8194 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8195 * {@link #SIZE_LARGE}.
8196 * <p>Some custom size presets are only applicable for custom display notifications created
8197 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8198 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8199 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008200 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008201 public int getCustomSizePreset() {
8202 return mCustomSizePreset;
8203 }
8204
8205 /**
8206 * Set the custom height in pixels for the display of this notification's content.
8207 * <p>This option is only available for custom display notifications created
8208 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8209 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8210 * {@link #getCustomContentHeight}.
8211 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008212 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008213 public WearableExtender setCustomContentHeight(int height) {
8214 mCustomContentHeight = height;
8215 return this;
8216 }
8217
8218 /**
8219 * Get the custom height in pixels for the display of this notification's content.
8220 * <p>This option is only available for custom display notifications created
8221 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8222 * {@link #setCustomContentHeight}.
8223 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008224 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008225 public int getCustomContentHeight() {
8226 return mCustomContentHeight;
8227 }
8228
8229 /**
8230 * Set whether the scrolling position for the contents of this notification should start
8231 * at the bottom of the contents instead of the top when the contents are too long to
8232 * display within the screen. Default is false (start scroll at the top).
8233 */
8234 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8235 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8236 return this;
8237 }
8238
8239 /**
8240 * Get whether the scrolling position for the contents of this notification should start
8241 * at the bottom of the contents instead of the top when the contents are too long to
8242 * display within the screen. Default is false (start scroll at the top).
8243 */
8244 public boolean getStartScrollBottom() {
8245 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8246 }
8247
8248 /**
8249 * Set whether the content intent is available when the wearable device is not connected
8250 * to a companion device. The user can still trigger this intent when the wearable device
8251 * is offline, but a visual hint will indicate that the content intent may not be available.
8252 * Defaults to true.
8253 */
8254 public WearableExtender setContentIntentAvailableOffline(
8255 boolean contentIntentAvailableOffline) {
8256 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8257 return this;
8258 }
8259
8260 /**
8261 * Get whether the content intent is available when the wearable device is not connected
8262 * to a companion device. The user can still trigger this intent when the wearable device
8263 * is offline, but a visual hint will indicate that the content intent may not be available.
8264 * Defaults to true.
8265 */
8266 public boolean getContentIntentAvailableOffline() {
8267 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8268 }
8269
8270 /**
8271 * Set a hint that this notification's icon should not be displayed.
8272 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8273 * @return this object for method chaining
8274 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008275 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008276 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8277 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8278 return this;
8279 }
8280
8281 /**
8282 * Get a hint that this notification's icon should not be displayed.
8283 * @return {@code true} if this icon should not be displayed, false otherwise.
8284 * The default value is {@code false} if this was never set.
8285 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008286 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008287 public boolean getHintHideIcon() {
8288 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8289 }
8290
8291 /**
8292 * Set a visual hint that only the background image of this notification should be
8293 * displayed, and other semantic content should be hidden. This hint is only applicable
8294 * to sub-pages added using {@link #addPage}.
8295 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008296 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008297 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8298 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8299 return this;
8300 }
8301
8302 /**
8303 * Get a visual hint that only the background image of this notification should be
8304 * displayed, and other semantic content should be hidden. This hint is only applicable
8305 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8306 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008307 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008308 public boolean getHintShowBackgroundOnly() {
8309 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8310 }
8311
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008312 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008313 * Set a hint that this notification's background should not be clipped if possible,
8314 * and should instead be resized to fully display on the screen, retaining the aspect
8315 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008316 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8317 * @return this object for method chaining
8318 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008319 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008320 public WearableExtender setHintAvoidBackgroundClipping(
8321 boolean hintAvoidBackgroundClipping) {
8322 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8323 return this;
8324 }
8325
8326 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008327 * Get a hint that this notification's background should not be clipped if possible,
8328 * and should instead be resized to fully display on the screen, retaining the aspect
8329 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008330 * @return {@code true} if it's ok if the background is clipped on the screen, false
8331 * otherwise. The default value is {@code false} if this was never set.
8332 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008333 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008334 public boolean getHintAvoidBackgroundClipping() {
8335 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8336 }
8337
8338 /**
8339 * Set a hint that the screen should remain on for at least this duration when
8340 * this notification is displayed on the screen.
8341 * @param timeout The requested screen timeout in milliseconds. Can also be either
8342 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8343 * @return this object for method chaining
8344 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008345 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008346 public WearableExtender setHintScreenTimeout(int timeout) {
8347 mHintScreenTimeout = timeout;
8348 return this;
8349 }
8350
8351 /**
8352 * Get the duration, in milliseconds, that the screen should remain on for
8353 * when this notification is displayed.
8354 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8355 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8356 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008357 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008358 public int getHintScreenTimeout() {
8359 return mHintScreenTimeout;
8360 }
8361
Alex Hills9ab3a232016-04-05 14:54:56 -04008362 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008363 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8364 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8365 * qr codes, as well as other simple black-and-white tickets.
8366 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8367 * @return this object for method chaining
8368 */
8369 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8370 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8371 return this;
8372 }
8373
8374 /**
8375 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8376 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8377 * qr codes, as well as other simple black-and-white tickets.
8378 * @return {@code true} if it should be displayed in ambient, false otherwise
8379 * otherwise. The default value is {@code false} if this was never set.
8380 */
8381 public boolean getHintAmbientBigPicture() {
8382 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8383 }
8384
8385 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008386 * Set a hint that this notification's content intent will launch an {@link Activity}
8387 * directly, telling the platform that it can generate the appropriate transitions.
8388 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8389 * an activity and transitions should be generated, false otherwise.
8390 * @return this object for method chaining
8391 */
8392 public WearableExtender setHintContentIntentLaunchesActivity(
8393 boolean hintContentIntentLaunchesActivity) {
8394 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8395 return this;
8396 }
8397
8398 /**
8399 * Get a hint that this notification's content intent will launch an {@link Activity}
8400 * directly, telling the platform that it can generate the appropriate transitions
8401 * @return {@code true} if the content intent will launch an activity and transitions should
8402 * be generated, false otherwise. The default value is {@code false} if this was never set.
8403 */
8404 public boolean getHintContentIntentLaunchesActivity() {
8405 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8406 }
8407
Nadia Benbernou948627e2016-04-14 14:41:08 -04008408 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008409 * Sets the dismissal id for this notification. If a notification is posted with a
8410 * dismissal id, then when that notification is canceled, notifications on other wearables
8411 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008412 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008413 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008414 * @param dismissalId the dismissal id of the notification.
8415 * @return this object for method chaining
8416 */
8417 public WearableExtender setDismissalId(String dismissalId) {
8418 mDismissalId = dismissalId;
8419 return this;
8420 }
8421
8422 /**
8423 * Returns the dismissal id of the notification.
8424 * @return the dismissal id of the notification or null if it has not been set.
8425 */
8426 public String getDismissalId() {
8427 return mDismissalId;
8428 }
8429
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008430 /**
8431 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8432 * posted from a phone to provide finer-grained control on what notifications are bridged
8433 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8434 * Features to Notifications</a> for more information.
8435 * @param bridgeTag the bridge tag of the notification.
8436 * @return this object for method chaining
8437 */
8438 public WearableExtender setBridgeTag(String bridgeTag) {
8439 mBridgeTag = bridgeTag;
8440 return this;
8441 }
8442
8443 /**
8444 * Returns the bridge tag of the notification.
8445 * @return the bridge tag or null if not present.
8446 */
8447 public String getBridgeTag() {
8448 return mBridgeTag;
8449 }
8450
Griff Hazen61a9e862014-05-22 16:05:19 -07008451 private void setFlag(int mask, boolean value) {
8452 if (value) {
8453 mFlags |= mask;
8454 } else {
8455 mFlags &= ~mask;
8456 }
8457 }
8458 }
8459
8460 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008461 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8462 * with car extensions:
8463 *
8464 * <ol>
8465 * <li>Create an {@link Notification.Builder}, setting any desired
8466 * properties.
8467 * <li>Create a {@link CarExtender}.
8468 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8469 * {@link CarExtender}.
8470 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8471 * to apply the extensions to a notification.
8472 * </ol>
8473 *
8474 * <pre class="prettyprint">
8475 * Notification notification = new Notification.Builder(context)
8476 * ...
8477 * .extend(new CarExtender()
8478 * .set*(...))
8479 * .build();
8480 * </pre>
8481 *
8482 * <p>Car extensions can be accessed on an existing notification by using the
8483 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8484 * to access values.
8485 */
8486 public static final class CarExtender implements Extender {
8487 private static final String TAG = "CarExtender";
8488
8489 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8490 private static final String EXTRA_LARGE_ICON = "large_icon";
8491 private static final String EXTRA_CONVERSATION = "car_conversation";
8492 private static final String EXTRA_COLOR = "app_color";
8493
8494 private Bitmap mLargeIcon;
8495 private UnreadConversation mUnreadConversation;
8496 private int mColor = Notification.COLOR_DEFAULT;
8497
8498 /**
8499 * Create a {@link CarExtender} with default options.
8500 */
8501 public CarExtender() {
8502 }
8503
8504 /**
8505 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8506 *
8507 * @param notif The notification from which to copy options.
8508 */
8509 public CarExtender(Notification notif) {
8510 Bundle carBundle = notif.extras == null ?
8511 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8512 if (carBundle != null) {
8513 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8514 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8515
8516 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8517 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8518 }
8519 }
8520
8521 /**
8522 * Apply car extensions to a notification that is being built. This is typically called by
8523 * the {@link Notification.Builder#extend(Notification.Extender)}
8524 * method of {@link Notification.Builder}.
8525 */
8526 @Override
8527 public Notification.Builder extend(Notification.Builder builder) {
8528 Bundle carExtensions = new Bundle();
8529
8530 if (mLargeIcon != null) {
8531 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8532 }
8533 if (mColor != Notification.COLOR_DEFAULT) {
8534 carExtensions.putInt(EXTRA_COLOR, mColor);
8535 }
8536
8537 if (mUnreadConversation != null) {
8538 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8539 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8540 }
8541
8542 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8543 return builder;
8544 }
8545
8546 /**
8547 * Sets the accent color to use when Android Auto presents the notification.
8548 *
8549 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8550 * to accent the displayed notification. However, not all colors are acceptable in an
8551 * automotive setting. This method can be used to override the color provided in the
8552 * notification in such a situation.
8553 */
Tor Norbye80756e32015-03-02 09:39:27 -08008554 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008555 mColor = color;
8556 return this;
8557 }
8558
8559 /**
8560 * Gets the accent color.
8561 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008562 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008563 */
Tor Norbye80756e32015-03-02 09:39:27 -08008564 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008565 public int getColor() {
8566 return mColor;
8567 }
8568
8569 /**
8570 * Sets the large icon of the car notification.
8571 *
8572 * If no large icon is set in the extender, Android Auto will display the icon
8573 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8574 *
8575 * @param largeIcon The large icon to use in the car notification.
8576 * @return This object for method chaining.
8577 */
8578 public CarExtender setLargeIcon(Bitmap largeIcon) {
8579 mLargeIcon = largeIcon;
8580 return this;
8581 }
8582
8583 /**
8584 * Gets the large icon used in this car notification, or null if no icon has been set.
8585 *
8586 * @return The large icon for the car notification.
8587 * @see CarExtender#setLargeIcon
8588 */
8589 public Bitmap getLargeIcon() {
8590 return mLargeIcon;
8591 }
8592
8593 /**
8594 * Sets the unread conversation in a message notification.
8595 *
8596 * @param unreadConversation The unread part of the conversation this notification conveys.
8597 * @return This object for method chaining.
8598 */
8599 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8600 mUnreadConversation = unreadConversation;
8601 return this;
8602 }
8603
8604 /**
8605 * Returns the unread conversation conveyed by this notification.
8606 * @see #setUnreadConversation(UnreadConversation)
8607 */
8608 public UnreadConversation getUnreadConversation() {
8609 return mUnreadConversation;
8610 }
8611
8612 /**
8613 * A class which holds the unread messages from a conversation.
8614 */
8615 public static class UnreadConversation {
8616 private static final String KEY_AUTHOR = "author";
8617 private static final String KEY_TEXT = "text";
8618 private static final String KEY_MESSAGES = "messages";
8619 private static final String KEY_REMOTE_INPUT = "remote_input";
8620 private static final String KEY_ON_REPLY = "on_reply";
8621 private static final String KEY_ON_READ = "on_read";
8622 private static final String KEY_PARTICIPANTS = "participants";
8623 private static final String KEY_TIMESTAMP = "timestamp";
8624
8625 private final String[] mMessages;
8626 private final RemoteInput mRemoteInput;
8627 private final PendingIntent mReplyPendingIntent;
8628 private final PendingIntent mReadPendingIntent;
8629 private final String[] mParticipants;
8630 private final long mLatestTimestamp;
8631
8632 UnreadConversation(String[] messages, RemoteInput remoteInput,
8633 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8634 String[] participants, long latestTimestamp) {
8635 mMessages = messages;
8636 mRemoteInput = remoteInput;
8637 mReadPendingIntent = readPendingIntent;
8638 mReplyPendingIntent = replyPendingIntent;
8639 mParticipants = participants;
8640 mLatestTimestamp = latestTimestamp;
8641 }
8642
8643 /**
8644 * Gets the list of messages conveyed by this notification.
8645 */
8646 public String[] getMessages() {
8647 return mMessages;
8648 }
8649
8650 /**
8651 * Gets the remote input that will be used to convey the response to a message list, or
8652 * null if no such remote input exists.
8653 */
8654 public RemoteInput getRemoteInput() {
8655 return mRemoteInput;
8656 }
8657
8658 /**
8659 * Gets the pending intent that will be triggered when the user replies to this
8660 * notification.
8661 */
8662 public PendingIntent getReplyPendingIntent() {
8663 return mReplyPendingIntent;
8664 }
8665
8666 /**
8667 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8668 * in this object's message list.
8669 */
8670 public PendingIntent getReadPendingIntent() {
8671 return mReadPendingIntent;
8672 }
8673
8674 /**
8675 * Gets the participants in the conversation.
8676 */
8677 public String[] getParticipants() {
8678 return mParticipants;
8679 }
8680
8681 /**
8682 * Gets the firs participant in the conversation.
8683 */
8684 public String getParticipant() {
8685 return mParticipants.length > 0 ? mParticipants[0] : null;
8686 }
8687
8688 /**
8689 * Gets the timestamp of the conversation.
8690 */
8691 public long getLatestTimestamp() {
8692 return mLatestTimestamp;
8693 }
8694
8695 Bundle getBundleForUnreadConversation() {
8696 Bundle b = new Bundle();
8697 String author = null;
8698 if (mParticipants != null && mParticipants.length > 1) {
8699 author = mParticipants[0];
8700 }
8701 Parcelable[] messages = new Parcelable[mMessages.length];
8702 for (int i = 0; i < messages.length; i++) {
8703 Bundle m = new Bundle();
8704 m.putString(KEY_TEXT, mMessages[i]);
8705 m.putString(KEY_AUTHOR, author);
8706 messages[i] = m;
8707 }
8708 b.putParcelableArray(KEY_MESSAGES, messages);
8709 if (mRemoteInput != null) {
8710 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8711 }
8712 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8713 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8714 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8715 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8716 return b;
8717 }
8718
8719 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8720 if (b == null) {
8721 return null;
8722 }
8723 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8724 String[] messages = null;
8725 if (parcelableMessages != null) {
8726 String[] tmp = new String[parcelableMessages.length];
8727 boolean success = true;
8728 for (int i = 0; i < tmp.length; i++) {
8729 if (!(parcelableMessages[i] instanceof Bundle)) {
8730 success = false;
8731 break;
8732 }
8733 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8734 if (tmp[i] == null) {
8735 success = false;
8736 break;
8737 }
8738 }
8739 if (success) {
8740 messages = tmp;
8741 } else {
8742 return null;
8743 }
8744 }
8745
8746 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8747 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8748
8749 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8750
8751 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8752 if (participants == null || participants.length != 1) {
8753 return null;
8754 }
8755
8756 return new UnreadConversation(messages,
8757 remoteInput,
8758 onReply,
8759 onRead,
8760 participants, b.getLong(KEY_TIMESTAMP));
8761 }
8762 };
8763
8764 /**
8765 * Builder class for {@link CarExtender.UnreadConversation} objects.
8766 */
8767 public static class Builder {
8768 private final List<String> mMessages = new ArrayList<String>();
8769 private final String mParticipant;
8770 private RemoteInput mRemoteInput;
8771 private PendingIntent mReadPendingIntent;
8772 private PendingIntent mReplyPendingIntent;
8773 private long mLatestTimestamp;
8774
8775 /**
8776 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8777 *
8778 * @param name The name of the other participant in the conversation.
8779 */
8780 public Builder(String name) {
8781 mParticipant = name;
8782 }
8783
8784 /**
8785 * Appends a new unread message to the list of messages for this conversation.
8786 *
8787 * The messages should be added from oldest to newest.
8788 *
8789 * @param message The text of the new unread message.
8790 * @return This object for method chaining.
8791 */
8792 public Builder addMessage(String message) {
8793 mMessages.add(message);
8794 return this;
8795 }
8796
8797 /**
8798 * Sets the pending intent and remote input which will convey the reply to this
8799 * notification.
8800 *
8801 * @param pendingIntent The pending intent which will be triggered on a reply.
8802 * @param remoteInput The remote input parcelable which will carry the reply.
8803 * @return This object for method chaining.
8804 *
8805 * @see CarExtender.UnreadConversation#getRemoteInput
8806 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8807 */
8808 public Builder setReplyAction(
8809 PendingIntent pendingIntent, RemoteInput remoteInput) {
8810 mRemoteInput = remoteInput;
8811 mReplyPendingIntent = pendingIntent;
8812
8813 return this;
8814 }
8815
8816 /**
8817 * Sets the pending intent that will be sent once the messages in this notification
8818 * are read.
8819 *
8820 * @param pendingIntent The pending intent to use.
8821 * @return This object for method chaining.
8822 */
8823 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8824 mReadPendingIntent = pendingIntent;
8825 return this;
8826 }
8827
8828 /**
8829 * Sets the timestamp of the most recent message in an unread conversation.
8830 *
8831 * If a messaging notification has been posted by your application and has not
8832 * yet been cancelled, posting a later notification with the same id and tag
8833 * but without a newer timestamp may result in Android Auto not displaying a
8834 * heads up notification for the later notification.
8835 *
8836 * @param timestamp The timestamp of the most recent message in the conversation.
8837 * @return This object for method chaining.
8838 */
8839 public Builder setLatestTimestamp(long timestamp) {
8840 mLatestTimestamp = timestamp;
8841 return this;
8842 }
8843
8844 /**
8845 * Builds a new unread conversation object.
8846 *
8847 * @return The new unread conversation object.
8848 */
8849 public UnreadConversation build() {
8850 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8851 String[] participants = { mParticipant };
8852 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8853 mReadPendingIntent, participants, mLatestTimestamp);
8854 }
8855 }
8856 }
8857
8858 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008859 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8860 * with a TV extension:
8861 *
8862 * <ol>
8863 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8864 * <li>Create a {@link TvExtender}.
8865 * <li>Set TV-specific properties using the {@code set} methods of
8866 * {@link TvExtender}.
8867 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8868 * to apply the extension to a notification.
8869 * </ol>
8870 *
8871 * <pre class="prettyprint">
8872 * Notification notification = new Notification.Builder(context)
8873 * ...
8874 * .extend(new TvExtender()
8875 * .set*(...))
8876 * .build();
8877 * </pre>
8878 *
8879 * <p>TV extensions can be accessed on an existing notification by using the
8880 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8881 * to access values.
8882 *
8883 * @hide
8884 */
8885 @SystemApi
8886 public static final class TvExtender implements Extender {
8887 private static final String TAG = "TvExtender";
8888
8889 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8890 private static final String EXTRA_FLAGS = "flags";
8891 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8892 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008893 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08008894 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008895
8896 // Flags bitwise-ored to mFlags
8897 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8898
8899 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008900 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008901 private PendingIntent mContentIntent;
8902 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08008903 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008904
8905 /**
8906 * Create a {@link TvExtender} with default options.
8907 */
8908 public TvExtender() {
8909 mFlags = FLAG_AVAILABLE_ON_TV;
8910 }
8911
8912 /**
8913 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8914 *
8915 * @param notif The notification from which to copy options.
8916 */
8917 public TvExtender(Notification notif) {
8918 Bundle bundle = notif.extras == null ?
8919 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8920 if (bundle != null) {
8921 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008922 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008923 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008924 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8925 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8926 }
8927 }
8928
8929 /**
8930 * Apply a TV extension to a notification that is being built. This is typically called by
8931 * the {@link Notification.Builder#extend(Notification.Extender)}
8932 * method of {@link Notification.Builder}.
8933 */
8934 @Override
8935 public Notification.Builder extend(Notification.Builder builder) {
8936 Bundle bundle = new Bundle();
8937
8938 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008939 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008940 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008941 if (mContentIntent != null) {
8942 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8943 }
8944
8945 if (mDeleteIntent != null) {
8946 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8947 }
8948
8949 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8950 return builder;
8951 }
8952
8953 /**
8954 * Returns true if this notification should be shown on TV. This method return true
8955 * if the notification was extended with a TvExtender.
8956 */
8957 public boolean isAvailableOnTv() {
8958 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8959 }
8960
8961 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008962 * Specifies the channel the notification should be delivered on when shown on TV.
8963 * It can be different from the channel that the notification is delivered to when
8964 * posting on a non-TV device.
8965 */
8966 public TvExtender setChannel(String channelId) {
8967 mChannelId = channelId;
8968 return this;
8969 }
8970
8971 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008972 * Specifies the channel the notification should be delivered on when shown on TV.
8973 * It can be different from the channel that the notification is delivered to when
8974 * posting on a non-TV device.
8975 */
8976 public TvExtender setChannelId(String channelId) {
8977 mChannelId = channelId;
8978 return this;
8979 }
8980
Jeff Sharkey000ce802017-04-29 13:13:27 -06008981 /** @removed */
8982 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008983 public String getChannel() {
8984 return mChannelId;
8985 }
8986
8987 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008988 * Returns the id of the channel this notification posts to on TV.
8989 */
8990 public String getChannelId() {
8991 return mChannelId;
8992 }
8993
8994 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008995 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8996 * If provided, it is used instead of the content intent specified
8997 * at the level of Notification.
8998 */
8999 public TvExtender setContentIntent(PendingIntent intent) {
9000 mContentIntent = intent;
9001 return this;
9002 }
9003
9004 /**
9005 * Returns the TV-specific content intent. If this method returns null, the
9006 * main content intent on the notification should be used.
9007 *
9008 * @see {@link Notification#contentIntent}
9009 */
9010 public PendingIntent getContentIntent() {
9011 return mContentIntent;
9012 }
9013
9014 /**
9015 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9016 * by the user on TV. If provided, it is used instead of the delete intent specified
9017 * at the level of Notification.
9018 */
9019 public TvExtender setDeleteIntent(PendingIntent intent) {
9020 mDeleteIntent = intent;
9021 return this;
9022 }
9023
9024 /**
9025 * Returns the TV-specific delete intent. If this method returns null, the
9026 * main delete intent on the notification should be used.
9027 *
9028 * @see {@link Notification#deleteIntent}
9029 */
9030 public PendingIntent getDeleteIntent() {
9031 return mDeleteIntent;
9032 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009033
9034 /**
9035 * Specifies whether this notification should suppress showing a message over top of apps
9036 * outside of the launcher.
9037 */
9038 public TvExtender setSuppressShowOverApps(boolean suppress) {
9039 mSuppressShowOverApps = suppress;
9040 return this;
9041 }
9042
9043 /**
9044 * Returns true if this notification should not show messages over top of apps
9045 * outside of the launcher.
9046 */
9047 public boolean getSuppressShowOverApps() {
9048 return mSuppressShowOverApps;
9049 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009050 }
9051
9052 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009053 * Get an array of Notification objects from a parcelable array bundle field.
9054 * Update the bundle to have a typed array so fetches in the future don't need
9055 * to do an array copy.
9056 */
9057 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9058 Parcelable[] array = bundle.getParcelableArray(key);
9059 if (array instanceof Notification[] || array == null) {
9060 return (Notification[]) array;
9061 }
9062 Notification[] typedArray = Arrays.copyOf(array, array.length,
9063 Notification[].class);
9064 bundle.putParcelableArray(key, typedArray);
9065 return typedArray;
9066 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009067
9068 private static class BuilderRemoteViews extends RemoteViews {
9069 public BuilderRemoteViews(Parcel parcel) {
9070 super(parcel);
9071 }
9072
Kenny Guy77320062014-08-27 21:37:15 +01009073 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9074 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009075 }
9076
9077 @Override
9078 public BuilderRemoteViews clone() {
9079 Parcel p = Parcel.obtain();
9080 writeToParcel(p, 0);
9081 p.setDataPosition(0);
9082 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9083 p.recycle();
9084 return brv;
9085 }
9086 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009087
9088 private static class StandardTemplateParams {
9089 boolean hasProgress = true;
9090 boolean ambient = false;
9091 CharSequence title;
9092 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009093 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009094 boolean hideLargeIcon;
9095 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009096
9097 final StandardTemplateParams reset() {
9098 hasProgress = true;
9099 ambient = false;
9100 title = null;
9101 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009102 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009103 return this;
9104 }
9105
9106 final StandardTemplateParams hasProgress(boolean hasProgress) {
9107 this.hasProgress = hasProgress;
9108 return this;
9109 }
9110
9111 final StandardTemplateParams title(CharSequence title) {
9112 this.title = title;
9113 return this;
9114 }
9115
9116 final StandardTemplateParams text(CharSequence text) {
9117 this.text = text;
9118 return this;
9119 }
9120
Selim Cinekafeed292017-12-12 17:32:44 -08009121 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9122 this.headerTextSecondary = text;
9123 return this;
9124 }
9125
Selim Cinek88188f22017-09-19 16:46:56 -07009126 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9127 this.alwaysShowReply = alwaysShowReply;
9128 return this;
9129 }
9130
9131 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9132 this.hideLargeIcon = hideLargeIcon;
9133 return this;
9134 }
9135
Adrian Roos70d7aa32017-01-11 15:39:06 -08009136 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009137 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009138 this.ambient = ambient;
9139 return this;
9140 }
9141
9142 final StandardTemplateParams fillTextsFrom(Builder b) {
9143 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009144 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9145
9146 // Big text notifications should contain their content when viewed in ambient mode.
9147 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9148 if (!ambient || TextUtils.isEmpty(text)) {
9149 text = extras.getCharSequence(EXTRA_TEXT);
9150 }
9151 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009152 return this;
9153 }
9154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009155}