blob: 233e09d90f9b7bdf4485c6e45c837fe9a4878c5d [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
Selim Cineked64a142018-02-06 18:06:01 -08004596 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4597 R.dimen.notification_content_margin);
Christoph Studerfe718432014-09-01 18:21:18 +02004598 }
4599
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004600 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004601 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004602 }
4603
Adrian Roos70d7aa32017-01-11 15:39:06 -08004604 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4605 StandardTemplateParams p) {
4606 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004607
Christoph Studerfe718432014-09-01 18:21:18 +02004608 resetStandardTemplateWithActions(big);
4609
Adrian Roose458aa82015-12-08 16:17:19 -08004610 boolean validRemoteInput = false;
4611
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004612 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004613 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004614 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004615 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004616 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004617 big.setViewVisibility(R.id.actions, View.VISIBLE);
Selim Cineked64a142018-02-06 18:06:01 -08004618 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004619 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004620 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004621 Action action = mActions.get(i);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004622 boolean actionHasValidInput = hasValidRemoteInput(action);
4623 validRemoteInput |= actionHasValidInput;
Adrian Roose458aa82015-12-08 16:17:19 -08004624
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004625 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004626 i % 2 != 0, p.ambient);
Selim Cinekffcc7cf2018-02-06 17:43:51 -08004627 if (actionHasValidInput) {
4628 // Clear the drawable
4629 button.setInt(R.id.action0, "setBackgroundResource", 0);
4630 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004631 big.addView(R.id.actions, button);
4632 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004633 } else {
4634 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004635 }
Adrian Roose458aa82015-12-08 16:17:19 -08004636
4637 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004638 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004639 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4640 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004641 big.setTextViewText(R.id.notification_material_reply_text_1,
4642 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004643 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004644
4645 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4646 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004647 big.setTextViewText(R.id.notification_material_reply_text_2,
4648 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004649 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004650
4651 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4652 big.setViewVisibility(
4653 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004654 big.setTextViewText(R.id.notification_material_reply_text_3,
4655 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004656 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004657 }
4658 }
4659 }
4660
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004661 return big;
4662 }
4663
Adrian Roose458aa82015-12-08 16:17:19 -08004664 private boolean hasValidRemoteInput(Action action) {
4665 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4666 // Weird actions
4667 return false;
4668 }
4669
4670 RemoteInput[] remoteInputs = action.getRemoteInputs();
4671 if (remoteInputs == null) {
4672 return false;
4673 }
4674
4675 for (RemoteInput r : remoteInputs) {
4676 CharSequence[] choices = r.getChoices();
4677 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4678 return true;
4679 }
4680 }
4681 return false;
4682 }
4683
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004684 /**
4685 * Construct a RemoteViews for the final 1U notification layout. In order:
4686 * 1. Custom contentView from the caller
4687 * 2. Style's proposed content view
4688 * 3. Standard template view
4689 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004690 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004691 return createContentView(false /* increasedheight */ );
4692 }
4693
4694 /**
4695 * Construct a RemoteViews for the smaller content view.
4696 *
4697 * @param increasedHeight true if this layout be created with an increased height. Some
4698 * styles may support showing more then just that basic 1U size
4699 * and the system may decide to render important notifications
4700 * slightly bigger even when collapsed.
4701 *
4702 * @hide
4703 */
4704 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004705 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004706 return mN.contentView;
4707 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004708 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004709 if (styleView != null) {
4710 return styleView;
4711 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004712 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004713 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004714 }
4715
Selim Cineka7679b62017-05-10 16:33:25 -07004716 private boolean useExistingRemoteView() {
4717 return mStyle == null || (!mStyle.displayCustomViewInline()
4718 && !mRebuildStyledRemoteViews);
4719 }
4720
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004721 /**
4722 * Construct a RemoteViews for the final big notification layout.
4723 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004724 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004725 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004726 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004727 return mN.bigContentView;
4728 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004729 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004730 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004731 } else if (mActions.size() != 0) {
4732 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004733 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004734 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004735 return result;
4736 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004737
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004738 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004739 * Construct a RemoteViews for the final notification header only. This will not be
4740 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004741 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004742 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004743 * @hide
4744 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004745 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004746 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4747 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004748 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004749 ambient ? R.layout.notification_template_ambient_header
4750 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004751 resetNotificationHeader(header);
Selim Cinekafeed292017-12-12 17:32:44 -08004752 bindNotificationHeader(header, ambient, null);
Selim Cinek414ad332017-02-24 19:06:12 -08004753 if (colorized != null) {
4754 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4755 } else {
4756 mN.extras.remove(EXTRA_COLORIZED);
4757 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004758 return header;
4759 }
4760
Adrian Roos487374f2017-01-11 15:48:14 -08004761 /**
4762 * Construct a RemoteViews for the ambient version of the notification.
4763 *
4764 * @hide
4765 */
4766 public RemoteViews makeAmbientNotification() {
4767 RemoteViews ambient = applyStandardTemplateWithActions(
4768 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004769 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004770 return ambient;
4771 }
4772
Selim Cinek29603462015-11-17 19:04:39 -08004773 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004774 if (result != null) {
4775 result.setViewVisibility(R.id.text_line_1, View.GONE);
4776 }
Selim Cinek29603462015-11-17 19:04:39 -08004777 }
4778
Selim Cinek6743c0b2017-01-18 18:24:01 -08004779 /**
4780 * Adapt the Notification header if this view is used as an expanded view.
4781 *
4782 * @hide
4783 */
4784 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004785 if (result != null) {
4786 result.setBoolean(R.id.notification_header, "setExpanded", true);
4787 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004788 }
4789
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004790 /**
4791 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004792 *
4793 * @param increasedHeight true if this layout be created with an increased height. Some
4794 * styles may support showing more then just that basic 1U size
4795 * and the system may decide to render important notifications
4796 * slightly bigger even when collapsed.
4797 *
4798 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004799 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004800 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004801 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004802 return mN.headsUpContentView;
4803 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004804 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4805 if (styleView != null) {
4806 return styleView;
4807 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004808 } else if (mActions.size() == 0) {
4809 return null;
4810 }
4811
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004812 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004813 }
4814
Selim Cinek624c02db2015-12-14 21:00:02 -08004815 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004816 * Construct a RemoteViews for the final heads-up notification layout.
4817 */
4818 public RemoteViews createHeadsUpContentView() {
4819 return createHeadsUpContentView(false /* useIncreasedHeight */);
4820 }
4821
4822 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004823 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4824 *
4825 * @hide
4826 */
4827 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004828 return makePublicView(false /* ambient */);
4829 }
4830
4831 /**
4832 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4833 *
4834 * @hide
4835 */
4836 public RemoteViews makePublicAmbientNotification() {
4837 return makePublicView(true /* ambient */);
4838 }
4839
4840 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004841 if (mN.publicVersion != null) {
4842 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004843 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004844 }
4845 Bundle savedBundle = mN.extras;
4846 Style style = mStyle;
4847 mStyle = null;
4848 Icon largeIcon = mN.mLargeIcon;
4849 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004850 Bitmap largeIconLegacy = mN.largeIcon;
4851 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004852 ArrayList<Action> actions = mActions;
4853 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004854 Bundle publicExtras = new Bundle();
4855 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4856 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4857 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4858 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004859 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4860 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004861 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004862 RemoteViews view;
4863 if (ambient) {
4864 publicExtras.putCharSequence(EXTRA_TITLE,
4865 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4866 view = makeAmbientNotification();
4867 } else{
4868 view = makeNotificationHeader(false /* ambient */);
4869 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4870 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004871 mN.extras = savedBundle;
4872 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004873 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004874 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004875 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004876 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004877 }
4878
Selim Cinek6743c0b2017-01-18 18:24:01 -08004879 /**
4880 * Construct a content view for the display when low - priority
4881 *
4882 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4883 * a new subtext is created consisting of the content of the
4884 * notification.
4885 * @hide
4886 */
4887 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4888 int color = mN.color;
4889 mN.color = COLOR_DEFAULT;
4890 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4891 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4892 CharSequence newSummary = createSummaryText();
4893 if (!TextUtils.isEmpty(newSummary)) {
4894 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4895 }
4896 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004897
Adrian Roos6f6e1592017-05-02 16:22:53 -07004898 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004899 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004900 if (summary != null) {
4901 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4902 } else {
4903 mN.extras.remove(EXTRA_SUB_TEXT);
4904 }
4905 mN.color = color;
4906 return header;
4907 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004908
Selim Cinek6743c0b2017-01-18 18:24:01 -08004909 private CharSequence createSummaryText() {
4910 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4911 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4912 return titleText;
4913 }
4914 SpannableStringBuilder summary = new SpannableStringBuilder();
4915 if (titleText == null) {
4916 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4917 }
4918 BidiFormatter bidi = BidiFormatter.getInstance();
4919 if (titleText != null) {
4920 summary.append(bidi.unicodeWrap(titleText));
4921 }
4922 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4923 if (titleText != null && contentText != null) {
4924 summary.append(bidi.unicodeWrap(mContext.getText(
4925 R.string.notification_header_divider_symbol_with_spaces)));
4926 }
4927 if (contentText != null) {
4928 summary.append(bidi.unicodeWrap(contentText));
4929 }
4930 return summary;
4931 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004932
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004933 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004934 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004935 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004936 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004937 emphazisedMode ? getEmphasizedActionLayoutResource()
4938 : tombstone ? getActionTombstoneLayoutResource()
4939 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004940 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004941 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004942 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004943 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004944 if (action.mRemoteInputs != null) {
4945 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4946 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004947 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004948 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004949 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004950 int bgColor;
4951 if (isColorized()) {
4952 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4953 } else {
4954 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4955 : R.color.notification_action_list_dark);
4956 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004957 button.setDrawableTint(R.id.button_holder, true,
4958 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004959 CharSequence title = action.title;
4960 ColorStateList[] outResultColor = null;
4961 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004962 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004963 } else {
4964 outResultColor = new ColorStateList[1];
4965 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4966 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004967 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004968 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004969 if (outResultColor != null && outResultColor[0] != null) {
4970 // We need to set the text color as well since changing a text to uppercase
4971 // clears its spans.
4972 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004973 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004974 button.setTextColor(R.id.action0,resolveContrastColor());
4975 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004976 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004977 button.setTextViewText(R.id.action0, processTextSpans(
4978 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004979 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004980 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004981 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004982 button.setTextColor(R.id.action0,
4983 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004984 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004985 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004986 return button;
4987 }
4988
Joe Onoratocb109a02011-01-18 17:57:41 -08004989 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004990 * Ensures contrast on color spans against a background color. also returns the color of the
4991 * text if a span was found that spans over the whole text.
4992 *
4993 * @param charSequence the charSequence on which the spans are
4994 * @param background the background color to ensure the contrast against
4995 * @param outResultColor an array in which a color will be returned as the first element if
4996 * there exists a full length color span.
4997 * @return the contrasted charSequence
4998 */
4999 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
5000 ColorStateList[] outResultColor) {
5001 if (charSequence instanceof Spanned) {
5002 Spanned ss = (Spanned) charSequence;
5003 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
5004 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
5005 for (Object span : spans) {
5006 Object resultSpan = span;
5007 int spanStart = ss.getSpanStart(span);
5008 int spanEnd = ss.getSpanEnd(span);
5009 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
5010 if (resultSpan instanceof CharacterStyle) {
5011 resultSpan = ((CharacterStyle) span).getUnderlying();
5012 }
5013 if (resultSpan instanceof TextAppearanceSpan) {
5014 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
5015 ColorStateList textColor = originalSpan.getTextColor();
5016 if (textColor != null) {
5017 int[] colors = textColor.getColors();
5018 int[] newColors = new int[colors.length];
5019 for (int i = 0; i < newColors.length; i++) {
5020 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005021 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005022 }
5023 textColor = new ColorStateList(textColor.getStates().clone(),
5024 newColors);
5025 resultSpan = new TextAppearanceSpan(
5026 originalSpan.getFamily(),
5027 originalSpan.getTextStyle(),
5028 originalSpan.getTextSize(),
5029 textColor,
5030 originalSpan.getLinkTextColor());
5031 if (fullLength) {
5032 outResultColor[0] = new ColorStateList(
5033 textColor.getStates().clone(), newColors);
5034 }
5035 }
5036 } else if (resultSpan instanceof ForegroundColorSpan) {
5037 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
5038 int foregroundColor = originalSpan.getForegroundColor();
5039 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07005040 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07005041 resultSpan = new ForegroundColorSpan(foregroundColor);
5042 if (fullLength) {
5043 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
5044 }
5045 } else {
5046 resultSpan = span;
5047 }
5048
5049 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
5050 }
5051 return builder;
5052 }
5053 return charSequence;
5054 }
5055
5056 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005057 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07005058 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005059 */
5060 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08005061 if (!mIsLegacyInitialized) {
5062 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
5063 < Build.VERSION_CODES.LOLLIPOP;
5064 mIsLegacyInitialized = true;
5065 }
5066 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005067 }
5068
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005069 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08005070 return processLegacyText(charSequence, false /* ambient */);
5071 }
5072
5073 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
5074 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
5075 boolean wantLightText = ambient;
5076 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005077 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005078 } else {
5079 return charSequence;
5080 }
5081 }
5082
Dan Sandler26e81cf2014-05-06 10:01:27 -04005083 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005084 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04005085 */
Adrian Roos487374f2017-01-11 15:48:14 -08005086 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
5087 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08005088 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08005089 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08005090 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07005091 contentView.setDrawableTint(R.id.icon, false, color,
5092 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08005093
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005094 }
Selim Cinekea4bef72015-12-02 15:51:10 -08005095 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08005096 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005097 }
5098
Dan Sandler26e81cf2014-05-06 10:01:27 -04005099 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005100 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04005101 * if it's grayscale).
5102 */
5103 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04005104 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
5105 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005106 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07005107 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07005108 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
5109 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01005110 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005111 }
5112
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05005113 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005114 if (mN.color != COLOR_DEFAULT) {
5115 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02005116 }
Jorim Jaggi74419312014-06-10 20:57:21 +02005117 }
5118
Adrian Roos4ff3b122016-02-01 12:26:13 -08005119 int resolveContrastColor() {
5120 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
5121 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005122 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005123
Selim Cinekac5f0272017-05-02 16:05:41 -07005124 int color;
5125 int background = mBackgroundColorHint;
5126 if (mBackgroundColorHint == COLOR_INVALID) {
5127 background = mContext.getColor(
5128 com.android.internal.R.color.notification_material_background_color);
5129 }
5130 if (mN.color == COLOR_DEFAULT) {
5131 ensureColors();
5132 color = mSecondaryTextColor;
5133 } else {
5134 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07005135 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07005136 }
5137 if (Color.alpha(color) < 255) {
5138 // alpha doesn't go well for color filters, so let's blend it manually
5139 color = NotificationColorUtil.compositeColors(color, background);
5140 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08005141 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07005142 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04005143 }
5144
Adrian Roos487374f2017-01-11 15:48:14 -08005145 int resolveAmbientColor() {
5146 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
5147 return mCachedAmbientColor;
5148 }
5149 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
5150
5151 mCachedAmbientColorIsFor = mN.color;
5152 return mCachedAmbientColor = contrasted;
5153 }
5154
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005155 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005156 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005157 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08005158 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005159 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005160 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005161 mN.actions = new Action[mActions.size()];
5162 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04005163 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005164 if (!mPersonList.isEmpty()) {
Selim Cineke7238dd2017-12-14 17:48:32 -08005165 mN.extras.putParcelableArrayList(EXTRA_PEOPLE_LIST, mPersonList);
Dan Sandler0bf2ed82013-12-21 23:33:41 -06005166 }
Selim Cinek247fa012016-02-18 09:50:48 -08005167 if (mN.bigContentView != null || mN.contentView != null
5168 || mN.headsUpContentView != null) {
5169 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
5170 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005171 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08005172 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005173
Julia Reynolds3b848122016-02-26 10:45:32 -05005174 /**
5175 * Creates a Builder from an existing notification so further changes can be made.
5176 * @param context The context for your application / activity.
5177 * @param n The notification to create a Builder from.
5178 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005179 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005180 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005181 ApplicationInfo applicationInfo = n.extras.getParcelable(
5182 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005183 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05005184 if (applicationInfo != null) {
5185 try {
5186 builderContext = context.createApplicationContext(applicationInfo,
5187 Context.CONTEXT_RESTRICTED);
5188 } catch (NameNotFoundException e) {
5189 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
5190 builderContext = context; // try with our context
5191 }
5192 } else {
5193 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02005194 }
5195
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005196 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005197 }
5198
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005199 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005200 * @deprecated Use {@link #build()} instead.
5201 */
5202 @Deprecated
5203 public Notification getNotification() {
5204 return build();
5205 }
5206
5207 /**
5208 * Combine all of the options that have been set and return a new {@link Notification}
5209 * object.
5210 */
5211 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005212 // first, add any extras from the calling code
5213 if (mUserExtras != null) {
5214 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005215 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005216
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005217 mN.creationTime = System.currentTimeMillis();
5218
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005219 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05005220 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02005221
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005222 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005223
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005224 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02005225 mStyle.reduceImageSizes(mContext);
5226 mStyle.purgeResources();
Selim Cinek90343862018-02-01 11:07:11 -08005227 mStyle.validate(mContext);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005228 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005229 }
Selim Cinekd0426622017-07-11 13:19:59 +02005230 mN.reduceImageSizes(mContext);
5231
Adrian Roos5081c0d2016-02-26 16:04:19 -08005232 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07005233 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005234 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005235 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005236 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
5237 mN.contentView.getSequenceNumber());
5238 }
5239 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005240 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005241 if (mN.bigContentView != null) {
5242 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
5243 mN.bigContentView.getSequenceNumber());
5244 }
5245 }
5246 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05005247 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005248 if (mN.headsUpContentView != null) {
5249 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
5250 mN.headsUpContentView.getSequenceNumber());
5251 }
5252 }
5253 }
5254
Julia Reynolds4c0c2022016-02-02 15:11:59 -05005255 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
5256 mN.flags |= FLAG_SHOW_LIGHTS;
5257 }
5258
Adrian Roosfb921842017-10-26 14:49:56 +02005259 mN.allPendingIntents = null;
5260
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005261 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005262 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005263
5264 /**
5265 * Apply this Builder to an existing {@link Notification} object.
5266 *
5267 * @hide
5268 */
5269 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005270 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005271 return n;
5272 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005273
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005274 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005275 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005276 * change. Also removes extenders on low ram devices, as
5277 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005278 *
5279 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5280 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005281 * @hide
5282 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005283 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005284 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005285
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005286 // Only strip views for known Styles because we won't know how to
5287 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005288 if (!isLowRam
5289 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005290 && getNotificationStyleClass(templateClass) == null) {
5291 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005292 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005293
5294 // Only strip unmodified BuilderRemoteViews.
5295 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005296 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005297 n.contentView.getSequenceNumber();
5298 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005299 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005300 n.bigContentView.getSequenceNumber();
5301 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005302 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005303 n.headsUpContentView.getSequenceNumber();
5304
5305 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005306 if (!isLowRam
5307 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005308 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005309 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005310
5311 Notification clone = n.clone();
5312 if (stripContentView) {
5313 clone.contentView = null;
5314 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5315 }
5316 if (stripBigContentView) {
5317 clone.bigContentView = null;
5318 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5319 }
5320 if (stripHeadsUpContentView) {
5321 clone.headsUpContentView = null;
5322 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5323 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005324 if (isLowRam) {
5325 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5326 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5327 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5328 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005329 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005330 }
5331
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005332 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005333 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005334 }
5335
5336 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005337 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005338 }
5339
5340 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005341 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005342 }
5343
5344 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005345 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005346 }
5347
5348 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005349 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005350 }
5351
Adrian Roosc1a80b02016-04-05 14:54:55 -07005352 private int getMessagingLayoutResource() {
5353 return R.layout.notification_template_material_messaging;
5354 }
5355
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005356 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005357 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005358 }
5359
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005360 private int getEmphasizedActionLayoutResource() {
5361 return R.layout.notification_material_action_emphasized;
5362 }
5363
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005364 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005365 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005366 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005367
5368 private int getBackgroundColor() {
5369 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005370 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005371 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005372 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5373 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005374 }
5375 }
5376
5377 private boolean isColorized() {
5378 return mN.isColorized();
5379 }
Selim Cinek99104832017-01-25 14:47:33 -08005380
Anthony Chenad4d1582017-04-10 16:07:58 -07005381 private boolean shouldTintActionButtons() {
5382 return mTintActionButtons;
5383 }
5384
Selim Cinek99104832017-01-25 14:47:33 -08005385 private boolean textColorsNeedInversion() {
5386 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5387 return false;
5388 }
5389 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5390 return targetSdkVersion > Build.VERSION_CODES.M
5391 && targetSdkVersion < Build.VERSION_CODES.O;
5392 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005393
5394 /**
5395 * Set a color palette to be used as the background and textColors
5396 *
5397 * @param backgroundColor the color to be used as the background
5398 * @param foregroundColor the color to be used as the foreground
5399 *
5400 * @hide
5401 */
5402 public void setColorPalette(int backgroundColor, int foregroundColor) {
5403 mBackgroundColor = backgroundColor;
5404 mForegroundColor = foregroundColor;
5405 mTextColorsAreForBackground = COLOR_INVALID;
5406 ensureColors();
5407 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005408
5409 /**
5410 * Sets the background color for this notification to be a different one then the default.
5411 * This is mainly used to calculate contrast and won't necessarily be applied to the
5412 * background.
5413 *
5414 * @hide
5415 */
5416 public void setBackgroundColorHint(int backgroundColor) {
5417 mBackgroundColorHint = backgroundColor;
5418 }
Selim Cineka7679b62017-05-10 16:33:25 -07005419
5420
5421 /**
5422 * Forces all styled remoteViews to be built from scratch and not use any cached
5423 * RemoteViews.
5424 * This is needed for legacy apps that are baking in their remoteviews into the
5425 * notification.
5426 *
5427 * @hide
5428 */
5429 public void setRebuildStyledRemoteViews(boolean rebuild) {
5430 mRebuildStyledRemoteViews = rebuild;
5431 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005432 }
5433
5434 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005435 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5436 * remote views.
5437 *
5438 * @hide
5439 */
5440 void reduceImageSizes(Context context) {
5441 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5442 return;
5443 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005444 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005445 if (mLargeIcon != null || largeIcon != null) {
5446 Resources resources = context.getResources();
5447 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005448 int maxWidth = resources.getDimensionPixelSize(isLowRam
5449 ? R.dimen.notification_right_icon_size_low_ram
5450 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005451 int maxHeight = maxWidth;
5452 if (MediaStyle.class.equals(style)
5453 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005454 maxHeight = resources.getDimensionPixelSize(isLowRam
5455 ? R.dimen.notification_media_image_max_height_low_ram
5456 : R.dimen.notification_media_image_max_height);
5457 maxWidth = resources.getDimensionPixelSize(isLowRam
5458 ? R.dimen.notification_media_image_max_width_low_ram
5459 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005460 }
5461 if (mLargeIcon != null) {
5462 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5463 }
5464 if (largeIcon != null) {
5465 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5466 }
5467 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005468 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5469 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5470 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005471 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5472 }
5473
Selim Cineka8cb1262017-08-15 16:53:44 -07005474 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5475 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005476 if (remoteView != null) {
5477 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005478 int maxWidth = resources.getDimensionPixelSize(isLowRam
5479 ? R.dimen.notification_custom_view_max_image_width_low_ram
5480 : R.dimen.notification_custom_view_max_image_width);
5481 int maxHeight = resources.getDimensionPixelSize(isLowRam
5482 ? R.dimen.notification_custom_view_max_image_height_low_ram
5483 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005484 remoteView.reduceImageSizes(maxWidth, maxHeight);
5485 }
5486 }
5487
5488 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005489 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005490 */
Selim Cinek22714f12017-04-13 16:23:53 -07005491 private boolean isForegroundService() {
5492 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005493 }
5494
5495 /**
Selim Cinek99104832017-01-25 14:47:33 -08005496 * @return whether this notification has a media session attached
5497 * @hide
5498 */
5499 public boolean hasMediaSession() {
5500 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5501 }
5502
5503 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005504 * @return the style class of this notification
5505 * @hide
5506 */
5507 public Class<? extends Notification.Style> getNotificationStyle() {
5508 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5509
5510 if (!TextUtils.isEmpty(templateClass)) {
5511 return Notification.getNotificationStyleClass(templateClass);
5512 }
5513 return null;
5514 }
5515
5516 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005517 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005518 *
5519 * @hide
5520 */
5521 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005522 if (isColorizedMedia()) {
5523 return true;
5524 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005525 return extras.getBoolean(EXTRA_COLORIZED)
5526 && (hasColorizedPermission() || isForegroundService());
5527 }
5528
5529 /**
5530 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5531 * permission. The permission is checked when a notification is enqueued.
5532 */
5533 private boolean hasColorizedPermission() {
5534 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005535 }
5536
5537 /**
5538 * @return true if this notification is colorized and it is a media notification
5539 *
5540 * @hide
5541 */
5542 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005543 Class<? extends Style> style = getNotificationStyle();
5544 if (MediaStyle.class.equals(style)) {
5545 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5546 if ((colorized == null || colorized) && hasMediaSession()) {
5547 return true;
5548 }
5549 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5550 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5551 return true;
5552 }
5553 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005554 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005555 }
5556
Selim Cinek0847acd2017-04-24 19:48:29 -07005557
5558 /**
5559 * @return true if this is a media notification
5560 *
5561 * @hide
5562 */
5563 public boolean isMediaNotification() {
5564 Class<? extends Style> style = getNotificationStyle();
5565 if (MediaStyle.class.equals(style)) {
5566 return true;
5567 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5568 return true;
5569 }
5570 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005571 }
5572
Selim Cinek279fa862016-06-14 10:57:25 -07005573 private boolean hasLargeIcon() {
5574 return mLargeIcon != null || largeIcon != null;
5575 }
5576
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005577 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005578 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005579 * @hide
5580 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005581 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005582 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5583 }
5584
5585 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005586 * @return true if the notification will show a chronometer; false otherwise
5587 * @hide
5588 */
5589 public boolean showsChronometer() {
5590 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5591 }
5592
5593 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005594 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005595 */
5596 @SystemApi
5597 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5598 Class<? extends Style>[] classes = new Class[] {
5599 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5600 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5601 MessagingStyle.class };
5602 for (Class<? extends Style> innerClass : classes) {
5603 if (templateClass.equals(innerClass.getName())) {
5604 return innerClass;
5605 }
5606 }
5607 return null;
5608 }
5609
5610 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005611 * An object that can apply a rich notification style to a {@link Notification.Builder}
5612 * object.
5613 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005614 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005615 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005616
5617 /**
5618 * @hide
5619 */
5620 protected CharSequence mSummaryText = null;
5621
5622 /**
5623 * @hide
5624 */
5625 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005626
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005627 protected Builder mBuilder;
5628
Chris Wrend6297db2012-05-03 16:20:13 -04005629 /**
5630 * Overrides ContentTitle in the big form of the template.
5631 * This defaults to the value passed to setContentTitle().
5632 */
5633 protected void internalSetBigContentTitle(CharSequence title) {
5634 mBigContentTitle = title;
5635 }
5636
5637 /**
5638 * Set the first line of text after the detail section in the big form of the template.
5639 */
5640 protected void internalSetSummaryText(CharSequence cs) {
5641 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005642 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005643 }
5644
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005645 public void setBuilder(Builder builder) {
5646 if (mBuilder != builder) {
5647 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005648 if (mBuilder != null) {
5649 mBuilder.setStyle(this);
5650 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005651 }
5652 }
5653
Chris Wrend6297db2012-05-03 16:20:13 -04005654 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005655 if (mBuilder == null) {
5656 throw new IllegalArgumentException("Style requires a valid Builder object");
5657 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005658 }
Chris Wrend6297db2012-05-03 16:20:13 -04005659
5660 protected RemoteViews getStandardView(int layoutId) {
5661 checkBuilder();
5662
Christoph Studer4600f9b2014-07-22 22:44:43 +02005663 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005664 CharSequence oldBuilderContentTitle =
5665 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005666 if (mBigContentTitle != null) {
5667 mBuilder.setContentTitle(mBigContentTitle);
5668 }
5669
Chris Wrend6297db2012-05-03 16:20:13 -04005670 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5671
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005672 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005673
Chris Wrend6297db2012-05-03 16:20:13 -04005674 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5675 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005676 } else {
5677 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005678 }
5679
Chris Wrend6297db2012-05-03 16:20:13 -04005680 return contentView;
5681 }
5682
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005683 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005684 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005685 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005686 *
5687 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005688 * @hide
5689 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005690 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005691 return null;
5692 }
5693
5694 /**
5695 * Construct a Style-specific RemoteViews for the final big notification layout.
5696 * @hide
5697 */
5698 public RemoteViews makeBigContentView() {
5699 return null;
5700 }
5701
5702 /**
5703 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005704 *
5705 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005706 * @hide
5707 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005708 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005709 return null;
5710 }
5711
5712 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005713 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005714 * @hide
5715 */
5716 public void addExtras(Bundle extras) {
5717 if (mSummaryTextSet) {
5718 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5719 }
5720 if (mBigContentTitle != null) {
5721 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5722 }
Chris Wren91ad5632013-06-05 15:05:57 -04005723 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005724 }
5725
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005726 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005727 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005728 * @hide
5729 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005730 protected void restoreFromExtras(Bundle extras) {
5731 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5732 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5733 mSummaryTextSet = true;
5734 }
5735 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5736 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5737 }
5738 }
5739
5740
5741 /**
5742 * @hide
5743 */
5744 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005745 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005746 return wip;
5747 }
5748
Daniel Sandler0ec46202015-06-24 01:27:05 -04005749 /**
5750 * @hide
5751 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005752 public void purgeResources() {}
5753
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005754 /**
5755 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5756 * attached to.
5757 *
5758 * @return the fully constructed Notification.
5759 */
5760 public Notification build() {
5761 checkBuilder();
5762 return mBuilder.build();
5763 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005764
5765 /**
5766 * @hide
5767 * @return true if the style positions the progress bar on the second line; false if the
5768 * style hides the progress bar
5769 */
5770 protected boolean hasProgress() {
5771 return true;
5772 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005773
5774 /**
5775 * @hide
5776 * @return Whether we should put the summary be put into the notification header
5777 */
5778 public boolean hasSummaryInHeader() {
5779 return true;
5780 }
Selim Cinek593610c2016-02-16 18:42:57 -08005781
5782 /**
5783 * @hide
5784 * @return Whether custom content views are displayed inline in the style
5785 */
5786 public boolean displayCustomViewInline() {
5787 return false;
5788 }
Selim Cinekd0426622017-07-11 13:19:59 +02005789
5790 /**
5791 * Reduces the image sizes contained in this style.
5792 *
5793 * @hide
5794 */
5795 public void reduceImageSizes(Context context) {
5796 }
Selim Cinek90343862018-02-01 11:07:11 -08005797
5798 /**
5799 * Validate that this style was properly composed. This is called at build time.
5800 * @hide
5801 */
5802 public void validate(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
Selim Cinek90343862018-02-01 11:07:11 -08006194 * by this user. The user also needs to have a valid name associated with it, which will
6195 * be enforced starting in Android P.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006196 */
6197 public MessagingStyle(@NonNull Person user) {
6198 mUser = user;
Selim Cinek90343862018-02-01 11:07:11 -08006199 }
6200
6201 /**
6202 * Validate that this style was properly composed. This is called at build time.
6203 * @hide
6204 */
6205 @Override
6206 public void validate(Context context) {
6207 super.validate(context);
6208 if (context.getApplicationInfo().targetSdkVersion
6209 >= Build.VERSION_CODES.P && (mUser == null || mUser.getName() == null)) {
6210 throw new RuntimeException("User must be valid and have a name.");
Selim Cinekcb8b9852017-12-15 18:01:52 -08006211 }
6212 }
6213
6214 /**
6215 * @return the user to be displayed for any replies sent by the user
6216 */
6217 public Person getUser() {
6218 return mUser;
Alex Hillsfc737de2016-03-23 17:33:02 -04006219 }
6220
6221 /**
6222 * Returns the name to be displayed for any replies sent by the user
Selim Cinekcb8b9852017-12-15 18:01:52 -08006223 *
6224 * @deprecated use {@link #getUser()} instead
Alex Hillsfc737de2016-03-23 17:33:02 -04006225 */
6226 public CharSequence getUserDisplayName() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006227 return mUser.getName();
Alex Hillsfc737de2016-03-23 17:33:02 -04006228 }
6229
6230 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006231 * Sets the title to be displayed on this conversation. May be set to {@code null}.
6232 *
Kodlee Yin14656422017-12-22 17:00:46 -08006233 * <p>This API's behavior was changed in SDK version {@link Build.VERSION_CODES#P}. If your
6234 * application's target version is less than {@link Build.VERSION_CODES#P}, setting a
6235 * conversation title to a non-null value will make {@link #isGroupConversation()} return
6236 * {@code true} and passing {@code null} will make it return {@code false}. In
6237 * {@link Build.VERSION_CODES#P} and beyond, use {@link #setGroupConversation(boolean)}
6238 * to set group conversation status.
6239 *
6240 * @param conversationTitle Title displayed for this conversation
6241 * @return this object for method chaining
Alex Hillsfc737de2016-03-23 17:33:02 -04006242 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006243 public MessagingStyle setConversationTitle(@Nullable CharSequence conversationTitle) {
Alex Hillsfc737de2016-03-23 17:33:02 -04006244 mConversationTitle = conversationTitle;
6245 return this;
6246 }
6247
6248 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006249 * Return the title to be displayed on this conversation. May return {@code null}.
Alex Hillsfc737de2016-03-23 17:33:02 -04006250 */
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006251 @Nullable
Alex Hillsfc737de2016-03-23 17:33:02 -04006252 public CharSequence getConversationTitle() {
6253 return mConversationTitle;
6254 }
6255
6256 /**
6257 * Adds a message for display by this notification. Convenience call for a simple
6258 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6259 * @param text A {@link CharSequence} to be displayed as the message content
6260 * @param timestamp Time at which the message arrived
6261 * @param sender A {@link CharSequence} to be used for displaying the name of the
6262 * sender. Should be <code>null</code> for messages by the current user, in which case
6263 * the platform will insert {@link #getUserDisplayName()}.
6264 * Should be unique amongst all individuals in the conversation, and should be
6265 * consistent during re-posts of the notification.
6266 *
6267 * @see Message#Message(CharSequence, long, CharSequence)
6268 *
6269 * @return this object for method chaining
Selim Cinekcb8b9852017-12-15 18:01:52 -08006270 *
6271 * @deprecated use {@link #addMessage(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006272 */
6273 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006274 return addMessage(text, timestamp,
6275 sender == null ? null : new Person().setName(sender));
6276 }
6277
6278 /**
6279 * Adds a message for display by this notification. Convenience call for a simple
6280 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
6281 * @param text A {@link CharSequence} to be displayed as the message content
6282 * @param timestamp Time at which the message arrived
6283 * @param sender The {@link Person} who sent the message.
6284 * Should be <code>null</code> for messages by the current user, in which case
6285 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6286 *
6287 * @see Message#Message(CharSequence, long, CharSequence)
6288 *
6289 * @return this object for method chaining
6290 */
6291 public MessagingStyle addMessage(CharSequence text, long timestamp, Person sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08006292 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04006293 }
6294
6295 /**
6296 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08006297 *
6298 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6299 * the newest last.
6300 *
Alex Hillsfc737de2016-03-23 17:33:02 -04006301 * @param message The {@link Message} to be displayed
6302 * @return this object for method chaining
6303 */
6304 public MessagingStyle addMessage(Message message) {
6305 mMessages.add(message);
6306 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6307 mMessages.remove(0);
6308 }
6309 return this;
6310 }
6311
6312 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006313 * Adds a {@link Message} for historic context in this notification.
6314 *
6315 * <p>Messages should be added as historic if they are not the main subject of the
6316 * notification but may give context to a conversation. The system may choose to present
6317 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
6318 *
6319 * <p>The messages should be added in chronologic order, i.e. the oldest first,
6320 * the newest last.
6321 *
6322 * @param message The historic {@link Message} to be added
6323 * @return this object for method chaining
6324 */
6325 public MessagingStyle addHistoricMessage(Message message) {
6326 mHistoricMessages.add(message);
6327 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
6328 mHistoricMessages.remove(0);
6329 }
6330 return this;
6331 }
6332
6333 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006334 * Gets the list of {@code Message} objects that represent the notification
6335 */
6336 public List<Message> getMessages() {
6337 return mMessages;
6338 }
6339
6340 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006341 * Gets the list of historic {@code Message}s in the notification.
6342 */
6343 public List<Message> getHistoricMessages() {
6344 return mHistoricMessages;
6345 }
6346
6347 /**
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006348 * Sets whether this conversation notification represents a group.
Kodlee Yin14656422017-12-22 17:00:46 -08006349 *
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006350 * @param isGroupConversation {@code true} if the conversation represents a group,
6351 * {@code false} otherwise.
6352 * @return this object for method chaining
6353 */
6354 public MessagingStyle setGroupConversation(boolean isGroupConversation) {
6355 mIsGroupConversation = isGroupConversation;
6356 return this;
6357 }
6358
6359 /**
Kodlee Yin14656422017-12-22 17:00:46 -08006360 * Returns {@code true} if this notification represents a group conversation, otherwise
6361 * {@code false}.
6362 *
6363 * <p> If the application that generated this {@link MessagingStyle} targets an SDK version
6364 * less than {@link Build.VERSION_CODES#P}, this method becomes dependent on whether or
6365 * not the conversation title is set; returning {@code true} if the conversation title is
6366 * a non-null value, or {@code false} otherwise. From {@link Build.VERSION_CODES#P} forward,
6367 * this method returns what's set by {@link #setGroupConversation(boolean)} allowing for
6368 * named, non-group conversations.
6369 *
6370 * @see #setConversationTitle(CharSequence)
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006371 */
6372 public boolean isGroupConversation() {
Kodlee Yin14656422017-12-22 17:00:46 -08006373 // When target SDK version is < P, a non-null conversation title dictates if this is
6374 // as group conversation.
6375 if (mBuilder != null
6376 && mBuilder.mContext.getApplicationInfo().targetSdkVersion
6377 < Build.VERSION_CODES.P) {
6378 return mConversationTitle != null;
6379 }
6380
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006381 return mIsGroupConversation;
6382 }
6383
6384 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006385 * @hide
6386 */
6387 @Override
6388 public void addExtras(Bundle extras) {
6389 super.addExtras(extras);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006390 if (mUser != null) {
6391 // For legacy usages
6392 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUser.getName());
6393 extras.putParcelable(EXTRA_MESSAGING_PERSON, mUser);
Alex Hillsfc737de2016-03-23 17:33:02 -04006394 }
6395 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006396 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006397 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006398 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6399 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006400 }
Adrian Roos437cd562017-01-18 15:47:03 -08006401 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6402 Message.getBundleArrayForMessages(mHistoricMessages));
6403 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006404
6405 fixTitleAndTextExtras(extras);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006406 extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, mIsGroupConversation);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006407 }
6408
6409 private void fixTitleAndTextExtras(Bundle extras) {
6410 Message m = findLatestIncomingMessage();
6411 CharSequence text = (m == null) ? null : m.mText;
6412 CharSequence sender = m == null ? null
Selim Cinekcb8b9852017-12-15 18:01:52 -08006413 : m.mSender == null || TextUtils.isEmpty(m.mSender.getName())
6414 ? mUser.getName() : m.mSender.getName();
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006415 CharSequence title;
6416 if (!TextUtils.isEmpty(mConversationTitle)) {
6417 if (!TextUtils.isEmpty(sender)) {
6418 BidiFormatter bidi = BidiFormatter.getInstance();
6419 title = mBuilder.mContext.getString(
6420 com.android.internal.R.string.notification_messaging_title_template,
Selim Cinekcb8b9852017-12-15 18:01:52 -08006421 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(sender));
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006422 } else {
6423 title = mConversationTitle;
6424 }
6425 } else {
6426 title = sender;
6427 }
6428
6429 if (title != null) {
6430 extras.putCharSequence(EXTRA_TITLE, title);
6431 }
6432 if (text != null) {
6433 extras.putCharSequence(EXTRA_TEXT, text);
6434 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006435 }
6436
6437 /**
6438 * @hide
6439 */
6440 @Override
6441 protected void restoreFromExtras(Bundle extras) {
6442 super.restoreFromExtras(extras);
6443
Selim Cinekcb8b9852017-12-15 18:01:52 -08006444 mUser = extras.getParcelable(EXTRA_MESSAGING_PERSON);
6445 if (mUser == null) {
6446 CharSequence displayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6447 mUser = new Person().setName(displayName);
6448 }
Adrian Roos96b7e202016-05-17 13:50:38 -07006449 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006450 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006451 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006452 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006453 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Kodlee Yin9ac617c2017-12-19 11:20:50 -08006454 mIsGroupConversation = extras.getBoolean(EXTRA_IS_GROUP_CONVERSATION);
Alex Hillsfc737de2016-03-23 17:33:02 -04006455 }
6456
6457 /**
6458 * @hide
6459 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006460 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006461 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006462 mBuilder.mOriginalActions = mBuilder.mActions;
6463 mBuilder.mActions = new ArrayList<>();
Selim Cinek7199ed92018-01-26 13:36:43 -08006464 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006465 mBuilder.mActions = mBuilder.mOriginalActions;
6466 mBuilder.mOriginalActions = null;
6467 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006468 }
6469
6470 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006471 return findLatestIncomingMessage(mMessages);
6472 }
6473
6474 /**
6475 * @hide
6476 */
6477 @Nullable
6478 public static Message findLatestIncomingMessage(
6479 List<Message> messages) {
6480 for (int i = messages.size() - 1; i >= 0; i--) {
6481 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006482 // Incoming messages have a non-empty sender.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006483 if (m.mSender != null && !TextUtils.isEmpty(m.mSender.getName())) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07006484 return m;
6485 }
6486 }
Selim Cinek88188f22017-09-19 16:46:56 -07006487 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006488 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006489 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006490 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006491 return null;
6492 }
6493
6494 /**
6495 * @hide
6496 */
6497 @Override
6498 public RemoteViews makeBigContentView() {
Selim Cinek7199ed92018-01-26 13:36:43 -08006499 return makeMessagingView(false /* isCollapsed */);
Selim Cinekafeed292017-12-12 17:32:44 -08006500 }
6501
6502 @NonNull
Selim Cinek7199ed92018-01-26 13:36:43 -08006503 private RemoteViews makeMessagingView(boolean isCollapsed) {
Selim Cinek88188f22017-09-19 16:46:56 -07006504 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006505 ? super.mBigContentTitle
6506 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006507 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006508 CharSequence nameReplacement = null;
Selim Cinekafeed292017-12-12 17:32:44 -08006509 if (hasOnlyWhiteSpaceSenders()) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006510 isOneToOne = true;
Selim Cinek2dd3e722018-01-19 11:06:06 -08006511 nameReplacement = conversationTitle;
6512 conversationTitle = null;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006513 }
Selim Cinek7199ed92018-01-26 13:36:43 -08006514 boolean hideLargeIcon = !isCollapsed || isOneToOne;
Adrian Roos48d746a2016-04-12 14:57:28 -07006515 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006516 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006517 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
Selim Cinek7199ed92018-01-26 13:36:43 -08006518 .hideLargeIcon(hideLargeIcon)
Selim Cinekafeed292017-12-12 17:32:44 -08006519 .headerTextSecondary(conversationTitle)
Selim Cinek7199ed92018-01-26 13:36:43 -08006520 .alwaysShowReply(isCollapsed));
Selim Cinek88188f22017-09-19 16:46:56 -07006521 addExtras(mBuilder.mN.extras);
Selim Cinekafeed292017-12-12 17:32:44 -08006522 // also update the end margin if there is an image
6523 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek7199ed92018-01-26 13:36:43 -08006524 if (isCollapsed) {
Selim Cinekafeed292017-12-12 17:32:44 -08006525 endMargin = R.dimen.notification_content_plus_picture_margin_end;
6526 }
6527 contentView.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07006528 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6529 mBuilder.resolveContrastColor());
Selim Cinek7199ed92018-01-26 13:36:43 -08006530 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsCollapsed",
6531 isCollapsed);
Selim Cinek88188f22017-09-19 16:46:56 -07006532 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6533 mBuilder.mN.mLargeIcon);
Selim Cinek2dd3e722018-01-19 11:06:06 -08006534 contentView.setCharSequence(R.id.status_bar_latest_event_content, "setNameReplacement",
6535 nameReplacement);
Selim Cinek88188f22017-09-19 16:46:56 -07006536 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6537 isOneToOne);
6538 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6539 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006540 return contentView;
6541 }
6542
Selim Cinekf7409db2017-10-24 16:17:14 -07006543 private boolean hasOnlyWhiteSpaceSenders() {
6544 for (int i = 0; i < mMessages.size(); i++) {
6545 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006546 Person sender = m.getSenderPerson();
6547 if (sender != null && !isWhiteSpace(sender.getName())) {
Selim Cinekf7409db2017-10-24 16:17:14 -07006548 return false;
6549 }
6550 }
6551 return true;
6552 }
6553
6554 private boolean isWhiteSpace(CharSequence sender) {
6555 if (TextUtils.isEmpty(sender)) {
6556 return true;
6557 }
6558 if (sender.toString().matches("^\\s*$")) {
6559 return true;
6560 }
6561 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6562 // For the presentation that we had.
6563 for (int i = 0; i < sender.length(); i++) {
6564 char c = sender.charAt(i);
6565 if (c != '\u200B') {
6566 return false;
6567 }
6568 }
6569 return true;
6570 }
6571
Selim Cinek88188f22017-09-19 16:46:56 -07006572 private CharSequence createConversationTitleFromMessages() {
6573 ArraySet<CharSequence> names = new ArraySet<>();
6574 for (int i = 0; i < mMessages.size(); i++) {
6575 Message m = mMessages.get(i);
Selim Cinekcb8b9852017-12-15 18:01:52 -08006576 Person sender = m.getSenderPerson();
Selim Cinek88188f22017-09-19 16:46:56 -07006577 if (sender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006578 names.add(sender.getName());
Selim Cinek88188f22017-09-19 16:46:56 -07006579 }
6580 }
6581 SpannableStringBuilder title = new SpannableStringBuilder();
6582 int size = names.size();
6583 for (int i = 0; i < size; i++) {
6584 CharSequence name = names.valueAt(i);
6585 if (!TextUtils.isEmpty(title)) {
6586 title.append(", ");
6587 }
6588 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6589 }
6590 return title;
6591 }
6592
Adrian Roosdedd1df2016-04-26 16:38:47 -07006593 /**
6594 * @hide
6595 */
6596 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006597 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek7199ed92018-01-26 13:36:43 -08006598 RemoteViews remoteViews = makeMessagingView(true /* isCollapsed */);
Selim Cineke62255c2017-09-28 18:23:23 -07006599 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6600 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006601 }
6602
Adrian Roosc1a80b02016-04-05 14:54:55 -07006603 private static TextAppearanceSpan makeFontColorSpan(int color) {
6604 return new TextAppearanceSpan(null, 0, 0,
6605 ColorStateList.valueOf(color), null);
6606 }
6607
Alex Hillsd9b04d92016-04-11 16:38:16 -04006608 public static final class Message {
6609
6610 static final String KEY_TEXT = "text";
6611 static final String KEY_TIMESTAMP = "time";
6612 static final String KEY_SENDER = "sender";
Selim Cinekcb8b9852017-12-15 18:01:52 -08006613 static final String KEY_SENDER_PERSON = "sender_person";
Alex Hillsd9b04d92016-04-11 16:38:16 -04006614 static final String KEY_DATA_MIME_TYPE = "type";
6615 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006616 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006617
6618 private final CharSequence mText;
6619 private final long mTimestamp;
Selim Cinekcb8b9852017-12-15 18:01:52 -08006620 @Nullable
6621 private final Person mSender;
Alex Hillsfc737de2016-03-23 17:33:02 -04006622
Shane Brennan5a871862017-03-11 13:14:17 -08006623 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006624 private String mDataMimeType;
6625 private Uri mDataUri;
6626
6627 /**
6628 * Constructor
6629 * @param text A {@link CharSequence} to be displayed as the message content
6630 * @param timestamp Time at which the message arrived
6631 * @param sender A {@link CharSequence} to be used for displaying the name of the
6632 * sender. Should be <code>null</code> for messages by the current user, in which case
6633 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6634 * Should be unique amongst all individuals in the conversation, and should be
6635 * consistent during re-posts of the notification.
Selim Cinekcb8b9852017-12-15 18:01:52 -08006636 *
6637 * @deprecated use {@code Message(CharSequence, long, Person)}
Alex Hillsfc737de2016-03-23 17:33:02 -04006638 */
6639 public Message(CharSequence text, long timestamp, CharSequence sender){
Selim Cinekcb8b9852017-12-15 18:01:52 -08006640 this(text, timestamp, sender == null ? null : new Person().setName(sender));
6641 }
6642
6643 /**
6644 * Constructor
6645 * @param text A {@link CharSequence} to be displayed as the message content
6646 * @param timestamp Time at which the message arrived
6647 * @param sender The {@link Person} who sent the message.
6648 * Should be <code>null</code> for messages by the current user, in which case
6649 * the platform will insert the user set in {@code MessagingStyle(Person)}.
6650 * <p>
6651 * The person provided should contain an Icon, set with {@link Person#setIcon(Icon)}
6652 * and also have a name provided with {@link Person#setName(CharSequence)}. If multiple
6653 * users have the same name, consider providing a key with {@link Person#setKey(String)}
6654 * in order to differentiate between the different users.
6655 * </p>
6656 */
6657 public Message(CharSequence text, long timestamp, @Nullable Person sender){
Alex Hillsfc737de2016-03-23 17:33:02 -04006658 mText = text;
6659 mTimestamp = timestamp;
6660 mSender = sender;
6661 }
6662
6663 /**
6664 * Sets a binary blob of data and an associated MIME type for a message. In the case
6665 * where the platform doesn't support the MIME type, the original text provided in the
6666 * constructor will be used.
6667 * @param dataMimeType The MIME type of the content. See
6668 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6669 * types on Android and Android Wear.
6670 * @param dataUri The uri containing the content whose type is given by the MIME type.
6671 * <p class="note">
6672 * <ol>
6673 * <li>Notification Listeners including the System UI need permission to access the
6674 * data the Uri points to. The recommended ways to do this are:</li>
6675 * <li>Store the data in your own ContentProvider, making sure that other apps have
6676 * the correct permission to access your provider. The preferred mechanism for
6677 * providing access is to use per-URI permissions which are temporary and only
6678 * grant access to the receiving application. An easy way to create a
6679 * ContentProvider like this is to use the FileProvider helper class.</li>
6680 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6681 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6682 * also store non-media types (see MediaStore.Files for more info). Files can be
6683 * inserted into the MediaStore using scanFile() after which a content:// style
6684 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6685 * Note that once added to the system MediaStore the content is accessible to any
6686 * app on the device.</li>
6687 * </ol>
6688 * @return this object for method chaining
6689 */
6690 public Message setData(String dataMimeType, Uri dataUri) {
6691 mDataMimeType = dataMimeType;
6692 mDataUri = dataUri;
6693 return this;
6694 }
6695
Alex Hillsfc737de2016-03-23 17:33:02 -04006696 /**
6697 * Get the text to be used for this message, or the fallback text if a type and content
6698 * Uri have been set
6699 */
6700 public CharSequence getText() {
6701 return mText;
6702 }
6703
6704 /**
6705 * Get the time at which this message arrived
6706 */
6707 public long getTimestamp() {
6708 return mTimestamp;
6709 }
6710
6711 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006712 * Get the extras Bundle for this message.
6713 */
6714 public Bundle getExtras() {
6715 return mExtras;
6716 }
6717
6718 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006719 * Get the text used to display the contact's name in the messaging experience
Selim Cinekcb8b9852017-12-15 18:01:52 -08006720 *
6721 * @deprecated use {@link #getSenderPerson()}
Alex Hillsfc737de2016-03-23 17:33:02 -04006722 */
6723 public CharSequence getSender() {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006724 return mSender == null ? null : mSender.getName();
6725 }
6726
6727 /**
6728 * Get the sender associated with this message.
6729 */
6730 @Nullable
6731 public Person getSenderPerson() {
Alex Hillsfc737de2016-03-23 17:33:02 -04006732 return mSender;
6733 }
6734
6735 /**
6736 * Get the MIME type of the data pointed to by the Uri
6737 */
6738 public String getDataMimeType() {
6739 return mDataMimeType;
6740 }
6741
6742 /**
6743 * Get the the Uri pointing to the content of the message. Can be null, in which case
6744 * {@see #getText()} is used.
6745 */
6746 public Uri getDataUri() {
6747 return mDataUri;
6748 }
6749
Alex Hillsd9b04d92016-04-11 16:38:16 -04006750 private Bundle toBundle() {
6751 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006752 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006753 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006754 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006755 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006756 if (mSender != null) {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006757 // Legacy listeners need this
6758 bundle.putCharSequence(KEY_SENDER, mSender.getName());
6759 bundle.putParcelable(KEY_SENDER_PERSON, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006760 }
6761 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006762 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006763 }
6764 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006765 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006766 }
Shane Brennan5a871862017-03-11 13:14:17 -08006767 if (mExtras != null) {
6768 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6769 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006770 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006771 }
6772
Alex Hillsd9b04d92016-04-11 16:38:16 -04006773 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6774 Bundle[] bundles = new Bundle[messages.size()];
6775 final int N = messages.size();
6776 for (int i = 0; i < N; i++) {
6777 bundles[i] = messages.get(i).toBundle();
6778 }
6779 return bundles;
6780 }
6781
Selim Cinek88188f22017-09-19 16:46:56 -07006782 /**
6783 * @return A list of messages read from the bundles.
6784 *
6785 * @hide
6786 */
6787 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6788 if (bundles == null) {
6789 return new ArrayList<>();
6790 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006791 List<Message> messages = new ArrayList<>(bundles.length);
6792 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006793 if (bundles[i] instanceof Bundle) {
6794 Message message = getMessageFromBundle((Bundle)bundles[i]);
6795 if (message != null) {
6796 messages.add(message);
6797 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006798 }
6799 }
6800 return messages;
6801 }
6802
6803 static Message getMessageFromBundle(Bundle bundle) {
6804 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006805 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006806 return null;
6807 } else {
Selim Cinekcb8b9852017-12-15 18:01:52 -08006808
6809 Person senderPerson = bundle.getParcelable(KEY_SENDER_PERSON);
6810 if (senderPerson == null) {
6811 // Legacy apps that use compat don't actually provide the sender objects
6812 // We need to fix the compat version to provide people / use
6813 // the native api instead
6814 CharSequence senderName = bundle.getCharSequence(KEY_SENDER);
6815 if (senderName != null) {
6816 senderPerson = new Person().setName(senderName);
6817 }
6818 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006819 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
Selim Cinekcb8b9852017-12-15 18:01:52 -08006820 bundle.getLong(KEY_TIMESTAMP),
6821 senderPerson);
Alex Hillsd9b04d92016-04-11 16:38:16 -04006822 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6823 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006824 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6825 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006826 }
Shane Brennan5a871862017-03-11 13:14:17 -08006827 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6828 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6829 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006830 return message;
6831 }
6832 } catch (ClassCastException e) {
6833 return null;
6834 }
6835 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006836 }
6837 }
6838
6839 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006840 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006841 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006842 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006843 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006844 * Notification notif = new Notification.Builder(mContext)
6845 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6846 * .setContentText(subject)
6847 * .setSmallIcon(R.drawable.new_mail)
6848 * .setLargeIcon(aBitmap)
6849 * .setStyle(new Notification.InboxStyle()
6850 * .addLine(str1)
6851 * .addLine(str2)
6852 * .setContentTitle(&quot;&quot;)
6853 * .setSummaryText(&quot;+3 more&quot;))
6854 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006855 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006856 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006857 * @see Notification#bigContentView
6858 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006859 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006860 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6861
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006862 public InboxStyle() {
6863 }
6864
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006865 /**
6866 * @deprecated use {@code InboxStyle()}.
6867 */
6868 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006869 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006870 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006871 }
6872
Chris Wrend6297db2012-05-03 16:20:13 -04006873 /**
6874 * Overrides ContentTitle in the big form of the template.
6875 * This defaults to the value passed to setContentTitle().
6876 */
6877 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006878 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006879 return this;
6880 }
6881
6882 /**
6883 * Set the first line of text after the detail section in the big form of the template.
6884 */
6885 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006886 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006887 return this;
6888 }
6889
Chris Wren0bd664d2012-08-01 13:56:56 -04006890 /**
6891 * Append a line to the digest section of the Inbox notification.
6892 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006893 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006894 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006895 return this;
6896 }
6897
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006898 /**
6899 * @hide
6900 */
6901 public void addExtras(Bundle extras) {
6902 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006903
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006904 CharSequence[] a = new CharSequence[mTexts.size()];
6905 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6906 }
6907
Christoph Studer4600f9b2014-07-22 22:44:43 +02006908 /**
6909 * @hide
6910 */
6911 @Override
6912 protected void restoreFromExtras(Bundle extras) {
6913 super.restoreFromExtras(extras);
6914
6915 mTexts.clear();
6916 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6917 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6918 }
6919 }
6920
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006921 /**
6922 * @hide
6923 */
6924 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006925 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006926 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006927 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6928 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006929
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006930 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006931
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006932 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006933
Chris Wrend6297db2012-05-03 16:20:13 -04006934 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 -04006935 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006936
Chris Wren4ed80d52012-05-17 09:30:03 -04006937 // Make sure all rows are gone in case we reuse a view.
6938 for (int rowId : rowIds) {
6939 contentView.setViewVisibility(rowId, View.GONE);
6940 }
6941
Daniel Sandler879c5e02012-04-17 16:46:51 -04006942 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006943 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6944 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006945 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006946 int onlyViewId = 0;
6947 int maxRows = rowIds.length;
6948 if (mBuilder.mActions.size() > 0) {
6949 maxRows--;
6950 }
6951 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006952 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006953 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006954 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006955 contentView.setTextViewText(rowIds[i],
6956 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006957 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006958 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006959 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006960 if (first) {
6961 onlyViewId = rowIds[i];
6962 } else {
6963 onlyViewId = 0;
6964 }
Selim Cinek247fa012016-02-18 09:50:48 -08006965 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006966 }
6967 i++;
6968 }
Selim Cinek07c80172016-04-21 16:40:47 -07006969 if (onlyViewId != 0) {
6970 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6971 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6972 R.dimen.notification_text_margin_top);
6973 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6974 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006975
Daniel Sandler879c5e02012-04-17 16:46:51 -04006976 return contentView;
6977 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006978
Selim Cinek247fa012016-02-18 09:50:48 -08006979 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006980 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006981 if (first) {
6982 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6983 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6984 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006985 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006986 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006987 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006988 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006989 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006990 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006991 }
Dan Sandler842dd772014-05-15 09:36:47 -04006992
6993 /**
6994 * Notification style for media playback notifications.
6995 *
6996 * In the expanded form, {@link Notification#bigContentView}, up to 5
6997 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006998 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006999 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
7000 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
7001 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08007002 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04007003 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
7004 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01007005 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04007006 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08007007 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007008 * Notifications created with MediaStyle will have their category set to
7009 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
7010 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08007011 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07007012 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
7013 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04007014 * the System UI can identify this as a notification representing an active media session
7015 * and respond accordingly (by showing album artwork in the lockscreen, for example).
7016 *
Selim Cinek99104832017-01-25 14:47:33 -08007017 * <p>
7018 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
7019 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
7020 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
7021 * <p>
7022 *
Dan Sandler842dd772014-05-15 09:36:47 -04007023 * To use this style with your Notification, feed it to
7024 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7025 * <pre class="prettyprint">
7026 * Notification noti = new Notification.Builder()
7027 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01007028 * .setContentTitle(&quot;Track title&quot;)
7029 * .setContentText(&quot;Artist - Album&quot;)
7030 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007031 * .setStyle(<b>new Notification.MediaStyle()</b>
7032 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04007033 * .build();
7034 * </pre>
7035 *
7036 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08007037 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04007038 */
7039 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007040 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04007041 static final int MAX_MEDIA_BUTTONS = 5;
7042
7043 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07007044 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04007045
7046 public MediaStyle() {
7047 }
7048
Adrian Roosf5faf9d2016-05-23 13:56:15 -07007049 /**
7050 * @deprecated use {@code MediaStyle()}.
7051 */
7052 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04007053 public MediaStyle(Builder builder) {
7054 setBuilder(builder);
7055 }
7056
7057 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007058 * 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 -04007059 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007060 *
7061 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04007062 */
7063 public MediaStyle setShowActionsInCompactView(int...actions) {
7064 mActionsToShowInCompact = actions;
7065 return this;
7066 }
7067
7068 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07007069 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
7070 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04007071 */
Jeff Browndba34ba2014-06-24 20:46:03 -07007072 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04007073 mToken = token;
7074 return this;
7075 }
7076
Christoph Studer4600f9b2014-07-22 22:44:43 +02007077 /**
7078 * @hide
7079 */
Dan Sandler842dd772014-05-15 09:36:47 -04007080 @Override
7081 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02007082 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01007083 if (wip.category == null) {
7084 wip.category = Notification.CATEGORY_TRANSPORT;
7085 }
Dan Sandler842dd772014-05-15 09:36:47 -04007086 return wip;
7087 }
7088
Christoph Studer4600f9b2014-07-22 22:44:43 +02007089 /**
7090 * @hide
7091 */
7092 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007093 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007094 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007095 }
7096
7097 /**
7098 * @hide
7099 */
7100 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007101 public RemoteViews makeBigContentView() {
7102 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02007103 }
7104
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007105 /**
7106 * @hide
7107 */
7108 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007109 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007110 RemoteViews expanded = makeMediaBigContentView();
7111 return expanded != null ? expanded : makeMediaContentView();
7112 }
7113
Dan Sandler842dd772014-05-15 09:36:47 -04007114 /** @hide */
7115 @Override
7116 public void addExtras(Bundle extras) {
7117 super.addExtras(extras);
7118
7119 if (mToken != null) {
7120 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
7121 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01007122 if (mActionsToShowInCompact != null) {
7123 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
7124 }
Dan Sandler842dd772014-05-15 09:36:47 -04007125 }
7126
Christoph Studer4600f9b2014-07-22 22:44:43 +02007127 /**
7128 * @hide
7129 */
7130 @Override
7131 protected void restoreFromExtras(Bundle extras) {
7132 super.restoreFromExtras(extras);
7133
7134 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
7135 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
7136 }
7137 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
7138 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
7139 }
7140 }
7141
Selim Cinek5bf069a2015-11-10 19:14:27 -05007142 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04007143 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07007144 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07007145 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04007146 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07007147
7148 // If the action buttons should not be tinted, then just use the default
7149 // notification color. Otherwise, just use the passed-in color.
7150 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
7151 ? color
7152 : NotificationColorUtil.resolveColor(mBuilder.mContext,
7153 Notification.COLOR_DEFAULT);
7154
Sunny Goyal5b153922017-09-21 21:00:36 -07007155 button.setDrawableTint(R.id.action0, false, tintColor,
7156 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04007157 if (!tombstone) {
7158 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
7159 }
7160 button.setContentDescription(R.id.action0, action.title);
7161 return button;
7162 }
7163
7164 private RemoteViews makeMediaContentView() {
7165 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007166 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04007167
7168 final int numActions = mBuilder.mActions.size();
7169 final int N = mActionsToShowInCompact == null
7170 ? 0
7171 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
7172 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007173 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04007174 for (int i = 0; i < N; i++) {
7175 if (i >= numActions) {
7176 throw new IllegalArgumentException(String.format(
7177 "setShowActionsInCompactView: action %d out of bounds (max %d)",
7178 i, numActions - 1));
7179 }
7180
7181 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05007182 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08007183 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007184 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007185 }
7186 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08007187 handleImage(view);
7188 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007189 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007190 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007191 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08007192 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007193 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04007194 return view;
7195 }
7196
Selim Cinek99104832017-01-25 14:47:33 -08007197 private int getPrimaryHighlightColor() {
7198 return mBuilder.getPrimaryHighlightColor();
7199 }
7200
Dan Sandler842dd772014-05-15 09:36:47 -04007201 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007202 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007203 // Dont add an expanded view if there is no more content to be revealed
7204 int actionsInCompact = mActionsToShowInCompact == null
7205 ? 0
7206 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07007207 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08007208 return null;
7209 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007210 RemoteViews big = mBuilder.applyStandardTemplate(
7211 R.layout.notification_template_material_big_media,
7212 false);
Dan Sandler842dd772014-05-15 09:36:47 -04007213
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007214 if (actionCount > 0) {
7215 big.removeAllViews(com.android.internal.R.id.media_actions);
7216 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05007217 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08007218 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007219 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04007220 }
7221 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05007222 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04007223 return big;
7224 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007225
Selim Cinek5bf069a2015-11-10 19:14:27 -05007226 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07007227 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007228 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
7229 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007230 }
7231 }
7232
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02007233 /**
7234 * @hide
7235 */
7236 @Override
7237 protected boolean hasProgress() {
7238 return false;
7239 }
Dan Sandler842dd772014-05-15 09:36:47 -04007240 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007241
Selim Cinek593610c2016-02-16 18:42:57 -08007242 /**
7243 * Notification style for custom views that are decorated by the system
7244 *
7245 * <p>Instead of providing a notification that is completely custom, a developer can set this
7246 * style and still obtain system decorations like the notification header with the expand
7247 * affordance and actions.
7248 *
7249 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7250 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7251 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7252 * corresponding custom views to display.
7253 *
7254 * To use this style with your Notification, feed it to
7255 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7256 * <pre class="prettyprint">
7257 * Notification noti = new Notification.Builder()
7258 * .setSmallIcon(R.drawable.ic_stat_player)
7259 * .setLargeIcon(albumArtBitmap))
7260 * .setCustomContentView(contentView);
7261 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
7262 * .build();
7263 * </pre>
7264 */
7265 public static class DecoratedCustomViewStyle extends Style {
7266
7267 public DecoratedCustomViewStyle() {
7268 }
7269
Selim Cinek593610c2016-02-16 18:42:57 -08007270 /**
7271 * @hide
7272 */
7273 public boolean displayCustomViewInline() {
7274 return true;
7275 }
7276
7277 /**
7278 * @hide
7279 */
7280 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007281 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007282 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
7283 }
7284
7285 /**
7286 * @hide
7287 */
7288 @Override
7289 public RemoteViews makeBigContentView() {
7290 return makeDecoratedBigContentView();
7291 }
7292
7293 /**
7294 * @hide
7295 */
7296 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007297 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08007298 return makeDecoratedHeadsUpContentView();
7299 }
7300
Selim Cinek593610c2016-02-16 18:42:57 -08007301 private RemoteViews makeDecoratedHeadsUpContentView() {
7302 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
7303 ? mBuilder.mN.contentView
7304 : mBuilder.mN.headsUpContentView;
7305 if (mBuilder.mActions.size() == 0) {
7306 return makeStandardTemplateWithCustomContent(headsUpContentView);
7307 }
7308 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7309 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007310 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007311 return remoteViews;
7312 }
7313
Selim Cinek593610c2016-02-16 18:42:57 -08007314 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
7315 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
7316 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007317 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08007318 return remoteViews;
7319 }
7320
Selim Cinek593610c2016-02-16 18:42:57 -08007321 private RemoteViews makeDecoratedBigContentView() {
7322 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
7323 ? mBuilder.mN.contentView
7324 : mBuilder.mN.bigContentView;
7325 if (mBuilder.mActions.size() == 0) {
7326 return makeStandardTemplateWithCustomContent(bigContentView);
7327 }
7328 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
7329 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08007330 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08007331 return remoteViews;
7332 }
Selim Cinek247fa012016-02-18 09:50:48 -08007333
7334 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
7335 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007336 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007337 // Need to clone customContent before adding, because otherwise it can no longer be
7338 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007339 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07007340 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
7341 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007342 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007343 }
Selim Cinek247fa012016-02-18 09:50:48 -08007344 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007345 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07007346 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007347 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08007348 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07007349 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08007350 }
Selim Cinek593610c2016-02-16 18:42:57 -08007351 }
7352
Selim Cinek03eb3b72016-02-18 10:39:45 -08007353 /**
7354 * Notification style for media custom views that are decorated by the system
7355 *
7356 * <p>Instead of providing a media notification that is completely custom, a developer can set
7357 * this style and still obtain system decorations like the notification header with the expand
7358 * affordance and actions.
7359 *
7360 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
7361 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
7362 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
7363 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08007364 * <p>
7365 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
7366 * notification by using {@link Notification.Builder#setColorized(boolean)}.
7367 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08007368 * To use this style with your Notification, feed it to
7369 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
7370 * <pre class="prettyprint">
7371 * Notification noti = new Notification.Builder()
7372 * .setSmallIcon(R.drawable.ic_stat_player)
7373 * .setLargeIcon(albumArtBitmap))
7374 * .setCustomContentView(contentView);
7375 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
7376 * .setMediaSession(mySession))
7377 * .build();
7378 * </pre>
7379 *
7380 * @see android.app.Notification.DecoratedCustomViewStyle
7381 * @see android.app.Notification.MediaStyle
7382 */
7383 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
7384
7385 public DecoratedMediaCustomViewStyle() {
7386 }
7387
Selim Cinek03eb3b72016-02-18 10:39:45 -08007388 /**
7389 * @hide
7390 */
7391 public boolean displayCustomViewInline() {
7392 return true;
7393 }
7394
7395 /**
7396 * @hide
7397 */
7398 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08007399 public RemoteViews makeContentView(boolean increasedHeight) {
7400 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007401 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7402 mBuilder.mN.contentView);
7403 }
7404
7405 /**
7406 * @hide
7407 */
7408 @Override
7409 public RemoteViews makeBigContentView() {
7410 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
7411 ? mBuilder.mN.bigContentView
7412 : mBuilder.mN.contentView;
7413 return makeBigContentViewWithCustomContent(customRemoteView);
7414 }
7415
7416 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
7417 RemoteViews remoteViews = super.makeBigContentView();
7418 if (remoteViews != null) {
7419 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
7420 customRemoteView);
7421 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08007422 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08007423 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
7424 customRemoteView);
7425 } else {
7426 return null;
7427 }
7428 }
7429
7430 /**
7431 * @hide
7432 */
7433 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08007434 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08007435 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
7436 ? mBuilder.mN.headsUpContentView
7437 : mBuilder.mN.contentView;
7438 return makeBigContentViewWithCustomContent(customRemoteView);
7439 }
7440
7441 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7442 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007443 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007444 // Need to clone customContent before adding, because otherwise it can no longer be
7445 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007446 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007447 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007448 remoteViews.removeAllViews(id);
7449 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007450 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007451 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007452 return remoteViews;
7453 }
7454 }
7455
Selim Cineke7238dd2017-12-14 17:48:32 -08007456 /**
7457 * A Person associated with this Notification.
7458 */
7459 public static final class Person implements Parcelable {
7460 @Nullable private CharSequence mName;
7461 @Nullable private Icon mIcon;
7462 @Nullable private String mUri;
7463 @Nullable private String mKey;
7464
7465 protected Person(Parcel in) {
7466 mName = in.readCharSequence();
7467 if (in.readInt() != 0) {
7468 mIcon = Icon.CREATOR.createFromParcel(in);
7469 }
7470 mUri = in.readString();
7471 mKey = in.readString();
7472 }
7473
7474 /**
7475 * Create a new person.
7476 */
7477 public Person() {
7478 }
7479
7480 /**
7481 * Give this person a name.
7482 *
7483 * @param name the name of this person
7484 */
7485 public Person setName(@Nullable CharSequence name) {
7486 this.mName = name;
7487 return this;
7488 }
7489
7490 /**
7491 * Add an icon for this person.
7492 * <br />
7493 * This is currently only used for {@link MessagingStyle} notifications and should not be
7494 * provided otherwise, in order to save memory. The system will prefer this icon over any
7495 * images that are resolved from the URI.
7496 *
7497 * @param icon the icon of the person
7498 */
7499 public Person setIcon(@Nullable Icon icon) {
7500 this.mIcon = icon;
7501 return this;
7502 }
7503
7504 /**
7505 * Set a URI associated with this person.
7506 *
7507 * <P>
7508 * Depending on user preferences, adding a URI to a Person may allow the notification to
7509 * pass through interruption filters, if this notification is of
7510 * category {@link #CATEGORY_CALL} or {@link #CATEGORY_MESSAGE}.
7511 * The addition of people may also cause this notification to appear more prominently in
7512 * the user interface.
7513 * </P>
7514 *
7515 * <P>
7516 * The person should be specified by the {@code String} representation of a
7517 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
7518 * </P>
7519 *
7520 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
7521 * URIs. The path part of these URIs must exist in the contacts database, in the
7522 * appropriate column, or the reference will be discarded as invalid. Telephone schema
7523 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
7524 * </P>
7525 *
7526 * @param uri a URI for the person
7527 */
7528 public Person setUri(@Nullable String uri) {
7529 mUri = uri;
7530 return this;
7531 }
7532
7533 /**
7534 * Add a key to this person in order to uniquely identify it.
7535 * This is especially useful if the name doesn't uniquely identify this person or if the
7536 * display name is a short handle of the actual name.
7537 *
7538 * <P>If no key is provided, the name serves as as the key for the purpose of
7539 * identification.</P>
7540 *
7541 * @param key the key that uniquely identifies this person
7542 */
7543 public Person setKey(@Nullable String key) {
7544 mKey = key;
7545 return this;
7546 }
7547
7548
7549 /**
7550 * @return the uri provided for this person or {@code null} if no Uri was provided
7551 */
7552 @Nullable
7553 public String getUri() {
7554 return mUri;
7555 }
7556
7557 /**
7558 * @return the name provided for this person or {@code null} if no name was provided
7559 */
7560 @Nullable
7561 public CharSequence getName() {
7562 return mName;
7563 }
7564
7565 /**
7566 * @return the icon provided for this person or {@code null} if no icon was provided
7567 */
7568 @Nullable
7569 public Icon getIcon() {
7570 return mIcon;
7571 }
7572
7573 /**
7574 * @return the key provided for this person or {@code null} if no key was provided
7575 */
7576 @Nullable
7577 public String getKey() {
7578 return mKey;
7579 }
7580
7581 /**
7582 * @return the URI associated with this person, or "name:mName" otherwise
7583 * @hide
7584 */
7585 public String resolveToLegacyUri() {
7586 if (mUri != null) {
7587 return mUri;
7588 }
7589 if (mName != null) {
7590 return "name:" + mName;
7591 }
7592 return "";
7593 }
7594
7595 @Override
7596 public int describeContents() {
7597 return 0;
7598 }
7599
7600 @Override
7601 public void writeToParcel(Parcel dest, @WriteFlags int flags) {
7602 dest.writeCharSequence(mName);
7603 if (mIcon != null) {
7604 dest.writeInt(1);
7605 mIcon.writeToParcel(dest, 0);
7606 } else {
7607 dest.writeInt(0);
7608 }
7609 dest.writeString(mUri);
7610 dest.writeString(mKey);
7611 }
7612
7613 public static final Creator<Person> CREATOR = new Creator<Person>() {
7614 @Override
7615 public Person createFromParcel(Parcel in) {
7616 return new Person(in);
7617 }
7618
7619 @Override
7620 public Person[] newArray(int size) {
7621 return new Person[size];
7622 }
7623 };
7624 }
7625
Christoph Studer4600f9b2014-07-22 22:44:43 +02007626 // When adding a new Style subclass here, don't forget to update
7627 // Builder.getNotificationStyleClass.
7628
Griff Hazen61a9e862014-05-22 16:05:19 -07007629 /**
7630 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7631 * metadata or change options on a notification builder.
7632 */
7633 public interface Extender {
7634 /**
7635 * Apply this extender to a notification builder.
7636 * @param builder the builder to be modified.
7637 * @return the build object for chaining.
7638 */
7639 public Builder extend(Builder builder);
7640 }
7641
7642 /**
7643 * Helper class to add wearable extensions to notifications.
7644 * <p class="note"> See
7645 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7646 * for Android Wear</a> for more information on how to use this class.
7647 * <p>
7648 * To create a notification with wearable extensions:
7649 * <ol>
7650 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7651 * properties.
7652 * <li>Create a {@link android.app.Notification.WearableExtender}.
7653 * <li>Set wearable-specific properties using the
7654 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7655 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7656 * notification.
7657 * <li>Post the notification to the notification system with the
7658 * {@code NotificationManager.notify(...)} methods.
7659 * </ol>
7660 *
7661 * <pre class="prettyprint">
7662 * Notification notif = new Notification.Builder(mContext)
7663 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7664 * .setContentText(subject)
7665 * .setSmallIcon(R.drawable.new_mail)
7666 * .extend(new Notification.WearableExtender()
7667 * .setContentIcon(R.drawable.new_mail))
7668 * .build();
7669 * NotificationManager notificationManger =
7670 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7671 * notificationManger.notify(0, notif);</pre>
7672 *
7673 * <p>Wearable extensions can be accessed on an existing notification by using the
7674 * {@code WearableExtender(Notification)} constructor,
7675 * and then using the {@code get} methods to access values.
7676 *
7677 * <pre class="prettyprint">
7678 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7679 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007680 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007681 */
7682 public static final class WearableExtender implements Extender {
7683 /**
7684 * Sentinel value for an action index that is unset.
7685 */
7686 public static final int UNSET_ACTION_INDEX = -1;
7687
7688 /**
7689 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7690 * default sizing.
7691 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007692 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007693 * on their content.
7694 */
7695 public static final int SIZE_DEFAULT = 0;
7696
7697 /**
7698 * Size value for use with {@link #setCustomSizePreset} to show this notification
7699 * with an extra small size.
7700 * <p>This value is only applicable for custom display notifications created using
7701 * {@link #setDisplayIntent}.
7702 */
7703 public static final int SIZE_XSMALL = 1;
7704
7705 /**
7706 * Size value for use with {@link #setCustomSizePreset} to show this notification
7707 * with a small size.
7708 * <p>This value is only applicable for custom display notifications created using
7709 * {@link #setDisplayIntent}.
7710 */
7711 public static final int SIZE_SMALL = 2;
7712
7713 /**
7714 * Size value for use with {@link #setCustomSizePreset} to show this notification
7715 * with a medium size.
7716 * <p>This value is only applicable for custom display notifications created using
7717 * {@link #setDisplayIntent}.
7718 */
7719 public static final int SIZE_MEDIUM = 3;
7720
7721 /**
7722 * Size value for use with {@link #setCustomSizePreset} to show this notification
7723 * with a large size.
7724 * <p>This value is only applicable for custom display notifications created using
7725 * {@link #setDisplayIntent}.
7726 */
7727 public static final int SIZE_LARGE = 4;
7728
Griff Hazend5f11f92014-05-27 15:40:09 -07007729 /**
7730 * Size value for use with {@link #setCustomSizePreset} to show this notification
7731 * full screen.
7732 * <p>This value is only applicable for custom display notifications created using
7733 * {@link #setDisplayIntent}.
7734 */
7735 public static final int SIZE_FULL_SCREEN = 5;
7736
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007737 /**
7738 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7739 * short amount of time when this notification is displayed on the screen. This
7740 * is the default value.
7741 */
7742 public static final int SCREEN_TIMEOUT_SHORT = 0;
7743
7744 /**
7745 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7746 * for a longer amount of time when this notification is displayed on the screen.
7747 */
7748 public static final int SCREEN_TIMEOUT_LONG = -1;
7749
Griff Hazen61a9e862014-05-22 16:05:19 -07007750 /** Notification extra which contains wearable extensions */
7751 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7752
Pete Gastaf6781d2014-10-07 15:17:05 -04007753 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007754 private static final String KEY_ACTIONS = "actions";
7755 private static final String KEY_FLAGS = "flags";
7756 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7757 private static final String KEY_PAGES = "pages";
7758 private static final String KEY_BACKGROUND = "background";
7759 private static final String KEY_CONTENT_ICON = "contentIcon";
7760 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7761 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7762 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7763 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7764 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007765 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007766 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007767 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007768
7769 // Flags bitwise-ored to mFlags
7770 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7771 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7772 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7773 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007774 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007775 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007776 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007777
7778 // Default value for flags integer
7779 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7780
7781 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7782 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7783
7784 private ArrayList<Action> mActions = new ArrayList<Action>();
7785 private int mFlags = DEFAULT_FLAGS;
7786 private PendingIntent mDisplayIntent;
7787 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7788 private Bitmap mBackground;
7789 private int mContentIcon;
7790 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7791 private int mContentActionIndex = UNSET_ACTION_INDEX;
7792 private int mCustomSizePreset = SIZE_DEFAULT;
7793 private int mCustomContentHeight;
7794 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007795 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007796 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007797 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007798
7799 /**
7800 * Create a {@link android.app.Notification.WearableExtender} with default
7801 * options.
7802 */
7803 public WearableExtender() {
7804 }
7805
7806 public WearableExtender(Notification notif) {
7807 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7808 if (wearableBundle != null) {
7809 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7810 if (actions != null) {
7811 mActions.addAll(actions);
7812 }
7813
7814 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7815 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7816
7817 Notification[] pages = getNotificationArrayFromBundle(
7818 wearableBundle, KEY_PAGES);
7819 if (pages != null) {
7820 Collections.addAll(mPages, pages);
7821 }
7822
7823 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7824 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7825 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7826 DEFAULT_CONTENT_ICON_GRAVITY);
7827 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7828 UNSET_ACTION_INDEX);
7829 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7830 SIZE_DEFAULT);
7831 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7832 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007833 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007834 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007835 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007836 }
7837 }
7838
7839 /**
7840 * Apply wearable extensions to a notification that is being built. This is typically
7841 * called by the {@link android.app.Notification.Builder#extend} method of
7842 * {@link android.app.Notification.Builder}.
7843 */
7844 @Override
7845 public Notification.Builder extend(Notification.Builder builder) {
7846 Bundle wearableBundle = new Bundle();
7847
7848 if (!mActions.isEmpty()) {
7849 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7850 }
7851 if (mFlags != DEFAULT_FLAGS) {
7852 wearableBundle.putInt(KEY_FLAGS, mFlags);
7853 }
7854 if (mDisplayIntent != null) {
7855 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7856 }
7857 if (!mPages.isEmpty()) {
7858 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7859 new Notification[mPages.size()]));
7860 }
7861 if (mBackground != null) {
7862 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7863 }
7864 if (mContentIcon != 0) {
7865 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7866 }
7867 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7868 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7869 }
7870 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7871 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7872 mContentActionIndex);
7873 }
7874 if (mCustomSizePreset != SIZE_DEFAULT) {
7875 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7876 }
7877 if (mCustomContentHeight != 0) {
7878 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7879 }
7880 if (mGravity != DEFAULT_GRAVITY) {
7881 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7882 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007883 if (mHintScreenTimeout != 0) {
7884 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7885 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007886 if (mDismissalId != null) {
7887 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7888 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007889 if (mBridgeTag != null) {
7890 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7891 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007892
7893 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7894 return builder;
7895 }
7896
7897 @Override
7898 public WearableExtender clone() {
7899 WearableExtender that = new WearableExtender();
7900 that.mActions = new ArrayList<Action>(this.mActions);
7901 that.mFlags = this.mFlags;
7902 that.mDisplayIntent = this.mDisplayIntent;
7903 that.mPages = new ArrayList<Notification>(this.mPages);
7904 that.mBackground = this.mBackground;
7905 that.mContentIcon = this.mContentIcon;
7906 that.mContentIconGravity = this.mContentIconGravity;
7907 that.mContentActionIndex = this.mContentActionIndex;
7908 that.mCustomSizePreset = this.mCustomSizePreset;
7909 that.mCustomContentHeight = this.mCustomContentHeight;
7910 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007911 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007912 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007913 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007914 return that;
7915 }
7916
7917 /**
7918 * Add a wearable action to this notification.
7919 *
7920 * <p>When wearable actions are added using this method, the set of actions that
7921 * show on a wearable device splits from devices that only show actions added
7922 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7923 * of which actions display on different devices.
7924 *
7925 * @param action the action to add to this notification
7926 * @return this object for method chaining
7927 * @see android.app.Notification.Action
7928 */
7929 public WearableExtender addAction(Action action) {
7930 mActions.add(action);
7931 return this;
7932 }
7933
7934 /**
7935 * Adds wearable actions to this notification.
7936 *
7937 * <p>When wearable actions are added using this method, the set of actions that
7938 * show on a wearable device splits from devices that only show actions added
7939 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7940 * of which actions display on different devices.
7941 *
7942 * @param actions the actions to add to this notification
7943 * @return this object for method chaining
7944 * @see android.app.Notification.Action
7945 */
7946 public WearableExtender addActions(List<Action> actions) {
7947 mActions.addAll(actions);
7948 return this;
7949 }
7950
7951 /**
7952 * Clear all wearable actions present on this builder.
7953 * @return this object for method chaining.
7954 * @see #addAction
7955 */
7956 public WearableExtender clearActions() {
7957 mActions.clear();
7958 return this;
7959 }
7960
7961 /**
7962 * Get the wearable actions present on this notification.
7963 */
7964 public List<Action> getActions() {
7965 return mActions;
7966 }
7967
7968 /**
7969 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007970 * this notification. The {@link PendingIntent} provided should be for an activity.
7971 *
7972 * <pre class="prettyprint">
7973 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7974 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7975 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7976 * Notification notif = new Notification.Builder(context)
7977 * .extend(new Notification.WearableExtender()
7978 * .setDisplayIntent(displayPendingIntent)
7979 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7980 * .build();</pre>
7981 *
7982 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007983 * should have an empty task affinity. It is also recommended to use the device
7984 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007985 *
7986 * <p>Example AndroidManifest.xml entry:
7987 * <pre class="prettyprint">
7988 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7989 * android:exported=&quot;true&quot;
7990 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007991 * android:taskAffinity=&quot;&quot;
7992 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007993 *
7994 * @param intent the {@link PendingIntent} for an activity
7995 * @return this object for method chaining
7996 * @see android.app.Notification.WearableExtender#getDisplayIntent
7997 */
7998 public WearableExtender setDisplayIntent(PendingIntent intent) {
7999 mDisplayIntent = intent;
8000 return this;
8001 }
8002
8003 /**
8004 * Get the intent to launch inside of an activity view when displaying this
8005 * notification. This {@code PendingIntent} should be for an activity.
8006 */
8007 public PendingIntent getDisplayIntent() {
8008 return mDisplayIntent;
8009 }
8010
8011 /**
8012 * Add an additional page of content to display with this notification. The current
8013 * notification forms the first page, and pages added using this function form
8014 * subsequent pages. This field can be used to separate a notification into multiple
8015 * sections.
8016 *
8017 * @param page the notification to add as another page
8018 * @return this object for method chaining
8019 * @see android.app.Notification.WearableExtender#getPages
8020 */
8021 public WearableExtender addPage(Notification page) {
8022 mPages.add(page);
8023 return this;
8024 }
8025
8026 /**
8027 * Add additional pages of content to display with this notification. The current
8028 * notification forms the first page, and pages added using this function form
8029 * subsequent pages. This field can be used to separate a notification into multiple
8030 * sections.
8031 *
8032 * @param pages a list of notifications
8033 * @return this object for method chaining
8034 * @see android.app.Notification.WearableExtender#getPages
8035 */
8036 public WearableExtender addPages(List<Notification> pages) {
8037 mPages.addAll(pages);
8038 return this;
8039 }
8040
8041 /**
8042 * Clear all additional pages present on this builder.
8043 * @return this object for method chaining.
8044 * @see #addPage
8045 */
8046 public WearableExtender clearPages() {
8047 mPages.clear();
8048 return this;
8049 }
8050
8051 /**
8052 * Get the array of additional pages of content for displaying this notification. The
8053 * current notification forms the first page, and elements within this array form
8054 * subsequent pages. This field can be used to separate a notification into multiple
8055 * sections.
8056 * @return the pages for this notification
8057 */
8058 public List<Notification> getPages() {
8059 return mPages;
8060 }
8061
8062 /**
8063 * Set a background image to be displayed behind the notification content.
8064 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8065 * will work with any notification style.
8066 *
8067 * @param background the background bitmap
8068 * @return this object for method chaining
8069 * @see android.app.Notification.WearableExtender#getBackground
8070 */
8071 public WearableExtender setBackground(Bitmap background) {
8072 mBackground = background;
8073 return this;
8074 }
8075
8076 /**
8077 * Get a background image to be displayed behind the notification content.
8078 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
8079 * will work with any notification style.
8080 *
8081 * @return the background image
8082 * @see android.app.Notification.WearableExtender#setBackground
8083 */
8084 public Bitmap getBackground() {
8085 return mBackground;
8086 }
8087
8088 /**
8089 * Set an icon that goes with the content of this notification.
8090 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008091 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008092 public WearableExtender setContentIcon(int icon) {
8093 mContentIcon = icon;
8094 return this;
8095 }
8096
8097 /**
8098 * Get an icon that goes with the content of this notification.
8099 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008100 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008101 public int getContentIcon() {
8102 return mContentIcon;
8103 }
8104
8105 /**
8106 * Set the gravity that the content icon should have within the notification display.
8107 * Supported values include {@link android.view.Gravity#START} and
8108 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8109 * @see #setContentIcon
8110 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008111 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008112 public WearableExtender setContentIconGravity(int contentIconGravity) {
8113 mContentIconGravity = contentIconGravity;
8114 return this;
8115 }
8116
8117 /**
8118 * Get the gravity that the content icon should have within the notification display.
8119 * Supported values include {@link android.view.Gravity#START} and
8120 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
8121 * @see #getContentIcon
8122 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008123 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008124 public int getContentIconGravity() {
8125 return mContentIconGravity;
8126 }
8127
8128 /**
8129 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07008130 * this notification. This action will no longer display separately from the
8131 * notification's content.
8132 *
Griff Hazenca48d352014-05-28 22:37:13 -07008133 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008134 * set, although the list of available actions comes from the main notification and not
8135 * from the child page's notification.
8136 *
8137 * @param actionIndex The index of the action to hoist onto the current notification page.
8138 * If wearable actions were added to the main notification, this index
8139 * will apply to that list, otherwise it will apply to the regular
8140 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07008141 */
8142 public WearableExtender setContentAction(int actionIndex) {
8143 mContentActionIndex = actionIndex;
8144 return this;
8145 }
8146
8147 /**
Griff Hazenca48d352014-05-28 22:37:13 -07008148 * Get the index of the notification action, if any, that was specified as being clickable
8149 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07008150 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07008151 *
Griff Hazenca48d352014-05-28 22:37:13 -07008152 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07008153 * set, although the list of available actions comes from the main notification and not
8154 * from the child page's notification.
8155 *
8156 * <p>If wearable specific actions were added to the main notification, this index will
8157 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07008158 *
8159 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07008160 */
8161 public int getContentAction() {
8162 return mContentActionIndex;
8163 }
8164
8165 /**
8166 * Set the gravity that this notification should have within the available viewport space.
8167 * Supported values include {@link android.view.Gravity#TOP},
8168 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8169 * The default value is {@link android.view.Gravity#BOTTOM}.
8170 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008171 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008172 public WearableExtender setGravity(int gravity) {
8173 mGravity = gravity;
8174 return this;
8175 }
8176
8177 /**
8178 * Get the gravity that this notification should have within the available viewport space.
8179 * Supported values include {@link android.view.Gravity#TOP},
8180 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
8181 * The default value is {@link android.view.Gravity#BOTTOM}.
8182 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008183 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008184 public int getGravity() {
8185 return mGravity;
8186 }
8187
8188 /**
8189 * Set the custom size preset for the display of this notification out of the available
8190 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8191 * {@link #SIZE_LARGE}.
8192 * <p>Some custom size presets are only applicable for custom display notifications created
8193 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
8194 * documentation for the preset in question. See also
8195 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
8196 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008197 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008198 public WearableExtender setCustomSizePreset(int sizePreset) {
8199 mCustomSizePreset = sizePreset;
8200 return this;
8201 }
8202
8203 /**
8204 * Get the custom size preset for the display of this notification out of the available
8205 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
8206 * {@link #SIZE_LARGE}.
8207 * <p>Some custom size presets are only applicable for custom display notifications created
8208 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
8209 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
8210 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008211 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008212 public int getCustomSizePreset() {
8213 return mCustomSizePreset;
8214 }
8215
8216 /**
8217 * Set the custom height in pixels for the display of this notification's content.
8218 * <p>This option is only available for custom display notifications created
8219 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
8220 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
8221 * {@link #getCustomContentHeight}.
8222 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008223 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008224 public WearableExtender setCustomContentHeight(int height) {
8225 mCustomContentHeight = height;
8226 return this;
8227 }
8228
8229 /**
8230 * Get the custom height in pixels for the display of this notification's content.
8231 * <p>This option is only available for custom display notifications created
8232 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
8233 * {@link #setCustomContentHeight}.
8234 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008235 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008236 public int getCustomContentHeight() {
8237 return mCustomContentHeight;
8238 }
8239
8240 /**
8241 * Set whether the scrolling position for the contents of this notification should start
8242 * at the bottom of the contents instead of the top when the contents are too long to
8243 * display within the screen. Default is false (start scroll at the top).
8244 */
8245 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
8246 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
8247 return this;
8248 }
8249
8250 /**
8251 * Get whether the scrolling position for the contents of this notification should start
8252 * at the bottom of the contents instead of the top when the contents are too long to
8253 * display within the screen. Default is false (start scroll at the top).
8254 */
8255 public boolean getStartScrollBottom() {
8256 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
8257 }
8258
8259 /**
8260 * Set whether the content intent is available when the wearable device is not connected
8261 * to a companion device. The user can still trigger this intent when the wearable device
8262 * is offline, but a visual hint will indicate that the content intent may not be available.
8263 * Defaults to true.
8264 */
8265 public WearableExtender setContentIntentAvailableOffline(
8266 boolean contentIntentAvailableOffline) {
8267 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
8268 return this;
8269 }
8270
8271 /**
8272 * Get whether the content intent is available when the wearable device is not connected
8273 * to a companion device. The user can still trigger this intent when the wearable device
8274 * is offline, but a visual hint will indicate that the content intent may not be available.
8275 * Defaults to true.
8276 */
8277 public boolean getContentIntentAvailableOffline() {
8278 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
8279 }
8280
8281 /**
8282 * Set a hint that this notification's icon should not be displayed.
8283 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
8284 * @return this object for method chaining
8285 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008286 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008287 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
8288 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
8289 return this;
8290 }
8291
8292 /**
8293 * Get a hint that this notification's icon should not be displayed.
8294 * @return {@code true} if this icon should not be displayed, false otherwise.
8295 * The default value is {@code false} if this was never set.
8296 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008297 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008298 public boolean getHintHideIcon() {
8299 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
8300 }
8301
8302 /**
8303 * Set 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 #addPage}.
8306 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008307 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008308 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
8309 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
8310 return this;
8311 }
8312
8313 /**
8314 * Get a visual hint that only the background image of this notification should be
8315 * displayed, and other semantic content should be hidden. This hint is only applicable
8316 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
8317 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008318 @Deprecated
Griff Hazen61a9e862014-05-22 16:05:19 -07008319 public boolean getHintShowBackgroundOnly() {
8320 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
8321 }
8322
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008323 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008324 * Set a hint that this notification's background should not be clipped if possible,
8325 * and should instead be resized to fully display on the screen, retaining the aspect
8326 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008327 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
8328 * @return this object for method chaining
8329 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008330 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008331 public WearableExtender setHintAvoidBackgroundClipping(
8332 boolean hintAvoidBackgroundClipping) {
8333 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
8334 return this;
8335 }
8336
8337 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08008338 * Get a hint that this notification's background should not be clipped if possible,
8339 * and should instead be resized to fully display on the screen, retaining the aspect
8340 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008341 * @return {@code true} if it's ok if the background is clipped on the screen, false
8342 * otherwise. The default value is {@code false} if this was never set.
8343 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008344 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008345 public boolean getHintAvoidBackgroundClipping() {
8346 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
8347 }
8348
8349 /**
8350 * Set a hint that the screen should remain on for at least this duration when
8351 * this notification is displayed on the screen.
8352 * @param timeout The requested screen timeout in milliseconds. Can also be either
8353 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8354 * @return this object for method chaining
8355 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008356 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008357 public WearableExtender setHintScreenTimeout(int timeout) {
8358 mHintScreenTimeout = timeout;
8359 return this;
8360 }
8361
8362 /**
8363 * Get the duration, in milliseconds, that the screen should remain on for
8364 * when this notification is displayed.
8365 * @return the duration in milliseconds if > 0, or either one of the sentinel values
8366 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
8367 */
Gus Prevasb5f9cf52018-02-21 14:21:50 -05008368 @Deprecated
Griff Hazen5f2edfc2014-09-29 16:28:44 -07008369 public int getHintScreenTimeout() {
8370 return mHintScreenTimeout;
8371 }
8372
Alex Hills9ab3a232016-04-05 14:54:56 -04008373 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04008374 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
8375 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8376 * qr codes, as well as other simple black-and-white tickets.
8377 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
8378 * @return this object for method chaining
8379 */
8380 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
8381 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
8382 return this;
8383 }
8384
8385 /**
8386 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
8387 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
8388 * qr codes, as well as other simple black-and-white tickets.
8389 * @return {@code true} if it should be displayed in ambient, false otherwise
8390 * otherwise. The default value is {@code false} if this was never set.
8391 */
8392 public boolean getHintAmbientBigPicture() {
8393 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
8394 }
8395
8396 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04008397 * Set a hint that this notification's content intent will launch an {@link Activity}
8398 * directly, telling the platform that it can generate the appropriate transitions.
8399 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
8400 * an activity and transitions should be generated, false otherwise.
8401 * @return this object for method chaining
8402 */
8403 public WearableExtender setHintContentIntentLaunchesActivity(
8404 boolean hintContentIntentLaunchesActivity) {
8405 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
8406 return this;
8407 }
8408
8409 /**
8410 * Get a hint that this notification's content intent will launch an {@link Activity}
8411 * directly, telling the platform that it can generate the appropriate transitions
8412 * @return {@code true} if the content intent will launch an activity and transitions should
8413 * be generated, false otherwise. The default value is {@code false} if this was never set.
8414 */
8415 public boolean getHintContentIntentLaunchesActivity() {
8416 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
8417 }
8418
Nadia Benbernou948627e2016-04-14 14:41:08 -04008419 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008420 * Sets the dismissal id for this notification. If a notification is posted with a
8421 * dismissal id, then when that notification is canceled, notifications on other wearables
8422 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04008423 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008424 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04008425 * @param dismissalId the dismissal id of the notification.
8426 * @return this object for method chaining
8427 */
8428 public WearableExtender setDismissalId(String dismissalId) {
8429 mDismissalId = dismissalId;
8430 return this;
8431 }
8432
8433 /**
8434 * Returns the dismissal id of the notification.
8435 * @return the dismissal id of the notification or null if it has not been set.
8436 */
8437 public String getDismissalId() {
8438 return mDismissalId;
8439 }
8440
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04008441 /**
8442 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
8443 * posted from a phone to provide finer-grained control on what notifications are bridged
8444 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
8445 * Features to Notifications</a> for more information.
8446 * @param bridgeTag the bridge tag of the notification.
8447 * @return this object for method chaining
8448 */
8449 public WearableExtender setBridgeTag(String bridgeTag) {
8450 mBridgeTag = bridgeTag;
8451 return this;
8452 }
8453
8454 /**
8455 * Returns the bridge tag of the notification.
8456 * @return the bridge tag or null if not present.
8457 */
8458 public String getBridgeTag() {
8459 return mBridgeTag;
8460 }
8461
Griff Hazen61a9e862014-05-22 16:05:19 -07008462 private void setFlag(int mask, boolean value) {
8463 if (value) {
8464 mFlags |= mask;
8465 } else {
8466 mFlags &= ~mask;
8467 }
8468 }
8469 }
8470
8471 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008472 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
8473 * with car extensions:
8474 *
8475 * <ol>
8476 * <li>Create an {@link Notification.Builder}, setting any desired
8477 * properties.
8478 * <li>Create a {@link CarExtender}.
8479 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
8480 * {@link CarExtender}.
8481 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8482 * to apply the extensions to a notification.
8483 * </ol>
8484 *
8485 * <pre class="prettyprint">
8486 * Notification notification = new Notification.Builder(context)
8487 * ...
8488 * .extend(new CarExtender()
8489 * .set*(...))
8490 * .build();
8491 * </pre>
8492 *
8493 * <p>Car extensions can be accessed on an existing notification by using the
8494 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
8495 * to access values.
8496 */
8497 public static final class CarExtender implements Extender {
8498 private static final String TAG = "CarExtender";
8499
8500 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
8501 private static final String EXTRA_LARGE_ICON = "large_icon";
8502 private static final String EXTRA_CONVERSATION = "car_conversation";
8503 private static final String EXTRA_COLOR = "app_color";
8504
8505 private Bitmap mLargeIcon;
8506 private UnreadConversation mUnreadConversation;
8507 private int mColor = Notification.COLOR_DEFAULT;
8508
8509 /**
8510 * Create a {@link CarExtender} with default options.
8511 */
8512 public CarExtender() {
8513 }
8514
8515 /**
8516 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
8517 *
8518 * @param notif The notification from which to copy options.
8519 */
8520 public CarExtender(Notification notif) {
8521 Bundle carBundle = notif.extras == null ?
8522 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
8523 if (carBundle != null) {
8524 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
8525 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
8526
8527 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
8528 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
8529 }
8530 }
8531
8532 /**
8533 * Apply car extensions to a notification that is being built. This is typically called by
8534 * the {@link Notification.Builder#extend(Notification.Extender)}
8535 * method of {@link Notification.Builder}.
8536 */
8537 @Override
8538 public Notification.Builder extend(Notification.Builder builder) {
8539 Bundle carExtensions = new Bundle();
8540
8541 if (mLargeIcon != null) {
8542 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
8543 }
8544 if (mColor != Notification.COLOR_DEFAULT) {
8545 carExtensions.putInt(EXTRA_COLOR, mColor);
8546 }
8547
8548 if (mUnreadConversation != null) {
8549 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
8550 carExtensions.putBundle(EXTRA_CONVERSATION, b);
8551 }
8552
8553 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
8554 return builder;
8555 }
8556
8557 /**
8558 * Sets the accent color to use when Android Auto presents the notification.
8559 *
8560 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
8561 * to accent the displayed notification. However, not all colors are acceptable in an
8562 * automotive setting. This method can be used to override the color provided in the
8563 * notification in such a situation.
8564 */
Tor Norbye80756e32015-03-02 09:39:27 -08008565 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008566 mColor = color;
8567 return this;
8568 }
8569
8570 /**
8571 * Gets the accent color.
8572 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04008573 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008574 */
Tor Norbye80756e32015-03-02 09:39:27 -08008575 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08008576 public int getColor() {
8577 return mColor;
8578 }
8579
8580 /**
8581 * Sets the large icon of the car notification.
8582 *
8583 * If no large icon is set in the extender, Android Auto will display the icon
8584 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
8585 *
8586 * @param largeIcon The large icon to use in the car notification.
8587 * @return This object for method chaining.
8588 */
8589 public CarExtender setLargeIcon(Bitmap largeIcon) {
8590 mLargeIcon = largeIcon;
8591 return this;
8592 }
8593
8594 /**
8595 * Gets the large icon used in this car notification, or null if no icon has been set.
8596 *
8597 * @return The large icon for the car notification.
8598 * @see CarExtender#setLargeIcon
8599 */
8600 public Bitmap getLargeIcon() {
8601 return mLargeIcon;
8602 }
8603
8604 /**
8605 * Sets the unread conversation in a message notification.
8606 *
8607 * @param unreadConversation The unread part of the conversation this notification conveys.
8608 * @return This object for method chaining.
8609 */
8610 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
8611 mUnreadConversation = unreadConversation;
8612 return this;
8613 }
8614
8615 /**
8616 * Returns the unread conversation conveyed by this notification.
8617 * @see #setUnreadConversation(UnreadConversation)
8618 */
8619 public UnreadConversation getUnreadConversation() {
8620 return mUnreadConversation;
8621 }
8622
8623 /**
8624 * A class which holds the unread messages from a conversation.
8625 */
8626 public static class UnreadConversation {
8627 private static final String KEY_AUTHOR = "author";
8628 private static final String KEY_TEXT = "text";
8629 private static final String KEY_MESSAGES = "messages";
8630 private static final String KEY_REMOTE_INPUT = "remote_input";
8631 private static final String KEY_ON_REPLY = "on_reply";
8632 private static final String KEY_ON_READ = "on_read";
8633 private static final String KEY_PARTICIPANTS = "participants";
8634 private static final String KEY_TIMESTAMP = "timestamp";
8635
8636 private final String[] mMessages;
8637 private final RemoteInput mRemoteInput;
8638 private final PendingIntent mReplyPendingIntent;
8639 private final PendingIntent mReadPendingIntent;
8640 private final String[] mParticipants;
8641 private final long mLatestTimestamp;
8642
8643 UnreadConversation(String[] messages, RemoteInput remoteInput,
8644 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8645 String[] participants, long latestTimestamp) {
8646 mMessages = messages;
8647 mRemoteInput = remoteInput;
8648 mReadPendingIntent = readPendingIntent;
8649 mReplyPendingIntent = replyPendingIntent;
8650 mParticipants = participants;
8651 mLatestTimestamp = latestTimestamp;
8652 }
8653
8654 /**
8655 * Gets the list of messages conveyed by this notification.
8656 */
8657 public String[] getMessages() {
8658 return mMessages;
8659 }
8660
8661 /**
8662 * Gets the remote input that will be used to convey the response to a message list, or
8663 * null if no such remote input exists.
8664 */
8665 public RemoteInput getRemoteInput() {
8666 return mRemoteInput;
8667 }
8668
8669 /**
8670 * Gets the pending intent that will be triggered when the user replies to this
8671 * notification.
8672 */
8673 public PendingIntent getReplyPendingIntent() {
8674 return mReplyPendingIntent;
8675 }
8676
8677 /**
8678 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8679 * in this object's message list.
8680 */
8681 public PendingIntent getReadPendingIntent() {
8682 return mReadPendingIntent;
8683 }
8684
8685 /**
8686 * Gets the participants in the conversation.
8687 */
8688 public String[] getParticipants() {
8689 return mParticipants;
8690 }
8691
8692 /**
8693 * Gets the firs participant in the conversation.
8694 */
8695 public String getParticipant() {
8696 return mParticipants.length > 0 ? mParticipants[0] : null;
8697 }
8698
8699 /**
8700 * Gets the timestamp of the conversation.
8701 */
8702 public long getLatestTimestamp() {
8703 return mLatestTimestamp;
8704 }
8705
8706 Bundle getBundleForUnreadConversation() {
8707 Bundle b = new Bundle();
8708 String author = null;
8709 if (mParticipants != null && mParticipants.length > 1) {
8710 author = mParticipants[0];
8711 }
8712 Parcelable[] messages = new Parcelable[mMessages.length];
8713 for (int i = 0; i < messages.length; i++) {
8714 Bundle m = new Bundle();
8715 m.putString(KEY_TEXT, mMessages[i]);
8716 m.putString(KEY_AUTHOR, author);
8717 messages[i] = m;
8718 }
8719 b.putParcelableArray(KEY_MESSAGES, messages);
8720 if (mRemoteInput != null) {
8721 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8722 }
8723 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8724 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8725 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8726 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8727 return b;
8728 }
8729
8730 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8731 if (b == null) {
8732 return null;
8733 }
8734 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8735 String[] messages = null;
8736 if (parcelableMessages != null) {
8737 String[] tmp = new String[parcelableMessages.length];
8738 boolean success = true;
8739 for (int i = 0; i < tmp.length; i++) {
8740 if (!(parcelableMessages[i] instanceof Bundle)) {
8741 success = false;
8742 break;
8743 }
8744 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8745 if (tmp[i] == null) {
8746 success = false;
8747 break;
8748 }
8749 }
8750 if (success) {
8751 messages = tmp;
8752 } else {
8753 return null;
8754 }
8755 }
8756
8757 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8758 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8759
8760 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8761
8762 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8763 if (participants == null || participants.length != 1) {
8764 return null;
8765 }
8766
8767 return new UnreadConversation(messages,
8768 remoteInput,
8769 onReply,
8770 onRead,
8771 participants, b.getLong(KEY_TIMESTAMP));
8772 }
8773 };
8774
8775 /**
8776 * Builder class for {@link CarExtender.UnreadConversation} objects.
8777 */
8778 public static class Builder {
8779 private final List<String> mMessages = new ArrayList<String>();
8780 private final String mParticipant;
8781 private RemoteInput mRemoteInput;
8782 private PendingIntent mReadPendingIntent;
8783 private PendingIntent mReplyPendingIntent;
8784 private long mLatestTimestamp;
8785
8786 /**
8787 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8788 *
8789 * @param name The name of the other participant in the conversation.
8790 */
8791 public Builder(String name) {
8792 mParticipant = name;
8793 }
8794
8795 /**
8796 * Appends a new unread message to the list of messages for this conversation.
8797 *
8798 * The messages should be added from oldest to newest.
8799 *
8800 * @param message The text of the new unread message.
8801 * @return This object for method chaining.
8802 */
8803 public Builder addMessage(String message) {
8804 mMessages.add(message);
8805 return this;
8806 }
8807
8808 /**
8809 * Sets the pending intent and remote input which will convey the reply to this
8810 * notification.
8811 *
8812 * @param pendingIntent The pending intent which will be triggered on a reply.
8813 * @param remoteInput The remote input parcelable which will carry the reply.
8814 * @return This object for method chaining.
8815 *
8816 * @see CarExtender.UnreadConversation#getRemoteInput
8817 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8818 */
8819 public Builder setReplyAction(
8820 PendingIntent pendingIntent, RemoteInput remoteInput) {
8821 mRemoteInput = remoteInput;
8822 mReplyPendingIntent = pendingIntent;
8823
8824 return this;
8825 }
8826
8827 /**
8828 * Sets the pending intent that will be sent once the messages in this notification
8829 * are read.
8830 *
8831 * @param pendingIntent The pending intent to use.
8832 * @return This object for method chaining.
8833 */
8834 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8835 mReadPendingIntent = pendingIntent;
8836 return this;
8837 }
8838
8839 /**
8840 * Sets the timestamp of the most recent message in an unread conversation.
8841 *
8842 * If a messaging notification has been posted by your application and has not
8843 * yet been cancelled, posting a later notification with the same id and tag
8844 * but without a newer timestamp may result in Android Auto not displaying a
8845 * heads up notification for the later notification.
8846 *
8847 * @param timestamp The timestamp of the most recent message in the conversation.
8848 * @return This object for method chaining.
8849 */
8850 public Builder setLatestTimestamp(long timestamp) {
8851 mLatestTimestamp = timestamp;
8852 return this;
8853 }
8854
8855 /**
8856 * Builds a new unread conversation object.
8857 *
8858 * @return The new unread conversation object.
8859 */
8860 public UnreadConversation build() {
8861 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8862 String[] participants = { mParticipant };
8863 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8864 mReadPendingIntent, participants, mLatestTimestamp);
8865 }
8866 }
8867 }
8868
8869 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008870 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8871 * with a TV extension:
8872 *
8873 * <ol>
8874 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8875 * <li>Create a {@link TvExtender}.
8876 * <li>Set TV-specific properties using the {@code set} methods of
8877 * {@link TvExtender}.
8878 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8879 * to apply the extension to a notification.
8880 * </ol>
8881 *
8882 * <pre class="prettyprint">
8883 * Notification notification = new Notification.Builder(context)
8884 * ...
8885 * .extend(new TvExtender()
8886 * .set*(...))
8887 * .build();
8888 * </pre>
8889 *
8890 * <p>TV extensions can be accessed on an existing notification by using the
8891 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8892 * to access values.
8893 *
8894 * @hide
8895 */
8896 @SystemApi
8897 public static final class TvExtender implements Extender {
8898 private static final String TAG = "TvExtender";
8899
8900 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8901 private static final String EXTRA_FLAGS = "flags";
8902 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8903 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008904 private static final String EXTRA_CHANNEL_ID = "channel_id";
Rhiannon Malia1a083932018-01-24 15:02:30 -08008905 private static final String EXTRA_SUPPRESS_SHOW_OVER_APPS = "suppressShowOverApps";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008906
8907 // Flags bitwise-ored to mFlags
8908 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8909
8910 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008911 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008912 private PendingIntent mContentIntent;
8913 private PendingIntent mDeleteIntent;
Rhiannon Malia1a083932018-01-24 15:02:30 -08008914 private boolean mSuppressShowOverApps;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008915
8916 /**
8917 * Create a {@link TvExtender} with default options.
8918 */
8919 public TvExtender() {
8920 mFlags = FLAG_AVAILABLE_ON_TV;
8921 }
8922
8923 /**
8924 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8925 *
8926 * @param notif The notification from which to copy options.
8927 */
8928 public TvExtender(Notification notif) {
8929 Bundle bundle = notif.extras == null ?
8930 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8931 if (bundle != null) {
8932 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008933 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008934 mSuppressShowOverApps = bundle.getBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008935 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8936 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8937 }
8938 }
8939
8940 /**
8941 * Apply a TV extension to a notification that is being built. This is typically called by
8942 * the {@link Notification.Builder#extend(Notification.Extender)}
8943 * method of {@link Notification.Builder}.
8944 */
8945 @Override
8946 public Notification.Builder extend(Notification.Builder builder) {
8947 Bundle bundle = new Bundle();
8948
8949 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008950 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Rhiannon Malia1a083932018-01-24 15:02:30 -08008951 bundle.putBoolean(EXTRA_SUPPRESS_SHOW_OVER_APPS, mSuppressShowOverApps);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008952 if (mContentIntent != null) {
8953 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8954 }
8955
8956 if (mDeleteIntent != null) {
8957 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8958 }
8959
8960 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8961 return builder;
8962 }
8963
8964 /**
8965 * Returns true if this notification should be shown on TV. This method return true
8966 * if the notification was extended with a TvExtender.
8967 */
8968 public boolean isAvailableOnTv() {
8969 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8970 }
8971
8972 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008973 * Specifies the channel the notification should be delivered on when shown on TV.
8974 * It can be different from the channel that the notification is delivered to when
8975 * posting on a non-TV device.
8976 */
8977 public TvExtender setChannel(String channelId) {
8978 mChannelId = channelId;
8979 return this;
8980 }
8981
8982 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008983 * Specifies the channel the notification should be delivered on when shown on TV.
8984 * It can be different from the channel that the notification is delivered to when
8985 * posting on a non-TV device.
8986 */
8987 public TvExtender setChannelId(String channelId) {
8988 mChannelId = channelId;
8989 return this;
8990 }
8991
Jeff Sharkey000ce802017-04-29 13:13:27 -06008992 /** @removed */
8993 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008994 public String getChannel() {
8995 return mChannelId;
8996 }
8997
8998 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008999 * Returns the id of the channel this notification posts to on TV.
9000 */
9001 public String getChannelId() {
9002 return mChannelId;
9003 }
9004
9005 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009006 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
9007 * If provided, it is used instead of the content intent specified
9008 * at the level of Notification.
9009 */
9010 public TvExtender setContentIntent(PendingIntent intent) {
9011 mContentIntent = intent;
9012 return this;
9013 }
9014
9015 /**
9016 * Returns the TV-specific content intent. If this method returns null, the
9017 * main content intent on the notification should be used.
9018 *
9019 * @see {@link Notification#contentIntent}
9020 */
9021 public PendingIntent getContentIntent() {
9022 return mContentIntent;
9023 }
9024
9025 /**
9026 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
9027 * by the user on TV. If provided, it is used instead of the delete intent specified
9028 * at the level of Notification.
9029 */
9030 public TvExtender setDeleteIntent(PendingIntent intent) {
9031 mDeleteIntent = intent;
9032 return this;
9033 }
9034
9035 /**
9036 * Returns the TV-specific delete intent. If this method returns null, the
9037 * main delete intent on the notification should be used.
9038 *
9039 * @see {@link Notification#deleteIntent}
9040 */
9041 public PendingIntent getDeleteIntent() {
9042 return mDeleteIntent;
9043 }
Rhiannon Malia1a083932018-01-24 15:02:30 -08009044
9045 /**
9046 * Specifies whether this notification should suppress showing a message over top of apps
9047 * outside of the launcher.
9048 */
9049 public TvExtender setSuppressShowOverApps(boolean suppress) {
9050 mSuppressShowOverApps = suppress;
9051 return this;
9052 }
9053
9054 /**
9055 * Returns true if this notification should not show messages over top of apps
9056 * outside of the launcher.
9057 */
9058 public boolean getSuppressShowOverApps() {
9059 return mSuppressShowOverApps;
9060 }
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08009061 }
9062
9063 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07009064 * Get an array of Notification objects from a parcelable array bundle field.
9065 * Update the bundle to have a typed array so fetches in the future don't need
9066 * to do an array copy.
9067 */
9068 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
9069 Parcelable[] array = bundle.getParcelableArray(key);
9070 if (array instanceof Notification[] || array == null) {
9071 return (Notification[]) array;
9072 }
9073 Notification[] typedArray = Arrays.copyOf(array, array.length,
9074 Notification[].class);
9075 bundle.putParcelableArray(key, typedArray);
9076 return typedArray;
9077 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02009078
9079 private static class BuilderRemoteViews extends RemoteViews {
9080 public BuilderRemoteViews(Parcel parcel) {
9081 super(parcel);
9082 }
9083
Kenny Guy77320062014-08-27 21:37:15 +01009084 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
9085 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02009086 }
9087
9088 @Override
9089 public BuilderRemoteViews clone() {
9090 Parcel p = Parcel.obtain();
9091 writeToParcel(p, 0);
9092 p.setDataPosition(0);
9093 BuilderRemoteViews brv = new BuilderRemoteViews(p);
9094 p.recycle();
9095 return brv;
9096 }
9097 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08009098
9099 private static class StandardTemplateParams {
9100 boolean hasProgress = true;
9101 boolean ambient = false;
9102 CharSequence title;
9103 CharSequence text;
Selim Cinekafeed292017-12-12 17:32:44 -08009104 CharSequence headerTextSecondary;
Selim Cinek88188f22017-09-19 16:46:56 -07009105 boolean hideLargeIcon;
9106 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009107
9108 final StandardTemplateParams reset() {
9109 hasProgress = true;
9110 ambient = false;
9111 title = null;
9112 text = null;
Selim Cinekafeed292017-12-12 17:32:44 -08009113 headerTextSecondary = null;
Adrian Roos70d7aa32017-01-11 15:39:06 -08009114 return this;
9115 }
9116
9117 final StandardTemplateParams hasProgress(boolean hasProgress) {
9118 this.hasProgress = hasProgress;
9119 return this;
9120 }
9121
9122 final StandardTemplateParams title(CharSequence title) {
9123 this.title = title;
9124 return this;
9125 }
9126
9127 final StandardTemplateParams text(CharSequence text) {
9128 this.text = text;
9129 return this;
9130 }
9131
Selim Cinekafeed292017-12-12 17:32:44 -08009132 final StandardTemplateParams headerTextSecondary(CharSequence text) {
9133 this.headerTextSecondary = text;
9134 return this;
9135 }
9136
Selim Cinek88188f22017-09-19 16:46:56 -07009137 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
9138 this.alwaysShowReply = alwaysShowReply;
9139 return this;
9140 }
9141
9142 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
9143 this.hideLargeIcon = hideLargeIcon;
9144 return this;
9145 }
9146
Adrian Roos70d7aa32017-01-11 15:39:06 -08009147 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08009148 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08009149 this.ambient = ambient;
9150 return this;
9151 }
9152
9153 final StandardTemplateParams fillTextsFrom(Builder b) {
9154 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07009155 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
9156
9157 // Big text notifications should contain their content when viewed in ambient mode.
9158 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
9159 if (!ambient || TextUtils.isEmpty(text)) {
9160 text = extras.getCharSequence(EXTRA_TEXT);
9161 }
9162 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08009163 return this;
9164 }
9165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009166}